From b79df7b22aa30f48590ef5797e6d66a7bb2cf069 Mon Sep 17 00:00:00 2001 From: Brendan Gregg Date: Sun, 24 Jul 2016 13:40:25 -0700 Subject: [PATCH] merge task_switch example --- README.md | 1 + examples/tracing/task_switch.c | 21 --------------------- examples/tracing/task_switch.py | 23 ++++++++++++++++++++++- 3 files changed, 23 insertions(+), 22 deletions(-) delete mode 100644 examples/tracing/task_switch.c diff --git a/README.md b/README.md index 543efb1..068df3c 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ Examples: - examples/tracing/[bitehist.py](examples/tracing/bitehist.py): Block I/O size histogram. [Examples](examples/tracing/bitehist_example.txt). - examples/tracing/[disksnoop.py](examples/tracing/disksnoop.py): Trace block device I/O latency. [Examples](examples/tracing/disksnoop_example.txt). - examples/[hello_world.py](examples/hello_world.py): Prints "Hello, World!" for new processes. +- examples/tracing/[task_switch.py](examples/tracing/task_switch.py): Count task switches with from and to PIDs. - examples/tracing/[tcpv4connect.py](examples/tracing/tcpv4connect.py): Trace TCP IPv4 active connections. [Examples](examples/tracing/tcpv4connect_example.txt). - examples/tracing/[trace_fields.py](examples/tracing/trace_fields.py): Simple example of printing fields from traced events. - examples/tracing/[urandomread.py](examples/tracing/urandomread.py): A kernel tracepoint example, which traces random:urandom_read. [Examples](examples/tracing/urandomread_example.txt). diff --git a/examples/tracing/task_switch.c b/examples/tracing/task_switch.c deleted file mode 100644 index 870e35f..0000000 --- a/examples/tracing/task_switch.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include - -struct key_t { - u32 prev_pid; - u32 curr_pid; -}; -// map_type, key_type, leaf_type, table_name, num_entry -BPF_TABLE("hash", struct key_t, u64, stats, 1024); -int count_sched(struct pt_regs *ctx, struct task_struct *prev) { - struct key_t key = {}; - u64 zero = 0, *val; - - key.curr_pid = bpf_get_current_pid_tgid(); - key.prev_pid = prev->pid; - - val = stats.lookup_or_init(&key, &zero); - (*val)++; - return 0; -} - diff --git a/examples/tracing/task_switch.py b/examples/tracing/task_switch.py index 43a4f3f..6117ee3 100755 --- a/examples/tracing/task_switch.py +++ b/examples/tracing/task_switch.py @@ -5,7 +5,28 @@ from bcc import BPF from time import sleep -b = BPF(src_file="task_switch.c") +b = BPF(text=""" +#include +#include + +struct key_t { + u32 prev_pid; + u32 curr_pid; +}; +// map_type, key_type, leaf_type, table_name, num_entry +BPF_TABLE("hash", struct key_t, u64, stats, 1024); +int count_sched(struct pt_regs *ctx, struct task_struct *prev) { + struct key_t key = {}; + u64 zero = 0, *val; + + key.curr_pid = bpf_get_current_pid_tgid(); + key.prev_pid = prev->pid; + + val = stats.lookup_or_init(&key, &zero); + (*val)++; + return 0; +} +""") b.attach_kprobe(event="finish_task_switch", fn_name="count_sched") # generate many schedule events -- 2.7.4