Skip to content

Commit

Permalink
Update trace perf output with struct deserialize example
Browse files Browse the repository at this point in the history
Signed-off-by: Brenden Blanco <[email protected]>
  • Loading branch information
Brenden Blanco committed Nov 6, 2015
1 parent 1d75282 commit 98d0beb
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions examples/tracing/trace_perf_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,28 @@

import atexit
from bcc import BPF
import ctypes
import ctypes as ct

class Data(ct.Structure):
_fields_ = [("ts", ct.c_ulonglong),
("magic", ct.c_ulonglong)]

counter = 0
def cb(cpu, data, size):
assert size >= ct.sizeof(Data)
event = ct.cast(data, ct.POINTER(Data)).contents
print("[%0d] %f: %x" % (cpu, float(event.ts) / 1000000, event.magic))
global counter
counter += 1

prog = """
BPF_PERF_OUTPUT(events);
BPF_TABLE("array", int, u64, counters, 10);
int kprobe__sys_write(void *ctx) {
int kprobe__sys_clone(void *ctx) {
struct {
u64 ts;
} data = {bpf_ktime_get_ns()};
u64 magic;
} data = {bpf_ktime_get_ns(), 0x12345678};
int rc;
if ((rc = events.perf_submit(ctx, &data, sizeof(data))) < 0)
bpf_trace_printk("perf_output failed: %d\\n", rc);
Expand All @@ -38,7 +46,7 @@ def cb(cpu, data, size):
def print_counter():
global counter
global b
print("counter = %d vs %d" % (counter, b["counters"][ctypes.c_int(0)].value))
print("counter = %d vs %d" % (counter, b["counters"][ct.c_int(0)].value))

print("Tracing sys_write, try `dd if=/dev/zero of=/dev/null`")
print("Tracing... Hit Ctrl-C to end.")
Expand Down

0 comments on commit 98d0beb

Please sign in to comment.