From 712ef17493124e2c7408db38c1930ed589a8ff88 Mon Sep 17 00:00:00 2001 From: Zhang Wei Date: Sun, 26 Jul 2015 16:14:00 +0800 Subject: [PATCH] Docker stats: display Block IO stats Signed-off-by: Zhang Wei Upstream-commit: 6a4ac63aaac829e94c7d6cbb31a8d36b93757620 Component: engine --- components/engine/api/client/stats.go | 24 ++++++++++++++++--- .../engine/api/client/stats_unit_test.go | 4 +++- components/engine/docs/articles/runmetrics.md | 6 ++--- .../docs/reference/commandline/stats.md | 6 ++--- components/engine/man/docker-stats.1.md | 6 ++--- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/components/engine/api/client/stats.go b/components/engine/api/client/stats.go index 334dc5417e..04e1937bf4 100644 --- a/components/engine/api/client/stats.go +++ b/components/engine/api/client/stats.go @@ -25,6 +25,8 @@ type containerStats struct { MemoryPercentage float64 NetworkRx float64 NetworkTx float64 + BlockRead float64 + BlockWrite float64 mu sync.RWMutex err error } @@ -66,6 +68,7 @@ func (s *containerStats) Collect(cli *DockerCli, streamStats bool) { previousCPU = v.PreCpuStats.CpuUsage.TotalUsage previousSystem = v.PreCpuStats.SystemUsage cpuPercent = calculateCPUPercent(previousCPU, previousSystem, v) + blkRead, blkWrite := calculateBlockIO(v.BlkioStats) s.mu.Lock() s.CPUPercentage = cpuPercent s.Memory = float64(v.MemoryStats.Usage) @@ -73,6 +76,8 @@ func (s *containerStats) Collect(cli *DockerCli, streamStats bool) { s.MemoryPercentage = memPercent s.NetworkRx = float64(v.Network.RxBytes) s.NetworkTx = float64(v.Network.TxBytes) + s.BlockRead = float64(blkRead) + s.BlockWrite = float64(blkWrite) s.mu.Unlock() u <- nil if !streamStats { @@ -110,12 +115,13 @@ func (s *containerStats) Display(w io.Writer) error { if s.err != nil { return s.err } - fmt.Fprintf(w, "%s\t%.2f%%\t%s / %s\t%.2f%%\t%s / %s\n", + fmt.Fprintf(w, "%s\t%.2f%%\t%s / %s\t%.2f%%\t%s / %s\t%s / %s\n", s.Name, s.CPUPercentage, units.HumanSize(s.Memory), units.HumanSize(s.MemoryLimit), s.MemoryPercentage, - units.HumanSize(s.NetworkRx), units.HumanSize(s.NetworkTx)) + units.HumanSize(s.NetworkRx), units.HumanSize(s.NetworkTx), + units.HumanSize(s.BlockRead), units.HumanSize(s.BlockWrite)) return nil } @@ -142,7 +148,7 @@ func (cli *DockerCli) CmdStats(args ...string) error { fmt.Fprint(cli.out, "\033[2J") fmt.Fprint(cli.out, "\033[H") } - io.WriteString(w, "CONTAINER\tCPU %\tMEM USAGE / LIMIT\tMEM %\tNET I/O\n") + io.WriteString(w, "CONTAINER\tCPU %\tMEM USAGE / LIMIT\tMEM %\tNET I/O\tBLOCK I/O\n") } for _, n := range names { s := &containerStats{Name: n} @@ -200,3 +206,15 @@ func calculateCPUPercent(previousCPU, previousSystem uint64, v *types.Stats) flo } return cpuPercent } + +func calculateBlockIO(blkio types.BlkioStats) (blkRead uint64, blkWrite uint64) { + for _, bioEntry := range blkio.IoServiceBytesRecursive { + switch strings.ToLower(bioEntry.Op) { + case "read": + blkRead = bioEntry.Value + case "write": + blkWrite = bioEntry.Value + } + } + return +} diff --git a/components/engine/api/client/stats_unit_test.go b/components/engine/api/client/stats_unit_test.go index b2db75a199..41f3743e05 100644 --- a/components/engine/api/client/stats_unit_test.go +++ b/components/engine/api/client/stats_unit_test.go @@ -15,6 +15,8 @@ func TestDisplay(t *testing.T) { MemoryPercentage: 100.0 / 2048.0 * 100.0, NetworkRx: 100 * 1024 * 1024, NetworkTx: 800 * 1024 * 1024, + BlockRead: 100 * 1024 * 1024, + BlockWrite: 800 * 1024 * 1024, mu: sync.RWMutex{}, } var b bytes.Buffer @@ -22,7 +24,7 @@ func TestDisplay(t *testing.T) { t.Fatalf("c.Display() gave error: %s", err) } got := b.String() - want := "app\t30.00%\t104.9 MB / 2.147 GB\t4.88%\t104.9 MB / 838.9 MB\n" + want := "app\t30.00%\t104.9 MB / 2.147 GB\t4.88%\t104.9 MB / 838.9 MB\t104.9 MB / 838.9 MB\n" if got != want { t.Fatalf("c.Display() = %q, want %q", got, want) } diff --git a/components/engine/docs/articles/runmetrics.md b/components/engine/docs/articles/runmetrics.md index d02f1d6806..27bd4027e3 100644 --- a/components/engine/docs/articles/runmetrics.md +++ b/components/engine/docs/articles/runmetrics.md @@ -21,9 +21,9 @@ and network IO metrics. The following is a sample output from the `docker stats` command $ docker stats redis1 redis2 - CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O - redis1 0.07% 796 KB / 64 MB 1.21% 788 B / 648 B - redis2 0.07% 2.746 MB / 64 MB 4.29% 1.266 KB / 648 B + CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O + redis1 0.07% 796 KB / 64 MB 1.21% 788 B / 648 B 3.568 MB / 512 KB + redis2 0.07% 2.746 MB / 64 MB 4.29% 1.266 KB / 648 B 12.4 MB / 0 B The [docker stats](/reference/commandline/stats/) reference page has diff --git a/components/engine/docs/reference/commandline/stats.md b/components/engine/docs/reference/commandline/stats.md index 6ee026a75f..d3e667bac6 100644 --- a/components/engine/docs/reference/commandline/stats.md +++ b/components/engine/docs/reference/commandline/stats.md @@ -21,9 +21,9 @@ weight=1 Running `docker stats` on multiple containers $ docker stats redis1 redis2 - CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O - redis1 0.07% 796 KB / 64 MB 1.21% 788 B / 648 B - redis2 0.07% 2.746 MB / 64 MB 4.29% 1.266 KB / 648 B + CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O + redis1 0.07% 796 KB / 64 MB 1.21% 788 B / 648 B 3.568 MB / 512 KB + redis2 0.07% 2.746 MB / 64 MB 4.29% 1.266 KB / 648 B 12.4 MB / 0 B The `docker stats` command will only return a live stream of data for running diff --git a/components/engine/man/docker-stats.1.md b/components/engine/man/docker-stats.1.md index 08502f44a5..b8dd119138 100644 --- a/components/engine/man/docker-stats.1.md +++ b/components/engine/man/docker-stats.1.md @@ -25,6 +25,6 @@ Display a live stream of one or more containers' resource usage statistics Run **docker stats** with multiple containers. $ docker stats redis1 redis2 - CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O - redis1 0.07% 796 KB / 64 MB 1.21% 788 B / 648 B - redis2 0.07% 2.746 MB / 64 MB 4.29% 1.266 KB / 648 B + CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O + redis1 0.07% 796 KB / 64 MB 1.21% 788 B / 648 B 3.568 MB / 512 KB + redis2 0.07% 2.746 MB / 64 MB 4.29% 1.266 KB / 648 B 12.4 MB / 0 B