2018-02-05 21:05:59 +00:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2015-01-08 02:02:08 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2015-01-08 02:02:08 +00:00
|
|
|
"encoding/json"
|
2016-02-03 01:59:11 +00:00
|
|
|
"errors"
|
|
|
|
"runtime"
|
2016-09-07 23:08:51 +00:00
|
|
|
"time"
|
2015-06-12 15:27:39 +00:00
|
|
|
|
2023-09-13 15:41:45 +00:00
|
|
|
"github.com/containerd/log"
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-01-27 22:09:42 +00:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2016-05-24 15:49:26 +00:00
|
|
|
"github.com/docker/docker/container"
|
2020-02-07 23:55:06 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2015-12-19 14:43:10 +00:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2015-01-08 02:02:08 +00:00
|
|
|
)
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// ContainerStats writes information about the container to the stream
|
|
|
|
// given in the config object.
|
2016-03-25 18:33:54 +00:00
|
|
|
func (daemon *Daemon) ContainerStats(ctx context.Context, prefixOrName string, config *backend.ContainerStatsConfig) error {
|
2019-08-09 12:10:07 +00:00
|
|
|
ctr, err := daemon.GetContainer(prefixOrName)
|
2015-09-16 21:16:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-07 23:55:06 +00:00
|
|
|
if config.Stream && config.OneShot {
|
|
|
|
return errdefs.InvalidParameter(errors.New("cannot have stream=true and one-shot=true"))
|
|
|
|
}
|
|
|
|
|
2016-11-03 10:07:18 +00:00
|
|
|
// If the container is either not running or restarting and requires no stream, return an empty stats.
|
2019-08-09 12:10:07 +00:00
|
|
|
if (!ctr.IsRunning() || ctr.IsRestarting()) && !config.Stream {
|
2016-12-26 09:43:18 +00:00
|
|
|
return json.NewEncoder(config.OutStream).Encode(&types.StatsJSON{
|
2019-08-09 12:10:07 +00:00
|
|
|
Name: ctr.Name,
|
|
|
|
ID: ctr.ID,
|
|
|
|
})
|
2015-09-16 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2023-09-26 04:44:04 +00:00
|
|
|
// Get container stats directly if OneShot is set
|
|
|
|
if config.OneShot {
|
|
|
|
stats, err := daemon.GetContainerStats(ctr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return json.NewEncoder(config.OutStream).Encode(stats)
|
|
|
|
}
|
|
|
|
|
2015-12-19 14:43:10 +00:00
|
|
|
outStream := config.OutStream
|
2015-06-16 14:33:49 +00:00
|
|
|
if config.Stream {
|
2015-12-19 14:43:10 +00:00
|
|
|
wf := ioutils.NewWriteFlusher(outStream)
|
|
|
|
defer wf.Close()
|
|
|
|
wf.Flush()
|
|
|
|
outStream = wf
|
2015-06-16 14:33:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
var preCPUStats types.CPUStats
|
2016-09-07 23:08:51 +00:00
|
|
|
var preRead time.Time
|
2015-08-24 09:17:15 +00:00
|
|
|
getStatJSON := func(v interface{}) *types.StatsJSON {
|
2016-04-18 18:12:07 +00:00
|
|
|
ss := v.(types.StatsJSON)
|
2019-08-09 12:10:07 +00:00
|
|
|
ss.Name = ctr.Name
|
|
|
|
ss.ID = ctr.ID
|
2015-07-30 21:01:53 +00:00
|
|
|
ss.PreCPUStats = preCPUStats
|
2016-09-07 23:08:51 +00:00
|
|
|
ss.PreRead = preRead
|
2015-07-30 21:01:53 +00:00
|
|
|
preCPUStats = ss.CPUStats
|
2016-09-07 23:08:51 +00:00
|
|
|
preRead = ss.Read
|
2016-04-18 18:12:07 +00:00
|
|
|
return &ss
|
2015-06-12 15:27:39 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 14:43:10 +00:00
|
|
|
enc := json.NewEncoder(outStream)
|
2015-06-12 15:27:39 +00:00
|
|
|
|
2019-08-09 12:10:07 +00:00
|
|
|
updates := daemon.subscribeToContainerStats(ctr)
|
|
|
|
defer daemon.unsubscribeToContainerStats(ctr, updates)
|
2015-06-12 15:27:39 +00:00
|
|
|
|
2020-02-07 23:55:06 +00:00
|
|
|
noStreamFirstFrame := !config.OneShot
|
2015-06-16 14:33:49 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case v, ok := <-updates:
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-21 17:57:00 +00:00
|
|
|
statsJSON := getStatJSON(v)
|
2015-06-16 14:33:49 +00:00
|
|
|
if !config.Stream && noStreamFirstFrame {
|
|
|
|
// prime the cpu stats so they aren't 0 in the final output
|
|
|
|
noStreamFirstFrame = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:17:15 +00:00
|
|
|
if err := enc.Encode(statsJSON); err != nil {
|
2015-06-16 14:33:49 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-06-12 15:27:39 +00:00
|
|
|
|
2015-06-16 14:33:49 +00:00
|
|
|
if !config.Stream {
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-25 18:33:54 +00:00
|
|
|
case <-ctx.Done():
|
2015-06-16 14:33:49 +00:00
|
|
|
return nil
|
2015-01-08 02:02:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-24 15:49:26 +00:00
|
|
|
|
|
|
|
func (daemon *Daemon) subscribeToContainerStats(c *container.Container) chan interface{} {
|
2017-01-04 17:01:59 +00:00
|
|
|
return daemon.statsCollector.Collect(c)
|
2016-05-24 15:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (daemon *Daemon) unsubscribeToContainerStats(c *container.Container, ch chan interface{}) {
|
2017-01-04 17:01:59 +00:00
|
|
|
daemon.statsCollector.Unsubscribe(c, ch)
|
2016-05-24 15:49:26 +00:00
|
|
|
}
|
2016-05-27 09:32:26 +00:00
|
|
|
|
|
|
|
// GetContainerStats collects all the stats published by a container
|
|
|
|
func (daemon *Daemon) GetContainerStats(container *container.Container) (*types.StatsJSON, error) {
|
2023-09-29 06:56:09 +00:00
|
|
|
stats, err := daemon.stats(container)
|
|
|
|
if err != nil {
|
|
|
|
goto done
|
|
|
|
}
|
2023-09-26 04:44:04 +00:00
|
|
|
|
2023-09-29 06:56:09 +00:00
|
|
|
// Sample system CPU usage close to container usage to avoid
|
|
|
|
// noise in metric calculations.
|
|
|
|
// FIXME: move to containerd on Linux (not Windows)
|
|
|
|
stats.CPUStats.SystemUsage, stats.CPUStats.OnlineCPUs, err = getSystemCPUUsage()
|
|
|
|
if err != nil {
|
|
|
|
goto done
|
|
|
|
}
|
2016-05-27 09:32:26 +00:00
|
|
|
|
2016-09-07 23:08:51 +00:00
|
|
|
// We already have the network stats on Windows directly from HCS.
|
2023-09-29 06:56:09 +00:00
|
|
|
if !container.Config.NetworkDisabled && runtime.GOOS != "windows" {
|
2023-09-26 04:44:04 +00:00
|
|
|
stats.Networks, err = daemon.getNetworkStats(container)
|
|
|
|
}
|
|
|
|
|
2023-09-29 06:56:09 +00:00
|
|
|
done:
|
2023-09-26 04:44:04 +00:00
|
|
|
switch err.(type) {
|
|
|
|
case nil:
|
|
|
|
return stats, nil
|
|
|
|
case errdefs.ErrConflict, errdefs.ErrNotFound:
|
|
|
|
// return empty stats containing only name and ID if not running or not found
|
|
|
|
return &types.StatsJSON{
|
|
|
|
Name: container.Name,
|
|
|
|
ID: container.ID,
|
|
|
|
}, nil
|
|
|
|
default:
|
2023-09-29 06:56:09 +00:00
|
|
|
log.G(context.TODO()).Errorf("collecting stats for container %s: %v", container.Name, err)
|
2023-09-26 04:44:04 +00:00
|
|
|
return nil, err
|
2016-05-27 09:32:26 +00:00
|
|
|
}
|
|
|
|
}
|