Skip to content

Instantly share code, notes, and snippets.

@awreece
Created May 14, 2012 05:01
Show Gist options
  • Save awreece/2691857 to your computer and use it in GitHub Desktop.
Save awreece/2691857 to your computer and use it in GitHub Desktop.
Go memory allocation tracing
#!/bin/dash
ltrace -x runtime.mallocgc -f -ttt -o malloc.trace $@
python parse.py malloc.trace
import fileinput
import re
import sys
total_mallocs = 0
cold_misses = 0
false_shares = 0
cache_line_size = 64
cache = {}
core_map = {}
def update(core, time, size, ptr):
global total_mallocs
global cold_misses
global false_shares
line = ptr / cache_line_size
if line not in cache:
cold_misses += 1
elif cache[line] != core:
false_shares += 1
total_mallocs += 1
core_map[core] = core
cache[line] = core
regex = re.compile(r'([^ ]*) ([^ ]*) runtime.mallocgc\(([^,]*),.*= (.*)')
# Note: If not using ltrace -o filename use the following regex.
# regex = re.compile(r'\[pid ([^ ]*)\] ([^ ]*) runtime.mallocgc\(([^,]*),.*= (.*)')
for line in fileinput.input(sys.argv[1:]):
m = regex.match(line.strip())
if m is not None:
g = m.groups()
update(int(g[0]), float(g[1]), int(g[2]), int(g[3],16))
print "Malloc trace of %d mallocs over %d cores" % (total_mallocs, len(core_map))
print "\tCache line shared %d (%.2f%%) times" % (false_shares,
float(false_shares) /
float(total_mallocs) * 100)
print "\tCache line cold missed %d (%.2f%%) times" % (cold_misses,
float(cold_misses) /
float(total_mallocs) * 100)
# uiktest
Malloc trace of 105753 mallocs over 6 cores
Cache line shared 6673 (6.31%) times
Cache line cold missed 10885 (10.29%) times
# go install -v std
Malloc trace of 307308 mallocs over 2 cores
Cache line shared 0 (0.00%) times
Cache line cold missed 25260 (8.22%) times
# go install github.com/petar/GoLLRB/llrb
Malloc trace of 15059 mallocs over 5 cores
Cache line shared 184 (1.22%) times
Cache line cold missed 4684 (31.10%) times
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment