File:  [gforth] / gforth / profile.fs
Revision 1.5: download - view: text, annotated - select for diffs
Mon Dec 31 19:02:24 2007 UTC (11 years, 7 months ago) by anton
Branches: MAIN
CVS tags: v0-7-0, HEAD
updated copyright year after changing license notice

    1: \ count execution of control-flow edges
    2: 
    3: \ Copyright (C) 2004,2007 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 3
   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, see http://www.gnu.org/licenses/.
   19: 
   20: 
   21: \ relies on some Gforth internals
   22: 
   23: \ !! assumption: each file is included only once; otherwise you get
   24: \ the counts for just one of the instances of the file.  This can be
   25: \ fixed by making sure that every source position occurs only once as
   26: \ a profile point.
   27: 
   28: true constant count-calls? \ do some profiling of colon definitions etc.
   29: 
   30: \ for true COUNT-CALLS?:
   31: 
   32: \ What data do I need for evaluating the effectiveness of (partial) inlining?
   33: 
   34: \ static and dynamic counts of everything:
   35: 
   36: \ original BB length (histogram and average)
   37: \ BB length with partial inlining (histogram and average)
   38: \   since we cannot partially inline library calls, we use a parameter
   39: \   that represents the amount of partial inlining we can expect there.
   40: \ number of tail calls (original and after partial inlining)
   41: \ number of calls (original and after partial inlining)
   42: \ reason for BB end: call, return, execute, branch
   43: 
   44: \ how many static calls are there to a word?  How many of the dynamic
   45: \ calls call just a single word?
   46: 
   47: struct
   48:     cell%    field profile-next
   49:     cell% 2* field profile-count
   50:     cell% 2* field profile-sourcepos
   51:     cell%    field profile-char \ character position in line
   52:     count-calls? [if]
   53: 	cell% field profile-colondef? \ is this a colon definition start
   54: 	cell% field profile-calls \ static calls to the colon def
   55: 	cell% field profile-straight-line \ may contain calls, but no other CF
   56: 	cell% field profile-calls-from \ static calls in the colon def
   57:     [endif]
   58: end-struct profile% \ profile point
   59: 
   60: variable profile-points \ linked list of profile%
   61: 0 profile-points !
   62: variable next-profile-point-p \ the address where the next pp will be stored
   63: profile-points next-profile-point-p !
   64: count-calls? [if]
   65:     variable last-colondef-profile \ pointer to the pp of last colon definition
   66: [endif]
   67:     
   68: : new-profile-point ( -- addr )
   69:     profile% %alloc >r
   70:     0. r@ profile-count 2!
   71:     current-sourcepos r@ profile-sourcepos 2!
   72:     >in @ r@ profile-char !
   73:     [ count-calls? ] [if]
   74: 	r@ profile-colondef? off
   75: 	0 r@ profile-calls !
   76: 	r@ profile-straight-line on
   77: 	0 r@ profile-calls-from !
   78:     [endif]
   79:     0 r@ profile-next !
   80:     r@ next-profile-point-p @ !
   81:     r@ profile-next next-profile-point-p !
   82:     r> ;
   83: 
   84: : print-profile ( -- )
   85:     profile-points @ begin
   86: 	dup while
   87: 	    dup >r
   88: 	    r@ profile-sourcepos 2@ .sourcepos ." :"
   89: 	    r@ profile-char @ 0 .r ." : "
   90: 	    r@ profile-count 2@ 0 d.r cr
   91: 	    r> profile-next @
   92:     repeat
   93:     drop ;
   94: 
   95: : print-profile-coldef ( -- )
   96:     profile-points @ begin
   97: 	dup while
   98: 	    dup >r
   99: 	    r@ profile-colondef? @ if
  100: 		r@ profile-sourcepos 2@ .sourcepos ." :"
  101: 		r@ profile-char @ 0 .r ." : "
  102: 		r@ profile-count 2@ 0 d.r
  103: 		r@ profile-straight-line @ space .
  104: 		cr
  105: 	    endif
  106: 	    r> profile-next @
  107:     repeat
  108:     drop ;
  109: 
  110: 
  111: : dinc ( d-addr -- )
  112:     \ increment double pointed to by d-addr
  113:     dup 2@ 1. d+ rot 2! ;
  114: 
  115: : profile-this ( -- )
  116:     new-profile-point profile-count POSTPONE literal POSTPONE dinc ;
  117: 
  118: \ Various words trigger PROFILE-THIS.  In order to avoid getting
  119: \ several calls to PROFILE-THIS from a compiling word (like ?EXIT), we
  120: \ just wait until the next word is parsed by the text interpreter (in
  121: \ compile state) and call PROFILE-THIS only once then.  The whole
  122: \ BEFORE-WORD hooking etc. is there for this.
  123: 
  124: \ The reason that we do this is because we use the source position for
  125: \ the profiling information, and there's only one source position for
  126: \ ?EXIT.  If we used the threaded code position instead, we would see
  127: \ that ?EXIT compiles to several threaded-code words, and could use
  128: \ different profile points for them.  However, usually dealing with
  129: \ the source is more practical.
  130: 
  131: \ Another benefit is that we can ask for profiling anywhere in a
  132: \ control-flow word (even before it compiles its own stuff).
  133: 
  134: \ Potential problem: Consider "COMPILING ] [" where COMPILING compiles
  135: \ a whole colon definition (and triggers our profiler), but during the
  136: \ compilation of the colon definition there is no parsing.  Afterwards
  137: \ you get interpret state at first (no profiling, either), but after
  138: \ the "]" you get parsing in compile state, and PROFILE-THIS gets
  139: \ called (and compiles code that is never executed).  It would be
  140: \ better if we had a way of knowing whether we are in a colon def or
  141: \ not (and used that knowledge instead of STATE).
  142: 
  143: Defer before-word-profile ( -- )
  144: ' noop IS before-word-profile
  145: 
  146: : before-word1 ( -- )
  147:     before-word-profile defers before-word ;
  148: 
  149: ' before-word1 IS before-word
  150: 
  151: : profile-this-compiling ( -- )
  152:     state @ if
  153: 	profile-this
  154: 	['] noop IS before-word-profile
  155:     endif ;
  156: 
  157: : cock-profiler ( -- )
  158:     \ as in cock the gun - pull the trigger
  159:     ['] profile-this-compiling IS before-word-profile
  160:     [ count-calls? ] [if] \ we are at a non-colondef profile point
  161: 	last-colondef-profile @ profile-straight-line off
  162:     [endif]
  163: ;
  164: 
  165: : hook-profiling-into ( "name" -- )
  166:     \ make (deferred word) "name" call cock-profiler, too
  167:     ' >body >r :noname
  168:     POSTPONE cock-profiler
  169:     r@ @ compile, \ old hook behaviour
  170:     POSTPONE ;
  171:     r> ! ; \ change hook behaviour
  172: 
  173: hook-profiling-into then-like
  174: \ hook-profiling-into if-like    \ subsumed by other-control-flow
  175: \ hook-profiling-into ahead-like \ subsumed by other-control-flow
  176: hook-profiling-into other-control-flow
  177: hook-profiling-into begin-like
  178: hook-profiling-into again-like
  179: hook-profiling-into until-like
  180: 
  181: count-calls? [if]
  182:     : :-hook-profile ( -- )
  183: 	defers :-hook
  184: 	next-profile-point-p @
  185: 	profile-this
  186: 	@ dup last-colondef-profile !
  187: 	profile-colondef? on ;
  188: 
  189:     ' :-hook-profile IS :-hook
  190: [else]
  191:     hook-profiling-into exit-like
  192:     hook-profiling-into :-hook
  193: [endif]

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