cpu: CPUInfo returns one item per physical thread. Fix "Cores" to 1 on Linux.

Fixes #104
pull/105/head
Daniel Theophanes 10 years ago
parent 127599b2cf
commit fa7f8ba4e8

@ -80,13 +80,20 @@ func finishCPUInfo(c *CPUInfoStat) error {
return nil return nil
} }
// CPUInfo on linux will return 1 item per physical thread.
//
// CPUs have three levels of counting: sockets, cores, threads.
// Cores with HyperThreading count as having 2 threads per core.
// Sockets often come with many physical CPU cores.
// For example a single socket board with two cores each with HT will
// return 4 CPUInfoStat structs on Linux and the "Cores" field set to 1.
func CPUInfo() ([]CPUInfoStat, error) { func CPUInfo() ([]CPUInfoStat, error) {
filename := common.HostProc("cpuinfo") filename := common.HostProc("cpuinfo")
lines, _ := common.ReadLines(filename) lines, _ := common.ReadLines(filename)
var ret []CPUInfoStat var ret []CPUInfoStat
c := CPUInfoStat{CPU: -1} c := CPUInfoStat{CPU: -1, Cores: 1}
for _, line := range lines { for _, line := range lines {
fields := strings.Split(line, ":") fields := strings.Split(line, ":")
if len(fields) < 2 { if len(fields) < 2 {
@ -104,7 +111,7 @@ func CPUInfo() ([]CPUInfoStat, error) {
} }
ret = append(ret, c) ret = append(ret, c)
} }
c = CPUInfoStat{} c = CPUInfoStat{Cores: 1}
t, err := strconv.ParseInt(value, 10, 64) t, err := strconv.ParseInt(value, 10, 64)
if err != nil { if err != nil {
return ret, err return ret, err
@ -140,12 +147,6 @@ func CPUInfo() ([]CPUInfoStat, error) {
c.PhysicalID = value c.PhysicalID = value
case "core id": case "core id":
c.CoreID = value c.CoreID = value
case "cpu cores":
t, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return ret, err
}
c.Cores = int32(t)
case "flags", "Features": case "flags", "Features":
c.Flags = strings.FieldsFunc(value, func(r rune) bool { c.Flags = strings.FieldsFunc(value, func(r rune) bool {
return r == ',' || r == ' ' return r == ',' || r == ' '

Loading…
Cancel
Save