Added Python interpreter version directives and reorganized example scripts
[babeltrace.git] / bindings / python / examples / python2 / histogram.py
1 #!/usr/bin/env python2
2 # histogram.py
3 #
4 # Babeltrace histogram example script
5 #
6 # Copyright 2012 EfficiOS Inc.
7 #
8 # Author: Danny Serres <danny.serres@efficios.com>
9 #
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:
16 #
17 # The above copyright notice and this permission notice shall be included in
18 # all copies or substantial portions of the Software.
19
20 # The script checks the number of events in the trace
21 # and outputs a table and a .svg histogram for the specified
22 # range (microseconds) or the total trace if no range specified.
23 # The graph is generated using the cairoplot module.
24
25 import sys
26 from babeltrace import *
27 from output_format_modules import cairoplot
28 from output_format_modules.pprint_table import pprint_table as pprint
29
30 # ------------------------------------------------
31 # Output settings
32
33 # number of intervals:
34 nbDiv = 25 # Should not be over 150
35 # for usable graph output
36
37 # table output stream (file-like object):
38 out = sys.stdout
39 # -------------------------------------------------
40
41 if len(sys.argv) < 2 or len(sys.argv) > 4:
42 raise TypeError("Usage: python histogram.py [ start_time [end_time] ] path/to/trace")
43
44 ctx = Context()
45 ret = ctx.add_trace(sys.argv[len(sys.argv)-1], "ctf")
46 if ret is None:
47 raise IOError("Error adding trace")
48
49 # Check when to start/stop graphing
50 sinceBegin = True
51 beginTime = 0.0
52 if len(sys.argv) > 2:
53 sinceBegin = False
54 beginTime = float(sys.argv[1])
55 untilEnd = True
56 if len(sys.argv) == 4:
57 untilEnd = False
58
59 # Setting iterator
60 bp = IterPos(SEEK_BEGIN)
61 ctf_it = ctf.Iterator(ctx, bp)
62
63 # Reading events
64 event = ctf_it.read_event()
65 start_time = event.get_timestamp()
66 time = 0
67 count = {}
68
69 while(event is not None):
70 # Microsec.
71 time = (event.get_timestamp() - start_time)/1000.0
72
73 # Check if in range
74 if not sinceBegin:
75 if time < beginTime:
76 # Next Event
77 ret = ctf_it.next()
78 if ret < 0:
79 break
80 event = ctf_it.read_event()
81 continue
82 if not untilEnd:
83 if time > float(sys.argv[2]):
84 break
85
86 # Counting events per timestamp:
87 if time in count:
88 count[time] += 1
89 else:
90 count[time] = 1
91
92 # Next Event
93 ret = ctf_it.next()
94 if ret < 0:
95 break
96 event = ctf_it.read_event()
97
98 del ctf_it
99
100 # Setting data for output
101 interval = (time - beginTime)/nbDiv
102 div_begin_time = beginTime
103 div_end_time = beginTime + interval
104 data = {}
105
106 # Prefix for string sorting, considering
107 # there should not be over 150 intervals.
108 # This would work up to 9999 intervals.
109 # If needed, add zeros.
110 prefix = 0.0001
111
112 while div_end_time <= time:
113 key = str(prefix) + '[' + str(div_begin_time) + ';' + str(div_end_time) + '['
114 for tmp in count:
115 if tmp >= div_begin_time and tmp < div_end_time:
116 if key in data:
117 data[key] += count[tmp]
118 else:
119 data[key] = count[tmp]
120 if not key in data:
121 data[key] = 0
122 div_begin_time = div_end_time
123 div_end_time += interval
124 # Prefix increment
125 prefix += 0.001
126
127 table = []
128 x_labels = []
129 for key in sorted(data):
130 table.append([key[key.find('['):], data[key]])
131 x_labels.append(key[key.find('['):])
132
133 # Table output
134 table.insert(0, ["INTERVAL (us)", "COUNT"])
135 pprint(table, 1, out)
136
137 # Graph output
138 cairoplot.vertical_bar_plot ( 'histogram.svg', data, 50 + 150*nbDiv, 50*nbDiv,
139 border = 20, display_values = True, grid = True,
140 x_labels = x_labels, rounded_corners = True )
This page took 0.032406 seconds and 4 git commands to generate.