| 1 | # histogram.py |
| 2 | # |
| 3 | # Babeltrace histogram 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 checks the number of events in the trace |
| 20 | # and outputs a table and a .svg histogram for the specified |
| 21 | # range (microseconds) or the total trace if no range specified. |
| 22 | # The graph is generated using the cairoplot module. |
| 23 | |
| 24 | import sys |
| 25 | from babeltrace import * |
| 26 | from output_format_modules import cairoplot |
| 27 | from output_format_modules.pprint_table import pprint_table as pprint |
| 28 | |
| 29 | # ------------------------------------------------ |
| 30 | # Output settings |
| 31 | |
| 32 | # number of intervals: |
| 33 | nbDiv = 25 # Should not be over 150 |
| 34 | # for usable graph output |
| 35 | |
| 36 | # table output stream (file-like object): |
| 37 | out = sys.stdout |
| 38 | # ------------------------------------------------- |
| 39 | |
| 40 | if len(sys.argv) < 2 or len(sys.argv) > 4: |
| 41 | raise TypeError("Usage: python histogram.py [ start_time [end_time] ] path/to/trace") |
| 42 | |
| 43 | ctx = Context() |
| 44 | ret = ctx.add_trace(sys.argv[len(sys.argv)-1], "ctf") |
| 45 | if ret is None: |
| 46 | raise IOError("Error adding trace") |
| 47 | |
| 48 | # Check when to start/stop graphing |
| 49 | sinceBegin = True |
| 50 | beginTime = 0.0 |
| 51 | if len(sys.argv) > 2: |
| 52 | sinceBegin = False |
| 53 | beginTime = float(sys.argv[1]) |
| 54 | untilEnd = True |
| 55 | if len(sys.argv) == 4: |
| 56 | untilEnd = False |
| 57 | |
| 58 | # Setting iterator |
| 59 | bp = IterPos(SEEK_BEGIN) |
| 60 | ctf_it = ctf.Iterator(ctx, bp) |
| 61 | |
| 62 | # Reading events |
| 63 | event = ctf_it.read_event() |
| 64 | start_time = event.get_timestamp() |
| 65 | time = 0 |
| 66 | count = {} |
| 67 | |
| 68 | while(event is not None): |
| 69 | # Microsec. |
| 70 | time = (event.get_timestamp() - start_time)/1000.0 |
| 71 | |
| 72 | # Check if in range |
| 73 | if not sinceBegin: |
| 74 | if time < beginTime: |
| 75 | # Next Event |
| 76 | ret = ctf_it.next() |
| 77 | if ret < 0: |
| 78 | break |
| 79 | event = ctf_it.read_event() |
| 80 | continue |
| 81 | if not untilEnd: |
| 82 | if time > float(sys.argv[2]): |
| 83 | break |
| 84 | |
| 85 | # Counting events per timestamp: |
| 86 | if time in count: |
| 87 | count[time] += 1 |
| 88 | else: |
| 89 | count[time] = 1 |
| 90 | |
| 91 | # Next Event |
| 92 | ret = ctf_it.next() |
| 93 | if ret < 0: |
| 94 | break |
| 95 | event = ctf_it.read_event() |
| 96 | |
| 97 | del ctf_it |
| 98 | |
| 99 | # Setting data for output |
| 100 | interval = (time - beginTime)/nbDiv |
| 101 | div_begin_time = beginTime |
| 102 | div_end_time = beginTime + interval |
| 103 | data = {} |
| 104 | |
| 105 | # Prefix for string sorting, considering |
| 106 | # there should not be over 150 intervals. |
| 107 | # This would work up to 9999 intervals. |
| 108 | # If needed, add zeros. |
| 109 | prefix = 0.0001 |
| 110 | |
| 111 | while div_end_time <= time: |
| 112 | key = str(prefix) + '[' + str(div_begin_time) + ';' + str(div_end_time) + '[' |
| 113 | for tmp in count: |
| 114 | if tmp >= div_begin_time and tmp < div_end_time: |
| 115 | if key in data: |
| 116 | data[key] += count[tmp] |
| 117 | else: |
| 118 | data[key] = count[tmp] |
| 119 | if not key in data: |
| 120 | data[key] = 0 |
| 121 | div_begin_time = div_end_time |
| 122 | div_end_time += interval |
| 123 | # Prefix increment |
| 124 | prefix += 0.001 |
| 125 | |
| 126 | table = [] |
| 127 | x_labels = [] |
| 128 | for key in sorted(data): |
| 129 | table.append([key[key.find('['):], data[key]]) |
| 130 | x_labels.append(key[key.find('['):]) |
| 131 | |
| 132 | # Table output |
| 133 | table.insert(0, ["INTERVAL (us)", "COUNT"]) |
| 134 | pprint(table, 1, out) |
| 135 | |
| 136 | # Graph output |
| 137 | cairoplot.vertical_bar_plot ( 'histogram.svg', data, 50 + 150*nbDiv, 50*nbDiv, |
| 138 | border = 20, display_values = True, grid = True, |
| 139 | x_labels = x_labels, rounded_corners = True ) |