Skip to content

Commit 004cf55

Browse files
committed
Use cgo to get systems clock ticks for metrics
Docker-DCO-1.1-Signed-off-by: Michael Crosby <[email protected]> (github: crosbymichael)
1 parent f59be98 commit 004cf55

4 files changed

Lines changed: 33 additions & 2 deletions

File tree

pkg/cgroups/fs/cpuacct.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import (
1414
"github.com/dotcloud/docker/pkg/system"
1515
)
1616

17-
var cpuCount = float64(runtime.NumCPU())
17+
var (
18+
cpuCount = float64(runtime.NumCPU())
19+
clockTicks = float64(system.GetClockTicks())
20+
)
1821

1922
type cpuacctGroup struct {
2023
}
@@ -58,7 +61,7 @@ func (s *cpuacctGroup) Stats(d *data) (map[string]float64, error) {
5861
deltaSystem = lastSystem - startSystem
5962
)
6063
if deltaSystem > 0.0 {
61-
percentage = ((deltaProc / deltaSystem) * 100.0) * cpuCount
64+
percentage = ((deltaProc / deltaSystem) * clockTicks) * cpuCount
6265
}
6366
// NOTE: a percentage over 100% is valid for POSIX because that means the
6467
// processes is using multiple cores

pkg/system/sysconfig.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// +build linux,cgo
2+
3+
package system
4+
5+
/*
6+
#include <unistd.h>
7+
int get_hz(void) { return sysconf(_SC_CLK_TCK); }
8+
*/
9+
import "C"
10+
11+
func GetClockTicks() int {
12+
return int(C.get_hz())
13+
}

pkg/system/sysconfig_nocgo.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// +build linux,!cgo
2+
3+
package system
4+
5+
func GetClockTicks() int {
6+
// when we cannot call out to C to get the sysconf it is fairly safe to
7+
// just return 100
8+
return 100
9+
}

pkg/system/unsupported.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ func UsetCloseOnExec(fd uintptr) error {
1717
func Gettid() int {
1818
return 0
1919
}
20+
21+
func GetClockTicks() int {
22+
// when we cannot call out to C to get the sysconf it is fairly safe to
23+
// just return 100
24+
return 100
25+
}

0 commit comments

Comments
 (0)