-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.go
291 lines (269 loc) · 6.51 KB
/
cpu.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package stats
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
)
// CPUType represents /dev/cputype.
type CPUType struct {
Name string
Clock int // clock rate in MHz
}
func ReadCPUType(ctx context.Context, opts ...Option) (*CPUType, error) {
cfg := newConfig(opts...)
var c CPUType
if err := readCPUType(cfg.rootdir, &c); err != nil {
return nil, err
}
return &c, nil
}
type SysStats struct {
ID int
NumCtxSwitch int64
NumInterrupt int64
NumSyscall int64
NumFault int64
NumTLBFault int64
NumTLBPurge int64
LoadAvg int64 // in units of milli-CPUs and is decayed over time
Idle int // percentage
Interrupt int // percentage
}
// ReadSysStats reads system statistics from /dev/sysstat.
func ReadSysStats(ctx context.Context, opts ...Option) ([]*SysStats, error) {
cfg := newConfig(opts...)
file := filepath.Join(cfg.rootdir, "/dev/sysstat")
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
var stats []*SysStats
for scanner.Scan() {
a := strings.Fields(scanner.Text())
if len(a) != 10 {
continue
}
var (
p intParser
stat SysStats
)
stat.ID = p.ParseInt(a[0], 10)
stat.NumCtxSwitch = p.ParseInt64(a[1], 10)
stat.NumInterrupt = p.ParseInt64(a[2], 10)
stat.NumSyscall = p.ParseInt64(a[3], 10)
stat.NumFault = p.ParseInt64(a[4], 10)
stat.NumTLBFault = p.ParseInt64(a[5], 10)
stat.NumTLBPurge = p.ParseInt64(a[6], 10)
stat.LoadAvg = p.ParseInt64(a[7], 10)
stat.Idle = p.ParseInt(a[8], 10)
stat.Interrupt = p.ParseInt(a[9], 10)
if err := p.Err(); err != nil {
return nil, err
}
stats = append(stats, &stat)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return stats, nil
}
func readCPUType(rootdir string, c *CPUType) error {
file := filepath.Join(rootdir, "/dev/cputype")
b, err := ioutil.ReadFile(file)
if err != nil {
return err
}
b = bytes.TrimSpace(b)
i := bytes.LastIndexByte(b, ' ')
if i < 0 {
return fmt.Errorf("%s: invalid format", file)
}
clock, err := strconv.Atoi(string(b[i+1:]))
if err != nil {
return err
}
c.Name = string(b[:i])
c.Clock = clock
return nil
}
// Time represents /dev/time.
type Time struct {
Unix time.Duration
UnixNano time.Duration
Ticks int64 // clock ticks
Freq int64 //cloc frequency
}
// Uptime returns uptime.
func (t *Time) Uptime() time.Duration {
v := float64(t.Ticks) / float64(t.Freq)
return time.Duration(v*1000_000_000) * time.Nanosecond
}
func ReadTime(ctx context.Context, opts ...Option) (*Time, error) {
cfg := newConfig(opts...)
file := filepath.Join(cfg.rootdir, "/dev/time")
var t Time
if err := readTime(file, &t); err != nil {
return nil, err
}
return &t, nil
}
// ProcStatus represents a /proc/n/status.
type ProcStatus struct {
Name string
User string
State string
Times CPUTime
MemUsed int64 // in units of 1024 bytes
BasePriority uint32 // 0(low) to 19(high)
Priority uint32 // 0(low) to 19(high)
}
// CPUTime represents /dev/cputime or a part of /proc/n/status.
type CPUTime struct {
User time.Duration // the time in user mode (millisecconds)
Sys time.Duration
Real time.Duration
ChildUser time.Duration // exited children and descendants time in user mode
ChildSys time.Duration
ChildReal time.Duration
}
// CPUStats emulates Linux's /proc/stat.
type CPUStats struct {
User time.Duration
Sys time.Duration
Idle time.Duration
}
func ReadCPUStats(ctx context.Context, opts ...Option) (*CPUStats, error) {
cfg := newConfig(opts...)
a, err := ReadSysStats(ctx, opts...)
if err != nil {
return nil, err
}
dir := filepath.Join(cfg.rootdir, "/proc")
d, err := os.Open(dir)
if err != nil {
return nil, err
}
defer d.Close()
names, err := d.Readdirnames(0)
if err != nil {
return nil, err
}
var up uint32parser
pids := make([]uint32, len(names))
for i, s := range names {
if s == "trace" {
continue
}
pids[i] = up.Parse(s)
}
if err := up.err; err != nil {
return nil, err
}
sort.Slice(pids, func(i, j int) bool {
return pids[i] < pids[j]
})
var stat CPUStats
for _, pid := range pids {
s := strconv.FormatUint(uint64(pid), 10)
file := filepath.Join(dir, s, "status")
var p ProcStatus
if err := readProcStatus(file, &p); err != nil {
return nil, err
}
stat.User += p.Times.User
stat.Sys += p.Times.Sys
}
var t Time
file := filepath.Join(cfg.rootdir, "/dev/time")
if err := readTime(file, &t); err != nil {
return nil, err
}
// In multi-processor host, Idle should multiple by number of cores.
u := t.Uptime() * time.Duration(len(a))
stat.Idle = u - stat.User - stat.Sys
return &stat, nil
}
func readProcStatus(file string, p *ProcStatus) error {
b, err := ioutil.ReadFile(file)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
fields := strings.Fields(string(b))
if len(fields) != 12 {
return errors.New("invalid format")
}
p.Name = string(fields[0])
p.User = string(fields[1])
p.State = string(fields[2])
var up uint32parser
p.Times.User = time.Duration(up.Parse(fields[3])) * time.Millisecond
p.Times.Sys = time.Duration(up.Parse(fields[4])) * time.Millisecond
p.Times.Real = time.Duration(up.Parse(fields[5])) * time.Millisecond
p.Times.ChildUser = time.Duration(up.Parse(fields[6])) * time.Millisecond
p.Times.ChildSys = time.Duration(up.Parse(fields[7])) * time.Millisecond
p.Times.ChildReal = time.Duration(up.Parse(fields[8])) * time.Millisecond
p.MemUsed, err = strconv.ParseInt(fields[9], 10, 64)
if err != nil {
return err
}
p.BasePriority = up.Parse(fields[10])
p.Priority = up.Parse(fields[11])
return up.err
}
func readTime(file string, t *Time) error {
b, err := ioutil.ReadFile(file)
if err != nil {
return err
}
fields := strings.Fields(string(b))
if len(fields) != 4 {
return errors.New("invalid format")
}
n, err := strconv.ParseInt(fields[0], 10, 32)
if err != nil {
return err
}
t.Unix = time.Duration(n) * time.Second
v, err := strconv.ParseInt(fields[1], 10, 64)
if err != nil {
return err
}
t.UnixNano = time.Duration(v) * time.Nanosecond
t.Ticks, err = strconv.ParseInt(fields[2], 10, 64)
if err != nil {
return err
}
t.Freq, err = strconv.ParseInt(fields[3], 10, 64)
if err != nil {
return err
}
return nil
}
type uint32parser struct {
err error
}
func (p *uint32parser) Parse(s string) uint32 {
if p.err != nil {
return 0
}
n, err := strconv.ParseUint(s, 10, 32)
if err != nil {
p.err = err
return 0
}
return uint32(n)
}