tools/gst-launch.1.in: Give example for network streaming (#351998)
[platform/upstream/gstreamer.git] / tools / gst-plot-timeline.py
1 #!/usr/bin/env python
2 #
3 # based on plot-timeline.py by Federico Mena-Quintero <federico at ximian dotcom>
4 # example:
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
7
8 import math
9 import optparse
10 import os
11 import re
12 import sys
13
14 import cairo
15
16 FONT_NAME = "Bitstream Vera Sans"
17 FONT_SIZE = 8
18 PIXELS_PER_SECOND = 1700
19 PIXELS_PER_LINE = 12
20 PLOT_WIDTH = 1400
21 TIME_SCALE_WIDTH = 20
22 SYSCALL_MARKER_WIDTH = 20
23 LOG_TEXT_XPOS = 400
24 LOG_MARKER_WIDTH = 20
25 BACKGROUND_COLOR = (0, 0, 0)
26
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
31 mark_log_group = 3
32
33 success_result = "0"
34
35 class BaseMark:
36     colors = 0, 0, 0
37     def __init__(self, timestamp, log):
38         self.timestamp = timestamp
39         self.log = log
40         self.timestamp_ypos = 0
41         self.log_ypos = 0
42
43 class AccessMark(BaseMark):
44     pass
45
46 class LastMark(BaseMark):
47     colors = 1.0, 0, 0
48
49 class FirstMark(BaseMark):
50     colors = 1.0, 0, 0
51
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))
58
59 class Metrics:
60     def __init__(self):
61         self.width = 0
62         self.height = 0
63
64 # don't use black or red
65 palette = [
66     (0.12, 0.29, 0.49),
67     (0.36, 0.51, 0.71),
68     (0.75, 0.31, 0.30),
69     (0.62, 0.73, 0.38),
70     (0.50, 0.40, 0.63),
71     (0.29, 0.67, 0.78),
72     (0.96, 0.62, 0.34)
73     ]
74
75 class SyscallParser:
76     def __init__ (self):
77         self.syscalls = []
78
79     def add_line (self, str):
80         m = mark_regex.search (str)
81         if m:
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)
86             if text == 'last':
87                 self.syscalls.append (LastMark (timestamp, text))
88             elif text == 'first':
89                 self.syscalls.append (FirstMark (timestamp, text))
90             else:
91                 s = AccessMark (timestamp, text)
92                 program_hash = program.__hash__ ()
93                 s.colors = palette[program_hash % len (palette)]
94                 self.syscalls.append (s)
95         else:
96             print 'No log in %s' % str
97             return
98
99 def parse_strace(filename):
100     parser = SyscallParser ()
101
102     for line in file(filename, "r").readlines():
103         if line == "":
104             break
105
106         parser.add_line (line)
107
108     return parser.syscalls
109
110 def normalize_timestamps(syscalls):
111
112     first_timestamp = syscalls[0].timestamp
113
114     for syscall in syscalls:
115         syscall.timestamp -= first_timestamp
116
117 def compute_syscall_metrics(syscalls):
118     num_syscalls = len(syscalls)
119
120     metrics = Metrics()
121     metrics.width = PLOT_WIDTH
122
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)
127
128     text_ypos = 0
129
130     for syscall in syscalls:
131         syscall.timestamp_ypos = syscall.timestamp * PIXELS_PER_SECOND
132         syscall.log_ypos = text_ypos + FONT_SIZE
133
134         text_ypos += PIXELS_PER_LINE
135
136     return metrics
137
138 def plot_time_scale(surface, ctx, metrics):
139     num_seconds = (metrics.height + PIXELS_PER_SECOND - 1) / PIXELS_PER_SECOND
140
141     ctx.set_source_rgb(0.5, 0.5, 0.5)
142     ctx.set_line_width(1.0)
143
144     for i in range(num_seconds):
145         ypos = i * PIXELS_PER_SECOND
146
147         ctx.move_to(0, ypos + 0.5)
148         ctx.line_to(TIME_SCALE_WIDTH, ypos + 0.5)
149         ctx.stroke()
150
151         ctx.move_to(0, ypos + 2 + FONT_SIZE)
152         ctx.show_text("%d s" % i)
153
154 def plot_syscall(surface, ctx, syscall):
155     ctx.set_source_rgb(*syscall.colors)
156
157     # Line
158
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)
163     ctx.stroke()
164
165     # Log text
166
167     ctx.move_to(LOG_TEXT_XPOS, syscall.log_ypos)
168     ctx.show_text("%8.5f: %s" % (syscall.timestamp, syscall.log))
169
170 def plot_syscalls_to_surface(syscalls, metrics):
171     num_syscalls = len(syscalls)
172
173     surface = cairo.ImageSurface(cairo.FORMAT_RGB24,
174                                  metrics.width, metrics.height)
175
176     ctx = cairo.Context(surface)
177     ctx.select_font_face(FONT_NAME)
178     ctx.set_font_size(FONT_SIZE)
179
180     # Background
181
182     ctx.set_source_rgb (*BACKGROUND_COLOR)
183     ctx.rectangle(0, 0, metrics.width, metrics.height)
184     ctx.fill()
185
186     # Time scale
187
188     plot_time_scale(surface, ctx, metrics)
189
190     # Contents
191
192     ctx.set_line_width(1.0)
193
194     for syscall in syscalls:
195         plot_syscall(surface, ctx, syscall)
196
197     return surface
198
199 def main(args):
200     option_parser = optparse.OptionParser(
201         usage="usage: %prog -o output.png <debug.log>")
202     option_parser.add_option("-o",
203                              "--output", dest="output",
204                              metavar="FILE",
205                              help="Name of output file (output is a PNG file)")
206
207     options, args = option_parser.parse_args()
208
209     if not options.output:
210         print 'Please specify an output filename with "-o file.png" or "--output=file.png".'
211         return 1
212
213     if len(args) != 1:
214         print 'Please specify only one input filename, which is an debug log taken with "GST_DEBUG_NO_COLOR=1 GST_DEBUG=XXX <application>"'
215         return 1
216
217     in_filename = args[0]
218     out_filename = options.output
219
220     syscalls = []
221     for syscall in parse_strace(in_filename):
222         syscalls.append(syscall)
223         if isinstance(syscall, FirstMark):
224             syscalls = []
225         elif isinstance(syscall, LastMark):
226             break
227
228     if not syscalls:
229         print 'No logs in %s' % in_filename
230         return 1
231
232     normalize_timestamps(syscalls)
233     metrics = compute_syscall_metrics(syscalls)
234
235     surface = plot_syscalls_to_surface(syscalls, metrics)
236     surface.write_to_png(out_filename)
237
238     return 0
239
240 if __name__ == "__main__":
241     sys.exit(main(sys.argv))