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
|
2011-11-02 02:40:46 -04:00
|
|
|
combinators.short-circuit continuations fry generalizations
|
|
|
|
hashtables.identity io kernel kernel.private locals math
|
|
|
|
math.statistics math.vectors memory namespaces prettyprint
|
|
|
|
sequences sequences.generalizations sets sorting
|
2011-11-03 02:57:15 -04:00
|
|
|
tools.profiler.sampling.private math.parser.private
|
|
|
|
math.parser ;
|
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
|
|
|
|
|
2011-11-01 23:24:17 -04:00
|
|
|
samples-per-second [ 1,000 ] initialize
|
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-11-01 23:18:52 -04:00
|
|
|
: ignore-word? ( word -- ? ) ignore-words member? ; inline
|
|
|
|
|
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 23:24:17 -04:00
|
|
|
: profile ( quot -- )
|
|
|
|
samples-per-second get-global profiling
|
2011-11-02 20:01:43 -04:00
|
|
|
[ 0 profiling (get-samples) raw-profile-data set-global ]
|
|
|
|
[ ] cleanup ; inline
|
2011-10-31 21:59:48 -04:00
|
|
|
|
2011-11-02 02:40:46 -04:00
|
|
|
: total-sample-count ( sample -- count ) 0 swap nth ;
|
|
|
|
: gc-sample-count ( sample -- count ) 1 swap nth ;
|
|
|
|
: jit-sample-count ( sample -- count ) 2 swap nth ;
|
|
|
|
: foreign-sample-count ( sample -- count ) 3 swap nth ;
|
|
|
|
: foreign-thread-sample-count ( sample -- count ) 4 swap nth ;
|
|
|
|
: sample-counts-slice ( sample -- counts ) 5 head-slice ;
|
|
|
|
|
|
|
|
: sample-thread ( sample -- alien ) 5 swap nth ;
|
|
|
|
: sample-callstack ( sample -- array ) 6 swap nth ;
|
|
|
|
: unclip-callstack ( sample -- sample' callstack-top )
|
|
|
|
clone 6 over [ unclip swap ] change-nth ;
|
2011-10-31 21:59:48 -04:00
|
|
|
|
|
|
|
: 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 23:28:17 -04:00
|
|
|
: collect-threads ( samples -- by-thread )
|
2011-11-01 22:39:22 -04:00
|
|
|
[ 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
|
|
|
|
2011-11-01 23:18:52 -04:00
|
|
|
: leaf-callstack? ( callstack -- ? )
|
|
|
|
[ ignore-word? ] all? ;
|
|
|
|
|
2011-11-02 02:40:46 -04:00
|
|
|
CONSTANT: zero-counts { 0 0 0 0 0 }
|
|
|
|
|
|
|
|
: sum-counts ( samples -- times )
|
|
|
|
zero-counts [ sample-counts-slice v+ ] reduce ;
|
2011-11-01 23:18:52 -04:00
|
|
|
|
2011-11-01 16:10:58 -04:00
|
|
|
TUPLE: profile-node
|
2011-11-03 02:57:15 -04:00
|
|
|
total-time gc-time jit-time foreign-time foreign-thread-time children
|
|
|
|
depth ;
|
2011-11-01 16:10:58 -04:00
|
|
|
|
2011-11-03 02:57:15 -04:00
|
|
|
: <profile-node> ( times children depth -- node )
|
|
|
|
[ 5 firstn [ samples>time ] 5 napply ] 2dip profile-node boa ;
|
2011-11-01 20:52:35 -04:00
|
|
|
|
2011-11-01 23:18:52 -04:00
|
|
|
: <profile-root-node> ( samples collector-quot -- node )
|
2011-11-03 02:57:15 -04:00
|
|
|
[ sum-counts ] swap bi 0 <profile-node> ; inline
|
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
|
|
|
|
2011-11-03 02:57:15 -04:00
|
|
|
:: collect-tops ( samples depth -- node )
|
|
|
|
samples [ unclip-callstack ] collect-pairs [
|
2011-11-02 02:40:46 -04:00
|
|
|
[ sum-counts ]
|
2011-11-03 02:57:15 -04:00
|
|
|
[ [ depth 1 + collect-tops ] (collect-subtrees) ] bi depth <profile-node>
|
2011-11-01 20:52:35 -04:00
|
|
|
] 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
|
|
|
|
2011-11-03 02:57:15 -04:00
|
|
|
:: (top-down) ( samples depth -- tree )
|
|
|
|
samples collect-threads
|
|
|
|
[ [ depth collect-tops ] <profile-root-node> trim-root ] assoc-map ;
|
2011-11-01 16:10:58 -04:00
|
|
|
|
|
|
|
: top-down ( -- tree )
|
2011-11-03 02:57:15 -04:00
|
|
|
get-raw-profile-data 0 (top-down) ;
|
|
|
|
|
|
|
|
:: counts+at ( key assoc sample -- )
|
|
|
|
key assoc [ zero-counts or sample sample-counts-slice v+ ] change-at ;
|
2011-11-01 16:10:58 -04:00
|
|
|
|
2011-11-01 22:39:22 -04:00
|
|
|
:: collect-flat ( samples -- flat )
|
|
|
|
IH{ } clone :> per-word-samples
|
|
|
|
samples [| sample |
|
2011-11-01 23:18:52 -04:00
|
|
|
sample sample-callstack unique keys [ ignore-word? not ] filter [
|
2011-11-03 02:57:15 -04:00
|
|
|
per-word-samples sample counts+at
|
2011-11-01 22:39:22 -04:00
|
|
|
] each
|
|
|
|
] each
|
2011-11-03 02:57:15 -04:00
|
|
|
per-word-samples [ f 0 <profile-node> ] assoc-map ;
|
2011-11-01 22:39:22 -04:00
|
|
|
|
2011-11-01 23:18:52 -04:00
|
|
|
: redundant-flat-node? ( child-node root-node -- ? )
|
|
|
|
[ total-time>> ] bi@ = ;
|
|
|
|
|
|
|
|
: trim-flat ( root-node -- root-node' )
|
|
|
|
dup '[ [ nip _ redundant-flat-node? not ] assoc-filter ] change-children ;
|
|
|
|
|
2011-11-01 22:39:22 -04:00
|
|
|
: (flat) ( samples -- flat )
|
2011-11-01 23:18:52 -04:00
|
|
|
collect-threads
|
|
|
|
[ [ collect-flat ] <profile-root-node> trim-flat ] assoc-map ;
|
2011-11-01 22:39:22 -04:00
|
|
|
|
|
|
|
: flat ( -- tree )
|
|
|
|
get-raw-profile-data (flat) ;
|
|
|
|
|
2011-11-03 02:57:15 -04:00
|
|
|
: nth-or-last ( n seq -- elt )
|
|
|
|
[ drop f ] [
|
|
|
|
2dup bounds-check? [ nth ] [ nip last ] if
|
|
|
|
] if-empty ;
|
|
|
|
|
|
|
|
:: collect-cross-section ( samples depth -- cross-section )
|
|
|
|
IH{ } clone :> per-word-samples
|
|
|
|
samples [| sample |
|
|
|
|
depth sample sample-callstack [ ignore-word? ] trim-tail nth-or-last :> word
|
|
|
|
word [
|
|
|
|
word per-word-samples sample counts+at
|
|
|
|
] when
|
|
|
|
] each
|
|
|
|
per-word-samples [ f depth <profile-node> ] assoc-map ;
|
|
|
|
|
|
|
|
:: (cross-section) ( depth samples -- flat )
|
|
|
|
samples collect-threads
|
|
|
|
[ [ depth collect-cross-section ] <profile-root-node> ] assoc-map ;
|
|
|
|
|
|
|
|
: cross-section ( depth -- tree )
|
|
|
|
get-raw-profile-data (cross-section) ;
|
|
|
|
|
2011-11-01 16:10:58 -04:00
|
|
|
: depth. ( depth -- )
|
2011-11-03 00:40:31 -04:00
|
|
|
[ "| " write ] times ;
|
2011-11-01 16:10:58 -04:00
|
|
|
|
|
|
|
: by-total-time ( nodes -- nodes' )
|
|
|
|
>alist [ second total-time>> ] inv-sort-with ;
|
|
|
|
|
|
|
|
: duration. ( duration -- )
|
2011-11-03 00:40:31 -04:00
|
|
|
duration>milliseconds >float "%9.1fms" format-float write ;
|
|
|
|
|
|
|
|
: percentage. ( num denom -- )
|
|
|
|
[ duration>seconds ] bi@ [ 100 * ] dip /f "%6.2f%%" format-float write ;
|
2011-11-01 16:10:58 -04:00
|
|
|
|
|
|
|
DEFER: (profile.)
|
|
|
|
|
2011-11-03 02:57:15 -04:00
|
|
|
:: times. ( node depth -- )
|
|
|
|
node {
|
|
|
|
[ depth>> number>string 3 CHAR: \s pad-head write " " write depth depth. ]
|
2011-11-02 02:40:46 -04:00
|
|
|
[ total-time>> duration. ]
|
2011-11-03 00:40:31 -04:00
|
|
|
[ " (GC:" write [ gc-time>> ] [ total-time>> ] bi percentage. ]
|
|
|
|
[ ", JIT:" write [ jit-time>> ] [ total-time>> ] bi percentage. ]
|
|
|
|
[ ", FFI:" write [ foreign-time>> ] [ total-time>> ] bi percentage. ]
|
|
|
|
[ ", FT:" write [ foreign-thread-time>> ] [ total-time>> ] bi percentage. ")" write ]
|
2011-11-01 20:52:35 -04:00
|
|
|
} cleave ;
|
|
|
|
|
2011-11-01 16:10:58 -04:00
|
|
|
:: (profile-node.) ( word node depth -- )
|
2011-11-03 02:57:15 -04:00
|
|
|
node depth 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 ;
|