Skip to content

Instantly share code, notes, and snippets.

@no-defun-allowed
Created August 20, 2025 06:57
Show Gist options
  • Save no-defun-allowed/40ee176ffc5f2ea00ef18716b390a027 to your computer and use it in GitHub Desktop.
Save no-defun-allowed/40ee176ffc5f2ea00ef18716b390a027 to your computer and use it in GitHub Desktop.
flamegraphs from SB-SPROF
(require :sb-sprof)
(defun format-info (info)
(etypecase info
((or list symbol) (substitute #\/ #\; (write-to-string info :case :downcase)))
(string (format nil "~A_[k]" info))
(sb-di:debug-fun (format-info (sb-di:debug-fun-name info)))))
(defun draw-flamegraph.pl (pathname samples)
(with-open-file (f pathname :direction :output :if-exists :supersede)
(let ((seen (make-hash-table :test #'equal)))
(sb-sprof:map-traces
(lambda (thread trace)
(declare (ignore thread))
(let ((calls '()))
(sb-sprof::map-trace-pc-locs
(lambda (info offset)
(declare (ignore offset))
(push (format-info info) calls))
trace)
(incf (gethash (format nil "~{~A~^;~}" (reverse calls)) seen 0))))
samples)
(loop for name being the hash-keys of seen
for count being the hash-values of seen
do (format f "~&~A ~D" (remove #\Newline name) count))))
(sb-ext:run-program "/home/hayley/Downloads/FlameGraph/flamegraph.pl"
'()
:input (pathname pathname)
:output (make-pathname :defaults pathname :type "svg")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment