|
| 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