Babeltrace python binding
[babeltrace.git] / bindings / python / examples / events_per_cpu.py
1 # events_per_cpu.py
2 #
3 # Babeltrace events per cpu 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 opens a trace and prints out CPU statistics
20 # for the given trace (event count per CPU, total active
21 # time and % of time processing events).
22 # It also outputs a .txt file showing each time interval
23 # (since the beginning of the trace) in which each CPU
24 # was active and the corresponding event.
25
26 import sys, multiprocessing
27 from output_format_modules.pprint_table import pprint_table as pprint
28 from babeltrace import *
29
30 if len(sys.argv) < 2:
31 raise TypeError("Usage: python events_per_cpu.py path/to/trace")
32
33 # Adding trace
34 ctx = Context()
35 ret = ctx.add_trace(sys.argv[1], "ctf")
36 if ret is None:
37 raise IOError("Error adding trace")
38
39 cpu_usage = []
40 nbEvents = 0
41 i = 0
42 while i < multiprocessing.cpu_count():
43 cpu_usage.append([])
44 i += 1
45
46 # Setting iterator
47 bp = IterPos(SEEK_BEGIN)
48 ctf_it = ctf.Iterator(ctx, bp)
49
50 # Reading events
51 event = ctf_it.read_event()
52 start_time = event.get_timestamp()
53
54 while(event is not None):
55
56 event_name = event.get_name()
57 ts = event.get_timestamp()
58
59 # Getting cpu_id
60 scope = event.get_top_level_scope(ctf.scope.STREAM_PACKET_CONTEXT)
61 field = event.get_field(scope, "cpu_id")
62 cpu_id = field.get_uint64()
63 if ctf.field_error():
64 print("ERROR: Missing cpu_id info for {}".format(event.get_name()))
65 else:
66 cpu_usage[cpu_id].append( (int(ts), event_name) )
67 nbEvents += 1
68
69 # Next Event
70 ret = ctf_it.next()
71 if ret < 0:
72 break
73 event = ctf_it.read_event()
74
75
76 # Outputting
77 table = []
78 output = open("events_per_cpu.txt", "wt")
79 output.write("(timestamp, event)\n")
80
81 for cpu in range(len(cpu_usage)):
82 # Setting table
83 event_str = str(100.0 * len(cpu_usage[cpu]) / nbEvents) + '000'
84 # % is printed with 2 decimals
85 table.append([cpu, len(cpu_usage[cpu]), event_str[0:event_str.find('.') + 3] + ' %'])
86
87 # Writing to file
88 output.write("\n\n\n----------------------\n")
89 output.write("CPU {}\n\n".format(cpu))
90 for event in cpu_usage[cpu]:
91 output.write(str(event) + '\n')
92
93 # Printing table
94 table.insert(0, ["CPU ID", "EVENT COUNT", "TRACE EVENT %"])
95 pprint(table)
96 print("Total event count: {}".format(nbEvents))
97 print("Total trace time: {} ns".format(ts - start_time))
98
99 output.close()
This page took 0.03012 seconds and 4 git commands to generate.