 ! (c)2011 Joe Groff bsd license
-USING: accessors assocs calendar continuations fry io
-kernel kernel.private locals math math.statistics
-math.vectors namespaces prettyprint sequences sorting
+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
 tools.profiler.sampling.private ;
 FROM: sequences => change-nth ;
 IN: tools.profiler.sampling

    >alist [ second total-time>> ] inv-sort-with ;

: duration. ( duration -- )
    duration>seconds >float pprint " " write \ seconds pprint ;

DEFER: (profile.)

:: (profile-node.) ( word node depth -- )
    depth depth. node total-time>> duration. ": " write word pprint nl
    node children>> depth 1 + (profile.) ;

: (profile.) ( nodes depth -- )
    [ by-total-time ] dip '[ _ (profile-node.) ] assoc-each ;

: profile. ( tree -- )
    [ [ "Context: " write pprint nl ] [ 1 (profile.) ] bi* ] assoc-each ;

CONSTANT: default-samples-per-second 1000

CONSTANT: ignore-words
    { signal-handler leaf-signal-handler profiling minor-gc }

: get-raw-profile-data ( -- data )
    raw-profile-data get-global [ "No profile data" throw ] unless* ;
