Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1 | package hst |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "bufio" |
Matus Fabian | 18c9f14 | 2024-04-29 11:06:44 +0200 | [diff] [blame] | 5 | "errors" |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 6 | "fmt" |
Adrian Villin | 0df582e | 2024-05-22 09:26:47 -0400 | [diff] [blame] | 7 | . "github.com/onsi/ginkgo/v2" |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 8 | "os" |
Matus Fabian | 18c9f14 | 2024-04-29 11:06:44 +0200 | [diff] [blame] | 9 | "os/exec" |
| 10 | "strings" |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 11 | ) |
| 12 | |
Matus Fabian | 18c9f14 | 2024-04-29 11:06:44 +0200 | [diff] [blame] | 13 | var CgroupPath = "/sys/fs/cgroup/" |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 14 | |
| 15 | type CpuContext struct { |
| 16 | cpuAllocator *CpuAllocatorT |
| 17 | cpus []int |
| 18 | } |
| 19 | |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 20 | type CpuAllocatorT struct { |
| 21 | cpus []int |
| 22 | } |
| 23 | |
| 24 | var cpuAllocator *CpuAllocatorT = nil |
| 25 | |
Adrian Villin | b9464cd | 2024-05-27 09:52:59 -0400 | [diff] [blame] | 26 | func (c *CpuAllocatorT) Allocate(containerCount int, nCpus int) (*CpuContext, error) { |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 27 | var cpuCtx CpuContext |
Adrian Villin | b9464cd | 2024-05-27 09:52:59 -0400 | [diff] [blame] | 28 | |
| 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 Villin | 0df582e | 2024-05-22 09:26:47 -0400 | [diff] [blame] | 36 | err := fmt.Errorf("could not allocate %d CPUs; available: %d; attempted to allocate cores %d-%d", |
Adrian Villin | b9464cd | 2024-05-27 09:52:59 -0400 | [diff] [blame] | 37 | nCpus*containerCount, len(c.cpus)-1, minCpu, maxCpu) |
Adrian Villin | 0df582e | 2024-05-22 09:26:47 -0400 | [diff] [blame] | 38 | return nil, err |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 39 | } |
Adrian Villin | b9464cd | 2024-05-27 09:52:59 -0400 | [diff] [blame] | 40 | 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 Villin | 0df582e | 2024-05-22 09:26:47 -0400 | [diff] [blame] | 44 | } else { |
Adrian Villin | b9464cd | 2024-05-27 09:52:59 -0400 | [diff] [blame] | 45 | return nil, fmt.Errorf("too many containers; CPU allocation for >%d containers is not implemented", maxContainerCount) |
Adrian Villin | 0df582e | 2024-05-22 09:26:47 -0400 | [diff] [blame] | 46 | } |
| 47 | |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 48 | cpuCtx.cpuAllocator = c |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 49 | return &cpuCtx, nil |
| 50 | } |
| 51 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 52 | func (c *CpuAllocatorT) readCpus() error { |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 53 | var first, last int |
Matus Fabian | 18c9f14 | 2024-04-29 11:06:44 +0200 | [diff] [blame] | 54 | |
| 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 Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 73 | 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 | |
| 91 | func CpuAllocator() (*CpuAllocatorT, error) { |
| 92 | if cpuAllocator == nil { |
| 93 | cpuAllocator = new(CpuAllocatorT) |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 94 | err := cpuAllocator.readCpus() |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 95 | if err != nil { |
| 96 | return nil, err |
| 97 | } |
| 98 | } |
| 99 | return cpuAllocator, nil |
| 100 | } |