--- gforth/prof-inline.fs 2004/09/06 13:44:56 1.3 +++ gforth/prof-inline.fs 2004/09/07 09:59:01 1.5 @@ -77,7 +77,8 @@ variable next-profile-point-p \ the addr profile-points next-profile-point-p ! variable last-colondef-profile \ pointer to the pp of last colon definition variable current-profile-point -variable library-calls \ list of calls to library colon defs +variable library-calls 0 library-calls ! \ list of calls to library colon defs +variable in-compile,? in-compile,? off \ list stuff @@ -169,35 +170,46 @@ variable library-calls \ list of calls t : call-count+ ( ud1 callp -- ud2 ) calls-call @ profile-count 2@ d+ ; -: add-calls ( ud-dyn-callee1 ud-dyn-caller1 u-stat1 xt-test profpp -- - ud-dyn-callee2 ud-dyn-caller2 u-stat2 xt-test ) - \ add the static and dynamic call counts to profpp up, if the - \ number of static calls to profpp satisfies xt-test ( u -- f ) +: count-dyncalls ( calls -- ud ) + 0. rot ['] call-count+ map-list ; + +: add-calls ( statistics1 xt-test profpp -- statistics2 xt-test ) + \ add statistics for callee profpp up, if the number of static + \ calls to profpp satisfies xt-test ( u -- f ); see below for what + \ statistics are computed. { xt-test p } - p profile-colondef? @ if ( u-dyn1 u-stat1 ) + p profile-colondef? @ if p profile-calls @ { calls } calls list-length { stat } - stat xt-test execute if ( u-dyn u-stat ) - stat + >r - 0. calls ['] call-count+ map-list d+ 2>r - p profile-count 2@ d+ - 2r> r> + stat xt-test execute if + { d: ud-dyn-callee d: ud-dyn-caller u-stat u-exec-callees u-callees } + ud-dyn-callee p profile-count 2@ 2dup { d: de } d+ + ud-dyn-caller calls count-dyncalls 2dup { d: dr } d+ + u-stat stat + + u-exec-callees de dr d<> - + u-callees 1+ endif endif xt-test ; : print-stat-line ( xt -- ) - >r 0. 0. 0 r> profile-points @ ['] add-calls map-list drop + >r 0. 0. 0 0 0 r> profile-points @ ['] add-calls map-list drop ( ud-dyn-callee ud-dyn-caller u-stat ) - 7 u.r 12 ud.r 12 ud.r space ; + 6 u.r 7 u.r 7 u.r 12 ud.r 12 ud.r space ; + +: print-library-stats ( -- ) + library-calls @ list-length 20 u.r \ static callers + library-calls @ count-dyncalls 12 ud.r \ dynamic callers + 13 spaces ; : print-statistics ( -- ) - ." static dyn-caller dyn-callee condition" cr + ." callee exec'd static dyn-caller dyn-callee condition" cr ['] 0= print-stat-line ." calls to coldefs with 0 callers" cr ['] 1= print-stat-line ." calls to coldefs with 1 callers" cr ['] 2= print-stat-line ." calls to coldefs with 2 callers" cr ['] 3= print-stat-line ." calls to coldefs with 3 callers" cr ['] 1u> print-stat-line ." calls to coldefs with >1 callers" cr + print-library-stats ." library calls" cr ; : dinc ( profilep -- ) @@ -205,7 +217,9 @@ variable library-calls \ list of calls t profile-count dup 2@ 1. d+ rot 2! ; : profile-this ( -- ) - new-profile-point POSTPONE literal POSTPONE dinc ; + in-compile,? @ in-compile,? on + new-profile-point POSTPONE literal POSTPONE dinc + in-compile,? ! ; \ Various words trigger PROFILE-THIS. In order to avoid getting \ several calls to PROFILE-THIS from a compiling word (like ?EXIT), we @@ -232,32 +246,32 @@ variable library-calls \ list of calls t \ better if we had a way of knowing whether we are in a colon def or \ not (and used that knowledge instead of STATE). -Defer before-word-profile ( -- ) -' noop IS before-word-profile +\ Defer before-word-profile ( -- ) +\ ' noop IS before-word-profile -: before-word1 ( -- ) - before-word-profile defers before-word ; +\ : before-word1 ( -- ) +\ before-word-profile defers before-word ; -' before-word1 IS before-word +\ ' before-word1 IS before-word -: profile-this-compiling ( -- ) - state @ if - profile-this - ['] noop IS before-word-profile - endif ; - -: cock-profiler ( -- ) - \ as in cock the gun - pull the trigger - ['] profile-this-compiling IS before-word-profile - [ count-calls? ] [if] \ we are at a non-colondef profile point - last-colondef-profile @ profile-straight-line off - [endif] -; +\ : profile-this-compiling ( -- ) +\ state @ if +\ profile-this +\ ['] noop IS before-word-profile +\ endif ; + +\ : cock-profiler ( -- ) +\ \ as in cock the gun - pull the trigger +\ ['] profile-this-compiling IS before-word-profile +\ [ count-calls? ] [if] \ we are at a non-colondef profile point +\ last-colondef-profile @ profile-straight-line off +\ [endif] +\ ; : hook-profiling-into ( "name" -- ) \ make (deferred word) "name" call cock-profiler, too ' >body >r :noname - POSTPONE cock-profiler + POSTPONE profile-this r@ @ compile, \ old hook behaviour POSTPONE ; r> ! ; \ change hook behaviour @@ -268,33 +282,32 @@ Defer before-word-profile ( -- ) : note-call ( addr -- ) \ addr is the body address of a called colon def or does handler - dup 3 cells + @ ['] dinc >body = if ( addr ) - current-profile-point @ new-call over cell+ @ profile-calls insert-list + dup ['] (does>2) >body = if \ adjust does handler address + 4 cells here 1 cells - +! endif - drop ; - + profile-this current-profile-point @ new-call + over 3 cells + @ ['] dinc >body = if ( addr call-prof-point ) + \ non-library call + swap cell+ @ profile-calls insert-list + else ( addr call-prof-point ) + library-calls insert-list drop + endif ; + : prof-compile, ( xt -- ) + in-compile,? @ if + DEFERS compile, EXIT + endif dup >does-code if dup >does-code note-call then dup >code-address CASE docol: OF dup >body note-call ENDOF dodefer: OF note-execute ENDOF - dofield: OF >body @ ['] lit+ peephole-compile, , EXIT ENDOF \ dofield: OF >body @ POSTPONE literal ['] + peephole-compile, EXIT ENDOF \ code words and ;code-defined words (code words could be optimized): - dup in-dictionary? IF drop POSTPONE literal ['] execute peephole-compile, EXIT THEN ENDCASE DEFERS compile, ; -\ hook-profiling-into then-like -\ \ hook-profiling-into if-like \ subsumed by other-control-flow -\ \ hook-profiling-into ahead-like \ subsumed by other-control-flow -\ hook-profiling-into other-control-flow -\ hook-profiling-into begin-like -\ hook-profiling-into again-like -\ hook-profiling-into until-like - : :-hook-profile ( -- ) defers :-hook next-profile-point-p @ @@ -302,5 +315,12 @@ Defer before-word-profile ( -- ) @ dup last-colondef-profile ! profile-colondef? on ; +\ hook-profiling-into then-like +\ \ hook-profiling-into if-like \ subsumed by other-control-flow +\ \ hook-profiling-into ahead-like \ subsumed by other-control-flow +\ hook-profiling-into other-control-flow +\ hook-profiling-into begin-like +\ hook-profiling-into again-like +\ hook-profiling-into until-like ' :-hook-profile IS :-hook -' prof-compile, IS compile, \ No newline at end of file +' prof-compile, IS compile,