-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathCGroup.cs
More file actions
59 lines (45 loc) · 1.99 KB
/
CGroup.cs
File metadata and controls
59 lines (45 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Licensed to the .NET Foundation under one or more agreements.
// 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.IO;
using System.Threading.Tasks;
using Microsoft.Crank.Models;
namespace Microsoft.Crank.Agent
{
public abstract class CGroup
{
// https://docs.docker.com/config/containers/resource_constraints/
public const double DefaultDockerCfsPeriod = 100000;
public static string GetCGroupController(Job job)
{
// Create a unique cgroup controller per agent
return $"benchmarks-{Environment.ProcessId}-{job.Id}";
}
public static async Task<CGroup> GetCGroupVersionAsync()
{
var statResult = await ProcessUtil.RunAsync("stat", "-fc %T /sys/fs/cgroup/", captureOutput: true, log: true);
var isV2 = statResult.StandardOutput.Trim() == "cgroup2fs";
return isV2 ? new CGroupV2() : new CGroupV1();
}
public abstract Task SetAsync(Job job);
public abstract Task<string> GetCpuStatAsync(Job job);
public virtual async Task DeleteAsync(Job job)
{
var controller = GetCGroupController(job);
await ProcessUtil.RunAsync("cgdelete", $"cpu,memory,cpuset:{controller}", log: true, throwOnError: false);
}
public virtual async Task<(string executable, string commandLine)> CreateAsync(Job job)
{
var controller = GetCGroupController(job);
var cgcreate = await ProcessUtil.RunAsync("cgcreate", $"-g memory,cpu,cpuset:{controller}", log: true);
if (cgcreate.ExitCode > 0)
{
job.Error += "Could not create cgroup";
return (null, null);
}
await SetAsync(job);
return ("cgexec", $"-g memory,cpu,cpuset:{controller}");
}
}
}