Skip to content

Instantly share code, notes, and snippets.

@angerman
Last active December 11, 2019 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angerman/96cf90722c1424493cd2b39ba06ce3de to your computer and use it in GitHub Desktop.
Save angerman/96cf90722c1424493cd2b39ba06ce3de to your computer and use it in GitHub Desktop.
#!/usr/bin/env nix-shell
#!nix-shell -i python3 -p python3 --pure
import sys
from pprint import pprint
stack = []
timestack = []
n = 0
print("PRAGMA synchronous = OFF;")
print("PRAGMA journal_mode = MEMORY;")
print("BEGIN TRANSACTION;")
print("CREATE TABLE prof (id INTEGER PRIMARY KEY, parent INTEGER, level INTEGER, name TEXT, time INTEGER);")
for line in open(sys.argv[1]):
components = line.strip().split(" ", 2)
if components[0] != "function-trace":
continue
direction = components[1]
components = components[2].rsplit(" ", 2)
loc = components[0]
# # We will ignore `undefined position` as those
# # are not really helpful. They are builtins.
# if loc == "undefined position":
# continue
_at = components[1]
time = int(components[2])
if direction == "entered":
n+=1
stack.append(n)
timestack.append(time)
elif direction == "exited":
dur = time - timestack.pop()
n_ = stack.pop()
lvl = len(stack)
print(f"INSERT INTO prof VALUES ({n_}, {lvl > 0 and stack[-1] or -1}, {lvl}, \"{loc}\", {dur});")
print("END TRANSACTION;")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment