Added Python interpreter version directives and reorganized example scripts
[babeltrace.git] / bindings / python / examples / python2 / eventcount.py
1 #!/usr/bin/env python2
2 # eventcount.py
3 #
4 # Babeltrace event count 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 of specified events and
21 # their related tid's in a given trace.
22 # The trace needs TID context (lttng add-context -k -t tid)
23
24 import sys
25 from babeltrace import *
26 from output_format_modules.pprint_table import pprint_table as pprint
27
28 if len(sys.argv) < 3:
29 raise TypeError("Usage: python eventcount.py event1 [event2 ...] path/to/trace")
30
31 ctx = Context()
32 ret = ctx.add_trace(sys.argv[len(sys.argv)-1], "ctf")
33 if ret is None:
34 raise IOError("Error adding trace")
35
36 counts = {}
37
38 # Setting iterator
39 bp = IterPos(SEEK_BEGIN)
40 ctf_it = ctf.Iterator(ctx, bp)
41
42 # Reading events
43 event = ctf_it.read_event()
44 while(event is not None):
45 for event_type in sys.argv[1:len(sys.argv)-1]:
46 if event_type == event.get_name():
47
48 # Getting scope definition
49 sco = event.get_top_level_scope(ctf.scope.STREAM_EVENT_CONTEXT)
50 if sco is None:
51 print("ERROR: Cannot get definition scope for {}".format(
52 event.get_name()))
53 continue
54
55 # Getting TID
56 tid_field = event.get_field(sco, "_tid")
57 tid = tid_field.get_int64()
58
59 if ctf.field_error():
60 print("ERROR: Missing TID info for {}".format(
61 event.get_name()))
62 continue
63
64 tmp = (tid, event.get_name())
65
66 if tmp in counts:
67 counts[tmp] += 1
68 else:
69 counts[tmp] = 1
70
71 # Next event
72 ret = ctf_it.next()
73 if ret < 0:
74 break
75 event = ctf_it.read_event()
76
77 del ctf_it
78
79 # Appending data to table for output
80 table = []
81 for item in counts:
82 table.append([item[0], item[1], counts[item]])
83 table = sorted(table)
84 table.insert(0,["TID", "EVENT", "COUNT"])
85 pprint(table, 2)
This page took 0.030068 seconds and 4 git commands to generate.