|
@@ -10,17 +10,30 @@ import (
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
-func readProcStatLine() string {
|
|
|
- f, err := os.Open("/proc/stat")
|
|
|
- defer f.Close()
|
|
|
- rd := bufio.NewReader(f)
|
|
|
+func readFirstLine(rd *bufio.Reader, f *os.File) string {
|
|
|
line, err := rd.ReadString('\n')
|
|
|
if err != nil {
|
|
|
log.Fatalf("could not read /proc/stat, error: %v", err)
|
|
|
}
|
|
|
+ rd.Reset(f)
|
|
|
+ f.Seek(0, 0)
|
|
|
return line
|
|
|
}
|
|
|
|
|
|
+func readProcStatLines(freq int) (string, string) {
|
|
|
+ var line, newline string
|
|
|
+ f, err := os.Open("/proc/stat")
|
|
|
+ if err != nil {
|
|
|
+ log.Fatalf("could not read /proc/stat, error: %v", err)
|
|
|
+ }
|
|
|
+ defer f.Close()
|
|
|
+ rd := bufio.NewReader(f)
|
|
|
+ line = readFirstLine(rd, f)
|
|
|
+ time.Sleep(time.Duration(freq) * time.Second)
|
|
|
+ newline = readFirstLine(rd, f)
|
|
|
+ return line, newline
|
|
|
+}
|
|
|
+
|
|
|
func main() {
|
|
|
var freq int
|
|
|
var partition, netInterface string
|
|
@@ -37,15 +50,10 @@ func main() {
|
|
|
flag.Parse()
|
|
|
|
|
|
fmt.Println(gupta.Hello())
|
|
|
- line := readProcStatLine()
|
|
|
- time.Sleep(time.Second)
|
|
|
- lineNew := readProcStatLine()
|
|
|
|
|
|
for true {
|
|
|
+ line, lineNew := readProcStatLines(freq)
|
|
|
cpustat := gupta.NewCPUStat(line, lineNew)
|
|
|
fmt.Printf("CPU usage %.2f%%\n", cpustat.CPUUsage())
|
|
|
- time.Sleep(time.Duration(freq) * time.Second)
|
|
|
- line = lineNew
|
|
|
- lineNew = readProcStatLine()
|
|
|
}
|
|
|
}
|