Commit | Line | Data |
---|---|---|
24a3136a DS |
1 | # eventcount.py |
2 | # | |
3 | # Babeltrace event count example script | |
4 | # | |
5 | # Copyright 2012 EfficiOS Inc. | |
6 | # | |
7 | # Author: Danny Serres <danny.serres@efficios.com> | |
8 | # | |
9 | # Permission is hereby granted, free of charge, to any person obtaining a copy | |
10 | # of this software and associated documentation files (the "Software"), to deal | |
11 | # in the Software without restriction, including without limitation the rights | |
12 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
13 | # copies of the Software, and to permit persons to whom the Software is | |
14 | # furnished to do so, subject to the following conditions: | |
15 | # | |
16 | # The above copyright notice and this permission notice shall be included in | |
17 | # all copies or substantial portions of the Software. | |
18 | ||
19 | # The script prints a count of specified events and | |
20 | # their related tid's in a given trace. | |
21 | # The trace needs TID context (lttng add-context -k -t tid) | |
22 | ||
23 | import sys | |
24 | from babeltrace import * | |
25 | from output_format_modules.pprint_table import pprint_table as pprint | |
26 | ||
27 | if len(sys.argv) < 3: | |
28 | raise TypeError("Usage: python eventcount.py event1 [event2 ...] path/to/trace") | |
29 | ||
30 | ctx = Context() | |
31 | ret = ctx.add_trace(sys.argv[len(sys.argv)-1], "ctf") | |
32 | if ret is None: | |
33 | raise IOError("Error adding trace") | |
34 | ||
35 | counts = {} | |
36 | ||
37 | # Setting iterator | |
38 | bp = IterPos(SEEK_BEGIN) | |
39 | ctf_it = ctf.Iterator(ctx, bp) | |
40 | ||
41 | # Reading events | |
42 | event = ctf_it.read_event() | |
43 | while(event is not None): | |
44 | for event_type in sys.argv[1:len(sys.argv)-1]: | |
45 | if event_type == event.get_name(): | |
46 | ||
47 | # Getting scope definition | |
48 | sco = event.get_top_level_scope(ctf.scope.STREAM_EVENT_CONTEXT) | |
49 | if sco is None: | |
50 | print("ERROR: Cannot get definition scope for {}".format( | |
51 | event.get_name())) | |
52 | continue | |
53 | ||
54 | # Getting TID | |
55 | tid_field = event.get_field(sco, "_tid") | |
56 | tid = tid_field.get_int64() | |
57 | ||
58 | if ctf.field_error(): | |
59 | print("ERROR: Missing TID info for {}".format( | |
60 | event.get_name())) | |
61 | continue | |
62 | ||
63 | tmp = (tid, event.get_name()) | |
64 | ||
65 | if tmp in counts: | |
66 | counts[tmp] += 1 | |
67 | else: | |
68 | counts[tmp] = 1 | |
69 | ||
70 | # Next event | |
71 | ret = ctf_it.next() | |
72 | if ret < 0: | |
73 | break | |
74 | event = ctf_it.read_event() | |
75 | ||
76 | del ctf_it | |
77 | ||
78 | # Appending data to table for output | |
79 | table = [] | |
80 | for item in counts: | |
81 | table.append([item[0], item[1], counts[item]]) | |
82 | table = sorted(table) | |
83 | table.insert(0,["TID", "EVENT", "COUNT"]) | |
84 | pprint(table, 2) |