File:  [gforth] / gforth / profile.fs
Revision 1.2: download - view: text, annotated - select for diffs
Mon Aug 23 14:03:52 2004 UTC (19 years, 6 months ago) by anton
Branches: MAIN
CVS tags: HEAD
added BEFORE-WORD and OTHER-CONTROL-FLOW hooks
changed profile.fs to use BEFORE-WORD hook and profile various control structs
added count-calls? stuff to profile.fs
minor changes

    1: \ count execution of control-flow edges
    2: 
    3: \ Copyright (C) 2004 Free Software Foundation, Inc.
    4: 
    5: \ This file is part of Gforth.
    6: 
    7: \ Gforth is free software; you can redistribute it and/or
    8: \ modify it under the terms of the GNU General Public License
    9: \ as published by the Free Software Foundation; either version 2
   10: \ of the License, or (at your option) any later version.
   11: 
   12: \ This program is distributed in the hope that it will be useful,
   13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
   14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15: \ GNU General Public License for more details.
   16: 
   17: \ You should have received a copy of the GNU General Public License
   18: \ along with this program; if not, write to the Free Software
   19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   20: 
   21: 
   22: \ relies on some Gforth internals
   23: 
   24: \ !! assumption: each file is included only once; otherwise you get
   25: \ the counts for just one of the instances of the file.  This can be
   26: \ fixed by making sure that every source position occurs only once as
   27: \ a profile point.
   28: 
   29: true constant count-calls? \ do some profiling of colon definitions etc.
   30: 
   31: struct
   32:     cell%    field profile-next
   33:     cell% 2* field profile-count
   34:     cell% 2* field profile-sourcepos
   35:     cell%    field profile-char \ character position in line
   36:     count-calls? [if]
   37: 	cell% field profile-colondef? \ is this a colon definition
   38: 	cell% field profile-calls \ static calls to the colon def
   39: 	cell% field profile-straight-line \ may contain calls, but no other CF
   40: 	cell% field profile-calls-from \ static calls in the colon def
   41:     [endif]
   42: end-struct profile% \ profile point
   43: 
   44: variable profile-points \ linked list of profile%
   45: 0 profile-points !
   46: variable next-profile-point-p \ the address where the next pp will be stored
   47: profile-points next-profile-point-p !
   48: count-calls? [if]
   49:     variable last-colondef-profile \ pointer to the pp of last colon definition
   50: [endif]
   51:     
   52: : new-profile-point ( -- addr )
   53:     profile% %alloc >r
   54:     0. r@ profile-count 2!
   55:     current-sourcepos r@ profile-sourcepos 2!
   56:     >in @ r@ profile-char !
   57:     [ count-calls? ] [if]
   58: 	r@ profile-colondef? off
   59: 	0 r@ profile-calls !
   60: 	r@ profile-straight-line on
   61: 	0 r@ profile-calls-from !
   62:     [endif]
   63:     0 r@ profile-next !
   64:     r@ next-profile-point-p @ !
   65:     r@ profile-next next-profile-point-p !
   66:     r> ;
   67: 
   68: : print-profile ( -- )
   69:     profile-points @ begin
   70: 	dup while
   71: 	    dup >r
   72: 	    r@ profile-sourcepos 2@ .sourcepos ." :"
   73: 	    r@ profile-char @ 0 .r ." : "
   74: 	    r@ profile-count 2@ 0 d.r cr
   75: 	    r> profile-next @
   76:     repeat
   77:     drop ;
   78: 
   79: : print-profile-coldef ( -- )
   80:     profile-points @ begin
   81: 	dup while
   82: 	    dup >r
   83: 	    r@ profile-colondef? @ if
   84: 		r@ profile-sourcepos 2@ .sourcepos ." :"
   85: 		r@ profile-char @ 0 .r ." : "
   86: 		r@ profile-count 2@ 0 d.r
   87: 		r@ profile-straight-line @ space .
   88: 		cr
   89: 	    endif
   90: 	    r> profile-next @
   91:     repeat
   92:     drop ;
   93: 
   94: 
   95: : dinc ( d-addr -- )
   96:     \ increment double pointed to by d-addr
   97:     dup 2@ 1. d+ rot 2! ;
   98: 
   99: : profile-this ( -- )
  100:     new-profile-point profile-count POSTPONE literal POSTPONE dinc ;
  101: 
  102: \ Various words trigger PROFILE-THIS.  In order to avoid getting
  103: \ several calls to PROFILE-THIS from a compiling word (like ?EXIT), we
  104: \ just wait until the next word is parsed by the text interpreter (in
  105: \ compile state) and call PROFILE-THIS only once then.  The whole
  106: \ BEFORE-WORD hooking etc. is there for this.
  107: 
  108: \ The reason that we do this is because we use the source position for
  109: \ the profiling information, and there's only one source position for
  110: \ ?EXIT.  If we used the threaded code position instead, we would see
  111: \ that ?EXIT compiles to several threaded-code words, and could use
  112: \ different profile points for them.  However, usually dealing with
  113: \ the source is more practical.
  114: 
  115: \ Another benefit is that we can ask for profiling anywhere in a
  116: \ control-flow word (even before it compiles its own stuff).
  117: 
  118: \ Potential problem: Consider "COMPILING ] [" where COMPILING compiles
  119: \ a whole colon definition (and triggers our profiler), but during the
  120: \ compilation of the colon definition there is no parsing.  Afterwards
  121: \ you get interpret state at first (no profiling, either), but after
  122: \ the "]" you get parsing in compile state, and PROFILE-THIS gets
  123: \ called (and compiles code that is never executed).  It would be
  124: \ better if we had a way of knowing whether we are in a colon def or
  125: \ not (and used that knowledge instead of STATE).
  126: 
  127: Defer before-word-profile ( -- )
  128: ' noop IS before-word-profile
  129: 
  130: : before-word1 ( -- )
  131:     before-word-profile defers before-word ;
  132: 
  133: ' before-word1 IS before-word
  134: 
  135: : profile-this-compiling ( -- )
  136:     state @ if
  137: 	profile-this
  138: 	['] noop IS before-word-profile
  139:     endif ;
  140: 
  141: : cock-profiler ( -- )
  142:     \ as in cock the gun - pull the trigger
  143:     ['] profile-this-compiling IS before-word-profile
  144:     [ count-calls? ] [if] \ we are at a non-colondef profile point
  145: 	last-colondef-profile @ profile-straight-line off
  146:     [endif]
  147: ;
  148: 
  149: : hook-profiling-into ( "name" -- )
  150:     \ make (deferred word) "name" call cock-profiler, too
  151:     ' >body >r :noname
  152:     POSTPONE cock-profiler
  153:     r@ @ compile, \ old hook behaviour
  154:     POSTPONE ;
  155:     r> ! ; \ change hook behaviour
  156: 
  157: hook-profiling-into then-like
  158: \ hook-profiling-into if-like
  159: \ hook-profiling-into ahead-like
  160: hook-profiling-into other-control-flow
  161: hook-profiling-into begin-like
  162: hook-profiling-into again-like
  163: hook-profiling-into until-like
  164: 
  165: count-calls? [if]
  166:     : :-hook-profile ( -- )
  167: 	defers :-hook
  168: 	next-profile-point-p @
  169: 	profile-this
  170: 	@ dup last-colondef-profile !
  171: 	profile-colondef? on ;
  172: 
  173:     ' :-hook-profile IS :-hook
  174: [else]
  175:     hook-profiling-into exit-like
  176:     hook-profiling-into :-hook
  177: [endif]

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>