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