annotate lisp/emacs-lisp/profile.el @ 22265:46bb1b22d269

(comint-get-next-from-history): New command. (comint-accumulate, comint-goto-process-mark): Likewise. (comint-set-process-mark, comint-bol-or-process-mark): Likewise. (comint-save-input-ring-index): New permanent local var. (comint-accum-marker): Likewise. (comint-send-input): Clear comint-input-ring-index and comint-accum-marker. (comint-previous-matching-input-from-input): Use comint-accum-marker if set. (comint-previous-matching-input): Likewise. (comint-mode): Make those vars buffer-local and initialize. (comint-mode-map): Make C-c C-a run comint-bol-or-process-mark. Bind C-c SPC to comint-accumulate. Bind C-c C-q to comint-get-next-from-history.
author Richard M. Stallman <rms@gnu.org>
date Wed, 27 May 1998 21:06:56 +0000
parents db005054f15d
children 25eb153a9d4c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2233
fb0ed5a1d0f3 Add standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 845
diff changeset
1 ;;; profile.el --- generate run time measurements of Emacs Lisp functions
789
71d052f72ac1 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 655
diff changeset
2
7298
4fd40bd394fe Update copyright.
Karl Heuer <kwzh@gnu.org>
parents: 5790
diff changeset
3 ;; Copyright (C) 1992, 1994 Free Software Foundation, Inc.
845
213978acbc1e entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
4
789
71d052f72ac1 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 655
diff changeset
5 ;; Author: Boaz Ben-Zvi <boaz@lcs.mit.edu>
807
4f28bd14272c *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 792
diff changeset
6 ;; Created: 07 Feb 1992
789
71d052f72ac1 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 655
diff changeset
7 ;; Version: 1.0
71d052f72ac1 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 655
diff changeset
8 ;; Adapted-By: ESR
814
38b2499cb3e9 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 807
diff changeset
9 ;; Keywords: lisp, tools
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
10
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
11 ;; This file is part of GNU Emacs.
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
12
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
14 ;; it under the terms of the GNU General Public License as published by
807
4f28bd14272c *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 792
diff changeset
15 ;; the Free Software Foundation; either version 2, or (at your option)
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
16 ;; any later version.
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
17
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
18 ;; GNU Emacs is distributed in the hope that it will be useful,
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
21 ;; GNU General Public License for more details.
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
22
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
23 ;; You should have received a copy of the GNU General Public License
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
26 ;; Boston, MA 02111-1307, USA.
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
27
789
71d052f72ac1 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 655
diff changeset
28 ;;; Commentary:
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
29
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
30 ;; DESCRIPTION:
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
31 ;; ------------
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
32 ;; This program can be used to monitor running time performance of Emacs Lisp
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
33 ;; functions. It takes a list of functions and report the real time spent
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
34 ;; inside these functions. It runs a process with a separate timer program.
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
35 ;; Caveat: the C code in ../lib-src/profile.c requires BSD-compatible
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
36 ;; time-of-day functions. If you're running an AT&T version prior to SVr4,
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
37 ;; you may have difficulty getting it to work. Your X library may supply
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
38 ;; the required routines if the standard C library does not.
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
39
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
40 ;; HOW TO USE:
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
41 ;; -----------
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
42 ;; Set the variable profile-functions-list to the list of functions
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
43 ;; (as symbols) You want to profile. Call M-x profile-functions to set
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
44 ;; this list on and start using your program. Note that profile-functions
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
45 ;; MUST be called AFTER all the functions in profile-functions-list have
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
46 ;; been loaded !! (This call modifies the code of the profiled functions.
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
47 ;; Hence if you reload these functions, you need to call profile-functions
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
48 ;; again! ).
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
49 ;; To display the results do M-x profile-results . For example:
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
50 ;;-------------------------------------------------------------------
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
51 ;; (setq profile-functions-list '(sokoban-set-mode-line sokoban-load-game
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
52 ;; sokoban-move-vertical sokoban-move))
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
53 ;; (load "sokoban")
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
54 ;; M-x profile-functions
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
55 ;; ... I play the sokoban game ..........
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
56 ;; M-x profile-results
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
57 ;;
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
58 ;; Function Time (Seconds.Useconds)
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
59 ;; ======== =======================
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
60 ;; sokoban-move 0.539088
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
61 ;; sokoban-move-vertical 0.410130
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
62 ;; sokoban-load-game 0.453235
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
63 ;; sokoban-set-mode-line 1.949203
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
64 ;;-----------------------------------------------------
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
65 ;; To clear all the settings to profile use profile-finish.
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
66 ;; To set one function at a time (instead of or in addition to setting the
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 7298
diff changeset
67 ;; above list and M-x profile-functions) use M-x profile-a-function.
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
68
789
71d052f72ac1 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 655
diff changeset
69 ;;; Code:
71d052f72ac1 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 655
diff changeset
70
21365
db005054f15d Customized.
Stephen Eglen <stephen@gnu.org>
parents: 18423
diff changeset
71 (defgroup profile nil
db005054f15d Customized.
Stephen Eglen <stephen@gnu.org>
parents: 18423
diff changeset
72 "Generate run time measurements of Emacs Lisp functions."
db005054f15d Customized.
Stephen Eglen <stephen@gnu.org>
parents: 18423
diff changeset
73 :prefix "profile-"
db005054f15d Customized.
Stephen Eglen <stephen@gnu.org>
parents: 18423
diff changeset
74 :group 'lisp)
db005054f15d Customized.
Stephen Eglen <stephen@gnu.org>
parents: 18423
diff changeset
75
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
76 ;;;
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
77 ;;; User modifiable VARIABLES
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
78 ;;;
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
79
21365
db005054f15d Customized.
Stephen Eglen <stephen@gnu.org>
parents: 18423
diff changeset
80 (defcustom profile-functions-list nil
db005054f15d Customized.
Stephen Eglen <stephen@gnu.org>
parents: 18423
diff changeset
81 "*List of functions to profile."
db005054f15d Customized.
Stephen Eglen <stephen@gnu.org>
parents: 18423
diff changeset
82 :type '(repeat function)
db005054f15d Customized.
Stephen Eglen <stephen@gnu.org>
parents: 18423
diff changeset
83 :group 'profile)
db005054f15d Customized.
Stephen Eglen <stephen@gnu.org>
parents: 18423
diff changeset
84
db005054f15d Customized.
Stephen Eglen <stephen@gnu.org>
parents: 18423
diff changeset
85 (defcustom profile-timer-program
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
86 (concat exec-directory "profile")
21365
db005054f15d Customized.
Stephen Eglen <stephen@gnu.org>
parents: 18423
diff changeset
87 "*Name of the profile timer program."
db005054f15d Customized.
Stephen Eglen <stephen@gnu.org>
parents: 18423
diff changeset
88 :type 'file
db005054f15d Customized.
Stephen Eglen <stephen@gnu.org>
parents: 18423
diff changeset
89 :group 'profile)
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
90
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
91 ;;;
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
92 ;;; V A R I A B L E S
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
93 ;;;
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
94
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
95 (defvar profile-timer-process nil "Process running the timer.")
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
96 (defvar profile-time-list nil
15535
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
97 "List of cumulative calls and time for each profiled function.")
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
98 (defvar profile-init-list nil
16375
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
99 "List of entry time for each function.
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
100 Both how many times invoked and real time of start.")
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
101 (defvar profile-max-fun-name 0 "Max length of name of any function profiled.")
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
102 (defvar profile-temp-result- nil "Should NOT be used anywhere else.")
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
103 (defvar profile-time (cons 0 0) "Used to return result from a filter.")
21365
db005054f15d Customized.
Stephen Eglen <stephen@gnu.org>
parents: 18423
diff changeset
104 (defcustom profile-buffer "*profile*"
db005054f15d Customized.
Stephen Eglen <stephen@gnu.org>
parents: 18423
diff changeset
105 "Name of profile buffer."
db005054f15d Customized.
Stephen Eglen <stephen@gnu.org>
parents: 18423
diff changeset
106 :type 'string
db005054f15d Customized.
Stephen Eglen <stephen@gnu.org>
parents: 18423
diff changeset
107 :group 'profile)
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
108
18423
3934c0e29d14 (profile-million): Variable moved.
Richard M. Stallman <rms@gnu.org>
parents: 18383
diff changeset
109 (defconst profile-million 1000000)
3934c0e29d14 (profile-million): Variable moved.
Richard M. Stallman <rms@gnu.org>
parents: 18383
diff changeset
110
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
111 ;;;
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
112 ;;; F U N C T I O N S
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
113 ;;;
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
114
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
115 (defun profile-functions (&optional flist)
16375
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
116 "Profile all the functions listed in `profile-functions-list'.
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
117 With argument FLIST, use the list FLIST instead."
15535
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
118 (interactive "P")
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
119 (if (null flist) (setq flist profile-functions-list))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
120 (mapcar 'profile-a-function flist))
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
121
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
122 (defun profile-filter (process input)
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
123 "Filter for the timer process. Sets `profile-time' to the returned time."
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
124 (if (zerop (string-match "\\." input))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
125 (error "Bad output from %s" profile-timer-program)
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
126 (setcar profile-time
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
127 (string-to-int (substring input 0 (match-beginning 0))))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
128 (setcdr profile-time
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
129 (string-to-int (substring input (match-end 0))))))
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
130
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
131
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
132 (defun profile-print (entry)
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
133 "Print one ENTRY (from `profile-time-list')."
15535
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
134 (let* ((calls (car (cdr entry)))
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
135 (timec (cdr (cdr entry)))
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
136 (time (+ (car timec) (/ (cdr timec) (float profile-million))))
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
137 (avgtime 0.0))
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
138 (insert (format (concat "%-"
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
139 (int-to-string profile-max-fun-name)
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
140 "s%8d%11d.%06d")
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
141 (car entry) calls (car timec) (cdr timec))
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
142 (if (zerop calls)
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
143 "\n"
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
144 (format "%12d.%06d\n"
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
145 (truncate (setq avgtime (/ time calls)))
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
146 (truncate (* (- avgtime (ftruncate avgtime))
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
147 profile-million))))
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
148 )))
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
149
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
150 (defun profile-results ()
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
151 "Display profiling results in the buffer `*profile*'.
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
152 \(The buffer name comes from `profile-buffer'.)"
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
153 (interactive)
15535
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
154 (switch-to-buffer profile-buffer)
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
155 (erase-buffer)
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
156 (insert "Function" (make-string (- profile-max-fun-name 6) ? ))
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
157 (insert " Calls Total time (sec) Avg time per call\n")
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
158 (insert (make-string profile-max-fun-name ?=) " ")
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
159 (insert "====== ================ =================\n")
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
160 (mapcar 'profile-print profile-time-list))
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
161
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
162 (defun profile-reset-timer ()
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
163 (process-send-string profile-timer-process "z\n"))
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
164
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
165 (defun profile-check-zero-init-times (entry)
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
166 "If ENTRY has non zero time, give an error."
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
167 (let ((time (cdr (cdr entry))))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
168 (if (and (zerop (car time)) (zerop (cdr time))) nil ; OK
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
169 (error "Process timer died while making performance profile."))))
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
170
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
171 (defun profile-get-time ()
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
172 "Get time from timer process into `profile-time'."
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
173 ;; first time or if process dies
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
174 (if (and (processp profile-timer-process)
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
175 (eq 'run (process-status profile-timer-process))) nil
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
176 (setq profile-timer-process;; [re]start the timer process
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
177 (start-process "timer"
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
178 (get-buffer-create profile-buffer)
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
179 profile-timer-program))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
180 (set-process-filter profile-timer-process 'profile-filter)
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
181 (process-kill-without-query profile-timer-process)
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
182 (profile-reset-timer)
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
183 ;; check if timer died during time measurement
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
184 (mapcar 'profile-check-zero-init-times profile-init-list))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
185 ;; make timer process return current time
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
186 (process-send-string profile-timer-process "p\n")
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
187 (accept-process-output))
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
188
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
189 (defun profile-find-function (fun flist)
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
190 "Linear search for FUN in FLIST."
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
191 (if (null flist) nil
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
192 (if (eq fun (car (car flist))) (cdr (car flist))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
193 (profile-find-function fun (cdr flist)))))
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
194
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
195 (defun profile-start-function (fun)
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
196 "On entry, keep current time for function FUN."
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
197 ;; assumes that profile-time contains the current time
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
198 (let ((init-time (profile-find-function fun profile-init-list)))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
199 (if (null init-time) (error "Function %s missing from list" fun))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
200 (if (not (zerop (car init-time)));; is it a recursive call ?
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
201 (setcar init-time (1+ (car init-time)))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
202 (setcar init-time 1) ; mark first entry
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
203 (setq init-time (cdr init-time))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
204 (setcar init-time (car profile-time))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
205 (setcdr init-time (cdr profile-time)))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
206 ))
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
207
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
208 (defun profile-update-function (fun)
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
209 "When the call to the function FUN is finished, add its run time."
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
210 ;; assumes that profile-time contains the current time
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
211 (let ((init-time (profile-find-function fun profile-init-list))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
212 (accum (profile-find-function fun profile-time-list))
15535
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
213 calls time sec usec)
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
214 (if (or (null init-time)
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
215 (null accum)) (error "Function %s missing from list" fun))
15535
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
216 (setq calls (car accum))
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
217 (setq time (cdr accum))
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
218 (setcar init-time (1- (car init-time))) ; pop one level in recursion
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
219 (if (not (zerop (car init-time)))
15535
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
220 nil ; in some recursion level,
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
221 ; do not update cumulated time
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
222 (setcar accum (1+ calls))
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
223 (setq init-time (cdr init-time))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
224 (setq sec (- (car profile-time) (car init-time))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
225 usec (- (cdr profile-time) (cdr init-time)))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
226 (setcar init-time 0) ; reset time to check for error
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
227 (setcdr init-time 0) ; in case timer process dies
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
228 (if (>= usec 0) nil
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
229 (setq usec (+ usec profile-million))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
230 (setq sec (1- sec)))
15535
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
231 (setcar time (+ sec (car time)))
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
232 (setcdr time (+ usec (cdr time)))
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
233 (if (< (cdr time) profile-million) nil
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
234 (setcar time (1+ (car time)))
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
235 (setcdr time (- (cdr time) profile-million)))
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
236 )))
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
237
14791
7d2e0f0d9bf7 (profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
238 (defun profile-convert-byte-code (function)
7d2e0f0d9bf7 (profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
239 (let ((defn (symbol-function function)))
7d2e0f0d9bf7 (profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
240 (if (byte-code-function-p defn)
7d2e0f0d9bf7 (profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
241 ;; It is a compiled code object.
7d2e0f0d9bf7 (profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
242 (let* ((contents (append defn nil))
7d2e0f0d9bf7 (profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
243 (body
7d2e0f0d9bf7 (profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
244 (list (list 'byte-code (nth 1 contents)
7d2e0f0d9bf7 (profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
245 (nth 2 contents) (nth 3 contents)))))
7d2e0f0d9bf7 (profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
246 (if (nthcdr 5 contents)
7d2e0f0d9bf7 (profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
247 (setq body (cons (list 'interactive (nth 5 contents)) body)))
7d2e0f0d9bf7 (profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
248 (if (nth 4 contents)
7d2e0f0d9bf7 (profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
249 ;; Use `documentation' here, to get the actual string,
7d2e0f0d9bf7 (profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
250 ;; in case the compiled function has a reference
7d2e0f0d9bf7 (profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
251 ;; to the .elc file.
7d2e0f0d9bf7 (profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
252 (setq body (cons (documentation function) body)))
7d2e0f0d9bf7 (profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
253 (fset function (cons 'lambda (cons (car contents) body)))))))
7d2e0f0d9bf7 (profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
254
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
255 (defun profile-a-function (fun)
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
256 "Profile the function FUN."
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
257 (interactive "aFunction to profile: ")
14791
7d2e0f0d9bf7 (profile-convert-byte-code): New function.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
258 (profile-convert-byte-code fun)
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
259 (let ((def (symbol-function fun)) (funlen (length (symbol-name fun))))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
260 (if (eq (car def) 'lambda) nil
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
261 (error "To profile: %s must be a user-defined function" fun))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
262 (setq profile-time-list ; add a new entry
15535
8895613b61dd (profile-functions): No need to assume that the
Richard M. Stallman <rms@gnu.org>
parents: 14791
diff changeset
263 (cons (cons fun (cons 0 (cons 0 0))) profile-time-list))
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
264 (setq profile-init-list ; add a new entry
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
265 (cons (cons fun (cons 0 (cons 0 0))) profile-init-list))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
266 (if (< profile-max-fun-name funlen) (setq profile-max-fun-name funlen))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
267 (fset fun (profile-fix-fun fun def))))
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
268
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
269 (defun profile-fix-fun (fun def)
16375
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
270 "Take function FUN and return it fixed for profiling.
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
271 DEF is (symbol-function FUN)."
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
272 (let (prefix first second third (count 2) inter suffix)
16375
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
273 (if (< (length def) 3)
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
274 nil ; nothing to see
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
275 (setq first (car def) second (car (cdr def))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
276 third (car (nthcdr 2 def)))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
277 (setq prefix (list first second))
16375
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
278 ;; Skip the doc string, if there is a string
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
279 ;; which serves only as a doc string,
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
280 ;; and put it in PREFIX.
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
281 (if (or (not (stringp third)) (not (nthcdr 3 def)))
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
282 ;; Either no doc string, or it is also the function value.
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
283 (setq inter third)
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
284 ;; Skip the doc string,
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
285 (setq count 3
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
286 prefix (nconc prefix (list third))
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
287 inter (car (nthcdr 3 def))))
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
288 ;; Check for an interactive spec.
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
289 ;; If found, put it inu PREFIX and skip it.
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
290 (if (not (and (listp inter)
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
291 (eq (car inter) 'interactive)))
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
292 nil
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
293 (setq prefix (nconc prefix (list inter)))
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
294 (setq count (1+ count))) ; skip this sexp for suffix
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
295 ;; Set SUFFIX to the function body forms.
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
296 (setq suffix (nthcdr count def))
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
297 (if (equal (car suffix) '(profile-get-time))
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
298 nil
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
299 ;; Prepare new function definition.
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
300 (nconc prefix
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
301 (list '(profile-get-time)) ; read time
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
302 (list (list 'profile-start-function
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
303 (list 'quote fun)))
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
304 (list (list 'setq 'profile-temp-result-
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
305 (nconc (list 'progn) suffix)))
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
306 (list '(profile-get-time)) ; read time
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
307 (list (list 'profile-update-function
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
308 (list 'quote fun)))
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
309 (list 'profile-temp-result-)
2dacebab0fd4 Delete several \n\'s.
Richard M. Stallman <rms@gnu.org>
parents: 15535
diff changeset
310 )))))
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
311
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
312 (defun profile-restore-fun (fun)
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
313 "Restore profiled function FUN to its original state."
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
314 (let ((def (symbol-function (car fun))) body index)
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
315 ;; move index beyond header
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
316 (setq index (cdr def))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
317 (if (stringp (car (cdr index))) (setq index (cdr index)))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
318 (if (and (listp (car (cdr index)))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
319 (eq (car (car (cdr index))) 'interactive))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
320 (setq index (cdr index)))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
321 (setq body (car (nthcdr 3 index)))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
322 (if (and (listp body) ; the right element ?
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
323 (eq (car (cdr body)) 'profile-temp-result-))
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
324 (setcdr index (cdr (car (cdr (cdr body))))))))
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
325
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
326 (defun profile-finish ()
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
327 "Stop profiling functions. Clear all the settings."
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
328 (interactive)
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
329 (mapcar 'profile-restore-fun profile-time-list)
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
330 (setq profile-max-fun-name 0)
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
331 (setq profile-time-list nil)
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
332 (setq profile-init-list nil))
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
333
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
334 (defun profile-quit ()
5790
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
335 "Kill the timer process."
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
336 (interactive)
a539cda9df58 Comment changes and doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 4567
diff changeset
337 (process-send-string profile-timer-process "q\n"))
655
02591d9e7ad3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
338
18383
11218164bc54 Add provide call.
Richard M. Stallman <rms@gnu.org>
parents: 16375
diff changeset
339 (provide 'profile)
11218164bc54 Add provide call.
Richard M. Stallman <rms@gnu.org>
parents: 16375
diff changeset
340
2233
fb0ed5a1d0f3 Add standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 845
diff changeset
341 ;;; profile.el ends here