Esempio n. 1
0
        public void EndMyProfilingBlock(int id, bool memoryProfiling)
        {
            if (EnableAsserts) //Check that thread which begun this block is same which ends it
            {
                System.Diagnostics.Debug.Assert(m_managedThreadId == Thread.CurrentThread.ManagedThreadId);
            }

            if (LevelSkipCount > 0)
            {
                LevelSkipCount--;
                return;
            }

            MyProfilerBlock profilingBlock = m_currentProfilingStack.Pop();

            if (profilingBlock != null)
            {
                if (EnableAsserts && id != -1)
                {
                    System.Diagnostics.Debug.Assert(profilingBlock.Id == id, "Corrupted profilling stack, mismatched IDs");
                }

                profilingBlock.End(memoryProfiling);
            }
            else
            if (EnableAsserts)
            {
                System.Diagnostics.Debug.Assert(false, "Corrupted profilling stack");
            }
        }
        public static MyObjectBuilder_ProfilerBlock GetObjectBuilder(MyProfilerBlock profilerBlock)
        {
            MyProfilerBlock.MyProfilerBlockObjectBuilderInfo data = profilerBlock.GetObjectBuilderInfo();
            MyObjectBuilder_ProfilerBlock objectBuilder           = new MyObjectBuilder_ProfilerBlock();

            objectBuilder.Id  = data.Id;
            objectBuilder.Key = data.Key;

            objectBuilder.Invalid            = data.Invalid;
            objectBuilder.TimeFormat         = data.TimeFormat;
            objectBuilder.ValueFormat        = data.ValueFormat;
            objectBuilder.CallFormat         = data.CallFormat;
            objectBuilder.ProcessMemory      = data.ProcessMemory;
            objectBuilder.ManagedMemoryBytes = data.ManagedMemoryBytes;
            objectBuilder.Miliseconds        = data.Miliseconds;
            objectBuilder.CustomValues       = data.CustomValues;
            objectBuilder.NumCallsArray      = data.NumCallsArray;

            objectBuilder.Children = new List <MyProfilerBlockKey>();
            foreach (var child in data.Children)
            {
                objectBuilder.Children.Add(child.Key);
            }
            if (data.Parent != null)
            {
                objectBuilder.Parent = data.Parent.Key;
            }

            return(objectBuilder);
        }
        public static MyProfilerBlock Init(MyObjectBuilder_ProfilerBlock objectBuilder, MyProfiler.MyProfilerObjectBuilderInfo profiler)
        {
            MyProfilerBlock.MyProfilerBlockObjectBuilderInfo data = new MyProfilerBlock.MyProfilerBlockObjectBuilderInfo();

            data.Id  = objectBuilder.Id;
            data.Key = objectBuilder.Key;

            data.Invalid            = objectBuilder.Invalid;
            data.TimeFormat         = objectBuilder.TimeFormat;
            data.ValueFormat        = objectBuilder.ValueFormat;
            data.CallFormat         = objectBuilder.CallFormat;
            data.ProcessMemory      = objectBuilder.ProcessMemory;
            data.ManagedMemoryBytes = objectBuilder.ManagedMemoryBytes;
            data.Miliseconds        = objectBuilder.Miliseconds;
            data.CustomValues       = objectBuilder.CustomValues;
            data.NumCallsArray      = objectBuilder.NumCallsArray;

            data.Children = new List <MyProfilerBlock>();
            foreach (MyProfilerBlockKey child in objectBuilder.Children)
            {
                data.Children.Add(profiler.ProfilingBlocks[child]);
            }
            if (objectBuilder.Parent.File != null)
            {
                data.Parent = profiler.ProfilingBlocks[objectBuilder.Parent];
            }

            MyProfilerBlock profilerBlock = profiler.ProfilingBlocks[data.Key];

            profilerBlock.Init(data);

            return(profilerBlock);
        }
