stats.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "runtime"
  7. "time"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/api/types/backend"
  10. "github.com/docker/docker/api/types/versions"
  11. "github.com/docker/docker/api/types/versions/v1p20"
  12. "github.com/docker/docker/container"
  13. "github.com/docker/docker/errdefs"
  14. "github.com/docker/docker/pkg/ioutils"
  15. )
  16. // ContainerStats writes information about the container to the stream
  17. // given in the config object.
  18. func (daemon *Daemon) ContainerStats(ctx context.Context, prefixOrName string, config *backend.ContainerStatsConfig) error {
  19. // Engine API version (used for backwards compatibility)
  20. apiVersion := config.Version
  21. if isWindows && versions.LessThan(apiVersion, "1.21") {
  22. return errors.New("API versions pre v1.21 do not support stats on Windows")
  23. }
  24. ctr, err := daemon.GetContainer(prefixOrName)
  25. if err != nil {
  26. return err
  27. }
  28. if config.Stream && config.OneShot {
  29. return errdefs.InvalidParameter(errors.New("cannot have stream=true and one-shot=true"))
  30. }
  31. // If the container is either not running or restarting and requires no stream, return an empty stats.
  32. if (!ctr.IsRunning() || ctr.IsRestarting()) && !config.Stream {
  33. return json.NewEncoder(config.OutStream).Encode(&types.StatsJSON{
  34. Name: ctr.Name,
  35. ID: ctr.ID,
  36. })
  37. }
  38. outStream := config.OutStream
  39. if config.Stream {
  40. wf := ioutils.NewWriteFlusher(outStream)
  41. defer wf.Close()
  42. wf.Flush()
  43. outStream = wf
  44. }
  45. var preCPUStats types.CPUStats
  46. var preRead time.Time
  47. getStatJSON := func(v interface{}) *types.StatsJSON {
  48. ss := v.(types.StatsJSON)
  49. ss.Name = ctr.Name
  50. ss.ID = ctr.ID
  51. ss.PreCPUStats = preCPUStats
  52. ss.PreRead = preRead
  53. preCPUStats = ss.CPUStats
  54. preRead = ss.Read
  55. return &ss
  56. }
  57. enc := json.NewEncoder(outStream)
  58. updates := daemon.subscribeToContainerStats(ctr)
  59. defer daemon.unsubscribeToContainerStats(ctr, updates)
  60. noStreamFirstFrame := !config.OneShot
  61. for {
  62. select {
  63. case v, ok := <-updates:
  64. if !ok {
  65. return nil
  66. }
  67. var statsJSON interface{}
  68. statsJSONPost120 := getStatJSON(v)
  69. if versions.LessThan(apiVersion, "1.21") {
  70. var (
  71. rxBytes uint64
  72. rxPackets uint64
  73. rxErrors uint64
  74. rxDropped uint64
  75. txBytes uint64
  76. txPackets uint64
  77. txErrors uint64
  78. txDropped uint64
  79. )
  80. for _, v := range statsJSONPost120.Networks {
  81. rxBytes += v.RxBytes
  82. rxPackets += v.RxPackets
  83. rxErrors += v.RxErrors
  84. rxDropped += v.RxDropped
  85. txBytes += v.TxBytes
  86. txPackets += v.TxPackets
  87. txErrors += v.TxErrors
  88. txDropped += v.TxDropped
  89. }
  90. statsJSON = &v1p20.StatsJSON{
  91. Stats: statsJSONPost120.Stats,
  92. Network: types.NetworkStats{
  93. RxBytes: rxBytes,
  94. RxPackets: rxPackets,
  95. RxErrors: rxErrors,
  96. RxDropped: rxDropped,
  97. TxBytes: txBytes,
  98. TxPackets: txPackets,
  99. TxErrors: txErrors,
  100. TxDropped: txDropped,
  101. },
  102. }
  103. } else {
  104. statsJSON = statsJSONPost120
  105. }
  106. if !config.Stream && noStreamFirstFrame {
  107. // prime the cpu stats so they aren't 0 in the final output
  108. noStreamFirstFrame = false
  109. continue
  110. }
  111. if err := enc.Encode(statsJSON); err != nil {
  112. return err
  113. }
  114. if !config.Stream {
  115. return nil
  116. }
  117. case <-ctx.Done():
  118. return nil
  119. }
  120. }
  121. }
  122. func (daemon *Daemon) subscribeToContainerStats(c *container.Container) chan interface{} {
  123. return daemon.statsCollector.Collect(c)
  124. }
  125. func (daemon *Daemon) unsubscribeToContainerStats(c *container.Container, ch chan interface{}) {
  126. daemon.statsCollector.Unsubscribe(c, ch)
  127. }
  128. // GetContainerStats collects all the stats published by a container
  129. func (daemon *Daemon) GetContainerStats(container *container.Container) (*types.StatsJSON, error) {
  130. stats, err := daemon.stats(container)
  131. if err != nil {
  132. return nil, err
  133. }
  134. // We already have the network stats on Windows directly from HCS.
  135. if !container.Config.NetworkDisabled && runtime.GOOS != "windows" {
  136. if stats.Networks, err = daemon.getNetworkStats(container); err != nil {
  137. return nil, err
  138. }
  139. }
  140. return stats, nil
  141. }