Alexey Ivanov | cc01a9c | 2019-01-16 09:50:46 -0800 | [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 |
xingfeng2510 | 7b2c114 | 2022-03-24 10:51:26 +0800 | [diff] [blame] | 18 | import sys |
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 | 2da3426 | 2018-06-13 06:12:22 -0700 | [diff] [blame] | 28 | void syscall__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"), |
yonghong-song | 2da3426 | 2018-06-13 06:12:22 -0700 | [diff] [blame] | 35 | fn_name="syscall__sync") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 36 | |
| 37 | # header |
Brendan Gregg | 762b1b4 | 2015-08-18 16:49:48 -0700 | [diff] [blame] | 38 | print("%-18s %s" % ("TIME(s)", "CALL")) |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 39 | |
mcaleavya | 29dbdda | 2016-02-19 21:22:39 +0000 | [diff] [blame] | 40 | # process event |
| 41 | def print_event(cpu, data, size): |
Xiaozhou Liu | 51d62d3 | 2019-02-15 13:03:05 +0800 | [diff] [blame] | 42 | event = b["events"].event(data) |
mcaleavya | 6a372f7 | 2016-02-19 21:39:52 +0000 | [diff] [blame] | 43 | print("%-18.9f sync()" % (float(event.ts) / 1000000)) |
xingfeng2510 | 7b2c114 | 2022-03-24 10:51:26 +0800 | [diff] [blame] | 44 | sys.stdout.flush() |
mcaleavya | 29dbdda | 2016-02-19 21:22:39 +0000 | [diff] [blame] | 45 | |
| 46 | # loop with callback to print_event |
| 47 | b["events"].open_perf_buffer(print_event) |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 48 | while 1: |
Jerome Marchand | 5167127 | 2018-12-19 01:57:24 +0100 | [diff] [blame] | 49 | try: |
| 50 | b.perf_buffer_poll() |
| 51 | except KeyboardInterrupt: |
| 52 | exit() |