blob: 507614b80083e99639bbcf4b1bc44b9269c38a00 [file] [log] [blame]
Alexey Ivanov777e8022019-01-03 13:46:38 -08001#!/usr/bin/env python
Brendan Greggbea34302016-02-13 21:07:23 -08002# @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
14from __future__ import print_function
15from bcc import BPF
16from time import strftime
17import ctypes as ct
18
19# load BPF program
20b = BPF(text="""
21#include <uapi/linux/ptrace.h>
22#include <linux/sched.h>
23#include <linux/genhd.h>
Sasha Goldshteind11179d2017-02-13 18:46:24 -050024#include <linux/bio.h>
Brendan Greggbea34302016-02-13 21:07:23 -080025
26struct data_t {
27 u64 pid;
28 char comm[TASK_COMM_LEN];
29 char disk[DISK_NAME_LEN];
30};
31BPF_PERF_OUTPUT(events);
32
33int 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 Chaignon3ffc80e2017-10-23 21:22:38 +020039/*
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 Chaignonf86f7e82018-06-14 02:20:03 +020047 struct gendisk *bi_disk = bio->bi_disk;
Paul Chaignon3ffc80e2017-10-23 21:22:38 +020048#else
Paul Chaignonf86f7e82018-06-14 02:20:03 +020049 struct gendisk *bi_disk = bio->bi_bdev->bd_disk;
Paul Chaignon3ffc80e2017-10-23 21:22:38 +020050#endif
Paul Chaignonf86f7e82018-06-14 02:20:03 +020051 bpf_probe_read(&data.disk, sizeof(data.disk), bi_disk->disk_name);
Brendan Greggbea34302016-02-13 21:07:23 -080052 events.perf_submit(ctx, &data, sizeof(data));
53 return 0;
54}
55""")
56
57# event data
58TASK_COMM_LEN = 16 # linux/sched.h
59DISK_NAME_LEN = 32 # linux/genhd.h
60class 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
68print("Tracing md flush requests... Hit Ctrl-C to end.")
69print("%-8s %-6s %-16s %s" % ("TIME", "PID", "COMM", "DEVICE"))
70
71# process event
72def print_event(cpu, data, size):
73 event = ct.cast(data, ct.POINTER(Data)).contents
Rafael F78948e42017-03-26 14:54:25 +020074 print("%-8s %-6d %-16s %s" % (strftime("%H:%M:%S"), event.pid,
jeromemarchandb96ebcd2018-10-10 01:58:15 +020075 event.comm.decode('utf-8', 'replace'),
76 event.disk.decode('utf-8', 'replace')))
Brendan Greggbea34302016-02-13 21:07:23 -080077
78# read events
79b["events"].open_perf_buffer(print_event)
80while 1:
Jerome Marchand51671272018-12-19 01:57:24 +010081 try:
82 b.perf_buffer_poll()
83 except KeyboardInterrupt:
84 exit()