Standardize `Mhz` to mean maximum CPU frequency on Linux platform

* resolve #249
* in `cpu_windows.go`, `Mhz` is the value of `MaxClockSpeed`
* on Linux platform, the `Mhz` value is extracted from `/proc/cpuinfo`
  which reflects the current clock speed; treat this as the fallback
  value instead
* read from `cpufreq/cpuinfo_max_freq` under sysfs to get the
  maximum clock speed for `Mhz`, just like for Windows platform
* also fix the path to `cpu.CoreID` value; the filename is `core_id`
pull/253/head
K.C. Wong 9 years ago
parent 79184fee44
commit 10a1ae2123

@ -65,22 +65,29 @@ func sysCPUPath(cpu int32, relPath string) string {
} }
func finishCPUInfo(c *InfoStat) error { func finishCPUInfo(c *InfoStat) error {
if c.Mhz == 0 { var lines []string
lines, err := common.ReadLines(sysCPUPath(c.CPU, "cpufreq/cpuinfo_max_freq")) var err error
if err == nil { var value float64
value, err := strconv.ParseFloat(lines[0], 64)
if err != nil {
return err
}
c.Mhz = value/1000.0 // value is in kHz
}
}
if len(c.CoreID) == 0 { if len(c.CoreID) == 0 {
lines, err := common.ReadLines(sysCPUPath(c.CPU, "topology/coreId")) lines, err = common.ReadLines(sysCPUPath(c.CPU, "topology/core_id"))
if err == nil { if err == nil {
c.CoreID = lines[0] c.CoreID = lines[0]
} }
} }
// override the value of c.Mhz with cpufreq/cpuinfo_max_freq regardless
// of the value from /proc/cpuinfo because we want to report the maximum
// clock-speed of the CPU for c.Mhz, matching the behaviour of Windows
lines, err = common.ReadLines(sysCPUPath(c.CPU, "cpufreq/cpuinfo_max_freq"))
if err != nil {
return err
}
value, err = strconv.ParseFloat(lines[0], 64)
if err != nil {
return err
}
c.Mhz = value/1000.0 // value is in kHz
return nil return nil
} }
@ -136,11 +143,10 @@ func Info() ([]InfoStat, error) {
} }
c.Stepping = int32(t) c.Stepping = int32(t)
case "cpu MHz": case "cpu MHz":
t, err := strconv.ParseFloat(value, 64) // treat this as the fallback value, thus we ignore error
if err != nil { if t, err := strconv.ParseFloat(value, 64); err == nil {
return ret, err c.Mhz = t
} }
c.Mhz = t
case "cache size": case "cache size":
t, err := strconv.ParseInt(strings.Replace(value, " KB", "", 1), 10, 64) t, err := strconv.ParseInt(strings.Replace(value, " KB", "", 1), 10, 64)
if err != nil { if err != nil {

Loading…
Cancel
Save