Ejemplo n.º 1
0
        public void EndBlock(string member, int line, string file, MyTimeSpan?customTime = null, float customValue = 0, string timeFormat = null, string valueFormat = null)
        {
            Debug.Assert(!EnableAsserts || OwnerThread == Thread.CurrentThread);

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

            if (m_currentProfilingStack.Count > 0)
            {
                MyProfilerBlock profilingBlock = m_currentProfilingStack.Pop();
                CheckEndBlock(profilingBlock, member, file, GetParentId());
                profilingBlock.CustomValue = customValue;
                profilingBlock.TimeFormat  = timeFormat;
                profilingBlock.ValueFormat = valueFormat;
                profilingBlock.End(MemoryProfiling, customTime);
            }
            else
            {
                Debug.Fail(String.Format("Unpaired profiling end block encountered for '{0}'{1}File: {2}({3}){1}", member, Environment.NewLine, file, line));
            }

            if (AutoCommit && m_currentProfilingStack.Count == 0)
            {
                CommitInternal();
            }
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
 public void Init(MyProfilerBlockObjectBuilderInfo data)
 {
     Id                 = data.Id;
     Key                = data.Key;
     Invalid            = data.Invalid;
     TimeFormat         = data.TimeFormat;
     ValueFormat        = data.ValueFormat;
     CallFormat         = data.CallFormat;
     ProcessMemory      = data.ProcessMemory;
     ManagedMemoryBytes = data.ManagedMemoryBytes;
     Miliseconds        = data.Miliseconds;
     CustomValues       = data.CustomValues;
     NumCallsArray      = data.NumCallsArray;
     Children           = data.Children;
     Parent             = data.Parent;
 }
Ejemplo n.º 4
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));
            }
        }
Ejemplo n.º 5
0
        public void StartBlock(string name, string memberName, int line, string file, int forceOrder = int.MaxValue)
        {
            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))
            {
                if (blockPool.Count == 0)
                {
                    for (int i = 0; i < PROFILER_BLOCK_INCREMENT_STEP; i++)
                    {
                        blockPool.Add(new MyProfilerBlock());
                    }
                }

                profilingBlock = blockPool[0];
                blockPool.RemoveAt(0);

                profilingBlock.SetBlockData(ref key, m_nextId++, forceOrder);

                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);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        public void EndBlock(string member, int line, string file, MyTimeSpan?customTime = null, float customValue = 0, string timeFormat = null, string valueFormat = null)
        {
            Debug.Assert(!EnableAsserts || OwnerThread == Thread.CurrentThread);

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

            MyProfilerBlock profilingBlock = m_currentProfilingStack.Pop();

            CheckEndBlock(profilingBlock, member, file, GetParentId());

            profilingBlock.CustomValue = customValue;
            profilingBlock.TimeFormat  = timeFormat;
            profilingBlock.ValueFormat = valueFormat;
            profilingBlock.End(MemoryProfiling, customTime);

            if (AutoCommit && m_currentProfilingStack.Count == 0)
            {
                CommitInternal();
            }
        }
Ejemplo n.º 8
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;
                GpuProfiler.AutoCommit = !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;
            }
        }
Ejemplo n.º 9
0
 static MyRenderProfiler()
 {
     m_levelLimit = VRage.MyCompilationSymbols.ProfileFromStart ? -1 : 0;
     // Create block, some unique id
     m_fpsBlock = MyProfiler.CreateExternalBlock("FPS", -2);
 }
Ejemplo n.º 10
0
        private void CommitInternal()
        {
            Debug.Assert(!EnableAsserts || OwnerThread == Thread.CurrentThread);
            Debug.Assert(m_currentProfilingStack.Count == 0, "CommitFrame cannot be called when there are some opened blocks, it must be outside blocks!");
            m_currentProfilingStack.Clear();

            if (m_blocksToAdd.Count > 0)
            {
                using (m_historyLock.AcquireExclusiveUsing())
                {
                    foreach (var block in m_blocksToAdd)
                    {
                        if (block.Value.Parent != null)
                        {
                            block.Value.Parent.Children.AddOrInsert(block.Value, block.Value.ForceOrder);
                        }
                        else
                        {
                            m_rootBlocks.AddOrInsert(block.Value, block.Value.ForceOrder);
                        }

                        m_profilingBlocks.Add(block.Key, block.Value);
                    }
                    m_blocksToAdd.Clear();
                    Interlocked.Exchange(ref m_remainingWindow, UPDATE_WINDOW - 1); // We have lock, no one is in draw, reset window
                }
            }
            else if (m_historyLock.TryAcquireExclusive())
            {
                Interlocked.Exchange(ref m_remainingWindow, UPDATE_WINDOW - 1); // We have lock, no one is in draw, reset window
                m_historyLock.ReleaseExclusive();
            }
            else if (Interlocked.Decrement(ref m_remainingWindow) < 0)
            {
                // Window is empty, wait for lock and reset it
                using (m_historyLock.AcquireExclusiveUsing())
                {
                    Interlocked.Exchange(ref m_remainingWindow, UPDATE_WINDOW - 1); // We have lock, no one is in draw, reset window
                }
            }

            int callCount = 0;

            m_levelLimit = m_newLevelLimit;

            int writeFrame = (m_lastFrameIndex + 1) % MyProfiler.MAX_FRAMES;

            foreach (MyProfilerBlock profilerBlock in m_profilingBlocks.Values)
            {
                callCount += profilerBlock.NumCalls;

                profilerBlock.ManagedMemoryBytes[writeFrame] = profilerBlock.DeltaManagedB;
                if (MemoryProfiling)
                {
                    profilerBlock.ProcessMemory[writeFrame] = profilerBlock.ProcessDeltaMB;
                }
                profilerBlock.NumCallsArray[writeFrame] = profilerBlock.NumCalls;
                profilerBlock.CustomValues[writeFrame]  = profilerBlock.CustomValue;
                profilerBlock.Miliseconds[writeFrame]   = (float)profilerBlock.Elapsed.Miliseconds;

                // Unused
                profilerBlock.averageMiliseconds = 0.9f * profilerBlock.averageMiliseconds + 0.1f * (float)profilerBlock.Elapsed.Miliseconds;
                //profilerBlock.NumChildCalls = profilerBlock.GetNumChildCalls();

                if (ENABLE_PROFILER_LOG)
                {
                    if (profilerBlock.Elapsed.Miliseconds > LOG_THRESHOLD_MS)
                    {
                        m_logWriter.Write(DateTime.Now.ToString());
                        m_logWriter.Write("; ");
                        m_logWriter.Write(((int)profilerBlock.Elapsed.Miliseconds).ToString());
                        m_logWriter.Write("; ");
                        m_logWriter.Write(profilerBlock.Name);
                        MyProfilerBlock tempBlock = profilerBlock;
                        while (tempBlock.Parent != null)
                        {
                            tempBlock = tempBlock.Parent;
                            m_logWriter.Write(" <- " + tempBlock.Name);
                        }
                        m_logWriter.WriteLine("");
                    }
                }

                profilerBlock.Clear();
            }

            TotalCalls[writeFrame] = callCount;
            m_lastFrameIndex       = writeFrame;
        }