Skip to content

trace2: add performance format target #1151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/shared/Atlassian.Bitbucket.UI.Avalonia/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private static void AppMain(object o)

// Set the session id (sid) for the helper process, to be
// used when TRACE2 tracing is enabled.
SidManager.CreateSid();
ProcessManager.CreateSid();
using (var context = new CommandContext())
using (var app = new HelperApplication(context))
{
Expand Down
33 changes: 33 additions & 0 deletions src/shared/Core.Tests/ProcessManagerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using GitCredentialManager;
using Xunit;

namespace Core.Tests;

public class ProcessManagerTests
{
[Theory]
[InlineData("", 0)]
[InlineData("foo", 0)]
[InlineData("foo/bar", 1)]
[InlineData("foo/bar/baz", 2)]
public void CreateSid_Envar_Returns_Expected_Sid(string input, int expected)
{
ProcessManager.Sid = input;
var actual = ProcessManager.GetProcessDepth();

Assert.Equal(expected, actual);
}

[Theory]
[InlineData("", 0)]
[InlineData("foo", 0)]
[InlineData("foo/bar", 1)]
[InlineData("foo/bar/baz", 2)]
public void TryGetProcessDepth_Returns_Expected_Depth(string input, int expected)
{
ProcessManager.Sid = input;
var actual = ProcessManager.GetProcessDepth();

Assert.Equal(expected, actual);
}
}
13 changes: 10 additions & 3 deletions src/shared/Core.Tests/Trace2Tests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Text.RegularExpressions;
using GitCredentialManager.Tests.Objects;
using Xunit;

namespace GitCredentialManager.Tests;
Expand Down Expand Up @@ -30,4 +27,14 @@ public void TryGetPipeName_Windows_Returns_Expected_Value(string input, string e
Assert.True(isSuccessful);
Assert.Matches(actual, expected);
}

[Theory]
[InlineData(0.013772, " 0.013772 ")]
[InlineData(26.316083, " 26.316083 ")]
[InlineData(100.316083, "100.316083 ")]
public void BuildTimeSpan_Match_Returns_Expected_String(double input, string expected)
{
var actual = Trace2Message.BuildTimeSpan(input);
Assert.Equal(expected, actual);
}
}
8 changes: 5 additions & 3 deletions src/shared/Core/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static class EnvironmentVariables
public const string GcmAllowWia = "GCM_ALLOW_WINDOWSAUTH";
public const string GitTrace2Event = "GIT_TRACE2_EVENT";
public const string GitTrace2Normal = "GIT_TRACE2";
public const string GitTrace2Performance = "GIT_TRACE2_PERF";

/*
* Unlike other environment variables, these proxy variables are normally lowercase only.
Expand Down Expand Up @@ -169,9 +170,10 @@ public static class Remote

public static class Trace2
{
public const string SectionName = "trace2";
public const string EventTarget = "eventtarget";
public const string NormalTarget = "normaltarget";
public const string SectionName = "trace2";
public const string EventTarget = "eventtarget";
public const string NormalTarget = "normaltarget";
public const string PerformanceTarget = "perftarget";
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/shared/Core/ITrace2Writer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@

namespace GitCredentialManager;

/// <summary>
/// The different format targets supported in the TRACE2 tracing
/// system.
/// </summary>
public enum Trace2FormatTarget
{
Event,
Normal,
Performance
}

public interface ITrace2Writer : IDisposable
{
bool Failed { get; }
Expand Down Expand Up @@ -34,6 +45,9 @@ protected string Format(Trace2Message message)
case Trace2FormatTarget.Normal:
sb.Append(message.ToNormalString());
break;
case Trace2FormatTarget.Performance:
sb.Append(message.ToPerformanceString());
break;
default:
Console.WriteLine($"warning: unrecognized format target '{_formatTarget}', disabling TRACE2 tracing.");
Failed = true;
Expand Down
51 changes: 50 additions & 1 deletion src/shared/Core/ProcessManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;

Expand Down Expand Up @@ -27,8 +28,14 @@ public interface IProcessManager

public class ProcessManager : IProcessManager
{
private const string SidEnvar = "GIT_TRACE2_PARENT_SID";

protected readonly ITrace2 Trace2;

public static string Sid { get; internal set; }

public static int Depth { get; internal set; }

public ProcessManager(ITrace2 trace2)
{
EnsureArgument.NotNull(trace2, nameof(trace2));
Expand All @@ -50,9 +57,51 @@ public virtual ChildProcess CreateProcess(string path, string args, bool useShel
return CreateProcess(psi);
}


public virtual ChildProcess CreateProcess(ProcessStartInfo psi)
{
return new ChildProcess(Trace2, psi);
}

/// <summary>
/// Create a TRACE2 "session id" (sid) for this process.
/// </summary>
public static void CreateSid()
{
Sid = Environment.GetEnvironmentVariable(SidEnvar);

if (!string.IsNullOrEmpty(Sid))
{
// Use trim to ensure no accidental leading or trailing slashes
Sid = $"{Sid.Trim('/')}/{Guid.NewGuid():D}";
// Only check for process depth if there is a parent.
// If there is not a parent, depth defaults to 0.
Depth = GetProcessDepth();
}
else
{
// We are the root process; create our own 'root' SID
Sid = Guid.NewGuid().ToString("D");
}

Environment.SetEnvironmentVariable(SidEnvar, Sid);
}

/// <summary>
/// Get "depth" of current process relative to top-level GCM process.
/// </summary>
/// <returns>Depth of current process.</returns>
internal static int GetProcessDepth()
{
char processSeparator = '/';

int count = 0;
// Use AsSpan() for slight performance bump over traditional foreach loop.
foreach (var c in Sid.AsSpan())
{
if (c == processSeparator)
count++;
}

return count;
}
}
6 changes: 6 additions & 0 deletions src/shared/Core/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,12 @@ public Trace2Settings GetTrace2Settings()
settings.FormatTargetsAndValues.Add(Trace2FormatTarget.Normal, value);
}

if (TryGetSetting(Constants.EnvironmentVariables.GitTrace2Performance, KnownGitCfg.Trace2.SectionName,
Constants.GitConfiguration.Trace2.PerformanceTarget, out value))
{
settings.FormatTargetsAndValues.Add(Trace2FormatTarget.Performance, value);
}

return settings;
}

Expand Down
27 changes: 0 additions & 27 deletions src/shared/Core/SidManager.cs

This file was deleted.

Loading