top_windows.go 2.2 KB

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