Skip to content

Commit 596b3e6

Browse files
committed
trace2: capture thread name
With the upcoming introduction of regions, we will need to capture thread names. This change adds a new static BuildThreadName() method that defines a thread name by: 1. Determining if it is the entry thread and, if so, calling it "main", per Trace2 convention. 2. If it's not the main thread, determining whether it is a thread pool thread and naming it with a static prefix and the thread pool thread id. 3. If it is not the entry thread or a thread pool thread, use the thread name, if it has one. 4. Return an empty string if none of the above are true.
1 parent e891b92 commit 596b3e6

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

src/shared/Core/Trace2.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.IO.Pipes;
55
using System.Text;
6+
using System.Threading;
67

78
namespace GitCredentialManager;
89

@@ -114,6 +115,7 @@ void WriteChildStart(DateTimeOffset startTime,
114115
/// <param name="pid">Id of exiting process.</param>
115116
/// <param name="code">Process exit code.</param>
116117
/// <param name="sid">The child process's session id.</param>
118+
/// <param name="threadName">Name of the currently-executing thread.</param>
117119
/// <param name="filePath">Path of the file this method is called from.</param>
118120
/// <param name="lineNumber">Line number of file this method is called from.</param>
119121
void WriteChildExit(
@@ -182,7 +184,7 @@ public void Initialize(DateTimeOffset startTime)
182184
public void Start(string appPath,
183185
string[] args,
184186
string filePath,
185-
int lineNumber)
187+
int lineNumber = 0)
186188
{
187189
if (!AssemblyUtils.TryGetAssemblyVersion(out string version))
188190
{
@@ -233,6 +235,7 @@ public void WriteChildStart(DateTimeOffset startTime,
233235
Event = Trace2Event.ChildStart,
234236
Sid = _sid,
235237
Time = startTime,
238+
Thread = BuildThreadName(),
236239
File = Path.GetFileName(filePath),
237240
Line = lineNumber,
238241
Id = ++_childProcCounter,
@@ -265,6 +268,7 @@ public void WriteChildExit(
265268
Event = Trace2Event.ChildExit,
266269
Sid = _sid,
267270
Time = DateTimeOffset.UtcNow,
271+
Thread = BuildThreadName(),
268272
File = Path.GetFileName(filePath),
269273
Line = lineNumber,
270274
Id = _childProcCounter,
@@ -296,6 +300,7 @@ public void WriteError(
296300
Event = Trace2Event.Error,
297301
Sid = _sid,
298302
Time = DateTimeOffset.UtcNow,
303+
Thread = BuildThreadName(),
299304
File = Path.GetFileName(filePath),
300305
Line = lineNumber,
301306
Message = errorMessage,
@@ -389,6 +394,7 @@ private void WriteVersion(
389394
Event = Trace2Event.Version,
390395
Sid = _sid,
391396
Time = DateTimeOffset.UtcNow,
397+
Thread = BuildThreadName(),
392398
File = Path.GetFileName(filePath),
393399
Line = lineNumber,
394400
Evt = eventFormatVersion,
@@ -418,6 +424,7 @@ private void WriteStart(
418424
Event = Trace2Event.Start,
419425
Sid = _sid,
420426
Time = DateTimeOffset.UtcNow,
427+
Thread = BuildThreadName(),
421428
File = Path.GetFileName(filePath),
422429
Line = lineNumber,
423430
Argv = argv,
@@ -434,6 +441,7 @@ private void WriteExit(int code, string filePath = "", int lineNumber = 0)
434441
Event = Trace2Event.Exit,
435442
Sid = _sid,
436443
Time = DateTimeOffset.UtcNow,
444+
Thread = BuildThreadName(),
437445
File = Path.GetFileName(filePath),
438446
Line = lineNumber,
439447
Code = code,
@@ -480,4 +488,28 @@ private void WriteMessage(Trace2Message message)
480488
}
481489
}
482490
}
491+
492+
private static string BuildThreadName()
493+
{
494+
// If this is the entry thread, call it "main", per Trace2 convention
495+
if (Thread.CurrentThread.ManagedThreadId == 0)
496+
{
497+
return "main";
498+
}
499+
500+
// If this is a thread pool thread, name it as such
501+
if (Thread.CurrentThread.IsThreadPoolThread)
502+
{
503+
return $"thread_pool_{Environment.CurrentManagedThreadId}";
504+
}
505+
506+
// Otherwise, if the thread is named, use it!
507+
if (!string.IsNullOrEmpty(Thread.CurrentThread.Name))
508+
{
509+
return Thread.CurrentThread.Name;
510+
}
511+
512+
// We don't know what this thread is!
513+
return string.Empty;
514+
}
483515
}

src/shared/Core/Trace2Message.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ public abstract class Trace2Message : ITrace2Message
3232
[JsonProperty("sid")]
3333
public string Sid { get; set; }
3434

35-
// TODO: Remove this default value when TRACE2 regions are introduced.
3635
[JsonProperty("thread")]
37-
public string Thread { get; set; } = "main";
36+
public string Thread { get; set; }
3837

3938
[JsonProperty("time")]
4039
public DateTimeOffset Time { get; set; }

src/shared/TestInfrastructure/Objects/NullTrace.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,11 @@ public void WriteChildExit(
7878
string filePath = "",
7979
int lineNumber = 0) { }
8080

81-
public void WriteError(string errorMessage,
81+
public void WriteError(
82+
string errorMessage,
8283
string parameterizedMessage = null,
8384
string filePath = "",
84-
int lineNumber = 0)
85-
{ }
86-
87-
public string SetSid() { return ""; }
85+
int lineNumber = 0) { }
8886

8987
#endregion
9088

0 commit comments

Comments
 (0)