Created
June 3, 2011 17:30
-
-
Save supr/1006741 to your computer and use it in GitHub Desktop.
goroutine for CPU Percentages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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