Skip to content

Commit

Permalink
Tracking child process (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros authored Jul 15, 2020
1 parent 3843ac3 commit 841112f
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/Microsoft.Crank.Agent/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -870,22 +870,30 @@ private static async Task ProcessJobs(string hostname, string dockerHostname, Ca
{
// TODO: Accessing the TotalProcessorTime on OSX throws so just leave it as 0 for now
// We need to dig into this
var newCPUTime = OperatingSystem == OperatingSystem.OSX ? TimeSpan.Zero : process.TotalProcessorTime;
var trackProcess = job.ChildProcessId == 0
? process
: Process.GetProcessById(job.ChildProcessId)
;

var newCPUTime = OperatingSystem == OperatingSystem.OSX
? TimeSpan.Zero
: trackProcess.TotalProcessorTime;

var elapsed = now.Subtract(lastMonitorTime).TotalMilliseconds;
var rawCpu = (newCPUTime - oldCPUTime).TotalMilliseconds / elapsed * 100;
var cpu = Math.Round(rawCpu / Environment.ProcessorCount);
lastMonitorTime = now;

process.Refresh();
trackProcess.Refresh();

// Ignore first measure
if (oldCPUTime != TimeSpan.Zero)
if (oldCPUTime != TimeSpan.Zero && cpu <= 100)
{
job.Measurements.Enqueue(new Measurement
{
Name = "benchmarks/working-set",
Timestamp = now,
Value = Math.Ceiling((double)process.WorkingSet64 / 1024 / 1024) // < 1MB still needs to appear as 1MB
Value = Math.Ceiling((double)trackProcess.WorkingSet64 / 1024 / 1024) // < 1MB still needs to appear as 1MB
});

job.Measurements.Enqueue(new Measurement
Expand Down Expand Up @@ -3421,6 +3429,15 @@ private static async Task<Process> StartProcess(string hostname, string benchmar
}
}
}

// Detect the app is wrapping a child process
var processIdMarker = "##ChildProcessId:";
if (e.Data.StartsWith(processIdMarker)
&& int.TryParse(e.Data.Substring(processIdMarker.Length), out var childProcessId))
{
Log.WriteLine($"Tracking child process id: {childProcessId}");
job.ChildProcessId = childProcessId;
}
}
};

Expand Down
6 changes: 6 additions & 0 deletions src/Microsoft.Crank.Jobs.Bombardier/BenchmarksEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics.Tracing;

namespace Benchmarks
Expand Down Expand Up @@ -59,5 +60,10 @@ public void Metadata(string name, string aggregate, string reduce, string shortD
{
WriteEvent(5, name, aggregate, reduce, shortDescription, longDescription, format);
}

public static void SetChildProcessId(int pid)
{
Console.Error.WriteLine($"##ChildProcessId:{pid}");
}
}
}
3 changes: 3 additions & 0 deletions src/Microsoft.Crank.Jobs.Bombardier/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ static async Task Main(string[] args)
Console.WriteLine("> bombardier " + process.StartInfo.Arguments);

process.Start();

BenchmarksEventSource.SetChildProcessId(process.Id);

process.BeginOutputReadLine();
process.WaitForExit();

Expand Down
3 changes: 3 additions & 0 deletions src/Microsoft.Crank.Jobs.Wrk/WrkProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ static void RunCore(string fileName, string[] args, bool parseLatency)
Console.WriteLine("> wrk " + process.StartInfo.Arguments);

process.Start();

BenchmarksEventSource.SetChildProcessId(process.Id);

process.BeginOutputReadLine();
process.WaitForExit();

Expand Down
6 changes: 6 additions & 0 deletions src/Microsoft.Crank.Jobs.Wrk2/BenchmarksEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics.Tracing;

namespace Microsoft.Crank.Jobs
Expand Down Expand Up @@ -59,5 +60,10 @@ public void Metadata(string name, string aggregate, string reduce, string shortD
{
WriteEvent(5, name, aggregate, reduce, shortDescription, longDescription, format);
}

public static void SetChildProcessId(int pid)
{
Console.Error.WriteLine($"##ChildProcessId:{pid}");
}
}
}
3 changes: 3 additions & 0 deletions src/Microsoft.Crank.Jobs.Wrk2/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ static async Task Main(string[] args)
Console.WriteLine("> wrk2 " + process.StartInfo.Arguments);

process.Start();

BenchmarksEventSource.SetChildProcessId(process.Id);

process.BeginOutputReadLine();
process.WaitForExit();

Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.Crank.Models/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public class Job
public List<string> CounterProviders { get; set; } = new List<string>();
public string BasePath { get; set; }
public int ProcessId { get; set; }
public int ChildProcessId { get; set; }
public Dictionary<string, string> EnvironmentVariables { get; set; } = new Dictionary<string, string>();
public List<string> BuildArguments { get; set; } = new List<string>();
public bool NoClean { get; set; }
Expand Down

0 comments on commit 841112f

Please sign in to comment.