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
|
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package hostcpu provides utilities for working with CPU information provided
// by a host Linux kernel.
package hostcpu
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
"unicode"
)
// GetCPU returns the caller's current CPU number, without using the Linux VDSO
// (which is not available to the sentry) or the getcpu(2) system call (which
// is relatively slow).
func GetCPU() uint32
// MaxPossibleCPU returns the highest possible CPU number, which is guaranteed
// not to change for the lifetime of the host kernel.
func MaxPossibleCPU() (uint32, error) {
const path = "/sys/devices/system/cpu/possible"
data, err := ioutil.ReadFile(path)
if err != nil {
return 0, err
}
str := string(data)
// Linux: drivers/base/cpu.c:show_cpus_attr() =>
// include/linux/cpumask.h:cpumask_print_to_pagebuf() =>
// lib/bitmap.c:bitmap_print_to_pagebuf()
i, err := maxValueInLinuxBitmap(str)
if err != nil {
return 0, fmt.Errorf("invalid %s (%q): %v", path, str, err)
}
return uint32(i), nil
}
// maxValueInLinuxBitmap returns the maximum value specified in str, which is a
// string emitted by Linux's lib/bitmap.c:bitmap_print_to_pagebuf(list=true).
func maxValueInLinuxBitmap(str string) (uint64, error) {
str = strings.TrimSpace(str)
// Find the last decimal number in str.
idx := strings.LastIndexFunc(str, func(c rune) bool {
return !unicode.IsDigit(c)
})
if idx != -1 {
str = str[idx+1:]
}
i, err := strconv.ParseUint(str, 10, 64)
if err != nil {
return 0, err
}
return i, nil
}
|