annotate lisp/emacs-lisp/elp.el @ 45077:a34463090ca3

*** empty log message ***
author Glenn Morris <rgm@gnu.org>
date Wed, 01 May 2002 23:38:05 +0000
parents e012bc3e9105
children 695cf19ef79e d7ddb3e565de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1 ;;; elp.el --- Emacs Lisp Profiler
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2
37785
908fb90452ba (elp-instrument-function): Handle advised
Gerd Moellmann <gerd@gnu.org>
parents: 37730
diff changeset
3 ;; Copyright (C) 1994,1995,1997,1998, 2001 Free Software Foundation, Inc.
8744
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
4
38414
67b464da13ec Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents: 37785
diff changeset
5 ;; Author: Barry A. Warsaw
67b464da13ec Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents: 37785
diff changeset
6 ;; Maintainer: FSF
67b464da13ec Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents: 37785
diff changeset
7 ;; Created: 26-Feb-1994
67b464da13ec Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents: 37785
diff changeset
8 ;; Keywords: debugging lisp tools
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
9
8744
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
10 ;; This file is part of GNU Emacs.
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
11
8744
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
13 ;; it under the terms of the GNU General Public License as published by
8744
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
14 ;; the Free Software Foundation; either version 2, or (at your option)
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
15 ;; any later version.
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
16
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
17 ;; GNU Emacs is distributed in the hope that it will be useful,
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
20 ;; GNU General Public License for more details.
8744
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
21
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
22 ;; You should have received a copy of the GNU General Public License
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 10280
diff changeset
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 10280
diff changeset
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 10280
diff changeset
25 ;; Boston, MA 02111-1307, USA.
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
26
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
27 ;;; Commentary:
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
28 ;;
10280
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
29 ;; If you want to profile a bunch of functions, set elp-function-list
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
30 ;; to the list of symbols, then do a M-x elp-instrument-list. This
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
31 ;; hacks those functions so that profiling information is recorded
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
32 ;; whenever they are called. To print out the current results, use
17419
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
33 ;; M-x elp-results. If you want output to go to standard-output
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
34 ;; instead of a separate buffer, setq elp-use-standard-output to
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
35 ;; non-nil. With elp-reset-after-results set to non-nil, profiling
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
36 ;; information will be reset whenever the results are displayed. You
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
37 ;; can also reset all profiling info at any time with M-x
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
38 ;; elp-reset-all.
8744
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
39 ;;
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
40 ;; You can also instrument all functions in a package, provided that
42348
e012bc3e9105 Comment change.
Richard M. Stallman <rms@gnu.org>
parents: 42206
diff changeset
41 ;; the package follows the GNU coding standard of a common textual
10280
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
42 ;; prefix. Use M-x elp-instrument-package for this.
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
43 ;;
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
44 ;; If you want to sort the results, set elp-sort-by-function to some
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45 ;; predicate function. The three most obvious choices are predefined:
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
46 ;; elp-sort-by-call-count, elp-sort-by-average-time, and
10280
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
47 ;; elp-sort-by-total-time. Also, you can prune from the output, all
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
48 ;; functions that have been called fewer than a given number of times
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
49 ;; by setting elp-report-limit.
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
50 ;;
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51 ;; Elp can instrument byte-compiled functions just as easily as
8744
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
52 ;; interpreted functions, but it cannot instrument macros. However,
10234
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
53 ;; when you redefine a function (e.g. with eval-defun), you'll need to
10280
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
54 ;; re-instrument it with M-x elp-instrument-function. This will also
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
55 ;; reset profiling information for that function. Elp can handle
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
56 ;; interactive functions (i.e. commands), but of course any time spent
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
57 ;; idling for user prompts will show up in the timing results.
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
58 ;;
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
59 ;; You can also designate a `master' function. Profiling times will
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60 ;; be gathered for instrumented functions only during execution of
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61 ;; this master function. Thus, if you have some defuns like:
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
62 ;;
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63 ;; (defun foo () (do-something-time-intensive))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
64 ;; (defun bar () (foo))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
65 ;; (defun baz () (bar) (foo))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
66 ;;
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
67 ;; and you want to find out the amount of time spent in bar and foo,
10234
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
68 ;; but only during execution of bar, make bar the master. The call of
10280
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
69 ;; foo from baz will not add to foo's total timing sums. Use M-x
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
70 ;; elp-set-master and M-x elp-unset-master to utilize this feature.
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
71 ;; Only one master function can be set at a time.
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
72
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
73 ;; You can restore any function's original function definition with
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
74 ;; elp-restore-function. The other instrument, restore, and reset
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
75 ;; functions are provided for symmetry.
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
76
10263
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
77 ;; Here is a list of variable you can use to customize elp:
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
78 ;; elp-function-list
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
79 ;; elp-reset-after-results
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
80 ;; elp-sort-by-function
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
81 ;; elp-report-limit
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
82 ;;
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
83 ;; Here is a list of the interactive commands you can use:
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
84 ;; elp-instrument-function
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
85 ;; elp-restore-function
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
86 ;; elp-instrument-list
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
87 ;; elp-restore-list
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
88 ;; elp-instrument-package
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
89 ;; elp-restore-all
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
90 ;; elp-reset-function
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
91 ;; elp-reset-list
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
92 ;; elp-reset-all
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
93 ;; elp-set-master
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
94 ;; elp-unset-master
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
95 ;; elp-results
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
96
10280
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
97 ;; Note that there are plenty of factors that could make the times
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
98 ;; reported unreliable, including the accuracy and granularity of your
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
99 ;; system clock, and the overhead spent in lisp calculating and
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
100 ;; recording the intervals. I figure the latter is pretty constant,
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
101 ;; so while the times may not be entirely accurate, I think they'll
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
102 ;; give you a good feel for the relative amount of work spent in the
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
103 ;; various lisp routines you are profiling. Note further that times
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
104 ;; are calculated using wall-clock time, so other system load will
17419
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
105 ;; affect accuracy too.
10280
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
106
10263
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
107 ;;; Background:
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
108
17527
2f06477dce5d (elp-report-limit): Change prompt string.
Richard M. Stallman <rms@gnu.org>
parents: 17424
diff changeset
109 ;; This program was inspired by the only two existing Emacs Lisp
2f06477dce5d (elp-report-limit): Change prompt string.
Richard M. Stallman <rms@gnu.org>
parents: 17424
diff changeset
110 ;; profilers that I'm aware of, Boaz Ben-Zvi's profile.el, and Root
2f06477dce5d (elp-report-limit): Change prompt string.
Richard M. Stallman <rms@gnu.org>
parents: 17424
diff changeset
111 ;; Boy Jim's profiler.el. Both were written for Emacs 18 and both were
2f06477dce5d (elp-report-limit): Change prompt string.
Richard M. Stallman <rms@gnu.org>
parents: 17424
diff changeset
112 ;; pretty good first shots at profiling, but I found that they didn't
2f06477dce5d (elp-report-limit): Change prompt string.
Richard M. Stallman <rms@gnu.org>
parents: 17424
diff changeset
113 ;; provide the functionality or interface that I wanted, so I wrote
2f06477dce5d (elp-report-limit): Change prompt string.
Richard M. Stallman <rms@gnu.org>
parents: 17424
diff changeset
114 ;; this. I've tested elp in XEmacs 19 and Emacs 19. There's no point
2f06477dce5d (elp-report-limit): Change prompt string.
Richard M. Stallman <rms@gnu.org>
parents: 17424
diff changeset
115 ;; in even trying to make this work with Emacs 18.
10263
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
116
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
117 ;; Unlike previous profilers, elp uses Emacs 19's built-in function
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
118 ;; current-time to return interval times. This obviates the need for
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
119 ;; both an external C program and Emacs processes to communicate with
10280
ba09e85fa992 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10263
diff changeset
120 ;; such a program, and thus simplifies the package as a whole.
10234
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
121
17419
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
122 ;; TBD:
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
123 ;; Make this act like a real profiler, so that it records time spent
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
124 ;; in all branches of execution.
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
125
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
126 ;;; Code:
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
127
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
128
17527
2f06477dce5d (elp-report-limit): Change prompt string.
Richard M. Stallman <rms@gnu.org>
parents: 17424
diff changeset
129 ;; start of user configuration variables
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
130 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
131
17423
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
132 (defgroup elp nil
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
133 "Emacs Lisp Profiler"
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
134 :group 'lisp)
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
135
17423
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
136 (defcustom elp-function-list nil
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
137 "*List of functions to profile.
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
138 Used by the command `elp-instrument-list'."
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
139 :type '(repeat function)
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
140 :group 'elp)
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
141
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
142 (defcustom elp-reset-after-results t
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143 "*Non-nil means reset all profiling info after results are displayed.
17423
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
144 Results are displayed with the `elp-results' command."
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
145 :type 'boolean
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
146 :group 'elp)
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
147
17423
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
148 (defcustom elp-sort-by-function 'elp-sort-by-total-time
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
149 "*Non-nil specifies elp results sorting function.
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
150 These functions are currently available:
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
152 elp-sort-by-call-count -- sort by the highest call count
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
153 elp-sort-by-total-time -- sort by the highest total time
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154 elp-sort-by-average-time -- sort by the highest average times
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
155
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
156 You can write you're own sort function. It should adhere to the
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
157 interface specified by the PRED argument for the `sort' defun. Each
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158 \"element of LIST\" is really a 4 element vector where element 0 is
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
159 the call count, element 1 is the total time spent in the function,
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
160 element 2 is the average time spent in the function, and element 3 is
17423
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
161 the symbol's name string."
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
162 :type 'function
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
163 :group 'elp)
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
164
17423
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
165 (defcustom elp-report-limit 1
8744
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
166 "*Prevents some functions from being displayed in the results buffer.
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
167 If a number, no function that has been called fewer than that number
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
168 of times will be displayed in the output buffer. If nil, all
17423
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
169 functions will be displayed."
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
170 :type '(choice integer
17527
2f06477dce5d (elp-report-limit): Change prompt string.
Richard M. Stallman <rms@gnu.org>
parents: 17424
diff changeset
171 (const :tag "Show All" nil))
17423
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
172 :group 'elp)
8744
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
173
17423
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
174 (defcustom elp-use-standard-output nil
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
175 "*Non-nil says to output to `standard-output' instead of a buffer."
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
176 :type 'boolean
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
177 :group 'elp)
17419
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
178
17423
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
179 (defcustom elp-recycle-buffers-p t
42206
0f4506820432 Doc fix.
Pavel Janík <Pavel@Janik.cz>
parents: 41177
diff changeset
180 "*nil says to not recycle the `elp-results-buffer'.
17419
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
181 In other words, a new unique buffer is create every time you run
17423
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
182 \\[elp-results]."
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
183 :type 'boolean
43e483167dd3 Add defgroup; use defcustom for user vars.
Richard M. Stallman <rms@gnu.org>
parents: 17420
diff changeset
184 :group 'elp)
17419
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
185
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
186
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
187 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17419
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
188 ;; end of user configuration variables
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
189
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
190
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
191 (defvar elp-results-buffer "*ELP Profiling Results*"
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
192 "Buffer name for outputting profiling results.")
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
193
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
194 (defconst elp-timer-info-property 'elp-info
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
195 "ELP information property name.")
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
196
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
197 (defvar elp-all-instrumented-list nil
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
198 "List of all functions currently being instrumented.")
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
199
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
200 (defvar elp-record-p t
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
201 "Controls whether functions should record times or not.
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
202 This variable is set by the master function.")
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
203
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
204 (defvar elp-master nil
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
205 "Master function symbol.")
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
206
41177
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
207 (defvar elp-not-profilable
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
208 '(elp-wrapper elp-elapsed-time error call-interactively apply current-time interactive-p)
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
209 "List of functions that cannot be profiled.
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
210 Those functions are used internally by the profiling code and profiling
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
211 them would thus lead to infinite recursion.")
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
212
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
213 (defun elp-not-profilable-p (fun)
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
214 (or (memq fun elp-not-profilable)
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
215 (keymapp fun)
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
216 (condition-case nil
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
217 (when (subrp (symbol-function fun))
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
218 (eq 'unevalled (cdr (subr-arity (symbol-function fun)))))
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
219 (error nil))))
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
220
17527
2f06477dce5d (elp-report-limit): Change prompt string.
Richard M. Stallman <rms@gnu.org>
parents: 17424
diff changeset
221
8745
ebbea7d79174 Add autoloads.
Richard M. Stallman <rms@gnu.org>
parents: 8744
diff changeset
222 ;;;###autoload
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
223 (defun elp-instrument-function (funsym)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
224 "Instrument FUNSYM for profiling.
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
225 FUNSYM must be a symbol of a defined function."
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
226 (interactive "aFunction to instrument: ")
17419
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
227 ;; restore the function. this is necessary to avoid infinite
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
228 ;; recursion of already instrumented functions (i.e. elp-wrapper
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
229 ;; calling elp-wrapper ad infinitum). it is better to simply
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
230 ;; restore the function than to throw an error. this will work
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
231 ;; properly in the face of eval-defun because if the function was
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
232 ;; redefined, only the timer info will be nil'd out since
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
233 ;; elp-restore-function is smart enough not to trash the new
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
234 ;; definition.
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
235 (elp-restore-function funsym)
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
236 (let* ((funguts (symbol-function funsym))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
237 (infovec (vector 0 0 funguts))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
238 (newguts '(lambda (&rest args))))
41177
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
239 ;; We cannot profile functions used internally during profiling.
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
240 (when (elp-not-profilable-p funsym)
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
241 (error "ELP cannot profile the function: %s" funsym))
8744
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
242 ;; we cannot profile macros
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
243 (and (eq (car-safe funguts) 'macro)
17419
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
244 (error "ELP cannot profile macro: %s" funsym))
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
245 ;; TBD: at some point it might be better to load the autoloaded
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
246 ;; function instead of throwing an error. if we do this, then we
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
247 ;; probably want elp-instrument-package to be updated with the
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
248 ;; newly loaded list of functions. i'm not sure it's smart to do
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
249 ;; the autoload here, since that could have side effects, and
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
250 ;; elp-instrument-function is similar (in my mind) to defun-ish
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
251 ;; type functionality (i.e. it shouldn't execute the function).
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
252 (and (eq (car-safe funguts) 'autoload)
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
253 (error "ELP cannot profile autoloaded function: %s" funsym))
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
254 ;; put rest of newguts together
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
255 (if (commandp funsym)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
256 (setq newguts (append newguts '((interactive)))))
41177
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
257 (setq newguts (append newguts `((elp-wrapper
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
258 (quote ,funsym)
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
259 ,(when (commandp funsym)
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
260 '(interactive-p))
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
261 args))))
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
262 ;; to record profiling times, we set the symbol's function
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
263 ;; definition so that it runs the elp-wrapper function with the
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
264 ;; function symbol as an argument. We place the old function
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
265 ;; definition on the info vector.
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
266 ;;
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
267 ;; The info vector data structure is a 3 element vector. The 0th
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
268 ;; element is the call-count, i.e. the total number of times this
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
269 ;; function has been entered. This value is bumped up on entry to
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
270 ;; the function so that non-local exists are still recorded. TBD:
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
271 ;; I haven't tested non-local exits at all, so no guarantees.
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
272 ;;
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
273 ;; The 1st element is the total amount of time in usecs that have
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
274 ;; been spent inside this function. This number is added to on
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
275 ;; function exit.
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
276 ;;
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
277 ;; The 2nd element is the old function definition list. This gets
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
278 ;; funcall'd in between start/end time retrievals. I believe that
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
279 ;; this lets us profile even byte-compiled functions.
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
280
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
281 ;; put the info vector on the property list
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
282 (put funsym elp-timer-info-property infovec)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
283
37785
908fb90452ba (elp-instrument-function): Handle advised
Gerd Moellmann <gerd@gnu.org>
parents: 37730
diff changeset
284 ;; Set the symbol's new profiling function definition to run
908fb90452ba (elp-instrument-function): Handle advised
Gerd Moellmann <gerd@gnu.org>
parents: 37730
diff changeset
285 ;; elp-wrapper.
908fb90452ba (elp-instrument-function): Handle advised
Gerd Moellmann <gerd@gnu.org>
parents: 37730
diff changeset
286 (let ((advice-info (get funsym 'ad-advice-info)))
908fb90452ba (elp-instrument-function): Handle advised
Gerd Moellmann <gerd@gnu.org>
parents: 37730
diff changeset
287 (if advice-info
908fb90452ba (elp-instrument-function): Handle advised
Gerd Moellmann <gerd@gnu.org>
parents: 37730
diff changeset
288 (progn
908fb90452ba (elp-instrument-function): Handle advised
Gerd Moellmann <gerd@gnu.org>
parents: 37730
diff changeset
289 ;; If function is advised, don't let Advice change
908fb90452ba (elp-instrument-function): Handle advised
Gerd Moellmann <gerd@gnu.org>
parents: 37730
diff changeset
290 ;; its definition from under us during the `fset'.
908fb90452ba (elp-instrument-function): Handle advised
Gerd Moellmann <gerd@gnu.org>
parents: 37730
diff changeset
291 (put funsym 'ad-advice-info nil)
908fb90452ba (elp-instrument-function): Handle advised
Gerd Moellmann <gerd@gnu.org>
parents: 37730
diff changeset
292 (fset funsym newguts)
908fb90452ba (elp-instrument-function): Handle advised
Gerd Moellmann <gerd@gnu.org>
parents: 37730
diff changeset
293 (put funsym 'ad-advice-info advice-info))
908fb90452ba (elp-instrument-function): Handle advised
Gerd Moellmann <gerd@gnu.org>
parents: 37730
diff changeset
294 (fset funsym newguts)))
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
295
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
296 ;; add this function to the instrumentation list
41177
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
297 (unless (memq funsym elp-all-instrumented-list)
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
298 (push funsym elp-all-instrumented-list))))
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
299
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
300 (defun elp-restore-function (funsym)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
301 "Restore an instrumented function to its original definition.
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
302 Argument FUNSYM is the symbol of a defined function."
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
303 (interactive "aFunction to restore: ")
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
304 (let ((info (get funsym elp-timer-info-property)))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
305 ;; delete the function from the all instrumented list
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
306 (setq elp-all-instrumented-list
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
307 (delq funsym elp-all-instrumented-list))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
308
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
309 ;; if the function was the master, reset the master
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
310 (if (eq funsym elp-master)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
311 (setq elp-master nil
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
312 elp-record-p t))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
313
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
314 ;; zap the properties
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
315 (put funsym elp-timer-info-property nil)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
316
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
317 ;; restore the original function definition, but if the function
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
318 ;; wasn't instrumented do nothing. we do this after the above
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
319 ;; because its possible the function got un-instrumented due to
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
320 ;; circumstances beyond our control. Also, check to make sure
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
321 ;; that the current function symbol points to elp-wrapper. If
17419
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
322 ;; not, then the user probably did an eval-defun, or loaded a
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
323 ;; byte-compiled version, while the function was instrumented and
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
324 ;; we don't want to destroy the new definition. can it ever be
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
325 ;; the case that a lisp function can be compiled instrumented?
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
326 (and info
17420
782c3dac70b1 (elp-functionp): Definitions deleted; use functionp.
Richard M. Stallman <rms@gnu.org>
parents: 17419
diff changeset
327 (functionp funsym)
29211
88e33ac31c14 (elp-restore-function): Don't use obsolete byte-code-function-p.
Dave Love <fx@gnu.org>
parents: 21172
diff changeset
328 (not (byte-code-function-p (symbol-function funsym)))
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
329 (assq 'elp-wrapper (symbol-function funsym))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
330 (fset funsym (aref info 2)))))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
331
8745
ebbea7d79174 Add autoloads.
Richard M. Stallman <rms@gnu.org>
parents: 8744
diff changeset
332 ;;;###autoload
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
333 (defun elp-instrument-list (&optional list)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
334 "Instrument for profiling, all functions in `elp-function-list'.
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
335 Use optional LIST if provided instead."
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
336 (interactive "PList of functions to instrument: ")
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
337 (let ((list (or list elp-function-list)))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
338 (mapcar 'elp-instrument-function list)))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
339
8745
ebbea7d79174 Add autoloads.
Richard M. Stallman <rms@gnu.org>
parents: 8744
diff changeset
340 ;;;###autoload
8744
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
341 (defun elp-instrument-package (prefix)
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
342 "Instrument for profiling, all functions which start with PREFIX.
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
343 For example, to instrument all ELP functions, do the following:
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
344
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
345 \\[elp-instrument-package] RET elp- RET"
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
346 (interactive "sPrefix of package to instrument: ")
37730
76d0f25cf1a3 (elp-instrument-package): Don't allow empty prefixes.
Eli Zaretskii <eliz@gnu.org>
parents: 33128
diff changeset
347 (if (zerop (length prefix))
38414
67b464da13ec Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents: 37785
diff changeset
348 (error "Instrumenting all Emacs functions would render Emacs unusable"))
8744
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
349 (elp-instrument-list
17527
2f06477dce5d (elp-report-limit): Change prompt string.
Richard M. Stallman <rms@gnu.org>
parents: 17424
diff changeset
350 (mapcar
2f06477dce5d (elp-report-limit): Change prompt string.
Richard M. Stallman <rms@gnu.org>
parents: 17424
diff changeset
351 'intern
2f06477dce5d (elp-report-limit): Change prompt string.
Richard M. Stallman <rms@gnu.org>
parents: 17424
diff changeset
352 (all-completions
2f06477dce5d (elp-report-limit): Change prompt string.
Richard M. Stallman <rms@gnu.org>
parents: 17424
diff changeset
353 prefix obarray
41177
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
354 (lambda (sym)
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
355 (and (fboundp sym)
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
356 (not (or (memq (car-safe (symbol-function sym)) '(autoload macro))
830a17080380 (elp-not-profilable): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38414
diff changeset
357 (elp-not-profilable-p sym)))))))))
8744
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
358
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
359 (defun elp-restore-list (&optional list)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
360 "Restore the original definitions for all functions in `elp-function-list'.
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
361 Use optional LIST if provided instead."
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
362 (interactive "PList of functions to restore: ")
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
363 (let ((list (or list elp-function-list)))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
364 (mapcar 'elp-restore-function list)))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
365
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
366 (defun elp-restore-all ()
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
367 "Restores the original definitions of all functions being profiled."
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
368 (interactive)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
369 (elp-restore-list elp-all-instrumented-list))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
370
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
371
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
372 (defun elp-reset-function (funsym)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
373 "Reset the profiling information for FUNSYM."
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
374 (interactive "aFunction to reset: ")
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
375 (let ((info (get funsym elp-timer-info-property)))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
376 (or info
38414
67b464da13ec Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents: 37785
diff changeset
377 (error "%s is not instrumented for profiling" funsym))
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
378 (aset info 0 0) ;reset call counter
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
379 (aset info 1 0.0) ;reset total time
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
380 ;; don't muck with aref 2 as that is the old symbol definition
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
381 ))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
382
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
383 (defun elp-reset-list (&optional list)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
384 "Reset the profiling information for all functions in `elp-function-list'.
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
385 Use optional LIST if provided instead."
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
386 (interactive "PList of functions to reset: ")
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
387 (let ((list (or list elp-function-list)))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
388 (mapcar 'elp-reset-function list)))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
389
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
390 (defun elp-reset-all ()
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
391 "Reset the profiling information for all functions being profiled."
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
392 (interactive)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
393 (elp-reset-list elp-all-instrumented-list))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
394
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
395 (defun elp-set-master (funsym)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
396 "Set the master function for profiling."
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
397 (interactive "aMaster function: ")
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
398 ;; when there's a master function, recording is turned off by
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
399 ;; default
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
400 (setq elp-master funsym
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
401 elp-record-p nil)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
402 ;; make sure master function is instrumented
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
403 (or (memq funsym elp-all-instrumented-list)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
404 (elp-instrument-function funsym)))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
405
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
406 (defun elp-unset-master ()
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
407 "Unsets the master function."
10234
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
408 (interactive)
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
409 ;; when there's no master function, recording is turned on by default.
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
410 (setq elp-master nil
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
411 elp-record-p t))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
412
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
413
17419
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
414 (defsubst elp-elapsed-time (start end)
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
415 (+ (* (- (car end) (car start)) 65536.0)
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
416 (- (car (cdr end)) (car (cdr start)))
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
417 (/ (- (car (cdr (cdr end))) (car (cdr (cdr start)))) 1000000.0)))
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
418
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
419 (defun elp-wrapper (funsym interactive-p args)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
420 "This function has been instrumented for profiling by the ELP.
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
421 ELP is the Emacs Lisp Profiler. To restore the function to its
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
422 original definition, use \\[elp-restore-function] or \\[elp-restore-all]."
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
423 ;; turn on recording if this is the master function
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
424 (if (and elp-master
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
425 (eq funsym elp-master))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
426 (setq elp-record-p t))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
427 ;; get info vector and original function symbol
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
428 (let* ((info (get funsym elp-timer-info-property))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
429 (func (aref info 2))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
430 result)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
431 (or func
38414
67b464da13ec Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents: 37785
diff changeset
432 (error "%s is not instrumented for profiling" funsym))
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
433 (if (not elp-record-p)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
434 ;; when not recording, just call the original function symbol
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
435 ;; and return the results.
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
436 (setq result
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
437 (if interactive-p
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
438 (call-interactively func)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
439 (apply func args)))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
440 ;; we are recording times
17419
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
441 (let (enter-time exit-time)
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
442 ;; increment the call-counter
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
443 (aset info 0 (1+ (aref info 0)))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
444 ;; now call the old symbol function, checking to see if it
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
445 ;; should be called interactively. make sure we return the
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
446 ;; correct value
17419
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
447 (if interactive-p
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
448 (setq enter-time (current-time)
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
449 result (call-interactively func)
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
450 exit-time (current-time))
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
451 (setq enter-time (current-time)
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
452 result (apply func args)
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
453 exit-time (current-time)))
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
454 ;; calculate total time in function
17419
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
455 (aset info 1 (+ (aref info 1) (elp-elapsed-time enter-time exit-time)))
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
456 ))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
457 ;; turn off recording if this is the master function
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
458 (if (and elp-master
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
459 (eq funsym elp-master))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
460 (setq elp-record-p nil))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
461 result))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
462
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
463
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
464 ;; shut the byte-compiler up
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
465 (defvar elp-field-len nil)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
466 (defvar elp-cc-len nil)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
467 (defvar elp-at-len nil)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
468 (defvar elp-et-len nil)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
469
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
470 (defun elp-sort-by-call-count (vec1 vec2)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
471 ;; sort by highest call count. See `sort'.
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
472 (>= (aref vec1 0) (aref vec2 0)))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
473
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
474 (defun elp-sort-by-total-time (vec1 vec2)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
475 ;; sort by highest total time spent in function. See `sort'.
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
476 (>= (aref vec1 1) (aref vec2 1)))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
477
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
478 (defun elp-sort-by-average-time (vec1 vec2)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
479 ;; sort by highest average time spent in function. See `sort'.
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
480 (>= (aref vec1 2) (aref vec2 2)))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
481
10234
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
482 (defsubst elp-pack-number (number width)
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
483 ;; pack the NUMBER string into WIDTH characters, watching out for
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
484 ;; very small or large numbers
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
485 (if (<= (length number) width)
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
486 number
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
487 ;; check for very large or small numbers
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
488 (if (string-match "^\\(.*\\)\\(e[+-].*\\)$" number)
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
489 (concat (substring
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
490 (substring number (match-beginning 1) (match-end 1))
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
491 0
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
492 (- width (match-end 2) (- (match-beginning 2)) 3))
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
493 "..."
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
494 (substring number (match-beginning 2) (match-end 2)))
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
495 (concat (substring number 0 width)))))
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
496
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
497 (defun elp-output-result (resultvec)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
498 ;; output the RESULTVEC into the results buffer. RESULTVEC is a 4 or
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
499 ;; more element vector where aref 0 is the call count, aref 1 is the
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
500 ;; total time spent in the function, aref 2 is the average time
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
501 ;; spent in the function, and aref 3 is the symbol's string
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
502 ;; name. All other elements in the vector are ignored.
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
503 (let* ((cc (aref resultvec 0))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
504 (tt (aref resultvec 1))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
505 (at (aref resultvec 2))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
506 (symname (aref resultvec 3))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
507 callcnt totaltime avetime)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
508 (setq callcnt (number-to-string cc)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
509 totaltime (number-to-string tt)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
510 avetime (number-to-string at))
8744
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
511 ;; possibly prune the results
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
512 (if (and elp-report-limit
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
513 (numberp elp-report-limit)
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
514 (< cc elp-report-limit))
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
515 nil
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
516 (insert symname)
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
517 (insert-char 32 (+ elp-field-len (- (length symname)) 2))
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
518 ;; print stuff out, formatting it nicely
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
519 (insert callcnt)
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
520 (insert-char 32 (+ elp-cc-len (- (length callcnt)) 2))
10234
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
521 (let ((ttstr (elp-pack-number totaltime elp-et-len))
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
522 (atstr (elp-pack-number avetime elp-at-len)))
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
523 (insert ttstr)
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
524 (insert-char 32 (+ elp-et-len (- (length ttstr)) 2))
170c4c188d4f (elp-pack-number): New function.
Richard M. Stallman <rms@gnu.org>
parents: 8745
diff changeset
525 (insert atstr))
8744
e1b824af0849 Update to 2.15.
Richard M. Stallman <rms@gnu.org>
parents: 8735
diff changeset
526 (insert "\n"))))
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
527
8745
ebbea7d79174 Add autoloads.
Richard M. Stallman <rms@gnu.org>
parents: 8744
diff changeset
528 ;;;###autoload
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
529 (defun elp-results ()
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
530 "Display current profiling results.
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
531 If `elp-reset-after-results' is non-nil, then current profiling
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
532 information for all instrumented functions are reset after results are
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
533 displayed."
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
534 (interactive)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
535 (let ((curbuf (current-buffer))
17419
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
536 (resultsbuf (if elp-recycle-buffers-p
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
537 (get-buffer-create elp-results-buffer)
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
538 (generate-new-buffer elp-results-buffer))))
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
539 (set-buffer resultsbuf)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
540 (erase-buffer)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
541 (beginning-of-buffer)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
542 ;; get the length of the longest function name being profiled
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
543 (let* ((longest 0)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
544 (title "Function Name")
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
545 (titlelen (length title))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
546 (elp-field-len titlelen)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
547 (cc-header "Call Count")
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
548 (elp-cc-len (length cc-header))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
549 (et-header "Elapsed Time")
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
550 (elp-et-len (length et-header))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
551 (at-header "Average Time")
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
552 (elp-at-len (length at-header))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
553 (resvec
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
554 (mapcar
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
555 (function
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
556 (lambda (funsym)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
557 (let* ((info (get funsym elp-timer-info-property))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
558 (symname (format "%s" funsym))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
559 (cc (aref info 0))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
560 (tt (aref info 1)))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
561 (if (not info)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
562 (insert "No profiling information found for: "
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
563 symname)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
564 (setq longest (max longest (length symname)))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
565 (vector cc tt (if (zerop cc)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
566 0.0 ;avoid arithmetic div-by-zero errors
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
567 (/ (float tt) (float cc)))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
568 symname)))))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
569 elp-all-instrumented-list))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
570 ) ; end let*
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
571 (insert title)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
572 (if (> longest titlelen)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
573 (progn
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
574 (insert-char 32 (- longest titlelen))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
575 (setq elp-field-len longest)))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
576 (insert " " cc-header " " et-header " " at-header "\n")
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
577 (insert-char ?= elp-field-len)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
578 (insert " ")
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
579 (insert-char ?= elp-cc-len)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
580 (insert " ")
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
581 (insert-char ?= elp-et-len)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
582 (insert " ")
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
583 (insert-char ?= elp-at-len)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
584 (insert "\n")
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
585 ;; if sorting is enabled, then sort the results list. in either
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
586 ;; case, call elp-output-result to output the result in the
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
587 ;; buffer
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
588 (if elp-sort-by-function
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
589 (setq resvec (sort resvec elp-sort-by-function)))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
590 (mapcar 'elp-output-result resvec))
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
591 ;; now pop up results buffer
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
592 (set-buffer curbuf)
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
593 (pop-to-buffer resultsbuf)
17419
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
594 ;; copy results to standard-output?
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
595 (if (or elp-use-standard-output noninteractive)
c9f73399244c (elp-functionp): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14858
diff changeset
596 (princ (buffer-substring (point-min) (point-max))))
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
597 ;; reset profiling info if desired
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
598 (and elp-reset-after-results
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
599 (elp-reset-all))))
33095
ef9eb0f5f0b6 (elp-unload-hook): New function.
Dave Love <fx@gnu.org>
parents: 29245
diff changeset
600
ef9eb0f5f0b6 (elp-unload-hook): New function.
Dave Love <fx@gnu.org>
parents: 29245
diff changeset
601 (defun elp-unload-hook ()
ef9eb0f5f0b6 (elp-unload-hook): New function.
Dave Love <fx@gnu.org>
parents: 29245
diff changeset
602 (elp-restore-all))
8735
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
603
d1f0811de024 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
604 (provide 'elp)
10263
525b67bc4f17 Comment changes.
Richard M. Stallman <rms@gnu.org>
parents: 10234
diff changeset
605
38414
67b464da13ec Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents: 37785
diff changeset
606 ;;; elp.el ends here