Mercurial > emacs
annotate lisp/emacs-lisp/profile.el @ 13009:a418156c25ea
Rename all register-name args to `register'.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Fri, 08 Sep 1995 17:19:37 +0000 |
parents | 4fd40bd394fe |
children | 83f275dcd93a |
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 |
7298 | 3 ;; Copyright (C) 1992, 1994 Free Software Foundation, Inc. |
845 | 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. | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
34 ; Caveat: the C code in ../lib-src/profile.c requires BSD-compatible |
655 | 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 ;----------------------------------------------------- | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
64 ; To clear all the settings to profile use profile-finish. |
655 | 65 ; To set one function at a time (instead of or in addition to setting the |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
66 ; above list and M-x profile-functions) use M-x profile-a-function. |
655 | 67 |
789
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
655
diff
changeset
|
68 ;;; Code: |
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
655
diff
changeset
|
69 |
655 | 70 ;;; |
71 ;;; User modifiable VARIABLES | |
72 ;;; | |
73 | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
74 (defvar profile-functions-list nil "*List of functions to profile.") |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
75 (defvar profile-timer-program |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
76 (concat exec-directory "profile") |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
77 "*Name of the profile timer program.") |
655 | 78 |
79 ;;; | |
80 ;;; V A R I A B L E S | |
81 ;;; | |
82 | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
83 (defvar profile-timer-process nil "Process running the timer.") |
655 | 84 (defvar profile-time-list nil |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
85 "List of accumulative time for each profiled function.") |
655 | 86 (defvar profile-init-list nil |
87 "List of entry time for each function. \n\ | |
88 Both how many times invoked and real time of start.") | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
89 (defvar profile-max-fun-name 0 "Max length of name of any function profiled.") |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
90 (defvar profile-temp-result- nil "Should NOT be used anywhere else.") |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
91 (defvar profile-time (cons 0 0) "Used to return result from a filter.") |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
92 (defvar profile-buffer "*profile*" "Name of profile buffer.") |
655 | 93 |
94 ;;; | |
95 ;;; F U N C T I O N S | |
96 ;;; | |
97 | |
98 (defun profile-functions (&optional flist) | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
99 "Profile all the functions listed in `profile-functions-list'.\n\ |
655 | 100 With argument FLIST, use the list FLIST instead." |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
101 (interactive "*P") |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
102 (if (null flist) (setq flist profile-functions-list)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
103 (mapcar 'profile-a-function flist)) |
655 | 104 |
105 (defun profile-filter (process input) | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
106 "Filter for the timer process. Sets `profile-time' to the returned time." |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
107 (if (zerop (string-match "\\." input)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
108 (error "Bad output from %s" profile-timer-program) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
109 (setcar profile-time |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
110 (string-to-int (substring input 0 (match-beginning 0)))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
111 (setcdr profile-time |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
112 (string-to-int (substring input (match-end 0)))))) |
655 | 113 |
114 | |
115 (defun profile-print (entry) | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
116 "Print one ENTRY (from `profile-time-list')." |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
117 (let ((time (cdr entry)) str (offset 5)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
118 (insert (format "%s" (car entry)) space) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
119 (move-to-column ref-column) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
120 (setq str (int-to-string (car time))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
121 (insert str) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
122 (if (>= (length str) offset) nil |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
123 (move-to-column ref-column) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
124 (insert (substring spaces 0 (- offset (length str)))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
125 (forward-char (length str))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
126 (setq str (int-to-string (cdr time))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
127 (insert "." (substring "000000" 0 (- 6 (length str))) str "\n"))) |
655 | 128 |
129 (defconst spaces " ") | |
130 | |
131 (defun profile-results () | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
132 "Display profiling results in the buffer `*profile*'. |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
133 \(The buffer name comes from `profile-buffer'.)" |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
134 (interactive) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
135 (let* ((ref-column (+ 8 profile-max-fun-name)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
136 (space (substring spaces 0 ref-column))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
137 (switch-to-buffer profile-buffer) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
138 (erase-buffer) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
139 (insert "Function" space) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
140 (move-to-column ref-column) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
141 (insert "Time (Seconds.Useconds)\n" "========" space ) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
142 (move-to-column ref-column) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
143 (insert "=======================\n") |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
144 (mapcar 'profile-print profile-time-list))) |
655 | 145 |
146 (defun profile-reset-timer () | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
147 (process-send-string profile-timer-process "z\n")) |
655 | 148 |
149 (defun profile-check-zero-init-times (entry) | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
150 "If ENTRY has non zero time, give an error." |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
151 (let ((time (cdr (cdr entry)))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
152 (if (and (zerop (car time)) (zerop (cdr time))) nil ; OK |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
153 (error "Process timer died while making performance profile.")))) |
655 | 154 |
155 (defun profile-get-time () | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
156 "Get time from timer process into `profile-time'." |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
157 ;; first time or if process dies |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
158 (if (and (processp profile-timer-process) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
159 (eq 'run (process-status profile-timer-process))) nil |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
160 (setq profile-timer-process;; [re]start the timer process |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
161 (start-process "timer" |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
162 (get-buffer-create profile-buffer) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
163 profile-timer-program)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
164 (set-process-filter profile-timer-process 'profile-filter) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
165 (process-kill-without-query profile-timer-process) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
166 (profile-reset-timer) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
167 ;; check if timer died during time measurement |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
168 (mapcar 'profile-check-zero-init-times profile-init-list)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
169 ;; make timer process return current time |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
170 (process-send-string profile-timer-process "p\n") |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
171 (accept-process-output)) |
655 | 172 |
173 (defun profile-find-function (fun flist) | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
174 "Linear search for FUN in FLIST." |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
175 (if (null flist) nil |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
176 (if (eq fun (car (car flist))) (cdr (car flist)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
177 (profile-find-function fun (cdr flist))))) |
655 | 178 |
179 (defun profile-start-function (fun) | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
180 "On entry, keep current time for function FUN." |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
181 ;; assumes that profile-time contains the current time |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
182 (let ((init-time (profile-find-function fun profile-init-list))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
183 (if (null init-time) (error "Function %s missing from list" fun)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
184 (if (not (zerop (car init-time)));; is it a recursive call ? |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
185 (setcar init-time (1+ (car init-time))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
186 (setcar init-time 1) ; mark first entry |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
187 (setq init-time (cdr init-time)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
188 (setcar init-time (car profile-time)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
189 (setcdr init-time (cdr profile-time))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
190 )) |
655 | 191 |
192 (defconst profile-million 1000000) | |
193 | |
194 (defun profile-update-function (fun) | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
195 "When the call to the function FUN is finished, add its run time." |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
196 ;; assumes that profile-time contains the current time |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
197 (let ((init-time (profile-find-function fun profile-init-list)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
198 (accum (profile-find-function fun profile-time-list)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
199 sec usec) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
200 (if (or (null init-time) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
201 (null accum)) (error "Function %s missing from list" fun)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
202 (setcar init-time (1- (car init-time))) ; pop one level in recursion |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
203 (if (not (zerop (car init-time))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
204 nil ; in some recursion level, do not update accum. time |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
205 (setq init-time (cdr init-time)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
206 (setq sec (- (car profile-time) (car init-time)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
207 usec (- (cdr profile-time) (cdr init-time))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
208 (setcar init-time 0) ; reset time to check for error |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
209 (setcdr init-time 0) ; in case timer process dies |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
210 (if (>= usec 0) nil |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
211 (setq usec (+ usec profile-million)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
212 (setq sec (1- sec))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
213 (setcar accum (+ sec (car accum))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
214 (setcdr accum (+ usec (cdr accum))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
215 (if (< (cdr accum) profile-million) nil |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
216 (setcar accum (1+ (car accum))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
217 (setcdr accum (- (cdr accum) profile-million))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
218 ))) |
655 | 219 |
220 (defun profile-a-function (fun) | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
221 "Profile the function FUN." |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
222 (interactive "aFunction to profile: ") |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
223 (let ((def (symbol-function fun)) (funlen (length (symbol-name fun)))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
224 (if (eq (car def) 'lambda) nil |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
225 (error "To profile: %s must be a user-defined function" fun)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
226 (setq profile-time-list ; add a new entry |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
227 (cons (cons fun (cons 0 0)) profile-time-list)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
228 (setq profile-init-list ; add a new entry |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
229 (cons (cons fun (cons 0 (cons 0 0))) profile-init-list)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
230 (if (< profile-max-fun-name funlen) (setq profile-max-fun-name funlen)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
231 (fset fun (profile-fix-fun fun def)))) |
655 | 232 |
233 (defun profile-fix-fun (fun def) | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
234 "Take function FUN and return it fixed for profiling.\n\ |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
235 DEF is (symbol-function FUN)." |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
236 (let (prefix first second third (count 2) inter suffix) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
237 (if (< (length def) 3) nil ; nothing to see |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
238 (setq first (car def) second (car (cdr def)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
239 third (car (nthcdr 2 def))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
240 (setq prefix (list first second)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
241 (if (and (stringp third) (< (length def) 3)) nil ; nothing to see |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
242 (if (not (stringp third)) (setq inter third) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
243 (setq count 3 ; suffix to start after doc string |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
244 prefix (nconc prefix (list third)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
245 inter (car (nthcdr 3 def))) ; fourth sexp |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
246 ) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
247 (if (not (and (listp inter) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
248 (eq (car inter) 'interactive))) nil |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
249 (setq prefix (nconc prefix (list inter))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
250 (setq count (1+ count))) ; skip this sexp for suffix |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
251 (setq suffix (nthcdr count def)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
252 (if (equal (car suffix) '(profile-get-time)) nil;; already set |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
253 ;; prepare new function |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
254 (nconc prefix |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
255 (list '(profile-get-time)) ; read time |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
256 (list (list 'profile-start-function |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
257 (list 'quote fun))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
258 (list (list 'setq 'profile-temp-result- |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
259 (nconc (list 'progn) suffix))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
260 (list '(profile-get-time)) ; read time |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
261 (list (list 'profile-update-function |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
262 (list 'quote fun))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
263 (list 'profile-temp-result-) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
264 )))))) |
655 | 265 |
266 (defun profile-restore-fun (fun) | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
267 "Restore profiled function FUN to its original state." |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
268 (let ((def (symbol-function (car fun))) body index) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
269 ;; move index beyond header |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
270 (setq index (cdr def)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
271 (if (stringp (car (cdr index))) (setq index (cdr index))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
272 (if (and (listp (car (cdr index))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
273 (eq (car (car (cdr index))) 'interactive)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
274 (setq index (cdr index))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
275 (setq body (car (nthcdr 3 index))) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
276 (if (and (listp body) ; the right element ? |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
277 (eq (car (cdr body)) 'profile-temp-result-)) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
278 (setcdr index (cdr (car (cdr (cdr body)))))))) |
655 | 279 |
280 (defun profile-finish () | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
281 "Stop profiling functions. Clear all the settings." |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
282 (interactive) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
283 (mapcar 'profile-restore-fun profile-time-list) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
284 (setq profile-max-fun-name 0) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
285 (setq profile-time-list nil) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
286 (setq profile-init-list nil)) |
655 | 287 |
288 (defun profile-quit () | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
289 "Kill the timer process." |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
290 (interactive) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
291 (process-send-string profile-timer-process "q\n")) |
655 | 292 |
2233
fb0ed5a1d0f3
Add standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
845
diff
changeset
|
293 ;;; profile.el ends here |