Esempio n. 4
0
        public static MyProfilerBlock FindBlockByMax(int frameIndex, int lastValidFrame)
        {
            if (!IsValidIndex(frameIndex, lastValidFrame))
            {
                return(null);
            }

            float           max   = float.MinValue;
            MyProfilerBlock block = null;

            var children = m_selectedProfiler.SelectedRootChildren;

            for (int i = 0; i < children.Count; i++)
            {
                var   profilerBlock = children[i];
                float val           = profilerBlock.Miliseconds[frameIndex];
                if (val > max)
                {
                    max   = val;
                    block = profilerBlock;
                }
            }
            return(block);
        }
Esempio n. 5
0
 static MyRenderProfiler()
 {
     m_levelLimit = VRage.MyCompilationSymbols.ProfileFromStart ? -1 : 0;
     // Create block, some unique id
     m_fpsBlock = MyProfiler.CreateExternalBlock("FPS", -2);
 }
        void DrawEvent(float textPosY, MyProfilerBlock profilerBlock, int blockIndex, int frameIndex, int lastValidFrame, ref Color color)
        {
            float miliseconds = 0;
            long managedMemory = 0;
            float processMemory = 0;
            int numCalls = -1; // To show update window in profiler
            float customValue = 0;

            if (IsValidIndex(frameIndex, lastValidFrame))
            {
                miliseconds = profilerBlock.Miliseconds[frameIndex];
                managedMemory = profilerBlock.ManagedMemoryBytes[frameIndex];
                processMemory = profilerBlock.ProcessMemory[frameIndex];
                numCalls = profilerBlock.NumCallsArray[frameIndex];
                customValue = profilerBlock.CustomValues[frameIndex];
            }

            float Y_TEXT_POSITION = ViewportSize.Y / 2;

            float textScale = 0.7f;

            if (blockIndex >= 0)
                m_text.Clear().Append(blockIndex + 1).Append(" ").Append(profilerBlock.Name);
            else
                m_text.Clear().Append("- ").Append(profilerBlock.Name);
            DrawTextShadow(new Vector2(20, textPosY), m_text, color, textScale);

            float length = 500;

            m_text.Clear();
            m_text.Append("(").Append(profilerBlock.Children.Count).Append(") ");
            DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);
            length += 50 * textScale;

            m_text.Clear();
            //text.Append(((index != -1 ? profilerBlock.TimePercentage[index] : profilerBlock.averagePctg)).ToString("#,#0.0%"));
            //MyDebugDraw.DrawTextShadow(new Vector2(20 + length, textPosY), text, color, textScale);
            length += 155 * textScale;

            m_text.Clear();
            m_text.ConcatFormat(profilerBlock.TimeFormat ?? "{0:.00}ms", miliseconds);
            DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);
            length += 155 * textScale;

            m_text.Clear();
            if (managedMemory < 1024 && managedMemory > -1024) // Still in bytes?
            {
                m_text.Append(managedMemory.ToString()).Append(" B");
            }
            else if (managedMemory < 1048576 && managedMemory > -1048576) // Still in kilobytes?
            {
                float managedMemoryKB = managedMemory / 1024f;
                m_text.Concat(managedMemoryKB, 3).Append(" KB");
            }
            else // Else display in megabytes
            {
                float managedMemoryKB = managedMemory / 1048576;
                m_text.Concat(managedMemoryKB, 3).Append(" MB");
            }
            DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);
            length += 40 + 158 * textScale;

            m_text.Clear();
            if (MemoryProfiling)
            {
                m_text.Concat(processMemory, 3).Append(" MB");
                DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);
                length += 158 * textScale;

                m_text.Clear();
            }

            length += 40 + 40 * textScale;
            m_text.ConcatFormat(profilerBlock.CallFormat ?? "{0} calls", numCalls);
            DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);

            length += 150 * textScale;
            m_text.Clear();
            m_text.ConcatFormat(profilerBlock.ValueFormat ?? "Custom: {0:.00}", customValue);
            DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);

            int maxIndex;
            length += 250 * textScale;
            float max = FindMaxWrap(profilerBlock.Miliseconds, frameIndex - m_frameLocalArea / 2, frameIndex + m_frameLocalArea / 2, lastValidFrame, out maxIndex);
            m_text.Clear();
            m_text.ConcatFormat(profilerBlock.TimeFormat ?? "{0:.00}ms", max);
            DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);

            length += DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);
        }
