blob: 69bcf8d11150a9475aca6e62d8563eb9124f8193 [file] [log] [blame]
Alexey Ivanovcc01a9c2019-01-16 09:50:46 -08001#!/usr/bin/python
Brendan Gregge422f5e2016-07-01 18:38:30 -07002#
Brendan Gregga4159da2016-07-11 18:27:01 -07003# urandomread Example of instrumenting a kernel tracepoint.
4# For Linux, uses BCC, BPF. Embedded C.
Brendan Gregge422f5e2016-07-01 18:38:30 -07005#
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
14from __future__ import print_function
15from bcc import BPF
Gary Linbb65bea2019-02-27 16:52:35 +080016from bcc.utils import printb
Brendan Gregge422f5e2016-07-01 18:38:30 -070017
Brendan Gregga4159da2016-07-11 18:27:01 -070018# load BPF program
19b = BPF(text="""
20TRACEPOINT_PROBE(random, urandom_read) {
21 // args is from /sys/kernel/debug/tracing/events/random/urandom_read/format
Brendan Gregge422f5e2016-07-01 18:38:30 -070022 bpf_trace_printk("%d\\n", args->got_bits);
23 return 0;
Liu Bo8268edb2018-02-09 11:58:40 -070024}
Brendan Gregga4159da2016-07-11 18:27:01 -070025""")
Brendan Gregge422f5e2016-07-01 18:38:30 -070026
27# header
28print("%-18s %-16s %-6s %s" % ("TIME(s)", "COMM", "PID", "GOTBITS"))
29
30# format output
31while 1:
32 try:
33 (task, pid, cpu, flags, ts, msg) = b.trace_fields()
34 except ValueError:
35 continue
Gary Lin593339d2019-02-27 16:54:44 +080036 except KeyboardInterrupt:
37 exit()
Gary Linbb65bea2019-02-27 16:52:35 +080038 printb(b"%-18.9f %-16s %-6d %s" % (ts, task, pid, msg))