1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #16142 from cpuguy83/stats_pass_container

Don't lookup container 4 times for stats
This commit is contained in:
Jess Frazelle 2015-09-08 11:19:33 -07:00
commit 451f4d0d3b
3 changed files with 8 additions and 21 deletions

View file

@ -103,7 +103,7 @@ func (s *Server) getContainersStats(version version.Version, w http.ResponseWrit
Stop: closeNotifier, Stop: closeNotifier,
} }
return s.daemon.ContainerStats(vars["name"], config) return s.daemon.ContainerStats(container, config)
} }
func (s *Server) getContainersLogs(version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { func (s *Server) getContainersLogs(version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {

View file

@ -886,20 +886,12 @@ func (daemon *Daemon) stats(c *Container) (*execdriver.ResourceStats, error) {
return daemon.execDriver.Stats(c.ID) return daemon.execDriver.Stats(c.ID)
} }
func (daemon *Daemon) subscribeToContainerStats(name string) (chan interface{}, error) { func (daemon *Daemon) subscribeToContainerStats(c *Container) (chan interface{}, error) {
c, err := daemon.Get(name)
if err != nil {
return nil, err
}
ch := daemon.statsCollector.collect(c) ch := daemon.statsCollector.collect(c)
return ch, nil return ch, nil
} }
func (daemon *Daemon) unsubscribeToContainerStats(name string, ch chan interface{}) error { func (daemon *Daemon) unsubscribeToContainerStats(c *Container, ch chan interface{}) error {
c, err := daemon.Get(name)
if err != nil {
return err
}
daemon.statsCollector.unsubscribe(c, ch) daemon.statsCollector.unsubscribe(c, ch)
return nil return nil
} }

View file

@ -20,8 +20,8 @@ type ContainerStatsConfig struct {
// ContainerStats writes information about the container to the stream // ContainerStats writes information about the container to the stream
// given in the config object. // given in the config object.
func (daemon *Daemon) ContainerStats(name string, config *ContainerStatsConfig) error { func (daemon *Daemon) ContainerStats(container *Container, config *ContainerStatsConfig) error {
updates, err := daemon.subscribeToContainerStats(name) updates, err := daemon.subscribeToContainerStats(container)
if err != nil { if err != nil {
return err return err
} }
@ -34,7 +34,7 @@ func (daemon *Daemon) ContainerStats(name string, config *ContainerStatsConfig)
getStat := func(v interface{}) *types.Stats { getStat := func(v interface{}) *types.Stats {
update := v.(*execdriver.ResourceStats) update := v.(*execdriver.ResourceStats)
// Retrieve the nw statistics from libnetwork and inject them in the Stats // Retrieve the nw statistics from libnetwork and inject them in the Stats
if nwStats, err := daemon.getNetworkStats(name); err == nil { if nwStats, err := daemon.getNetworkStats(container); err == nil {
update.Stats.Interfaces = nwStats update.Stats.Interfaces = nwStats
} }
ss := convertStatsToAPITypes(update.Stats) ss := convertStatsToAPITypes(update.Stats)
@ -48,7 +48,7 @@ func (daemon *Daemon) ContainerStats(name string, config *ContainerStatsConfig)
enc := json.NewEncoder(config.OutStream) enc := json.NewEncoder(config.OutStream)
defer daemon.unsubscribeToContainerStats(name, updates) defer daemon.unsubscribeToContainerStats(container, updates)
noStreamFirstFrame := true noStreamFirstFrame := true
for { for {
@ -78,14 +78,9 @@ func (daemon *Daemon) ContainerStats(name string, config *ContainerStatsConfig)
} }
} }
func (daemon *Daemon) getNetworkStats(name string) ([]*libcontainer.NetworkInterface, error) { func (daemon *Daemon) getNetworkStats(c *Container) ([]*libcontainer.NetworkInterface, error) {
var list []*libcontainer.NetworkInterface var list []*libcontainer.NetworkInterface
c, err := daemon.Get(name)
if err != nil {
return list, err
}
sb, err := daemon.netController.SandboxByID(c.NetworkSettings.SandboxID) sb, err := daemon.netController.SandboxByID(c.NetworkSettings.SandboxID)
if err != nil { if err != nil {
return list, err return list, err