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
12 PRIMITIVE: (get-samples) ( -- samples/f )
13 PRIMITIVE: profiling ( ? -- )
14 PRIMITIVE: (clear-samples) ( -- )
17 SYMBOL: samples-per-second
19 samples-per-second [ 1,000 ] initialize
22 SYMBOL: raw-profile-data
23 CONSTANT: ignore-words
24 { signal-handler leaf-signal-handler profiling minor-gc }
26 : ignore-word? ( word -- ? ) ignore-words member? ; inline
29 : most-recent-profile-data ( -- profile-data )
30 raw-profile-data get-global [ "No profile data" throw ] unless* ;
33 samples-per-second get-global profiling
34 [ 0 profiling (get-samples) raw-profile-data set-global ]
37 : total-sample-count ( sample -- count ) 0 swap nth ;
38 : gc-sample-count ( sample -- count ) 1 swap nth ;
39 : jit-sample-count ( sample -- count ) 2 swap nth ;
40 : foreign-sample-count ( sample -- count ) 3 swap nth ;
41 : foreign-thread-sample-count ( sample -- count ) 4 swap nth ;
42 : sample-counts-slice ( sample -- counts ) 5 head-slice ;
44 : sample-thread ( sample -- thread ) 5 swap nth ;
45 : sample-callstack ( sample -- array ) 6 swap nth ;
46 : unclip-callstack ( sample -- sample' callstack-top )
47 clone 6 over [ unclip swap ] change-nth ;
49 : samples>time ( samples -- seconds )
50 samples-per-second get-global / ;
52 : total-time* ( profile-data -- n )
53 [ total-sample-count ] map-sum samples>time ;
55 : gc-time* ( profile-data -- n )
56 [ gc-sample-count ] map-sum samples>time ;
58 : foreign-time* ( profile-data -- n )
59 [ foreign-sample-count ] map-sum samples>time ;
61 : foreign-thread-time* ( profile-data -- n )
62 [ foreign-thread-sample-count ] map-sum samples>time ;
65 most-recent-profile-data total-time* ;
68 most-recent-profile-data gc-time* ;
70 : foreign-time ( -- n )
71 most-recent-profile-data foreign-time* ;
73 : foreign-thread-time ( -- n )
74 most-recent-profile-data foreign-thread-time* ;
77 total-time gc-time jit-time foreign-time foreign-thread-time children
82 : collect-threads ( samples -- by-thread )
83 [ sample-thread ] collect-by ;
85 : time-per-thread ( -- n )
86 most-recent-profile-data collect-threads [ total-time* ] assoc-map ;
88 : leaf-callstack? ( callstack -- ? )
89 [ ignore-word? ] all? ;
91 CONSTANT: zero-counts { 0 0 0 0 0 }
93 : sum-counts ( samples -- times )
94 zero-counts [ sample-counts-slice v+ ] reduce ;
96 : <profile-node> ( times children depth -- node )
97 [ 5 firstn [ samples>time ] 5 napply ] 2dip profile-node boa ;
99 : <profile-root-node> ( samples collector-quot -- node )
100 [ sum-counts ] swap bi 0 <profile-node> ; inline
102 :: (collect-subtrees) ( samples max-depth depth child-quot: ( samples -- child ) -- children )
104 samples [ sample-callstack leaf-callstack? ] reject
105 [ f ] [ child-quot call ] if-empty
108 :: collect-tops ( samples max-depth depth -- node )
110 '[ unclip-callstack _ push-at ] each
113 [ max-depth depth [ max-depth depth 1 + collect-tops ] (collect-subtrees) ] bi
117 : redundant-root-node? ( assoc -- ? )
119 [ children>> assoc-size 1 = ]
120 [ children>> values first children>> ]
121 [ [ total-time>> ] [ children>> values first total-time>> ] bi = ]
124 : trim-root ( root -- root' )
125 dup redundant-root-node? [ children>> values first trim-root ] when ;
127 :: (top-down) ( max-depth profile-data depth -- tree )
128 profile-data collect-threads
129 [ [ max-depth depth collect-tops ] <profile-root-node> trim-root ] assoc-map ;
133 : top-down-max-depth* ( max-depth profile-data -- tree )
136 : top-down-max-depth ( max-depth -- tree )
137 most-recent-profile-data top-down-max-depth* ;
139 : top-down* ( profile-data -- tree )
140 [ most-positive-fixnum ] dip top-down-max-depth* ;
142 : top-down ( -- tree )
143 most-positive-fixnum top-down-max-depth ;
147 :: counts+at ( key assoc sample -- )
148 key assoc [ zero-counts or sample sample-counts-slice v+ ] change-at ;
150 :: collect-flat ( samples -- flat )
151 IH{ } clone :> per-word-samples
153 sample sample-callstack members [ ignore-word? ] reject [
154 per-word-samples sample counts+at
157 per-word-samples [ f 0 <profile-node> ] assoc-map ;
159 : redundant-flat-node? ( child-node root-node -- ? )
160 [ total-time>> ] same? ;
162 : trim-flat ( root-node -- root-node' )
163 dup '[ [ nip _ redundant-flat-node? ] assoc-reject ] change-children ;
167 : flat* ( profile-data -- flat )
169 [ [ collect-flat ] <profile-root-node> trim-flat ] assoc-map ;
172 most-recent-profile-data flat* ;
176 : nth-or-last ( n seq -- elt )
178 2dup bounds-check? [ nth ] [ nip last ] if
181 :: collect-cross-section ( samples depth -- cross-section )
182 IH{ } clone :> per-word-samples
184 depth sample sample-callstack [ ignore-word? ] trim-tail nth-or-last :> word
186 word per-word-samples sample counts+at
189 per-word-samples [ f depth <profile-node> ] assoc-map ;
193 :: cross-section* ( depth profile-data -- tree )
194 profile-data collect-threads
195 [ [ depth collect-cross-section ] <profile-root-node> ] assoc-map ;
197 : cross-section ( depth -- tree )
198 most-recent-profile-data cross-section* ;
202 : depth. ( depth -- )
203 [ " " write ] times ;
205 : by-total-time ( nodes -- nodes' )
206 >alist [ second total-time>> ] inv-sort-with ;
208 : duration. ( duration -- )
209 1000 * "%9.1f" printf ;
211 : percentage. ( num denom -- )
212 [ 100 * ] dip /f "%6.2f" printf ;
216 :: times. ( node -- )
218 [ depth>> number>string 4 CHAR: \s pad-head write bl ]
219 [ total-time>> duration. bl ]
220 [ [ gc-time>> ] [ total-time>> ] bi percentage. bl ]
221 [ [ jit-time>> ] [ total-time>> ] bi percentage. bl ]
222 [ [ foreign-time>> ] [ total-time>> ] bi percentage. bl ]
223 [ [ foreign-thread-time>> ] [ total-time>> ] bi percentage. bl ]
226 :: (profile-node.) ( word node depth -- )
230 node children>> depth 1 + (profile.) ;
232 : (profile.) ( nodes depth -- )
233 [ by-total-time ] dip '[ _ (profile-node.) ] assoc-each ;
235 : profile-heading. ( -- )
236 "depth time ms GC % JIT % FFI % FT %" print ;
237 ! NNNN XXXXXXX.X XXXX.X XXXX.X XXXX.X XXXX.X | | foo
241 : profile. ( tree -- )
243 [ 0 (profile-node.) ] assoc-each ;