daemon/stats: use const for clockTicksPerSecond

The value comes from `C.sysconf(C._SC_CLK_TCK)`, and on Linux it's a
constant which is safe to be hard coded. See for example in the Musl
libc source code https://git.musl-libc.org/cgit/musl/tree/src/conf/sysconf.c#n29

This removes the github.com/opencontainers/runc/libcontainer/system
dependency from this package.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2020-07-08 12:50:28 +02:00
parent c833222d54
commit b42ac8d370
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
3 changed files with 9 additions and 20 deletions

View File

@ -19,9 +19,6 @@ type Collector struct {
interval time.Duration
publishers map[*container.Container]*pubsub.Publisher
bufReader *bufio.Reader
// The following fields are not set on Windows currently.
clockTicksPerSecond uint64
}
// NewCollector creates a stats collector that will poll the supervisor with the specified interval
@ -33,9 +30,6 @@ func NewCollector(supervisor supervisor, interval time.Duration) *Collector {
bufReader: bufio.NewReaderSize(nil, 128),
}
s.cond = sync.NewCond(&s.m)
platformNewStatsCollector(s)
return s
}

View File

@ -8,17 +8,17 @@ import (
"strconv"
"strings"
"github.com/opencontainers/runc/libcontainer/system"
"golang.org/x/sys/unix"
)
// platformNewStatsCollector performs platform specific initialisation of the
// Collector structure.
func platformNewStatsCollector(s *Collector) {
s.clockTicksPerSecond = uint64(system.GetClockTicks())
}
const nanoSecondsPerSecond = 1e9
const (
// The value comes from `C.sysconf(C._SC_CLK_TCK)`, and
// on Linux it's a constant which is safe to be hard coded,
// so we can avoid using cgo here. For details, see:
// https://github.com/containerd/cgroups/pull/12
clockTicksPerSecond = 100
nanoSecondsPerSecond = 1e9
)
// getSystemCPUUsage returns the host system's cpu usage in
// nanoseconds. An error is returned if the format of the underlying
@ -59,7 +59,7 @@ func (s *Collector) getSystemCPUUsage() (uint64, error) {
totalClockTicks += v
}
return (totalClockTicks * nanoSecondsPerSecond) /
s.clockTicksPerSecond, nil
clockTicksPerSecond, nil
}
}
return 0, fmt.Errorf("invalid stat format. Error trying to parse the '/proc/stat' file")

View File

@ -1,10 +1,5 @@
package stats // import "github.com/docker/docker/daemon/stats"
// platformNewStatsCollector performs platform specific initialisation of the
// Collector structure. This is a no-op on Windows.
func platformNewStatsCollector(s *Collector) {
}
// getSystemCPUUsage returns the host system's cpu usage in
// nanoseconds. An error is returned if the format of the underlying
// file does not match. This is a no-op on Windows.