| 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 | |
| 22 | import sys |
| 23 | from babeltrace import * |
| 24 | from output_format_modules import cairoplot |
| 25 | from output_format_modules.pprint_table import pprint_table as pprint |
| 26 | |
| 27 | # Check for path arg: |
| 28 | if len(sys.argv) < 2: |
| 29 | raise TypeError("Usage: python eventcountlist.py path/to/trace") |
| 30 | |
| 31 | ctx = Context() |
| 32 | ret = ctx.add_trace(sys.argv[1], "ctf") |
| 33 | if ret is None: |
| 34 | raise IOError("Error adding trace") |
| 35 | |
| 36 | # Events and their assossiated count |
| 37 | # will be stored as a dict: |
| 38 | events_count = {} |
| 39 | |
| 40 | # Setting iterator: |
| 41 | bp = IterPos(SEEK_BEGIN) |
| 42 | ctf_it = ctf.Iterator(ctx,bp) |
| 43 | |
| 44 | prev_event = None |
| 45 | event = ctf_it.read_event() |
| 46 | |
| 47 | start_time = event.get_timestamp() |
| 48 | |
| 49 | # Reading events: |
| 50 | while(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 | |
| 63 | if event: |
| 64 | total_time = event.get_timestamp() - start_time |
| 65 | else: |
| 66 | total_time = prev_event.get_timestamp() - start_time |
| 67 | |
| 68 | del ctf_it |
| 69 | |
| 70 | # Printing encountered events with respective count and rate: |
| 71 | print("Total time: {} ns".format(total_time)) |
| 72 | table = [["EVENT", "COUNT", "RATE (Hz)"]] |
| 73 | for item in sorted(events_count.iterkeys()): |
| 74 | tmp = [item, events_count[item], |
| 75 | events_count[item]/(total_time/1000000000.0)] |
| 76 | table.append(tmp) |
| 77 | pprint(table) |
| 78 | |
| 79 | # Exporting data as bar graph |
| 80 | cairoplot.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()) ) |