-
Notifications
You must be signed in to change notification settings - Fork 0
/
majortop.py
executable file
·455 lines (368 loc) · 13.4 KB
/
majortop.py
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# majortop Trace major pagefaults with related filenames.
# For Linux, uses BCC, eBPF. Embedded C.
#
# USAGE: majortop [-h] [-f]
#
# You have to specify maximum depth of file path resolution (MAXDEPTH).
# You should set moderate amount, otherwise, the program won't start. Default is 5.
# File path resolution works only within specific mount namespace.
#
# It's initial release without aggregation feature.
import sys, os
import locale
import argparse
import functools
import ctypes as ct
import struct
import time
from subprocess import call
from collections import namedtuple, Counter
from bcc import BPF
EXAMPLES = """examples:
./majortop # trace all major faults
./majortop -p 181 # only trace PID 181
./majortop -f # follow mode
"""
DEBUG = 0
loadavg = "/proc/loadavg"
# define BPF program
bpf_text = """
#include <linux/mm.h>
#include <linux/mount.h>
enum event_type {
EVENT_RET,
EVENT_PATH,
};
struct data_t {
u32 pid;
enum event_type type;
u64 delta;
u64 inode;
char comm[TASK_COMM_LEN];
char file[DNAME_INLINE_LEN];
u64 address;
unsigned int major;
unsigned int minor;
};
struct val_t {
struct vm_fault *vmf;
u64 ts;
};
BPF_PERF_OUTPUT(events);
BPF_HASH(faults, u32, struct val_t);
int fault_handle_start(struct pt_regs *ctx, struct vm_fault *vmf) {
u32 pid = bpf_get_current_pid_tgid();
FILTER
struct val_t val = {};
val.vmf = vmf;
val.ts = bpf_ktime_get_ns();
faults.update(&pid, &val);
return 0;
}
int fault_handle_return(struct pt_regs *ctx) {
u32 pid = bpf_get_current_pid_tgid();
FILTER
struct val_t *val = faults.lookup(&pid);
if (val == 0)
return 0;
vm_fault_t retcode = PT_REGS_RC(ctx);
if ( !(retcode & VM_FAULT_MAJOR)) {
faults.delete(&pid);
return 0;
}
struct data_t data = {};
data.pid = pid;
data.delta = bpf_ktime_get_ns() - val->ts;
if (bpf_get_current_comm(&data.comm, sizeof(data.comm)) != 0) {
char unknown_comm[] = "...";
__builtin_memcpy(&data.comm, unknown_comm, sizeof(data.comm));
}
struct vm_fault *vmf = val->vmf;
data.address = vmf->address;
struct file *vm_file = vmf->vma->vm_file;
// get device id
struct vfsmount *mnt = vm_file->f_path.mnt;
dev_t dev = mnt->mnt_sb->s_dev;
data.major = MAJOR(dev);
data.minor = MINOR(dev);
struct dentry *dentry = vm_file->f_path.dentry;
// get inode
data.inode = dentry->d_inode->i_ino;
// get filename
data.type = EVENT_PATH;
for (int i = 0; ((i < MAXDEPTH) && (dentry != dentry->d_parent)); i++) {
bpf_probe_read_str(&data.file, sizeof(data.file), dentry->d_name.name);
events.perf_submit(ctx, &data, sizeof(data));
dentry = dentry->d_parent;
}
faults.delete(&pid);
data.type = EVENT_RET;
events.perf_submit(ctx, &data, sizeof(data));
return 0;
}
"""
TASK_COMM_LEN = 16 # linux/sched.h
DNAME_INLINE_LEN = 32 # linux/dcache.h
class Data(ct.Structure):
_fields_ = [("pid", ct.c_uint),
("type", ct.c_int),
("delta", ct.c_ulonglong),
("inode", ct.c_ulonglong),
("comm", ct.c_char * TASK_COMM_LEN),
("file", ct.c_char * DNAME_INLINE_LEN),
("address", ct.c_ulonglong),
("major", ct.c_uint),
("minor", ct.c_uint)]
class EventType(object):
EVENT_RET = 0
EVENT_PATH = 1
class Pagemap(object):
PFN_MASK = 0x7FFFFFFFFFFFFF
PAGEMAP = "/proc/{pid}/pagemap"
KPAGECGROUP = "/proc/kpagecgroup"
KPAGECOUNT = "/proc/kpagecount"
CGROUP = "/sys/fs/cgroup/memory"
@staticmethod
def _read_pagemap(pagemap, offset, entry_size):
try:
with open(pagemap, 'rb') as f:
f.seek(offset, 0)
entry = struct.unpack('Q', f.read(entry_size))[0]
except:
return
return entry
@staticmethod
def _parse_addr(addr):
if isinstance(addr, str):
base = 16 if addr.startswith("0x") else 10
return int(addr, base=base)
return addr
def _get_pfn(self, pid, addr):
addr = self._parse_addr(addr)
pagemap = self.PAGEMAP.format(pid=pid)
if not os.path.isfile(pagemap):
return
offset = (addr / self.page_size) * self.entry_size
entry = self._read_pagemap(pagemap, int(offset), self.entry_size)
if not entry:
return
return entry & self.PFN_MASK
def _find_cgroup(self, inode):
if inode in self.cache_ino:
return self.cache_ino[inode]
for root, subdirs, files in os.walk(self.CGROUP):
if os.stat(root).st_ino == inode:
self.cache_ino[inode] = root
return root
return
def get_cgroup(self, pid, addr):
entry = self._get_pfn(pid, addr)
if not entry:
return
offset = entry * self.entry_size
cgroup_ino = self._read_pagemap(self.KPAGECGROUP, offset, self.entry_size)
if not cgroup_ino:
return
cgroup_name = self._find_cgroup(cgroup_ino)
if not cgroup_name:
return cgroup_ino
return cgroup_name
def __init__(self):
self.cache_ino = dict()
self.page_size = os.sysconf("SC_PAGE_SIZE")
self.entry_size = 8
def process_event(filepaths, event_data, cpu, data, size):
decode_locale = sys.stdin.encoding or locale.getpreferredencoding(True)
event = ct.cast(data, ct.POINTER(Data)).contents
if event.type == EventType.EVENT_PATH:
key = (event.pid, event.address)
strfile = event.file.decode(decode_locale)
if key not in filepaths:
filepaths[key] = strfile
else:
filepaths[key] = os.path.join(strfile, filepaths[key])
elif event.type == EventType.EVENT_RET:
key = (event.pid, event.address)
if key not in filepaths:
return
# pid
event_data.pid = event.pid
# convert ns to ms
event_data.ts = float(event.delta) / 10**6
# extract byte strings using default locale
event_data.comm = event.comm.decode(decode_locale)
# represent memory address
event_data.addr = hex(event.address)
# device id
event_data.dev = "{}:{}".format(event.major, event.minor)
# inode
event_data.inum = event.inode
# filename
event_data.filename = filepaths.pop(key)
def get_cgroup_name(pagemap, pid, addr):
cgroup_name = pagemap.get_cgroup(pid, addr)
if not cgroup_name:
cgroup_name = '...'
elif os.path.isabs(cgroup_name):
cgroup_name = cgroup_name[len(Pagemap.CGROUP) + 1:]
return cgroup_name
def poll(b, event, events, pagemap, cgroup):
while 1:
b.perf_buffer_poll()
# cgroup
if cgroup:
cgroup_name = get_cgroup_name(pagemap, event.pid, event.addr)
sub_entity = cgroup_name
else:
sub_entity = (event.comm, event.pid)
if event.filename not in events:
entity = {
"counter": 1,
"sub_entity": Counter({sub_entity})
}
events[event.filename] = entity
else:
entity = events[event.filename]
entity["counter"] += 1
entity["sub_entity"][sub_entity] += 1
def follow_events(b, event, cgroup):
# header
header_fmt = "{time:<12} {pid:<12} {comm:<20} {cgroup} {dev:<12} {inum:<12} {file}"
# cgroup support
pagemap = Pagemap() if cgroup else None
print(header_fmt.format(time="TIME(ms)",
pid="PID",
comm="COMM",
dev="DEVICE",
inum="INODE",
file="FILENAME",
cgroup="{:<12}".format("CGROUP") if cgroup else ""))
# loop with callback to print event
try:
while 1:
b.perf_buffer_poll()
# cgroup
cgroup_name = get_cgroup_name(pagemap, event.pid, event.addr) if cgroup else None
# print eventMAXACTIVE
print(header_fmt.format(time="{:.4}".format(event.ts),
pid=str(event.pid),
comm=event.comm,
dev=event.dev,
inum=str(event.inum),
file=event.filename,
cgroup="{:<12}".format(cgroup_name) if cgroup else ""))
except KeyboardInterrupt:
print("Detaching...")
def format_loadavg():
with open(loadavg, "r") as stats:
return "%-8s loadavg: %s" % (time.strftime("%H:%M:%S"), stats.read())
def top(b, event, cgroup, interval, noclear, verbose):
from threading import Thread
events = dict()
# suppress stderr
if not verbose:
_stderr, sys.stderr = sys.stderr, open(os.devnull, "w")
# special chars
arrow = "─"
header_arrow = "┌─"
branch_arrow = "├"
root_arrow = "└"
scale_char = "█"
empty_scale_char = "▒"
# header
file_maxlen = 80
scale_width = 25
header_fmt = "{{file:<{}}} {{amount}}".format(file_maxlen)
# cgroup support
pagemap = Pagemap() if cgroup else None
t = Thread(target=poll, args=(b, event, events, pagemap, cgroup), daemon=True)
try:
print('Tracing... Output every {} secs. Hit Ctrl-C to end'.format(interval))
t.start()
while 1:
time.sleep(interval)
# update screen
call("clear") if not noclear else print("")
print(format_loadavg())
print(header_fmt.format(file="FILE",
amount=" AMOUNT"))
for name, options in sorted(events.items(), key=lambda x: x[1]["counter"], reverse=True):
counter = options["counter"]
sub_entity = options["sub_entity"]
print(header_arrow + header_fmt.format(file=name[:file_maxlen],
amount=counter))
for i, sub_counter in enumerate(sorted(sub_entity.most_common(), key=lambda x: x[1], reverse=True)):
sub_name, sub_count = sub_counter
if isinstance(sub_name, tuple):
sub_name = "{} ({})".format(sub_name[0], sub_name[1])
sub_ratio = int(scale_width * sub_count / counter)
scale_bar = scale_char * sub_ratio + empty_scale_char * (scale_width - sub_ratio)
tree_char = root_arrow if i >= (len(sub_entity) - 1) else branch_arrow
print("{tree_char}{arrow}{name:<80}{prc}".format(tree_char=tree_char, arrow=arrow*2, name=sub_name, prc=scale_bar))
events.clear()
except KeyboardInterrupt:
print("Detaching...")
finally:
if not verbose:
sys.stderr.close()
sys.stderr = _stderr
def main():
def perf_buffer_lost_cb(lost_cb):
if args.verbose:
print("Possibly lost {} samples".format(lost_cb))
global bpf_text
args = parse_args()
# pid filter
if args.pid:
bpf_text = bpf_text.replace('FILTER',
'if (pid != {}) {{ return 0; }}'.format(args.pid))
else:
bpf_text = bpf_text.replace('FILTER', '')
# depth replace
bpf_text = bpf_text.replace('MAXDEPTH', args.max_depth)
# print bpf text
if DEBUG or args.ebpf:
print(bpf_text)
if args.ebpf:
return 0
# initialize BPF
b = BPF(text=bpf_text)
b.attach_kprobe(event='filemap_fault', fn_name='fault_handle_start')
b.attach_kretprobe(event='filemap_fault', fn_name='fault_handle_return')
filepaths = dict()
event = namedtuple("Event", ["ts", "pid", "addr", "comm", "dev", "inum", "filename", "cgroup"])
b["events"].open_perf_buffer(
functools.partial(process_event, filepaths, event), lost_cb=perf_buffer_lost_cb)
# follow mode
if args.follow:
follow_events(b, event, args.cgroup)
else:
top(b, event, args.cgroup, args.interval, args.noclear, args.verbose)
return 0
def parse_args():
parser = argparse.ArgumentParser(
description="Trace major faults per file",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=EXAMPLES)
parser.add_argument("-p", "--pid",
help="trace specific PID only")
parser.add_argument("-i", "--interval", type=int, default=1,
help="output interval (seconds)")
parser.add_argument("-C", "--noclear", action="store_true", default=False,
help="do not clear the screen")
parser.add_argument("--max-depth", default="5",
help="descend at minimal level in filesystem hierarchy (defaults to 5)")
parser.add_argument("-f", "--follow", action="store_true", default=False,
help="trace new events sequently")
parser.add_argument("-c", "--cgroup", action="store_true", default=False,
help="show cgroup name which charged faulted memory page")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
parser.add_argument("--verbose", action="store_true",
help=argparse.SUPPRESS)
return parser.parse_args()
if __name__ == '__main__':
sys.exit(main())