1#!/usr/bin/env python 2# @lint-avoid-python-3-compatibility-imports 3# 4# syncsnoop Trace sync() syscall. 5# For Linux, uses BCC, eBPF. Embedded C. 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# 13# 13-Aug-2015 Brendan Gregg Created this. 14# 19-Feb-2016 Allan McAleavy migrated to BPF_PERF_OUTPUT 15 16from __future__ import print_function 17from bcc import BPF 18import ctypes as ct 19 20# load BPF program 21b = BPF(text=""" 22struct data_t { 23 u64 ts; 24}; 25 26BPF_PERF_OUTPUT(events); 27 28void syscall__sync(void *ctx) { 29 struct data_t data = {}; 30 data.ts = bpf_ktime_get_ns() / 1000; 31 events.perf_submit(ctx, &data, sizeof(data)); 32}; 33""") 34b.attach_kprobe(event=b.get_syscall_fnname("sync"), 35 fn_name="syscall__sync") 36 37class Data(ct.Structure): 38 _fields_ = [ 39 ("ts", ct.c_ulonglong) 40 ] 41 42# header 43print("%-18s %s" % ("TIME(s)", "CALL")) 44 45# process event 46def print_event(cpu, data, size): 47 event = ct.cast(data, ct.POINTER(Data)).contents 48 print("%-18.9f sync()" % (float(event.ts) / 1000000)) 49 50# loop with callback to print_event 51b["events"].open_perf_buffer(print_event) 52while 1: 53 try: 54 b.perf_buffer_poll() 55 except KeyboardInterrupt: 56 exit() 57