Skip to content

Instantly share code, notes, and snippets.

@Muratam

Muratam/flamegraph.nim

Last active Jan 12, 2020
Embed
What would you like to do?
profile_results.txt を flamegraph.pl 形式にするやつ
import os
import system
import strutils
import sequtils
import algorithm
proc findCallCount(line:string) : string =
let args = line.split(" ")
for i,arg in args:
if not arg.startsWith("Call"): continue
return args[i+1].split("/")[0]
assert false,"cant find call count:\n[" & line & "]"
proc parseTree(lines:seq[string]): seq[seq[string]] =
result = @[]
var ignored = false
for line in lines:
if line == "" : continue
if line.startsWith(" "):
let flames = line.strip().split(" ")
let file = flames[0].replace(".nim","").replace(":","")
let fun = flames[1].replace(";","").replace(":","")
if file.startsWith(".") or fun.startsWith(".") : continue
if file == "nimprof" : ignored = true
if ignored : continue
result[^1] &= @[fun & "(" & file & ")"]
elif line.startsWith("Entry"):
ignored = false
result &= @[line.findCallCount()]
proc parse(fileName:string) : string =
result = ""
let lines = open(fileName,FileMode.fmRead).readAll().split("\n")
let tree = lines.parseTree
for t in tree:
result &= t[1..^1].reversed().join(";") & " " & t[0] & "\n"
if isMainModule:
echo parse(commandLineParams()[0])
# profile_results.txt を flamegraph.svg に
nim c -r flamegraph.nim profile_results.txt | perl flamegraph.pl > flamegraph.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment