Skip to content

Commit b748de3

Browse files
Lessley Denningtonldennington
authored andcommitted
trace2: add child_start and child_exit events
Write the TRACE2 child_start and child_exit events for each child process invoked by GCM.
1 parent 0395563 commit b748de3

File tree

9 files changed

+247
-23
lines changed

9 files changed

+247
-23
lines changed

src/shared/Core/Authentication/AuthenticationBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected internal virtual async Task<IDictionary<string, string>> InvokeHelperA
4343
// authentication helper's messages.
4444
Context.Trace.Flush();
4545

46-
var process = ChildProcess.Start(Context.Trace2, procStartInfo);
46+
var process = ChildProcess.Start(Context.Trace2, procStartInfo, Trace2ProcessClass.UIHelper);
4747
if (process is null)
4848
{
4949
throw new Exception($"Failed to start helper process: {path} {args}");

src/shared/Core/ChildProcess.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,41 @@ public class ChildProcess : DisposableObject
1111

1212
private DateTimeOffset _startTime;
1313
private DateTimeOffset _exitTime => Process.ExitTime;
14+
private ProcessStartInfo _startInfo => Process.StartInfo;
1415

1516
private int _id => Process.Id;
1617

1718
public Process Process { get; }
1819
public StreamWriter StandardInput => Process.StandardInput;
1920
public StreamReader StandardOutput => Process.StandardOutput;
2021
public StreamReader StandardError => Process.StandardError;
21-
public int Id => Process.Id;
2222
public int ExitCode => Process.ExitCode;
2323

24-
public static ChildProcess Start(ITrace2 trace2, ProcessStartInfo startInfo)
24+
public static ChildProcess Start(ITrace2 trace2, ProcessStartInfo startInfo, Trace2ProcessClass processClass)
2525
{
2626
var childProc = new ChildProcess(trace2, startInfo);
27-
childProc.Start();
27+
childProc.Start(processClass);
2828
return childProc;
2929
}
3030

3131
public ChildProcess(ITrace2 trace2, ProcessStartInfo startInfo)
3232
{
3333
_trace2 = trace2;
3434
Process = new Process() { StartInfo = startInfo };
35+
Process.Exited += ProcessOnExited;
3536
}
3637

37-
public void Start()
38+
public void Start(Trace2ProcessClass processClass)
3839
{
3940
ThrowIfDisposed();
41+
// Record the time just before the process starts, since:
42+
// (1) There is no event related to Start as there is with Exit.
43+
// (2) Using Process.StartTime causes a race condition that leads
44+
// to an exception if the process finishes executing before the
45+
// variable is passed to Trace2.
46+
_startTime = DateTimeOffset.UtcNow;
4047
Process.Start();
48+
_trace2.WriteChildStart(_startTime, processClass, _startInfo.UseShellExecute, _startInfo.FileName, _startInfo.Arguments);
4149
}
4250

4351
public void WaitForExit() => Process.WaitForExit();
@@ -46,7 +54,17 @@ public void Start()
4654

4755
protected override void ReleaseManagedResources()
4856
{
57+
Process.Exited -= ProcessOnExited;
4958
Process.Dispose();
5059
base.ReleaseUnmanagedResources();
5160
}
61+
62+
private void ProcessOnExited(object sender, EventArgs e)
63+
{
64+
if (sender is Process)
65+
{
66+
double elapsedTime = (_exitTime - _startTime).TotalSeconds;
67+
_trace2.WriteChildExit(elapsedTime, _id, Process.ExitCode);
68+
}
69+
}
5270
}

