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)
60 encoder_thread_events = dict()
62 def add_encoder_thread_event(thread, time, event):
63 global encoder_threads
64 global encoder_thread_events
65 if not thread in encoder_threads:
66 encoder_threads.append(thread)
67 encoder_thread_events[thread] = [(time, event)]
69 encoder_thread_events[thread].append((time, event))
71 def add_general_event(time, event):
73 general_events.append((time, event))
75 f = open(args.log_file)
87 if len(p[0].split(':')) == 2:
88 # s:us timestamp: LOG_TIMING
90 T = Time(int(t[0]), int(t[1]))
94 for i in range(2, len(p)):
98 # Date/time timestamp: other LOG_*
99 s = find_nth(l, ':', 3)
100 T = Time(time.mktime(time.strptime(l[:s])))
103 # T is elapsed time since the first log message
109 # Not-so-human-readable log messages (LOG_TIMING)
110 if message == 'add-frame-to-queue':
111 queue_size.append((T, values['queue']))
112 elif message in ['encoder-sleep', 'encoder-wake', 'start-local-encode', 'finish-local-encode', 'start-remote-send', 'finish-remote-send', 'start-remote-encode-and-receive', 'finish-remote-encode-and-receive']:
113 add_encoder_thread_event(values['thread'], T, message)
114 # Human-readable log message (other LOG_*)
115 elif message.startswith('Finished locally-encoded'):
116 add_general_event(T, 'end_local_encode')
117 elif message.startswith('Finished remotely-encoded'):
118 add_general_event(T, 'end_remote_encode')
119 elif message.startswith('Transcode job starting'):
120 add_general_event(T, 'begin_transcode')
121 elif message.startswith('Transcode job completed successfully'):
122 add_general_event(T, 'end_transcode')
125 # Plot queue size against time; queue_size contains times and queue sizes
130 x.append(q[0].seconds)
136 elif args.encoder_threads:
137 # Plot the things that are happening in each encoder thread with time
138 # y=0 thread is sleeping
139 # y=1 thread is awake
140 # y=2 thread is encoding
142 N = len(encoder_thread_events)
144 for thread, events in encoder_thread_events.iteritems():
150 if args.from_time is not None and e[0].float_seconds() <= args.from_time:
152 if args.to_time is not None and e[0].float_seconds() >= args.to_time:
154 x.append(e[0].float_seconds())
155 x.append(e[0].float_seconds())
161 elif e[1] == 'begin_encode':
163 elif e[1] == 'end_encode':
173 elif args.plot_first_encoder:
175 N = len(encoder_thread_events)
177 events = encoder_thread_events.itervalues().next()
181 for t in ['sleep', 'wake', 'begin_encode', 'end_encode']:
186 if args.from_time is not None and e[0].float_seconds() <= args.from_time:
188 if args.to_time is not None and e[0].float_seconds() >= args.to_time:
191 x.append(e[0].float_seconds())
192 x.append(e[0].float_seconds())
193 x.append(e[0].float_seconds())
204 elif args.dump_first_encoder:
205 events = encoder_thread_events.itervalues().next()
208 print e[0].float_seconds(), (e[0].float_seconds() - last), e[1]
209 last = e[0].float_seconds()
216 for e in general_events:
217 if e[1] == 'begin_transcode':
219 elif e[1] == 'end_transcode':
221 elif e[1] == 'end_local_encode':
223 elif e[1] == 'end_remote_encode':
227 print 'Job did not appear to end'
230 duration = end - start
232 print 'Job ran for %fs' % duration.float_seconds()
233 print '%d local and %d remote' % (local, remote)
234 print '%.2f fps local and %.2f fps remote' % (local / duration.float_seconds(), remote / duration.float_seconds())
236 elif args.encoder_stats:
238 for t in encoder_threads:
243 for e in encoder_thread_events[t]:
244 if e[1] == 'encoder-sleep':
246 encoding += e[0] - last
248 elif e[1] == 'encoder-wake':
250 asleep += e[0] - last
253 print '-- Encoder thread %s' % t
254 print 'Awoken %d times' % wakes
255 total = asleep.float_seconds() + encoding.float_seconds()
256 print 'Asleep: %s (%s%%)' % (asleep, asleep.float_seconds() * 100 / total)
257 print 'Encoding: %s (%s%%)' % (encoding, encoding.float_seconds() * 100 / total)