• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
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>
24#include <linux/bio.h>
25
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));
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
47    struct gendisk *bi_disk = bio->bi_disk;
48#else
49    struct gendisk *bi_disk = bio->bi_bdev->bd_disk;
50#endif
51    bpf_probe_read(&data.disk, sizeof(data.disk), bi_disk->disk_name);
52    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
74    print("%-8s %-6d %-16s %s" % (strftime("%H:%M:%S"), event.pid,
75        event.comm.decode('utf-8', 'replace'),
76        event.disk.decode('utf-8', 'replace')))
77
78# read events
79b["events"].open_perf_buffer(print_event)
80while 1:
81    b.perf_buffer_poll()
82