Skip to content

Instantly share code, notes, and snippets.

@deepikabhavnani
Last active June 7, 2019 19:17
Show Gist options
  • Save deepikabhavnani/be3c04fafc5dd6d9d7a2eb7d1b6a561b to your computer and use it in GitHub Desktop.
Save deepikabhavnani/be3c04fafc5dd6d9d7a2eb7d1b6a561b to your computer and use it in GitHub Desktop.
Script to map alloc/free calls from golang trace
# input is trace file obtained by executing your go binary with trace enabled
# tmp directory should be created by user
# GODEBUG=allocfreetrace=1 go_binary
# output file gives alloc/free collection in csv file - tmp/memory.csv
# allocs which didnt have matching free calls in - tmp/leftallocs.csv
import sys
allocStr = "tracealloc"
freeStr = "tracefree"
goStr = "goroutine"
def findFree(rwfile, allocLine):
for line in rwfile:
if freeStr in line:
freeFields = line.split(" ")
allocFields = allocLine.split(" ")
if freeFields[2] == allocFields [2] and freeFields[3] == allocFields [3]:
#print freeFields
#print allocFields
return 1
return 0
print "Generating tmp/extract.txt to extact allocs/free"
#Extract tracealloc and tracefree from log file
wfile = open("tmp/extract.txt", "w")
with open (sys.argv[1], 'rt') as rfile:
found = 0
for num, line in enumerate(rfile, 1):
if found is 1:
found = 0
if goStr in line:
wfile.write(line)
else:
wfile.write('\n')
if allocStr in line or freeStr in line:
found = 1
wfile.write(str(num))
wfile.write(' ' +line.replace('\n', ' '))
wfile.close()
print "Fetching data from allocs/free and adding it to tmp/memory.csv"
#Add all values as separate fields in excel sheet
extract = []
wfile = open("tmp/memory.csv", "w")
with open ("tmp/extract.txt", 'rt') as rfile:
for line in rfile:
line = line.replace('(', ' ')
line = line.replace(',', '')
line = line.replace(')', '')
line = line.replace(':', '')
wfile.write(line)
wfile.close()
print "Scanning the files and removing matching alloc-free pairs"
print "Allocations not having matching free will be stored in tmp/leftallocs.csv"
#Compare and remove matching alloc-free pairs
wfile = open("tmp/leftallocs.csv", "w")
with open ("tmp/memory.csv", 'r+') as rwfile:
pos = 0
while True:
line = rwfile.readline()
pos = rwfile.tell()
if line:
if allocStr not in line:
continue
found = findFree(rwfile, line)
if found is 0:
wfile.write(line)
else:
break
rwfile.seek(pos, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment