From 97dbfe9e1facd06276586393749098b6ec4eb409 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Tue, 20 Sep 2016 11:20:17 -0700 Subject: [PATCH] Move cputicks into osutils package Signed-off-by: Michael Crosby --- api/grpc/server/server.go | 56 ++--------------------------- api/grpc/server/server_linux.go | 3 -- osutils/proc.go | 64 +++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 57 deletions(-) create mode 100644 osutils/proc.go diff --git a/api/grpc/server/server.go b/api/grpc/server/server.go index bb7ad02..bcc7e13 100644 --- a/api/grpc/server/server.go +++ b/api/grpc/server/server.go @@ -1,12 +1,8 @@ package server import ( - "bufio" "errors" "fmt" - "os" - "strconv" - "strings" "syscall" "time" @@ -15,6 +11,7 @@ import ( "github.com/docker/containerd" "github.com/docker/containerd/api/grpc/types" + "github.com/docker/containerd/osutils" "github.com/docker/containerd/runtime" "github.com/docker/containerd/supervisor" "github.com/golang/protobuf/ptypes" @@ -351,7 +348,7 @@ func convertToPb(st *runtime.Stat) *types.StatsResponse { Timestamp: tsp, CgroupStats: &types.CgroupStats{}, } - systemUsage, _ := getSystemCPUUsage() + systemUsage, _ := osutils.GetSystemCPUUsage() pbSt.CgroupStats.CpuStats = &types.CpuStats{ CpuUsage: &types.CpuUsage{ TotalUsage: st.CPU.Usage.Total, @@ -426,55 +423,6 @@ func convertBlkioEntryToPb(b []runtime.BlkioEntry) []*types.BlkioStatsEntry { return pbEs } -const nanoSecondsPerSecond = 1e9 - -// getSystemCPUUsage returns the host system's cpu usage in -// nanoseconds. An error is returned if the format of the underlying -// file does not match. -// -// Uses /proc/stat defined by POSIX. Looks for the cpu -// statistics line and then sums up the first seven fields -// provided. See `man 5 proc` for details on specific field -// information. -func getSystemCPUUsage() (uint64, error) { - var line string - f, err := os.Open("/proc/stat") - if err != nil { - return 0, err - } - bufReader := bufio.NewReaderSize(nil, 128) - defer func() { - bufReader.Reset(nil) - f.Close() - }() - bufReader.Reset(f) - err = nil - for err == nil { - line, err = bufReader.ReadString('\n') - if err != nil { - break - } - parts := strings.Fields(line) - switch parts[0] { - case "cpu": - if len(parts) < 8 { - return 0, fmt.Errorf("bad format of cpu stats") - } - var totalClockTicks uint64 - for _, i := range parts[1:8] { - v, err := strconv.ParseUint(i, 10, 64) - if err != nil { - return 0, fmt.Errorf("error parsing cpu stats") - } - totalClockTicks += v - } - return (totalClockTicks * nanoSecondsPerSecond) / - clockTicksPerSecond, nil - } - } - return 0, fmt.Errorf("bad stats format") -} - func (s *apiServer) Stats(ctx context.Context, r *types.StatsRequest) (*types.StatsResponse, error) { e := &supervisor.StatsTask{} e.ID = r.Id diff --git a/api/grpc/server/server_linux.go b/api/grpc/server/server_linux.go index 1051f1f..4e9b43b 100644 --- a/api/grpc/server/server_linux.go +++ b/api/grpc/server/server_linux.go @@ -6,13 +6,10 @@ import ( "github.com/docker/containerd/api/grpc/types" "github.com/docker/containerd/specs" "github.com/docker/containerd/supervisor" - "github.com/opencontainers/runc/libcontainer/system" ocs "github.com/opencontainers/runtime-spec/specs-go" "golang.org/x/net/context" ) -var clockTicksPerSecond = uint64(system.GetClockTicks()) - func (s *apiServer) AddProcess(ctx context.Context, r *types.AddProcessRequest) (*types.AddProcessResponse, error) { process := &specs.ProcessSpec{ Terminal: r.Terminal, diff --git a/osutils/proc.go b/osutils/proc.go new file mode 100644 index 0000000..1bb728e --- /dev/null +++ b/osutils/proc.go @@ -0,0 +1,64 @@ +// +build linux + +package osutils + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" + + "github.com/opencontainers/runc/libcontainer/system" +) + +const nanoSecondsPerSecond = 1e9 + +var clockTicksPerSecond = uint64(system.GetClockTicks()) + +// GetSystemCPUUsage returns the host system's cpu usage in +// nanoseconds. An error is returned if the format of the underlying +// file does not match. +// +// Uses /proc/stat defined by POSIX. Looks for the cpu +// statistics line and then sums up the first seven fields +// provided. See `man 5 proc` for details on specific field +// information. +func GetSystemCPUUsage() (uint64, error) { + var line string + f, err := os.Open("/proc/stat") + if err != nil { + return 0, err + } + bufReader := bufio.NewReaderSize(nil, 128) + defer func() { + bufReader.Reset(nil) + f.Close() + }() + bufReader.Reset(f) + err = nil + for err == nil { + line, err = bufReader.ReadString('\n') + if err != nil { + break + } + parts := strings.Fields(line) + switch parts[0] { + case "cpu": + if len(parts) < 8 { + return 0, fmt.Errorf("bad format of cpu stats") + } + var totalClockTicks uint64 + for _, i := range parts[1:8] { + v, err := strconv.ParseUint(i, 10, 64) + if err != nil { + return 0, fmt.Errorf("error parsing cpu stats") + } + totalClockTicks += v + } + return (totalClockTicks * nanoSecondsPerSecond) / + clockTicksPerSecond, nil + } + } + return 0, fmt.Errorf("bad stats format") +}