blob: e5fa78e3e5df90f4917d3728309fc21c89891dc8 [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
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070018
19# load BPF program
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080020b = BPF(text="""
mcaleavya29dbdda2016-02-19 21:22:39 +000021struct data_t {
22 u64 ts;
mcaleavya29dbdda2016-02-19 21:22:39 +000023};
24
25BPF_PERF_OUTPUT(events);
26
yonghong-song2da34262018-06-13 06:12:22 -070027void syscall__sync(void *ctx) {
mcaleavya29dbdda2016-02-19 21:22:39 +000028 struct data_t data = {};
Allan McAleavy0ccf7082016-02-19 22:49:21 +000029 data.ts = bpf_ktime_get_ns() / 1000;
mcaleavya29dbdda2016-02-19 21:22:39 +000030 events.perf_submit(ctx, &data, sizeof(data));
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070031};
32""")
Yonghong Song64335692018-04-25 00:40:13 -070033b.attach_kprobe(event=b.get_syscall_fnname("sync"),
yonghong-song2da34262018-06-13 06:12:22 -070034 fn_name="syscall__sync")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070035
36# header
Brendan Gregg762b1b42015-08-18 16:49:48 -070037print("%-18s %s" % ("TIME(s)", "CALL"))
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070038
mcaleavya29dbdda2016-02-19 21:22:39 +000039# process event
40def print_event(cpu, data, size):
Xiaozhou Liu51d62d32019-02-15 13:03:05 +080041 event = b["events"].event(data)
mcaleavya6a372f72016-02-19 21:39:52 +000042 print("%-18.9f sync()" % (float(event.ts) / 1000000))
mcaleavya29dbdda2016-02-19 21:22:39 +000043
44# loop with callback to print_event
45b["events"].open_perf_buffer(print_event)
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070046while 1:
Jerome Marchand51671272018-12-19 01:57:24 +010047 try:
48 b.perf_buffer_poll()
49 except KeyboardInterrupt:
50 exit()