Skip to content

Commit

Permalink
procfs: add topology-related x86 cpuinfo fields
Browse files Browse the repository at this point in the history
Linux prints these fields when the kernel is built with support for multiple
processors/cores (CONFIG_SMP), in
arch/x86/kernel/cpu/proc.c:show_cpuinfo_core().

Fixes #10205

PiperOrigin-RevId: 632621375
  • Loading branch information
nixprime authored and gvisor-bot committed May 10, 2024
1 parent eb0505c commit f84a013
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
8 changes: 7 additions & 1 deletion pkg/cpuid/cpuid_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func (fs FeatureSet) HasFeature(feature Feature) bool {
// WriteCPUInfoTo is to generate a section of one cpu in /proc/cpuinfo. This is
// a minimal /proc/cpuinfo, it is missing some fields like "microcode" that are
// not always printed in Linux. The bogomips field is simply made up.
func (fs FeatureSet) WriteCPUInfoTo(cpu uint, w io.Writer) {
func (fs FeatureSet) WriteCPUInfoTo(cpu, numCPU uint, w io.Writer) {
// Avoid many redundant calls here, since this can occasionally appear
// in the hot path. Read all basic information up front, see above.
ax, _, _, _ := fs.query(featureInfo)
Expand All @@ -322,6 +322,12 @@ func (fs FeatureSet) WriteCPUInfoTo(cpu uint, w io.Writer) {
fmt.Fprintf(w, "model name\t: %s\n", "unknown") // Unknown for now.
fmt.Fprintf(w, "stepping\t: %s\n", "unknown") // Unknown for now.
fmt.Fprintf(w, "cpu MHz\t\t: %.3f\n", cpuFreqMHz)
fmt.Fprintf(w, "physical id\t: 0\n") // Pretend all CPUs are in the same socket.
fmt.Fprintf(w, "siblings\t: %d\n", numCPU)
fmt.Fprintf(w, "core id\t\t: %d\n", cpu)
fmt.Fprintf(w, "cpu cores\t: %d\n", numCPU) // Pretend each CPU is a distinct core (rather than a hyperthread).
fmt.Fprintf(w, "apicid\t\t: %d\n", cpu)
fmt.Fprintf(w, "initial apicid\t: %d\n", cpu)
fmt.Fprintf(w, "fpu\t\t: yes\n")
fmt.Fprintf(w, "fpu_exception\t: yes\n")
fmt.Fprintf(w, "cpuid level\t: %d\n", uint32(xSaveInfo)) // Same as ax in vendorID.
Expand Down
2 changes: 1 addition & 1 deletion pkg/cpuid/cpuid_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (fs FeatureSet) HasFeature(feature Feature) bool {

// WriteCPUInfoTo is to generate a section of one cpu in /proc/cpuinfo. This is
// a minimal /proc/cpuinfo, and the bogomips field is simply made up.
func (fs FeatureSet) WriteCPUInfoTo(cpu uint, w io.Writer) {
func (fs FeatureSet) WriteCPUInfoTo(cpu, numCPU uint, w io.Writer) {
fmt.Fprintf(w, "processor\t: %d\n", cpu)
fmt.Fprintf(w, "BogoMIPS\t: %.02f\n", fs.cpuFreqMHz) // It's bogus anyway.
fmt.Fprintf(w, "Features\t\t: %s\n", fs.FlagString())
Expand Down
2 changes: 1 addition & 1 deletion pkg/sentry/fsimpl/proc/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func cpuInfoData(k *kernel.Kernel) string {
features := k.FeatureSet()
var buf bytes.Buffer
for i, max := uint(0), k.ApplicationCores(); i < max; i++ {
features.WriteCPUInfoTo(i, &buf)
features.WriteCPUInfoTo(i, max, &buf)
}
return buf.String()
}
Expand Down
6 changes: 6 additions & 0 deletions test/syscalls/linux/proc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ static const char* required_fields[] = {
"model name",
"stepping",
"cpu MHz",
"physical id",
"siblings",
"core id",
"cpu cores",
"apicid\t\t:",
"initial apicid",
"fpu\t\t:",
"fpu_exception",
"cpuid level",
Expand Down

0 comments on commit f84a013

Please sign in to comment.