...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package procfs
15
16 import (
17 "bufio"
18 "errors"
19 "os"
20 "regexp"
21 "strconv"
22 )
23
24 var (
25 cpuLineRE = regexp.MustCompile(`cpu(\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+)`)
26 procLineRE = regexp.MustCompile(`(\d+) (\d+) (\d+)`)
27 )
28
29
30
31
32
33
34
35
36
37
38
39 type Schedstat struct {
40 CPUs []*SchedstatCPU
41 }
42
43
44 type SchedstatCPU struct {
45 CPUNum string
46
47 RunningNanoseconds uint64
48 WaitingNanoseconds uint64
49 RunTimeslices uint64
50 }
51
52
53 type ProcSchedstat struct {
54 RunningNanoseconds uint64
55 WaitingNanoseconds uint64
56 RunTimeslices uint64
57 }
58
59
60 func (fs FS) Schedstat() (*Schedstat, error) {
61 file, err := os.Open(fs.proc.Path("schedstat"))
62 if err != nil {
63 return nil, err
64 }
65 defer file.Close()
66
67 stats := &Schedstat{}
68 scanner := bufio.NewScanner(file)
69
70 for scanner.Scan() {
71 match := cpuLineRE.FindStringSubmatch(scanner.Text())
72 if match != nil {
73 cpu := &SchedstatCPU{}
74 cpu.CPUNum = match[1]
75
76 cpu.RunningNanoseconds, err = strconv.ParseUint(match[8], 10, 64)
77 if err != nil {
78 continue
79 }
80
81 cpu.WaitingNanoseconds, err = strconv.ParseUint(match[9], 10, 64)
82 if err != nil {
83 continue
84 }
85
86 cpu.RunTimeslices, err = strconv.ParseUint(match[10], 10, 64)
87 if err != nil {
88 continue
89 }
90
91 stats.CPUs = append(stats.CPUs, cpu)
92 }
93 }
94
95 return stats, nil
96 }
97
98 func parseProcSchedstat(contents string) (ProcSchedstat, error) {
99 var (
100 stats ProcSchedstat
101 err error
102 )
103 match := procLineRE.FindStringSubmatch(contents)
104
105 if match != nil {
106 stats.RunningNanoseconds, err = strconv.ParseUint(match[1], 10, 64)
107 if err != nil {
108 return stats, err
109 }
110
111 stats.WaitingNanoseconds, err = strconv.ParseUint(match[2], 10, 64)
112 if err != nil {
113 return stats, err
114 }
115
116 stats.RunTimeslices, err = strconv.ParseUint(match[3], 10, 64)
117 return stats, err
118 }
119
120 return stats, errors.New("could not parse schedstat")
121 }
122
View as plain text