File: platform_linux.go

package info (click to toggle)
docker.io 28.5.2%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 69,048 kB
  • sloc: sh: 5,867; makefile: 863; ansic: 184; python: 162; asm: 159
file content (58 lines) | stat: -rw-r--r-- 1,445 bytes parent folder | download
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
// TODO(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
//go:build go1.23

package platform

import (
	"os"
	"strconv"
	"strings"
	"sync"
)

// possibleCPUs returns the set of possible CPUs on the host (which is
// equal or larger to the number of CPUs currently online). The returned
// set may be a single number ({0}), or a continuous range ({0,1,2,3}), or
// a non-continuous range ({0,1,2,3,12,13,14,15})
//
// Returns nil on errors. Assume CPUs are 0 -> runtime.NumCPU() in that case.
var possibleCPUs = sync.OnceValue(func() []int {
	data, err := os.ReadFile("/sys/devices/system/cpu/possible")
	if err != nil {
		return nil
	}
	content := strings.TrimSpace(string(data))
	return parsePossibleCPUs(content)
})

func parsePossibleCPUs(content string) []int {
	ranges := strings.Split(content, ",")

	var cpus []int
	for _, r := range ranges {
		// Each entry is either a single number (e.g., "0") or a continuous range
		// (e.g., "0-3").
		if rStart, rEnd, ok := strings.Cut(r, "-"); !ok {
			cpu, err := strconv.Atoi(rStart)
			if err != nil {
				return nil
			}
			cpus = append(cpus, cpu)
		} else {
			var start, end int
			start, err := strconv.Atoi(rStart)
			if err != nil {
				return nil
			}
			end, err = strconv.Atoi(rEnd)
			if err != nil {
				return nil
			}
			for i := start; i <= end; i++ {
				cpus = append(cpus, i)
			}
		}
	}

	return cpus
}