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.vectors memory namespaces prettyprint sequences
6 sequences.generalizations sets sorting ;
7 IN: tools.profiler.sampling
10 PRIMITIVE: (get-samples) ( -- samples/f )
11 PRIMITIVE: profiling ( n -- )
14 SYMBOL: samples-per-second
16 samples-per-second [ 1,000 ] initialize
19 SYMBOL: raw-profile-data
20 CONSTANT: ignore-words
21 { signal-handler leaf-signal-handler profiling minor-gc }
23 : ignore-word? ( word -- ? ) ignore-words member? ; inline
26 : most-recent-profile-data ( -- profile-data )
27 raw-profile-data get-global [ "No profile data" throw ] unless* ;
30 samples-per-second get-global profiling
31 [ 0 profiling (get-samples) raw-profile-data set-global ]
34 : total-sample-count ( sample -- count ) 0 swap nth ;
35 : gc-sample-count ( sample -- count ) 1 swap nth ;
36 : jit-sample-count ( sample -- count ) 2 swap nth ;
37 : foreign-sample-count ( sample -- count ) 3 swap nth ;
38 : foreign-thread-sample-count ( sample -- count ) 4 swap nth ;
39 : sample-counts-slice ( sample -- counts ) 5 head-slice ;
41 : sample-thread ( sample -- thread ) 5 swap nth ;
42 : sample-callstack ( sample -- array ) 6 swap nth ;
43 : unclip-callstack ( sample -- sample' callstack-top )
44 clone 6 over [ unclip-last swap ] change-nth ;
46 : samples>time ( samples -- seconds )
47 samples-per-second get-global / ;
49 : total-time* ( profile-data -- n )
50 [ total-sample-count ] map-sum samples>time ;
52 : gc-time* ( profile-data -- n )
53 [ gc-sample-count ] map-sum samples>time ;
55 : foreign-time* ( profile-data -- n )
56 [ foreign-sample-count ] map-sum samples>time ;
58 : foreign-thread-time* ( profile-data -- n )
59 [ foreign-thread-sample-count ] map-sum samples>time ;
62 most-recent-profile-data total-time* ;
65 most-recent-profile-data gc-time* ;
67 : foreign-time ( -- n )
68 most-recent-profile-data foreign-time* ;
70 : foreign-thread-time ( -- n )
71 most-recent-profile-data foreign-thread-time* ;
74 total-time gc-time jit-time foreign-time foreign-thread-time children
79 : collect-threads ( samples -- by-thread )
80 [ sample-thread ] collect-by ;
82 : time-per-thread ( -- n )
83 most-recent-profile-data collect-threads [ total-time* ] assoc-map ;
85 : leaf-callstack? ( callstack -- ? )
86 [ ignore-word? ] all? ;
88 CONSTANT: zero-counts { 0 0 0 0 0 }
90 : sum-counts ( samples -- times )
91 zero-counts [ sample-counts-slice v+ ] reduce ;
93 : <profile-node> ( times children depth -- node )
94 [ 5 firstn [ samples>time ] 5 napply ] 2dip profile-node boa ;
96 : <profile-root-node> ( samples collector-quot -- node )
97 [ sum-counts ] swap bi 0 <profile-node> ; inline
99 :: (collect-subtrees) ( samples max-depth depth child-quot: ( samples -- child ) -- children )
101 samples [ sample-callstack leaf-callstack? ] reject
102 [ f ] [ child-quot call ] if-empty
105 :: collect-tops ( samples max-depth depth -- node )
107 '[ unclip-callstack _ push-at ] each
110 [ max-depth depth [ max-depth depth 1 + collect-tops ] (collect-subtrees) ] bi
114 : redundant-root-node? ( assoc -- ? )
116 [ children>> assoc-size 1 = ]
117 [ children>> values first children>> ]
118 [ [ total-time>> ] [ children>> values first total-time>> ] bi = ]
121 : trim-root ( root -- root' )
122 dup redundant-root-node? [ children>> values first trim-root ] when ;
124 :: (top-down) ( max-depth profile-data depth -- tree )
125 profile-data collect-threads
126 [ [ max-depth depth collect-tops ] <profile-root-node> trim-root ] assoc-map ;
130 : top-down-max-depth* ( max-depth profile-data -- tree )
133 : top-down-max-depth ( max-depth -- tree )
134 most-recent-profile-data top-down-max-depth* ;
136 : top-down* ( profile-data -- tree )
137 [ most-positive-fixnum ] dip top-down-max-depth* ;
139 : top-down ( -- tree )
140 most-positive-fixnum top-down-max-depth ;
144 :: counts+at ( key assoc sample -- )
145 key assoc [ zero-counts or sample sample-counts-slice v+ ] change-at ;
147 :: collect-flat ( samples -- flat )
148 IH{ } clone :> per-word-samples
150 sample sample-callstack members [ ignore-word? ] reject [
151 per-word-samples sample counts+at
154 per-word-samples [ f 0 <profile-node> ] assoc-map ;
156 : redundant-flat-node? ( child-node root-node -- ? )
157 [ total-time>> ] same? ;
159 : trim-flat ( root-node -- root-node' )
160 dup '[ [ nip _ redundant-flat-node? ] assoc-reject ] change-children ;
164 : flat* ( profile-data -- flat )
166 [ [ collect-flat ] <profile-root-node> trim-flat ] assoc-map ;
169 most-recent-profile-data flat* ;
173 : nth-or-last ( n seq -- elt )
175 2dup bounds-check? [ nth ] [ nip last ] if
178 :: collect-cross-section ( samples depth -- cross-section )
179 IH{ } clone :> per-word-samples
181 depth sample sample-callstack [ ignore-word? ] trim-tail nth-or-last :> word
183 word per-word-samples sample counts+at
186 per-word-samples [ f depth <profile-node> ] assoc-map ;
190 :: cross-section* ( depth profile-data -- tree )
191 profile-data collect-threads
192 [ [ depth collect-cross-section ] <profile-root-node> ] assoc-map ;
194 : cross-section ( depth -- tree )
195 most-recent-profile-data cross-section* ;
199 : depth. ( depth -- )
200 [ " " write ] times ;
202 : by-total-time ( nodes -- nodes' )
203 >alist [ second total-time>> ] inv-sort-with ;
205 : duration. ( duration -- )
206 1000 * "%9.1f" printf ;
208 : percentage. ( num denom -- )
209 [ 100 * ] dip /f "%6.2f" printf ;
213 :: times. ( node -- )
215 [ depth>> number>string 4 CHAR: \s pad-head write bl ]
216 [ total-time>> duration. bl ]
217 [ [ gc-time>> ] [ total-time>> ] bi percentage. bl ]
218 [ [ jit-time>> ] [ total-time>> ] bi percentage. bl ]
219 [ [ foreign-time>> ] [ total-time>> ] bi percentage. bl ]
220 [ [ foreign-thread-time>> ] [ total-time>> ] bi percentage. bl ]
223 :: (profile-node.) ( word node depth -- )
227 node children>> depth 1 + (profile.) ;
229 : (profile.) ( nodes depth -- )
230 [ by-total-time ] dip '[ _ (profile-node.) ] assoc-each ;
232 : profile-heading. ( -- )
233 "depth time ms GC % JIT % FFI % FT %" print ;
234 ! NNNN XXXXXXX.X XXXX.X XXXX.X XXXX.X XXXX.X | | foo
238 : profile. ( tree -- )
240 [ 0 (profile-node.) ] assoc-each ;