Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com>
| ... | ... |
@@ -24,6 +24,7 @@ func newStatsCollector(interval time.Duration) *statsCollector {
|
| 24 | 24 |
interval: interval, |
| 25 | 25 |
publishers: make(map[*Container]*pubsub.Publisher), |
| 26 | 26 |
clockTicks: uint64(system.GetClockTicks()), |
| 27 |
+ bufReader: bufio.NewReaderSize(nil, 128), |
|
| 27 | 28 |
} |
| 28 | 29 |
go s.run() |
| 29 | 30 |
return s |
| ... | ... |
@@ -35,6 +36,7 @@ type statsCollector struct {
|
| 35 | 35 |
interval time.Duration |
| 36 | 36 |
clockTicks uint64 |
| 37 | 37 |
publishers map[*Container]*pubsub.Publisher |
| 38 |
+ bufReader *bufio.Reader |
|
| 38 | 39 |
} |
| 39 | 40 |
|
| 40 | 41 |
// collect registers the container with the collector and adds it to |
| ... | ... |
@@ -121,14 +123,23 @@ const nanoSeconds = 1e9 |
| 121 | 121 |
// getSystemCpuUSage returns the host system's cpu usage in nanoseconds |
| 122 | 122 |
// for the system to match the cgroup readings are returned in the same format. |
| 123 | 123 |
func (s *statsCollector) getSystemCpuUsage() (uint64, error) {
|
| 124 |
+ var line string |
|
| 124 | 125 |
f, err := os.Open("/proc/stat")
|
| 125 | 126 |
if err != nil {
|
| 126 | 127 |
return 0, err |
| 127 | 128 |
} |
| 128 |
- defer f.Close() |
|
| 129 |
- sc := bufio.NewScanner(f) |
|
| 130 |
- for sc.Scan() {
|
|
| 131 |
- parts := strings.Fields(sc.Text()) |
|
| 129 |
+ defer func() {
|
|
| 130 |
+ s.bufReader.Reset(nil) |
|
| 131 |
+ f.Close() |
|
| 132 |
+ }() |
|
| 133 |
+ s.bufReader.Reset(f) |
|
| 134 |
+ err = nil |
|
| 135 |
+ for err == nil {
|
|
| 136 |
+ line, err = s.bufReader.ReadString('\n')
|
|
| 137 |
+ if err != nil {
|
|
| 138 |
+ break |
|
| 139 |
+ } |
|
| 140 |
+ parts := strings.Fields(line) |
|
| 132 | 141 |
switch parts[0] {
|
| 133 | 142 |
case "cpu": |
| 134 | 143 |
if len(parts) < 8 {
|