forked from kata-containers/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgroup.go
65 lines (54 loc) · 1.52 KB
/
cgroup.go
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
//
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"io/ioutil"
"strings"
"github.com/docker/docker/pkg/parsers"
"github.com/sirupsen/logrus"
)
// set function in variable to overwrite for testing.
var getCpusetGuest = func() (string, error) {
cpusetGuestByte, err := ioutil.ReadFile("/sys/devices/system/cpu/online")
if err != nil {
return "", err
}
return strings.TrimSpace(string(cpusetGuestByte)), nil
}
// Return the best match for cpuset list in the guest.
// The runtime caller may apply cpuset for specific CPUs in the host.
// The CPUs may not exist on the guest as they are hotplugged based
// on cpu and qouta.
// This function return a working cpuset to apply on the guest.
func getAvailableCpusetList(cpusetReq string) (string, error) {
cpusetGuest, err := getCpusetGuest()
if err != nil {
return "", err
}
cpusetListReq, err := parsers.ParseUintList(cpusetReq)
if err != nil {
return "", err
}
cpusetGuestList, err := parsers.ParseUintList(cpusetGuest)
if err != nil {
return "", err
}
for k := range cpusetListReq {
if !cpusetGuestList[k] {
agentLog.WithFields(logrus.Fields{
"cpuset": cpusetReq,
"cpu": k,
"guest-cpus": cpusetGuest,
}).Warnf("cpu is not in guest cpu list, using guest cpus")
return cpusetGuest, nil
}
}
// All the cpus are valid keep the same cpuset string
agentLog.WithFields(logrus.Fields{
"cpuset": cpusetReq,
}).Debugf("the requested cpuset is valid, using it")
return cpusetReq, nil
}