Skip to content

Commit 58f7154

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 f5c3651 commit 58f7154

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

src/shared/Core/Trace2.cs

Lines changed: 31 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

@@ -113,7 +114,6 @@ void WriteChildStart(DateTimeOffset startTime,
113114
/// <param name="relativeTime">Runtime of child process.</param>
114115
/// <param name="pid">Id of exiting process.</param>
115116
/// <param name="code">Process exit code.</param>
116-
/// <param name="sid">The child process's session id.</param>
117117
/// <param name="filePath">Path of the file this method is called from.</param>
118118
/// <param name="lineNumber">Line number of file this method is called from.</param>
119119
void WriteChildExit(
@@ -233,6 +233,7 @@ public void WriteChildStart(DateTimeOffset startTime,
233233
Event = Trace2Event.ChildStart,
234234
Sid = _sid,
235235
Time = startTime,
236+
Thread = BuildThreadName(),
236237
File = Path.GetFileName(filePath),
237238
Line = lineNumber,
238239
Id = ++_childProcCounter,
@@ -265,6 +266,7 @@ public void WriteChildExit(
265266
Event = Trace2Event.ChildExit,
266267
Sid = _sid,
267268
Time = DateTimeOffset.UtcNow,
269+
Thread = BuildThreadName(),
268270
File = Path.GetFileName(filePath),
269271
Line = lineNumber,
270272
Id = _childProcCounter,
@@ -296,6 +298,7 @@ public void WriteError(
296298
Event = Trace2Event.Error,
297299
Sid = _sid,
298300
Time = DateTimeOffset.UtcNow,
301+
Thread = BuildThreadName(),
299302
File = Path.GetFileName(filePath),
300303
Line = lineNumber,
301304
Message = errorMessage,
@@ -389,6 +392,7 @@ private void WriteVersion(
389392
Event = Trace2Event.Version,
390393
Sid = _sid,
391394
Time = DateTimeOffset.UtcNow,
395+
Thread = BuildThreadName(),
392396
File = Path.GetFileName(filePath),
393397
Line = lineNumber,
394398
Evt = eventFormatVersion,
@@ -418,6 +422,7 @@ private void WriteStart(
418422
Event = Trace2Event.Start,
419423
Sid = _sid,
420424
Time = DateTimeOffset.UtcNow,
425+
Thread = BuildThreadName(),
421426
File = Path.GetFileName(filePath),
422427
Line = lineNumber,
423428
Argv = argv,
@@ -434,6 +439,7 @@ private void WriteExit(int code, string filePath = "", int lineNumber = 0)
434439
Event = Trace2Event.Exit,
435440
Sid = _sid,
436441
Time = DateTimeOffset.UtcNow,
442+
Thread = BuildThreadName(),
437443
File = Path.GetFileName(filePath),
438444
Line = lineNumber,
439445
Code = code,
@@ -480,4 +486,28 @@ private void WriteMessage(Trace2Message message)
480486
}
481487
}
482488
}
489+
490+
private static string BuildThreadName()
491+
{
492+
// If this is the entry thread, call it "main", per Trace2 convention
493+
if (Thread.CurrentThread.ManagedThreadId == 0)
494+
{
495+
return "main";
496+
}
497+
498+
// If this is a thread pool thread, name it as such
499+
if (Thread.CurrentThread.IsThreadPoolThread)
500+
{
501+
return $"thread_pool_{Environment.CurrentManagedThreadId}";
502+
}
503+
504+
// Otherwise, if the thread is named, use it!
505+
if (!string.IsNullOrEmpty(Thread.CurrentThread.Name))
506+
{
507+
return Thread.CurrentThread.Name;
508+
}
509+
510+
// We don't know what this thread is!
511+
return string.Empty;
512+
}
483513
}

src/shared/Core/Trace2Message.cs

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

24-
// TODO: Remove this default value when TRACE2 regions are introduced.
2524
[JsonProperty("thread")]
26-
public string Thread { get; set; } = "main";
25+
public string Thread { get; set; }
2726

2827
[JsonProperty("time")]
2928
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)