Selaa lähdekoodia

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 11 vuotta sitten
vanhempi
commit
3723d6341e

+ 22 - 0
pkg/libcontainer/cgroups/fs/cpuacct.go

@@ -3,6 +3,7 @@ package fs
 import (
 	"bufio"
 	"fmt"
+	"io/ioutil"
 	"os"
 	"path/filepath"
 	"runtime"
@@ -76,6 +77,11 @@ func (s *cpuacctGroup) GetStats(d *data, stats *cgroups.Stats) error {
 	stats.CpuStats.CpuUsage.PercentUsage = percentage
 	// Delta usage is in nanoseconds of CPU time so get the usage (in cores) over the sample time.
 	stats.CpuStats.CpuUsage.CurrentUsage = deltaUsage / uint64(usageSampleDuration.Nanoseconds())
+	percpuUsage, err := s.getPercpuUsage(path)
+	if err != nil {
+		return err
+	}
+	stats.CpuStats.CpuUsage.PercpuUsage = percpuUsage
 	return nil
 }
 
@@ -132,3 +138,19 @@ func (s *cpuacctGroup) getCpuUsage(d *data, path string) (uint64, error) {
 	}
 	return cpuTotal, nil
 }
+
+func (s *cpuacctGroup) getPercpuUsage(path string) ([]uint64, error) {
+	percpuUsage := []uint64{}
+	data, err := ioutil.ReadFile(filepath.Join(path, "cpuacct.usage_percpu"))
+	if err != nil {
+		return percpuUsage, err
+	}
+	for _, value := range strings.Fields(string(data)) {
+		value, err := strconv.ParseUint(value, 10, 64)
+		if err != nil {
+			return percpuUsage, fmt.Errorf("Unable to convert param value to uint64: %s", err)
+		}
+		percpuUsage = append(percpuUsage, value)
+	}
+	return percpuUsage, nil
+}

+ 1 - 1
pkg/libcontainer/cgroups/fs/utils.go

@@ -22,7 +22,7 @@ func getCgroupParamKeyValue(t string) (string, uint64, error) {
 	case 2:
 		value, err := strconv.ParseUint(parts[1], 10, 64)
 		if err != nil {
-			return "", 0, fmt.Errorf("Unable to convert param value to int: %s", err)
+			return "", 0, fmt.Errorf("Unable to convert param value to uint64: %s", err)
 		}
 		return parts[0], value, nil
 	default:

+ 2 - 1
pkg/libcontainer/cgroups/stats.go

@@ -13,7 +13,8 @@ type CpuUsage struct {
 	// percentage of available CPUs currently being used.
 	PercentUsage uint64 `json:"percent_usage,omitempty"`
 	// nanoseconds of cpu time consumed over the last 100 ms.
-	CurrentUsage uint64 `json:"current_usage,omitempty"`
+	CurrentUsage uint64   `json:"current_usage,omitempty"`
+	PercpuUsage  []uint64 `json:"percpu_usage,omitempty"`
 }
 
 type CpuStats struct {