| 1 |
|
| 2 |
""" |
| 3 |
Produces a logfile.gnumeric file which contains the data extracted from the |
| 4 |
logfile generated with the PYPYLOG env variable. |
| 5 |
|
| 6 |
Run your program like this:: |
| 7 |
|
| 8 |
$ PYPYLOG=gc-collect,jit-mem:logfile pypy your-program.py |
| 9 |
|
| 10 |
This will produce "logfile", containing information about the memory used by |
| 11 |
the GC and the number of loops created/freed by the JIT. |
| 12 |
|
| 13 |
If you want, you can also measure the amout of used memory as seen by the OS |
| 14 |
(the VmRSS) using memusage.py:: |
| 15 |
|
| 16 |
$ PYPYLOG=gc-collect,jit-mem:logfile ./memusage.py -o logfile.vmrss /path/to/pypy your-program.py |
| 17 |
|
| 18 |
log2gnumeric will automatically pick logfile.vmrss, if present. |
| 19 |
|
| 20 |
If you want to compare PyPy to CPython, you can add its VmRSS to the graph, by |
| 21 |
using the -c option. To produce the .vmrss file, use again ./memusage.py:: |
| 22 |
|
| 23 |
$ ./memusage.py -o cpython.vmrss python your-program.py |
| 24 |
$ ./log2gnumeric.py -c cpython.vmrss logfile |
| 25 |
|
| 26 |
Note that on CPython it will take a different amout of time to complete, but |
| 27 |
on the graph the plot will be scaled to match the duration of the PyPy run |
| 28 |
(i.e., the two lines will end "at the same time"). |
| 29 |
|
| 30 |
If you are benchmarking translate.py, you can add the "translation-task" |
| 31 |
category to the log, by setting PYPYLOG=gc-collect,jit-mem,translation-task. |
| 32 |
|
| 33 |
You can freely edit the graph in log-template.gnumeric: this script will |
| 34 |
create a new file replacing the 'translation-task' and 'gc-collect' sheets. |
| 35 |
""" |
| 36 |
|
| 37 |
import re, sys |
| 38 |
import gzip |
| 39 |
import optparse |
| 40 |
|
| 41 |
|
| 42 |
def main(logname, options): |
| 43 |
outname = logname + '.gnumeric' |
| 44 |
data = open(logname).read() |
| 45 |
data = data.replace('\n', '') |
| 46 |
minclock, maxclock = get_clock_range(data) |
| 47 |
time0 = minclock |
| 48 |
maxtime = maxclock-time0 |
| 49 |
|
| 50 |
xml = gzip.open('log-template.gnumeric').read() |
| 51 |
xml = replace_sheet(xml, 'translation-task', tasks_rows(time0, data)) |
| 52 |
xml = replace_sheet(xml, 'gc-collect', gc_collect_rows(time0, data)) |
| 53 |
xml = replace_sheet(xml, 'loops', loops_rows(time0, data)) |
| 54 |
xml = replace_sheet(xml, 'vmrss', vmrss_rows(logname + '.vmrss', maxtime)) |
| 55 |
xml = replace_sheet(xml, 'cpython-vmrss', vmrss_rows(options.cpython_vmrss, maxtime)) |
| 56 |
|
| 57 |
out = gzip.open(outname, 'wb') |
| 58 |
out.write(xml) |
| 59 |
out.close() |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
def replace_sheet(xml, sheet_name, data): |
| 67 |
pattern = '<gnm:Sheet .*?<gnm:Name>%s</gnm:Name>.*?(<gnm:Cells>.*?</gnm:Cells>)' |
| 68 |
regex = re.compile(pattern % sheet_name, re.DOTALL) |
| 69 |
cells = gen_cells(data) |
| 70 |
match = regex.search(xml) |
| 71 |
if not match: |
| 72 |
print 'Cannot find sheet %s' % sheet_name |
| 73 |
return xml |
| 74 |
a, b = match.span(1) |
| 75 |
xml2 = xml[:a] + cells + xml[b:] |
| 76 |
return xml2 |
| 77 |
|
| 78 |
def gen_cells(data): |
| 79 |
|
| 80 |
ValueType_Empty = 'ValueType="10"' |
| 81 |
ValueType_Number = 'ValueType="40"' |
| 82 |
ValueType_String = 'ValueType="60"' |
| 83 |
|
| 84 |
parts = [] |
| 85 |
parts.append('<gnm:Cells>') |
| 86 |
for i, row in enumerate(data): |
| 87 |
for j, val in enumerate(row): |
| 88 |
if val is None: |
| 89 |
attr = ValueType_Empty |
| 90 |
val = '' |
| 91 |
elif isinstance(val, (int, long, float)): |
| 92 |
attr = ValueType_Number |
| 93 |
else: |
| 94 |
attr = ValueType_String |
| 95 |
cell = ' <gnm:Cell Row="%d" Col="%d" %s>%s</gnm:Cell>' |
| 96 |
parts.append(cell % (i, j, attr, val)) |
| 97 |
parts.append(' </gnm:Cells>') |
| 98 |
return '\n'.join(parts) |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
CLOCK_FACTOR = 1 |
| 106 |
def read_clock(x): |
| 107 |
timestamp = int(x, 16) |
| 108 |
return timestamp / CLOCK_FACTOR |
| 109 |
|
| 110 |
def get_clock_range(data): |
| 111 |
s = r"\[([0-9a-f]+)\] " |
| 112 |
r = re.compile(s) |
| 113 |
clocks = [read_clock(x) for x in r.findall(data)] |
| 114 |
return min(clocks), max(clocks) |
| 115 |
|
| 116 |
def gc_collect_rows(time0, data): |
| 117 |
s = r""" |
| 118 |
----------- Full collection ------------------ |
| 119 |
\| used before collection: |
| 120 |
\| in ArenaCollection: (\d+) bytes |
| 121 |
\| raw_malloced: (\d+) bytes |
| 122 |
\| used after collection: |
| 123 |
\| in ArenaCollection: (\d+) bytes |
| 124 |
\| raw_malloced: (\d+) bytes |
| 125 |
\| number of major collects: (\d+) |
| 126 |
`---------------------------------------------- |
| 127 |
\[([0-9a-f]+)\] gc-collect\}""" |
| 128 |
|
| 129 |
r = re.compile(s.replace('\n', '')) |
| 130 |
yield 'clock', 'gc-before', 'gc-after' |
| 131 |
for a,b,c,d,e,f in r.findall(data): |
| 132 |
clock = read_clock(f) - time0 |
| 133 |
yield clock, int(a)+int(b), int(c)+int(d) |
| 134 |
|
| 135 |
def tasks_rows(time0, data): |
| 136 |
s = r""" |
| 137 |
\[([0-9a-f]+)\] \{translation-task |
| 138 |
starting ([\w-]+) |
| 139 |
""" |
| 140 |
|
| 141 |
r = re.compile(s.replace('\n', '')) |
| 142 |
yield 'clock', None, 'task' |
| 143 |
for a,b in r.findall(data): |
| 144 |
clock = read_clock(a) - time0 |
| 145 |
yield clock, 1, b |
| 146 |
|
| 147 |
|
| 148 |
def loops_rows(time0, data): |
| 149 |
s = r""" |
| 150 |
\[([0-9a-f]+)\] \{jit-mem-looptoken-(alloc|free) |
| 151 |
(.*?)\[ |
| 152 |
""" |
| 153 |
|
| 154 |
r = re.compile(s.replace('\n', '')) |
| 155 |
yield 'clock', 'total', 'loops', 'bridges' |
| 156 |
loops = 0 |
| 157 |
bridges = 0 |
| 158 |
fake_total = 0 |
| 159 |
for clock, action, text in r.findall(data): |
| 160 |
clock = read_clock(clock) - time0 |
| 161 |
if text.startswith('allocating Loop #'): |
| 162 |
loops += 1 |
| 163 |
elif text.startswith('allocating Bridge #'): |
| 164 |
bridges += 1 |
| 165 |
elif text.startswith('freeing Loop #'): |
| 166 |
match = re.match('freeing Loop # .* with ([0-9]*) attached bridges', text) |
| 167 |
loops -=1 |
| 168 |
bridges -= int(match.group(1)) |
| 169 |
total = loops+bridges |
| 170 |
yield clock, loops+bridges, loops, bridges |
| 171 |
|
| 172 |
|
| 173 |
def vmrss_rows(filename, maxtime): |
| 174 |
lines = [] |
| 175 |
if filename: |
| 176 |
try: |
| 177 |
lines = open(filename).readlines() |
| 178 |
except IOError: |
| 179 |
print 'Warning: cannot find file %s, skipping this sheet' % filename |
| 180 |
for row in vmrss_rows_impl(lines, maxtime): |
| 181 |
yield row |
| 182 |
|
| 183 |
def vmrss_rows_impl(lines, maxtime): |
| 184 |
yield 'inferred clock', 'VmRSS' |
| 185 |
numlines = len(lines) |
| 186 |
for i, line in enumerate(lines): |
| 187 |
mem = int(line) |
| 188 |
clock = maxtime * i // (numlines-1) |
| 189 |
yield clock, mem |
| 190 |
|
| 191 |
|
| 192 |
if __name__ == '__main__': |
| 193 |
CLOCK_FACTOR = 1000000000.0 |
| 194 |
parser = optparse.OptionParser(usage="%prog logfile [options]") |
| 195 |
parser.format_description = lambda fmt: __doc__ |
| 196 |
parser.description = __doc__ |
| 197 |
parser.add_option('-c', '--cpython-vmrss', dest='cpython_vmrss', default=None, metavar='FILE', type=str, |
| 198 |
help='the .vmrss file produced by CPython') |
| 199 |
|
| 200 |
options, args = parser.parse_args() |
| 201 |
if len(args) != 1: |
| 202 |
parser.print_help() |
| 203 |
sys.exit(2) |
| 204 |
main(args[0], options) |