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