Skip to content

Commit

Permalink
Merge pull request #375 from rjnagal/cpu
Browse files Browse the repository at this point in the history
Add cpu frequency to machine info.
  • Loading branch information
vmarmol committed Dec 18, 2014
2 parents 6455f81 + c37f386 commit 37db214
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions info/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ type MachineInfo struct {
// The number of cores in this machine.
NumCores int `json:"num_cores"`

// Maximum clock speed for the cores, in KHz.
CpuFrequency uint64 `json:"cpu_frequency_khz"`

// The amount of memory (in bytes) in this machine
MemoryCapacity int64 `json:"memory_capacity"`

Expand Down
35 changes: 35 additions & 0 deletions manager/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,42 @@ import (
"github.com/google/cadvisor/container/docker"
"github.com/google/cadvisor/fs"
"github.com/google/cadvisor/info"
"github.com/google/cadvisor/utils"
"github.com/google/cadvisor/utils/sysfs"
)

var numCpuRegexp = regexp.MustCompile("processor\\t*: +[0-9]+")
var CpuClockSpeedMHz = regexp.MustCompile("cpu MHz\\t*: +([0-9]+.[0-9]+)")
var memoryCapacityRegexp = regexp.MustCompile("MemTotal: *([0-9]+) kB")

func getClockSpeed(procInfo []byte) (uint64, error) {
// First look through sys to find a max supported cpu frequency.
const maxFreqFile = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"
if utils.FileExists(maxFreqFile) {
val, err := ioutil.ReadFile(maxFreqFile)
if err != nil {
return 0, err
}
var maxFreq uint64
n, err := fmt.Sscanf(string(val), "%d", &maxFreq)
if err != nil || n != 1 {
return 0, fmt.Errorf("could not parse frequency %q", val)
}
return maxFreq, nil
}
// Fall back to /proc/cpuinfo
matches := CpuClockSpeedMHz.FindSubmatch(procInfo)
if len(matches) != 2 {
return 0, fmt.Errorf("could not detect clock speed from output: %q", string(procInfo))
}
speed, err := strconv.ParseFloat(string(matches[1]), 64)
if err != nil {
return 0, err
}
// Convert to kHz
return uint64(speed * 1000), nil
}

func getMachineInfo(sysFs sysfs.SysFs) (*info.MachineInfo, error) {
// Get the number of CPUs from /proc/cpuinfo.
out, err := ioutil.ReadFile("/proc/cpuinfo")
Expand All @@ -43,6 +73,10 @@ func getMachineInfo(sysFs sysfs.SysFs) (*info.MachineInfo, error) {
if numCores == 0 {
return nil, fmt.Errorf("failed to count cores in output: %s", string(out))
}
clockSpeed, err := getClockSpeed(out)
if err != nil {
return nil, err
}

// Get the amount of usable memory from /proc/meminfo.
out, err = ioutil.ReadFile("/proc/meminfo")
Expand Down Expand Up @@ -77,6 +111,7 @@ func getMachineInfo(sysFs sysfs.SysFs) (*info.MachineInfo, error) {

machineInfo := &info.MachineInfo{
NumCores: numCores,
CpuFrequency: clockSpeed,
MemoryCapacity: memoryCapacity,
DiskMap: diskMap,
}
Expand Down

0 comments on commit 37db214

Please sign in to comment.