Esempio n. 7
0
        void CheckEndBlock(MyProfilerBlock profilingBlock, string member, string file, int parentId)
        {
            if (EnableAsserts && !profilingBlock.Key.Member.Equals(member) || profilingBlock.Key.ParentId != parentId || profilingBlock.Key.File != file)
            {
                var trace = new StackTrace(2, true);
                for (int i = 0; i < trace.FrameCount; i++)
                {
                    var frame = trace.GetFrame(i);
                    if (frame.GetFileName() == profilingBlock.Key.File && frame.GetMethod().Name == member)
                    {
                        Debug.Fail(String.Format("Premature call to EndProfilingBlock in {0}({1}){2}", file, frame.GetFileLineNumber(), Environment.NewLine));
                        return;
                    }
                }

                Debug.Fail(String.Format("Profiling end block missing for '{0}'{1}File: {2}({3}){1}", profilingBlock.Key.Name, Environment.NewLine, profilingBlock.Key.File, profilingBlock.Key.Line));
            }
        }
Esempio n. 8
0
        public int StartMyProfilingBlock(string name, bool memoryProfiling, float customValue = 0)
        {
            if (m_managedThreadId != Thread.CurrentThread.ManagedThreadId)
            {
                return(-1);
            }

            //lock (m_lock)
            {
                if (MyProfiler.EnableAsserts)
                {        /*
                          * foreach (MyProfilerBlock block in m_currentProfilingStack)
                          * {
                          * if (block.Name.ToString() == name)
                          * {
                          * System.Diagnostics.Debug.Assert(false, "Event is already in stack!");
                          * return -1;
                          * }
                          * }      */
                }

                MyProfilerBlock profilingBlock = null;
                int             key;

                unchecked
                {
                    key = (397 * name.GetHashCode()) ^ GetParentHash();
                }

                int id = 1;

                //if (!m_profilingBlocks.TryGetValue(m_nameComposer.ToString(), out profilingBlock))
                if (!m_profilingBlocks.TryGetValue(key, out profilingBlock))
                {
                    profilingBlock = new MyProfilerBlock(name /*m_nameComposer.ToString()*/);

                    if (m_currentProfilingStack.Count > 0)
                    {
                        MyProfilerBlock parentBlock = m_currentProfilingStack.Peek();
                        if (!parentBlock.Children.Contains(profilingBlock))
                        {
                            parentBlock.Children.Add(profilingBlock);
                        }

                        profilingBlock.Parent = m_currentProfilingStack.Peek();
                    }

                    m_profilingBlocks.Add(key, profilingBlock);
                    profilingBlock.Id = m_profilingBlocks.Count - 1;
                }

                id = profilingBlock.Id;

                profilingBlock.Start(memoryProfiling);
                profilingBlock.CustomValue = customValue;

                m_currentProfilingStack.Push(profilingBlock);

                return(id);
            }
        }
        void DrawEvent(float textPosY, MyProfilerBlock profilerBlock, int blockIndex, int frameIndex, int lastValidFrame, ref Color color)
        {
            float miliseconds   = 0;
            long  managedMemory = 0;
            float processMemory = 0;
            int   numCalls      = -1; // To show update window in profiler
            float customValue   = 0;

            if (IsValidIndex(frameIndex, lastValidFrame))
            {
                miliseconds   = profilerBlock.Miliseconds[frameIndex];
                managedMemory = profilerBlock.ManagedMemoryBytes[frameIndex];
                processMemory = profilerBlock.ProcessMemory[frameIndex];
                numCalls      = profilerBlock.NumCallsArray[frameIndex];
                customValue   = profilerBlock.CustomValues[frameIndex];
            }

            float Y_TEXT_POSITION = ViewportSize.Y / 2;

            float textScale = 0.7f;

            if (blockIndex >= 0)
            {
                m_text.Clear().Append(blockIndex + 1).Append(" ").Append(profilerBlock.Name);
            }
            else
            {
                m_text.Clear().Append("- ").Append(profilerBlock.Name);
            }
            DrawTextShadow(new Vector2(20, textPosY), m_text, color, textScale);

            float length = 500;

            m_text.Clear();
            m_text.Append("(").Append(profilerBlock.Children.Count).Append(") ");
            DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);
            length += 50 * textScale;

            m_text.Clear();
            //text.Append(((index != -1 ? profilerBlock.TimePercentage[index] : profilerBlock.averagePctg)).ToString("#,#0.0%"));
            //MyDebugDraw.DrawTextShadow(new Vector2(20 + length, textPosY), text, color, textScale);
            length += 155 * textScale;

            m_text.Clear();
            m_text.ConcatFormat(profilerBlock.TimeFormat ?? "{0:.00}ms", miliseconds);
            DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);
            length += 155 * textScale;

            m_text.Clear();
            if (managedMemory < 1024 && managedMemory > -1024) // Still in bytes?
            {
                m_text.Append(managedMemory.ToString()).Append(" B");
            }
            else if (managedMemory < 1048576 && managedMemory > -1048576) // Still in kilobytes?
            {
                float managedMemoryKB = managedMemory / 1024f;
                m_text.Concat(managedMemoryKB, 3).Append(" KB");
            }
            else // Else display in megabytes
            {
                float managedMemoryKB = managedMemory / 1048576;
                m_text.Concat(managedMemoryKB, 3).Append(" MB");
            }
            DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);
            length += 40 + 158 * textScale;

            m_text.Clear();
            if (MemoryProfiling)
            {
                m_text.Concat(processMemory, 3).Append(" MB");
                DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);
                length += 158 * textScale;

                m_text.Clear();
            }

            length += 40 + 40 * textScale;
            m_text.ConcatFormat(profilerBlock.CallFormat ?? "{0} calls", numCalls);
            DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);

            length += 150 * textScale;
            m_text.Clear();
            m_text.ConcatFormat(profilerBlock.ValueFormat ?? "Custom: {0:.00}", customValue);
            DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);

            int maxIndex;

            length += 250 * textScale;
            float max = FindMaxWrap(profilerBlock.Miliseconds, frameIndex - m_frameLocalArea / 2, frameIndex + m_frameLocalArea / 2, lastValidFrame, out maxIndex);

            m_text.Clear();
            m_text.ConcatFormat(profilerBlock.TimeFormat ?? "{0:.00}ms", max);
            DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);

            length += DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);
        }
        protected sealed override void Draw(MyProfiler drawProfiler, int lastFrameIndex, int frameToDraw)
        {
            Debug.Assert(frameToDraw >= 0 && frameToDraw < MyProfiler.MAX_FRAMES, "Invalid selected frame");

            if (!m_initialized)
            {
                // Init linebatch
                Init();
                m_initialized = true;
                m_fpsBlock.Start(false);
            }

            // Handle FPS timer
            m_fpsBlock.End(false);

            float elapsedTime    = (float)m_fpsBlock.Elapsed.Seconds;
            float invElapsedTime = elapsedTime > 0 ? 1 / elapsedTime : 0;

            m_fpsPctg = 0.9f * m_fpsPctg + 0.1f * invElapsedTime;

            if (MemoryProfiling)
            {
                // Handle memory usage for frame
                float processDeltaMB = m_fpsBlock.ProcessDeltaMB;
                m_fpsBlock.ProcessMemory[lastFrameIndex] = processDeltaMB;
            }

            long managedDeltaMB = m_fpsBlock.ManagedDeltaMB;

            m_fpsBlock.ManagedMemoryBytes[lastFrameIndex] = managedDeltaMB;
            m_fpsBlock.CustomValues[lastFrameIndex]       = m_fpsBlock.CustomValue;

            m_fpsBlock.Reset();

            m_fpsBlock.Start(false);

            if (m_enabled)
            {
                // Draw events as text
                float eventLineSize     = 20;
                float largeTextLineSize = 28;
                float textOffsetY       = ViewportSize.Y / 2 - 8 * largeTextLineSize;

                // Draw thread name and level limit
                m_text.Clear();
                m_text.ConcatFormat("\"{2}\" ({0}/{1})", m_threadProfilers.IndexOf(m_selectedProfiler) + 1, m_threadProfilers.Count, m_selectedProfiler.DisplayedName).AppendLine();
                m_text.Append("Level limit: ").AppendInt32(m_selectedProfiler.LevelLimit).AppendLine();
                DrawText(new Vector2(20, textOffsetY), m_text, Color.LightGray, 1);
                textOffsetY += largeTextLineSize * 2 + 10;

                // Draw frame number and local area
                m_text.Clear();
                m_text.Append("Frame: ").AppendInt32(frameToDraw).AppendLine();
                m_text.Append("Local area: ").AppendInt32(m_frameLocalArea);
                DrawText(new Vector2(20, textOffsetY), m_text, Color.Yellow, 1);
                textOffsetY += largeTextLineSize * 2 + 10;

                // Draw fps and total calls
                m_text.Clear();
                m_text.Append(m_fpsBlock.Name).Append(" ");
                if (!m_useCustomFrame) // Show FPS only when not using custom frame
                {
                    m_text.AppendDecimal(m_fpsPctg, 3);
                }
                m_text.AppendLine();
                m_text.Append("Total calls: ").AppendInt32(IsValidIndex(frameToDraw, lastFrameIndex) ? m_selectedProfiler.TotalCalls[frameToDraw] : -1);
                DrawText(new Vector2(20, textOffsetY), m_text, Color.Red, 1);
                textOffsetY += largeTextLineSize;

                m_text.Clear();
                if (!VRage.MyCompilationSymbols.PerformanceProfiling)
                {
                    m_text.Append("MyCompilationSymbols.PerformanceProfiling NOT ENABLED!").AppendLine();
                }
                if (!ProfilerProcessingEnabled)
                {
                    m_text.Append("Profiler processing disabled, F12 -> Profiler").AppendLine();
                }
                DrawText(new Vector2(0, 0), m_text, Color.Yellow, 0.6f);

                textOffsetY = ViewportSize.Y / 2;
                List <MyProfilerBlock> children       = m_selectedProfiler.SelectedRootChildren;
                List <MyProfilerBlock> sortedChildren = GetSortedChildren(frameToDraw);

                // Draw the 'stack trace'
                m_text.Clear();
                MyProfilerBlock currentBlock = m_selectedProfiler.SelectedRoot;

                while (currentBlock != null)
                {
                    // Stop inserting new elements if the path becomes too long
                    if (currentBlock.Name.Length + 3 + m_text.Length > 170)
                    {
                        m_text.Insert(0, "... > ");
                        break;
                    }

                    if (m_text.Length > 0)
                    {
                        m_text.Insert(0, " > ");
                    }
                    m_text.Insert(0, currentBlock.Name);
                    currentBlock = currentBlock.Parent;
                }

                DrawTextShadow(new Vector2(20, textOffsetY), m_text, Color.White, 0.7f);
                textOffsetY += eventLineSize;

                if (m_selectedProfiler.SelectedRoot != null)
                {
                    Color whiteColor = Color.White;
                    DrawEvent(textOffsetY, m_selectedProfiler.SelectedRoot, -1, frameToDraw, lastFrameIndex, ref whiteColor);
                    textOffsetY += eventLineSize;
                }

                if (sortedChildren.Count > 0)
                {
                    // Draw the sorting order indicator
                    m_text.Clear().Append("\\/");
                    switch (m_sortingOrder)
                    {
                    case RenderProfilerSortingOrder.Id:
                        m_text.Append(" ASC");
                        DrawTextShadow(new Vector2(20, textOffsetY), m_text, Color.White, 0.7f);
                        break;

                    case RenderProfilerSortingOrder.MillisecondsLastFrame:
                        m_text.Append(" DESC");
                        DrawTextShadow(new Vector2(660, textOffsetY), m_text, Color.White, 0.7f);
                        break;

                    case RenderProfilerSortingOrder.MillisecondsAverage:
                        m_text.Append(" DESC");
                        DrawTextShadow(new Vector2(1270, textOffsetY), m_text, Color.White, 0.7f);
                        break;
                    }
                    textOffsetY += eventLineSize;

                    // Draw the profiler blocks
                    for (int i = 0; i < sortedChildren.Count; i++)
                    {
                        MyProfilerBlock profilerBlock = sortedChildren[i];

                        Color lineColor = IndexToColor(children.IndexOf(profilerBlock));

                        DrawEvent(textOffsetY, profilerBlock, i, frameToDraw, lastFrameIndex, ref lineColor);
                        textOffsetY += eventLineSize;
                    }
                }
                else
                {
                    m_text.Clear().Append("No more blocks at this point!");
                    textOffsetY += eventLineSize;
                    DrawTextShadow(new Vector2(20, textOffsetY), m_text, Color.White, 0.7f);
                    textOffsetY += eventLineSize;
                }

                // Draw graphs
                BeginLineBatch();
                DrawPerfEvents(lastFrameIndex);
                EndLineBatch();
            }

            // Update horizontal offset
            if (!Paused && !m_useCustomFrame)
            {
                m_selectedFrame = lastFrameIndex;
            }
        }
