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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
// 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.
// +build 386 amd64
package cpuid
import (
"fmt"
"io/ioutil"
"regexp"
"strconv"
"strings"
"syscall"
"testing"
)
func kernelVersion() (int, int, error) {
var u syscall.Utsname
if err := syscall.Uname(&u); err != nil {
return 0, 0, err
}
var sb strings.Builder
for _, b := range u.Release {
if b == 0 {
break
}
sb.WriteByte(byte(b))
}
s := strings.Split(sb.String(), ".")
if len(s) < 2 {
return 0, 0, fmt.Errorf("kernel release missing major and minor component: %s", sb.String())
}
major, err := strconv.Atoi(s[0])
if err != nil {
return 0, 0, fmt.Errorf("error parsing major version %q in %q: %w", s[0], sb.String(), err)
}
minor, err := strconv.Atoi(s[1])
if err != nil {
return 0, 0, fmt.Errorf("error parsing minor version %q in %q: %w", s[1], sb.String(), err)
}
return major, minor, nil
}
// TestHostFeatureFlags tests that all features detected by HostFeatureSet are
// on the host.
//
// It does *not* verify that all features reported by the host are detected by
// HostFeatureSet.
//
// i.e., test that HostFeatureSet is a subset of the host features.
func TestHostFeatureFlags(t *testing.T) {
cpuinfoBytes, _ := ioutil.ReadFile("/proc/cpuinfo")
cpuinfo := string(cpuinfoBytes)
t.Logf("Host cpu info:\n%s", cpuinfo)
major, minor, err := kernelVersion()
if err != nil {
t.Fatalf("Unable to parse kernel version: %v", err)
}
re := regexp.MustCompile(`(?m)^flags\s+: (.*)$`)
m := re.FindStringSubmatch(cpuinfo)
if len(m) != 2 {
t.Fatalf("Unable to extract flags from %q", cpuinfo)
}
cpuinfoFlags := make(map[string]struct{})
for _, f := range strings.Split(m[1], " ") {
cpuinfoFlags[f] = struct{}{}
}
fs := HostFeatureSet()
// All features have a string and appear in host cpuinfo.
for f := range fs.Set {
name := f.flagString(false)
if name == "" {
t.Errorf("Non-parsable feature: %v", f)
}
// Special cases not consistently visible. We don't mind if
// they are exposed in earlier versions.
switch {
// Block 0.
case f == X86FeatureSDBG && (major < 4 || major == 4 && minor < 3):
// SDBG only exposed in
// b1c599b8ff80ea79b9f8277a3f9f36a7b0cfedce (4.3).
continue
// Block 2.
case f == X86FeatureRDT && (major < 4 || major == 4 && minor < 10):
// RDT only exposed in
// 4ab1586488cb56ed8728e54c4157cc38646874d9 (4.10).
continue
// Block 3.
case f == X86FeatureAVX512VBMI && (major < 4 || major == 4 && minor < 10):
// AVX512VBMI only exposed in
// a8d9df5a509a232a959e4ef2e281f7ecd77810d6 (4.10).
continue
case f == X86FeatureUMIP && (major < 4 || major == 4 && minor < 15):
// UMIP only exposed in
// 3522c2a6a4f341058b8291326a945e2a2d2aaf55 (4.15).
continue
case f == X86FeaturePKU && (major < 4 || major == 4 && minor < 9):
// PKU only exposed in
// dfb4a70f20c5b3880da56ee4c9484bdb4e8f1e65 (4.9).
continue
// Block 4.
case f == X86FeatureXSAVES && (major < 4 || major == 4 && minor < 8):
// XSAVES only exposed in
// b8be15d588060a03569ac85dc4a0247460988f5b (4.8).
continue
// Block 5.
case f == X86FeaturePERFCTR_LLC && (major < 4 || major == 4 && minor < 14):
// PERFCTR_LLC renamed in
// 910448bbed066ab1082b510eef1ae61bb792d854 (4.14).
continue
}
hidden := f.flagString(true) == ""
_, ok := cpuinfoFlags[name]
if hidden && ok {
t.Errorf("Unexpectedly hidden flag: %v", f)
} else if !hidden && !ok {
t.Errorf("Non-native flag: %v", f)
}
}
}
|