1 ! (c)2011 Joe Groff bsd license
2 USING: accessors assocs calendar combinators
3 combinators.short-circuit continuations fry generalizations
4 hashtables.identity io kernel kernel.private locals math
5 math.statistics math.vectors memory namespaces prettyprint
6 sequences sequences.generalizations sets sorting
7 tools.profiler.sampling.private math.parser.private
9 FROM: sequences => change-nth ;
10 FROM: assocs => change-at ;
11 IN: tools.profiler.sampling
13 SYMBOL: samples-per-second
15 samples-per-second [ 1,000 ] initialize
18 SYMBOL: raw-profile-data
19 CONSTANT: ignore-words
20 { signal-handler leaf-signal-handler profiling minor-gc }
22 : ignore-word? ( word -- ? ) ignore-words member? ; inline
25 : most-recent-profile-data ( -- profile-data )
26 raw-profile-data get-global [ "No profile data" throw ] unless* ;
29 samples-per-second get-global profiling
30 [ 0 profiling (get-samples) raw-profile-data set-global ]
33 : total-sample-count ( sample -- count ) 0 swap nth ;
34 : gc-sample-count ( sample -- count ) 1 swap nth ;
35 : jit-sample-count ( sample -- count ) 2 swap nth ;
36 : foreign-sample-count ( sample -- count ) 3 swap nth ;
37 : foreign-thread-sample-count ( sample -- count ) 4 swap nth ;
38 : sample-counts-slice ( sample -- counts ) 5 head-slice ;
40 : sample-thread ( sample -- thread ) 5 swap nth ;
41 : sample-callstack ( sample -- array ) 6 swap nth ;
42 : unclip-callstack ( sample -- sample' callstack-top )
43 clone 6 over [ unclip swap ] change-nth ;
45 : samples>time ( samples -- time )
46 samples-per-second get-global / seconds ;
48 : total-time* ( profile-data -- n )
49 [ total-sample-count ] map-sum samples>time ;
51 : gc-time* ( profile-data -- n )
52 [ gc-sample-count ] map-sum samples>time ;
54 : foreign-time* ( profile-data -- n )
55 [ foreign-sample-count ] map-sum samples>time ;
57 : foreign-thread-time* ( profile-data -- n )
58 [ foreign-thread-sample-count ] map-sum samples>time ;
61 most-recent-profile-data total-time* ;
63 most-recent-profile-data gc-time* ;
64 : foreign-time ( -- n )
65 most-recent-profile-data foreign-time* ;
66 : foreign-thread-time ( -- n )
67 most-recent-profile-data foreign-thread-time* ;
70 total-time gc-time jit-time foreign-time foreign-thread-time children
75 : collect-threads ( samples -- by-thread )
76 [ sample-thread ] collect-by ;
78 : time-per-thread ( -- n )
79 most-recent-profile-data collect-threads [ total-time* ] assoc-map ;
81 : leaf-callstack? ( callstack -- ? )
82 [ ignore-word? ] all? ;
84 CONSTANT: zero-counts { 0 0 0 0 0 }
86 : sum-counts ( samples -- times )
87 zero-counts [ sample-counts-slice v+ ] reduce ;
89 : <profile-node> ( times children depth -- node )
90 [ 5 firstn [ samples>time ] 5 napply ] 2dip profile-node boa ;
92 : <profile-root-node> ( samples collector-quot -- node )
93 [ sum-counts ] swap bi 0 <profile-node> ; inline
95 :: (collect-subtrees) ( samples max-depth depth child-quot: ( samples -- child ) -- children )
97 samples [ sample-callstack leaf-callstack? not ] filter
98 [ f ] [ child-quot call ] if-empty
101 :: collect-tops ( samples max-depth depth -- node )
102 samples [ unclip-callstack ] collect-pairs [
104 [ max-depth depth [ max-depth depth 1 + collect-tops ] (collect-subtrees) ] bi
108 : redundant-root-node? ( assoc -- ? )
110 [ children>> assoc-size 1 = ]
111 [ children>> values first children>> ]
112 [ [ total-time>> ] [ children>> values first total-time>> ] bi = ]
115 : trim-root ( root -- root' )
116 dup redundant-root-node? [ children>> values first trim-root ] when ;
118 :: (top-down) ( max-depth profile-data depth -- tree )
119 profile-data collect-threads
120 [ [ max-depth depth collect-tops ] <profile-root-node> trim-root ] assoc-map ;
124 : top-down-max-depth* ( max-depth profile-data -- tree )
127 : top-down-max-depth ( max-depth -- tree )
128 most-recent-profile-data top-down-max-depth* ;
130 : top-down* ( profile-data -- tree )
131 most-positive-fixnum top-down-max-depth* ;
133 : top-down ( -- tree )
134 most-positive-fixnum top-down-max-depth ;
138 :: counts+at ( key assoc sample -- )
139 key assoc [ zero-counts or sample sample-counts-slice v+ ] change-at ;
141 :: collect-flat ( samples -- flat )
142 IH{ } clone :> per-word-samples
144 sample sample-callstack unique keys [ ignore-word? not ] filter [
145 per-word-samples sample counts+at
148 per-word-samples [ f 0 <profile-node> ] assoc-map ;
150 : redundant-flat-node? ( child-node root-node -- ? )
151 [ total-time>> ] bi@ = ;
153 : trim-flat ( root-node -- root-node' )
154 dup '[ [ nip _ redundant-flat-node? not ] assoc-filter ] change-children ;
158 : flat* ( profile-data -- flat )
160 [ [ collect-flat ] <profile-root-node> trim-flat ] assoc-map ;
163 most-recent-profile-data flat* ;
167 : nth-or-last ( n seq -- elt )
169 2dup bounds-check? [ nth ] [ nip last ] if
172 :: collect-cross-section ( samples depth -- cross-section )
173 IH{ } clone :> per-word-samples
175 depth sample sample-callstack [ ignore-word? ] trim-tail nth-or-last :> word
177 word per-word-samples sample counts+at
180 per-word-samples [ f depth <profile-node> ] assoc-map ;
184 :: cross-section* ( depth profile-data -- tree )
185 profile-data collect-threads
186 [ [ depth collect-cross-section ] <profile-root-node> ] assoc-map ;
188 : cross-section ( depth -- tree )
189 most-recent-profile-data cross-section* ;
193 : depth. ( depth -- )
194 [ " " write ] times ;
196 : by-total-time ( nodes -- nodes' )
197 >alist [ second total-time>> ] inv-sort-with ;
199 : duration. ( duration -- )
200 duration>milliseconds >float "%9.1f" format-float write ;
202 : percentage. ( num denom -- )
203 [ duration>seconds ] bi@ [ 100 * ] dip /f "%6.2f" format-float write ;
207 :: times. ( node -- )
209 [ depth>> number>string 4 CHAR: \s pad-head write " " write ]
210 [ total-time>> duration. " " write ]
211 [ [ gc-time>> ] [ total-time>> ] bi percentage. " " write ]
212 [ [ jit-time>> ] [ total-time>> ] bi percentage. " " write ]
213 [ [ foreign-time>> ] [ total-time>> ] bi percentage. " " write ]
214 [ [ foreign-thread-time>> ] [ total-time>> ] bi percentage. " " write ]
217 :: (profile-node.) ( word node depth -- )
221 node children>> depth 1 + (profile.) ;
223 : (profile.) ( nodes depth -- )
224 [ by-total-time ] dip '[ _ (profile-node.) ] assoc-each ;
226 : profile-heading. ( -- )
227 "depth time ms GC % JIT % FFI % FT %" print ;
228 ! NNNN XXXXXXX.X XXXX.X XXXX.X XXXX.X XXXX.X | | foo
232 : profile. ( tree -- )
234 [ 0 (profile-node.) ] assoc-each ;