stats.go 4.0 KB

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