blob: 90dcb2098fec3b718039a0943763ce9615d6e0de [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
Brendan Gregg61c7ff12015-09-10 13:43:34 -070028void kprobe__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""")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070034
mcaleavya29dbdda2016-02-19 21:22:39 +000035class Data(ct.Structure):
36 _fields_ = [
mcaleavya6a372f72016-02-19 21:39:52 +000037 ("ts", ct.c_ulonglong)
mcaleavya29dbdda2016-02-19 21:22:39 +000038 ]
39
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070040# header
Brendan Gregg762b1b42015-08-18 16:49:48 -070041print("%-18s %s" % ("TIME(s)", "CALL"))
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070042
mcaleavya29dbdda2016-02-19 21:22:39 +000043# process event
44def print_event(cpu, data, size):
45 event = ct.cast(data, ct.POINTER(Data)).contents
mcaleavya6a372f72016-02-19 21:39:52 +000046 print("%-18.9f sync()" % (float(event.ts) / 1000000))
mcaleavya29dbdda2016-02-19 21:22:39 +000047
48# loop with callback to print_event
49b["events"].open_perf_buffer(print_event)
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070050while 1:
mcaleavya29dbdda2016-02-19 21:22:39 +000051 b.kprobe_poll()