3 # Babeltrace histogram 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 checks the number of events in the trace
20 # and outputs a table and a .svg histogram for the specified
21 # range (microseconds) or the total trace if no range specified.
22 # The graph is generated using the cairoplot module.
25 from babeltrace
import *
26 from output_format_modules
import cairoplot
27 from output_format_modules
.pprint_table
import pprint_table
as pprint
29 # ------------------------------------------------
32 # number of intervals:
33 nbDiv
= 25 # Should not be over 150
34 # for usable graph output
36 # table output stream (file-like object):
38 # -------------------------------------------------
40 if len(sys
.argv
) < 2 or len(sys
.argv
) > 4:
41 raise TypeError("Usage: python histogram.py [ start_time [end_time] ] path/to/trace")
44 ret
= ctx
.add_trace(sys
.argv
[len(sys
.argv
)-1], "ctf")
46 raise IOError("Error adding trace")
48 # Check when to start/stop graphing
53 beginTime
= float(sys
.argv
[1])
55 if len(sys
.argv
) == 4:
59 bp
= IterPos(SEEK_BEGIN
)
60 ctf_it
= ctf
.Iterator(ctx
, bp
)
63 event
= ctf_it
.read_event()
64 start_time
= event
.get_timestamp()
68 while(event
is not None):
70 time
= (event
.get_timestamp() - start_time
)/1000.0
79 event
= ctf_it
.read_event()
82 if time
> float(sys
.argv
[2]):
85 # Counting events per timestamp:
95 event
= ctf_it
.read_event()
99 # Setting data for output
100 interval
= (time
- beginTime
)/nbDiv
101 div_begin_time
= beginTime
102 div_end_time
= beginTime
+ interval
105 # Prefix for string sorting, considering
106 # there should not be over 150 intervals.
107 # This would work up to 9999 intervals.
108 # If needed, add zeros.
111 while div_end_time
<= time
:
112 key
= str(prefix
) + '[' + str(div_begin_time
) + ';' + str(div_end_time
) + '['
114 if tmp
>= div_begin_time
and tmp
< div_end_time
:
116 data
[key
] += count
[tmp
]
118 data
[key
] = count
[tmp
]
121 div_begin_time
= div_end_time
122 div_end_time
+= interval
128 for key
in sorted(data
):
129 table
.append([key
[key
.find('['):], data
[key
]])
130 x_labels
.append(key
[key
.find('['):])
133 table
.insert(0, ["INTERVAL (us)", "COUNT"])
134 pprint(table
, 1, out
)
137 cairoplot
.vertical_bar_plot ( 'histogram.svg', data
, 50 + 150*nbDiv
, 50*nbDiv
,
138 border
= 20, display_values
= True, grid
= True,
139 x_labels
= x_labels
, rounded_corners
= True )
This page took 0.030941 seconds and 4 git commands to generate.