From ded58db1ebddcfd7669a39d5158cd3aa3258c6bf Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Mon, 21 Apr 2014 10:26:03 -0700 Subject: [PATCH] Use cgo to get systems clock ticks for metrics Docker-DCO-1.1-Signed-off-by: Michael Crosby (github: crosbymichael) --- cgroups/fs/cpuacct.go | 7 +++++-- system/sysconfig.go | 13 +++++++++++++ system/sysconfig_nocgo.go | 9 +++++++++ system/unsupported.go | 6 ++++++ 4 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 system/sysconfig.go create mode 100644 system/sysconfig_nocgo.go diff --git a/cgroups/fs/cpuacct.go b/cgroups/fs/cpuacct.go index 8c123b7..4ea2b1f 100644 --- a/cgroups/fs/cpuacct.go +++ b/cgroups/fs/cpuacct.go @@ -14,7 +14,10 @@ import ( "github.com/dotcloud/docker/pkg/system" ) -var cpuCount = float64(runtime.NumCPU()) +var ( + cpuCount = float64(runtime.NumCPU()) + clockTicks = float64(system.GetClockTicks()) +) type cpuacctGroup struct { } @@ -58,7 +61,7 @@ func (s *cpuacctGroup) Stats(d *data) (map[string]float64, error) { deltaSystem = lastSystem - startSystem ) if deltaSystem > 0.0 { - percentage = ((deltaProc / deltaSystem) * 100.0) * cpuCount + percentage = ((deltaProc / deltaSystem) * clockTicks) * cpuCount } // NOTE: a percentage over 100% is valid for POSIX because that means the // processes is using multiple cores diff --git a/system/sysconfig.go b/system/sysconfig.go new file mode 100644 index 0000000..dcbe6c9 --- /dev/null +++ b/system/sysconfig.go @@ -0,0 +1,13 @@ +// +build linux,cgo + +package system + +/* +#include +int get_hz(void) { return sysconf(_SC_CLK_TCK); } +*/ +import "C" + +func GetClockTicks() int { + return int(C.get_hz()) +} diff --git a/system/sysconfig_nocgo.go b/system/sysconfig_nocgo.go new file mode 100644 index 0000000..7ca3488 --- /dev/null +++ b/system/sysconfig_nocgo.go @@ -0,0 +1,9 @@ +// +build linux,!cgo + +package system + +func GetClockTicks() int { + // when we cannot call out to C to get the sysconf it is fairly safe to + // just return 100 + return 100 +} diff --git a/system/unsupported.go b/system/unsupported.go index c52a1e5..4ae2a48 100644 --- a/system/unsupported.go +++ b/system/unsupported.go @@ -17,3 +17,9 @@ func UsetCloseOnExec(fd uintptr) error { func Gettid() int { return 0 } + +func GetClockTicks() int { + // when we cannot call out to C to get the sysconf it is fairly safe to + // just return 100 + return 100 +}