3 # based on plot-timeline.py by Federico Mena-Quintero <federico at ximian dotcom>
5 # GST_DEBUG_NO_COLOR=1 GST_DEBUG="*:3" gst-launch-0.10 2>debug.log audiotestsrc num-buffers=10 ! audioconvert ! alsasink
6 # gst-plot-timeline.py debug.log --output=debug.png
16 FONT_NAME = "Bitstream Vera Sans"
18 PIXELS_PER_SECOND = 1700
22 SYSCALL_MARKER_WIDTH = 20
25 BACKGROUND_COLOR = (0, 0, 0)
27 # assumes GST_DEBUG_LOG_COLOR=1
28 mark_regex = re.compile (r'^(\d:\d\d:\d\d\.\d+) +\d+ 0?x?[0-9a-f]+ [A-Z]+ +([a-zA-Z_]+ )(.*)')
29 mark_timestamp_group = 1
30 mark_program_group = 2
37 def __init__(self, timestamp, log):
38 self.timestamp = timestamp
40 self.timestamp_ypos = 0
43 class AccessMark(BaseMark):
46 class LastMark(BaseMark):
49 class FirstMark(BaseMark):
52 class ExecMark(BaseMark):
53 # colors = 0.75, 0.33, 0.33
54 colors = (1.0, 0.0, 0.0)
55 def __init__(self, timestamp, log):
56 BaseMark.__init__(self, timestamp,
57 'execve: ' + os.path.basename(log))
64 # don't use black or red
79 def add_line (self, str):
80 m = mark_regex.search (str)
82 timestr = m.group (mark_timestamp_group)
83 timestamp = float (timestr[5:]) + (float (timestr[2:3]) * 60.0) + (float (timestr[0]) * 60.0*60.0)
84 program = m.group (mark_program_group)
85 text = program + m.group (mark_log_group)
87 self.syscalls.append (LastMark (timestamp, text))
89 self.syscalls.append (FirstMark (timestamp, text))
91 s = AccessMark (timestamp, text)
92 program_hash = program.__hash__ ()
93 s.colors = palette[program_hash % len (palette)]
94 self.syscalls.append (s)
96 print 'No log in %s' % str
99 def parse_strace(filename):
100 parser = SyscallParser ()
102 for line in file(filename, "r").readlines():
106 parser.add_line (line)
108 return parser.syscalls
110 def normalize_timestamps(syscalls):
112 first_timestamp = syscalls[0].timestamp
114 for syscall in syscalls:
115 syscall.timestamp -= first_timestamp
117 def compute_syscall_metrics(syscalls):
118 num_syscalls = len(syscalls)
121 metrics.width = PLOT_WIDTH
123 last_timestamp = syscalls[num_syscalls - 1].timestamp
124 num_seconds = int(math.ceil(last_timestamp))
125 metrics.height = max(num_seconds * PIXELS_PER_SECOND,
126 num_syscalls * PIXELS_PER_LINE)
130 for syscall in syscalls:
131 syscall.timestamp_ypos = syscall.timestamp * PIXELS_PER_SECOND
132 syscall.log_ypos = text_ypos + FONT_SIZE
134 text_ypos += PIXELS_PER_LINE
138 def plot_time_scale(surface, ctx, metrics):
139 num_seconds = (metrics.height + PIXELS_PER_SECOND - 1) / PIXELS_PER_SECOND
141 ctx.set_source_rgb(0.5, 0.5, 0.5)
142 ctx.set_line_width(1.0)
144 for i in range(num_seconds):
145 ypos = i * PIXELS_PER_SECOND
147 ctx.move_to(0, ypos + 0.5)
148 ctx.line_to(TIME_SCALE_WIDTH, ypos + 0.5)
151 ctx.move_to(0, ypos + 2 + FONT_SIZE)
152 ctx.show_text("%d s" % i)
154 def plot_syscall(surface, ctx, syscall):
155 ctx.set_source_rgb(*syscall.colors)
159 ctx.move_to(TIME_SCALE_WIDTH, syscall.timestamp_ypos)
160 ctx.line_to(TIME_SCALE_WIDTH + SYSCALL_MARKER_WIDTH, syscall.timestamp_ypos)
161 ctx.line_to(LOG_TEXT_XPOS - LOG_MARKER_WIDTH, syscall.log_ypos - FONT_SIZE / 2 + 0.5)
162 ctx.line_to(LOG_TEXT_XPOS, syscall.log_ypos - FONT_SIZE / 2 + 0.5)
167 ctx.move_to(LOG_TEXT_XPOS, syscall.log_ypos)
168 ctx.show_text("%8.5f: %s" % (syscall.timestamp, syscall.log))
170 def plot_syscalls_to_surface(syscalls, metrics):
171 num_syscalls = len(syscalls)
173 surface = cairo.ImageSurface(cairo.FORMAT_RGB24,
174 metrics.width, metrics.height)
176 ctx = cairo.Context(surface)
177 ctx.select_font_face(FONT_NAME)
178 ctx.set_font_size(FONT_SIZE)
182 ctx.set_source_rgb (*BACKGROUND_COLOR)
183 ctx.rectangle(0, 0, metrics.width, metrics.height)
188 plot_time_scale(surface, ctx, metrics)
192 ctx.set_line_width(1.0)
194 for syscall in syscalls:
195 plot_syscall(surface, ctx, syscall)
200 option_parser = optparse.OptionParser(
201 usage="usage: %prog -o output.png <debug.log>")
202 option_parser.add_option("-o",
203 "--output", dest="output",
205 help="Name of output file (output is a PNG file)")
207 options, args = option_parser.parse_args()
209 if not options.output:
210 print 'Please specify an output filename with "-o file.png" or "--output=file.png".'
214 print 'Please specify only one input filename, which is an debug log taken with "GST_DEBUG_NO_COLOR=1 GST_DEBUG=XXX <application>"'
217 in_filename = args[0]
218 out_filename = options.output
221 for syscall in parse_strace(in_filename):
222 syscalls.append(syscall)
223 if isinstance(syscall, FirstMark):
225 elif isinstance(syscall, LastMark):
229 print 'No logs in %s' % in_filename
232 normalize_timestamps(syscalls)
233 metrics = compute_syscall_metrics(syscalls)
235 surface = plot_syscalls_to_surface(syscalls, metrics)
236 surface.write_to_png(out_filename)
240 if __name__ == "__main__":
241 sys.exit(main(sys.argv))