Skip to content

Commit

Permalink
cpu: allow override of /sys dir. Fix CPUInfo /proc + cpuinfo combine.
Browse files Browse the repository at this point in the history
  • Loading branch information
kardianos committed Oct 18, 2015
1 parent ab2cba5 commit 6922174
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ The output is below.
{"total":3179569152,"available":492572672,"used":2895335424,"usedPercent":84.50819439828305, (snip)}

You can set an alternative location to /proc by setting the HOST_PROC environment variable.
You can set an alternative location to /sys by setting the HOST_SYS environment variable.

Documentation
------------------------
Expand Down
12 changes: 9 additions & 3 deletions cpu/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package cpu

import (
"path/filepath"
"fmt"
"errors"
"os/exec"
Expand Down Expand Up @@ -56,9 +57,14 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
return ret, nil
}

func sysCpuPath(cpu int32, relPath string) string {
root := common.GetEnv("HOST_SYS", "/sys")
return filepath.Join(root, fmt.Sprintf("devices/system/cpu/cpu%d", cpu), relPath)
}

func finishCPUInfo(c *CPUInfoStat) error {
if c.Mhz == 0 {
lines, err := common.ReadLines(fmt.Sprintf("/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", c.CPU))
lines, err := common.ReadLines(sysCpuPath(c.CPU, "cpufreq/cpuinfo_max_freq"))
if err == nil {
value, err := strconv.ParseFloat(lines[0], 64)
if err != nil {
Expand All @@ -68,7 +74,7 @@ func finishCPUInfo(c *CPUInfoStat) error {
}
}
if len(c.CoreID) == 0 {
lines, err := common.ReadLines(fmt.Sprintf("/sys/devices/system/cpu/cpu%d/topology/core_id", c.CPU))
lines, err := common.ReadLines(sysCpuPath(c.CPU, "topology/core_id"))
if err == nil {
c.CoreID = lines[0]
}
Expand All @@ -77,7 +83,7 @@ func finishCPUInfo(c *CPUInfoStat) error {
}

func CPUInfo() ([]CPUInfoStat, error) {
filename := common.GetEnv("HOST_PROC", "/proc") + "cpuinfo"
filename := filepath.Join(common.GetEnv("HOST_PROC", "/proc"), "cpuinfo")
lines, _ := common.ReadLines(filename)

var ret []CPUInfoStat
Expand Down

0 comments on commit 6922174

Please sign in to comment.