瀏覽代碼

Exit cli when all containers when no more containers to monitor

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
Michael Crosby 10 年之前
父節點
當前提交
4b173199fd
共有 3 個文件被更改,包括 16 次插入10 次删除
  1. 14 4
      api/client/commands.go
  2. 1 1
      daemon/stats_collector.go
  3. 1 5
      docs/man/docker-stats.1.md

+ 14 - 4
api/client/commands.go

@@ -2672,11 +2672,11 @@ func (s *containerStats) Collect(stream io.ReadCloser) {
 	}
 }
 
-func (s *containerStats) Display(w io.Writer) {
+func (s *containerStats) Display(w io.Writer) error {
 	s.mu.RLock()
 	defer s.mu.RUnlock()
 	if s.err != nil {
-		return
+		return s.err
 	}
 	fmt.Fprintf(w, "%s\t%.2f%%\t%s/%s\t%.2f%%\t%s/%s\n",
 		s.Name,
@@ -2684,6 +2684,7 @@ func (s *containerStats) Display(w io.Writer) {
 		units.BytesSize(s.Memory), units.BytesSize(s.MemoryLimit),
 		s.MemoryPercentage,
 		units.BytesSize(s.NetworkRx), units.BytesSize(s.NetworkTx))
+	return nil
 }
 
 func (cli *DockerCli) CmdStats(args ...string) error {
@@ -2708,8 +2709,17 @@ func (cli *DockerCli) CmdStats(args ...string) error {
 		fmt.Fprint(cli.out, "\033[2J")
 		fmt.Fprint(cli.out, "\033[H")
 		fmt.Fprintln(w, "CONTAINER\tCPU %\tMEM USAGE/LIMIT\tMEM %\tNET I/O")
-		for _, s := range cStats {
-			s.Display(w)
+		toRemove := []int{}
+		for i, s := range cStats {
+			if err := s.Display(w); err != nil {
+				toRemove = append(toRemove, i)
+			}
+		}
+		for _, i := range toRemove {
+			cStats = append(cStats[:i], cStats[i+1:]...)
+		}
+		if len(cStats) == 0 {
+			return nil
 		}
 		w.Flush()
 	}

+ 1 - 1
daemon/stats_collector.go

@@ -98,7 +98,7 @@ func (s *statsCollector) run() {
 
 const nanoSeconds = 1e9
 
-// getSystemdCpuUSage returns the host system's cpu usage in nanoseconds
+// getSystemCpuUSage returns the host system's cpu usage in nanoseconds
 // for the system to match the cgroup readings are returned in the same format.
 func (s *statsCollector) getSystemCpuUsage() (uint64, error) {
 	f, err := os.Open("/proc/stat")

+ 1 - 5
docs/man/docker-stats.1.md

@@ -5,7 +5,7 @@
 docker-stats - Display live container stats based on resource usage.
 
 # SYNOPSIS
-**docker top**
+**docker stats**
 [**--help**]
 [CONTAINERS]
 
@@ -26,7 +26,3 @@ Run **docker stats** with multiple containers.
     redis1              0.07%               796 KiB/64 MiB      1.21%               788 B/648 B
     redis2              0.07%               2.746 MiB/64 MiB    4.29%               1.266 KiB/648 B
 
-# HISTORY
-April 2014, Originally compiled by William Henry (whenry at redhat dot com)
-based on docker.com source material and internal work.
-June 2014, updated by Sven Dowideit <SvenDowideit@home.org.au>