]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/profile.el
Delete the hilit19 support--it doesn't work.
[gnu-emacs] / lisp / emacs-lisp / profile.el
1 ;;; profile.el --- generate run time measurements of Emacs Lisp functions
2
3 ;; Copyright (C) 1992, 1994 Free Software Foundation, Inc.
4
5 ;; Author: Boaz Ben-Zvi <boaz@lcs.mit.edu>
6 ;; Created: 07 Feb 1992
7 ;; Version: 1.0
8 ;; Adapted-By: ESR
9 ;; Keywords: lisp, tools
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
15 ;; the Free Software Foundation; either version 2, or (at your option)
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 the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;; DESCRIPTION:
31 ;; ------------
32 ;; This program can be used to monitor running time performance of Emacs Lisp
33 ;; functions. It takes a list of functions and report the real time spent
34 ;; inside these functions. It runs a process with a separate timer program.
35 ;; Caveat: the C code in ../lib-src/profile.c requires BSD-compatible
36 ;; time-of-day functions. If you're running an AT&T version prior to SVr4,
37 ;; you may have difficulty getting it to work. Your X library may supply
38 ;; the required routines if the standard C library does not.
39
40 ;; HOW TO USE:
41 ;; -----------
42 ;; Set the variable profile-functions-list to the list of functions
43 ;; (as symbols) You want to profile. Call M-x profile-functions to set
44 ;; this list on and start using your program. Note that profile-functions
45 ;; MUST be called AFTER all the functions in profile-functions-list have
46 ;; been loaded !! (This call modifies the code of the profiled functions.
47 ;; Hence if you reload these functions, you need to call profile-functions
48 ;; again! ).
49 ;; To display the results do M-x profile-results . For example:
50 ;;-------------------------------------------------------------------
51 ;; (setq profile-functions-list '(sokoban-set-mode-line sokoban-load-game
52 ;; sokoban-move-vertical sokoban-move))
53 ;; (load "sokoban")
54 ;; M-x profile-functions
55 ;; ... I play the sokoban game ..........
56 ;; M-x profile-results
57 ;;
58 ;; Function Time (Seconds.Useconds)
59 ;; ======== =======================
60 ;; sokoban-move 0.539088
61 ;; sokoban-move-vertical 0.410130
62 ;; sokoban-load-game 0.453235
63 ;; sokoban-set-mode-line 1.949203
64 ;;-----------------------------------------------------
65 ;; To clear all the settings to profile use profile-finish.
66 ;; To set one function at a time (instead of or in addition to setting the
67 ;; above list and M-x profile-functions) use M-x profile-a-function.
68
69 ;;; Code:
70
71 ;;;
72 ;;; User modifiable VARIABLES
73 ;;;
74
75 (defvar profile-functions-list nil "*List of functions to profile.")
76 (defvar profile-timer-program
77 (concat exec-directory "profile")
78 "*Name of the profile timer program.")
79
80 ;;;
81 ;;; V A R I A B L E S
82 ;;;
83
84 (defvar profile-timer-process nil "Process running the timer.")
85 (defvar profile-time-list nil
86 "List of accumulative time for each profiled function.")
87 (defvar profile-init-list nil
88 "List of entry time for each function. \n\
89 Both how many times invoked and real time of start.")
90 (defvar profile-max-fun-name 0 "Max length of name of any function profiled.")
91 (defvar profile-temp-result- nil "Should NOT be used anywhere else.")
92 (defvar profile-time (cons 0 0) "Used to return result from a filter.")
93 (defvar profile-buffer "*profile*" "Name of profile buffer.")
94
95 ;;;
96 ;;; F U N C T I O N S
97 ;;;
98
99 (defun profile-functions (&optional flist)
100 "Profile all the functions listed in `profile-functions-list'.\n\
101 With argument FLIST, use the list FLIST instead."
102 (interactive "*P")
103 (if (null flist) (setq flist profile-functions-list))
104 (mapcar 'profile-a-function flist))
105
106 (defun profile-filter (process input)
107 "Filter for the timer process. Sets `profile-time' to the returned time."
108 (if (zerop (string-match "\\." input))
109 (error "Bad output from %s" profile-timer-program)
110 (setcar profile-time
111 (string-to-int (substring input 0 (match-beginning 0))))
112 (setcdr profile-time
113 (string-to-int (substring input (match-end 0))))))
114
115
116 (defun profile-print (entry)
117 "Print one ENTRY (from `profile-time-list')."
118 (let ((time (cdr entry)) str (offset 5))
119 (insert (format "%s" (car entry)) space)
120 (move-to-column ref-column)
121 (setq str (int-to-string (car time)))
122 (insert str)
123 (if (>= (length str) offset) nil
124 (move-to-column ref-column)
125 (insert (substring spaces 0 (- offset (length str))))
126 (forward-char (length str)))
127 (setq str (int-to-string (cdr time)))
128 (insert "." (substring "000000" 0 (- 6 (length str))) str "\n")))
129
130 (defconst spaces " ")
131
132 (defun profile-results ()
133 "Display profiling results in the buffer `*profile*'.
134 \(The buffer name comes from `profile-buffer'.)"
135 (interactive)
136 (let* ((ref-column (+ 8 profile-max-fun-name))
137 (space (substring spaces 0 ref-column)))
138 (switch-to-buffer profile-buffer)
139 (erase-buffer)
140 (insert "Function" space)
141 (move-to-column ref-column)
142 (insert "Time (Seconds.Useconds)\n" "========" space )
143 (move-to-column ref-column)
144 (insert "=======================\n")
145 (mapcar 'profile-print profile-time-list)))
146
147 (defun profile-reset-timer ()
148 (process-send-string profile-timer-process "z\n"))
149
150 (defun profile-check-zero-init-times (entry)
151 "If ENTRY has non zero time, give an error."
152 (let ((time (cdr (cdr entry))))
153 (if (and (zerop (car time)) (zerop (cdr time))) nil ; OK
154 (error "Process timer died while making performance profile."))))
155
156 (defun profile-get-time ()
157 "Get time from timer process into `profile-time'."
158 ;; first time or if process dies
159 (if (and (processp profile-timer-process)
160 (eq 'run (process-status profile-timer-process))) nil
161 (setq profile-timer-process;; [re]start the timer process
162 (start-process "timer"
163 (get-buffer-create profile-buffer)
164 profile-timer-program))
165 (set-process-filter profile-timer-process 'profile-filter)
166 (process-kill-without-query profile-timer-process)
167 (profile-reset-timer)
168 ;; check if timer died during time measurement
169 (mapcar 'profile-check-zero-init-times profile-init-list))
170 ;; make timer process return current time
171 (process-send-string profile-timer-process "p\n")
172 (accept-process-output))
173
174 (defun profile-find-function (fun flist)
175 "Linear search for FUN in FLIST."
176 (if (null flist) nil
177 (if (eq fun (car (car flist))) (cdr (car flist))
178 (profile-find-function fun (cdr flist)))))
179
180 (defun profile-start-function (fun)
181 "On entry, keep current time for function FUN."
182 ;; assumes that profile-time contains the current time
183 (let ((init-time (profile-find-function fun profile-init-list)))
184 (if (null init-time) (error "Function %s missing from list" fun))
185 (if (not (zerop (car init-time)));; is it a recursive call ?
186 (setcar init-time (1+ (car init-time)))
187 (setcar init-time 1) ; mark first entry
188 (setq init-time (cdr init-time))
189 (setcar init-time (car profile-time))
190 (setcdr init-time (cdr profile-time)))
191 ))
192
193 (defconst profile-million 1000000)
194
195 (defun profile-update-function (fun)
196 "When the call to the function FUN is finished, add its run time."
197 ;; assumes that profile-time contains the current time
198 (let ((init-time (profile-find-function fun profile-init-list))
199 (accum (profile-find-function fun profile-time-list))
200 sec usec)
201 (if (or (null init-time)
202 (null accum)) (error "Function %s missing from list" fun))
203 (setcar init-time (1- (car init-time))) ; pop one level in recursion
204 (if (not (zerop (car init-time)))
205 nil ; in some recursion level, do not update accum. time
206 (setq init-time (cdr init-time))
207 (setq sec (- (car profile-time) (car init-time))
208 usec (- (cdr profile-time) (cdr init-time)))
209 (setcar init-time 0) ; reset time to check for error
210 (setcdr init-time 0) ; in case timer process dies
211 (if (>= usec 0) nil
212 (setq usec (+ usec profile-million))
213 (setq sec (1- sec)))
214 (setcar accum (+ sec (car accum)))
215 (setcdr accum (+ usec (cdr accum)))
216 (if (< (cdr accum) profile-million) nil
217 (setcar accum (1+ (car accum)))
218 (setcdr accum (- (cdr accum) profile-million)))
219 )))
220
221 (defun profile-convert-byte-code (function)
222 (let ((defn (symbol-function function)))
223 (if (byte-code-function-p defn)
224 ;; It is a compiled code object.
225 (let* ((contents (append defn nil))
226 (body
227 (list (list 'byte-code (nth 1 contents)
228 (nth 2 contents) (nth 3 contents)))))
229 (if (nthcdr 5 contents)
230 (setq body (cons (list 'interactive (nth 5 contents)) body)))
231 (if (nth 4 contents)
232 ;; Use `documentation' here, to get the actual string,
233 ;; in case the compiled function has a reference
234 ;; to the .elc file.
235 (setq body (cons (documentation function) body)))
236 (fset function (cons 'lambda (cons (car contents) body)))))))
237
238 (defun profile-a-function (fun)
239 "Profile the function FUN."
240 (interactive "aFunction to profile: ")
241 (profile-convert-byte-code fun)
242 (let ((def (symbol-function fun)) (funlen (length (symbol-name fun))))
243 (if (eq (car def) 'lambda) nil
244 (error "To profile: %s must be a user-defined function" fun))
245 (setq profile-time-list ; add a new entry
246 (cons (cons fun (cons 0 0)) profile-time-list))
247 (setq profile-init-list ; add a new entry
248 (cons (cons fun (cons 0 (cons 0 0))) profile-init-list))
249 (if (< profile-max-fun-name funlen) (setq profile-max-fun-name funlen))
250 (fset fun (profile-fix-fun fun def))))
251
252 (defun profile-fix-fun (fun def)
253 "Take function FUN and return it fixed for profiling.\n\
254 DEF is (symbol-function FUN)."
255 (let (prefix first second third (count 2) inter suffix)
256 (if (< (length def) 3) nil ; nothing to see
257 (setq first (car def) second (car (cdr def))
258 third (car (nthcdr 2 def)))
259 (setq prefix (list first second))
260 (if (and (stringp third) (< (length def) 3)) nil ; nothing to see
261 (if (not (stringp third)) (setq inter third)
262 (setq count 3 ; suffix to start after doc string
263 prefix (nconc prefix (list third))
264 inter (car (nthcdr 3 def))) ; fourth sexp
265 )
266 (if (not (and (listp inter)
267 (eq (car inter) 'interactive))) nil
268 (setq prefix (nconc prefix (list inter)))
269 (setq count (1+ count))) ; skip this sexp for suffix
270 (setq suffix (nthcdr count def))
271 (if (equal (car suffix) '(profile-get-time)) nil;; already set
272 ;; prepare new function
273 (nconc prefix
274 (list '(profile-get-time)) ; read time
275 (list (list 'profile-start-function
276 (list 'quote fun)))
277 (list (list 'setq 'profile-temp-result-
278 (nconc (list 'progn) suffix)))
279 (list '(profile-get-time)) ; read time
280 (list (list 'profile-update-function
281 (list 'quote fun)))
282 (list 'profile-temp-result-)
283 ))))))
284
285 (defun profile-restore-fun (fun)
286 "Restore profiled function FUN to its original state."
287 (let ((def (symbol-function (car fun))) body index)
288 ;; move index beyond header
289 (setq index (cdr def))
290 (if (stringp (car (cdr index))) (setq index (cdr index)))
291 (if (and (listp (car (cdr index)))
292 (eq (car (car (cdr index))) 'interactive))
293 (setq index (cdr index)))
294 (setq body (car (nthcdr 3 index)))
295 (if (and (listp body) ; the right element ?
296 (eq (car (cdr body)) 'profile-temp-result-))
297 (setcdr index (cdr (car (cdr (cdr body))))))))
298
299 (defun profile-finish ()
300 "Stop profiling functions. Clear all the settings."
301 (interactive)
302 (mapcar 'profile-restore-fun profile-time-list)
303 (setq profile-max-fun-name 0)
304 (setq profile-time-list nil)
305 (setq profile-init-list nil))
306
307 (defun profile-quit ()
308 "Kill the timer process."
309 (interactive)
310 (process-send-string profile-timer-process "q\n"))
311
312 ;;; profile.el ends here