Esempio n. 11
0
        public static void HandleInput(RenderProfilerCommand command, int index)
        {
            switch (command)
            {
            case RenderProfilerCommand.Enable:
            {
                if (!m_enabled)
                {
                    m_enabled = true;
                    m_profilerProcessingEnabled = true;         // Enable when disabled and keep enabled
                    SetLevel();
                }
                break;
            }

            case RenderProfilerCommand.ToggleEnabled:
            {
                // Enable or Disable profiler drawing
                if (m_enabled)
                {
                    m_enabled        = false;
                    m_useCustomFrame = false;
                }
                else
                {
                    m_enabled = true;
                    m_profilerProcessingEnabled = true;         // Enable when disabled and keep enabled
                }
                break;
            }

            case RenderProfilerCommand.JumpToRoot:
                m_selectedProfiler.SelectedRoot = null;
                break;

            case RenderProfilerCommand.JumpToLevel:
            {
                // Enable when disabled, added this for programmers who are too used to using the numpad 0 to open the profiler.
                if (index == 0 && !m_enabled)
                {
                    m_enabled = true;
                    m_profilerProcessingEnabled = true;         // Enable when disabled and keep enabled
                }
                else
                {
                    m_selectedProfiler.SelectedRoot = FindBlockByIndex(index - 1);         // On screen it's indexed from 1 (zero is level up)
                }
                break;
            }

            case RenderProfilerCommand.Pause:
            {
                Paused = !Paused;
                break;
            }

            case RenderProfilerCommand.NextThread:
            {
                lock (m_threadProfilers)
                {
                    int profilerIndex = (m_threadProfilers.IndexOf(m_selectedProfiler) + 1) % m_threadProfilers.Count;
                    m_selectedProfiler = m_threadProfilers[profilerIndex];
                }
                break;
            }

            case RenderProfilerCommand.PreviousThread:
            {
                lock (m_threadProfilers)
                {
                    int profilerIndex = (m_threadProfilers.IndexOf(m_selectedProfiler) - 1 + m_threadProfilers.Count) % m_threadProfilers.Count;
                    m_selectedProfiler = m_threadProfilers[profilerIndex];
                }
                break;
            }

            case RenderProfilerCommand.Reset:
            {
                lock (m_threadProfilers)
                {
                    foreach (var profiler in m_threadProfilers)
                    {
                        profiler.Reset();
                    }
                    m_selectedFrame = 0;
                }
                break;
            }

            case RenderProfilerCommand.NextFrame:
            {
                MyRenderProfiler.NextFrame(index);
                break;
            }

            case RenderProfilerCommand.PreviousFrame:
            {
                MyRenderProfiler.PreviousFrame(index);
                break;
            }

            case RenderProfilerCommand.DisableFrameSelection:
            {
                m_useCustomFrame = false;
                break;
            }

            case RenderProfilerCommand.IncreaseLevel:
            {
                m_levelLimit++;
                SetLevel();
                break;
            }

            case RenderProfilerCommand.DecreaseLevel:
            {
                m_levelLimit--;
                if (m_levelLimit < -1)
                {
                    m_levelLimit = -1;
                }
                SetLevel();
                break;
            }

            case RenderProfilerCommand.CopyPathToClipboard:
            {
                StringBuilder   pathBuilder  = new StringBuilder(200);
                MyProfilerBlock currentBlock = m_selectedProfiler.SelectedRoot;

                while (currentBlock != null)
                {
                    if (pathBuilder.Length > 0)
                    {
                        pathBuilder.Insert(0, " > ");
                    }
                    pathBuilder.Insert(0, currentBlock.Name);
                    currentBlock = currentBlock.Parent;
                }

                if (pathBuilder.Length > 0)
                {
                    // Clipboard can only be accessed from a thread on the STA apartment
                    System.Threading.Thread thread = new System.Threading.Thread(() => System.Windows.Forms.Clipboard.SetText(pathBuilder.ToString()));
                    thread.SetApartmentState(System.Threading.ApartmentState.STA);
                    thread.Start();
                    thread.Join();
                }
                break;
            }

            case RenderProfilerCommand.TryGoToPathInClipboard:
            {
                string fullPath = string.Empty;

                Exception threadEx = null;
                System.Threading.Thread staThread = new System.Threading.Thread(
                    delegate()
                    {
                        try
                        {
                            fullPath = System.Windows.Forms.Clipboard.GetText();
                        }

                        catch (Exception ex)
                        {
                            threadEx = ex;
                        }
                    });
                staThread.SetApartmentState(System.Threading.ApartmentState.STA);
                staThread.Start();
                staThread.Join();

                if (!string.IsNullOrEmpty(fullPath))
                {
                    string[] split = fullPath.Split(new string[] { " > " }, StringSplitOptions.None);

                    MyProfilerBlock        pathBlock = null;
                    List <MyProfilerBlock> blockSet  = m_selectedProfiler.RootBlocks;
                    for (int i = 0; i < split.Length; i++)
                    {
                        string          blockName = split[i];
                        MyProfilerBlock oldPath   = pathBlock;

                        for (int j = 0; j < blockSet.Count; j++)
                        {
                            MyProfilerBlock block = blockSet[j];
                            if (block.Name == blockName)
                            {
                                pathBlock = block;
                                blockSet  = pathBlock.Children;
                                break;
                            }
                        }

                        // If the path did not change, we cannot go any deeper, break out of this loop
                        if (oldPath == pathBlock)
                        {
                            break;
                        }
                    }

                    if (pathBlock != null)
                    {
                        m_selectedProfiler.SelectedRoot = pathBlock;
                    }
                }
                break;
            }

            case RenderProfilerCommand.SetLevel:
            {
                m_levelLimit = index;
                if (m_levelLimit < -1)
                {
                    m_levelLimit = -1;
                }
                SetLevel();
                break;
            }

            case RenderProfilerCommand.DecreaseLocalArea:
                m_frameLocalArea = Math.Max(2, m_frameLocalArea / 2);
                break;

            case RenderProfilerCommand.IncreaseLocalArea:
                m_frameLocalArea = Math.Min(MyProfiler.MAX_FRAMES, m_frameLocalArea * 2);
                break;

            case RenderProfilerCommand.IncreaseRange:
                m_milisecondsGraphScale.IncreaseYRange();
                break;

            case RenderProfilerCommand.DecreaseRange:
                m_milisecondsGraphScale.DecreaseYRange();
                break;

            case RenderProfilerCommand.ChangeSortingOrder:
                m_sortingOrder += 1;
                if (m_sortingOrder >= RenderProfilerSortingOrder.NumSortingTypes)
                {
                    m_sortingOrder = RenderProfilerSortingOrder.Id;
                }
                break;

            default:
                System.Diagnostics.Debug.Assert(false, "Unknown command");
                break;
            }
        }
