Added Python 3.0 or better requirement to the README file
[babeltrace.git] / bindings / python / examples / histogram.py
CommitLineData
24a3136a
DS
1# histogram.py
2#
3# Babeltrace histogram 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 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.
23
24import sys
25from babeltrace import *
26from output_format_modules import cairoplot
27from output_format_modules.pprint_table import pprint_table as pprint
28
29# ------------------------------------------------
30# Output settings
31
32# number of intervals:
33nbDiv = 25 # Should not be over 150
34 # for usable graph output
35
36# table output stream (file-like object):
37out = sys.stdout
38# -------------------------------------------------
39
40if len(sys.argv) < 2 or len(sys.argv) > 4:
41 raise TypeError("Usage: python histogram.py [ start_time [end_time] ] path/to/trace")
42
43ctx = Context()
44ret = ctx.add_trace(sys.argv[len(sys.argv)-1], "ctf")
45if ret is None:
46 raise IOError("Error adding trace")
47
48# Check when to start/stop graphing
49sinceBegin = True
50beginTime = 0.0
51if len(sys.argv) > 2:
52 sinceBegin = False
53 beginTime = float(sys.argv[1])
54untilEnd = True
55if len(sys.argv) == 4:
56 untilEnd = False
57
58# Setting iterator
59bp = IterPos(SEEK_BEGIN)
60ctf_it = ctf.Iterator(ctx, bp)
61
62# Reading events
63event = ctf_it.read_event()
64start_time = event.get_timestamp()
65time = 0
66count = {}
67
68while(event is not None):
69 # Microsec.
70 time = (event.get_timestamp() - start_time)/1000.0
71
72 # Check if in range
73 if not sinceBegin:
74 if time < beginTime:
75 # Next Event
76 ret = ctf_it.next()
77 if ret < 0:
78 break
79 event = ctf_it.read_event()
80 continue
81 if not untilEnd:
82 if time > float(sys.argv[2]):
83 break
84
85 # Counting events per timestamp:
86 if time in count:
87 count[time] += 1
88 else:
89 count[time] = 1
90
91 # Next Event
92 ret = ctf_it.next()
93 if ret < 0:
94 break
95 event = ctf_it.read_event()
96
97del ctf_it
98
99# Setting data for output
100interval = (time - beginTime)/nbDiv
101div_begin_time = beginTime
102div_end_time = beginTime + interval
103data = {}
104
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.
109prefix = 0.0001
110
111while div_end_time <= time:
112 key = str(prefix) + '[' + str(div_begin_time) + ';' + str(div_end_time) + '['
113 for tmp in count:
114 if tmp >= div_begin_time and tmp < div_end_time:
115 if key in data:
116 data[key] += count[tmp]
117 else:
118 data[key] = count[tmp]
119 if not key in data:
120 data[key] = 0
121 div_begin_time = div_end_time
122 div_end_time += interval
123 # Prefix increment
124 prefix += 0.001
125
126table = []
127x_labels = []
128for key in sorted(data):
129 table.append([key[key.find('['):], data[key]])
130 x_labels.append(key[key.find('['):])
131
132# Table output
133table.insert(0, ["INTERVAL (us)", "COUNT"])
134pprint(table, 1, out)
135
136# Graph output
137cairoplot.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.027107 seconds and 4 git commands to generate.