Browse code

Adding percpu usage to cgroup stats reported by libcontainer.

Docker-DCO-1.1-Signed-off-by: Vishnu Kannan <vishnuk@google.com> (github: vishh)

Vishnu Kannan authored on 2014/06/02 15:56:15
Showing 3 changed files
... ...
@@ -3,6 +3,7 @@ package fs
3 3
 import (
4 4
 	"bufio"
5 5
 	"fmt"
6
+	"io/ioutil"
6 7
 	"os"
7 8
 	"path/filepath"
8 9
 	"runtime"
... ...
@@ -76,6 +77,11 @@ func (s *cpuacctGroup) GetStats(d *data, stats *cgroups.Stats) error {
76 76
 	stats.CpuStats.CpuUsage.PercentUsage = percentage
77 77
 	// Delta usage is in nanoseconds of CPU time so get the usage (in cores) over the sample time.
78 78
 	stats.CpuStats.CpuUsage.CurrentUsage = deltaUsage / uint64(usageSampleDuration.Nanoseconds())
79
+	percpuUsage, err := s.getPercpuUsage(path)
80
+	if err != nil {
81
+		return err
82
+	}
83
+	stats.CpuStats.CpuUsage.PercpuUsage = percpuUsage
79 84
 	return nil
80 85
 }
81 86
 
... ...
@@ -132,3 +138,19 @@ func (s *cpuacctGroup) getCpuUsage(d *data, path string) (uint64, error) {
132 132
 	}
133 133
 	return cpuTotal, nil
134 134
 }
135
+
136
+func (s *cpuacctGroup) getPercpuUsage(path string) ([]uint64, error) {
137
+	percpuUsage := []uint64{}
138
+	data, err := ioutil.ReadFile(filepath.Join(path, "cpuacct.usage_percpu"))
139
+	if err != nil {
140
+		return percpuUsage, err
141
+	}
142
+	for _, value := range strings.Fields(string(data)) {
143
+		value, err := strconv.ParseUint(value, 10, 64)
144
+		if err != nil {
145
+			return percpuUsage, fmt.Errorf("Unable to convert param value to uint64: %s", err)
146
+		}
147
+		percpuUsage = append(percpuUsage, value)
148
+	}
149
+	return percpuUsage, nil
150
+}
... ...
@@ -22,7 +22,7 @@ func getCgroupParamKeyValue(t string) (string, uint64, error) {
22 22
 	case 2:
23 23
 		value, err := strconv.ParseUint(parts[1], 10, 64)
24 24
 		if err != nil {
25
-			return "", 0, fmt.Errorf("Unable to convert param value to int: %s", err)
25
+			return "", 0, fmt.Errorf("Unable to convert param value to uint64: %s", err)
26 26
 		}
27 27
 		return parts[0], value, nil
28 28
 	default:
... ...
@@ -13,7 +13,8 @@ type CpuUsage struct {
13 13
 	// percentage of available CPUs currently being used.
14 14
 	PercentUsage uint64 `json:"percent_usage,omitempty"`
15 15
 	// nanoseconds of cpu time consumed over the last 100 ms.
16
-	CurrentUsage uint64 `json:"current_usage,omitempty"`
16
+	CurrentUsage uint64   `json:"current_usage,omitempty"`
17
+	PercpuUsage  []uint64 `json:"percpu_usage,omitempty"`
17 18
 }
18 19
 
19 20
 type CpuStats struct {