blob: b5555d85b9832cc5246ac3a63ee3cafc8e96e000 [file] [log] [blame]
Adrian Villin4677d922024-06-14 09:32:39 +02001package hst
Filip Tehlar608d0062023-04-28 10:29:47 +02002
3import (
4 "bufio"
Matus Fabian18c9f142024-04-29 11:06:44 +02005 "errors"
Filip Tehlar608d0062023-04-28 10:29:47 +02006 "fmt"
Adrian Villin0df582e2024-05-22 09:26:47 -04007 . "github.com/onsi/ginkgo/v2"
Filip Tehlar608d0062023-04-28 10:29:47 +02008 "os"
Matus Fabian18c9f142024-04-29 11:06:44 +02009 "os/exec"
10 "strings"
Filip Tehlar608d0062023-04-28 10:29:47 +020011)
12
Matus Fabian18c9f142024-04-29 11:06:44 +020013var CgroupPath = "/sys/fs/cgroup/"
Filip Tehlar608d0062023-04-28 10:29:47 +020014
15type CpuContext struct {
16 cpuAllocator *CpuAllocatorT
17 cpus []int
18}
19
Filip Tehlar608d0062023-04-28 10:29:47 +020020type CpuAllocatorT struct {
21 cpus []int
22}
23
24var cpuAllocator *CpuAllocatorT = nil
25
Adrian Villinb9464cd2024-05-27 09:52:59 -040026func (c *CpuAllocatorT) Allocate(containerCount int, nCpus int) (*CpuContext, error) {
Filip Tehlar608d0062023-04-28 10:29:47 +020027 var cpuCtx CpuContext
Adrian Villinb9464cd2024-05-27 09:52:59 -040028
29 // splitting cpus into equal parts; this will over-allocate cores but it's good enough for now
30 maxContainerCount := 4
31 // skip CPU 0
32 minCpu := ((GinkgoParallelProcess() - 1) * maxContainerCount * nCpus) + 1
33 maxCpu := (GinkgoParallelProcess() * maxContainerCount * nCpus)
34
35 if len(c.cpus)-1 < maxCpu {
Adrian Villin0df582e2024-05-22 09:26:47 -040036 err := fmt.Errorf("could not allocate %d CPUs; available: %d; attempted to allocate cores %d-%d",
Adrian Villinb9464cd2024-05-27 09:52:59 -040037 nCpus*containerCount, len(c.cpus)-1, minCpu, maxCpu)
Adrian Villin0df582e2024-05-22 09:26:47 -040038 return nil, err
Filip Tehlar608d0062023-04-28 10:29:47 +020039 }
Adrian Villinb9464cd2024-05-27 09:52:59 -040040 if containerCount == 1 {
41 cpuCtx.cpus = c.cpus[minCpu : minCpu+nCpus]
42 } else if containerCount > 1 && containerCount <= maxContainerCount {
43 cpuCtx.cpus = c.cpus[minCpu+(nCpus*(containerCount-1)) : minCpu+(nCpus*containerCount)]
Adrian Villin0df582e2024-05-22 09:26:47 -040044 } else {
Adrian Villinb9464cd2024-05-27 09:52:59 -040045 return nil, fmt.Errorf("too many containers; CPU allocation for >%d containers is not implemented", maxContainerCount)
Adrian Villin0df582e2024-05-22 09:26:47 -040046 }
47
Filip Tehlar608d0062023-04-28 10:29:47 +020048 cpuCtx.cpuAllocator = c
Filip Tehlar608d0062023-04-28 10:29:47 +020049 return &cpuCtx, nil
50}
51
Adrian Villincee15aa2024-03-14 11:42:55 -040052func (c *CpuAllocatorT) readCpus() error {
Filip Tehlar608d0062023-04-28 10:29:47 +020053 var first, last int
Matus Fabian18c9f142024-04-29 11:06:44 +020054
55 // Path depends on cgroup version. We need to check which version is in use.
56 // For that following command can be used: 'stat -fc %T /sys/fs/cgroup/'
57 // In case the output states 'cgroup2fs' then cgroups v2 is used, 'tmpfs' in case cgroups v1.
58 cmd := exec.Command("stat", "-fc", "%T", "/sys/fs/cgroup/")
59 byteOutput, err := cmd.CombinedOutput()
60 if err != nil {
61 return err
62 }
63 CpuPath := CgroupPath
64 if strings.Contains(string(byteOutput), "tmpfs") {
65 CpuPath += "cpuset/cpuset.effective_cpus"
66 } else if strings.Contains(string(byteOutput), "cgroup2fs") {
67 CpuPath += "cpuset.cpus.effective"
68 } else {
69 return errors.New("cgroup unknown fs: " + string(byteOutput))
70 }
71
72 file, err := os.Open(CpuPath)
Filip Tehlar608d0062023-04-28 10:29:47 +020073 if err != nil {
74 return err
75 }
76 defer file.Close()
77
78 sc := bufio.NewScanner(file)
79 sc.Scan()
80 line := sc.Text()
81 _, err = fmt.Sscanf(line, "%d-%d", &first, &last)
82 if err != nil {
83 return err
84 }
85 for i := first; i <= last; i++ {
86 c.cpus = append(c.cpus, i)
87 }
88 return nil
89}
90
91func CpuAllocator() (*CpuAllocatorT, error) {
92 if cpuAllocator == nil {
93 cpuAllocator = new(CpuAllocatorT)
Adrian Villincee15aa2024-03-14 11:42:55 -040094 err := cpuAllocator.readCpus()
Filip Tehlar608d0062023-04-28 10:29:47 +020095 if err != nil {
96 return nil, err
97 }
98 }
99 return cpuAllocator, nil
100}