libvips/tools/vipsprofile.py

203 lines
5.3 KiB
Python
Raw Normal View History

2013-11-19 11:13:38 +01:00
#!/usr/bin/python
import re
2013-11-21 15:58:44 +01:00
import math
import cairo
2013-11-19 11:13:38 +01:00
class ReadFile:
def __init__(self, filename):
self.filename = filename
def __enter__(self):
self.f = open(self.filename, 'r')
self.source = iter(self.f.readline, '')
self.lineno = 0
self.getnext();
return self
def __exit__(self, type, value, traceback):
self.f.close()
return isinstance(value, StopIteration)
def __nonzero__(self):
return self.line != ""
def getnext(self):
self.lineno += 1
self.line = self.source.next()
def read_times(rf):
times = []
while True:
match = re.match('[0-9]+ ', rf.line)
if not match:
break
2013-11-20 22:35:18 +01:00
times += [int(x) for x in re.split(' ', rf.line.rstrip())]
2013-11-19 11:13:38 +01:00
rf.getnext()
return times[::-1]
2013-11-25 11:36:37 +01:00
class Thread:
thread_number = 0
def __init__(self, thread_name):
2013-11-19 15:59:40 +01:00
self.thread_name = thread_name
2013-11-25 11:36:37 +01:00
self.thread_number = Thread.thread_number
self.events = []
Thread.thread_number += 1
class Event:
def __init__(self, thread, gate_name, start, stop):
self.thread = thread
2013-11-19 15:59:40 +01:00
self.gate_name = gate_name
self.start = start
2013-11-20 22:35:18 +01:00
self.stop = stop
2013-11-19 15:59:40 +01:00
2013-11-25 11:36:37 +01:00
self.work = False
self.wait = False
if re.match('.*: .*work.*', gate_name):
2013-11-19 15:59:40 +01:00
self.work = True
2013-11-25 11:36:37 +01:00
if re.match('.*: .*wait.*', gate_name):
2013-11-19 15:59:40 +01:00
self.wait = True
2013-11-25 11:36:37 +01:00
thread.events.append(self)
2013-11-21 15:58:44 +01:00
thread_id = 0
2013-11-25 11:36:37 +01:00
threads = []
n_events = 0
2013-11-19 11:13:38 +01:00
with ReadFile('vips-profile.txt') as rf:
while rf:
2013-11-21 15:58:44 +01:00
match = re.match('thread: (.*)', rf.line)
2013-11-19 11:13:38 +01:00
if not match:
print 'parse error line %d, expected "thread"' % rf.lineno
2013-11-21 15:58:44 +01:00
thread_name = match.group(1) + " " + str(thread_id)
thread_id += 1
2013-11-25 11:36:37 +01:00
thread = Thread(thread_name)
threads.append(thread)
2013-11-19 11:13:38 +01:00
rf.getnext()
while True:
match = re.match('gate: (.*)', rf.line)
if not match:
break
gate_name = match.group(1)
rf.getnext()
match = re.match('start:', rf.line)
if not match:
continue
rf.getnext()
2013-11-19 15:59:40 +01:00
start = read_times(rf)
2013-11-19 11:13:38 +01:00
match = re.match('stop:', rf.line)
if not match:
continue
rf.getnext()
2013-11-19 15:59:40 +01:00
stop = read_times(rf)
if len(start) != len(stop):
print 'start and stop length mismatch'
2013-11-20 22:35:18 +01:00
for a, b in zip(start, stop):
2013-11-25 11:36:37 +01:00
Event(thread, gate_name, a, b)
n_events += 1
2013-11-20 22:35:18 +01:00
2013-11-25 11:36:37 +01:00
for thread in threads:
thread.events.sort(lambda x, y: cmp(x.start, y.start))
2013-11-20 22:35:18 +01:00
2013-11-25 11:36:37 +01:00
print 'loaded %d events' % n_events
2013-11-21 15:58:44 +01:00
# normalise time axis to secs of computation
ticks_per_sec = 1000000.0
2013-11-25 11:36:37 +01:00
start_time = threads[0].events[0].start
last_time = 0
for thread in threads:
for event in thread.events:
event.start = (event.start - start_time) / ticks_per_sec
event.stop = (event.stop - start_time) / ticks_per_sec
2013-11-21 15:58:44 +01:00
2013-11-25 11:36:37 +01:00
if event.stop > last_time:
last_time = event.stop
2013-11-21 15:58:44 +01:00
2013-11-25 11:36:37 +01:00
print 'last time =', last_time
2013-11-21 15:58:44 +01:00
2013-11-25 11:36:37 +01:00
# do two gates overlap?
def is_overlap(events, gate_name1, gate_name2):
for event1 in events:
if event1.gate_name != gate_name1:
continue
2013-11-21 15:58:44 +01:00
2013-11-25 11:36:37 +01:00
for event2 in events:
if event2.gate_name != gate_name2:
continue
# if either endpoint of 1 is within 2
if event1.start > event2.start and event1.stop < event2.stop:
return True
if event1.stop > event2.start and event1.stop < event2.stop:
return True
return False
# allocate a y position for each gate
total_y = 0
for thread in threads:
y = 1
gate_positions = {}
for event in thread.events:
if event.work or event.wait:
gate_positions[event.gate_name] = 0
elif not event.gate_name in gate_positions:
overlap = True
for gate_name in gate_positions:
if not is_overlap(thread.events, gate_name, event.gate_name):
gate_positions[event.gate_name] = gate_positions[gate_name]
overlap = False
break
if overlap:
gate_positions[event.gate_name] = y
y += 1
event.y = gate_positions[event.gate_name]
event.total_y = total_y + y
total_y += y
PIXELS_PER_SECOND = 1000
PIXELS_PER_GATE = 20
2013-11-25 12:01:09 +01:00
WIDTH = int(last_time * PIXELS_PER_SECOND) + 100
HEIGHT = int(total_y * PIXELS_PER_GATE) + 100
2013-11-21 15:58:44 +01:00
surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
ctx = cairo.Context (surface)
2013-11-25 11:36:37 +01:00
ctx.scale (PIXELS_PER_SECOND, PIXELS_PER_GATE)
2013-11-21 15:58:44 +01:00
2013-11-25 11:36:37 +01:00
for thread in threads:
for event in thread.events:
ctx.move_to (event.start, event.total_y)
ctx.line_to (event.stop, event.total_y)
ctx.close_path ()
2013-11-25 12:01:09 +01:00
ctx.set_line_width (0.1)
2013-11-21 15:58:44 +01:00
2013-11-25 11:36:37 +01:00
if event.wait:
ctx.set_source_rgb (0.9, 0.1, 0.1)
elif event.work:
ctx.set_source_rgb (0.1, 0.9, 0.1)
else:
ctx.set_source_rgb (0.1, 0.1, 0.9)
2013-11-21 15:58:44 +01:00
2013-11-25 11:36:37 +01:00
ctx.stroke ()
2013-11-21 15:58:44 +01:00
2013-11-25 12:01:09 +01:00
ctx.select_font_face('Georgia')
ctx.set_font_size(0.9)
ctx.move_to(event.start, event.total_y + 0.5)
ctx.set_source_rgb(1, 1, 1)
ctx.show_text(event.gate_name)
2013-11-21 15:58:44 +01:00
surface.write_to_png ("example.png") # Output to PNG