blob: e96cd3c4bc90fab8e46aaa43e13830f8b71c7562 [file] [log] [blame]
Alexey Ivanovcc01a9c2019-01-16 09:50:46 -08001#!/usr/bin/python
Alexei Starovoitovbdf07732016-01-14 10:09:20 -08002# @lint-avoid-python-3-compatibility-imports
Brendan Gregg48fbc3e2015-08-18 14:56:14 -07003#
Alexei Starovoitovbdf07732016-01-14 10:09:20 -08004# syncsnoop Trace sync() syscall.
5# For Linux, uses BCC, eBPF. Embedded C.
Brendan Gregg48fbc3e2015-08-18 14:56:14 -07006#
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 Starovoitovbdf07732016-01-14 10:09:20 -080013# 13-Aug-2015 Brendan Gregg Created this.
mcaleavya29dbdda2016-02-19 21:22:39 +000014# 19-Feb-2016 Allan McAleavy migrated to BPF_PERF_OUTPUT
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070015
Brendan Gregg762b1b42015-08-18 16:49:48 -070016from __future__ import print_function
Brenden Blancoc35989d2015-09-02 18:04:07 -070017from bcc import BPF
xingfeng25107b2c1142022-03-24 10:51:26 +080018import sys
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070019
20# load BPF program
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080021b = BPF(text="""
mcaleavya29dbdda2016-02-19 21:22:39 +000022struct data_t {
23 u64 ts;
mcaleavya29dbdda2016-02-19 21:22:39 +000024};
25
26BPF_PERF_OUTPUT(events);
27
yonghong-song2da34262018-06-13 06:12:22 -070028void syscall__sync(void *ctx) {
mcaleavya29dbdda2016-02-19 21:22:39 +000029 struct data_t data = {};
Allan McAleavy0ccf7082016-02-19 22:49:21 +000030 data.ts = bpf_ktime_get_ns() / 1000;
mcaleavya29dbdda2016-02-19 21:22:39 +000031 events.perf_submit(ctx, &data, sizeof(data));
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070032};
33""")
Yonghong Song64335692018-04-25 00:40:13 -070034b.attach_kprobe(event=b.get_syscall_fnname("sync"),
yonghong-song2da34262018-06-13 06:12:22 -070035 fn_name="syscall__sync")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070036
37# header
Brendan Gregg762b1b42015-08-18 16:49:48 -070038print("%-18s %s" % ("TIME(s)", "CALL"))
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070039
mcaleavya29dbdda2016-02-19 21:22:39 +000040# process event
41def print_event(cpu, data, size):
Xiaozhou Liu51d62d32019-02-15 13:03:05 +080042 event = b["events"].event(data)
mcaleavya6a372f72016-02-19 21:39:52 +000043 print("%-18.9f sync()" % (float(event.ts) / 1000000))
xingfeng25107b2c1142022-03-24 10:51:26 +080044 sys.stdout.flush()
mcaleavya29dbdda2016-02-19 21:22:39 +000045
46# loop with callback to print_event
47b["events"].open_perf_buffer(print_event)
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070048while 1:
Jerome Marchand51671272018-12-19 01:57:24 +010049 try:
50 b.perf_buffer_poll()
51 except KeyboardInterrupt:
52 exit()