4 # Babeltrace events per cpu example script
6 # Copyright 2012 EfficiOS Inc.
8 # Author: Danny Serres <danny.serres@efficios.com>
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:
17 # The above copyright notice and this permission notice shall be included in
18 # all copies or substantial portions of the Software.
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.
27 import sys
, multiprocessing
28 from output_format_modules
.pprint_table
import pprint_table
as pprint
29 from babeltrace
import *
32 raise TypeError("Usage: python events_per_cpu.py path/to/trace")
36 ret
= ctx
.add_trace(sys
.argv
[1], "ctf")
38 raise IOError("Error adding trace")
43 while i
< multiprocessing
.cpu_count():
48 bp
= IterPos(SEEK_BEGIN
)
49 ctf_it
= ctf
.Iterator(ctx
, bp
)
52 event
= ctf_it
.read_event()
53 start_time
= event
.get_timestamp()
55 while(event
is not None):
57 event_name
= event
.get_name()
58 ts
= event
.get_timestamp()
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()
65 print("ERROR: Missing cpu_id info for {}".format(event
.get_name()))
67 cpu_usage
[cpu_id
].append( (int(ts
), event_name
) )
74 event
= ctf_it
.read_event()
79 output
= open("events_per_cpu.txt", "wt")
80 output
.write("(timestamp, event)\n")
82 for cpu
in range(len(cpu_usage
)):
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] + ' %'])
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')
95 table
.insert(0, ["CPU ID", "EVENT COUNT", "TRACE EVENT %"])
97 print("Total event count: {}".format(nbEvents
))
98 print("Total trace time: {} ns".format(ts
- start_time
))
This page took 0.030612 seconds and 4 git commands to generate.