private void OnSelectionChangedInCpuProfilerModule(IProfilerFrameTimeViewSampleSelectionController controller, ProfilerTimeSampleSelection selection) { if (controller == m_CpuProfilerModule && !m_SendingSelectionEventToProfilerWindowInProgress) { if (selection != null && selection.markerNamePath != null && selection.markerNamePath.Count > 0) { selectedMarkerChanged(selection.markerNamePath[selection.markerNamePath.Count - 1], selection.threadGroupName, selection.threadName); } } }
private void GetProfiler() { if (m_ProfilerWindow == null) { m_ProfilerWindow = EditorWindow.GetWindow <ProfilerWindow>(); } if (m_CpuProfilerModule == null) { m_CpuProfilerModule = m_ProfilerWindow.GetFrameTimeViewSampleSelectionController(ProfilerWindow.cpuModuleIdentifier); } }
/// <summary> /// Search for a sample fitting the '/' seperated path to it and select it /// </summary> /// <param name="markerNameOrMarkerNamePath">'/' seperated path to the marker </param> /// <param name="frameIndex"> The frame to make the selection in, or -1 to select in currently active frame. </param> /// <param name="threadIndex"> The index of the thread to find the sample in. </param> /// <returns></returns> public static bool SetSelection(this IProfilerFrameTimeViewSampleSelectionController controller, string markerNameOrMarkerNamePath, long frameIndex = FrameDataView.invalidOrCurrentFrameIndex, string threadGroupName = CPUProfilerModule.mainThreadGroupName, string threadName = CPUProfilerModule.mainThreadName, ulong threadId = FrameDataView.invalidThreadId) { var iController = controller as IProfilerFrameTimeViewSampleSelectionControllerInternal; if (controller == null || iController == null) { throw new ArgumentNullException($"{nameof(controller)}", $"The IProfilerFrameTimeViewSampleSelectionController you are setting a selection on can't be null."); } ProfilerTimeSampleSelection selection; List <int> markerIdPath; using (CPUOrGPUProfilerModule.setSelectionIntegrityCheckMarker.Auto()) { // this could've come from anywhere, check the inputs first if (string.IsNullOrEmpty(markerNameOrMarkerNamePath)) { throw new ArgumentException($"{nameof(markerNameOrMarkerNamePath)} can't be null or empty. Hint: To clear a selection, use {nameof(IProfilerFrameTimeViewSampleSelectionController.ClearSelection)} instead."); } if (frameIndex == FrameDataView.invalidOrCurrentFrameIndex) { frameIndex = iController.GetActiveVisibleFrameIndexOrLatestFrameForSettingTheSelection(); } var threadIndex = CPUOrGPUProfilerModule.IntegrityCheckFrameAndThreadDataOfSelection(frameIndex, threadGroupName, threadName, ref threadId); int lastSlashIndex = markerNameOrMarkerNamePath.LastIndexOf('/'); string sampleName = lastSlashIndex == -1 ? markerNameOrMarkerNamePath : markerNameOrMarkerNamePath.Substring(lastSlashIndex + 1, markerNameOrMarkerNamePath.Length - (lastSlashIndex + 1)); if (lastSlashIndex == -1)// no path provided? just find the first sample { markerNameOrMarkerNamePath = null; } int selectedSampleRawIndex = iController.FindMarkerPathAndRawSampleIndexToFirstMatchingSampleInCurrentView((int)frameIndex, 0, sampleName, out markerIdPath, markerNameOrMarkerNamePath); if (selectedSampleRawIndex < 0) { return(false); } selection = new ProfilerTimeSampleSelection(frameIndex, threadGroupName, threadName, threadId, selectedSampleRawIndex, sampleName); } using (CPUOrGPUProfilerModule.setSelectionApplyMarker.Auto()) { // looks good, apply selection.frameIndexIsSafe = true; iController.SetSelectionWithoutIntegrityChecks(selection, markerIdPath); return(true); } }
public void OnDisable() { if (m_ProfilerWindow != null) { m_ProfilerWindow = null; } #if UNITY_2021_1_OR_NEWER if (m_CpuProfilerModule != null) { m_CpuProfilerModule.selectionChanged -= OnSelectionChangedInCpuProfilerModule; m_CpuProfilerModule = null; } #endif }
public void OpenProfilerOrUseExisting() { // Note we use existing if possible to fix a bug after domain reload // Where calling EditorWindow.GetWindow directly causes a second window to open if (m_ProfilerWindow == null) { #if UNITY_2021_1_OR_NEWER m_ProfilerWindow = EditorWindow.GetWindow <ProfilerWindow>(); m_CpuProfilerModule = m_ProfilerWindow.GetFrameTimeViewSampleSelectionController(ProfilerWindow.cpuModuleName); m_CpuProfilerModule.selectionChanged -= OnSelectionChangedInCpuProfilerModule; m_CpuProfilerModule.selectionChanged += OnSelectionChangedInCpuProfilerModule; #else // Create new m_ProfilerWindow = EditorWindow.GetWindow(m_ProfilerWindowType); #endif } }
// selects first occurence of this sample in the given frame and thread (and optionally given the markerNamePath leading up to it or a (grand, (grand)...) parent of it) public static bool SetSelection(this IProfilerFrameTimeViewSampleSelectionController controller, long frameIndex, string threadGroupName, string threadName, string sampleName, string markerNamePath = null, ulong threadId = FrameDataView.invalidThreadId) { var iController = controller as IProfilerFrameTimeViewSampleSelectionControllerInternal; if (controller == null || iController == null) { throw new ArgumentNullException($"{nameof(controller)}", $"The IProfilerFrameTimeViewSampleSelectionController you are setting a selection on can't be null."); } ProfilerTimeSampleSelection selection; List <int> markerIdPath; using (CPUOrGPUProfilerModule.setSelectionIntegrityCheckMarker.Auto()) { // this could've come from anywhere, check the inputs first if (string.IsNullOrEmpty(sampleName)) { throw new ArgumentException($"{nameof(sampleName)} can't be null or empty. Hint: To clear a selection, use {nameof(IProfilerFrameTimeViewSampleSelectionController.ClearSelection)} instead."); } var threadIndex = CPUOrGPUProfilerModule.IntegrityCheckFrameAndThreadDataOfSelection(frameIndex, threadGroupName, threadName, ref threadId); int selectedSampleRawIndex = iController.FindMarkerPathAndRawSampleIndexToFirstMatchingSampleInCurrentView((int)frameIndex, threadIndex, sampleName, out markerIdPath, markerNamePath); if (selectedSampleRawIndex < 0) { return(false); } selection = new ProfilerTimeSampleSelection(frameIndex, threadGroupName, threadName, threadId, selectedSampleRawIndex, sampleName); } using (CPUOrGPUProfilerModule.setSelectionApplyMarker.Auto()) { // looks good, apply selection.frameIndexIsSafe = true; iController.SetSelectionWithoutIntegrityChecks(selection, markerIdPath); return(true); } }
public void GetProfilerWindowHandle() { Profiler.BeginSample("GetProfilerWindowHandle"); #if UNITY_2021_1_OR_NEWER if (m_CpuProfilerModule != null) { m_CpuProfilerModule.selectionChanged -= OnSelectionChangedInCpuProfilerModule; m_CpuProfilerModule = null; } var windows = Resources.FindObjectsOfTypeAll <ProfilerWindow>(); if (windows != null && windows.Length > 0) { m_ProfilerWindow = windows[0]; } if (m_ProfilerWindow != null) { m_CpuProfilerModule = m_ProfilerWindow.GetFrameTimeViewSampleSelectionController(ProfilerWindow.cpuModuleName); m_CpuProfilerModule.selectionChanged -= OnSelectionChangedInCpuProfilerModule; m_CpuProfilerModule.selectionChanged += OnSelectionChangedInCpuProfilerModule; m_ProfilerWindow.Repaint(); m_ProfilerWindowInitialized = false; // wait a frame for the Profiler to get Repainted EditorApplication.delayCall += () => m_ProfilerWindowInitialized = true; } #else UnityEngine.Object[] windows = Resources.FindObjectsOfTypeAll(m_ProfilerWindowType); if (windows != null && windows.Length > 0) { m_ProfilerWindow = (EditorWindow)windows[0]; } m_ProfilerWindowInitialized = true; #endif Profiler.EndSample(); }
public bool SetProfilerWindowMarkerName(string markerName, List <string> threadFilters) { m_SendingSelectionEventToProfilerWindowInProgress = true; if (m_ProfilerWindow == null) { return(false); } #if UNITY_2021_1_OR_NEWER m_CpuProfilerModule = m_ProfilerWindow.GetFrameTimeViewSampleSelectionController(ProfilerWindow.cpuModuleName); if (m_CpuProfilerModule != null && m_ProfilerWindow.selectedModuleName == ProfilerWindow.cpuModuleName && m_ProfilerWindow.firstAvailableFrameIndex >= 0) { // Read profiler data direct from profile to find time/duration int currentFrameIndex = (int)m_ProfilerWindow.selectedFrameIndex; var iterator = GetNextThreadIndexFittingThreadFilters(currentFrameIndex, threadFilters); while (iterator.MoveNext()) { using (var rawFrameDataView = ProfilerDriver.GetRawFrameDataView(currentFrameIndex, iterator.Current.threadIndex)) { if (m_CpuProfilerModule.SetSelection(currentFrameIndex, rawFrameDataView.threadGroupName, rawFrameDataView.threadName, markerName, threadId: rawFrameDataView.threadId)) { m_ProfilerWindow.Repaint(); m_SendingSelectionEventToProfilerWindowInProgress = false; return(true); // setting the selection was successful, nothing more to do here. } } } // selection couldn't be found, so clear the current one to avoid confusion m_CpuProfilerModule.ClearSelection(); m_ProfilerWindow.Repaint(); } #else var timeLineGUI = GetTimeLineGUI(); if (timeLineGUI == null) { m_SendingSelectionEventToProfilerWindowInProgress = false; return(false); } if (m_SelectedEntryFieldInfo != null) { var selectedEntry = m_SelectedEntryFieldInfo.GetValue(timeLineGUI); if (selectedEntry != null) { // Read profiler data direct from profile to find time/duration int currentFrameIndex = (int)m_CurrentFrameFieldInfo.GetValue(m_ProfilerWindow); float time; float duration; int instanceId; int threadIndex; if (GetMarkerInfo(markerName, currentFrameIndex, threadFilters, out threadIndex, out time, out duration, out instanceId)) { /* * Debug.Log(string.Format("Setting profiler to {0} on {1} at frame {2} at {3}ms for {4}ms ({5})", * markerName, currentFrameIndex, threadFilter, time, duration, instanceId)); */ if (m_SelectedNameFieldInfo != null) { m_SelectedNameFieldInfo.SetValue(selectedEntry, markerName); } if (m_SelectedTimeFieldInfo != null) { m_SelectedTimeFieldInfo.SetValue(selectedEntry, time); } if (m_SelectedDurationFieldInfo != null) { m_SelectedDurationFieldInfo.SetValue(selectedEntry, duration); } if (m_SelectedInstanceIdFieldInfo != null) { m_SelectedInstanceIdFieldInfo.SetValue(selectedEntry, instanceId); } if (m_SelectedFrameIdFieldInfo != null) { m_SelectedFrameIdFieldInfo.SetValue(selectedEntry, currentFrameIndex); } if (m_SelectedThreadIndexFieldInfo != null) { m_SelectedThreadIndexFieldInfo.SetValue(selectedEntry, threadIndex); } // TODO : Update to fill in the total and number of instances. // For now we force Instance count to 1 to avoid the incorrect info showing. if (m_SelectedInstanceCountFieldInfo != null) { m_SelectedInstanceCountFieldInfo.SetValue(selectedEntry, 1); } // Set other values to non negative values so selection appears if (m_SelectedNativeIndexFieldInfo != null) { m_SelectedNativeIndexFieldInfo.SetValue(selectedEntry, currentFrameIndex); } m_ProfilerWindow.Repaint(); m_SendingSelectionEventToProfilerWindowInProgress = false; return(true); } } } #endif m_SendingSelectionEventToProfilerWindowInProgress = false; return(false); }