Add a python bindings sequence test
[babeltrace.git] / bindings / python / examples / python2 / eventcountlist.py
1 #!/usr/bin/env python2
2 # eventcountlist.py
3 #
4 # Babeltrace event count list 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 prints a count and rate of events.
21 # It also outputs a bar graph of count per event, using the cairoplot module.
22
23 import sys
24 from babeltrace import *
25 from output_format_modules import cairoplot
26 from output_format_modules.pprint_table import pprint_table as pprint
27
28 # Check for path arg:
29 if len(sys.argv) < 2:
30 raise TypeError("Usage: python eventcountlist.py path/to/trace")
31
32 ctx = Context()
33 ret = ctx.add_trace(sys.argv[1], "ctf")
34 if ret is None:
35 raise IOError("Error adding trace")
36
37 # Events and their assossiated count
38 # will be stored as a dict:
39 events_count = {}
40
41 # Setting iterator:
42 bp = IterPos(SEEK_BEGIN)
43 ctf_it = ctf.Iterator(ctx,bp)
44
45 prev_event = None
46 event = ctf_it.read_event()
47
48 start_time = event.get_timestamp()
49
50 # Reading events:
51 while(event is not None):
52 if event.get_name() in events_count:
53 events_count[event.get_name()] += 1
54 else:
55 events_count[event.get_name()] = 1
56
57 ret = ctf_it.next()
58 if ret < 0:
59 break
60 else:
61 prev_event = event
62 event = ctf_it.read_event()
63
64 if event:
65 total_time = event.get_timestamp() - start_time
66 else:
67 total_time = prev_event.get_timestamp() - start_time
68
69 del ctf_it
70
71 # Printing encountered events with respective count and rate:
72 print("Total time: {} ns".format(total_time))
73 table = [["EVENT", "COUNT", "RATE (Hz)"]]
74 for item in sorted(events_count.iterkeys()):
75 tmp = [item, events_count[item],
76 events_count[item]/(total_time/1000000000.0)]
77 table.append(tmp)
78 pprint(table)
79
80 # Exporting data as bar graph
81 cairoplot.vertical_bar_plot ( 'eventcountlist.svg', events_count, 50+85*len(events_count),
82 800, border = 20, display_values = True, grid = True,
83 rounded_corners = True,
84 x_labels = sorted(events_count.keys()) )
This page took 0.030323 seconds and 4 git commands to generate.