Use cgo to get systems clock ticks for metrics

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
Michael Crosby 2014-04-21 10:26:03 -07:00
parent f59be989dc
commit 004cf556e8
4 changed files with 33 additions and 2 deletions

View file

@ -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

13
pkg/system/sysconfig.go Normal file
View file

@ -0,0 +1,13 @@
// +build linux,cgo
package system
/*
#include <unistd.h>
int get_hz(void) { return sysconf(_SC_CLK_TCK); }
*/
import "C"
func GetClockTicks() int {
return int(C.get_hz())
}

View file

@ -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
}

View file

@ -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
}