4f174aa7 |
package daemon
import (
"encoding/json" |
855a056a |
"io"
|
2f46b760 |
"github.com/docker/docker/daemon/execdriver" |
d3379946 |
"github.com/docker/docker/pkg/version" |
907407d0 |
"github.com/docker/engine-api/types"
"github.com/docker/engine-api/types/versions/v1p20" |
4f174aa7 |
)
|
abd72d40 |
// ContainerStatsConfig holds information for configuring the runtime
// behavior of a daemon.ContainerStats() call. |
1cbf5a54 |
type ContainerStatsConfig struct {
Stream bool
OutStream io.Writer
Stop <-chan bool |
d3379946 |
Version version.Version |
1cbf5a54 |
}
|
abd72d40 |
// ContainerStats writes information about the container to the stream
// given in the config object. |
b08f071e |
func (daemon *Daemon) ContainerStats(prefixOrName string, config *ContainerStatsConfig) error { |
d7d512bb |
container, err := daemon.GetContainer(prefixOrName) |
2d5d606f |
if err != nil {
return err
}
// If the container is not running and requires no stream, return an empty stats.
if !container.IsRunning() && !config.Stream {
return json.NewEncoder(config.OutStream).Encode(&types.Stats{})
}
|
1cbf5a54 |
if config.Stream { |
3b443abe |
// Write an empty chunk of data.
// This is to ensure that the HTTP status code is sent immediately,
// even if the container has not yet produced any data. |
1cbf5a54 |
config.OutStream.Write(nil)
}
|
abd72d40 |
var preCPUStats types.CPUStats |
d3379946 |
getStatJSON := func(v interface{}) *types.StatsJSON { |
2f46b760 |
update := v.(*execdriver.ResourceStats) |
855a056a |
ss := convertStatsToAPITypes(update.Stats) |
abd72d40 |
ss.PreCPUStats = preCPUStats |
4f174aa7 |
ss.MemoryStats.Limit = uint64(update.MemoryLimit)
ss.Read = update.Read |
3d6617ff |
ss.CPUStats.SystemUsage = update.SystemUsage |
abd72d40 |
preCPUStats = ss.CPUStats |
855a056a |
return ss
}
|
1cbf5a54 |
enc := json.NewEncoder(config.OutStream) |
855a056a |
|
99065544 |
updates := daemon.subscribeToContainerStats(container) |
dc8a3903 |
defer daemon.unsubscribeToContainerStats(container, updates) |
855a056a |
|
1cbf5a54 |
noStreamFirstFrame := true
for {
select {
case v, ok := <-updates:
if !ok {
return nil
}
|
d2c04f84 |
var statsJSON interface{}
statsJSONPost120 := getStatJSON(v) |
d3379946 |
if config.Version.LessThan("1.21") {
var (
rxBytes uint64
rxPackets uint64
rxErrors uint64
rxDropped uint64
txBytes uint64
txPackets uint64
txErrors uint64
txDropped uint64
) |
d2c04f84 |
for _, v := range statsJSONPost120.Networks { |
d3379946 |
rxBytes += v.RxBytes
rxPackets += v.RxPackets
rxErrors += v.RxErrors
rxDropped += v.RxDropped
txBytes += v.TxBytes
txPackets += v.TxPackets
txErrors += v.TxErrors
txDropped += v.TxDropped
} |
d2c04f84 |
statsJSON = &v1p20.StatsJSON{
Stats: statsJSONPost120.Stats, |
d3379946 |
Network: types.NetworkStats{
RxBytes: rxBytes,
RxPackets: rxPackets,
RxErrors: rxErrors,
RxDropped: rxDropped,
TxBytes: txBytes,
TxPackets: txPackets,
TxErrors: txErrors,
TxDropped: txDropped,
},
} |
d2c04f84 |
} else {
statsJSON = statsJSONPost120 |
d3379946 |
}
|
1cbf5a54 |
if !config.Stream && noStreamFirstFrame {
// prime the cpu stats so they aren't 0 in the final output
noStreamFirstFrame = false
continue
}
|
d3379946 |
if err := enc.Encode(statsJSON); err != nil { |
1cbf5a54 |
return err
} |
855a056a |
|
1cbf5a54 |
if !config.Stream {
return nil
}
case <-config.Stop:
return nil |
4f174aa7 |
}
}
} |