Browse code

Use cgo to get systems clock ticks for metrics Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Michael Crosby authored on 2014/04/22 02:26:03
Showing 4 changed files
... ...
@@ -14,7 +14,10 @@ import (
14 14
 	"github.com/dotcloud/docker/pkg/system"
15 15
 )
16 16
 
17
-var cpuCount = float64(runtime.NumCPU())
17
+var (
18
+	cpuCount   = float64(runtime.NumCPU())
19
+	clockTicks = float64(system.GetClockTicks())
20
+)
18 21
 
19 22
 type cpuacctGroup struct {
20 23
 }
... ...
@@ -58,7 +61,7 @@ func (s *cpuacctGroup) Stats(d *data) (map[string]float64, error) {
58 58
 		deltaSystem = lastSystem - startSystem
59 59
 	)
60 60
 	if deltaSystem > 0.0 {
61
-		percentage = ((deltaProc / deltaSystem) * 100.0) * cpuCount
61
+		percentage = ((deltaProc / deltaSystem) * clockTicks) * cpuCount
62 62
 	}
63 63
 	// NOTE: a percentage over 100% is valid for POSIX because that means the
64 64
 	// processes is using multiple cores
65 65
new file mode 100644
... ...
@@ -0,0 +1,13 @@
0
+// +build linux,cgo
1
+
2
+package system
3
+
4
+/*
5
+#include <unistd.h>
6
+int get_hz(void) { return sysconf(_SC_CLK_TCK); }
7
+*/
8
+import "C"
9
+
10
+func GetClockTicks() int {
11
+	return int(C.get_hz())
12
+}
0 13
new file mode 100644
... ...
@@ -0,0 +1,9 @@
0
+// +build linux,!cgo
1
+
2
+package system
3
+
4
+func GetClockTicks() int {
5
+	// when we cannot call out to C to get the sysconf it is fairly safe to
6
+	// just return 100
7
+	return 100
8
+}
... ...
@@ -17,3 +17,9 @@ func UsetCloseOnExec(fd uintptr) error {
17 17
 func Gettid() int {
18 18
 	return 0
19 19
 }
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
+}