2015-01-07 21:02:08 -05:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-05-30 13:25:51 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-01-19 18:29:42 -05:00
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2015-05-30 13:25:51 -04:00
|
|
|
"io"
|
2015-01-07 21:02:08 -05:00
|
|
|
)
|
|
|
|
|
2015-02-13 11:45:04 -05:00
|
|
|
func (daemon *Daemon) ContainerStats(name string, stream bool, out io.Writer) error {
|
2015-04-13 02:33:53 -04:00
|
|
|
updates, err := daemon.SubscribeToContainerStats(name)
|
2015-01-07 21:02:08 -05:00
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2015-01-07 21:02:08 -05:00
|
|
|
}
|
2015-05-30 13:25:51 -04:00
|
|
|
var pre_cpu_stats types.CpuStats
|
|
|
|
for first_v := range updates {
|
|
|
|
first_update := first_v.(*execdriver.ResourceStats)
|
|
|
|
first_stats := convertToAPITypes(first_update.Stats)
|
|
|
|
pre_cpu_stats = first_stats.CpuStats
|
|
|
|
pre_cpu_stats.SystemUsage = first_update.SystemUsage
|
|
|
|
break
|
|
|
|
}
|
2015-04-13 02:33:53 -04:00
|
|
|
enc := json.NewEncoder(out)
|
2015-01-19 18:29:42 -05:00
|
|
|
for v := range updates {
|
|
|
|
update := v.(*execdriver.ResourceStats)
|
2015-03-05 12:55:14 -05:00
|
|
|
ss := convertToAPITypes(update.Stats)
|
2015-05-30 13:25:51 -04:00
|
|
|
ss.PreCpuStats = pre_cpu_stats
|
2015-01-07 21:02:08 -05:00
|
|
|
ss.MemoryStats.Limit = uint64(update.MemoryLimit)
|
|
|
|
ss.Read = update.Read
|
|
|
|
ss.CpuStats.SystemUsage = update.SystemUsage
|
2015-05-30 13:25:51 -04:00
|
|
|
pre_cpu_stats = ss.CpuStats
|
2015-01-07 21:02:08 -05:00
|
|
|
if err := enc.Encode(ss); err != nil {
|
|
|
|
// TODO: handle the specific broken pipe
|
2015-04-13 02:33:53 -04:00
|
|
|
daemon.UnsubscribeToContainerStats(name, updates)
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2015-01-07 21:02:08 -05:00
|
|
|
}
|
2015-02-13 11:45:04 -05:00
|
|
|
if !stream {
|
|
|
|
break
|
|
|
|
}
|
2015-01-07 21:02:08 -05:00
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2015-01-07 21:02:08 -05:00
|
|
|
}
|