Annotation of gforth/profile.fs, revision 1.3

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

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