1 ! (c)2011 Joe Groff bsd license
2 USING: accessors assocs combinators
3 combinators.short-circuit continuations fry generalizations
4 hashtables.identity io kernel kernel.private layouts locals
5 math math.parser math.parser.private math.statistics
6 math.vectors memory namespaces prettyprint sequences
7 sequences.generalizations sets sorting ;
8 FROM: sequences => change-nth ;
9 FROM: assocs => change-at ;
10 IN: tools.profiler.sampling
12 SYMBOL: samples-per-second
14 samples-per-second [ 1,000 ] initialize
17 SYMBOL: raw-profile-data
18 CONSTANT: ignore-words
19 { signal-handler leaf-signal-handler profiling minor-gc }
21 : ignore-word? ( word -- ? ) ignore-words member? ; inline
24 : most-recent-profile-data ( -- profile-data )
25 raw-profile-data get-global [ "No profile data" throw ] unless* ;
28 samples-per-second get-global profiling
29 [ 0 profiling (get-samples) raw-profile-data set-global ]
32 : total-sample-count ( sample -- count ) 0 swap nth ;
33 : gc-sample-count ( sample -- count ) 1 swap nth ;
34 : jit-sample-count ( sample -- count ) 2 swap nth ;
35 : foreign-sample-count ( sample -- count ) 3 swap nth ;
36 : foreign-thread-sample-count ( sample -- count ) 4 swap nth ;
37 : sample-counts-slice ( sample -- counts ) 5 head-slice ;
39 : sample-thread ( sample -- thread ) 5 swap nth ;
40 : sample-callstack ( sample -- array ) 6 swap nth ;
41 : unclip-callstack ( sample -- sample' callstack-top )
42 clone 6 over [ unclip swap ] change-nth ;
44 : samples>time ( samples -- seconds )
45 samples-per-second get-global / ;
47 : total-time* ( profile-data -- n )
48 [ total-sample-count ] map-sum samples>time ;
50 : gc-time* ( profile-data -- n )
51 [ gc-sample-count ] map-sum samples>time ;
53 : foreign-time* ( profile-data -- n )
54 [ foreign-sample-count ] map-sum samples>time ;
56 : foreign-thread-time* ( profile-data -- n )
57 [ foreign-thread-sample-count ] map-sum samples>time ;
60 most-recent-profile-data total-time* ;
62 most-recent-profile-data gc-time* ;
63 : foreign-time ( -- n )
64 most-recent-profile-data foreign-time* ;
65 : foreign-thread-time ( -- n )
66 most-recent-profile-data foreign-thread-time* ;
69 total-time gc-time jit-time foreign-time foreign-thread-time children
74 : collect-threads ( samples -- by-thread )
75 [ sample-thread ] collect-by ;
77 : time-per-thread ( -- n )
78 most-recent-profile-data collect-threads [ total-time* ] assoc-map ;
80 : leaf-callstack? ( callstack -- ? )
81 [ ignore-word? ] all? ;
83 CONSTANT: zero-counts { 0 0 0 0 0 }
85 : sum-counts ( samples -- times )
86 zero-counts [ sample-counts-slice v+ ] reduce ;
88 : <profile-node> ( times children depth -- node )
89 [ 5 firstn [ samples>time ] 5 napply ] 2dip profile-node boa ;
91 : <profile-root-node> ( samples collector-quot -- node )
92 [ sum-counts ] swap bi 0 <profile-node> ; inline
94 :: (collect-subtrees) ( samples max-depth depth child-quot: ( samples -- child ) -- children )
96 samples [ sample-callstack leaf-callstack? not ] filter
97 [ f ] [ child-quot call ] if-empty
100 :: collect-tops ( samples max-depth depth -- node )
101 samples [ unclip-callstack ] collect-pairs [
103 [ max-depth depth [ max-depth depth 1 + collect-tops ] (collect-subtrees) ] bi
107 : redundant-root-node? ( assoc -- ? )
109 [ children>> assoc-size 1 = ]
110 [ children>> values first children>> ]
111 [ [ total-time>> ] [ children>> values first total-time>> ] bi = ]
114 : trim-root ( root -- root' )
115 dup redundant-root-node? [ children>> values first trim-root ] when ;
117 :: (top-down) ( max-depth profile-data depth -- tree )
118 profile-data collect-threads
119 [ [ max-depth depth collect-tops ] <profile-root-node> trim-root ] assoc-map ;
123 : top-down-max-depth* ( max-depth profile-data -- tree )
126 : top-down-max-depth ( max-depth -- tree )
127 most-recent-profile-data top-down-max-depth* ;
129 : top-down* ( profile-data -- tree )
130 most-positive-fixnum top-down-max-depth* ;
132 : top-down ( -- tree )
133 most-positive-fixnum top-down-max-depth ;
137 :: counts+at ( key assoc sample -- )
138 key assoc [ zero-counts or sample sample-counts-slice v+ ] change-at ;
140 :: collect-flat ( samples -- flat )
141 IH{ } clone :> per-word-samples
143 sample sample-callstack unique keys [ ignore-word? not ] filter [
144 per-word-samples sample counts+at
147 per-word-samples [ f 0 <profile-node> ] assoc-map ;
149 : redundant-flat-node? ( child-node root-node -- ? )
150 [ total-time>> ] bi@ = ;
152 : trim-flat ( root-node -- root-node' )
153 dup '[ [ nip _ redundant-flat-node? not ] assoc-filter ] change-children ;
157 : flat* ( profile-data -- flat )
159 [ [ collect-flat ] <profile-root-node> trim-flat ] assoc-map ;
162 most-recent-profile-data flat* ;
166 : nth-or-last ( n seq -- elt )
168 2dup bounds-check? [ nth ] [ nip last ] if
171 :: collect-cross-section ( samples depth -- cross-section )
172 IH{ } clone :> per-word-samples
174 depth sample sample-callstack [ ignore-word? ] trim-tail nth-or-last :> word
176 word per-word-samples sample counts+at
179 per-word-samples [ f depth <profile-node> ] assoc-map ;
183 :: cross-section* ( depth profile-data -- tree )
184 profile-data collect-threads
185 [ [ depth collect-cross-section ] <profile-root-node> ] assoc-map ;
187 : cross-section ( depth -- tree )
188 most-recent-profile-data cross-section* ;
192 : depth. ( depth -- )
193 [ " " write ] times ;
195 : by-total-time ( nodes -- nodes' )
196 >alist [ second total-time>> ] inv-sort-with ;
198 : duration. ( duration -- )
199 1000 * >float "%9.1f" format-float write ;
201 : percentage. ( num denom -- )
202 [ 100 * ] dip /f "%6.2f" format-float write ;
206 :: times. ( node -- )
208 [ depth>> number>string 4 CHAR: \s pad-head write " " write ]
209 [ total-time>> duration. " " write ]
210 [ [ gc-time>> ] [ total-time>> ] bi percentage. " " write ]
211 [ [ jit-time>> ] [ total-time>> ] bi percentage. " " write ]
212 [ [ foreign-time>> ] [ total-time>> ] bi percentage. " " write ]
213 [ [ foreign-thread-time>> ] [ total-time>> ] bi percentage. " " write ]
216 :: (profile-node.) ( word node depth -- )
220 node children>> depth 1 + (profile.) ;
222 : (profile.) ( nodes depth -- )
223 [ by-total-time ] dip '[ _ (profile-node.) ] assoc-each ;
225 : profile-heading. ( -- )
226 "depth time ms GC % JIT % FFI % FT %" print ;
227 ! NNNN XXXXXXX.X XXXX.X XXXX.X XXXX.X XXXX.X | | foo
231 : profile. ( tree -- )
233 [ 0 (profile-node.) ] assoc-each ;