Make host report memory usage in bytes.

This commit is contained in:
Tom Wilkie
2015-12-16 14:13:53 +00:00
parent caff695f96
commit 21a16771c9
4 changed files with 16 additions and 14 deletions

View File

@@ -20,7 +20,7 @@ const (
Load5 = "load5"
Load15 = "load15"
CPUUsage = "cpu_usage_percent"
MemUsage = "mem_usage_percent"
MemUsage = "mem_usage_bytes"
)
// Exposed for testing.
@@ -76,7 +76,7 @@ func (r *Reporter) Report() (report.Report, error) {
metrics := GetLoad(now)
cpuUsage, max := GetCPUUsagePercent()
metrics[CPUUsage] = report.MakeMetric().Add(now, cpuUsage).WithMax(max)
memUsage, max := GetMemoryUsagePercent()
memUsage, max := GetMemoryUsageBytes()
metrics[MemUsage] = report.MakeMetric().Add(now, memUsage).WithMax(max)
rep.Host.AddNode(report.MakeHostNodeID(r.hostID), report.MakeNodeWith(map[string]string{

View File

@@ -38,24 +38,24 @@ func TestReporter(t *testing.T) {
defer mtime.NowReset()
var (
oldGetKernelVersion = host.GetKernelVersion
oldGetLoad = host.GetLoad
oldGetUptime = host.GetUptime
oldGetCPUUsagePercent = host.GetCPUUsagePercent
oldGetMemoryUsagePercent = host.GetMemoryUsagePercent
oldGetKernelVersion = host.GetKernelVersion
oldGetLoad = host.GetLoad
oldGetUptime = host.GetUptime
oldGetCPUUsagePercent = host.GetCPUUsagePercent
oldGetMemoryUsageBytes = host.GetMemoryUsageBytes
)
defer func() {
host.GetKernelVersion = oldGetKernelVersion
host.GetLoad = oldGetLoad
host.GetUptime = oldGetUptime
host.GetCPUUsagePercent = oldGetCPUUsagePercent
host.GetMemoryUsagePercent = oldGetMemoryUsagePercent
host.GetMemoryUsageBytes = oldGetMemoryUsageBytes
}()
host.GetKernelVersion = func() (string, error) { return release + " " + version, nil }
host.GetLoad = func(time.Time) report.Metrics { return load }
host.GetUptime = func() (time.Duration, error) { return time.ParseDuration(uptime) }
host.GetCPUUsagePercent = func() (float64, float64) { return 30.0, 100.0 }
host.GetMemoryUsagePercent = func() (float64, float64) { return 60.0, 100.0 }
host.GetMemoryUsageBytes = func() (float64, float64) { return 60.0, 100.0 }
want := report.MakeReport()
want.Host.AddNode(report.MakeHostNodeID(hostID), report.MakeNodeWith(map[string]string{

View File

@@ -89,7 +89,7 @@ var GetCPUUsagePercent = func() (float64, float64) {
return 0.0, 0.0
}
// GetMemoryUsagePercent returns the percent memory usage and max (ie 100)
var GetMemoryUsagePercent = func() (float64, float64) {
// GetMemoryUsageBytes returns the bytes memory usage and max
var GetMemoryUsageBytes = func() (float64, float64) {
return 0.0, 0.0
}

View File

@@ -13,6 +13,8 @@ import (
"github.com/weaveworks/scope/report"
)
const kb = 1024
// Uname is swappable for mocking in tests.
var Uname = syscall.Uname
@@ -102,13 +104,13 @@ var GetCPUUsagePercent = func() (float64, float64) {
return float64(totald-idled) * 100. / float64(totald), float64(len(stat.CPUStats)) * 100.
}
// GetMemoryUsagePercent returns the percent memory usage and max (ie 100)
var GetMemoryUsagePercent = func() (float64, float64) {
// GetMemoryUsageBytes returns the bytes memory usage and max
var GetMemoryUsageBytes = func() (float64, float64) {
meminfo, err := linuxproc.ReadMemInfo(ProcMemInfo)
if err != nil {
return 0.0, 0.0
}
used := meminfo.MemTotal - meminfo.MemFree - meminfo.Buffers - meminfo.Cached
return float64(used) * 100. / float64(meminfo.MemTotal), 100.
return float64(used * kb), float64(meminfo.MemTotal * kb)
}