Skip to content

Commit 2538e03

Browse files
committed
trace2: add process depth calculation
The performance format target requires recording of process depth. We use the number of forward slashes in the process's sid as a convenient way to determine this. Because the sid and the depth are process-related concepts, this change also moves sid/depth related properties into ProcessManager.cs and removes the SidManager.cs class.
1 parent 86c4b08 commit 2538e03

File tree

13 files changed

+81
-38
lines changed

13 files changed

+81
-38
lines changed

src/shared/Atlassian.Bitbucket.UI.Avalonia/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private static void AppMain(object o)
4747

4848
// Set the session id (sid) for the helper process, to be
4949
// used when TRACE2 tracing is enabled.
50-
SidManager.CreateSid();
50+
ProcessManager.CreateSid();
5151
using (var context = new CommandContext())
5252
using (var app = new HelperApplication(context))
5353
{
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using GitCredentialManager;
2+
using Xunit;
3+
4+
namespace Core.Tests;
5+
6+
public class ProcessManagerTests
7+
{
8+
[Theory]
9+
[InlineData("", 0)]
10+
[InlineData("foo", 0)]
11+
[InlineData("foo/bar", 1)]
12+
[InlineData("foo/bar/baz", 2)]
13+
public void TryGetProcessDepth_Returns_Expected_Depth(string input, int expected)
14+
{
15+
ProcessManager.Sid = input;
16+
var actual = ProcessManager.GetProcessDepth();
17+
18+
Assert.Equal(expected, actual);
19+
}
20+
}

src/shared/Core/ProcessManager.cs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Diagnostics;
23
using System.Threading.Tasks;
34

@@ -27,8 +28,14 @@ public interface IProcessManager
2728

2829
public class ProcessManager : IProcessManager
2930
{
31+
private const string SidEnvar = "GIT_TRACE2_PARENT_SID";
32+
3033
private readonly ITrace2 _trace2;
3134

35+
public static string Sid { get; internal set; }
36+
37+
public static int Depth { get; internal set; }
38+
3239
public ProcessManager(ITrace2 trace2)
3340
{
3441
_trace2 = trace2;
@@ -48,9 +55,52 @@ public virtual ChildProcess CreateProcess(string path, string args, bool useShel
4855
return CreateProcess(psi);
4956
}
5057

51-
5258
public virtual ChildProcess CreateProcess(ProcessStartInfo psi)
5359
{
5460
return new ChildProcess(_trace2, psi);
5561
}
62+
63+
/// <summary>
64+
/// Create a TRACE2 "session id" (sid) for this process.
65+
/// </summary>
66+
public static void CreateSid()
67+
{
68+
Sid = Environment.GetEnvironmentVariable(SidEnvar);
69+
70+
if (!string.IsNullOrEmpty(Sid))
71+
{
72+
Sid = $"{Sid}/{Guid.NewGuid():D}";
73+
}
74+
else
75+
{
76+
// We are the root process; create our own 'root' SID
77+
Sid = Guid.NewGuid().ToString("D");
78+
}
79+
80+
Environment.SetEnvironmentVariable(SidEnvar, Sid);
81+
Depth = GetProcessDepth();
82+
}
83+
84+
/// <summary>
85+
/// Get "depth" of current process relative to top-level GCM process.
86+
/// </summary>
87+
/// <returns>Depth of current process.</returns>
88+
internal static int GetProcessDepth()
89+
{
90+
char processSeparator = '/';
91+
if (!Sid.Contains(processSeparator))
92+
{
93+
return 0;
94+
}
95+
96+
int count = 0;
97+
// Use AsSpan() for slight performance bump over traditional foreach loop.
98+
foreach (var c in Sid.AsSpan())
99+
{
100+
if (c == processSeparator)
101+
count++;
102+
}
103+
104+
return count;
105+
}
56106
}

src/shared/Core/SidManager.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/shared/Core/Trace2.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public void Initialize(DateTimeOffset startTime)
146146

147147
_applicationStartTime = startTime;
148148
_settings = _commandContext.Settings.GetTrace2Settings();
149-
_sid = SidManager.Sid;
149+
_sid = ProcessManager.Sid;
150150

151151
InitializeWriters();
152152

src/shared/Git-Credential-Manager.UI.Avalonia/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private static void AppMain(object o)
4545

4646
// Set the session id (sid) for the helper process, to be
4747
// used when TRACE2 tracing is enabled.
48-
SidManager.CreateSid();
48+
ProcessManager.CreateSid();
4949
using (var context = new CommandContext())
5050
using (var app = new HelperApplication(context))
5151
{

src/shared/Git-Credential-Manager/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static void Main(string[] args)
1414
var startTime = DateTimeOffset.UtcNow;
1515
// Set the session id (sid) and start time for the GCM process, to be
1616
// used when TRACE2 tracing is enabled.
17-
SidManager.CreateSid();
17+
ProcessManager.CreateSid();
1818

1919
using (var context = new CommandContext())
2020
using (var app = new Application(context))

src/shared/GitHub.UI.Avalonia/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private static void AppMain(object o)
4747

4848
// Set the session id (sid) for the helper process, to be
4949
// used when TRACE2 tracing is enabled.
50-
SidManager.CreateSid();
50+
ProcessManager.CreateSid();
5151
using (var context = new CommandContext())
5252
using (var app = new HelperApplication(context))
5353
{

src/shared/GitLab.UI.Avalonia/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private static void AppMain(object o)
4747

4848
// Set the session id (sid) for the helper process, to be
4949
// used when TRACE2 tracing is enabled.
50-
SidManager.CreateSid();
50+
ProcessManager.CreateSid();
5151
using (var context = new CommandContext())
5252
using (var app = new HelperApplication(context))
5353
{

src/windows/Atlassian.Bitbucket.UI.Windows/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static async Task Main(string[] args)
1414
{
1515
// Set the session id (sid) for the helper process, to be
1616
// used when TRACE2 tracing is enabled.
17-
SidManager.CreateSid();
17+
ProcessManager.CreateSid();
1818
using (var context = new CommandContext())
1919
using (var app = new HelperApplication(context))
2020
{

0 commit comments

Comments
 (0)