Mercurial > emacs
annotate lisp/emacs-lisp/profile.el @ 35562:6b8b7796f28b
(define-minor-mode): Docstring fix.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Thu, 25 Jan 2001 15:31:00 +0000 |
parents | 1cd5555e67a9 |
children | 70fadf968ae4 |
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 |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
3 ;; Copyright (C) 1992, 1994, 1998 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 | |
14169 | 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. | |
655 | 27 |
789
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
655
diff
changeset
|
28 ;;; Commentary: |
655 | 29 |
14169 | 30 ;; DESCRIPTION: |
31 ;; ------------ | |
32 ;; This program can be used to monitor running time performance of Emacs Lisp | |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
33 ;; functions. It takes a list of functions and report the real time spent |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
34 ;; inside these functions. (Actually, for each function it reports the amount |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
35 ;; of time spent while at least one instance of that function is on the call |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
36 ;; stack. So if profiled function FOO calls profiled function BAR, the time |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
37 ;; spent inside BAR is credited to both functions.) |
655 | 38 |
14169 | 39 ;; HOW TO USE: |
40 ;; ----------- | |
41 ;; Set the variable profile-functions-list to the list of functions | |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
42 ;; (as symbols) You want to profile. Call M-x profile-functions to set |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
43 ;; this list on and start using your program. Note that profile-functions |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
44 ;; MUST be called AFTER all the functions in profile-functions-list have |
14169 | 45 ;; been loaded !! (This call modifies the code of the profiled functions. |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
46 ;; Hence if you reload these functions, you need to call profile-functions |
14169 | 47 ;; again! ). |
48 ;; To display the results do M-x profile-results . For example: | |
49 ;;------------------------------------------------------------------- | |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
50 ;; (setq profile-functions-list '(sokoban-set-mode-line sokoban-load-game |
14169 | 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 ;;----------------------------------------------------- | |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
64 ;; To clear all the settings to profile use profile-finish. |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
65 ;; To set one function at a time (instead of or in addition to setting the |
14169 | 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 | |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
74 (defvar profile-functions-list nil "*List of functions to profile.") |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
75 (defvar profile-buffer "*profile*" |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
76 "Name of profile buffer.") |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
77 (defvar profile-distinct nil |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
78 "If non-nil, each time slice gets credited to at most one function. |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
79 \(Namely, the most recent one in the call stack.) If nil, then the |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
80 time reported for a function includes the entire time from beginning |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
81 to end, even if it called some other function that was also profiled.") |
655 | 82 |
83 ;;; | |
84 ;;; V A R I A B L E S | |
85 ;;; | |
86 | |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
87 (defvar profile-time-list nil |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
88 "List of cumulative calls and time for each profiled function. |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
89 Each element looks like (FUN NCALLS SEC . USEC).") |
655 | 90 (defvar profile-init-list nil |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
91 "List of entry time for each function. |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
92 Both how many times invoked and real time of start. |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
93 Each element looks like (FUN DEPTH HISEC LOSEC USEC), where DEPTH is |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
94 the current recursion depth, and HISEC, LOSEC, and USEC represent the |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
95 starting time of the call (or of the outermost recursion).") |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
96 (defvar profile-max-fun-name 0 |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
97 "Max length of name of any function profiled.") |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
98 (defvar profile-call-stack nil |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
99 "A list of the profiled functions currently executing. |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
100 Used only when profile-distinct is non-nil.") |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
101 (defvar profile-last-time nil |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
102 "The start time of the current time slice. |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
103 Used only when profile-distinct is non-nil.") |
655 | 104 |
18423
3934c0e29d14
(profile-million): Variable moved.
Richard M. Stallman <rms@gnu.org>
parents:
18383
diff
changeset
|
105 (defconst profile-million 1000000) |
3934c0e29d14
(profile-million): Variable moved.
Richard M. Stallman <rms@gnu.org>
parents:
18383
diff
changeset
|
106 |
655 | 107 ;;; |
108 ;;; F U N C T I O N S | |
109 ;;; | |
110 | |
111 (defun profile-functions (&optional flist) | |
16375 | 112 "Profile all the functions listed in `profile-functions-list'. |
655 | 113 With argument FLIST, use the list FLIST instead." |
15535
8895613b61dd
(profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents:
14791
diff
changeset
|
114 (interactive "P") |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
115 (mapcar 'profile-a-function (or flist profile-functions-list))) |
655 | 116 |
117 (defun profile-print (entry) | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
118 "Print one ENTRY (from `profile-time-list')." |
15535
8895613b61dd
(profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents:
14791
diff
changeset
|
119 (let* ((calls (car (cdr entry))) |
8895613b61dd
(profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents:
14791
diff
changeset
|
120 (timec (cdr (cdr entry))) |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
121 (avgtime (and (not (zerop calls)) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
122 (/ (+ (car timec) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
123 (/ (cdr timec) (float profile-million))) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
124 calls)))) |
15535
8895613b61dd
(profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents:
14791
diff
changeset
|
125 (insert (format (concat "%-" |
8895613b61dd
(profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents:
14791
diff
changeset
|
126 (int-to-string profile-max-fun-name) |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
127 "s %7d %10d.%06d") |
15535
8895613b61dd
(profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents:
14791
diff
changeset
|
128 (car entry) calls (car timec) (cdr timec)) |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
129 (if (null avgtime) |
15535
8895613b61dd
(profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents:
14791
diff
changeset
|
130 "\n" |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
131 (format " %18.6f\n" avgtime))))) |
655 | 132 |
133 (defun profile-results () | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
134 "Display profiling results in the buffer `*profile*'. |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
135 \(The buffer name comes from `profile-buffer'.)" |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
136 (interactive) |
15535
8895613b61dd
(profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents:
14791
diff
changeset
|
137 (switch-to-buffer profile-buffer) |
8895613b61dd
(profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents:
14791
diff
changeset
|
138 (erase-buffer) |
8895613b61dd
(profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents:
14791
diff
changeset
|
139 (insert "Function" (make-string (- profile-max-fun-name 6) ? )) |
8895613b61dd
(profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents:
14791
diff
changeset
|
140 (insert " Calls Total time (sec) Avg time per call\n") |
8895613b61dd
(profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents:
14791
diff
changeset
|
141 (insert (make-string profile-max-fun-name ?=) " ") |
8895613b61dd
(profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents:
14791
diff
changeset
|
142 (insert "====== ================ =================\n") |
8895613b61dd
(profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents:
14791
diff
changeset
|
143 (mapcar 'profile-print profile-time-list)) |
655 | 144 |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
145 (defun profile-add-time (dest now prev) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
146 "Add to DEST the difference between timestamps NOW and PREV. |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
147 DEST is a pair (SEC . USEC) which is modified in place. |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
148 NOW and PREV are triples as returned by `current-time'." |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
149 (let ((sec (+ (car dest) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
150 (* 65536 (- (car now) (car prev))) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
151 (- (cadr now) (cadr prev)))) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
152 (usec (+ (cdr dest) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
153 (- (car (cddr now)) (car (cddr prev)))))) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
154 (if (< usec 0) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
155 (setq sec (1- sec) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
156 usec (+ usec profile-million)) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
157 (if (>= usec profile-million) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
158 (setq sec (1+ sec) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
159 usec (- usec profile-million)))) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
160 (setcar dest sec) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
161 (setcdr dest usec))) |
655 | 162 |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
163 (defun profile-function-prolog (fun) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
164 "Mark the beginning of a call to function FUN." |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
165 (if profile-distinct |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
166 (let ((profile-time (current-time))) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
167 (if profile-call-stack |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
168 (profile-add-time (cdr (cdr (assq (car profile-call-stack) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
169 profile-time-list))) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
170 profile-time profile-last-time)) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
171 (setq profile-call-stack (cons fun profile-call-stack) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
172 profile-last-time profile-time)) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
173 (let ((profile-time (current-time)) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
174 (init-time (cdr (assq fun profile-init-list)))) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
175 (if (null init-time) (error "Function %s missing from list" fun)) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
176 (if (not (zerop (car init-time)));; is it a recursive call ? |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
177 (setcar init-time (1+ (car init-time))) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
178 (setcar init-time 1) ; mark first entry |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
179 (setcdr init-time profile-time))))) |
655 | 180 |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
181 (defun profile-function-epilog (fun) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
182 "Mark the end of a call to function FUN." |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
183 (if profile-distinct |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
184 (let ((profile-time (current-time)) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
185 (accum (cdr (assq fun profile-time-list)))) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
186 (setcar accum (1+ (car accum))) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
187 (profile-add-time (cdr accum) profile-time profile-last-time) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
188 (setq profile-call-stack (cdr profile-call-stack) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
189 profile-last-time profile-time)) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
190 (let ((profile-time (current-time)) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
191 (init-time (cdr (assq fun profile-init-list))) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
192 (accum (cdr (assq fun profile-time-list)))) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
193 (if (or (null init-time) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
194 (null accum)) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
195 (error "Function %s missing from list" fun)) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
196 (setcar init-time (1- (car init-time))) ; pop one level in recursion |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
197 ;; Update only if we've finished the outermost recursive call |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
198 (when (zerop (car init-time)) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
199 (setcar accum (1+ (car accum))) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
200 (profile-add-time (cdr accum) profile-time (cdr init-time)))))) |
655 | 201 |
14791
7d2e0f0d9bf7
(profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
202 (defun profile-convert-byte-code (function) |
7d2e0f0d9bf7
(profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
203 (let ((defn (symbol-function function))) |
7d2e0f0d9bf7
(profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
204 (if (byte-code-function-p defn) |
7d2e0f0d9bf7
(profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
205 ;; It is a compiled code object. |
7d2e0f0d9bf7
(profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
206 (let* ((contents (append defn nil)) |
7d2e0f0d9bf7
(profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
207 (body |
7d2e0f0d9bf7
(profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
208 (list (list 'byte-code (nth 1 contents) |
7d2e0f0d9bf7
(profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
209 (nth 2 contents) (nth 3 contents))))) |
7d2e0f0d9bf7
(profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
210 (if (nthcdr 5 contents) |
7d2e0f0d9bf7
(profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
211 (setq body (cons (list 'interactive (nth 5 contents)) body))) |
7d2e0f0d9bf7
(profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
212 (if (nth 4 contents) |
7d2e0f0d9bf7
(profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
213 ;; Use `documentation' here, to get the actual string, |
7d2e0f0d9bf7
(profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
214 ;; in case the compiled function has a reference |
7d2e0f0d9bf7
(profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
215 ;; to the .elc file. |
7d2e0f0d9bf7
(profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
216 (setq body (cons (documentation function) body))) |
7d2e0f0d9bf7
(profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
217 (fset function (cons 'lambda (cons (car contents) body))))))) |
7d2e0f0d9bf7
(profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
218 |
655 | 219 (defun profile-a-function (fun) |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
220 "Profile the function FUN." |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
221 (interactive "aFunction to profile: ") |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
222 (let ((def (symbol-function fun))) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
223 (when (eq (car-safe def) 'autoload) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
224 (load (car (cdr def))) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
225 (setq def (symbol-function fun))) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
226 (fetch-bytecode def)) |
14791
7d2e0f0d9bf7
(profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
227 (profile-convert-byte-code fun) |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
228 (let ((def (symbol-function fun)) (funlen (length (symbol-name fun)))) |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
229 (or (eq (car def) 'lambda) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
230 (error "To profile: %s must be a user-defined function" fun)) |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
231 (setq profile-time-list ; add a new entry |
15535
8895613b61dd
(profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents:
14791
diff
changeset
|
232 (cons (cons fun (cons 0 (cons 0 0))) profile-time-list)) |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
233 (setq profile-init-list ; add a new entry |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
234 (cons (cons fun (cons 0 nil)) profile-init-list)) |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
235 (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
|
236 (fset fun (profile-fix-fun fun def)))) |
655 | 237 |
238 (defun profile-fix-fun (fun def) | |
16375 | 239 "Take function FUN and return it fixed for profiling. |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
240 DEF is (symbol-function FUN)." |
23394
25eb153a9d4c
(profile-fix-fun): If already profiled,
Karl Heuer <kwzh@gnu.org>
parents:
21365
diff
changeset
|
241 (if (< (length def) 3) |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
242 def ; nothing to change |
23394
25eb153a9d4c
(profile-fix-fun): If already profiled,
Karl Heuer <kwzh@gnu.org>
parents:
21365
diff
changeset
|
243 (let ((prefix (list (car def) (car (cdr def)))) |
25eb153a9d4c
(profile-fix-fun): If already profiled,
Karl Heuer <kwzh@gnu.org>
parents:
21365
diff
changeset
|
244 (suffix (cdr (cdr def)))) |
16375 | 245 ;; Skip the doc string, if there is a string |
246 ;; which serves only as a doc string, | |
247 ;; and put it in PREFIX. | |
23394
25eb153a9d4c
(profile-fix-fun): If already profiled,
Karl Heuer <kwzh@gnu.org>
parents:
21365
diff
changeset
|
248 (if (and (stringp (car suffix)) (cdr suffix)) |
25eb153a9d4c
(profile-fix-fun): If already profiled,
Karl Heuer <kwzh@gnu.org>
parents:
21365
diff
changeset
|
249 (setq prefix (nconc prefix (list (car suffix))) |
25eb153a9d4c
(profile-fix-fun): If already profiled,
Karl Heuer <kwzh@gnu.org>
parents:
21365
diff
changeset
|
250 suffix (cdr suffix))) |
16375 | 251 ;; Check for an interactive spec. |
23394
25eb153a9d4c
(profile-fix-fun): If already profiled,
Karl Heuer <kwzh@gnu.org>
parents:
21365
diff
changeset
|
252 ;; If found, put it into PREFIX and skip it. |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
253 (if (and (listp (car suffix)) |
23394
25eb153a9d4c
(profile-fix-fun): If already profiled,
Karl Heuer <kwzh@gnu.org>
parents:
21365
diff
changeset
|
254 (eq (car (car suffix)) 'interactive)) |
25eb153a9d4c
(profile-fix-fun): If already profiled,
Karl Heuer <kwzh@gnu.org>
parents:
21365
diff
changeset
|
255 (setq prefix (nconc prefix (list (car suffix))) |
25eb153a9d4c
(profile-fix-fun): If already profiled,
Karl Heuer <kwzh@gnu.org>
parents:
21365
diff
changeset
|
256 suffix (cdr suffix))) |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
257 (if (eq (car-safe (car suffix)) 'profile-function-prolog) |
23394
25eb153a9d4c
(profile-fix-fun): If already profiled,
Karl Heuer <kwzh@gnu.org>
parents:
21365
diff
changeset
|
258 def ; already profiled |
16375 | 259 ;; Prepare new function definition. |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
260 ;; If you change this structure, also change profile-restore-fun. |
16375 | 261 (nconc prefix |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
262 (list (list 'profile-function-prolog |
23394
25eb153a9d4c
(profile-fix-fun): If already profiled,
Karl Heuer <kwzh@gnu.org>
parents:
21365
diff
changeset
|
263 (list 'quote fun)) |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
264 (list 'unwind-protect |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
265 (cons 'progn suffix) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
266 (list 'profile-function-epilog |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
267 (list 'quote fun))))))))) |
655 | 268 |
269 (defun profile-restore-fun (fun) | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
270 "Restore profiled function FUN to its original state." |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
271 (let ((def (symbol-function fun)) body index) |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
272 ;; move index beyond header |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
273 (setq index (cdr-safe def)) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
274 (if (stringp (car (cdr index))) |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
275 (setq index (cdr index))) |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
276 (if (eq (car-safe (car (cdr index))) 'interactive) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
277 (setq index (cdr index))) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
278 (if (eq (car-safe (car (cdr index))) 'profile-function-prolog) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
279 (setcdr index (cdr (car (cdr (car (cdr (cdr index)))))))))) |
655 | 280 |
281 (defun profile-finish () | |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
282 "Stop profiling functions. Clear all the settings." |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
283 (interactive) |
23512
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
284 (while profile-time-list |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
285 (profile-restore-fun (car (car profile-time-list))) |
1cd5555e67a9
(profile-timer-program): Var deleted.
Karl Heuer <kwzh@gnu.org>
parents:
23394
diff
changeset
|
286 (setq profile-time-list (cdr profile-time-list))) |
5790
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
287 (setq profile-max-fun-name 0) |
a539cda9df58
Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
4567
diff
changeset
|
288 (setq profile-init-list nil)) |
655 | 289 |
18383 | 290 (provide 'profile) |
291 | |
2233
fb0ed5a1d0f3
Add standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
845
diff
changeset
|
292 ;;; profile.el ends here |