github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/daemon/top_windows.go (about)

     1  package daemon
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"time"
     7  
     8  	containertypes "github.com/docker/docker/api/types/container"
     9  	"github.com/docker/go-units"
    10  )
    11  
    12  // ContainerTop handles `docker top` client requests.
    13  // Future considerations:
    14  // -- Windows users are far more familiar with CPU% total.
    15  //    Further, users on Windows rarely see user/kernel CPU stats split.
    16  //    The kernel returns everything in terms of 100ns. To obtain
    17  //    CPU%, we could do something like docker stats does which takes two
    18  //    samples, subtract the difference and do the maths. Unfortunately this
    19  //    would slow the stat call down and require two kernel calls. So instead,
    20  //    we do something similar to linux and display the CPU as combined HH:MM:SS.mmm.
    21  // -- Perhaps we could add an argument to display "raw" stats
    22  // -- "Memory" is an extremely overloaded term in Windows. Hence we do what
    23  //    task manager does and use the private working set as the memory counter.
    24  //    We could return more info for those who really understand how memory
    25  //    management works in Windows if we introduced a "raw" stats (above).
    26  func (daemon *Daemon) ContainerTop(name string, psArgs string) (*containertypes.ContainerTopOKBody, error) {
    27  	// It's not at all an equivalent to linux 'ps' on Windows
    28  	if psArgs != "" {
    29  		return nil, errors.New("Windows does not support arguments to top")
    30  	}
    31  
    32  	container, err := daemon.GetContainer(name)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	s, err := daemon.containerd.Summary(container.ID)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	procList := &containertypes.ContainerTopOKBody{}
    42  	procList.Titles = []string{"Name", "PID", "CPU", "Private Working Set"}
    43  
    44  	for _, j := range s {
    45  		d := time.Duration((j.KernelTime100ns + j.UserTime100ns) * 100) // Combined time in nanoseconds
    46  		procList.Processes = append(procList.Processes, []string{
    47  			j.ImageName,
    48  			fmt.Sprint(j.ProcessId),
    49  			fmt.Sprintf("%02d:%02d:%02d.%03d", int(d.Hours()), int(d.Minutes())%60, int(d.Seconds())%60, int(d.Nanoseconds()/1000000)%1000),
    50  			units.HumanSize(float64(j.MemoryWorkingSetPrivateBytes))})
    51  	}
    52  	return procList, nil
    53  }