-
Notifications
You must be signed in to change notification settings - Fork 32
/
sched.go
57 lines (48 loc) · 1.84 KB
/
sched.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package linux
import "strconv"
// ProcSched contains statistics about process scheduling, utilization, and switches.
type ProcSched struct {
NRSwitches uint64 // nr_switches
NRVoluntarySwitches uint64 // nr_voluntary_switches
NRInvoluntarySwitches uint64 // nr_involuntary_switches
SEAvgLoadSum uint64 // se.avg.load_sum
SEAvgUtilSum uint64 // se.avg.util_sum
SEAvgLoadAvg uint64 // se.avg.load_avg
SEAvgUtilAvg uint64 // se.avg.util_avg
}
// ReadProcSched returns a ProcSched and error, if any, for a PID.
func ReadProcSched(pid int) (proc ProcSched, err error) {
defer func() { err = convertPanicToError(recover()) }()
proc = parseProcSched(readProcFile(pid, "sched"))
return
}
// ParseProcSched processes system process scheduling data and returns a ProcSched and error, if any.
func ParseProcSched(s string) (proc ProcSched, err error) {
defer func() { err = convertPanicToError(recover()) }()
proc = parseProcSched(s)
return
}
func parseProcSched(s string) (proc ProcSched) {
intFields := map[string]*uint64{
"nr_switches": &proc.NRSwitches,
"nr_voluntary_switches": &proc.NRVoluntarySwitches,
"nr_involuntary_switches": &proc.NRInvoluntarySwitches,
"se.avg.load_sum": &proc.SEAvgLoadSum,
"se.avg.util_sum": &proc.SEAvgUtilSum,
"se.avg.load_avg": &proc.SEAvgLoadAvg,
"se.avg.util_avg": &proc.SEAvgUtilAvg,
}
s = skipLine(s) // <progname> (<pid>, #threads: 1)
s = skipLine(s) // -------------------------------
forEachProperty(s, func(key, val string) {
if field := intFields[key]; field != nil {
v, e := strconv.ParseUint(val, 10, 64)
check(e)
// There's no much doc on the structure of this file, unsure if
// there would be a breakdown per thread... unlikely but this should
// cover for it.
*field += v
}
})
return
}