From 4e19b85e00489920f93cbb7e325b62ec0289d738 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Mon, 15 Feb 2021 15:04:37 +0100 Subject: [PATCH] Add support for multi-threaded profile events in build tool --- Source/Tools/Flax.Build/Build/Profiling.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Source/Tools/Flax.Build/Build/Profiling.cs b/Source/Tools/Flax.Build/Build/Profiling.cs index 98bc2c62a..c5cc89bcb 100644 --- a/Source/Tools/Flax.Build/Build/Profiling.cs +++ b/Source/Tools/Flax.Build/Build/Profiling.cs @@ -4,6 +4,7 @@ using System; using System.IO; using System.Collections.Generic; using System.Text; +using System.Threading; namespace Flax.Build { @@ -12,7 +13,7 @@ namespace Flax.Build /// public class ProfileEventScope : IDisposable { - private int _id; + private readonly int _id; /// /// Initializes a new instance of the class. @@ -61,6 +62,11 @@ namespace Flax.Build /// The event call depth (for parent-children events evaluation). /// public int Depth; + + /// + /// The calling thread id. + /// + public int ThreadId; } private static int _depth; @@ -78,6 +84,7 @@ namespace Flax.Build e.StartTime = DateTime.Now; e.Duration = TimeSpan.Zero; e.Depth = _depth++; + e.ThreadId = Thread.CurrentThread.ManagedThreadId; _events.Add(e); return _events.Count - 1; } @@ -147,8 +154,8 @@ namespace Flax.Build for (int i = 0; i < _events.Count; i++) { var e = _events[i]; - - contents.Append($"{{ \"pid\":1, \"tid\":1, \"ts\":{(int)((e.StartTime - startTime).TotalMilliseconds * 1000.0)}, \"dur\":{(int)(e.Duration.TotalMilliseconds * 1000.0)}, \"ph\":\"X\", \"name\":\"{e.Name}\", \"args\":{{ \"startTime\":\"{e.StartTime.ToShortTimeString()}\" }} }}\n"); + + contents.Append($"{{ \"pid\":{e.ThreadId}, \"tid\":1, \"ts\":{(int)((e.StartTime - startTime).TotalMilliseconds * 1000.0)}, \"dur\":{(int)(e.Duration.TotalMilliseconds * 1000.0)}, \"ph\":\"X\", \"name\":\"{e.Name}\", \"args\":{{ \"startTime\":\"{e.StartTime.ToShortTimeString()}\" }} }}\n"); if (i + 1 != _events.Count) contents.Append(",");