Mercurial > emacs
annotate lisp/emacs-lisp/profile.el @ 5613:cad51b2de6cd
(Fkey_binding): Handle Voverriding_local_map.
(describe_buffer_bindings): Likewise.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 16 Jan 1994 23:39:50 +0000 |
parents | a92eb4fb44b3 |
children | a539cda9df58 |
rev | line source |
---|---|
2233
fb0ed5a1d0f3
Add standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
845
diff
changeset
|
1 ;;; profile.el --- generate run time measurements of Emacs Lisp functions |
789
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
655
diff
changeset
|
2 |
845 | 3 ;; Copyright (C) 1992 Free Software Foundation, Inc. |
4 | |
789
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
655
diff
changeset
|
5 ;; Author: Boaz Ben-Zvi <boaz@lcs.mit.edu> |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
792
diff
changeset
|
6 ;; Created: 07 Feb 1992 |
789
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
655
diff
changeset
|
7 ;; Version: 1.0 |
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
655
diff
changeset
|
8 ;; Adapted-By: ESR |
814
38b2499cb3e9
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
9 ;; Keywords: lisp, tools |
655 | 10 |
11 ;; This file is part of GNU Emacs. | |
12 | |
13 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
14 ;; it under the terms of the GNU General Public License as published by | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
792
diff
changeset
|
15 ;; the Free Software Foundation; either version 2, or (at your option) |
655 | 16 ;; any later version. |
17 | |
18 ;; GNU Emacs is distributed in the hope that it will be useful, | |
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 ;; GNU General Public License for more details. | |
22 | |
23 ;; You should have received a copy of the GNU General Public License | |
24 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
26 | |
789
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
655
diff
changeset
|
27 ;;; Commentary: |
655 | 28 |
29 ; DESCRIPTION: | |
30 ; ------------ | |
792
45d748a65f24
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
789
diff
changeset
|
31 ; This program can be used to monitor running time performance of Emacs Lisp |
655 | 32 ; functions. It takes a list of functions and report the real time spent |
33 ; inside these functions. It runs a process with a separate timer program. | |
34 ; Caveat: the C code included with this package requires BSD-compatible | |
35 ; time-of-day functions. If you're running an AT&T version prior to SVr4, | |
36 ; you may have difficulty getting it to work. Your X library may supply | |
37 ; the required routines if the standard C library does not. | |
38 | |
39 ; HOW TO USE: | |
40 ; ----------- | |
41 ; Set the variable profile-functions-list to the list of functions | |
42 ; (as symbols) You want to profile. Call M-x profile-functions to set | |
43 ; this list on and start using your program. Note that profile-functions | |
44 ; MUST be called AFTER all the functions in profile-functions-list have | |
45 ; been loaded !! (This call modifies the code of the profiled functions. | |
46 ; Hence if you reload these functions, you need to call profile-functions | |
47 ; again! ). | |
48 ; To display the results do M-x profile-results . For example: | |
49 ;------------------------------------------------------------------- | |
50 ; (setq profile-functions-list '(sokoban-set-mode-line sokoban-load-game | |
51 ; sokoban-move-vertical sokoban-move)) | |
52 ; (load "sokoban") | |
53 ; M-x profile-functions | |
54 ; ... I play the sokoban game .......... | |
55 ; M-x profile-results | |
56 ; | |
57 ; Function Time (Seconds.Useconds) | |
58 ; ======== ======================= | |
59 ; sokoban-move 0.539088 | |
60 ; sokoban-move-vertical 0.410130 | |
61 ; sokoban-load-game 0.453235 | |
62 ; sokoban-set-mode-line 1.949203 | |
63 ;----------------------------------------------------- | |
64 ; To clear all the settings to profile use profile-finish. | |
65 ; To set one function at a time (instead of or in addition to setting the | |
66 ; above list and M-x profile-functions ) use M-x profile-a-function . | |
67 | |
68 ; HOW TO INSTALL: | |
69 ; --------------- | |
70 ; First you need to compile and install the following C program in your | |
71 ; path under the name "emacs-timer" (or set the variable | |
72 ; profile-timer-program to whatever name you picked). | |
73 ; | |
74 ;/** | |
75 ; ** To be run as an emacs process. Input string that starts with: | |
76 ; ** 'z' -- resets the watch (to zero). | |
77 ; ** 'p' -- return time (on stdout) as string with format <sec>.<micro-sec> | |
78 ; ** 'q' -- exit. | |
79 ; ** | |
80 ; ** abstraction : a stopwatch | |
81 ; ** operations: reset_watch, get_time | |
82 ; */ | |
83 ;#include <strings.h> | |
84 ;#include <sys/time.h> | |
85 ;#include <stdio.h> | |
86 ;static struct timeval TV1,TV2; | |
87 ;static struct timezone *tzp = (struct timezone *) NULL; /* no need timezone */ | |
88 ;static int watch_not_started = 1 ; /* flag */ | |
4567 | 89 ;static char time_string[30]; |
655 | 90 ; |
91 ;int reset_watch() /* this call resets the stopwatch to zero */ | |
92 ;{ | |
93 ; gettimeofday(&TV1, tzp) ; | |
94 ; watch_not_started = 0; | |
95 ;} | |
96 ; | |
97 ;char *get_time() | |
98 ; /* this call returns the time since the last reset_watch() call. The time | |
99 ; is returned as a string with the format <seconds>.<micro-seconds> | |
100 ; If reset_watch() was not called yet, returns NULL */ | |
101 ;{ | |
102 ; char *result = time_string ; | |
103 ; int i; | |
104 ; if (watch_not_started) return((char *) 0); /* call reset_watch first ! */ | |
105 ; gettimeofday(&TV2, tzp); | |
106 ; if ( TV1.tv_usec > TV2.tv_usec ) | |
107 ; { | |
108 ; TV2.tv_usec += 1000000; | |
109 ; TV2.tv_sec--; | |
110 ; } | |
111 ; sprintf(result,"%lu.%6lu", | |
112 ; TV2.tv_sec - TV1.tv_sec, TV2.tv_usec - TV1.tv_usec); | |
113 ; for (result = index(result,'.') + 1 ; *result == ' ' ; result++ ) | |
114 ; *result = '0'; | |
115 ; return(time_string); | |
116 ;} | |
117 ; | |
118 ;void main() | |
119 ;{ | |
120 ; char inp[10]; | |
121 ; while (1) | |
122 ; { | |
123 ; gets(inp); | |
124 ; switch (inp[0]) | |
125 ; { | |
126 ; case 'z': reset_watch(); | |
127 ; break; | |
128 ; case 'p': puts(get_time()); | |
129 ; break; | |
130 ; case 'q': exit(0); | |
131 ; } | |
132 ; } | |
133 ;} | |
134 ; -------- end of clip ---------------- | |
135 | |
789
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
655
diff
changeset
|
136 ;;; Code: |
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
655
diff
changeset
|
137 |
655 | 138 ;;; |
139 ;;; User modifiable VARIABLES | |
140 ;;; | |
141 | |
142 (defvar profile-functions-list nil "*List of functions to profile") | |
143 (defvar profile-timer-program "emacs-timer" "*Name of the timer program") | |
144 | |
145 ;;; | |
146 ;;; V A R I A B L E S | |
147 ;;; | |
148 | |
149 (defvar profile-timer-process nil "Process running the timer") | |
150 (defvar profile-time-list nil | |
151 "List of accumulative time for each profiled function") | |
152 (defvar profile-init-list nil | |
153 "List of entry time for each function. \n\ | |
154 Both how many times invoked and real time of start.") | |
155 (defvar profile-max-fun-name 0 "Max length of name of any function profiled") | |
156 (defvar profile-temp-result- nil "Should NOT be used anywhere else") | |
157 (defvar profile-time (cons 0 0) "Used to return result from a filter") | |
158 (defvar profile-buffer "*profile*" "Name of profile buffer") | |
159 | |
160 ;;; | |
161 ;;; F U N C T I O N S | |
162 ;;; | |
163 | |
164 (defun profile-functions (&optional flist) | |
165 "Profile all the functions listed in profile-functions-list.\n\ | |
166 With argument FLIST, use the list FLIST instead." | |
167 (interactive "*P") | |
168 (if (null flist) (setq flist profile-functions-list)) | |
169 (mapcar 'profile-a-function flist)) | |
170 | |
171 (defun profile-filter (process input) | |
172 "Filter for the timer process. Sets profile-time to the returned time." | |
173 (if (zerop (string-match "\\." input)) | |
174 (error "Bad output from %s" profile-timer-program) | |
175 (setcar profile-time | |
176 (string-to-int (substring input 0 (match-beginning 0)))) | |
177 (setcdr profile-time | |
178 (string-to-int (substring input (match-end 0)))))) | |
179 | |
180 | |
181 (defun profile-print (entry) | |
182 "Print one ENTRY (from profile-time-list) ." | |
183 (let ((time (cdr entry)) str (offset 5)) | |
184 (insert (format "%s" (car entry)) space) | |
185 (move-to-column ref-column) | |
186 (setq str (int-to-string (car time))) | |
187 (insert str) | |
188 (if (>= (length str) offset) nil | |
189 (move-to-column ref-column) | |
190 (insert (substring spaces 0 (- offset (length str)))) | |
191 (forward-char (length str))) | |
192 (setq str (int-to-string (cdr time))) | |
193 (insert "." (substring "000000" 0 (- 6 (length str))) str "\n") | |
194 )) | |
195 | |
196 (defconst spaces " ") | |
197 | |
198 (defun profile-results () | |
199 "Display profiling results in profile-buffer ." | |
200 (interactive) | |
201 (let* ((ref-column (+ 8 profile-max-fun-name)) | |
202 (space (substring spaces 0 ref-column))) | |
203 (switch-to-buffer profile-buffer) | |
204 (erase-buffer) | |
205 (insert "Function" space) | |
206 (move-to-column ref-column) | |
207 (insert "Time (Seconds.Useconds)\n" "========" space ) | |
208 (move-to-column ref-column) | |
209 (insert "=======================\n") | |
210 (mapcar 'profile-print profile-time-list))) | |
211 | |
212 (defun profile-reset-timer () | |
213 (process-send-string profile-timer-process "z\n")) | |
214 | |
215 (defun profile-check-zero-init-times (entry) | |
216 "If ENTRY has non zero time, give an error." | |
217 (let ((time (cdr (cdr entry)))) | |
218 (if (and (zerop (car time)) (zerop (cdr time))) nil ; OK | |
219 (error "Process timer died while making performance profile.")))) | |
220 | |
221 (defun profile-get-time () | |
222 "Get time from timer process into profile-time ." | |
223 ;; first time or if process dies | |
224 (if (and (processp profile-timer-process) | |
225 (eq 'run (process-status profile-timer-process))) nil | |
226 (setq profile-timer-process ;; [re]start the timer process | |
227 (start-process "timer" | |
228 (get-buffer-create profile-buffer) | |
229 profile-timer-program)) | |
230 (set-process-filter profile-timer-process 'profile-filter) | |
231 (process-kill-without-query profile-timer-process) | |
232 (profile-reset-timer) | |
233 ;; check if timer died during time measurement | |
234 (mapcar 'profile-check-zero-init-times profile-init-list)) | |
235 ;; make timer process return current time | |
236 (process-send-string profile-timer-process "p\n") | |
237 (accept-process-output)) | |
238 | |
239 (defun profile-find-function (fun flist) | |
240 "Linear search for FUN in FLIST ." | |
241 (if (null flist) nil | |
242 (if (eq fun (car (car flist))) (cdr (car flist)) | |
243 (profile-find-function fun (cdr flist))))) | |
244 | |
245 (defun profile-start-function (fun) | |
246 "On entry, keep current time for function FUN." | |
247 ;; assumes that profile-time contains the current time | |
248 (let ((init-time (profile-find-function fun profile-init-list))) | |
249 (if (null init-time) (error "Function %s missing from list" fun)) | |
250 (if (not (zerop (car init-time))) ;; is it a recursive call ? | |
251 (setcar init-time (1+ (car init-time))) | |
252 (setcar init-time 1) ; mark first entry | |
253 (setq init-time (cdr init-time)) | |
254 (setcar init-time (car profile-time)) | |
255 (setcdr init-time (cdr profile-time))) | |
256 )) | |
257 | |
258 (defconst profile-million 1000000) | |
259 | |
260 (defun profile-update-function (fun) | |
261 "When the call to the function FUN is finished, add its run time." | |
262 ;; assumes that profile-time contains the current time | |
263 (let ((init-time (profile-find-function fun profile-init-list)) | |
264 (accum (profile-find-function fun profile-time-list)) | |
265 sec usec) | |
266 (if (or (null init-time) | |
267 (null accum)) (error "Function %s missing from list" fun)) | |
268 (setcar init-time (1- (car init-time))) ; pop one level in recursion | |
269 (if (not (zerop (car init-time))) | |
270 nil ; in some recursion level, do not update accum. time | |
271 (setq init-time (cdr init-time)) | |
272 (setq sec (- (car profile-time) (car init-time)) | |
273 usec (- (cdr profile-time) (cdr init-time))) | |
274 (setcar init-time 0) ; reset time to check for error | |
275 (setcdr init-time 0) ; in case timer process dies | |
276 (if (>= usec 0) nil | |
277 (setq usec (+ usec profile-million)) | |
278 (setq sec (1- sec))) | |
279 (setcar accum (+ sec (car accum))) | |
280 (setcdr accum (+ usec (cdr accum))) | |
281 (if (< (cdr accum) profile-million) nil | |
282 (setcar accum (1+ (car accum))) | |
283 (setcdr accum (- (cdr accum) profile-million))) | |
284 ))) | |
285 | |
286 (defun profile-a-function (fun) | |
287 "Profile the function FUN" | |
288 (interactive "aFunction to profile: ") | |
289 (let ((def (symbol-function fun)) (funlen (length (symbol-name fun)))) | |
290 (if (eq (car def) 'lambda) nil | |
291 (error "To profile: %s must be a user-defined function" fun)) | |
292 (setq profile-time-list ; add a new entry | |
293 (cons (cons fun (cons 0 0)) profile-time-list)) | |
294 (setq profile-init-list ; add a new entry | |
295 (cons (cons fun (cons 0 (cons 0 0))) profile-init-list)) | |
296 (if (< profile-max-fun-name funlen) (setq profile-max-fun-name funlen)) | |
297 (fset fun (profile-fix-fun fun def)))) | |
298 | |
299 (defun profile-fix-fun (fun def) | |
300 "Take function FUN and return it fixed for profiling.\n\ | |
301 DEF is (symbol-function FUN) ." | |
302 (let (prefix first second third (count 2) inter suffix) | |
303 (if (< (length def) 3) nil ; nothing to see | |
304 (setq first (car def) second (car (cdr def)) | |
305 third (car (nthcdr 2 def))) | |
306 (setq prefix (list first second)) | |
307 (if (and (stringp third) (< (length def) 3)) nil ; nothing to see | |
308 (if (not (stringp third)) (setq inter third) | |
309 (setq count 3 ; suffix to start after doc string | |
310 prefix (nconc prefix (list third)) | |
311 inter (car (nthcdr 3 def))) ; fourth sexp | |
312 ) | |
313 (if (not (and (listp inter) | |
314 (eq (car inter) 'interactive))) nil | |
315 (setq prefix (nconc prefix (list inter))) | |
316 (setq count (1+ count))) ; skip this sexp for suffix | |
317 (setq suffix (nthcdr count def)) | |
318 (if (equal (car suffix) '(profile-get-time)) nil ;; already set | |
319 ;; prepare new function | |
320 (nconc prefix | |
321 (list '(profile-get-time)) ; read time | |
322 (list (list 'profile-start-function | |
323 (list 'quote fun))) | |
324 (list (list 'setq 'profile-temp-result- | |
325 (nconc (list 'progn) suffix))) | |
326 (list '(profile-get-time)) ; read time | |
327 (list (list 'profile-update-function | |
328 (list 'quote fun))) | |
329 (list 'profile-temp-result-) | |
330 )))))) | |
331 | |
332 (defun profile-restore-fun (fun) | |
333 "Restore profiled function FUN to its original state." | |
334 (let ((def (symbol-function (car fun))) body index) | |
335 ;; move index beyond header | |
336 (setq index (cdr def)) | |
337 (if (stringp (car (cdr index))) (setq index (cdr index))) | |
338 (if (and (listp (car (cdr index))) | |
339 (eq (car (car (cdr index))) 'interactive)) | |
340 (setq index (cdr index))) | |
341 (setq body (car (nthcdr 3 index))) | |
342 (if (and (listp body) ; the right element ? | |
343 (eq (car (cdr body)) 'profile-temp-result-)) | |
344 (setcdr index (cdr (car (cdr (cdr body)))))))) | |
345 | |
346 (defun profile-finish () | |
347 "Stop profiling functions. Clear all the settings." | |
348 (interactive) | |
349 (mapcar 'profile-restore-fun profile-time-list) | |
350 (setq profile-max-fun-name 0) | |
351 (setq profile-time-list nil) | |
352 (setq profile-init-list nil)) | |
353 | |
354 (defun profile-quit () | |
355 "Kill the timer process." | |
356 (interactive) | |
357 (process-send-string profile-timer-process "q\n")) | |
358 | |
2233
fb0ed5a1d0f3
Add standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
845
diff
changeset
|
359 ;;; profile.el ends here |