mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #13246 from unclejack/refactor_statscollector
daemon/stats_collector: refactor getSystemCpuUsage
This commit is contained in:
commit
88127ce475
1 changed files with 15 additions and 4 deletions
|
@ -24,6 +24,7 @@ func newStatsCollector(interval time.Duration) *statsCollector {
|
||||||
interval: interval,
|
interval: interval,
|
||||||
publishers: make(map[*Container]*pubsub.Publisher),
|
publishers: make(map[*Container]*pubsub.Publisher),
|
||||||
clockTicks: uint64(system.GetClockTicks()),
|
clockTicks: uint64(system.GetClockTicks()),
|
||||||
|
bufReader: bufio.NewReaderSize(nil, 128),
|
||||||
}
|
}
|
||||||
go s.run()
|
go s.run()
|
||||||
return s
|
return s
|
||||||
|
@ -35,6 +36,7 @@ type statsCollector struct {
|
||||||
interval time.Duration
|
interval time.Duration
|
||||||
clockTicks uint64
|
clockTicks uint64
|
||||||
publishers map[*Container]*pubsub.Publisher
|
publishers map[*Container]*pubsub.Publisher
|
||||||
|
bufReader *bufio.Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
// collect registers the container with the collector and adds it to
|
// collect registers the container with the collector and adds it to
|
||||||
|
@ -121,14 +123,23 @@ const nanoSeconds = 1e9
|
||||||
// getSystemCpuUSage returns the host system's cpu usage in nanoseconds
|
// getSystemCpuUSage returns the host system's cpu usage in nanoseconds
|
||||||
// for the system to match the cgroup readings are returned in the same format.
|
// for the system to match the cgroup readings are returned in the same format.
|
||||||
func (s *statsCollector) getSystemCpuUsage() (uint64, error) {
|
func (s *statsCollector) getSystemCpuUsage() (uint64, error) {
|
||||||
|
var line string
|
||||||
f, err := os.Open("/proc/stat")
|
f, err := os.Open("/proc/stat")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer func() {
|
||||||
sc := bufio.NewScanner(f)
|
s.bufReader.Reset(nil)
|
||||||
for sc.Scan() {
|
f.Close()
|
||||||
parts := strings.Fields(sc.Text())
|
}()
|
||||||
|
s.bufReader.Reset(f)
|
||||||
|
err = nil
|
||||||
|
for err == nil {
|
||||||
|
line, err = s.bufReader.ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
parts := strings.Fields(line)
|
||||||
switch parts[0] {
|
switch parts[0] {
|
||||||
case "cpu":
|
case "cpu":
|
||||||
if len(parts) < 8 {
|
if len(parts) < 8 {
|
||||||
|
|
Loading…
Add table
Reference in a new issue