Brendan Gregg | bea3430 | 2016-02-13 21:07:23 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
| 4 | # mdflush Trace md flush events. |
| 5 | # For Linux, uses BCC, eBPF. |
| 6 | # |
| 7 | # Todo: add more details of the flush (latency, I/O count). |
| 8 | # |
| 9 | # Copyright 2016 Netflix, Inc. |
| 10 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 11 | # |
| 12 | # 13-Feb-2015 Brendan Gregg Created this. |
| 13 | |
| 14 | from __future__ import print_function |
| 15 | from bcc import BPF |
| 16 | from time import strftime |
| 17 | import ctypes as ct |
| 18 | |
| 19 | # load BPF program |
| 20 | b = BPF(text=""" |
| 21 | #include <uapi/linux/ptrace.h> |
| 22 | #include <linux/sched.h> |
| 23 | #include <linux/genhd.h> |
Sasha Goldshtein | d11179d | 2017-02-13 18:46:24 -0500 | [diff] [blame] | 24 | #include <linux/bio.h> |
Brendan Gregg | bea3430 | 2016-02-13 21:07:23 -0800 | [diff] [blame] | 25 | |
| 26 | struct data_t { |
| 27 | u64 pid; |
| 28 | char comm[TASK_COMM_LEN]; |
| 29 | char disk[DISK_NAME_LEN]; |
| 30 | }; |
| 31 | BPF_PERF_OUTPUT(events); |
| 32 | |
| 33 | int kprobe__md_flush_request(struct pt_regs *ctx, void *mddev, struct bio *bio) |
| 34 | { |
| 35 | struct data_t data = {}; |
| 36 | u32 pid = bpf_get_current_pid_tgid(); |
| 37 | data.pid = pid; |
| 38 | bpf_get_current_comm(&data.comm, sizeof(data.comm)); |
Paul Chaignon | 3ffc80e | 2017-10-23 21:22:38 +0200 | [diff] [blame] | 39 | /* |
| 40 | * The following deals with a kernel version change (in mainline 4.14, although |
| 41 | * it may be backported to earlier kernels) with how the disk name is accessed. |
| 42 | * We handle both pre- and post-change versions here. Please avoid kernel |
| 43 | * version tests like this as much as possible: they inflate the code, test, |
| 44 | * and maintenance burden. |
| 45 | */ |
| 46 | #ifdef bio_dev |
Paul Chaignon | f86f7e8 | 2018-06-14 02:20:03 +0200 | [diff] [blame] | 47 | struct gendisk *bi_disk = bio->bi_disk; |
Paul Chaignon | 3ffc80e | 2017-10-23 21:22:38 +0200 | [diff] [blame] | 48 | #else |
Paul Chaignon | f86f7e8 | 2018-06-14 02:20:03 +0200 | [diff] [blame] | 49 | struct gendisk *bi_disk = bio->bi_bdev->bd_disk; |
Paul Chaignon | 3ffc80e | 2017-10-23 21:22:38 +0200 | [diff] [blame] | 50 | #endif |
Paul Chaignon | f86f7e8 | 2018-06-14 02:20:03 +0200 | [diff] [blame] | 51 | bpf_probe_read(&data.disk, sizeof(data.disk), bi_disk->disk_name); |
Brendan Gregg | bea3430 | 2016-02-13 21:07:23 -0800 | [diff] [blame] | 52 | events.perf_submit(ctx, &data, sizeof(data)); |
| 53 | return 0; |
| 54 | } |
| 55 | """) |
| 56 | |
| 57 | # event data |
| 58 | TASK_COMM_LEN = 16 # linux/sched.h |
| 59 | DISK_NAME_LEN = 32 # linux/genhd.h |
| 60 | class Data(ct.Structure): |
| 61 | _fields_ = [ |
| 62 | ("pid", ct.c_ulonglong), |
| 63 | ("comm", ct.c_char * TASK_COMM_LEN), |
| 64 | ("disk", ct.c_char * DISK_NAME_LEN) |
| 65 | ] |
| 66 | |
| 67 | # header |
| 68 | print("Tracing md flush requests... Hit Ctrl-C to end.") |
| 69 | print("%-8s %-6s %-16s %s" % ("TIME", "PID", "COMM", "DEVICE")) |
| 70 | |
| 71 | # process event |
| 72 | def print_event(cpu, data, size): |
| 73 | event = ct.cast(data, ct.POINTER(Data)).contents |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 74 | print("%-8s %-6d %-16s %s" % (strftime("%H:%M:%S"), event.pid, |
| 75 | event.comm.decode(), event.disk.decode())) |
Brendan Gregg | bea3430 | 2016-02-13 21:07:23 -0800 | [diff] [blame] | 76 | |
| 77 | # read events |
| 78 | b["events"].open_perf_buffer(print_event) |
| 79 | while 1: |
Teng Qin | dbf0029 | 2018-02-28 21:47:50 -0800 | [diff] [blame] | 80 | b.perf_buffer_poll() |