-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathCGroupV2.cs
More file actions
68 lines (58 loc) · 2.8 KB
/
CGroupV2.cs
File metadata and controls
68 lines (58 loc) · 2.8 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
60
61
62
63
64
65
66
67
68
// 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
{
internal class CGroupV2 : CGroup
{
public override async Task SetAsync(Job job)
{
var controller = GetCGroupController(job);
if (job.MemoryLimitInBytes > 0)
{
await ProcessUtil.RunAsync("cgset", $"-r memory.max={job.MemoryLimitInBytes} {controller}", log: true);
}
else
{
await ProcessUtil.RunAsync("cgset", $"-r memory.max=max {controller}", log: true);
}
if (job.CpuLimitRatio > 0)
{
// $MAX $PERIOD
// which indicates that the group may consume up to $MAX in each $PERIOD duration. "max" for $MAX indicates no limit. If only one number is written, $MAX is updated.
await ProcessUtil.RunAsync("cgset", $"-r cpu.max=\"{Math.Floor(job.CpuLimitRatio * DefaultDockerCfsPeriod)} {DefaultDockerCfsPeriod}\" {controller}", log: true);
}
else
{
await ProcessUtil.RunAsync("cgset", $"-r cpu.max=\"max {DefaultDockerCfsPeriod}\" {controller}", log: true);
}
if (!String.IsNullOrEmpty(job.CpuSet))
{
await ProcessUtil.RunAsync("cgset", $"-r cpuset.cpus={job.CpuSet} {controller}", log: true);
}
else
{
await ProcessUtil.RunAsync("cgset", $"-r cpuset.cpus=0-{Environment.ProcessorCount - 1} {controller}", log: true);
}
// The cpuset.mems value for the 'benchmarks' controller needs to match the root one
// to be compatible with the allowed nodes
var memsRoot = await File.ReadAllTextAsync($"/sys/fs/cgroup/{controller}/cpuset.mems");
if (String.IsNullOrWhiteSpace(memsRoot))
{
memsRoot = await File.ReadAllTextAsync($"/sys/fs/cgroup/{controller}/cpuset.mems.effective");
}
// Both cpus and mems need to be initialized
await ProcessUtil.RunAsync("cgset", $"-r cpuset.mems={memsRoot.Trim()} {controller}", log: true);
}
public override async Task<string> GetCpuStatAsync(Job job)
{
var controller = GetCGroupController(job);
var result = await ProcessUtil.RunAsync("cat", $"/sys/fs/cgroup/{controller}/cpu.stat", throwOnError: false, captureOutput: true);
return result.StandardOutput;
}
}
}