Skip to content

Instantly share code, notes, and snippets.

@supr
Created June 3, 2011 17:30
Show Gist options
  • Save supr/1006741 to your computer and use it in GitHub Desktop.
Save supr/1006741 to your computer and use it in GitHub Desktop.
goroutine for CPU Percentages
go func(ws *websocket.Conn, ch chan bool) {
var prev []string
var vals []string
for ;; {
f,e := os.Open("/proc/stat")
if e != nil {
return
}
defer f.Close()
r := bufio.NewReader(f)
line,_,e := r.ReadLine()
if e != nil {
return
}
vals = strings.Split(string(line), " ", -1)[2:6]
if prev != nil {
percent := (100 * (Sum(vals[:3]) - Sum(prev[:3]))/(Sum(vals) - Sum(prev)))
msg := fmt.Sprintf("%d", percent)
fmt.Println(string(msg))
ws.Write([]byte(msg))
}
prev = vals
}
ch <- true
}(ws, ch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment