aboutsummaryrefslogtreecommitdiffstats
path: root/extras/hs-test/cpu.go
blob: 69b4cabd4e35f8cb3fdbc4ed1f1bc06a9a6b90ce (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main

import (
	"bufio"
	"errors"
	"fmt"
	. "github.com/onsi/ginkgo/v2"
	"os"
	"os/exec"
	"strings"
)

var CgroupPath = "/sys/fs/cgroup/"

type CpuContext struct {
	cpuAllocator *CpuAllocatorT
	cpus         []int
}

type CpuAllocatorT struct {
	cpus []int
}

var cpuAllocator *CpuAllocatorT = nil

func (c *CpuAllocatorT) Allocate(vppContainerCount int, nCpus int) (*CpuContext, error) {
	var cpuCtx CpuContext
	maxCpu := GinkgoParallelProcess() * 2 * nCpus
	minCpu := (GinkgoParallelProcess() - 1) * 2 * nCpus
	if len(c.cpus) < maxCpu {
		vppContainerCount += 1
		err := fmt.Errorf("could not allocate %d CPUs; available: %d; attempted to allocate cores %d-%d",
			nCpus*vppContainerCount, len(c.cpus), minCpu, minCpu+nCpus*vppContainerCount)
		return nil, err
	}
	if vppContainerCount == 0 {
		cpuCtx.cpus = c.cpus[minCpu : maxCpu-nCpus]
	} else if vppContainerCount == 1 {
		cpuCtx.cpus = c.cpus[minCpu+nCpus : maxCpu]
	} else {
		return nil, fmt.Errorf("too many VPP containers; CPU allocation for >2 VPP containers is not implemented yet")
	}

	cpuCtx.cpuAllocator = c
	return &cpuCtx, nil
}

func (c *CpuAllocatorT) readCpus() error {
	var first, last int

	// Path depends on cgroup version. We need to check which version is in use.
	// For that following command can be used: 'stat -fc %T /sys/fs/cgroup/'
	// In case the output states 'cgroup2fs' then cgroups v2 is used, 'tmpfs' in case cgroups v1.
	cmd := exec.Command("stat", "-fc", "%T", "/sys/fs/cgroup/")
	byteOutput, err := cmd.CombinedOutput()
	if err != nil {
		return err
	}
	CpuPath := CgroupPath
	if strings.Contains(string(byteOutput), "tmpfs") {
		CpuPath += "cpuset/cpuset.effective_cpus"
	} else if strings.Contains(string(byteOutput), "cgroup2fs") {
		CpuPath += "cpuset.cpus.effective"
	} else {
		return errors.New("cgroup unknown fs: " + string(byteOutput))
	}

	file, err := os.Open(CpuPath)
	if err != nil {
		return err
	}
	defer file.Close()

	sc := bufio.NewScanner(file)
	sc.Scan()
	line := sc.Text()
	_, err = fmt.Sscanf(line, "%d-%d", &first, &last)
	if err != nil {
		return err
	}
	for i := first; i <= last; i++ {
		c.cpus = append(c.cpus, i)
	}
	return nil
}

func CpuAllocator() (*CpuAllocatorT, error) {
	if cpuAllocator == nil {
		cpuAllocator = new(CpuAllocatorT)
		err := cpuAllocator.readCpus()
		if err != nil {
			return nil, err
		}
	}
	return cpuAllocator, nil
}