Added Python 3.0 or better requirement to the README file
[babeltrace.git] / bindings / python / examples / events_per_cpu.py
CommitLineData
24a3136a
DS
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
26import sys, multiprocessing
27from output_format_modules.pprint_table import pprint_table as pprint
28from babeltrace import *
29
30if len(sys.argv) < 2:
31 raise TypeError("Usage: python events_per_cpu.py path/to/trace")
32
33# Adding trace
34ctx = Context()
35ret = ctx.add_trace(sys.argv[1], "ctf")
36if ret is None:
37 raise IOError("Error adding trace")
38
39cpu_usage = []
40nbEvents = 0
41i = 0
42while i < multiprocessing.cpu_count():
43 cpu_usage.append([])
44 i += 1
45
46# Setting iterator
47bp = IterPos(SEEK_BEGIN)
48ctf_it = ctf.Iterator(ctx, bp)
49
50# Reading events
51event = ctf_it.read_event()
52start_time = event.get_timestamp()
53
54while(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
77table = []
78output = open("events_per_cpu.txt", "wt")
79output.write("(timestamp, event)\n")
80
81for 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
94table.insert(0, ["CPU ID", "EVENT COUNT", "TRACE EVENT %"])
95pprint(table)
96print("Total event count: {}".format(nbEvents))
97print("Total trace time: {} ns".format(ts - start_time))
98
99output.close()
This page took 0.02585 seconds and 4 git commands to generate.