2011-10-31 23:52:18 -04:00
|
|
|
! (c)2011 Joe Groff bsd license
|
2011-11-01 20:52:35 -04:00
|
|
|
USING: accessors assocs calendar combinators
|
|
|
|
combinators.short-circuit continuations fry io kernel
|
|
|
|
kernel.private locals math math.statistics math.vectors memory
|
|
|
|
namespaces prettyprint sequences sorting
|
2011-11-01 22:39:22 -04:00
|
|
|
tools.profiler.sampling.private hashtables.identity generalizations ;
|
2011-11-01 16:10:58 -04:00
|
|
|
FROM: sequences => change-nth ;
|
2011-11-01 22:39:22 -04:00
|
|
|
FROM: assocs => change-at ;
|
2011-10-31 21:59:48 -04:00
|
|
|
IN: tools.profiler.sampling
|
|
|
|
|
|
|
|
SYMBOL: raw-profile-data
|
2011-11-01 00:31:41 -04:00
|
|
|
SYMBOL: samples-per-second
|
|
|
|
|
|
|
|
CONSTANT: default-samples-per-second 1000
|
2011-10-31 21:59:48 -04:00
|
|
|
|
2011-11-01 16:10:58 -04:00
|
|
|
CONSTANT: ignore-words
|
2011-11-01 20:52:35 -04:00
|
|
|
{ signal-handler leaf-signal-handler profiling minor-gc }
|
2011-11-01 16:10:58 -04:00
|
|
|
|
2011-10-31 21:59:48 -04:00
|
|
|
: get-raw-profile-data ( -- data )
|
|
|
|
raw-profile-data get-global [ "No profile data" throw ] unless* ;
|
|
|
|
|
2011-11-01 00:31:41 -04:00
|
|
|
: profile* ( rate quot -- )
|
|
|
|
[ [ samples-per-second set-global ] [ profiling ] bi ] dip
|
|
|
|
[ 0 profiling ] [ ] cleanup
|
2011-10-31 21:59:48 -04:00
|
|
|
(get-samples) raw-profile-data set-global ; inline
|
|
|
|
|
2011-11-01 00:31:41 -04:00
|
|
|
: profile ( quot -- ) default-samples-per-second swap profile* ; inline
|
|
|
|
|
2011-10-31 21:59:48 -04:00
|
|
|
: total-sample-count ( sample -- count ) first ;
|
|
|
|
: gc-sample-count ( sample -- count ) second ;
|
|
|
|
: foreign-sample-count ( sample -- count ) third ;
|
|
|
|
: foreign-thread-sample-count ( sample -- count ) fourth ;
|
2011-11-01 22:39:22 -04:00
|
|
|
: sample-thread ( sample -- alien ) 4 swap nth ;
|
2011-10-31 21:59:48 -04:00
|
|
|
: sample-callstack ( sample -- array ) 5 swap nth ;
|
|
|
|
|
|
|
|
: samples>time ( samples -- time )
|
2011-11-01 00:31:41 -04:00
|
|
|
samples-per-second get-global / seconds ;
|
2011-10-31 21:59:48 -04:00
|
|
|
|
|
|
|
: (total-time) ( samples -- n )
|
|
|
|
[ total-sample-count ] map-sum samples>time ;
|
|
|
|
|
|
|
|
: (gc-time) ( samples -- n )
|
|
|
|
[ gc-sample-count ] map-sum samples>time ;
|
|
|
|
|
|
|
|
: (foreign-time) ( samples -- n )
|
|
|
|
[ foreign-sample-count ] map-sum samples>time ;
|
|
|
|
|
|
|
|
: (foreign-thread-time) ( samples -- n )
|
|
|
|
[ foreign-thread-sample-count ] map-sum samples>time ;
|
|
|
|
|
|
|
|
: total-time ( -- n )
|
|
|
|
get-raw-profile-data (total-time) ;
|
|
|
|
: gc-time ( -- n )
|
|
|
|
get-raw-profile-data (gc-time) ;
|
|
|
|
: foreign-time ( -- n )
|
|
|
|
get-raw-profile-data (foreign-time) ;
|
|
|
|
: foreign-thread-time ( -- n )
|
|
|
|
get-raw-profile-data (foreign-thread-time) ;
|
|
|
|
|
2011-11-01 22:39:22 -04:00
|
|
|
: collect-threads ( samples -- by-top )
|
|
|
|
[ sample-thread ] collect-by ;
|
2011-11-01 16:10:58 -04:00
|
|
|
|
2011-11-01 22:39:22 -04:00
|
|
|
: time-per-thread ( -- n )
|
|
|
|
get-raw-profile-data collect-threads [ (total-time) ] assoc-map ;
|
2011-11-01 16:10:58 -04:00
|
|
|
|
|
|
|
: unclip-callstack ( sample -- sample' callstack-top )
|
|
|
|
clone 5 over [ unclip swap ] change-nth ;
|
|
|
|
|
|
|
|
TUPLE: profile-node
|
|
|
|
total-time gc-time foreign-time foreign-thread-time children ;
|
|
|
|
|
2011-11-01 20:52:35 -04:00
|
|
|
: <profile-node> ( times children -- node )
|
2011-11-01 22:39:22 -04:00
|
|
|
[ first4 [ samples>time ] 4 napply ] dip profile-node boa ;
|
2011-11-01 20:52:35 -04:00
|
|
|
|
|
|
|
: leaf-callstack? ( callstack -- ? )
|
|
|
|
[ ignore-words member? ] all? ;
|
|
|
|
|
|
|
|
: sum-times ( samples -- times )
|
2011-11-01 22:39:22 -04:00
|
|
|
{ 0 0 0 0 } [ 4 head-slice v+ ] reduce ;
|
2011-11-01 20:52:35 -04:00
|
|
|
|
|
|
|
:: (collect-subtrees) ( samples child-quot -- children )
|
|
|
|
samples [ sample-callstack leaf-callstack? not ] filter
|
|
|
|
[ f ] [ child-quot call ] if-empty ; inline
|
2011-11-01 16:10:58 -04:00
|
|
|
|
|
|
|
: collect-tops ( samples -- node )
|
2011-11-01 20:52:35 -04:00
|
|
|
[ unclip-callstack ] collect-pairs [
|
|
|
|
[ sum-times ]
|
|
|
|
[ [ collect-tops ] (collect-subtrees) ] bi <profile-node>
|
|
|
|
] assoc-map ;
|
2011-11-01 16:10:58 -04:00
|
|
|
|
2011-11-01 20:52:35 -04:00
|
|
|
: redundant-root-node? ( assoc -- ? )
|
|
|
|
{
|
|
|
|
[ children>> assoc-size 1 = ]
|
|
|
|
[ children>> values first children>> ]
|
|
|
|
[ [ total-time>> ] [ children>> values first total-time>> ] bi = ]
|
|
|
|
} 1&& ;
|
|
|
|
|
|
|
|
: trim-root ( root -- root' )
|
|
|
|
dup redundant-root-node? [ children>> values first trim-root ] when ;
|
2011-11-01 16:10:58 -04:00
|
|
|
|
|
|
|
: (top-down) ( samples -- tree )
|
2011-11-01 22:39:22 -04:00
|
|
|
collect-threads [
|
2011-11-01 20:52:35 -04:00
|
|
|
[ sum-times ] [ collect-tops ] bi <profile-node> trim-root
|
|
|
|
] assoc-map ;
|
2011-11-01 16:10:58 -04:00
|
|
|
|
|
|
|
: top-down ( -- tree )
|
|
|
|
get-raw-profile-data (top-down) ;
|
|
|
|
|
2011-11-01 22:39:22 -04:00
|
|
|
:: collect-flat ( samples -- flat )
|
|
|
|
IH{ } clone :> per-word-samples
|
|
|
|
samples [| sample |
|
|
|
|
sample sample-callstack unique keys [
|
|
|
|
per-word-samples [ { 0 0 0 0 } or sample 4 head-slice v+ ] change-at
|
|
|
|
] each
|
|
|
|
] each
|
|
|
|
per-word-samples [ f <profile-node> ] assoc-map ;
|
|
|
|
|
|
|
|
: (flat) ( samples -- flat )
|
|
|
|
collect-threads [
|
|
|
|
[ sum-times ] [ collect-flat ] bi <profile-node>
|
|
|
|
] assoc-map ;
|
|
|
|
|
|
|
|
: flat ( -- tree )
|
|
|
|
get-raw-profile-data (flat) ;
|
|
|
|
|
2011-11-01 16:10:58 -04:00
|
|
|
: depth. ( depth -- )
|
|
|
|
[ " " write ] times ;
|
|
|
|
|
|
|
|
: by-total-time ( nodes -- nodes' )
|
|
|
|
>alist [ second total-time>> ] inv-sort-with ;
|
|
|
|
|
|
|
|
: duration. ( duration -- )
|
2011-11-01 20:52:35 -04:00
|
|
|
duration>milliseconds >integer pprint "ms" write ;
|
2011-11-01 16:10:58 -04:00
|
|
|
|
|
|
|
DEFER: (profile.)
|
|
|
|
|
2011-11-01 20:52:35 -04:00
|
|
|
: times. ( node -- )
|
|
|
|
{
|
|
|
|
[ total-time>> duration. " (" write ]
|
|
|
|
[ gc-time>> duration. " gc, " write ]
|
|
|
|
[ foreign-time>> duration. " foreign, " write ]
|
|
|
|
[ foreign-thread-time>> duration. " foreign threads)" write ]
|
|
|
|
} cleave ;
|
|
|
|
|
2011-11-01 16:10:58 -04:00
|
|
|
:: (profile-node.) ( word node depth -- )
|
2011-11-01 22:39:22 -04:00
|
|
|
depth depth. node times. ": " write word pprint-short nl
|
2011-11-01 16:10:58 -04:00
|
|
|
node children>> depth 1 + (profile.) ;
|
|
|
|
|
|
|
|
: (profile.) ( nodes depth -- )
|
|
|
|
[ by-total-time ] dip '[ _ (profile-node.) ] assoc-each ;
|
|
|
|
|
|
|
|
: profile. ( tree -- )
|
2011-11-01 20:52:35 -04:00
|
|
|
[ 0 (profile-node.) ] assoc-each ;
|