Commit | Line | Data |
---|---|---|
20f5d145 RS |
1 | ;;; elp.el --- Emacs Lisp Profiler |
2 | ||
cebf1b97 | 3 | ;; Copyright (C) 1994,1995,1997,1998, 2001 Free Software Foundation, Inc. |
c0fd04c8 | 4 | |
60370d40 PJ |
5 | ;; Author: Barry A. Warsaw |
6 | ;; Maintainer: FSF | |
7 | ;; Created: 26-Feb-1994 | |
8 | ;; Keywords: debugging lisp tools | |
20f5d145 | 9 | |
c0fd04c8 | 10 | ;; This file is part of GNU Emacs. |
20f5d145 | 11 | |
c0fd04c8 | 12 | ;; GNU Emacs is free software; you can redistribute it and/or modify |
20f5d145 | 13 | ;; it under the terms of the GNU General Public License as published by |
c0fd04c8 RS |
14 | ;; the Free Software Foundation; either version 2, or (at your option) |
15 | ;; any later version. | |
16 | ||
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
20f5d145 RS |
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. | |
20f5d145 | 21 | |
c0fd04c8 | 22 | ;; You should have received a copy of the GNU General Public License |
b578f267 EN |
23 | ;; along with GNU Emacs; 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. | |
20f5d145 RS |
26 | |
27 | ;;; Commentary: | |
28 | ;; | |
b96bffd7 RS |
29 | ;; If you want to profile a bunch of functions, set elp-function-list |
30 | ;; to the list of symbols, then do a M-x elp-instrument-list. This | |
31 | ;; hacks those functions so that profiling information is recorded | |
32 | ;; whenever they are called. To print out the current results, use | |
9b267eba RS |
33 | ;; M-x elp-results. If you want output to go to standard-output |
34 | ;; instead of a separate buffer, setq elp-use-standard-output to | |
35 | ;; non-nil. With elp-reset-after-results set to non-nil, profiling | |
36 | ;; information will be reset whenever the results are displayed. You | |
37 | ;; can also reset all profiling info at any time with M-x | |
38 | ;; elp-reset-all. | |
c0fd04c8 RS |
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 | |
b96bffd7 | 42 | ;; prefix. Use M-x elp-instrument-package for this. |
20f5d145 RS |
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 | |
b96bffd7 RS |
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. | |
20f5d145 RS |
50 | ;; |
51 | ;; Elp can instrument byte-compiled functions just as easily as | |
c0fd04c8 | 52 | ;; interpreted functions, but it cannot instrument macros. However, |
5fe4899a | 53 | ;; when you redefine a function (e.g. with eval-defun), you'll need to |
b96bffd7 RS |
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. | |
20f5d145 RS |
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, | |
5fe4899a | 68 | ;; but only during execution of bar, make bar the master. The call of |
b96bffd7 RS |
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. | |
20f5d145 RS |
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 | ||
5b689f60 RS |
77 | ;; Here is a list of variable you can use to customize elp: |
78 | ;; elp-function-list | |
79 | ;; elp-reset-after-results | |
80 | ;; elp-sort-by-function | |
81 | ;; elp-report-limit | |
82 | ;; | |
83 | ;; Here is a list of the interactive commands you can use: | |
84 | ;; elp-instrument-function | |
85 | ;; elp-restore-function | |
86 | ;; elp-instrument-list | |
87 | ;; elp-restore-list | |
88 | ;; elp-instrument-package | |
89 | ;; elp-restore-all | |
90 | ;; elp-reset-function | |
91 | ;; elp-reset-list | |
92 | ;; elp-reset-all | |
93 | ;; elp-set-master | |
94 | ;; elp-unset-master | |
95 | ;; elp-results | |
5b689f60 | 96 | |
b96bffd7 RS |
97 | ;; Note that there are plenty of factors that could make the times |
98 | ;; reported unreliable, including the accuracy and granularity of your | |
99 | ;; system clock, and the overhead spent in lisp calculating and | |
100 | ;; recording the intervals. I figure the latter is pretty constant, | |
101 | ;; so while the times may not be entirely accurate, I think they'll | |
102 | ;; give you a good feel for the relative amount of work spent in the | |
103 | ;; various lisp routines you are profiling. Note further that times | |
104 | ;; are calculated using wall-clock time, so other system load will | |
9b267eba | 105 | ;; affect accuracy too. |
b96bffd7 | 106 | |
5b689f60 RS |
107 | ;;; Background: |
108 | ||
d83cce6d RS |
109 | ;; This program was inspired by the only two existing Emacs Lisp |
110 | ;; profilers that I'm aware of, Boaz Ben-Zvi's profile.el, and Root | |
111 | ;; Boy Jim's profiler.el. Both were written for Emacs 18 and both were | |
112 | ;; pretty good first shots at profiling, but I found that they didn't | |
113 | ;; provide the functionality or interface that I wanted, so I wrote | |
114 | ;; this. I've tested elp in XEmacs 19 and Emacs 19. There's no point | |
115 | ;; in even trying to make this work with Emacs 18. | |
5b689f60 RS |
116 | |
117 | ;; Unlike previous profilers, elp uses Emacs 19's built-in function | |
118 | ;; current-time to return interval times. This obviates the need for | |
119 | ;; both an external C program and Emacs processes to communicate with | |
b96bffd7 | 120 | ;; such a program, and thus simplifies the package as a whole. |
5fe4899a | 121 | |
9b267eba RS |
122 | ;; TBD: |
123 | ;; Make this act like a real profiler, so that it records time spent | |
124 | ;; in all branches of execution. | |
125 | ||
20f5d145 RS |
126 | ;;; Code: |
127 | ||
128 | \f | |
d83cce6d | 129 | ;; start of user configuration variables |
20f5d145 RS |
130 | ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv |
131 | ||
9e5b444e RS |
132 | (defgroup elp nil |
133 | "Emacs Lisp Profiler" | |
134 | :group 'lisp) | |
20f5d145 | 135 | |
9e5b444e RS |
136 | (defcustom elp-function-list nil |
137 | "*List of functions to profile. | |
138 | Used by the command `elp-instrument-list'." | |
139 | :type '(repeat function) | |
140 | :group 'elp) | |
141 | ||
142 | (defcustom elp-reset-after-results t | |
20f5d145 | 143 | "*Non-nil means reset all profiling info after results are displayed. |
9e5b444e RS |
144 | Results are displayed with the `elp-results' command." |
145 | :type 'boolean | |
146 | :group 'elp) | |
20f5d145 | 147 | |
9e5b444e | 148 | (defcustom elp-sort-by-function 'elp-sort-by-total-time |
20f5d145 RS |
149 | "*Non-nil specifies elp results sorting function. |
150 | These functions are currently available: | |
151 | ||
152 | elp-sort-by-call-count -- sort by the highest call count | |
153 | elp-sort-by-total-time -- sort by the highest total time | |
154 | elp-sort-by-average-time -- sort by the highest average times | |
155 | ||
156 | You can write you're own sort function. It should adhere to the | |
157 | interface specified by the PRED argument for the `sort' defun. Each | |
158 | \"element of LIST\" is really a 4 element vector where element 0 is | |
159 | the call count, element 1 is the total time spent in the function, | |
160 | element 2 is the average time spent in the function, and element 3 is | |
9e5b444e RS |
161 | the symbol's name string." |
162 | :type 'function | |
163 | :group 'elp) | |
20f5d145 | 164 | |
9e5b444e | 165 | (defcustom elp-report-limit 1 |
c0fd04c8 RS |
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 | |
9e5b444e RS |
169 | functions will be displayed." |
170 | :type '(choice integer | |
d83cce6d | 171 | (const :tag "Show All" nil)) |
9e5b444e | 172 | :group 'elp) |
c0fd04c8 | 173 | |
9e5b444e RS |
174 | (defcustom elp-use-standard-output nil |
175 | "*Non-nil says to output to `standard-output' instead of a buffer." | |
176 | :type 'boolean | |
177 | :group 'elp) | |
9b267eba | 178 | |
9e5b444e | 179 | (defcustom elp-recycle-buffers-p t |
9b267eba RS |
180 | "*Nil says to not recycle the `elp-results-buffer'. |
181 | In other words, a new unique buffer is create every time you run | |
9e5b444e RS |
182 | \\[elp-results]." |
183 | :type 'boolean | |
184 | :group 'elp) | |
9b267eba | 185 | |
20f5d145 RS |
186 | |
187 | ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
9b267eba | 188 | ;; end of user configuration variables |
20f5d145 RS |
189 | |
190 | \f | |
20f5d145 RS |
191 | (defvar elp-results-buffer "*ELP Profiling Results*" |
192 | "Buffer name for outputting profiling results.") | |
193 | ||
194 | (defconst elp-timer-info-property 'elp-info | |
195 | "ELP information property name.") | |
196 | ||
197 | (defvar elp-all-instrumented-list nil | |
198 | "List of all functions currently being instrumented.") | |
199 | ||
200 | (defvar elp-record-p t | |
201 | "Controls whether functions should record times or not. | |
202 | This variable is set by the master function.") | |
203 | ||
204 | (defvar elp-master nil | |
205 | "Master function symbol.") | |
206 | ||
d83cce6d | 207 | \f |
62f91105 | 208 | ;;;###autoload |
20f5d145 RS |
209 | (defun elp-instrument-function (funsym) |
210 | "Instrument FUNSYM for profiling. | |
211 | FUNSYM must be a symbol of a defined function." | |
212 | (interactive "aFunction to instrument: ") | |
9b267eba RS |
213 | ;; restore the function. this is necessary to avoid infinite |
214 | ;; recursion of already instrumented functions (i.e. elp-wrapper | |
215 | ;; calling elp-wrapper ad infinitum). it is better to simply | |
216 | ;; restore the function than to throw an error. this will work | |
217 | ;; properly in the face of eval-defun because if the function was | |
218 | ;; redefined, only the timer info will be nil'd out since | |
219 | ;; elp-restore-function is smart enough not to trash the new | |
220 | ;; definition. | |
221 | (elp-restore-function funsym) | |
20f5d145 RS |
222 | (let* ((funguts (symbol-function funsym)) |
223 | (infovec (vector 0 0 funguts)) | |
224 | (newguts '(lambda (&rest args)))) | |
c0fd04c8 RS |
225 | ;; we cannot profile macros |
226 | (and (eq (car-safe funguts) 'macro) | |
9b267eba RS |
227 | (error "ELP cannot profile macro: %s" funsym)) |
228 | ;; TBD: at some point it might be better to load the autoloaded | |
229 | ;; function instead of throwing an error. if we do this, then we | |
230 | ;; probably want elp-instrument-package to be updated with the | |
231 | ;; newly loaded list of functions. i'm not sure it's smart to do | |
232 | ;; the autoload here, since that could have side effects, and | |
233 | ;; elp-instrument-function is similar (in my mind) to defun-ish | |
234 | ;; type functionality (i.e. it shouldn't execute the function). | |
235 | (and (eq (car-safe funguts) 'autoload) | |
236 | (error "ELP cannot profile autoloaded function: %s" funsym)) | |
20f5d145 RS |
237 | ;; put rest of newguts together |
238 | (if (commandp funsym) | |
239 | (setq newguts (append newguts '((interactive))))) | |
240 | (setq newguts (append newguts (list | |
241 | (list 'elp-wrapper | |
242 | (list 'quote funsym) | |
243 | (list 'and | |
244 | '(interactive-p) | |
245 | (not (not (commandp funsym)))) | |
246 | 'args)))) | |
247 | ;; to record profiling times, we set the symbol's function | |
248 | ;; definition so that it runs the elp-wrapper function with the | |
249 | ;; function symbol as an argument. We place the old function | |
250 | ;; definition on the info vector. | |
251 | ;; | |
252 | ;; The info vector data structure is a 3 element vector. The 0th | |
253 | ;; element is the call-count, i.e. the total number of times this | |
254 | ;; function has been entered. This value is bumped up on entry to | |
255 | ;; the function so that non-local exists are still recorded. TBD: | |
256 | ;; I haven't tested non-local exits at all, so no guarantees. | |
257 | ;; | |
258 | ;; The 1st element is the total amount of time in usecs that have | |
259 | ;; been spent inside this function. This number is added to on | |
260 | ;; function exit. | |
261 | ;; | |
262 | ;; The 2nd element is the old function definition list. This gets | |
263 | ;; funcall'd in between start/end time retrievals. I believe that | |
264 | ;; this lets us profile even byte-compiled functions. | |
265 | ||
266 | ;; put the info vector on the property list | |
267 | (put funsym elp-timer-info-property infovec) | |
268 | ||
cebf1b97 GM |
269 | ;; Set the symbol's new profiling function definition to run |
270 | ;; elp-wrapper. | |
271 | (let ((advice-info (get funsym 'ad-advice-info))) | |
272 | (if advice-info | |
273 | (progn | |
274 | ;; If function is advised, don't let Advice change | |
275 | ;; its definition from under us during the `fset'. | |
276 | (put funsym 'ad-advice-info nil) | |
277 | (fset funsym newguts) | |
278 | (put funsym 'ad-advice-info advice-info)) | |
279 | (fset funsym newguts))) | |
20f5d145 RS |
280 | |
281 | ;; add this function to the instrumentation list | |
282 | (or (memq funsym elp-all-instrumented-list) | |
283 | (setq elp-all-instrumented-list | |
cebf1b97 | 284 | (cons funsym elp-all-instrumented-list))))) |
20f5d145 RS |
285 | |
286 | (defun elp-restore-function (funsym) | |
287 | "Restore an instrumented function to its original definition. | |
288 | Argument FUNSYM is the symbol of a defined function." | |
289 | (interactive "aFunction to restore: ") | |
290 | (let ((info (get funsym elp-timer-info-property))) | |
291 | ;; delete the function from the all instrumented list | |
292 | (setq elp-all-instrumented-list | |
293 | (delq funsym elp-all-instrumented-list)) | |
294 | ||
295 | ;; if the function was the master, reset the master | |
296 | (if (eq funsym elp-master) | |
297 | (setq elp-master nil | |
298 | elp-record-p t)) | |
299 | ||
300 | ;; zap the properties | |
301 | (put funsym elp-timer-info-property nil) | |
302 | ||
303 | ;; restore the original function definition, but if the function | |
304 | ;; wasn't instrumented do nothing. we do this after the above | |
305 | ;; because its possible the function got un-instrumented due to | |
306 | ;; circumstances beyond our control. Also, check to make sure | |
307 | ;; that the current function symbol points to elp-wrapper. If | |
9b267eba RS |
308 | ;; not, then the user probably did an eval-defun, or loaded a |
309 | ;; byte-compiled version, while the function was instrumented and | |
310 | ;; we don't want to destroy the new definition. can it ever be | |
311 | ;; the case that a lisp function can be compiled instrumented? | |
20f5d145 | 312 | (and info |
b5ca6f92 | 313 | (functionp funsym) |
36172f49 | 314 | (not (byte-code-function-p (symbol-function funsym))) |
20f5d145 RS |
315 | (assq 'elp-wrapper (symbol-function funsym)) |
316 | (fset funsym (aref info 2))))) | |
317 | ||
62f91105 | 318 | ;;;###autoload |
20f5d145 RS |
319 | (defun elp-instrument-list (&optional list) |
320 | "Instrument for profiling, all functions in `elp-function-list'. | |
321 | Use optional LIST if provided instead." | |
322 | (interactive "PList of functions to instrument: ") | |
323 | (let ((list (or list elp-function-list))) | |
324 | (mapcar 'elp-instrument-function list))) | |
325 | ||
62f91105 | 326 | ;;;###autoload |
c0fd04c8 RS |
327 | (defun elp-instrument-package (prefix) |
328 | "Instrument for profiling, all functions which start with PREFIX. | |
329 | For example, to instrument all ELP functions, do the following: | |
330 | ||
331 | \\[elp-instrument-package] RET elp- RET" | |
332 | (interactive "sPrefix of package to instrument: ") | |
940cf42e | 333 | (if (zerop (length prefix)) |
60370d40 | 334 | (error "Instrumenting all Emacs functions would render Emacs unusable")) |
c0fd04c8 | 335 | (elp-instrument-list |
d83cce6d RS |
336 | (mapcar |
337 | 'intern | |
338 | (all-completions | |
339 | prefix obarray | |
340 | (function | |
341 | (lambda (sym) | |
342 | (and (fboundp sym) | |
343 | (not (memq (car-safe (symbol-function sym)) '(autoload macro)))) | |
344 | )) | |
345 | )))) | |
c0fd04c8 | 346 | |
20f5d145 RS |
347 | (defun elp-restore-list (&optional list) |
348 | "Restore the original definitions for all functions in `elp-function-list'. | |
349 | Use optional LIST if provided instead." | |
350 | (interactive "PList of functions to restore: ") | |
351 | (let ((list (or list elp-function-list))) | |
352 | (mapcar 'elp-restore-function list))) | |
353 | ||
354 | (defun elp-restore-all () | |
355 | "Restores the original definitions of all functions being profiled." | |
356 | (interactive) | |
357 | (elp-restore-list elp-all-instrumented-list)) | |
358 | ||
359 | \f | |
360 | (defun elp-reset-function (funsym) | |
361 | "Reset the profiling information for FUNSYM." | |
362 | (interactive "aFunction to reset: ") | |
363 | (let ((info (get funsym elp-timer-info-property))) | |
364 | (or info | |
60370d40 | 365 | (error "%s is not instrumented for profiling" funsym)) |
20f5d145 RS |
366 | (aset info 0 0) ;reset call counter |
367 | (aset info 1 0.0) ;reset total time | |
368 | ;; don't muck with aref 2 as that is the old symbol definition | |
369 | )) | |
370 | ||
371 | (defun elp-reset-list (&optional list) | |
372 | "Reset the profiling information for all functions in `elp-function-list'. | |
373 | Use optional LIST if provided instead." | |
374 | (interactive "PList of functions to reset: ") | |
375 | (let ((list (or list elp-function-list))) | |
376 | (mapcar 'elp-reset-function list))) | |
377 | ||
378 | (defun elp-reset-all () | |
379 | "Reset the profiling information for all functions being profiled." | |
380 | (interactive) | |
381 | (elp-reset-list elp-all-instrumented-list)) | |
382 | ||
383 | (defun elp-set-master (funsym) | |
384 | "Set the master function for profiling." | |
385 | (interactive "aMaster function: ") | |
386 | ;; when there's a master function, recording is turned off by | |
387 | ;; default | |
388 | (setq elp-master funsym | |
389 | elp-record-p nil) | |
390 | ;; make sure master function is instrumented | |
391 | (or (memq funsym elp-all-instrumented-list) | |
392 | (elp-instrument-function funsym))) | |
393 | ||
394 | (defun elp-unset-master () | |
395 | "Unsets the master function." | |
5fe4899a | 396 | (interactive) |
20f5d145 RS |
397 | ;; when there's no master function, recording is turned on by default. |
398 | (setq elp-master nil | |
399 | elp-record-p t)) | |
400 | ||
401 | \f | |
9b267eba RS |
402 | (defsubst elp-elapsed-time (start end) |
403 | (+ (* (- (car end) (car start)) 65536.0) | |
404 | (- (car (cdr end)) (car (cdr start))) | |
405 | (/ (- (car (cdr (cdr end))) (car (cdr (cdr start)))) 1000000.0))) | |
20f5d145 RS |
406 | |
407 | (defun elp-wrapper (funsym interactive-p args) | |
408 | "This function has been instrumented for profiling by the ELP. | |
409 | ELP is the Emacs Lisp Profiler. To restore the function to its | |
410 | original definition, use \\[elp-restore-function] or \\[elp-restore-all]." | |
411 | ;; turn on recording if this is the master function | |
412 | (if (and elp-master | |
413 | (eq funsym elp-master)) | |
414 | (setq elp-record-p t)) | |
415 | ;; get info vector and original function symbol | |
416 | (let* ((info (get funsym elp-timer-info-property)) | |
417 | (func (aref info 2)) | |
418 | result) | |
419 | (or func | |
60370d40 | 420 | (error "%s is not instrumented for profiling" funsym)) |
20f5d145 RS |
421 | (if (not elp-record-p) |
422 | ;; when not recording, just call the original function symbol | |
423 | ;; and return the results. | |
424 | (setq result | |
425 | (if interactive-p | |
426 | (call-interactively func) | |
427 | (apply func args))) | |
428 | ;; we are recording times | |
9b267eba | 429 | (let (enter-time exit-time) |
20f5d145 RS |
430 | ;; increment the call-counter |
431 | (aset info 0 (1+ (aref info 0))) | |
432 | ;; now call the old symbol function, checking to see if it | |
433 | ;; should be called interactively. make sure we return the | |
434 | ;; correct value | |
9b267eba RS |
435 | (if interactive-p |
436 | (setq enter-time (current-time) | |
437 | result (call-interactively func) | |
438 | exit-time (current-time)) | |
439 | (setq enter-time (current-time) | |
440 | result (apply func args) | |
441 | exit-time (current-time))) | |
20f5d145 | 442 | ;; calculate total time in function |
9b267eba | 443 | (aset info 1 (+ (aref info 1) (elp-elapsed-time enter-time exit-time))) |
20f5d145 RS |
444 | )) |
445 | ;; turn off recording if this is the master function | |
446 | (if (and elp-master | |
447 | (eq funsym elp-master)) | |
448 | (setq elp-record-p nil)) | |
449 | result)) | |
450 | ||
451 | \f | |
452 | ;; shut the byte-compiler up | |
453 | (defvar elp-field-len nil) | |
454 | (defvar elp-cc-len nil) | |
455 | (defvar elp-at-len nil) | |
456 | (defvar elp-et-len nil) | |
457 | ||
458 | (defun elp-sort-by-call-count (vec1 vec2) | |
459 | ;; sort by highest call count. See `sort'. | |
460 | (>= (aref vec1 0) (aref vec2 0))) | |
461 | ||
462 | (defun elp-sort-by-total-time (vec1 vec2) | |
463 | ;; sort by highest total time spent in function. See `sort'. | |
464 | (>= (aref vec1 1) (aref vec2 1))) | |
465 | ||
466 | (defun elp-sort-by-average-time (vec1 vec2) | |
467 | ;; sort by highest average time spent in function. See `sort'. | |
468 | (>= (aref vec1 2) (aref vec2 2))) | |
469 | ||
5fe4899a RS |
470 | (defsubst elp-pack-number (number width) |
471 | ;; pack the NUMBER string into WIDTH characters, watching out for | |
472 | ;; very small or large numbers | |
473 | (if (<= (length number) width) | |
474 | number | |
475 | ;; check for very large or small numbers | |
476 | (if (string-match "^\\(.*\\)\\(e[+-].*\\)$" number) | |
477 | (concat (substring | |
478 | (substring number (match-beginning 1) (match-end 1)) | |
479 | 0 | |
480 | (- width (match-end 2) (- (match-beginning 2)) 3)) | |
481 | "..." | |
482 | (substring number (match-beginning 2) (match-end 2))) | |
483 | (concat (substring number 0 width))))) | |
484 | ||
20f5d145 RS |
485 | (defun elp-output-result (resultvec) |
486 | ;; output the RESULTVEC into the results buffer. RESULTVEC is a 4 or | |
487 | ;; more element vector where aref 0 is the call count, aref 1 is the | |
488 | ;; total time spent in the function, aref 2 is the average time | |
489 | ;; spent in the function, and aref 3 is the symbol's string | |
490 | ;; name. All other elements in the vector are ignored. | |
491 | (let* ((cc (aref resultvec 0)) | |
492 | (tt (aref resultvec 1)) | |
493 | (at (aref resultvec 2)) | |
494 | (symname (aref resultvec 3)) | |
495 | callcnt totaltime avetime) | |
20f5d145 RS |
496 | (setq callcnt (number-to-string cc) |
497 | totaltime (number-to-string tt) | |
498 | avetime (number-to-string at)) | |
c0fd04c8 RS |
499 | ;; possibly prune the results |
500 | (if (and elp-report-limit | |
501 | (numberp elp-report-limit) | |
502 | (< cc elp-report-limit)) | |
503 | nil | |
504 | (insert symname) | |
505 | (insert-char 32 (+ elp-field-len (- (length symname)) 2)) | |
506 | ;; print stuff out, formatting it nicely | |
507 | (insert callcnt) | |
508 | (insert-char 32 (+ elp-cc-len (- (length callcnt)) 2)) | |
5fe4899a RS |
509 | (let ((ttstr (elp-pack-number totaltime elp-et-len)) |
510 | (atstr (elp-pack-number avetime elp-at-len))) | |
511 | (insert ttstr) | |
512 | (insert-char 32 (+ elp-et-len (- (length ttstr)) 2)) | |
513 | (insert atstr)) | |
c0fd04c8 | 514 | (insert "\n")))) |
20f5d145 | 515 | |
62f91105 | 516 | ;;;###autoload |
20f5d145 RS |
517 | (defun elp-results () |
518 | "Display current profiling results. | |
519 | If `elp-reset-after-results' is non-nil, then current profiling | |
520 | information for all instrumented functions are reset after results are | |
521 | displayed." | |
522 | (interactive) | |
523 | (let ((curbuf (current-buffer)) | |
9b267eba RS |
524 | (resultsbuf (if elp-recycle-buffers-p |
525 | (get-buffer-create elp-results-buffer) | |
526 | (generate-new-buffer elp-results-buffer)))) | |
20f5d145 RS |
527 | (set-buffer resultsbuf) |
528 | (erase-buffer) | |
529 | (beginning-of-buffer) | |
530 | ;; get the length of the longest function name being profiled | |
531 | (let* ((longest 0) | |
532 | (title "Function Name") | |
533 | (titlelen (length title)) | |
534 | (elp-field-len titlelen) | |
535 | (cc-header "Call Count") | |
536 | (elp-cc-len (length cc-header)) | |
537 | (et-header "Elapsed Time") | |
538 | (elp-et-len (length et-header)) | |
539 | (at-header "Average Time") | |
540 | (elp-at-len (length at-header)) | |
541 | (resvec | |
542 | (mapcar | |
543 | (function | |
544 | (lambda (funsym) | |
545 | (let* ((info (get funsym elp-timer-info-property)) | |
546 | (symname (format "%s" funsym)) | |
547 | (cc (aref info 0)) | |
548 | (tt (aref info 1))) | |
549 | (if (not info) | |
550 | (insert "No profiling information found for: " | |
551 | symname) | |
552 | (setq longest (max longest (length symname))) | |
553 | (vector cc tt (if (zerop cc) | |
554 | 0.0 ;avoid arithmetic div-by-zero errors | |
555 | (/ (float tt) (float cc))) | |
556 | symname))))) | |
557 | elp-all-instrumented-list)) | |
558 | ) ; end let* | |
559 | (insert title) | |
560 | (if (> longest titlelen) | |
561 | (progn | |
562 | (insert-char 32 (- longest titlelen)) | |
563 | (setq elp-field-len longest))) | |
564 | (insert " " cc-header " " et-header " " at-header "\n") | |
565 | (insert-char ?= elp-field-len) | |
566 | (insert " ") | |
567 | (insert-char ?= elp-cc-len) | |
568 | (insert " ") | |
569 | (insert-char ?= elp-et-len) | |
570 | (insert " ") | |
571 | (insert-char ?= elp-at-len) | |
572 | (insert "\n") | |
573 | ;; if sorting is enabled, then sort the results list. in either | |
574 | ;; case, call elp-output-result to output the result in the | |
575 | ;; buffer | |
576 | (if elp-sort-by-function | |
577 | (setq resvec (sort resvec elp-sort-by-function))) | |
578 | (mapcar 'elp-output-result resvec)) | |
579 | ;; now pop up results buffer | |
580 | (set-buffer curbuf) | |
581 | (pop-to-buffer resultsbuf) | |
9b267eba RS |
582 | ;; copy results to standard-output? |
583 | (if (or elp-use-standard-output noninteractive) | |
584 | (princ (buffer-substring (point-min) (point-max)))) | |
20f5d145 RS |
585 | ;; reset profiling info if desired |
586 | (and elp-reset-after-results | |
587 | (elp-reset-all)))) | |
4edc4a39 DL |
588 | |
589 | (defun elp-unload-hook () | |
590 | (elp-restore-all)) | |
20f5d145 RS |
591 | \f |
592 | (provide 'elp) | |
5b689f60 | 593 | |
60370d40 | 594 | ;;; elp.el ends here |