Commit | Line | Data |
---|---|---|
300b4c33 | 1 | #!/usr/bin/env python2 |
24a3136a | 2 | # events_per_cpu.py |
300b4c33 | 3 | # |
24a3136a | 4 | # Babeltrace events per cpu example script |
300b4c33 | 5 | # |
24a3136a | 6 | # Copyright 2012 EfficiOS Inc. |
300b4c33 | 7 | # |
24a3136a | 8 | # Author: Danny Serres <danny.serres@efficios.com> |
300b4c33 | 9 | # |
24a3136a DS |
10 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
11 | # of this software and associated documentation files (the "Software"), to deal | |
12 | # in the Software without restriction, including without limitation the rights | |
13 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | # copies of the Software, and to permit persons to whom the Software is | |
15 | # furnished to do so, subject to the following conditions: | |
300b4c33 | 16 | # |
24a3136a DS |
17 | # The above copyright notice and this permission notice shall be included in |
18 | # all copies or substantial portions of the Software. | |
19 | ||
20 | # The script opens a trace and prints out CPU statistics | |
21 | # for the given trace (event count per CPU, total active | |
22 | # time and % of time processing events). | |
23 | # It also outputs a .txt file showing each time interval | |
24 | # (since the beginning of the trace) in which each CPU | |
25 | # was active and the corresponding event. | |
26 | ||
27 | import sys, multiprocessing | |
28 | from output_format_modules.pprint_table import pprint_table as pprint | |
29 | from babeltrace import * | |
30 | ||
31 | if len(sys.argv) < 2: | |
32 | raise TypeError("Usage: python events_per_cpu.py path/to/trace") | |
33 | ||
34 | # Adding trace | |
35 | ctx = Context() | |
36 | ret = ctx.add_trace(sys.argv[1], "ctf") | |
37 | if ret is None: | |
38 | raise IOError("Error adding trace") | |
39 | ||
40 | cpu_usage = [] | |
41 | nbEvents = 0 | |
42 | i = 0 | |
43 | while i < multiprocessing.cpu_count(): | |
44 | cpu_usage.append([]) | |
45 | i += 1 | |
46 | ||
47 | # Setting iterator | |
48 | bp = IterPos(SEEK_BEGIN) | |
49 | ctf_it = ctf.Iterator(ctx, bp) | |
50 | ||
51 | # Reading events | |
52 | event = ctf_it.read_event() | |
53 | start_time = event.get_timestamp() | |
54 | ||
55 | while(event is not None): | |
56 | ||
57 | event_name = event.get_name() | |
58 | ts = event.get_timestamp() | |
59 | ||
60 | # Getting cpu_id | |
61 | scope = event.get_top_level_scope(ctf.scope.STREAM_PACKET_CONTEXT) | |
62 | field = event.get_field(scope, "cpu_id") | |
63 | cpu_id = field.get_uint64() | |
64 | if ctf.field_error(): | |
65 | print("ERROR: Missing cpu_id info for {}".format(event.get_name())) | |
66 | else: | |
67 | cpu_usage[cpu_id].append( (int(ts), event_name) ) | |
68 | nbEvents += 1 | |
69 | ||
70 | # Next Event | |
71 | ret = ctf_it.next() | |
72 | if ret < 0: | |
73 | break | |
74 | event = ctf_it.read_event() | |
75 | ||
76 | ||
77 | # Outputting | |
78 | table = [] | |
79 | output = open("events_per_cpu.txt", "wt") | |
80 | output.write("(timestamp, event)\n") | |
81 | ||
82 | for cpu in range(len(cpu_usage)): | |
83 | # Setting table | |
84 | event_str = str(100.0 * len(cpu_usage[cpu]) / nbEvents) + '000' | |
85 | # % is printed with 2 decimals | |
86 | table.append([cpu, len(cpu_usage[cpu]), event_str[0:event_str.find('.') + 3] + ' %']) | |
87 | ||
88 | # Writing to file | |
89 | output.write("\n\n\n----------------------\n") | |
90 | output.write("CPU {}\n\n".format(cpu)) | |
91 | for event in cpu_usage[cpu]: | |
92 | output.write(str(event) + '\n') | |
93 | ||
94 | # Printing table | |
95 | table.insert(0, ["CPU ID", "EVENT COUNT", "TRACE EVENT %"]) | |
96 | pprint(table) | |
97 | print("Total event count: {}".format(nbEvents)) | |
98 | print("Total trace time: {} ns".format(ts - start_time)) | |
99 | ||
100 | output.close() |