Added Python interpreter version directives and reorganized example scripts
[babeltrace.git] / bindings / python / examples / python2 / eventcount.py
CommitLineData
300b4c33 1#!/usr/bin/env python2
24a3136a 2# eventcount.py
300b4c33 3#
24a3136a 4# Babeltrace event count example script
300b4c33 5#
24a3136a 6# Copyright 2012 EfficiOS Inc.
300b4c33 7#
24a3136a 8# Author: Danny Serres <danny.serres@efficios.com>
300b4c33 9#
24a3136a
DS
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:
300b4c33 16#
24a3136a
DS
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
24import sys
25from babeltrace import *
26from output_format_modules.pprint_table import pprint_table as pprint
27
28if len(sys.argv) < 3:
29 raise TypeError("Usage: python eventcount.py event1 [event2 ...] path/to/trace")
30
31ctx = Context()
32ret = ctx.add_trace(sys.argv[len(sys.argv)-1], "ctf")
33if ret is None:
34 raise IOError("Error adding trace")
35
36counts = {}
37
38# Setting iterator
39bp = IterPos(SEEK_BEGIN)
40ctf_it = ctf.Iterator(ctx, bp)
41
42# Reading events
43event = ctf_it.read_event()
44while(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
77del ctf_it
78
79# Appending data to table for output
80table = []
81for item in counts:
82 table.append([item[0], item[1], counts[item]])
83table = sorted(table)
84table.insert(0,["TID", "EVENT", "COUNT"])
85pprint(table, 2)
This page took 0.026353 seconds and 4 git commands to generate.