stats.go 3.9 KB

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