blob: 6e35c175dfa97e5352267feb6f45fadbc9c9765e [file] [log] [blame]
Brendan Greggebdb2422015-08-18 16:53:41 -07001#!/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
mcaleavya29dbdda2016-02-19 21:22:39 +000018import ctypes as ct
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 Song64335692018-04-25 00:40:13 -070028void do_sys_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"),
35 fn_name="do_sys_sync")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070036
mcaleavya29dbdda2016-02-19 21:22:39 +000037class Data(ct.Structure):
38 _fields_ = [
mcaleavya6a372f72016-02-19 21:39:52 +000039 ("ts", ct.c_ulonglong)
mcaleavya29dbdda2016-02-19 21:22:39 +000040 ]
41
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070042# header
Brendan Gregg762b1b42015-08-18 16:49:48 -070043print("%-18s %s" % ("TIME(s)", "CALL"))
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070044
mcaleavya29dbdda2016-02-19 21:22:39 +000045# process event
46def print_event(cpu, data, size):
47 event = ct.cast(data, ct.POINTER(Data)).contents
mcaleavya6a372f72016-02-19 21:39:52 +000048 print("%-18.9f sync()" % (float(event.ts) / 1000000))
mcaleavya29dbdda2016-02-19 21:22:39 +000049
50# loop with callback to print_event
51b["events"].open_perf_buffer(print_event)
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070052while 1:
Teng Qindbf00292018-02-28 21:47:50 -080053 b.perf_buffer_poll()