]> gitweb.factorcode.org Git - factor.git/blob - extra/flamegraph/flamegraph.factor
flamegraph: adding a flamegraph tool
[factor.git] / extra / flamegraph / flamegraph.factor
1 ! Copyright (C) 2024 John Benediktsson
2 ! See https://factorcode.org/license.txt for BSD license
3
4 USING: accessors arrays assocs io io.encodings.utf8 io.files
5 io.files.temp io.launcher io.pathnames kernel math math.parser
6 prettyprint sequences tools.profiler.sampling ;
7
8 IN: flamegraph
9
10 <PRIVATE
11
12 :: write-flamegraph-node ( word node depth -- )
13     depth word unparse-short suffix [
14         ";" join write bl node total-time>> 1000 *
15         >integer number>string print
16     ] [
17         node children>> swap
18         '[ _ write-flamegraph-node ] assoc-each
19     ] bi ;
20
21 : write-flamegraph ( -- )
22     top-down >alist first second children>>
23     [ { } write-flamegraph-node ] assoc-each ;
24
25 PRIVATE>
26
27 : flamegraph ( -- )
28     "output.txt" temp-file
29     [ utf8 [ flamegraph. ] with-file-writer ]
30     [ "vocab:flamegraph/flamegraph.pl" absolute-path swap 2array process-contents ] bi
31     "output.svg" temp-file
32     [ utf8 set-file-contents ]
33     [ { "open" "-a" "Safari" } swap suffix try-process ] bi ;