Brendan Gregg | ebdb242 | 2015-08-18 16:53:41 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 2 | # @lint-avoid-python-3-compatibility-imports |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 3 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 4 | # syncsnoop Trace sync() syscall. |
| 5 | # For Linux, uses BCC, eBPF. Embedded C. |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 6 | # |
| 7 | # Written as a basic example of BCC trace & reformat. See |
| 8 | # examples/hello_world.py for a BCC trace with default output example. |
| 9 | # |
| 10 | # Copyright (c) 2015 Brendan Gregg. |
| 11 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 12 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 13 | # 13-Aug-2015 Brendan Gregg Created this. |
mcaleavya | 29dbdda | 2016-02-19 21:22:39 +0000 | [diff] [blame] | 14 | # 19-Feb-2016 Allan McAleavy migrated to BPF_PERF_OUTPUT |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 15 | |
Brendan Gregg | 762b1b4 | 2015-08-18 16:49:48 -0700 | [diff] [blame] | 16 | from __future__ import print_function |
Brenden Blanco | c35989d | 2015-09-02 18:04:07 -0700 | [diff] [blame] | 17 | from bcc import BPF |
mcaleavya | 29dbdda | 2016-02-19 21:22:39 +0000 | [diff] [blame] | 18 | import ctypes as ct |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 19 | |
| 20 | # load BPF program |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 21 | b = BPF(text=""" |
mcaleavya | 29dbdda | 2016-02-19 21:22:39 +0000 | [diff] [blame] | 22 | struct data_t { |
| 23 | u64 ts; |
mcaleavya | 29dbdda | 2016-02-19 21:22:39 +0000 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | BPF_PERF_OUTPUT(events); |
| 27 | |
Yonghong Song | 6433569 | 2018-04-25 00:40:13 -0700 | [diff] [blame] | 28 | void do_sys_sync(void *ctx) { |
mcaleavya | 29dbdda | 2016-02-19 21:22:39 +0000 | [diff] [blame] | 29 | struct data_t data = {}; |
Allan McAleavy | 0ccf708 | 2016-02-19 22:49:21 +0000 | [diff] [blame] | 30 | data.ts = bpf_ktime_get_ns() / 1000; |
mcaleavya | 29dbdda | 2016-02-19 21:22:39 +0000 | [diff] [blame] | 31 | events.perf_submit(ctx, &data, sizeof(data)); |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 32 | }; |
| 33 | """) |
Yonghong Song | 6433569 | 2018-04-25 00:40:13 -0700 | [diff] [blame] | 34 | b.attach_kprobe(event=b.get_syscall_fnname("sync"), |
| 35 | fn_name="do_sys_sync") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 36 | |
mcaleavya | 29dbdda | 2016-02-19 21:22:39 +0000 | [diff] [blame] | 37 | class Data(ct.Structure): |
| 38 | _fields_ = [ |
mcaleavya | 6a372f7 | 2016-02-19 21:39:52 +0000 | [diff] [blame] | 39 | ("ts", ct.c_ulonglong) |
mcaleavya | 29dbdda | 2016-02-19 21:22:39 +0000 | [diff] [blame] | 40 | ] |
| 41 | |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 42 | # header |
Brendan Gregg | 762b1b4 | 2015-08-18 16:49:48 -0700 | [diff] [blame] | 43 | print("%-18s %s" % ("TIME(s)", "CALL")) |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 44 | |
mcaleavya | 29dbdda | 2016-02-19 21:22:39 +0000 | [diff] [blame] | 45 | # process event |
| 46 | def print_event(cpu, data, size): |
| 47 | event = ct.cast(data, ct.POINTER(Data)).contents |
mcaleavya | 6a372f7 | 2016-02-19 21:39:52 +0000 | [diff] [blame] | 48 | print("%-18.9f sync()" % (float(event.ts) / 1000000)) |
mcaleavya | 29dbdda | 2016-02-19 21:22:39 +0000 | [diff] [blame] | 49 | |
| 50 | # loop with callback to print_event |
| 51 | b["events"].open_perf_buffer(print_event) |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 52 | while 1: |
Teng Qin | dbf0029 | 2018-02-28 21:47:50 -0800 | [diff] [blame] | 53 | b.perf_buffer_poll() |