home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / f / forthmac / !Forthmacs / extend / runtime < prev    next >
Encoding:
Text File  |  1997-02-27  |  5.0 KB  |  142 lines

  1. \ portable RUNTIME PROFILER  Version 1.05 hs10.12.96
  2. \ USE:  -- runtime-profiling of colon definitions.
  3. \ mini manual:
  4. \ 1) load the cpu specific runtimer file, this file should be loaded automatically.
  5. \       fload extend\??cpu??\runtimer.fth
  6. \       fload extend\runtime.fth
  7. \ 2) From now on all colon definitions have an internal time measuring code part which is
  8. \       executed by calling the word.
  9. \ 3) Glossary:
  10. \ new ( -- )
  11. \       clear all timing information
  12. \ .times ( -- )
  13. \       display timing information
  14. \ profile ( -- ) name
  15. \       the word name is executed and the timing takes place. name could be a "benchmark" or
  16. \       the timing critical part of your application.       
  17. \ see   is patched, it will give correct information and knows about the timer information data
  18. \       structure
  19. \
  20. \ 4) example
  21. \ : test1       dup dup dup ;
  22. \ : test2       drop drop drop ;
  23. \ : test3       swap swap swap ;
  24. \ : test4       test1 test3 test2 test1 test3 test2 ;
  25. \ : test5       10000 0 do test4 test4 loop ;
  26. \ profile test5 .times
  27.  
  28. \
  29. \ User words of runtimer Version 1.0
  30. \       new (-- )             set all calls and ticks to 0
  31. \       .times ( -- )         print runtimes
  32. \       profile  name ( -- )  make a runtime profile of word name)
  33. \ Lit.  [1] "Laufzeitmessung von Forthworten", Bernd Pennemann, VD2/87
  34. \       [2] Forthmacs User's Guide, Mitch Bradley/Hanno Schwalm 
  35. \
  36. \ After loading the runtimer every colon definition is linked by an extra
  37. \ link-list for easy finding of all words.
  38.  
  39. \ Structure of new colon definition:
  40. \       timer-link-list long
  41. \       standard forthmacs header
  42. \       apf parameter field address of word
  43. \        0 (time
  44. \        4 calls
  45. \        8 #time
  46. \       12 compiled code continues ......
  47.  
  48. only forth also hidden also definitions  decimal
  49.  
  50. [ifdef]    profile        \ this file shouldn't be compiled twice
  51.         ??cr .( runtimer already loaded)
  52.         cr fexit
  53. [then]
  54.  
  55. [ifndef] du/mod        \ double math library required
  56.     ?cr .( Forthmacs kernel with extended double library required)
  57.     cr fexit
  58. [then]
  59.  
  60. [ifndef] (time        \ defined in cpu-specific file
  61.         ??cr .( runtimer needs cpu-specific timer definition-file first)
  62.         cr fexit
  63. [then]
  64.  
  65. variable used-time
  66. variable timer-link             \ base of link-list bound to 0
  67.         here timer-link link!  0 ,
  68. defer scan-action               \ ( anf -- )
  69.  
  70. : scan-all      \ ( -- ) scan-action is done with all timer-linked words
  71.     timer-link
  72.     begin    link@ dup link@  exit? not and
  73.     while    dup cell+  l>name   scan-action
  74.     repeat    drop ;
  75.  
  76. : ticker-data   \ ( anf -- adr-timer adr-data )
  77.     name> >body cell+ dup cell+ swap ;
  78. : new-data      \ ( anf -- ) Call-nr und Time-nr  = 0
  79.     ticker-data off off  used-time off ;
  80. : legal-data    \ ( anf -- )
  81.     ticker-data drop @ 2-  used-time @ >
  82.     if used-time off then ;
  83. : .percent      \ ( n-ticks -- )
  84.     100 used-time @ */ ?dup if 4 .r else ."   <1" then ." %" ;
  85.  
  86. : .nsec         \ ( d-nsec -- )
  87.     30000000. 2over  du<    if 1000000 um/mod nip        8 u.r ."  msec" exit then
  88.     30000.    2over  du<    if 1000 um/mod nip        8 u.r ."  µsec" exit then
  89.     drop 50      over  u>    if drop ."      <50" else     8 u.r then ."  nsec" ;
  90. : .one-data     \ ( anf -- ) prints data of one word
  91.     dup ticker-data @  swap @ swap
  92.     dup 0=  ( anf #time calls flag )
  93.     if 2drop drop exit then
  94.     rot .id  20 to-column
  95.     2dup 8 .r ."  Calls"    nsec/tick um* .nsec
  96.     ( ticks calls )
  97.     2dup >r nsec/tick  um* r> s>d  du/mod .nsec ." /call" 2drop
  98.     drop  used-time @
  99.     if  .percent else drop then ??cr ;
  100.  
  101. \ support for the decompiler
  102. [ifdef] see
  103.     : .runtime-info        ( ip -- ip' )
  104.         +indent .." \ <-- profiler-information available, "
  105.         cell+ dup @ .d ." calls, " cell+ dup @ .d ." ticks." cell+ -indent ;
  106.     : skip-runtime-info     ( ip -- ip' )
  107.         cell+ cell+ cell+ ;
  108.     ' (time ' .runtime-info  ' skip-runtime-info  install-decomp
  109. [then]
  110.  
  111. : (bye        remove-ticker (bye ;                
  112. : (forget       true abort" forget is not supported with profiler loaded!" ;
  113. : (save-forth   true abort" save-forth is not supported with profiler loaded!" ;
  114.         ' (forget     ' forget >body token!
  115.         ' (save-forth ' save-forth >body token!
  116.         ' (bye is bye
  117. forth definitions
  118.  
  119. variable runtimer  runtimer off     \ on -> the runtimer code is compiled
  120. : new           \ ( -- ) set all ticks and calls to 0
  121.         ['] new-data is scan-action  scan-all ;
  122. : .times        \ ( -- ) prints data of all words
  123.         ??cr cr ." Runtime profiler V 1.05 © 1993,94,96 Hanno Schwalm"
  124.         cr rmargin @ 0 do [char] _ emit loop cr cr
  125.         ['] legal-data is scan-action scan-all
  126.         ['] .one-data is scan-action
  127.         base @ >r decimal scan-all  r> base !
  128.         cr rmargin @ 0 do [char] _ emit loop cr cr ;
  129.  
  130. : profile       \ name ( -- ) make runtime-profile of name
  131.         new '
  132.         get-ticks >r execute get-ticks r> -  used-time ! ;
  133.  
  134. : :             \ name ( -- ) redefined colon, using runtimer
  135.         runtimer @ 0= if :  exit then
  136.         timer-link link-here
  137.         :            \ make a colon definition
  138.         postpone (time 0 , 0 , ; immediate
  139.  
  140. only  forth also  definitions  decimal
  141. runtimer on
  142.