Esempio n. 12
0
 static MyRenderProfiler()
 {
     m_levelLimit = VRage.MyCompilationSymbols.ProfileFromStart ? -1 : 0;
     // Create block, some unique id
     m_fpsBlock = MyProfiler.CreateExternalBlock("FPS", -2);
 }
Esempio n. 13
0
        public void StartBlock(string name, string memberName, int line, string file)
        {
            Debug.Assert(!EnableAsserts || OwnerThread == Thread.CurrentThread);

            if (m_levelLimit != -1 && m_currentProfilingStack.Count >= m_levelLimit)
            {
                m_levelSkipCount++;
                return;
            }

            MyProfilerBlock profilingBlock = null;
            MyProfilerBlockKey key = new MyProfilerBlockKey(file, memberName, name, line, GetParentId());

            if (!m_profilingBlocks.TryGetValue(key, out profilingBlock) && !m_blocksToAdd.TryGetValue(key, out profilingBlock))
            {
                profilingBlock = new MyProfilerBlock(ref key, memberName, m_nextId++);

                if (m_currentProfilingStack.Count > 0)
                {
                    profilingBlock.Parent = m_currentProfilingStack.Peek();
                    Debug.Assert(!profilingBlock.Parent.Children.Contains(profilingBlock), "Why is already between children?");
                }

                m_blocksToAdd.Add(key, profilingBlock);
            }

            profilingBlock.Start(MemoryProfiling);

            m_currentProfilingStack.Push(profilingBlock);
        }
