From 484470889fcdbbf6bafdc50f00ad80431f96d6d1 Mon Sep 17 00:00:00 2001 From: Andrew Danforth Date: Thu, 8 Jun 2017 23:25:46 -0400 Subject: [PATCH] Only read /proc/stat once when cpu.Times(true) is called on Linux --- cpu/cpu_linux.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/cpu/cpu_linux.go b/cpu/cpu_linux.go index 9f90a7e7..48e45da3 100644 --- a/cpu/cpu_linux.go +++ b/cpu/cpu_linux.go @@ -33,18 +33,15 @@ func Times(percpu bool) ([]TimesStat, error) { filename := common.HostProc("stat") var lines = []string{} if percpu { - var startIdx uint = 1 - for { - linen, _ := common.ReadLinesOffsetN(filename, startIdx, 1) - if len(linen) == 0 { - break - } - line := linen[0] + statlines, err := common.ReadLines(filename) + if err != nil || len(statlines) < 2 { + return []TimesStat{}, nil + } + for _, line := range statlines[1:] { if !strings.HasPrefix(line, "cpu") { break } lines = append(lines, line) - startIdx++ } } else { lines, _ = common.ReadLinesOffsetN(filename, 0, 1)