Brendan Gregg | 177e07e | 2015-08-18 16:11:35 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 2 | # |
| 3 | # syncsnoop Trace sync() syscall. |
| 4 | # For Linux, uses BCC, eBPF. Embedded C. |
| 5 | # |
| 6 | # Written as a basic example of BCC trace & reformat. See |
| 7 | # examples/hello_world.py for a BCC trace with default output example. |
| 8 | # |
| 9 | # Copyright (c) 2015 Brendan Gregg. |
| 10 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 11 | # |
| 12 | # 13-Aug-2015 Brendan Gregg Created this. |
| 13 | |
| 14 | from bpf import BPF |
| 15 | import sys |
| 16 | |
| 17 | # load BPF program |
| 18 | b = BPF(text = """ |
| 19 | int do_sync(void *ctx) { |
| 20 | bpf_trace_printk("sync()\\n"); |
| 21 | return 0; |
| 22 | }; |
| 23 | """) |
| 24 | BPF.attach_kprobe(b.load_func("do_sync", BPF.KPROBE), "sys_sync") |
| 25 | |
| 26 | # header |
| 27 | print "%-18s %s" % ("TIME(s)", "CALL") |
| 28 | |
| 29 | # open trace pipe |
| 30 | try: |
| 31 | trace = open("/sys/kernel/debug/tracing/trace_pipe", "r") |
| 32 | except: |
| 33 | print >> sys.stderr, "ERROR: opening trace_pipe" |
| 34 | exit(1) |
| 35 | |
| 36 | # format output |
| 37 | while 1: |
| 38 | try: |
| 39 | line = trace.readline().rstrip() |
| 40 | except KeyboardInterrupt: |
| 41 | pass; exit() |
| 42 | |
| 43 | prolog, time_s, colon, word = line.rsplit(" ", 3) |
| 44 | time_s = time_s[:-1] # strip trailing ":" |
| 45 | |
| 46 | print "%-18s %s" % (time_s, word) |