diff options
author | Jiri Olsa <jolsa@kernel.org> | 2016-07-10 07:08:01 -0400 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2016-07-12 15:23:35 -0400 |
commit | 9881d7df9dddef24e34949a4510245e156746c21 (patch) | |
tree | 0b1d6a7f0ea0809d86cc3900f79351152846b9bd /tools/perf/python | |
parent | bae57e3825a3dded15f61cd20c6757d60ad6c712 (diff) |
perf python: Add tracepoint example
To show how to enable a tracepoint and access its fields.
Committer note:
Testing it:
# ls -l /tmp/build/perf/python/perf.so
-rwxrwxr-x. 1 acme acme 1563256 Jul 12 16:19 /tmp/build/perf/python/perf.so
# export PYTHONPATH=/tmp/build/perf/python/
# tools/perf/python/tracepoint.py 2> /dev/null | head -200 | tail -10
time 76345337296548 prev_comm=swapper/0 prev_pid=0 prev_prio=120 prev_state=0x0 ==> next_comm=tracepoint.py- next_pid=18479 next_prio=120
time 76345338520479 prev_comm=gnome-shelln-b prev_pid=2186 prev_prio=120 prev_state=0x1 ==> next_comm=swapper/1 next_pid=0 next_prio=120
time 76345337309942 prev_comm=tracepoint.py- prev_pid=18479 prev_prio=120 prev_state=0x1 ==> next_comm=swapper/0 next_pid=0 next_prio=120
time 76345337312302 prev_comm=swapper/0 prev_pid=0 prev_prio=120 prev_state=0x0 ==> next_comm=tracepoint.py- next_pid=18479 next_prio=120
time 76345337324927 prev_comm=tracepoint.py- prev_pid=18479 prev_prio=120 prev_state=0x1 ==> next_comm=swapper/0 next_pid=0 next_prio=120
time 76345337327115 prev_comm=swapper/0 prev_pid=0 prev_prio=120 prev_state=0x0 ==> next_comm=tracepoint.py- next_pid=18479 next_prio=120
time 76345338621750 prev_comm=swapper/2 prev_pid=0 prev_prio=120 prev_state=0x0 ==> next_comm=rcuos/2 next_pid=29 next_prio=120
time 76345338607922 prev_comm=swapper/3 prev_pid=0 prev_prio=120 prev_state=0x0 ==> next_comm=rcu_sched next_pid=7 next_prio=120
time 76345337338817 prev_comm=tracepoint.py- prev_pid=18479 prev_prio=120 prev_state=0x1 ==> next_comm=swapper/0 next_pid=0 next_prio=120
time 76345338627156 prev_comm=swapper/1 prev_pid=0 prev_prio=120 prev_state=0x0 ==> next_comm=head-terminal- next_pid=18480 next_prio=120
#
# strip /tmp/build/perf/python/perf.so
# ls -l /tmp/build/perf/python/perf.so
-rwxrwxr-x. 1 acme acme 319616 Jul 12 16:25 /tmp/build/perf/python/perf.so
Reported-and-Tested-by: Jiri Pirko <jiri@mellanox.com>
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Link: http://lkml.kernel.org/r/1468148882-10362-10-git-send-email-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf/python')
-rwxr-xr-x | tools/perf/python/tracepoint.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/tools/perf/python/tracepoint.py b/tools/perf/python/tracepoint.py new file mode 100755 index 000000000000..eb4dbed57de7 --- /dev/null +++ b/tools/perf/python/tracepoint.py | |||
@@ -0,0 +1,47 @@ | |||
1 | #! /usr/bin/python | ||
2 | # -*- python -*- | ||
3 | # -*- coding: utf-8 -*- | ||
4 | |||
5 | import perf | ||
6 | |||
7 | class tracepoint(perf.evsel): | ||
8 | def __init__(self, sys, name): | ||
9 | config = perf.tracepoint(sys, name) | ||
10 | perf.evsel.__init__(self, | ||
11 | type = perf.TYPE_TRACEPOINT, | ||
12 | config = config, | ||
13 | freq = 0, sample_period = 1, wakeup_events = 1, | ||
14 | sample_type = perf.SAMPLE_PERIOD | perf.SAMPLE_TID | perf.SAMPLE_CPU | perf.SAMPLE_RAW | perf.SAMPLE_TIME) | ||
15 | |||
16 | def main(): | ||
17 | tp = tracepoint("sched", "sched_switch") | ||
18 | cpus = perf.cpu_map() | ||
19 | threads = perf.thread_map(-1) | ||
20 | |||
21 | evlist = perf.evlist(cpus, threads) | ||
22 | evlist.add(tp) | ||
23 | evlist.open() | ||
24 | evlist.mmap() | ||
25 | |||
26 | while True: | ||
27 | evlist.poll(timeout = -1) | ||
28 | for cpu in cpus: | ||
29 | event = evlist.read_on_cpu(cpu) | ||
30 | if not event: | ||
31 | continue | ||
32 | |||
33 | if not isinstance(event, perf.sample_event): | ||
34 | continue | ||
35 | |||
36 | print "time %u prev_comm=%s prev_pid=%d prev_prio=%d prev_state=0x%x ==> next_comm=%s next_pid=%d next_prio=%d" % ( | ||
37 | event.sample_time, | ||
38 | event.prev_comm, | ||
39 | event.prev_pid, | ||
40 | event.prev_prio, | ||
41 | event.prev_state, | ||
42 | event.next_comm, | ||
43 | event.next_pid, | ||
44 | event.next_prio) | ||
45 | |||
46 | if __name__ == '__main__': | ||
47 | main() | ||