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 | |
f2f22d26 | 41 | ;; the package follows the GNU coding standard of a common textual |
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 |
f0529b5b | 180 | "*nil says to not recycle the `elp-results-buffer'. |
9b267eba | 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 | ||
5848fe5c SM |
207 | (defvar elp-not-profilable |
208 | '(elp-wrapper elp-elapsed-time error call-interactively apply current-time interactive-p) | |
209 | "List of functions that cannot be profiled. | |
210 | Those functions are used internally by the profiling code and profiling | |
211 | them would thus lead to infinite recursion.") | |
212 | ||
213 | (defun elp-not-profilable-p (fun) | |
214 | (or (memq fun elp-not-profilable) | |
215 | (keymapp fun) | |
216 | (condition-case nil | |
217 | (when (subrp (symbol-function fun)) | |
218 | (eq 'unevalled (cdr (subr-arity (symbol-function fun))))) | |
219 | (error nil)))) | |
220 | ||
d83cce6d | 221 | \f |
62f91105 | 222 | ;;;###autoload |
20f5d145 RS |
223 | (defun elp-instrument-function (funsym) |
224 | "Instrument FUNSYM for profiling. | |
225 | FUNSYM must be a symbol of a defined function." | |
226 | (interactive "aFunction to instrument: ") | |
9b267eba RS |
227 | ;; restore the function. this is necessary to avoid infinite |
228 | ;; recursion of already instrumented functions (i.e. elp-wrapper | |
229 | ;; calling elp-wrapper ad infinitum). it is better to simply | |
230 | ;; restore the function than to throw an error. this will work | |
231 | ;; properly in the face of eval-defun because if the function was | |
232 | ;; redefined, only the timer info will be nil'd out since | |
233 | ;; elp-restore-function is smart enough not to trash the new | |
234 | ;; definition. | |
235 | (elp-restore-function funsym) | |
20f5d145 RS |
236 | (let* ((funguts (symbol-function funsym)) |
237 | (infovec (vector 0 0 funguts)) | |
238 | (newguts '(lambda (&rest args)))) | |
5848fe5c SM |
239 | ;; We cannot profile functions used internally during profiling. |
240 | (when (elp-not-profilable-p funsym) | |
241 | (error "ELP cannot profile the function: %s" funsym)) | |
c0fd04c8 RS |
242 | ;; we cannot profile macros |
243 | (and (eq (car-safe funguts) 'macro) | |
9b267eba RS |
244 | (error "ELP cannot profile macro: %s" funsym)) |
245 | ;; TBD: at some point it might be better to load the autoloaded | |
246 | ;; function instead of throwing an error. if we do this, then we | |
247 | ;; probably want elp-instrument-package to be updated with the | |
248 | ;; newly loaded list of functions. i'm not sure it's smart to do | |
249 | ;; the autoload here, since that could have side effects, and | |
250 | ;; elp-instrument-function is similar (in my mind) to defun-ish | |
251 | ;; type functionality (i.e. it shouldn't execute the function). | |
252 | (and (eq (car-safe funguts) 'autoload) | |
253 | (error "ELP cannot profile autoloaded function: %s" funsym)) | |
20f5d145 RS |
254 | ;; put rest of newguts together |
255 | (if (commandp funsym) | |
256 | (setq newguts (append newguts '((interactive))))) | |
5848fe5c SM |
257 | (setq newguts (append newguts `((elp-wrapper |
258 | (quote ,funsym) | |
259 | ,(when (commandp funsym) | |
ea346a5d | 260 | '(called-interactively-p)) |
5848fe5c | 261 | args)))) |
20f5d145 RS |
262 | ;; to record profiling times, we set the symbol's function |
263 | ;; definition so that it runs the elp-wrapper function with the | |
264 | ;; function symbol as an argument. We place the old function | |
265 | ;; definition on the info vector. | |
266 | ;; | |
267 | ;; The info vector data structure is a 3 element vector. The 0th | |
268 | ;; element is the call-count, i.e. the total number of times this | |
269 | ;; function has been entered. This value is bumped up on entry to | |
270 | ;; the function so that non-local exists are still recorded. TBD: | |
271 | ;; I haven't tested non-local exits at all, so no guarantees. | |
272 | ;; | |
273 | ;; The 1st element is the total amount of time in usecs that have | |
274 | ;; been spent inside this function. This number is added to on | |
275 | ;; function exit. | |
276 | ;; | |
277 | ;; The 2nd element is the old function definition list. This gets | |
278 | ;; funcall'd in between start/end time retrievals. I believe that | |
279 | ;; this lets us profile even byte-compiled functions. | |
280 | ||
281 | ;; put the info vector on the property list | |
282 | (put funsym elp-timer-info-property infovec) | |
283 | ||
cebf1b97 GM |
284 | ;; Set the symbol's new profiling function definition to run |
285 | ;; elp-wrapper. | |
286 | (let ((advice-info (get funsym 'ad-advice-info))) | |
287 | (if advice-info | |
288 | (progn | |
289 | ;; If function is advised, don't let Advice change | |
290 | ;; its definition from under us during the `fset'. | |
291 | (put funsym 'ad-advice-info nil) | |
292 | (fset funsym newguts) | |
293 | (put funsym 'ad-advice-info advice-info)) | |
294 | (fset funsym newguts))) | |
20f5d145 RS |
295 | |
296 | ;; add this function to the instrumentation list | |
5848fe5c SM |
297 | (unless (memq funsym elp-all-instrumented-list) |
298 | (push funsym elp-all-instrumented-list)))) | |
20f5d145 RS |
299 | |
300 | (defun elp-restore-function (funsym) | |
301 | "Restore an instrumented function to its original definition. | |
302 | Argument FUNSYM is the symbol of a defined function." | |
303 | (interactive "aFunction to restore: ") | |
304 | (let ((info (get funsym elp-timer-info-property))) | |
305 | ;; delete the function from the all instrumented list | |
306 | (setq elp-all-instrumented-list | |
307 | (delq funsym elp-all-instrumented-list)) | |
308 | ||
309 | ;; if the function was the master, reset the master | |
310 | (if (eq funsym elp-master) | |
311 | (setq elp-master nil | |
312 | elp-record-p t)) | |
313 | ||
314 | ;; zap the properties | |
315 | (put funsym elp-timer-info-property nil) | |
316 | ||
317 | ;; restore the original function definition, but if the function | |
318 | ;; wasn't instrumented do nothing. we do this after the above | |
319 | ;; because its possible the function got un-instrumented due to | |
320 | ;; circumstances beyond our control. Also, check to make sure | |
321 | ;; that the current function symbol points to elp-wrapper. If | |
9b267eba RS |
322 | ;; not, then the user probably did an eval-defun, or loaded a |
323 | ;; byte-compiled version, while the function was instrumented and | |
324 | ;; we don't want to destroy the new definition. can it ever be | |
325 | ;; the case that a lisp function can be compiled instrumented? | |
20f5d145 | 326 | (and info |
b5ca6f92 | 327 | (functionp funsym) |
36172f49 | 328 | (not (byte-code-function-p (symbol-function funsym))) |
20f5d145 RS |
329 | (assq 'elp-wrapper (symbol-function funsym)) |
330 | (fset funsym (aref info 2))))) | |
331 | ||
62f91105 | 332 | ;;;###autoload |
20f5d145 RS |
333 | (defun elp-instrument-list (&optional list) |
334 | "Instrument for profiling, all functions in `elp-function-list'. | |
335 | Use optional LIST if provided instead." | |
336 | (interactive "PList of functions to instrument: ") | |
337 | (let ((list (or list elp-function-list))) | |
338 | (mapcar 'elp-instrument-function list))) | |
339 | ||
62f91105 | 340 | ;;;###autoload |
c0fd04c8 RS |
341 | (defun elp-instrument-package (prefix) |
342 | "Instrument for profiling, all functions which start with PREFIX. | |
343 | For example, to instrument all ELP functions, do the following: | |
344 | ||
345 | \\[elp-instrument-package] RET elp- RET" | |
346 | (interactive "sPrefix of package to instrument: ") | |
940cf42e | 347 | (if (zerop (length prefix)) |
60370d40 | 348 | (error "Instrumenting all Emacs functions would render Emacs unusable")) |
c0fd04c8 | 349 | (elp-instrument-list |
d83cce6d RS |
350 | (mapcar |
351 | 'intern | |
352 | (all-completions | |
353 | prefix obarray | |
5848fe5c SM |
354 | (lambda (sym) |
355 | (and (fboundp sym) | |
356 | (not (or (memq (car-safe (symbol-function sym)) '(autoload macro)) | |
357 | (elp-not-profilable-p sym))))))))) | |
c0fd04c8 | 358 | |
20f5d145 RS |
359 | (defun elp-restore-list (&optional list) |
360 | "Restore the original definitions for all functions in `elp-function-list'. | |
361 | Use optional LIST if provided instead." | |
362 | (interactive "PList of functions to restore: ") | |
363 | (let ((list (or list elp-function-list))) | |
364 | (mapcar 'elp-restore-function list))) | |
365 | ||
366 | (defun elp-restore-all () | |
367 | "Restores the original definitions of all functions being profiled." | |
368 | (interactive) | |
369 | (elp-restore-list elp-all-instrumented-list)) | |
370 | ||
371 | \f | |
372 | (defun elp-reset-function (funsym) | |
373 | "Reset the profiling information for FUNSYM." | |
374 | (interactive "aFunction to reset: ") | |
375 | (let ((info (get funsym elp-timer-info-property))) | |
376 | (or info | |
60370d40 | 377 | (error "%s is not instrumented for profiling" funsym)) |
20f5d145 RS |
378 | (aset info 0 0) ;reset call counter |
379 | (aset info 1 0.0) ;reset total time | |
380 | ;; don't muck with aref 2 as that is the old symbol definition | |
381 | )) | |
382 | ||
383 | (defun elp-reset-list (&optional list) | |
384 | "Reset the profiling information for all functions in `elp-function-list'. | |
385 | Use optional LIST if provided instead." | |
386 | (interactive "PList of functions to reset: ") | |
387 | (let ((list (or list elp-function-list))) | |
388 | (mapcar 'elp-reset-function list))) | |
389 | ||
390 | (defun elp-reset-all () | |
391 | "Reset the profiling information for all functions being profiled." | |
392 | (interactive) | |
393 | (elp-reset-list elp-all-instrumented-list)) | |
394 | ||
395 | (defun elp-set-master (funsym) | |
396 | "Set the master function for profiling." | |
397 | (interactive "aMaster function: ") | |
398 | ;; when there's a master function, recording is turned off by | |
399 | ;; default | |
400 | (setq elp-master funsym | |
401 | elp-record-p nil) | |
402 | ;; make sure master function is instrumented | |
403 | (or (memq funsym elp-all-instrumented-list) | |
404 | (elp-instrument-function funsym))) | |
405 | ||
406 | (defun elp-unset-master () | |
407 | "Unsets the master function." | |
5fe4899a | 408 | (interactive) |
20f5d145 RS |
409 | ;; when there's no master function, recording is turned on by default. |
410 | (setq elp-master nil | |
411 | elp-record-p t)) | |
412 | ||
413 | \f | |
9b267eba RS |
414 | (defsubst elp-elapsed-time (start end) |
415 | (+ (* (- (car end) (car start)) 65536.0) | |
416 | (- (car (cdr end)) (car (cdr start))) | |
417 | (/ (- (car (cdr (cdr end))) (car (cdr (cdr start)))) 1000000.0))) | |
20f5d145 RS |
418 | |
419 | (defun elp-wrapper (funsym interactive-p args) | |
420 | "This function has been instrumented for profiling by the ELP. | |
421 | ELP is the Emacs Lisp Profiler. To restore the function to its | |
422 | original definition, use \\[elp-restore-function] or \\[elp-restore-all]." | |
423 | ;; turn on recording if this is the master function | |
424 | (if (and elp-master | |
425 | (eq funsym elp-master)) | |
426 | (setq elp-record-p t)) | |
427 | ;; get info vector and original function symbol | |
428 | (let* ((info (get funsym elp-timer-info-property)) | |
429 | (func (aref info 2)) | |
430 | result) | |
431 | (or func | |
60370d40 | 432 | (error "%s is not instrumented for profiling" funsym)) |
20f5d145 RS |
433 | (if (not elp-record-p) |
434 | ;; when not recording, just call the original function symbol | |
435 | ;; and return the results. | |
436 | (setq result | |
437 | (if interactive-p | |
438 | (call-interactively func) | |
439 | (apply func args))) | |
440 | ;; we are recording times | |
9b267eba | 441 | (let (enter-time exit-time) |
20f5d145 RS |
442 | ;; increment the call-counter |
443 | (aset info 0 (1+ (aref info 0))) | |
444 | ;; now call the old symbol function, checking to see if it | |
445 | ;; should be called interactively. make sure we return the | |
446 | ;; correct value | |
9b267eba RS |
447 | (if interactive-p |
448 | (setq enter-time (current-time) | |
449 | result (call-interactively func) | |
450 | exit-time (current-time)) | |
451 | (setq enter-time (current-time) | |
452 | result (apply func args) | |
453 | exit-time (current-time))) | |
20f5d145 | 454 | ;; calculate total time in function |
9b267eba | 455 | (aset info 1 (+ (aref info 1) (elp-elapsed-time enter-time exit-time))) |
20f5d145 RS |
456 | )) |
457 | ;; turn off recording if this is the master function | |
458 | (if (and elp-master | |
459 | (eq funsym elp-master)) | |
460 | (setq elp-record-p nil)) | |
461 | result)) | |
462 | ||
463 | \f | |
464 | ;; shut the byte-compiler up | |
465 | (defvar elp-field-len nil) | |
466 | (defvar elp-cc-len nil) | |
467 | (defvar elp-at-len nil) | |
468 | (defvar elp-et-len nil) | |
469 | ||
470 | (defun elp-sort-by-call-count (vec1 vec2) | |
471 | ;; sort by highest call count. See `sort'. | |
472 | (>= (aref vec1 0) (aref vec2 0))) | |
473 | ||
474 | (defun elp-sort-by-total-time (vec1 vec2) | |
475 | ;; sort by highest total time spent in function. See `sort'. | |
476 | (>= (aref vec1 1) (aref vec2 1))) | |
477 | ||
478 | (defun elp-sort-by-average-time (vec1 vec2) | |
479 | ;; sort by highest average time spent in function. See `sort'. | |
480 | (>= (aref vec1 2) (aref vec2 2))) | |
481 | ||
5fe4899a RS |
482 | (defsubst elp-pack-number (number width) |
483 | ;; pack the NUMBER string into WIDTH characters, watching out for | |
484 | ;; very small or large numbers | |
485 | (if (<= (length number) width) | |
486 | number | |
487 | ;; check for very large or small numbers | |
488 | (if (string-match "^\\(.*\\)\\(e[+-].*\\)$" number) | |
489 | (concat (substring | |
490 | (substring number (match-beginning 1) (match-end 1)) | |
491 | 0 | |
492 | (- width (match-end 2) (- (match-beginning 2)) 3)) | |
493 | "..." | |
494 | (substring number (match-beginning 2) (match-end 2))) | |
495 | (concat (substring number 0 width))))) | |
496 | ||
20f5d145 RS |
497 | (defun elp-output-result (resultvec) |
498 | ;; output the RESULTVEC into the results buffer. RESULTVEC is a 4 or | |
499 | ;; more element vector where aref 0 is the call count, aref 1 is the | |
500 | ;; total time spent in the function, aref 2 is the average time | |
501 | ;; spent in the function, and aref 3 is the symbol's string | |
502 | ;; name. All other elements in the vector are ignored. | |
503 | (let* ((cc (aref resultvec 0)) | |
504 | (tt (aref resultvec 1)) | |
505 | (at (aref resultvec 2)) | |
506 | (symname (aref resultvec 3)) | |
507 | callcnt totaltime avetime) | |
20f5d145 RS |
508 | (setq callcnt (number-to-string cc) |
509 | totaltime (number-to-string tt) | |
510 | avetime (number-to-string at)) | |
c0fd04c8 RS |
511 | ;; possibly prune the results |
512 | (if (and elp-report-limit | |
513 | (numberp elp-report-limit) | |
514 | (< cc elp-report-limit)) | |
515 | nil | |
2b8d823f | 516 | (elp-output-insert-symname symname) |
c0fd04c8 RS |
517 | (insert-char 32 (+ elp-field-len (- (length symname)) 2)) |
518 | ;; print stuff out, formatting it nicely | |
519 | (insert callcnt) | |
520 | (insert-char 32 (+ elp-cc-len (- (length callcnt)) 2)) | |
5fe4899a RS |
521 | (let ((ttstr (elp-pack-number totaltime elp-et-len)) |
522 | (atstr (elp-pack-number avetime elp-at-len))) | |
523 | (insert ttstr) | |
524 | (insert-char 32 (+ elp-et-len (- (length ttstr)) 2)) | |
525 | (insert atstr)) | |
c0fd04c8 | 526 | (insert "\n")))) |
20f5d145 | 527 | |
2b8d823f MY |
528 | (defvar elp-results-symname-map |
529 | (let ((map (make-sparse-keymap))) | |
530 | (define-key map [mouse-2] 'elp-results-jump-to-definition-by-mouse) | |
531 | (define-key map "\C-m" 'elp-results-jump-to-definition) | |
532 | map) | |
533 | "Keymap used on the function name column." ) | |
534 | ||
535 | (defun elp-results-jump-to-definition-by-mouse (event) | |
536 | "Jump to the definition of the function under the place specified by EVENT." | |
537 | (interactive "e") | |
538 | (posn-set-point (event-end event)) | |
539 | (elp-results-jump-to-definition)) | |
540 | ||
541 | (defun elp-results-jump-to-definition () | |
542 | "Jump to the definition of the function under the point." | |
543 | (interactive) | |
544 | (find-function (get-text-property (point) 'elp-symname))) | |
545 | ||
546 | (defun elp-output-insert-symname (symname) | |
547 | ;; Insert SYMNAME with text properties. | |
548 | (insert (propertize symname | |
549 | 'elp-symname (intern symname) | |
550 | 'keymap elp-results-symname-map | |
551 | 'mouse-face 'highlight | |
552 | 'help-echo (substitute-command-keys "\\{elp-results-symname-map}")))) | |
553 | ||
62f91105 | 554 | ;;;###autoload |
20f5d145 RS |
555 | (defun elp-results () |
556 | "Display current profiling results. | |
557 | If `elp-reset-after-results' is non-nil, then current profiling | |
558 | information for all instrumented functions are reset after results are | |
559 | displayed." | |
560 | (interactive) | |
561 | (let ((curbuf (current-buffer)) | |
9b267eba RS |
562 | (resultsbuf (if elp-recycle-buffers-p |
563 | (get-buffer-create elp-results-buffer) | |
564 | (generate-new-buffer elp-results-buffer)))) | |
20f5d145 RS |
565 | (set-buffer resultsbuf) |
566 | (erase-buffer) | |
20f5d145 RS |
567 | ;; get the length of the longest function name being profiled |
568 | (let* ((longest 0) | |
569 | (title "Function Name") | |
570 | (titlelen (length title)) | |
571 | (elp-field-len titlelen) | |
572 | (cc-header "Call Count") | |
573 | (elp-cc-len (length cc-header)) | |
574 | (et-header "Elapsed Time") | |
575 | (elp-et-len (length et-header)) | |
576 | (at-header "Average Time") | |
577 | (elp-at-len (length at-header)) | |
578 | (resvec | |
579 | (mapcar | |
580 | (function | |
581 | (lambda (funsym) | |
582 | (let* ((info (get funsym elp-timer-info-property)) | |
583 | (symname (format "%s" funsym)) | |
584 | (cc (aref info 0)) | |
585 | (tt (aref info 1))) | |
586 | (if (not info) | |
587 | (insert "No profiling information found for: " | |
588 | symname) | |
589 | (setq longest (max longest (length symname))) | |
590 | (vector cc tt (if (zerop cc) | |
591 | 0.0 ;avoid arithmetic div-by-zero errors | |
592 | (/ (float tt) (float cc))) | |
593 | symname))))) | |
594 | elp-all-instrumented-list)) | |
595 | ) ; end let* | |
596 | (insert title) | |
597 | (if (> longest titlelen) | |
598 | (progn | |
599 | (insert-char 32 (- longest titlelen)) | |
600 | (setq elp-field-len longest))) | |
601 | (insert " " cc-header " " et-header " " at-header "\n") | |
602 | (insert-char ?= elp-field-len) | |
603 | (insert " ") | |
604 | (insert-char ?= elp-cc-len) | |
605 | (insert " ") | |
606 | (insert-char ?= elp-et-len) | |
607 | (insert " ") | |
608 | (insert-char ?= elp-at-len) | |
609 | (insert "\n") | |
610 | ;; if sorting is enabled, then sort the results list. in either | |
611 | ;; case, call elp-output-result to output the result in the | |
612 | ;; buffer | |
613 | (if elp-sort-by-function | |
614 | (setq resvec (sort resvec elp-sort-by-function))) | |
615 | (mapcar 'elp-output-result resvec)) | |
616 | ;; now pop up results buffer | |
617 | (set-buffer curbuf) | |
618 | (pop-to-buffer resultsbuf) | |
9b267eba RS |
619 | ;; copy results to standard-output? |
620 | (if (or elp-use-standard-output noninteractive) | |
621 | (princ (buffer-substring (point-min) (point-max)))) | |
20f5d145 RS |
622 | ;; reset profiling info if desired |
623 | (and elp-reset-after-results | |
624 | (elp-reset-all)))) | |
4edc4a39 DL |
625 | |
626 | (defun elp-unload-hook () | |
627 | (elp-restore-all)) | |
5172bdd1 | 628 | (add-hook 'elp-unload-hook 'elp-unload-hook) |
20f5d145 RS |
629 | \f |
630 | (provide 'elp) | |
5b689f60 | 631 | |
ab5796a9 | 632 | ;;; arch-tag: c4eef311-9b3e-4bb2-8a54-3485d41b4eb1 |
60370d40 | 633 | ;;; elp.el ends here |