From: Jason Fleet
Subject: Called me how many times.
Date: 
Message-ID: <01bb90e2$3e276b40$d2d19284@Deskpro5133.info.bt.co.uk>
I am trying to write some lisp that can be used to find out how many times
a function is called at runtime.

I have thought about creating a 'mydefun' function, but this means first
going through the source files and changing the 'defun' statement to
'mydefun'.

The 'mydefun' would add extra functionality to each function as it is
defined.

I do not really want to do it like this, I would rather redefine 'defun' to
avoid chaging the source file.

Has anyone already done this or has anyone got any ideas on how.

I am using 'Allegro CL for windows' - if this helps any.

Thankyou.......
		J
From: Marty Hall
Subject: Re: Called me how many times.
Date: 
Message-ID: <DwLAJC.6sD@aplcenmp.apl.jhu.edu>
In article <··························@Deskpro5133.info.bt.co.uk>
"Jason Fleet" <···········@bt-sys.bt.co.uk> writes: 
>I am trying to write some lisp that can be used to find out how many times
>a function is called at runtime.
[...]
>Has anyone already done this or has anyone got any ideas on how.

I'm sure plenty of people have done this. My version is in
<http://www.apl.jhu.edu/~hall/lisp/Simple-Metering.lisp>. Here's the
top-level macro:

(defmacro With-Function-Call-Count (Function-Name-List &body Body)
  "Takes a list of function names and a body of code, and returns two values:
   (A) the normal return value of the body of code and (B) a list of the
   number of times the functions were called during the execution of the Body.
   Eg:

   (With-Function-Call-Count (Speed Latitude Longitude)
     (Make-Top-Level-Display)
     (Make-MAD-Display))

   returns (867 651 651) as the secondary value, indicating SPEED was called
   867 times, and LATITUDE and LONGITUDE 651 times each during the execution
   of the top-level and MAD displays."
  (let ((Call-Count-Variable (gensym "CALL-COUNT-"))
        (Return-Value (gensym "RETURN-VALUE-")))
    `(let (,Call-Count-Variable ,Return-Value)
       (unwind-protect
           (progn
             (mapc #'Make-Function-Countable ',Function-Name-List)
             (setq ,Return-Value (progn ,@Body))
             (setq ,Call-Count-Variable
                   (mapcar #'(lambda (Function-Name)
                               (get Function-Name :Call-Count))
                           ',Function-Name-List))
             (values ,Return-Value ,Call-Count-Variable) )
         (mapc #'Make-Function-Uncountable ',Function-Name-List) ) )
))

If you're not familiar with UNWIND-PROTECT, GENSYM, etc, the basic
idea is simple: change each function by calling SETF on its SYMBOL
FUNCTION, making the new version just increment a variable then call
the old version. Then collect the values, then reset the function
definitions, then return the values.

BTW, much as I've enjoyed (yes, I know: blasphemy!) working in Java
recently, you sure as heck can't even *dream* of doing this kind of
stuff in Java.
						- Marty

Lisp Resources: <http://www.apl.jhu.edu/~hall/lisp.html>