]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/elp.el
Delete the hilit19 support--it doesn't work.
[gnu-emacs] / lisp / emacs-lisp / elp.el
1 ;;; elp.el --- Emacs Lisp Profiler
2
3 ;; Copyright (C) 1994 Free Software Foundation, Inc.
4
5 ;; Author: 1994 Barry A. Warsaw <bwarsaw@cnri.reston.va.us>
6 ;; Maintainer: tools-help@anthem.nlm.nih.gov
7 ;; Created: 26-Feb-1994
8 ;; Version: 2.23
9 ;; Last Modified: 1994/12/28 22:39:31
10 ;; Keywords: Emacs Lisp Profile Timing
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
28
29 ;;; Commentary:
30 ;;
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. With elp-reset-after-results set to non-nil,
36 ;; profiling information will be reset whenever the results are
37 ;; displayed. You can also reset all profiling info at any time with
38 ;; M-x elp-reset-all.
39 ;;
40 ;; You can also instrument all functions in a package, provided that
41 ;; the package follows the GNU coding standard of a common textural
42 ;; prefix. Use M-x elp-instrument-package for this.
43 ;;
44 ;; If you want to sort the results, set elp-sort-by-function to some
45 ;; predicate function. The three most obvious choices are predefined:
46 ;; elp-sort-by-call-count, elp-sort-by-average-time, and
47 ;; elp-sort-by-total-time. Also, you can prune from the output, all
48 ;; functions that have been called fewer than a given number of times
49 ;; by setting elp-report-limit.
50 ;;
51 ;; Elp can instrument byte-compiled functions just as easily as
52 ;; interpreted functions, but it cannot instrument macros. However,
53 ;; when you redefine a function (e.g. with eval-defun), you'll need to
54 ;; re-instrument it with M-x elp-instrument-function. This will also
55 ;; reset profiling information for that function. Elp can handle
56 ;; interactive functions (i.e. commands), but of course any time spent
57 ;; idling for user prompts will show up in the timing results.
58 ;;
59 ;; You can also designate a `master' function. Profiling times will
60 ;; be gathered for instrumented functions only during execution of
61 ;; this master function. Thus, if you have some defuns like:
62 ;;
63 ;; (defun foo () (do-something-time-intensive))
64 ;; (defun bar () (foo))
65 ;; (defun baz () (bar) (foo))
66 ;;
67 ;; and you want to find out the amount of time spent in bar and foo,
68 ;; but only during execution of bar, make bar the master. The call of
69 ;; foo from baz will not add to foo's total timing sums. Use M-x
70 ;; elp-set-master and M-x elp-unset-master to utilize this feature.
71 ;; Only one master function can be set at a time.
72
73 ;; You can restore any function's original function definition with
74 ;; elp-restore-function. The other instrument, restore, and reset
75 ;; functions are provided for symmetry.
76
77 ;; Note that there are plenty of factors that could make the times
78 ;; reported unreliable, including the accuracy and granularity of your
79 ;; system clock, and the overhead spent in lisp calculating and
80 ;; recording the intervals. The latter I figure is pretty constant
81 ;; so, while the times may not be entirely accurate, I think they'll
82 ;; give you a good feel for the relative amount of work spent in the
83 ;; various lisp routines you are profiling. Note further that times
84 ;; are calculated using wall-clock time, so other system load will
85 ;; affect accuracy too.
86
87 ;; Here is a list of variable you can use to customize elp:
88 ;; elp-function-list
89 ;; elp-reset-after-results
90 ;; elp-sort-by-function
91 ;; elp-report-limit
92 ;;
93 ;; Here is a list of the interactive commands you can use:
94 ;; elp-instrument-function
95 ;; elp-restore-function
96 ;; elp-instrument-list
97 ;; elp-restore-list
98 ;; elp-instrument-package
99 ;; elp-restore-all
100 ;; elp-reset-function
101 ;; elp-reset-list
102 ;; elp-reset-all
103 ;; elp-set-master
104 ;; elp-unset-master
105 ;; elp-results
106 ;; elp-submit-bug-report
107
108 ;; Note that there are plenty of factors that could make the times
109 ;; reported unreliable, including the accuracy and granularity of your
110 ;; system clock, and the overhead spent in lisp calculating and
111 ;; recording the intervals. I figure the latter is pretty constant,
112 ;; so while the times may not be entirely accurate, I think they'll
113 ;; give you a good feel for the relative amount of work spent in the
114 ;; various lisp routines you are profiling. Note further that times
115 ;; are calculated using wall-clock time, so other system load will
116 ;; affect accuracy too. You cannot profile anything longer than ~18
117 ;; hours since I throw away the most significant 16 bits of seconds
118 ;; returned by current-time: 2^16 == 65536 seconds == ~1092 minutes ==
119 ;; ~18 hours. I doubt you will ever want to profile stuff on the
120 ;; order of 18 hours anyway.
121
122 ;;; Background:
123
124 ;; This program is based on the only two existing Emacs Lisp profilers
125 ;; that I'm aware of, Boaz Ben-Zvi's profile.el, and Root Boy Jim's
126 ;; profiler.el. Both were written for Emacs 18 and both were pretty
127 ;; good first shots at profiling, but I found that they didn't provide
128 ;; the functionality or interface that I wanted. So I wrote this.
129 ;; I've tested elp in GNU Emacs 19 and in GNU XEmacs. There's no
130 ;; point in even trying to make this work with Emacs 18.
131
132 ;; Unlike previous profilers, elp uses Emacs 19's built-in function
133 ;; current-time to return interval times. This obviates the need for
134 ;; both an external C program and Emacs processes to communicate with
135 ;; such a program, and thus simplifies the package as a whole.
136
137 ;;; Code:
138
139 \f
140 ;; start user configuration variables
141 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
142
143 (defvar elp-function-list nil
144 "*List of function to profile.")
145
146 (defvar elp-reset-after-results t
147 "*Non-nil means reset all profiling info after results are displayed.
148 Results are displayed with the `elp-results' command.")
149
150 (defvar elp-sort-by-function nil
151 "*Non-nil specifies elp results sorting function.
152 These functions are currently available:
153
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
157
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.")
164
165 (defvar elp-report-limit nil
166 "*Prevents some functions from being displayed in the results buffer.
167 If a number, no function that has been called fewer than that number
168 of times will be displayed in the output buffer. If nil, all
169 functions will be displayed.")
170
171
172 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
173 ;; end user configuration variables
174
175 \f
176 (defconst elp-version "2.23"
177 "ELP version number.")
178
179 (defconst elp-help-address "tools-help@anthem.nlm.nih.gov"
180 "Address accepting submissions of bug reports and questions.")
181
182 (defvar elp-results-buffer "*ELP Profiling Results*"
183 "Buffer name for outputting profiling results.")
184
185 (defconst elp-timer-info-property 'elp-info
186 "ELP information property name.")
187
188 (defvar elp-all-instrumented-list nil
189 "List of all functions currently being instrumented.")
190
191 (defvar elp-record-p t
192 "Controls whether functions should record times or not.
193 This variable is set by the master function.")
194
195 (defvar elp-master nil
196 "Master function symbol.")
197
198 \f
199 ;;;###autoload
200 (defun elp-instrument-function (funsym)
201 "Instrument FUNSYM for profiling.
202 FUNSYM must be a symbol of a defined function."
203 (interactive "aFunction to instrument: ")
204 ;; TBD what should we do if the function is already instrumented???
205 (let* ((funguts (symbol-function funsym))
206 (infovec (vector 0 0 funguts))
207 (newguts '(lambda (&rest args))))
208 ;; we cannot profile macros
209 (and (eq (car-safe funguts) 'macro)
210 (error "ELP cannot profile macro %s" funsym))
211 ;; put rest of newguts together
212 (if (commandp funsym)
213 (setq newguts (append newguts '((interactive)))))
214 (setq newguts (append newguts (list
215 (list 'elp-wrapper
216 (list 'quote funsym)
217 (list 'and
218 '(interactive-p)
219 (not (not (commandp funsym))))
220 'args))))
221 ;; to record profiling times, we set the symbol's function
222 ;; definition so that it runs the elp-wrapper function with the
223 ;; function symbol as an argument. We place the old function
224 ;; definition on the info vector.
225 ;;
226 ;; The info vector data structure is a 3 element vector. The 0th
227 ;; element is the call-count, i.e. the total number of times this
228 ;; function has been entered. This value is bumped up on entry to
229 ;; the function so that non-local exists are still recorded. TBD:
230 ;; I haven't tested non-local exits at all, so no guarantees.
231 ;;
232 ;; The 1st element is the total amount of time in usecs that have
233 ;; been spent inside this function. This number is added to on
234 ;; function exit.
235 ;;
236 ;; The 2nd element is the old function definition list. This gets
237 ;; funcall'd in between start/end time retrievals. I believe that
238 ;; this lets us profile even byte-compiled functions.
239
240 ;; put the info vector on the property list
241 (put funsym elp-timer-info-property infovec)
242
243 ;; set the symbol's new profiling function definition to run
244 ;; elp-wrapper
245 (fset funsym newguts)
246
247 ;; add this function to the instrumentation list
248 (or (memq funsym elp-all-instrumented-list)
249 (setq elp-all-instrumented-list
250 (cons funsym elp-all-instrumented-list)))
251 ))
252
253 ;;;###autoload
254 (defun elp-restore-function (funsym)
255 "Restore an instrumented function to its original definition.
256 Argument FUNSYM is the symbol of a defined function."
257 (interactive "aFunction to restore: ")
258 (let ((info (get funsym elp-timer-info-property)))
259 ;; delete the function from the all instrumented list
260 (setq elp-all-instrumented-list
261 (delq funsym elp-all-instrumented-list))
262
263 ;; if the function was the master, reset the master
264 (if (eq funsym elp-master)
265 (setq elp-master nil
266 elp-record-p t))
267
268 ;; zap the properties
269 (put funsym elp-timer-info-property nil)
270
271 ;; restore the original function definition, but if the function
272 ;; wasn't instrumented do nothing. we do this after the above
273 ;; because its possible the function got un-instrumented due to
274 ;; circumstances beyond our control. Also, check to make sure
275 ;; that the current function symbol points to elp-wrapper. If
276 ;; not, then the user probably did an eval-defun while the
277 ;; function was instrumented and we don't want to destroy the new
278 ;; definition.
279 (and info
280 (assq 'elp-wrapper (symbol-function funsym))
281 (fset funsym (aref info 2)))))
282
283 ;;;###autoload
284 (defun elp-instrument-list (&optional list)
285 "Instrument for profiling, all functions in `elp-function-list'.
286 Use optional LIST if provided instead."
287 (interactive "PList of functions to instrument: ")
288 (let ((list (or list elp-function-list)))
289 (mapcar 'elp-instrument-function list)))
290
291 ;;;###autoload
292 (defun elp-instrument-package (prefix)
293 "Instrument for profiling, all functions which start with PREFIX.
294 For example, to instrument all ELP functions, do the following:
295
296 \\[elp-instrument-package] RET elp- RET"
297 (interactive "sPrefix of package to instrument: ")
298 (elp-instrument-list
299 (mapcar 'intern (all-completions prefix obarray
300 (function
301 (lambda (sym)
302 (and (fboundp sym)
303 (not (memq (car-safe
304 (symbol-function sym))
305 '(macro keymap autoload))))))))))
306
307 (defun elp-restore-list (&optional list)
308 "Restore the original definitions for all functions in `elp-function-list'.
309 Use optional LIST if provided instead."
310 (interactive "PList of functions to restore: ")
311 (let ((list (or list elp-function-list)))
312 (mapcar 'elp-restore-function list)))
313
314 (defun elp-restore-all ()
315 "Restores the original definitions of all functions being profiled."
316 (interactive)
317 (elp-restore-list elp-all-instrumented-list))
318
319 \f
320 (defun elp-reset-function (funsym)
321 "Reset the profiling information for FUNSYM."
322 (interactive "aFunction to reset: ")
323 (let ((info (get funsym elp-timer-info-property)))
324 (or info
325 (error "%s is not instrumented for profiling." funsym))
326 (aset info 0 0) ;reset call counter
327 (aset info 1 0.0) ;reset total time
328 ;; don't muck with aref 2 as that is the old symbol definition
329 ))
330
331 (defun elp-reset-list (&optional list)
332 "Reset the profiling information for all functions in `elp-function-list'.
333 Use optional LIST if provided instead."
334 (interactive "PList of functions to reset: ")
335 (let ((list (or list elp-function-list)))
336 (mapcar 'elp-reset-function list)))
337
338 (defun elp-reset-all ()
339 "Reset the profiling information for all functions being profiled."
340 (interactive)
341 (elp-reset-list elp-all-instrumented-list))
342
343 (defun elp-set-master (funsym)
344 "Set the master function for profiling."
345 (interactive "aMaster function: ")
346 ;; when there's a master function, recording is turned off by
347 ;; default
348 (setq elp-master funsym
349 elp-record-p nil)
350 ;; make sure master function is instrumented
351 (or (memq funsym elp-all-instrumented-list)
352 (elp-instrument-function funsym)))
353
354 (defun elp-unset-master ()
355 "Unsets the master function."
356 (interactive)
357 ;; when there's no master function, recording is turned on by default.
358 (setq elp-master nil
359 elp-record-p t))
360
361 \f
362 (defsubst elp-get-time ()
363 ;; get current time in seconds and microseconds. I throw away the
364 ;; most significant 16 bits of seconds since I doubt we'll ever want
365 ;; to profile lisp on the order of 18 hours. See notes at top of file.
366 (let ((now (current-time)))
367 (+ (float (nth 1 now)) (/ (float (nth 2 now)) 1000000.0))))
368
369 (defun elp-wrapper (funsym interactive-p args)
370 "This function has been instrumented for profiling by the ELP.
371 ELP is the Emacs Lisp Profiler. To restore the function to its
372 original definition, use \\[elp-restore-function] or \\[elp-restore-all]."
373 ;; turn on recording if this is the master function
374 (if (and elp-master
375 (eq funsym elp-master))
376 (setq elp-record-p t))
377 ;; get info vector and original function symbol
378 (let* ((info (get funsym elp-timer-info-property))
379 (func (aref info 2))
380 result)
381 (or func
382 (error "%s is not instrumented for profiling." funsym))
383 (if (not elp-record-p)
384 ;; when not recording, just call the original function symbol
385 ;; and return the results.
386 (setq result
387 (if interactive-p
388 (call-interactively func)
389 (apply func args)))
390 ;; we are recording times
391 (let ((enter-time (elp-get-time)))
392 ;; increment the call-counter
393 (aset info 0 (1+ (aref info 0)))
394 ;; now call the old symbol function, checking to see if it
395 ;; should be called interactively. make sure we return the
396 ;; correct value
397 (setq result
398 (if interactive-p
399 (call-interactively func)
400 (apply func args)))
401 ;; calculate total time in function
402 (aset info 1 (+ (aref info 1) (- (elp-get-time) enter-time)))
403 ))
404 ;; turn off recording if this is the master function
405 (if (and elp-master
406 (eq funsym elp-master))
407 (setq elp-record-p nil))
408 result))
409
410 \f
411 ;; shut the byte-compiler up
412 (defvar elp-field-len nil)
413 (defvar elp-cc-len nil)
414 (defvar elp-at-len nil)
415 (defvar elp-et-len nil)
416
417 (defun elp-sort-by-call-count (vec1 vec2)
418 ;; sort by highest call count. See `sort'.
419 (>= (aref vec1 0) (aref vec2 0)))
420
421 (defun elp-sort-by-total-time (vec1 vec2)
422 ;; sort by highest total time spent in function. See `sort'.
423 (>= (aref vec1 1) (aref vec2 1)))
424
425 (defun elp-sort-by-average-time (vec1 vec2)
426 ;; sort by highest average time spent in function. See `sort'.
427 (>= (aref vec1 2) (aref vec2 2)))
428
429 (defsubst elp-pack-number (number width)
430 ;; pack the NUMBER string into WIDTH characters, watching out for
431 ;; very small or large numbers
432 (if (<= (length number) width)
433 number
434 ;; check for very large or small numbers
435 (if (string-match "^\\(.*\\)\\(e[+-].*\\)$" number)
436 (concat (substring
437 (substring number (match-beginning 1) (match-end 1))
438 0
439 (- width (match-end 2) (- (match-beginning 2)) 3))
440 "..."
441 (substring number (match-beginning 2) (match-end 2)))
442 (concat (substring number 0 width)))))
443
444 (defun elp-output-result (resultvec)
445 ;; output the RESULTVEC into the results buffer. RESULTVEC is a 4 or
446 ;; more element vector where aref 0 is the call count, aref 1 is the
447 ;; total time spent in the function, aref 2 is the average time
448 ;; spent in the function, and aref 3 is the symbol's string
449 ;; name. All other elements in the vector are ignored.
450 (let* ((cc (aref resultvec 0))
451 (tt (aref resultvec 1))
452 (at (aref resultvec 2))
453 (symname (aref resultvec 3))
454 callcnt totaltime avetime)
455 (setq callcnt (number-to-string cc)
456 totaltime (number-to-string tt)
457 avetime (number-to-string at))
458 ;; possibly prune the results
459 (if (and elp-report-limit
460 (numberp elp-report-limit)
461 (< cc elp-report-limit))
462 nil
463 (insert symname)
464 (insert-char 32 (+ elp-field-len (- (length symname)) 2))
465 ;; print stuff out, formatting it nicely
466 (insert callcnt)
467 (insert-char 32 (+ elp-cc-len (- (length callcnt)) 2))
468 (let ((ttstr (elp-pack-number totaltime elp-et-len))
469 (atstr (elp-pack-number avetime elp-at-len)))
470 (insert ttstr)
471 (insert-char 32 (+ elp-et-len (- (length ttstr)) 2))
472 (insert atstr))
473 (insert "\n"))))
474
475 ;;;###autoload
476 (defun elp-results ()
477 "Display current profiling results.
478 If `elp-reset-after-results' is non-nil, then current profiling
479 information for all instrumented functions are reset after results are
480 displayed."
481 (interactive)
482 (let ((curbuf (current-buffer))
483 (resultsbuf (get-buffer-create elp-results-buffer)))
484 (set-buffer resultsbuf)
485 (erase-buffer)
486 (beginning-of-buffer)
487 ;; get the length of the longest function name being profiled
488 (let* ((longest 0)
489 (title "Function Name")
490 (titlelen (length title))
491 (elp-field-len titlelen)
492 (cc-header "Call Count")
493 (elp-cc-len (length cc-header))
494 (et-header "Elapsed Time")
495 (elp-et-len (length et-header))
496 (at-header "Average Time")
497 (elp-at-len (length at-header))
498 (resvec
499 (mapcar
500 (function
501 (lambda (funsym)
502 (let* ((info (get funsym elp-timer-info-property))
503 (symname (format "%s" funsym))
504 (cc (aref info 0))
505 (tt (aref info 1)))
506 (if (not info)
507 (insert "No profiling information found for: "
508 symname)
509 (setq longest (max longest (length symname)))
510 (vector cc tt (if (zerop cc)
511 0.0 ;avoid arithmetic div-by-zero errors
512 (/ (float tt) (float cc)))
513 symname)))))
514 elp-all-instrumented-list))
515 ) ; end let*
516 (insert title)
517 (if (> longest titlelen)
518 (progn
519 (insert-char 32 (- longest titlelen))
520 (setq elp-field-len longest)))
521 (insert " " cc-header " " et-header " " at-header "\n")
522 (insert-char ?= elp-field-len)
523 (insert " ")
524 (insert-char ?= elp-cc-len)
525 (insert " ")
526 (insert-char ?= elp-et-len)
527 (insert " ")
528 (insert-char ?= elp-at-len)
529 (insert "\n")
530 ;; if sorting is enabled, then sort the results list. in either
531 ;; case, call elp-output-result to output the result in the
532 ;; buffer
533 (if elp-sort-by-function
534 (setq resvec (sort resvec elp-sort-by-function)))
535 (mapcar 'elp-output-result resvec))
536 ;; now pop up results buffer
537 (set-buffer curbuf)
538 (pop-to-buffer resultsbuf)
539 ;; reset profiling info if desired
540 (and elp-reset-after-results
541 (elp-reset-all))))
542
543 \f
544 (eval-when-compile
545 (require 'reporter))
546
547 ;;;###autoload
548 (defun elp-submit-bug-report ()
549 "Submit via mail, a bug report on elp."
550 (interactive)
551 (and
552 (y-or-n-p "Do you want to submit a report on elp? ")
553 (require 'reporter)
554 (reporter-submit-bug-report
555 elp-help-address (concat "elp " elp-version)
556 '(elp-report-limit
557 elp-reset-after-results
558 elp-sort-by-function))))
559
560 \f
561 (provide 'elp)
562
563 ;; elp.el ends here