3 # Babeltrace events per cpu example script
5 # Copyright 2012 EfficiOS Inc.
7 # Author: Danny Serres <danny.serres@efficios.com>
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:
16 # The above copyright notice and this permission notice shall be included in
17 # all copies or substantial portions of the Software.
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.
26 import sys
, multiprocessing
27 from output_format_modules
.pprint_table
import pprint_table
as pprint
28 from babeltrace
import *
31 raise TypeError("Usage: python events_per_cpu.py path/to/trace")
35 ret
= ctx
.add_trace(sys
.argv
[1], "ctf")
37 raise IOError("Error adding trace")
42 while i
< multiprocessing
.cpu_count():
47 bp
= IterPos(SEEK_BEGIN
)
48 ctf_it
= ctf
.Iterator(ctx
, bp
)
51 event
= ctf_it
.read_event()
52 start_time
= event
.get_timestamp()
54 while(event
is not None):
56 event_name
= event
.get_name()
57 ts
= event
.get_timestamp()
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()
64 print("ERROR: Missing cpu_id info for {}".format(event
.get_name()))
66 cpu_usage
[cpu_id
].append( (int(ts
), event_name
) )
73 event
= ctf_it
.read_event()
78 output
= open("events_per_cpu.txt", "wt")
79 output
.write("(timestamp, event)\n")
81 for cpu
in range(len(cpu_usage
)):
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] + ' %'])
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')
94 table
.insert(0, ["CPU ID", "EVENT COUNT", "TRACE EVENT %"])
96 print("Total event count: {}".format(nbEvents
))
97 print("Total trace time: {} ns".format(ts
- start_time
))
This page took 0.036059 seconds and 4 git commands to generate.