forked from uber-archive/cpustat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetlink.go
355 lines (324 loc) · 11.4 KB
/
netlink.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package main
// #include <linux/netlink.h>
// #include <linux/genetlink.h>
// #include <linux/taskstats.h>
import "C"
import (
"bytes"
"encoding/binary"
"fmt"
"os"
"syscall"
"time"
netlink "github.com/remyoudompheng/go-netlink"
"github.com/remyoudompheng/go-netlink/genl"
)
type taskStats struct {
captureTime time.Time
prevTime time.Time
version uint16 // internal, probably 8
exitcode uint32 // not used until we listen for events
flag uint8 // not sure
nice uint8 // seems like it'd be obvious, but it isn't
cpudelaycount uint64 // delay count waiting for CPU, while runnable
cpudelaytotal uint64 // delay time waiting for CPU, while runnable, in ns
blkiodelaycount uint64 // delay count waiting for disk
blkiodelaytotal uint64 // delay time waiting for disk
swapindelaycount uint64 // delay count waiting for swap
swapindelaytotal uint64 // delay time waiting for swap
cpurunrealtotal uint64 // probably the time spent running on CPU, in ns, perhaps adjusted for virt steal
cpurunvirtualtotal uint64 // probably the time spent running on CPU, in ns
comm string // common name, best to ignore this and use /proc/pid/cmdline
sched uint8 // scheduling discipline, whatever that means
uid uint32 // user id
gid uint32 // group id
pid uint32 // process id, should be the same as TGid, maybe
ppid uint32 // parent process id
btime uint32 // begin time since epoch
etime uint64 // elapsed total time in us
utime uint64 // elapsed user time in us
stime uint64 // elapsed system time in us
minflt uint64 // major page fault count
majflt uint64 // minor page fault count
coremem uint64 // RSS in MBytes/usec
virtmem uint64 // VSZ in MBytes/usec
hiwaterrss uint64 // highest RSS in KB
hiwatervm uint64 // highest VSZ in KB
readchar uint64 // total bytes read
writechar uint64 // total bytes written
readsyscalls uint64 // read system calls
writesyscalls uint64 // write system calls
readbytes uint64 // bytes read total
writebytes uint64 // bytes written total
cancelledwritebytes uint64 // bytes of cancelled write IO, whatever that is
nvcsw uint64 // voluntary context switches
nivcsw uint64 // involuntary context switches
utimescaled uint64 // user time scaled by CPU frequency
stimescaled uint64 // system time scaled by CPU frequency
cpuscaledrunrealtotal uint64 // total time scaled by CPU frequency
freepagescount uint64 // delay count waiting for memory reclaim
freepagesdelaytotal uint64 // delay time waiting for memory reclaim in unknown units
}
func stringFromBytes(c []byte) string {
nullPos := 0
i := 0
for ; i < len(c); i++ {
if c[i] == 0 {
nullPos = i
break
}
}
return string(c[:nullPos])
}
func parseResponse(msg syscall.NetlinkMessage) (*taskStats, error) {
var err error
buf := bytes.NewBuffer(msg.Data)
var genHeader netlink.GenlMsghdr
err = binary.Read(buf, netlink.SystemEndianness, &genHeader)
if err != nil {
return nil, err
}
var attr syscall.RtAttr
err = binary.Read(buf, netlink.SystemEndianness, &attr)
if err != nil {
return nil, err
}
err = binary.Read(buf, netlink.SystemEndianness, &attr)
if err != nil {
return nil, err
}
var tgid uint32
err = binary.Read(buf, netlink.SystemEndianness, &tgid)
if err != nil {
return nil, err
}
err = binary.Read(buf, netlink.SystemEndianness, &attr)
if err != nil {
return nil, err
}
payload := buf.Bytes()
offset := 0
endian := netlink.SystemEndianness
var stats taskStats
stats.captureTime = time.Now()
stats.version = endian.Uint16(payload[offset : offset+2])
offset += 2
offset += 2 // 2 byte padding
stats.exitcode = endian.Uint32(payload[offset : offset+4])
offset += 4
stats.flag = uint8(payload[offset])
offset++
stats.nice = uint8(payload[offset])
offset++
offset += 6 // 6 byte padding
stats.cpudelaycount = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.cpudelaytotal = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.blkiodelaycount = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.blkiodelaytotal = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.swapindelaycount = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.swapindelaytotal = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.cpurunrealtotal = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.cpurunvirtualtotal = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.comm = stringFromBytes(payload[offset : offset+32])
offset += 32
stats.sched = payload[offset]
offset++
offset += 7 // 7 byte padding
stats.uid = endian.Uint32(payload[offset : offset+4])
offset += 4
stats.gid = endian.Uint32(payload[offset : offset+4])
offset += 4
stats.pid = endian.Uint32(payload[offset : offset+4])
offset += 4
stats.ppid = endian.Uint32(payload[offset : offset+4])
offset += 4
stats.btime = endian.Uint32(payload[offset : offset+4])
offset += 4
if stats.pid != tgid {
fmt.Printf("read value for unexpected pid %d != %d %+v\n", stats.pid, tgid, stats)
}
offset += 4 // 4 byte padding
stats.etime = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.utime = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.stime = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.minflt = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.majflt = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.coremem = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.virtmem = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.hiwaterrss = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.hiwatervm = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.readchar = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.writechar = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.readsyscalls = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.writesyscalls = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.readbytes = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.writebytes = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.cancelledwritebytes = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.nvcsw = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.nivcsw = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.utimescaled = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.stimescaled = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.cpuscaledrunrealtotal = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.freepagescount = endian.Uint64(payload[offset : offset+8])
offset += 8
stats.freepagesdelaytotal = endian.Uint64(payload[offset : offset+8])
offset += 8
return &stats, nil
}
type NetlinkError struct {
msg string
Code int32
}
func (e *NetlinkError) Error() string {
return e.msg
}
func parseError(msg syscall.NetlinkMessage) error {
var errno int32
buf := bytes.NewBuffer(msg.Data)
err := binary.Read(buf, netlink.SystemEndianness, &errno)
if err != nil {
return err
}
return &NetlinkError{"netlink read", errno}
}
func parseTaskStats(msg syscall.NetlinkMessage) (*taskStats, error) {
switch msg.Header.Type {
case syscall.NLMSG_NOOP:
fmt.Printf("NLMSG_NOOP")
return nil, nil
case syscall.NLMSG_ERROR:
return nil, parseError(msg)
case syscall.NLMSG_DONE:
fmt.Printf("NLMSG_DONE\n")
return nil, nil
case syscall.NLMSG_OVERRUN:
fmt.Printf("NLMSG_OVERRUN\n")
}
return parseResponse(msg)
}
var (
systemEndianness = binary.LittleEndian
globalSeq = uint32(0)
)
func cmdMessage(family uint16, pid int) (msg netlink.GenericNetlinkMessage) {
msg.Header.Type = family
msg.Header.Flags = syscall.NLM_F_REQUEST
msg.GenHeader.Command = genl.TASKSTATS_CMD_GET
msg.GenHeader.Version = genl.TASKSTATS_GENL_VERSION
buf := bytes.NewBuffer([]byte{})
netlink.PutAttribute(buf, genl.TASKSTATS_CMD_ATTR_PID, uint32(pid))
msg.Data = buf.Bytes()
return msg
}
func sendCmdMessage(conn *NLConn, pid int) error {
globalSeq++
// payload of this message is genl header + a single nl attribute
attrBuf := bytes.NewBuffer([]byte{})
netlink.PutAttribute(attrBuf, genl.TASKSTATS_CMD_ATTR_PID, uint32(pid))
attrBytes := attrBuf.Bytes()
msg := netlink.GenericNetlinkMessage{}
// this packet: is nl header(16) + genl header(4) + attribute(8) = 28
msg.Header.Len = uint32(syscall.NLMSG_HDRLEN + 4 + len(attrBytes))
msg.Header.Type = conn.family
msg.Header.Flags = syscall.NLM_F_REQUEST
msg.Header.Seq = globalSeq
msg.Header.Pid = uint32(conn.pid)
msg.GenHeader.Command = genl.TASKSTATS_CMD_GET
msg.GenHeader.Version = genl.TASKSTATS_GENL_VERSION
// don't set reserved because it's reserved
outBuf := bytes.NewBuffer([]byte{})
binary.Write(outBuf, systemEndianness, msg.Header)
binary.Write(outBuf, systemEndianness, msg.GenHeader)
outBuf.Write(attrBytes)
_, err := conn.sock.Write(outBuf.Bytes())
return err
}
func taskstatsLookupPid(conn *NLConn, pid int) (*taskStats, error) {
sendCmdMessage(conn, pid)
// cmd := cmdMessage(conn.family, pid)
// netlink.WriteMessage(conn.sock, &cmd)
res, err := netlink.ReadMessage(conn.sock)
if err != nil {
panic(err)
}
parsed, err := parseTaskStats(res)
if err != nil {
nerr := err.(*NetlinkError)
if nerr.Code == -1 {
panic("No permission")
} else {
return nil, &NetlinkError{"proc missing", nerr.Code}
}
} else {
return parsed, nil
}
}
// NLConn holds the context necessary to pass around to external callers
// This family thing should really be encapsulated within netlink.NetlinkConn, but it isn't.
type NLConn struct {
family uint16
sock *netlink.NetlinkConn
pid int
}
// NLInit sets up a new taskstats netlink socket
func NLInit() *NLConn {
idMap, err := genl.GetFamilyIDs()
if err != nil {
panic(err)
}
taskstatsGenlName := string(C.TASKSTATS_GENL_NAME)
family := idMap[taskstatsGenlName]
sock, err := netlink.DialNetlink("generic", 0)
if err != nil {
panic(err)
}
return &NLConn{family, sock, os.Getpid()}
}