// ContainerStats writes information about the container to the stream
// given in the config object.
ContainerStats writes information about the container to the stream
given in the config object.
// ContainerStats writes information about the container to the stream // given in the config object .
func (daemon *Daemon) ContainerStats(container *Container, config *ContainerStatsConfig) error {
	updates, err := daemon.subscribeToContainerStats(container)
	if err != nil {
		return err
	}
	if config.Stream {
		config.OutStream.Write(nil)
	}
	var preCPUStats types.CPUStats
	getStat := func(v interface{}) *types.Stats {
		update := v.(*execdriver.ResourceStats)
		if nwStats, err := daemon.getNetworkStats(container); err == nil {
			update.Stats.Interfaces = nwStats
		}
		ss := convertStatsToAPITypes(update.Stats)
		ss.PreCPUStats = preCPUStats
		ss.MemoryStats.Limit = uint64(update.MemoryLimit)
		ss.Read = update.Read
		ss.CPUStats.SystemUsage = update.SystemUsage
		preCPUStats = ss.CPUStats
		return ss
	}
	enc := json.NewEncoder(config.OutStream)
	defer daemon.unsubscribeToContainerStats(container, updates)
	noStreamFirstFrame := true
	for {
		select {
		case v, ok := <-updates:
			if !ok {
				return nil
			}
			s := getStat(v)
			if !config.Stream && noStreamFirstFrame {
				noStreamFirstFrame = false
				continue
			}
			if err := enc.Encode(s); err != nil {
				return err
			}
			if !config.Stream {
				return nil
			}
		case <-config.Stop:
			return nil
		}
	}
}
func ( daemon * Daemon ) ContainerStats ( container * Container , config * ContainerStatsConfig ) error { updates , err := daemon . subscribeToContainerStats ( container ) if err != nil { return err } if config . Stream { config . OutStream . Write ( nil ) } var preCPUStats types . CPUStats getStat := func ( v interface { } ) * types . Stats { update := v . ( * execdriver . ResourceStats ) if nwStats , err := daemon . getNetworkStats ( container ) ; err == nil { update . Stats . Interfaces = nwStats } ss := convertStatsToAPITypes ( update . Stats ) ss . PreCPUStats = preCPUStats ss . MemoryStats . Limit = uint64 ( update . MemoryLimit ) ss . Read = update . Read ss . CPUStats . SystemUsage = update . SystemUsage preCPUStats = ss . CPUStats return ss } enc := json . NewEncoder ( config . OutStream ) defer daemon . unsubscribeToContainerStats ( container , updates ) noStreamFirstFrame := true for { select { case v , ok := <- updates : if ! ok { return nil } s := getStat ( v ) if ! config . Stream && noStreamFirstFrame { noStreamFirstFrame = false continue } if err := enc . Encode ( s ) ; err != nil { return err } if ! config . Stream { return nil } case <- config . Stop : return nil } } }
ContainerStats writes information about the container to the stream
given in the config object.
ContainerStats writes information about the container to the stream given in the config object .
// Retrieve the nw statistics from libnetwork and inject them in the Stats // prime the cpu stats so they aren't 0 in the final output