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