src/shared/Core/Diagnostics/GitDiagnostic.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected override Task<bool> RunInternalAsync(StringBuilder log, IList<string>
3131

3232
log.Append("Listing all Git configuration...");
3333
ChildProcess configProc = _git.CreateProcess("config --list --show-origin");
34-
configProc.Start();
34+
configProc.Start(Trace2ProcessClass.Git);
3535
// To avoid deadlocks, always read the output stream first and then wait
3636
// TODO: don't read in all the data at once; stream it
3737
string gitConfig = configProc.StandardOutput.ReadToEnd().TrimEnd();

src/shared/Core/Git.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public GitVersion Version
9090
{
9191
using (var git = CreateProcess("version"))
9292
{
93-
git.Start();
93+
git.Start(Trace2ProcessClass.Git);
9494

9595
string data = git.StandardOutput.ReadToEnd();
9696
git.WaitForExit();
@@ -120,7 +120,7 @@ public string GetCurrentRepository()
120120
{
121121
using (var git = CreateProcess("rev-parse --absolute-git-dir"))
122122
{
123-
git.Start();
123+
git.Start(Trace2ProcessClass.Git);
124124
string data = git.StandardOutput.ReadToEnd();
125125
git.WaitForExit();
126126

@@ -141,7 +141,7 @@ public IEnumerable<GitRemote> GetRemotes()
141141
{
142142
using (var git = CreateProcess("remote -v show"))
143143
{
144-
git.Start();
144+
git.Start(Trace2ProcessClass.Git);
145145
// To avoid deadlocks, always read the output stream first and then wait
146146
// TODO: don't read in all the data at once; stream it
147147
string data = git.StandardOutput.ReadToEnd();

src/shared/Core/GitConfiguration.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public void Enumerate(GitConfigurationLevel level, GitConfigurationEnumerationCa
129129
string levelArg = GetLevelFilterArg(level);
130130
using (ChildProcess git = _git.CreateProcess($"config --null {levelArg} --list"))
131131
{
132-
git.Start();
132+
git.Start(Trace2ProcessClass.Git);
133133
// To avoid deadlocks, always read the output stream first and then wait
134134
// TODO: don't read in all the data at once; stream it
135135
string data = git.StandardOutput.ReadToEnd();
@@ -198,7 +198,7 @@ public bool TryGet(GitConfigurationLevel level, GitConfigurationType type, strin
198198
string typeArg = GetCanonicalizeTypeArg(type);
199199
using (ChildProcess git = _git.CreateProcess($"config --null {levelArg} {typeArg} {QuoteCmdArg(name)}"))
200200
{
201-
git.Start();
201+
git.Start(Trace2ProcessClass.Git);
202202
// To avoid deadlocks, always read the output stream first and then wait
203203
// TODO: don't read in all the data at once; stream it
204204
string data = git.StandardOutput.ReadToEnd();
@@ -236,7 +236,7 @@ public void Set(GitConfigurationLevel level, string name, string value)
236236
string levelArg = GetLevelFilterArg(level);
237237
using (ChildProcess git = _git.CreateProcess($"config {levelArg} {QuoteCmdArg(name)} {QuoteCmdArg(value)}"))
238238
{
239-
git.Start();
239+
git.Start(Trace2ProcessClass.Git);
240240
git.WaitForExit();
241241

242242
switch (git.ExitCode)
@@ -257,7 +257,7 @@ public void Add(GitConfigurationLevel level, string name, string value)
257257
string levelArg = GetLevelFilterArg(level);
258258
using (ChildProcess git = _git.CreateProcess($"config {levelArg} --add {QuoteCmdArg(name)} {QuoteCmdArg(value)}"))
259259
{
260-
git.Start();
260+
git.Start(Trace2ProcessClass.Git);
261261
git.WaitForExit();
262262

263263
switch (git.ExitCode)
@@ -278,7 +278,7 @@ public void Unset(GitConfigurationLevel level, string name)
278278
string levelArg = GetLevelFilterArg(level);
279279
using (ChildProcess git = _git.CreateProcess($"config {levelArg} --unset {QuoteCmdArg(name)}"))
280280
{
281-
git.Start();
281+
git.Start(Trace2ProcessClass.Git);
282282
git.WaitForExit();
283283

284284
switch (git.ExitCode)
@@ -302,7 +302,7 @@ public IEnumerable<string> GetAll(GitConfigurationLevel level, GitConfigurationT
302302

303303
using (ChildProcess git = _git.CreateProcess(gitArgs))
304304
{
305-
git.Start();
305+
git.Start(Trace2ProcessClass.Git);
306306
// To avoid deadlocks, always read the output stream first and then wait
307307
// TODO: don't read in all the data at once; stream it
308308
string data = git.StandardOutput.ReadToEnd();
@@ -344,7 +344,7 @@ public IEnumerable<string> GetRegex(GitConfigurationLevel level, GitConfiguratio
344344

345345
using (ChildProcess git = _git.CreateProcess(gitArgs))
346346
{
347-
git.Start();
347+
git.Start(Trace2ProcessClass.Git);
348348
// To avoid deadlocks, always read the output stream first and then wait
349349
// TODO: don't read in all the data at once; stream it
350350
string data = git.StandardOutput.ReadToEnd();
@@ -386,7 +386,7 @@ public void ReplaceAll(GitConfigurationLevel level, string name, string valueReg
386386

387387
using (ChildProcess git = _git.CreateProcess(gitArgs))
388388
{
389-
git.Start();
389+
git.Start(Trace2ProcessClass.Git);
390390
git.WaitForExit();
391391

392392
switch (git.ExitCode)
@@ -413,7 +413,7 @@ public void UnsetAll(GitConfigurationLevel level, string name, string valueRegex
413413

414414
using (ChildProcess git = _git.CreateProcess(gitArgs))
415415
{
416-
git.Start();
416+
git.Start(Trace2ProcessClass.Git);
417417
git.WaitForExit();
418418

419419
switch (git.ExitCode)

src/shared/Core/PlatformUtils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ private static string GetOSVersion(ITrace2 trace2)
338338

339339
using (var swvers = new ChildProcess(trace2, psi))
340340
{
341-
swvers.Start();
341+
swvers.Start(Trace2ProcessClass.OperatingSystem);
342342
swvers.WaitForExit();
343343

344344
if (swvers.ExitCode == 0)
@@ -359,7 +359,7 @@ private static string GetOSVersion(ITrace2 trace2)
359359

360360
using (var uname = new ChildProcess(trace2, psi))
361361
{
362-
uname.Start();
362+
uname.Start(Trace2ProcessClass.OperatingSystem);
363363
uname.Process.WaitForExit();
364364

365365
if (uname.ExitCode == 0)

0 commit comments

Comments
 (0)