2015-01-08 02:02:08 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-06-12 15:27:39 +00:00
|
|
|
"io"
|
|
|
|
|
2015-05-30 17:25:51 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-01-19 23:29:42 +00:00
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2015-01-08 02:02:08 +00:00
|
|
|
)
|
|
|
|
|
2015-02-13 16:45:04 +00:00
|
|
|
func (daemon *Daemon) ContainerStats(name string, stream bool, out io.Writer) error {
|
2015-04-13 06:33:53 +00:00
|
|
|
updates, err := daemon.SubscribeToContainerStats(name)
|
2015-01-08 02:02:08 +00:00
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2015-01-08 02:02:08 +00:00
|
|
|
}
|
2015-06-12 15:27:39 +00:00
|
|
|
|
|
|
|
var preCpuStats types.CpuStats
|
|
|
|
getStat := func(v interface{}) *types.Stats {
|
2015-01-19 23:29:42 +00:00
|
|
|
update := v.(*execdriver.ResourceStats)
|
2015-06-12 15:27:39 +00:00
|
|
|
ss := convertStatsToAPITypes(update.Stats)
|
|
|
|
ss.PreCpuStats = preCpuStats
|
2015-01-08 02:02:08 +00:00
|
|
|
ss.MemoryStats.Limit = uint64(update.MemoryLimit)
|
|
|
|
ss.Read = update.Read
|
|
|
|
ss.CpuStats.SystemUsage = update.SystemUsage
|
2015-06-12 15:27:39 +00:00
|
|
|
preCpuStats = ss.CpuStats
|
|
|
|
return ss
|
|
|
|
}
|
|
|
|
|
|
|
|
enc := json.NewEncoder(out)
|
|
|
|
|
|
|
|
if !stream {
|
|
|
|
// prime the cpu stats so they aren't 0 in the final output
|
|
|
|
s := getStat(<-updates)
|
|
|
|
|
|
|
|
// now pull stats again with the cpu stats primed
|
|
|
|
s = getStat(<-updates)
|
|
|
|
err := enc.Encode(s)
|
|
|
|
daemon.UnsubscribeToContainerStats(name, updates)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for v := range updates {
|
|
|
|
s := getStat(v)
|
|
|
|
if err := enc.Encode(s); err != nil {
|
2015-01-08 02:02:08 +00:00
|
|
|
// TODO: handle the specific broken pipe
|
2015-04-13 06:33:53 +00:00
|
|
|
daemon.UnsubscribeToContainerStats(name, updates)
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2015-01-08 02:02:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-25 07:44:12 +00:00
|
|
|
return nil
|
2015-01-08 02:02:08 +00:00
|
|
|
}
|