diff --git a/api/client/stats.go b/api/client/stats.go index ba56982b9e..592da553c1 100644 --- a/api/client/stats.go +++ b/api/client/stats.go @@ -46,7 +46,6 @@ func (s *containerStats) Collect(cli *DockerCli, streamStats bool) { var ( previousCPU uint64 previousSystem uint64 - start = true dec = json.NewDecoder(stream) u = make(chan error, 1) ) @@ -61,10 +60,9 @@ func (s *containerStats) Collect(cli *DockerCli, streamStats bool) { memPercent = float64(v.MemoryStats.Usage) / float64(v.MemoryStats.Limit) * 100.0 cpuPercent = 0.0 ) - if !start { - cpuPercent = calculateCPUPercent(previousCPU, previousSystem, v) - } - start = false + previousCPU = v.PreCpuStats.CpuUsage.TotalUsage + previousSystem = v.PreCpuStats.SystemUsage + cpuPercent = calculateCPUPercent(previousCPU, previousSystem, v) s.mu.Lock() s.CPUPercentage = cpuPercent s.Memory = float64(v.MemoryStats.Usage) @@ -73,8 +71,6 @@ func (s *containerStats) Collect(cli *DockerCli, streamStats bool) { s.NetworkRx = float64(v.Network.RxBytes) s.NetworkTx = float64(v.Network.TxBytes) s.mu.Unlock() - previousCPU = v.CpuStats.CpuUsage.TotalUsage - previousSystem = v.CpuStats.SystemUsage u <- nil if !streamStats { return @@ -151,7 +147,7 @@ func (cli *DockerCli) CmdStats(args ...string) error { } // do a quick pause so that any failed connections for containers that do not exist are able to be // evicted before we display the initial or default values. - time.Sleep(500 * time.Millisecond) + time.Sleep(1500 * time.Millisecond) var errs []string for _, c := range cStats { c.mu.Lock() diff --git a/api/types/stats.go b/api/types/stats.go index c6f9911385..507830ceeb 100644 --- a/api/types/stats.go +++ b/api/types/stats.go @@ -84,6 +84,7 @@ type Network struct { type Stats struct { Read time.Time `json:"read"` Network Network `json:"network,omitempty"` + PreCpuStats CpuStats `json:"precpu_stats,omitempty"` CpuStats CpuStats `json:"cpu_stats,omitempty"` MemoryStats MemoryStats `json:"memory_stats,omitempty"` BlkioStats BlkioStats `json:"blkio_stats,omitempty"` diff --git a/daemon/stats.go b/daemon/stats.go index a382437535..6798ad887e 100644 --- a/daemon/stats.go +++ b/daemon/stats.go @@ -2,9 +2,9 @@ package daemon import ( "encoding/json" - "io" - + "github.com/docker/docker/api/types" "github.com/docker/docker/daemon/execdriver" + "io" ) func (daemon *Daemon) ContainerStats(name string, stream bool, out io.Writer) error { @@ -12,13 +12,23 @@ func (daemon *Daemon) ContainerStats(name string, stream bool, out io.Writer) er if err != nil { return err } + var pre_cpu_stats types.CpuStats + for first_v := range updates { + first_update := first_v.(*execdriver.ResourceStats) + first_stats := convertToAPITypes(first_update.Stats) + pre_cpu_stats = first_stats.CpuStats + pre_cpu_stats.SystemUsage = first_update.SystemUsage + break + } enc := json.NewEncoder(out) for v := range updates { update := v.(*execdriver.ResourceStats) ss := convertToAPITypes(update.Stats) + ss.PreCpuStats = pre_cpu_stats ss.MemoryStats.Limit = uint64(update.MemoryLimit) ss.Read = update.Read ss.CpuStats.SystemUsage = update.SystemUsage + pre_cpu_stats = ss.CpuStats if err := enc.Encode(ss); err != nil { // TODO: handle the specific broken pipe daemon.UnsubscribeToContainerStats(name, updates) diff --git a/integration-cli/docker_api_stats.go b/integration-cli/docker_api_stats.go new file mode 100644 index 0000000000..41d2f86083 --- /dev/null +++ b/integration-cli/docker_api_stats.go @@ -0,0 +1,48 @@ +package main + +import ( + "encoding/json" + "fmt" + "github.com/docker/docker/api/types" + "github.com/go-check/check" + "strings" + "time" +) + +func (s *DockerSuite) TestCliStatsNoStreamGetCpu(c *check.C) { + out, _ := dockerCmd(c, "run", "-d", "--cpu-quota=2000", "busybox", "/bin/sh", "-c", "while true;do echo 'Hello';done") + + id := strings.TrimSpace(out) + if err := waitRun(id); err != nil { + c.Fatal(err) + } + ch := make(chan error) + var v *types.Stats + go func() { + _, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=1", id), nil, "") + if err != nil { + ch <- err + } + dec := json.NewDecoder(body) + if err := dec.Decode(&v); err != nil { + ch <- err + } + ch <- nil + }() + select { + case e := <-ch: + if e == nil { + var cpuPercent = 0.0 + cpuDelta := float64(v.CpuStats.CpuUsage.TotalUsage - v.PreCpuStats.CpuUsage.TotalUsage) + systemDelta := float64(v.CpuStats.SystemUsage - v.PreCpuStats.SystemUsage) + cpuPercent = (cpuDelta / systemDelta) * float64(len(v.CpuStats.CpuUsage.PercpuUsage)) * 100.0 + if cpuPercent < 1.8 || cpuPercent > 2.2 { + c.Fatal("docker stats with no-stream get cpu usage failed") + } + + } + case <-time.After(4 * time.Second): + c.Fatal("docker stats with no-stream timeout") + } + +} diff --git a/integration-cli/docker_cli_stats_test.go b/integration-cli/docker_cli_stats_test.go index 7664de5977..9c5c07123b 100644 --- a/integration-cli/docker_cli_stats_test.go +++ b/integration-cli/docker_cli_stats_test.go @@ -29,7 +29,7 @@ func (s *DockerSuite) TestCliStatsNoStream(c *check.C) { if err != nil { c.Fatalf("Error running stats: %v", err) } - case <-time.After(2 * time.Second): + case <-time.After(3 * time.Second): statsCmd.Process.Kill() c.Fatalf("stats did not return immediately when not streaming") }