File: platform.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 (51 lines) | stat: -rw-r--r-- 1,247 bytes parent folder | download | duplicates (2)
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
package platform

import (
	"context"
	"runtime"
	"sync"

	"github.com/containerd/log"
)

var (
	arch     string
	onceArch sync.Once
)

// Architecture returns the runtime architecture of the process.
//
// Unlike [runtime.GOARCH] (which refers to the compiler platform),
// Architecture refers to the running platform.
//
// For example, Architecture reports "x86_64" as architecture, even
// when running a "linux/386" compiled binary on "linux/amd64" hardware.
func Architecture() string {
	onceArch.Do(func() {
		var err error
		arch, err = runtimeArchitecture()
		if err != nil {
			log.G(context.TODO()).WithError(err).Error("Could not read system architecture info")
		}
	})
	return arch
}

// PossibleCPU 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 CPU number ({0}), or a continuous range of CPU numbers ({0,1,2,3}), or
// a non-continuous range of CPU numbers ({0,1,2,3,12,13,14,15}).
func PossibleCPU() []int {
	if ncpu := possibleCPUs(); ncpu != nil {
		return ncpu
	}

	// Fallback in case possibleCPUs() fails.
	var cpus []int
	ncpu := runtime.NumCPU()
	for i := 0; i <= ncpu; i++ {
		cpus = append(cpus, i)
	}

	return cpus
}