1 ;;; elp.el --- Emacs Lisp Profiler
3 ;; Copyright (C) 1994,1995,1997,1998, 2001 Free Software Foundation, Inc.
5 ;; Author: Barry A. Warsaw
6 ;; Maintainer: XEmacs Development Team <xemacs-beta@xemacs.org>
7 ;; Created: 26-Feb-1994
8 ;; Keywords: debugging lisp tools
10 ;; This file is part of XEmacs.
12 ;; XEmacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; XEmacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with XEmacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Synched with GNU Emacs 21.1 2001-12-25 Simon Josefsson <jas@extundo.com>
31 ;; If you want to profile a bunch of functions, set elp-function-list
32 ;; to the list of symbols, then do a M-x elp-instrument-list. This
33 ;; hacks those functions so that profiling information is recorded
34 ;; whenever they are called. To print out the current results, use
35 ;; M-x elp-results. If you want output to go to standard-output
36 ;; instead of a separate buffer, setq elp-use-standard-output to
37 ;; non-nil. With elp-reset-after-results set to non-nil, profiling
38 ;; information will be reset whenever the results are displayed. You
39 ;; can also reset all profiling info at any time with M-x
42 ;; You can also instrument all functions in a package, provided that
43 ;; the package follows the GNU coding standard of a common textual
44 ;; prefix. Use M-x elp-instrument-package for this.
46 ;; If you want to sort the results, set elp-sort-by-function to some
47 ;; predicate function. The three most obvious choices are predefined:
48 ;; elp-sort-by-call-count, elp-sort-by-average-time, and
49 ;; elp-sort-by-total-time. Also, you can prune from the output, all
50 ;; functions that have been called fewer than a given number of times
51 ;; by setting elp-report-limit.
53 ;; Elp can instrument byte-compiled functions just as easily as
54 ;; interpreted functions, but it cannot instrument macros. However,
55 ;; when you redefine a function (e.g. with eval-defun), you'll need to
56 ;; re-instrument it with M-x elp-instrument-function. This will also
57 ;; reset profiling information for that function. Elp can handle
58 ;; interactive functions (i.e. commands), but of course any time spent
59 ;; idling for user prompts will show up in the timing results.
61 ;; You can also designate a `master' function. Profiling times will
62 ;; be gathered for instrumented functions only during execution of
63 ;; this master function. Thus, if you have some defuns like:
65 ;; (defun foo () (do-something-time-intensive))
66 ;; (defun bar () (foo))
67 ;; (defun baz () (bar) (foo))
69 ;; and you want to find out the amount of time spent in bar and foo,
70 ;; but only during execution of bar, make bar the master. The call of
71 ;; foo from baz will not add to foo's total timing sums. Use M-x
72 ;; elp-set-master and M-x elp-unset-master to utilize this feature.
73 ;; Only one master function can be set at a time.
75 ;; You can restore any function's original function definition with
76 ;; elp-restore-function. The other instrument, restore, and reset
77 ;; functions are provided for symmetry.
79 ;; Here is a list of variable you can use to customize elp:
81 ;; elp-reset-after-results
82 ;; elp-sort-by-function
85 ;; Here is a list of the interactive commands you can use:
86 ;; elp-instrument-function
87 ;; elp-restore-function
88 ;; elp-instrument-list
90 ;; elp-instrument-package
99 ;; Note that there are plenty of factors that could make the times
100 ;; reported unreliable, including the accuracy and granularity of your
101 ;; system clock, and the overhead spent in lisp calculating and
102 ;; recording the intervals. I figure the latter is pretty constant,
103 ;; so while the times may not be entirely accurate, I think they'll
104 ;; give you a good feel for the relative amount of work spent in the
105 ;; various lisp routines you are profiling. Note further that times
106 ;; are calculated using wall-clock time, so other system load will
107 ;; affect accuracy too.
111 ;; This program was inspired by the only two existing Emacs Lisp
112 ;; profilers that I'm aware of, Boaz Ben-Zvi's profile.el, and Root
113 ;; Boy Jim's profiler.el. Both were written for Emacs 18 and both were
114 ;; pretty good first shots at profiling, but I found that they didn't
115 ;; provide the functionality or interface that I wanted, so I wrote
116 ;; this. I've tested elp in XEmacs 19 and Emacs 19. There's no point
117 ;; in even trying to make this work with Emacs 18.
119 ;; Unlike previous profilers, elp uses Emacs 19's built-in function
120 ;; current-time to return interval times. This obviates the need for
121 ;; both an external C program and Emacs processes to communicate with
122 ;; such a program, and thus simplifies the package as a whole.
125 ;; Make this act like a real profiler, so that it records time spent
126 ;; in all branches of execution.
131 ;; start of user configuration variables
132 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
135 "Emacs Lisp Profiler"
138 (defcustom elp-function-list nil
139 "*List of functions to profile.
140 Used by the command `elp-instrument-list'."
141 :type '(repeat function)
144 (defcustom elp-reset-after-results t
145 "*Non-nil means reset all profiling info after results are displayed.
146 Results are displayed with the `elp-results' command."
150 (defcustom elp-sort-by-function 'elp-sort-by-total-time
151 "*Non-nil specifies elp results sorting function.
152 These functions are currently available:
154 elp-sort-by-call-count -- sort by the highest call count
155 elp-sort-by-total-time -- sort by the highest total time
156 elp-sort-by-average-time -- sort by the highest average times
158 You can write you're own sort function. It should adhere to the
159 interface specified by the PRED argument for the `sort' defun. Each
160 \"element of LIST\" is really a 4 element vector where element 0 is
161 the call count, element 1 is the total time spent in the function,
162 element 2 is the average time spent in the function, and element 3 is
163 the symbol's name string."
167 (defcustom elp-report-limit 1
168 "*Prevents some functions from being displayed in the results buffer.
169 If a number, no function that has been called fewer than that number
170 of times will be displayed in the output buffer. If nil, all
171 functions will be displayed."
172 :type '(choice integer
173 (const :tag "Show All" nil))
176 (defcustom elp-use-standard-output nil
177 "*Non-nil says to output to `standard-output' instead of a buffer."
181 (defcustom elp-recycle-buffers-p t
182 "*nil says to not recycle the `elp-results-buffer'.
183 In other words, a new unique buffer is create every time you run
189 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
190 ;; end of user configuration variables
193 (defvar elp-results-buffer "*ELP Profiling Results*"