Brendan Gregg | e422f5e | 2016-07-01 18:38:30 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
Brendan Gregg | a4159da | 2016-07-11 18:27:01 -0700 | [diff] [blame] | 3 | # urandomread Example of instrumenting a kernel tracepoint. |
| 4 | # For Linux, uses BCC, BPF. Embedded C. |
Brendan Gregg | e422f5e | 2016-07-01 18:38:30 -0700 | [diff] [blame] | 5 | # |
| 6 | # REQUIRES: Linux 4.7+ (BPF_PROG_TYPE_TRACEPOINT support). |
| 7 | # |
| 8 | # Test by running this, then in another shell, run: |
| 9 | # dd if=/dev/urandom of=/dev/null bs=1k count=5 |
| 10 | # |
| 11 | # Copyright 2016 Netflix, Inc. |
| 12 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 13 | |
| 14 | from __future__ import print_function |
| 15 | from bcc import BPF |
| 16 | |
Brendan Gregg | a4159da | 2016-07-11 18:27:01 -0700 | [diff] [blame] | 17 | # load BPF program |
| 18 | b = BPF(text=""" |
| 19 | TRACEPOINT_PROBE(random, urandom_read) { |
| 20 | // args is from /sys/kernel/debug/tracing/events/random/urandom_read/format |
Brendan Gregg | e422f5e | 2016-07-01 18:38:30 -0700 | [diff] [blame] | 21 | bpf_trace_printk("%d\\n", args->got_bits); |
| 22 | return 0; |
Liu Bo | 8268edb | 2018-02-09 11:58:40 -0700 | [diff] [blame] | 23 | } |
Brendan Gregg | a4159da | 2016-07-11 18:27:01 -0700 | [diff] [blame] | 24 | """) |
Brendan Gregg | e422f5e | 2016-07-01 18:38:30 -0700 | [diff] [blame] | 25 | |
| 26 | # header |
| 27 | print("%-18s %-16s %-6s %s" % ("TIME(s)", "COMM", "PID", "GOTBITS")) |
| 28 | |
| 29 | # format output |
| 30 | while 1: |
| 31 | try: |
| 32 | (task, pid, cpu, flags, ts, msg) = b.trace_fields() |
| 33 | except ValueError: |
| 34 | continue |
| 35 | print("%-18.9f %-16s %-6d %s" % (ts, task, pid, msg)) |