3 # Analyse a DCP-o-matic log file to extract various information.
9 import matplotlib.pyplot as plt
11 parser = argparse.ArgumentParser()
12 parser.add_argument('log_file')
13 parser.add_argument('-q', '--queue', help='plot queue size', action='store_true')
14 parser.add_argument('-e', '--encoder-threads', help='plot encoder thread activity', action='store_true')
15 parser.add_argument('-f', '--plot-first-encoder', help='plot more detailed activity of the first encoder thread', action='store_true')
16 parser.add_argument('-s', '--fps-stats', help='frames-per-second stats', action='store_true')
17 parser.add_argument('--encoder-stats', help='encoder thread activity stats', action='store_true')
18 parser.add_argument('--dump-first-encoder', help='dump activity of the first encoder thread', action='store_true')
19 parser.add_argument('--from', help='time in seconds to start at', type=int, dest='from_time')
20 parser.add_argument('--to', help='time in seconds to stop at', type=int, dest='to_time')
21 args = parser.parse_args()
23 def find_nth(haystack, needle, n):
24 start = haystack.find(needle)
25 while start >= 0 and n > 1:
26 start = haystack.find(needle, start+len(needle))
30 # Representation of time in seconds and microseconds
32 def __init__(self, s = 0, m = 0):
37 return '%d:%d' % (self.seconds, self.microseconds)
39 def float_seconds(self):
40 return self.seconds + self.microseconds / 1000000.0
42 def __iadd__(self, x):
43 self.microseconds += x.microseconds
44 self.seconds += x.seconds
45 if self.microseconds >= 1000000:
46 self.microseconds -= 1000000
51 m = self.microseconds - x.microseconds
53 return Time(self.seconds - x.seconds - 1, m + 1000000)
55 return Time(self.seconds - x.seconds, m)
58 def __init__(self, id):
63 def add_event(self, time, message, values):
64 self.events.append((time, message, values))
70 def find_encoder_thread(id):
71 global encoder_threads
73 for t in encoder_threads:
78 thread = EncoderThread(id)
79 encoder_threads.append(thread)
83 def add_general_event(time, event):
85 general_events.append((time, event))
87 f = open(args.log_file)
99 if len(p[0].split(':')) == 2:
100 # s:us timestamp: LOG_TIMING
102 T = Time(int(t[0]), int(t[1]))
106 for i in range(2, len(p)):
110 # Date/time timestamp: other LOG_*
111 s = find_nth(l, ':', 3)
113 T = Time(time.mktime(time.strptime(l[:s])))
115 T = Time(time.mktime(time.strptime(l[:s], "%d.%m.%Y %H:%M:%S")))
118 # T is elapsed time since the first log message
124 # Not-so-human-readable log messages (LOG_TIMING)
125 if message == 'add-frame-to-queue':
126 queue_size.append((T, values['queue']))
127 elif message in ['encoder-sleep', 'encoder-wake', 'start-local-encode', 'finish-local-encode', 'start-remote-send', 'start-remote-encode', 'start-remote-receive', 'finish-remote-receive']:
128 find_encoder_thread(values['thread']).add_event(T, message, values)
129 # Human-readable log message (other LOG_*)
130 elif message.startswith('Finished locally-encoded'):
131 add_general_event(T, 'end_local_encode')
132 elif message.startswith('Finished remotely-encoded'):
133 add_general_event(T, 'end_remote_encode')
134 elif message.startswith('Transcode job starting'):
135 add_general_event(T, 'begin_transcode')
136 elif message.startswith('Transcode job completed successfully'):
137 add_general_event(T, 'end_transcode')
140 # Plot queue size against time; queue_size contains times and queue sizes
145 x.append(q[0].seconds)
151 elif args.encoder_threads:
152 # Plot the things that are happening in each encoder thread with time
153 # y=0 thread is sleeping
154 # y=1 thread is awake
155 # y=2 thread is encoding
157 N = len(encoder_threads)
159 for thread in encoder_threads:
161 plt.ylim([-0.5, 2.5])
165 for e in thread.events:
166 if args.from_time is not None and e[0].float_seconds() <= args.from_time:
168 if args.to_time is not None and e[0].float_seconds() >= args.to_time:
170 x.append(e[0].float_seconds())
171 x.append(e[0].float_seconds())
173 if e[1] == 'encoder-sleep':
175 elif e[1] == 'encoder-wake':
177 elif e[1] == 'start-local-encode':
179 elif e[1] == 'finish-local-encode':
182 print>>sys.stderr,'unknown event %s' % e[1]
192 elif args.plot_first_encoder:
194 N = len(encoder_threads)
196 events = encoder_threads[0].events
200 for t in ['sleep', 'wake', 'begin_encode', 'end_encode']:
205 if args.from_time is not None and e[0].float_seconds() <= args.from_time:
207 if args.to_time is not None and e[0].float_seconds() >= args.to_time:
210 x.append(e[0].float_seconds())
211 x.append(e[0].float_seconds())
212 x.append(e[0].float_seconds())
223 elif args.dump_first_encoder:
224 events = encoder_thread_events.itervalues().next()
227 print e[0].float_seconds(), (e[0].float_seconds() - last), e[1]
228 last = e[0].float_seconds()
235 for e in general_events:
236 if e[1] == 'begin_transcode':
238 elif e[1] == 'end_transcode':
240 elif e[1] == 'end_local_encode':
242 elif e[1] == 'end_remote_encode':
246 print 'Job did not appear to end'
249 duration = end - start
251 print 'Job ran for %fs' % duration.float_seconds()
252 print '%d local and %d remote' % (local, remote)
253 print '%.2f fps local and %.2f fps remote' % (local / duration.float_seconds(), remote / duration.float_seconds())
255 elif args.encoder_stats:
256 # Broad stats on what encoder threads spent their time doing
257 for t in encoder_threads:
260 local_encoding = Time()
262 remote_encoding = Time()
267 if last[1] == 'encoder-sleep':
268 asleep += e[0] - last[0]
269 elif last[1] == 'encoder-wake':
271 elif last[1] == 'start-local-encode':
272 local_encoding += e[0] - last[0]
273 elif last[1] == 'start-remote-send':
274 sending += e[0] - last[0]
275 elif last[1] == 'start-remote-encode':
276 remote_encoding += e[0] - last[0]
277 elif last[1] == 'start-remote-receive':
278 receiving += e[0] - last[0]
279 elif last[1] == 'start-encoder-thread':
280 find_encoder_thread(last[2]['thread']).server = last[2]['server']
284 print '-- Encoder thread %s (%s)' % (t.server, t.id)
285 print '\tAwoken %d times' % wakes
287 total = asleep.float_seconds() + local_encoding.float_seconds() + sending.float_seconds() + remote_encoding.float_seconds() + receiving.float_seconds()
291 print '\t%s: %2.f%%' % ('Asleep'.ljust(16), asleep.float_seconds() * 100 / total)
293 def print_with_fps(v, name, total, frames):
294 if v.float_seconds() > 1:
295 print '\t%s: %2.f%% %.2ffps' % (name.ljust(16), v.float_seconds() * 100 / total, frames / v.float_seconds())
297 print_with_fps(local_encoding, 'Local encoding', total, wakes)
298 if sending.float_seconds() > 0:
299 print '\t%s: %2.f%%' % ('Sending'.ljust(16), sending.float_seconds() * 100 / total)
300 print_with_fps(remote_encoding, 'Remote encoding', total, wakes)
301 if receiving.float_seconds() > 0:
302 print '\t%s: %2.f%%' % ('Receiving'.ljust(16), receiving.float_seconds() * 100 / total)