annotate lisp/prim/profile.el @ 203:850242ba4a81 r20-3b28

Import from CVS: tag r20-3b28
author cvs
date Mon, 13 Aug 2007 10:02:21 +0200
parents eb5470882647
children e45d5e7c476e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; profile.el --- basic profiling commands for XEmacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2
199
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
3 ;; Copyright (C) 1996 Ben Wing, (C) 1997 Free Software Foundation.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
155
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 70
diff changeset
5 ;; Maintainer: XEmacs Development Team
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 70
diff changeset
6 ;; Keywords: internal
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 70
diff changeset
7
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ;; This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;; XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 ;; under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;; XEmacs is distributed in the hope that it will be useful, but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 ;; General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 ;; You should have received a copy of the GNU General Public License
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 0
diff changeset
21 ;; along with XEmacs; see the file COPYING. If not, write to the
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 16
diff changeset
22 ;; Free Software Foundation, 59 Temple Place - Suite 330,
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 0
diff changeset
23 ;; Boston, MA 02111-1307, USA.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 ;;; Synched up with: Not in FSF.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26
155
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 70
diff changeset
27 ;;; Commentary:
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 70
diff changeset
28
199
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
29 ;; In addition to Lisp-based `elp', XEmacs contains a set of
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
30 ;; primitives able to profile evaluation of Lisp functions, created by
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
31 ;; the illustrious Ben Wing. The functions in this file can be used
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
32 ;; to gain easy access to the internal profiling functions.
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
33
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
34 ;; The profiler works by catching "ticks" (actually SIGPROF signals),
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
35 ;; and looking at the current Lisp function, at the time of each tick.
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
36 ;; The output of this process is an alist with keys being the
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
37 ;; functions, and values being the number of ticks per function. From
201
eb5470882647 Import from CVS: tag r20-3b27
cvs
parents: 199
diff changeset
38 ;; this, `profiling-results' easily extracts the total number of
eb5470882647 Import from CVS: tag r20-3b27
cvs
parents: 199
diff changeset
39 ;; ticks, and the percentage CPU time of each function.
199
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
40
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
41 ;; Unless stated otherwise, profiling info is being accumulated (the
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
42 ;; current info is returned by `get-profiling-info'). Use
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
43 ;; `clear-profiling-info' to break the accumulation chain.
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
44
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
45 ;; Caveats (ELP users should read this):
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
46 ;; 1) The time reported is function time, rather than
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
47 ;; function+descendants time;
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
48 ;; 2) The Time/ms is CPU time (user+kernel), not the real time;
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
49 ;; 3) Only the actuall funcalls are profiled. If a subr Ffoo calls
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
50 ;; Fbar using Fbar (), only Ffoo will appear in the profile.
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
51
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
52 ;; A typical profiling session consists of using `clear-profiling-info'
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
53 ;; followed by `profile' or `profile-key-sequence', followed by
201
eb5470882647 Import from CVS: tag r20-3b27
cvs
parents: 199
diff changeset
54 ;; `profiling-results'.
199
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
55
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
56 ;; For instance, to see where Gnus spends time when generating Summary
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
57 ;; buffer, go to the group buffer, and press `M-x clear-profiling-info'
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
58 ;; followed by `M-x profile-key-sequence RET SPC'.
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 165
diff changeset
59
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 165
diff changeset
60
155
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 70
diff changeset
61 ;;; Code:
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 70
diff changeset
62
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
63 (defun profile-align (form width)
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
64 ;; Bletch! this is what (format "%-*s" width form) should do.
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
65 (let ((printed-form (format "%s" form)))
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
66 (concat printed-form
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
67 (make-string (max 0 (- width (length printed-form))) ?\ ))))
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
68
155
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 70
diff changeset
69 ;;;###autoload
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
70 (defun profile-results (&optional info stream)
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 165
diff changeset
71 "Print profiling info INFO to STREAM in a pretty format.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 If INFO is omitted, the current profiling info is retrieved using
199
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
73 `get-profiling-info'.
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
74 If STREAM is omitted, either a *Profiling Results* buffer or standard
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
75 output are used, depending on whether the function was called
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
76 interactively or not."
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 165
diff changeset
77 (interactive)
199
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
78 (setq info (if info
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
79 (copy-alist info)
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
80 (get-profiling-info)))
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
81 (when (and (not stream)
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
82 (interactive-p))
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
83 (pop-to-buffer (get-buffer-create "*Profiling Results*"))
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
84 (erase-buffer))
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
85 (let ((standard-output (or stream (if (interactive-p)
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
86 (current-buffer)
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
87 standard-output)))
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
88 ;; Calculate the longest function
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
89 (maxfunlen (apply #'max
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
90 (length "Function Name")
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
91 (mapcar (lambda (el)
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
92 ;; Functions longer than 40
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
93 ;; characters don't qualify
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
94 (let ((l (length (format "%s" (car el)))))
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
95 (if (< l 40)
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
96 l 0)))
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
97 info))))
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
98 (princ (format "%s Ticks %%/Total\n"
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
99 (profile-align "Function Name" maxfunlen)))
199
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
100 (princ (make-string maxfunlen ?=))
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
101 (princ " ===== =======\n")
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
102 (let ((sum (float (apply #'+ (mapcar #'cdr info)))))
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
103 (dolist (entry (nreverse (sort info #'cdr-less-than-cdr)))
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
104 (princ (format "%s %-5d %-6.3f\n"
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
105 (profile-align (car entry) maxfunlen)
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
106 (cdr entry) (* 100 (/ (cdr entry) sum)))))
199
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
107 (princ (make-string maxfunlen ?-))
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
108 (princ "--------------------\n")
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
109 (princ (format "%s %-5d %-6.2f\n"
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
110 (profile-align "Total" maxfunlen) sum 100.0))
199
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
111 (princ (format "\n\nOne tick = %g ms\n"
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
112 (/ default-profiling-interval 1000.0)))))
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
113 (when (and (not stream)
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
114 (interactive-p))
169c0442b401 Import from CVS: tag r20-3b26
cvs
parents: 173
diff changeset
115 (goto-char (point-min))))
155
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 70
diff changeset
116
201
eb5470882647 Import from CVS: tag r20-3b27
cvs
parents: 199
diff changeset
117 ;; Support the old name for a while.
eb5470882647 Import from CVS: tag r20-3b27
cvs
parents: 199
diff changeset
118 (define-obsolete-function-alias 'pretty-print-profiling-info
eb5470882647 Import from CVS: tag r20-3b27
cvs
parents: 199
diff changeset
119 'profile-results)
eb5470882647 Import from CVS: tag r20-3b27
cvs
parents: 199
diff changeset
120
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents: 155
diff changeset
121 ;;;###autoload
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents: 155
diff changeset
122 (defmacro profile (&rest forms)
173
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 167
diff changeset
123 "Turn on profiling, execute FORMS and restore profiling state.
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 167
diff changeset
124 Profiling state here means that if profiling was not in effect when
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 167
diff changeset
125 PROFILE was called, it will be turned off after FORMS are evaluated.
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 167
diff changeset
126 Otherwise, profiling will be left running.
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 167
diff changeset
127
201
eb5470882647 Import from CVS: tag r20-3b27
cvs
parents: 199
diff changeset
128 Returns the profiling info, printable by `profiling-results'."
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
129 `(let ((was-profiling (profiling-active-p)))
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
130 (unwind-protect
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents: 155
diff changeset
131 (progn
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
132 (start-profiling)
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents: 155
diff changeset
133 ,@forms)
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 201
diff changeset
134 (unless was-profiling
173
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 167
diff changeset
135 (stop-profiling)))
165
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents: 155
diff changeset
136 (get-profiling-info)))
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents: 155
diff changeset
137
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents: 155
diff changeset
138 (put 'profile 'lisp-indent-function 0)
5a88923fcbfe Import from CVS: tag r20-3b9
cvs
parents: 155
diff changeset
139
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 165
diff changeset
140 ;;;###autoload
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 165
diff changeset
141 (defun profile-key-sequence (keys)
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 165
diff changeset
142 "Dispatch the key sequence KEYS and profile the execution.
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 165
diff changeset
143 KEYS can be a vector of keypress events, a keypress event, or a character.
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 165
diff changeset
144 The function returns the profiling info."
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 165
diff changeset
145 (interactive "kProfile keystroke: ")
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 165
diff changeset
146 (and (characterp keys)
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 165
diff changeset
147 (setq keys (character-to-event keys)))
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 165
diff changeset
148 (or (vectorp keys)
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 165
diff changeset
149 (setq keys (vector keys)))
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 165
diff changeset
150 (profile
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 165
diff changeset
151 (mapc 'dispatch-event keys)))
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 165
diff changeset
152
155
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 70
diff changeset
153 ;;; profile.el ends here