github.com/sams1990/dockerrepo@v17.12.1-ce-rc2+incompatible/daemon/top_windows.go (about)

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