Skip to content

Commit f8c2e2f

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 7c9328c commit f8c2e2f

File tree

13 files changed

+82
-38
lines changed

13 files changed

+82
-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: 52 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
protected 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
EnsureArgument.NotNull(trace2, nameof(trace2));
@@ -50,9 +57,53 @@ public virtual ChildProcess CreateProcess(string path, string args, bool useShel
5057
return CreateProcess(psi);
5158
}
5259

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

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
{

src/windows/Git-Credential-Manager.UI.Windows/Program.cs

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

src/windows/GitHub.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
{

src/windows/GitLab.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)