Esempio n. 14
0
        public int StartMyProfilingBlock(string name, bool memoryProfiling, float customValue = 0)
        {
            if (m_managedThreadId != Thread.CurrentThread.ManagedThreadId)
                return -1;

            //lock (m_lock)
            {
                if (MyProfiler.EnableAsserts)
                {        /*
                    foreach (MyProfilerBlock block in m_currentProfilingStack)
                    {
                        if (block.Name.ToString() == name)
                        {
                            System.Diagnostics.Debug.Assert(false, "Event is already in stack!");
                            return -1;
                        }
                    }      */
                }
                 
                MyProfilerBlock profilingBlock = null;
                int key;

                unchecked
                {
                    key = (397 * name.GetHashCode()) ^ GetParentHash();
                }

                int id = 1;
                
                //if (!m_profilingBlocks.TryGetValue(m_nameComposer.ToString(), out profilingBlock))
                if (!m_profilingBlocks.TryGetValue(key, out profilingBlock))
                {
                    profilingBlock = new MyProfilerBlock(name/*m_nameComposer.ToString()*/);

                    if (m_currentProfilingStack.Count > 0)
                    {
                        MyProfilerBlock parentBlock = m_currentProfilingStack.Peek();
                        if (!parentBlock.Children.Contains(profilingBlock))
                            parentBlock.Children.Add(profilingBlock);

                        profilingBlock.Parent = m_currentProfilingStack.Peek();
                    }

                    m_profilingBlocks.Add(key, profilingBlock);
                    profilingBlock.Id = m_profilingBlocks.Count - 1;
                }

                id = profilingBlock.Id;
                
                profilingBlock.Start(memoryProfiling);
                profilingBlock.CustomValue = customValue;
                                   
                m_currentProfilingStack.Push(profilingBlock);

                return id;
            }
        }