Skip to content

Commit f3900b2

Browse files
committed
cmd: add subcommand cpu-features to dump supported CPU features
This patch adds the subcommand cpu-features, used for retrieving the features supported by the current physical machine's CPU. Signed-off-by: Tianyu Zhou <[email protected]>
1 parent 93333d2 commit f3900b2

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

runsc/cli/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ func forEachCmd(cb func(cmd subcommands.Command, group string)) {
280280
cb(new(cmd.Uninstall), helperGroup)
281281
cb(new(nvproxy.Nvproxy), helperGroup)
282282
cb(new(trace.Trace), helperGroup)
283+
cb(new(cmd.CPUFeatures), helperGroup)
283284

284285
const debugGroup = "debug"
285286
cb(new(cmd.Debug), debugGroup)

runsc/cmd/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ go_library(
3939
"chroot.go",
4040
"cmd.go",
4141
"create.go",
42+
"cpu_features.go",
4243
"debug.go",
4344
"delete.go",
4445
"do.go",

runsc/cmd/cpu_features.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2025 The gVisor Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"strings"
21+
22+
"github.com/google/subcommands"
23+
"gvisor.dev/gvisor/pkg/cpuid"
24+
"gvisor.dev/gvisor/runsc/flag"
25+
)
26+
27+
// List implements subcommands.Command for the "cpu-features" command.
28+
type CPUFeatures struct{}
29+
30+
// Name implements subcommands.command.name.
31+
func (*CPUFeatures) Name() string {
32+
return "cpu-features"
33+
}
34+
35+
// Synopsis implements subcommands.Command.Synopsis.
36+
func (*CPUFeatures) Synopsis() string {
37+
return "list CPU features supported on current machine"
38+
}
39+
40+
// Usage implements subcommands.Command.Usage.
41+
func (*CPUFeatures) Usage() string {
42+
return "cpu-features\n"
43+
}
44+
45+
// SetFlags implements subcommands.Command.SetFlags.
46+
func (*CPUFeatures) SetFlags(*flag.FlagSet) {}
47+
48+
// Execute implements subcommands.Command.Execute.
49+
func (*CPUFeatures) Execute(_ context.Context, _ *flag.FlagSet, args ...any) subcommands.ExitStatus {
50+
cpuid.Initialize()
51+
hfs := cpuid.HostFeatureSet().Fixed()
52+
allFeatures := cpuid.AllFeatures()
53+
54+
features := []string{}
55+
for _, v := range allFeatures {
56+
if hfs.HasFeature(v) {
57+
features = append(features, v.String())
58+
}
59+
}
60+
fmt.Println(strings.Join(features, ","))
61+
62+
return subcommands.ExitSuccess
63+
}

0 commit comments

Comments
 (0)