1 ! (c)2011 Joe Groff bsd license
2 USING: accessors assocs combinators combinators.short-circuit
3 continuations formatting fry generalizations hashtables.identity
4 io kernel kernel.private layouts locals math math.parser
5 math.statistics math.vectors memory namespaces prettyprint
6 sequences sequences.generalizations sets sorting ;
7 FROM: sequences => change-nth ;
8 FROM: assocs => change-at ;
9 IN: tools.profiler.sampling
11 SYMBOL: samples-per-second
13 samples-per-second [ 1,000 ] initialize
16 SYMBOL: raw-profile-data
17 CONSTANT: ignore-words
18 { signal-handler leaf-signal-handler profiling minor-gc }
20 : ignore-word? ( word -- ? ) ignore-words member? ; inline
23 : most-recent-profile-data ( -- profile-data )
24 raw-profile-data get-global [ "No profile data" throw ] unless* ;
27 samples-per-second get-global profiling
28 [ 0 profiling (get-samples) raw-profile-data set-global ]
31 : total-sample-count ( sample -- count ) 0 swap nth ;
32 : gc-sample-count ( sample -- count ) 1 swap nth ;
33 : jit-sample-count ( sample -- count ) 2 swap nth ;
34 : foreign-sample-count ( sample -- count ) 3 swap nth ;
35 : foreign-thread-sample-count ( sample -- count ) 4 swap nth ;
36 : sample-counts-slice ( sample -- counts ) 5 head-slice ;
38 : sample-thread ( sample -- thread ) 5 swap nth ;
39 : sample-callstack ( sample -- array ) 6 swap nth ;
40 : unclip-callstack ( sample -- sample' callstack-top )
41 clone 6 over [ unclip swap ] change-nth ;
43 : samples>time ( samples -- seconds )
44 samples-per-second get-global / ;
46 : total-time* ( profile-data -- n )
47 [ total-sample-count ] map-sum samples>time ;
49 : gc-time* ( profile-data -- n )
50 [ gc-sample-count ] map-sum samples>time ;
52 : foreign-time* ( profile-data -- n )
53 [ foreign-sample-count ] map-sum samples>time ;
55 : foreign-thread-time* ( profile-data -- n )
56 [ foreign-thread-sample-count ] map-sum samples>time ;
59 most-recent-profile-data total-time* ;
62 most-recent-profile-data gc-time* ;
64 : foreign-time ( -- n )
65 most-recent-profile-data foreign-time* ;
67 : foreign-thread-time ( -- n )
68 most-recent-profile-data foreign-thread-time* ;
71 total-time gc-time jit-time foreign-time foreign-thread-time children
76 : collect-threads ( samples -- by-thread )
77 [ sample-thread ] collect-by ;
79 : time-per-thread ( -- n )
80 most-recent-profile-data collect-threads [ total-time* ] assoc-map ;
82 : leaf-callstack? ( callstack -- ? )
83 [ ignore-word? ] all? ;
85 CONSTANT: zero-counts { 0 0 0 0 0 }
87 : sum-counts ( samples -- times )
88 zero-counts [ sample-counts-slice v+ ] reduce ;
90 : <profile-node> ( times children depth -- node )
91 [ 5 firstn [ samples>time ] 5 napply ] 2dip profile-node boa ;
93 : <profile-root-node> ( samples collector-quot -- node )
94 [ sum-counts ] swap bi 0 <profile-node> ; inline
96 :: (collect-subtrees) ( samples max-depth depth child-quot: ( samples -- child ) -- children )
98 samples [ sample-callstack leaf-callstack? not ] filter
99 [ f ] [ child-quot call ] if-empty
102 :: collect-tops ( samples max-depth depth -- node )
104 '[ unclip-callstack _ push-at ] each
107 [ max-depth depth [ max-depth depth 1 + collect-tops ] (collect-subtrees) ] bi
111 : redundant-root-node? ( assoc -- ? )
113 [ children>> assoc-size 1 = ]
114 [ children>> values first children>> ]
115 [ [ total-time>> ] [ children>> values first total-time>> ] bi = ]
118 : trim-root ( root -- root' )
119 dup redundant-root-node? [ children>> values first trim-root ] when ;
121 :: (top-down) ( max-depth profile-data depth -- tree )
122 profile-data collect-threads
123 [ [ max-depth depth collect-tops ] <profile-root-node> trim-root ] assoc-map ;
127 : top-down-max-depth* ( max-depth profile-data -- tree )
130 : top-down-max-depth ( max-depth -- tree )
131 most-recent-profile-data top-down-max-depth* ;
133 : top-down* ( profile-data -- tree )
134 [ most-positive-fixnum ] dip top-down-max-depth* ;
136 : top-down ( -- tree )
137 most-positive-fixnum top-down-max-depth ;
141 :: counts+at ( key assoc sample -- )
142 key assoc [ zero-counts or sample sample-counts-slice v+ ] change-at ;
144 :: collect-flat ( samples -- flat )
145 IH{ } clone :> per-word-samples
147 sample sample-callstack members [ ignore-word? not ] filter [
148 per-word-samples sample counts+at
151 per-word-samples [ f 0 <profile-node> ] assoc-map ;
153 : redundant-flat-node? ( child-node root-node -- ? )
154 [ total-time>> ] same? ;
156 : trim-flat ( root-node -- root-node' )
157 dup '[ [ nip _ redundant-flat-node? not ] assoc-filter ] change-children ;
161 : flat* ( profile-data -- flat )
163 [ [ collect-flat ] <profile-root-node> trim-flat ] assoc-map ;
166 most-recent-profile-data flat* ;
170 : nth-or-last ( n seq -- elt )
172 2dup bounds-check? [ nth ] [ nip last ] if
175 :: collect-cross-section ( samples depth -- cross-section )
176 IH{ } clone :> per-word-samples
178 depth sample sample-callstack [ ignore-word? ] trim-tail nth-or-last :> word
180 word per-word-samples sample counts+at
183 per-word-samples [ f depth <profile-node> ] assoc-map ;
187 :: cross-section* ( depth profile-data -- tree )
188 profile-data collect-threads
189 [ [ depth collect-cross-section ] <profile-root-node> ] assoc-map ;
191 : cross-section ( depth -- tree )
192 most-recent-profile-data cross-section* ;
196 : depth. ( depth -- )
197 [ " " write ] times ;
199 : by-total-time ( nodes -- nodes' )
200 >alist [ second total-time>> ] inv-sort-with ;
202 : duration. ( duration -- )
203 1000 * "%9.1f" printf ;
205 : percentage. ( num denom -- )
206 [ 100 * ] dip /f "%6.2f" printf ;
210 :: times. ( node -- )
212 [ depth>> number>string 4 CHAR: \s pad-head write bl ]
213 [ total-time>> duration. bl ]
214 [ [ gc-time>> ] [ total-time>> ] bi percentage. bl ]
215 [ [ jit-time>> ] [ total-time>> ] bi percentage. bl ]
216 [ [ foreign-time>> ] [ total-time>> ] bi percentage. bl ]
217 [ [ foreign-thread-time>> ] [ total-time>> ] bi percentage. bl ]
220 :: (profile-node.) ( word node depth -- )
224 node children>> depth 1 + (profile.) ;
226 : (profile.) ( nodes depth -- )
227 [ by-total-time ] dip '[ _ (profile-node.) ] assoc-each ;
229 : profile-heading. ( -- )
230 "depth time ms GC % JIT % FFI % FT %" print ;
231 ! NNNN XXXXXXX.X XXXX.X XXXX.X XXXX.X XXXX.X | | foo
235 : profile. ( tree -- )
237 [ 0 (profile-node.) ] assoc-each ;