0
|
1 /* Why the hell is XEmacs so fucking slow?
|
|
2 Copyright (C) 1996 Ben Wing.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 #include <config.h>
|
|
22 #include "lisp.h"
|
|
23
|
|
24 #include "backtrace.h"
|
|
25 #include "bytecode.h"
|
|
26 #include "hash.h"
|
|
27
|
|
28 #include "syssignal.h"
|
|
29 #include "systime.h"
|
|
30
|
|
31 /*
|
|
32
|
|
33 We implement our own profiling scheme so that we can determine things
|
|
34 like which Lisp functions are occupying the most time. Any standard
|
|
35 OS-provided profiling works on C functions, which is somewhat useless.
|
|
36
|
|
37 The basic idea is simple. We set a profiling timer using
|
|
38 setitimer (ITIMER_PROF), which generates a SIGPROF every so often.
|
|
39 \(This runs not in real time but rather when the process is executing
|
|
40 or the system is running on behalf of the process.) When the signal
|
|
41 goes off, we see what we're in, and add by 1 the count associated with
|
|
42 that function.
|
|
43
|
|
44 It would be nice to use the Lisp allocation mechanism etc. to keep
|
|
45 track of the profiling information, but we can't because that's not
|
|
46 safe, and trying to make it safe would be much more work than is
|
|
47 worth.
|
|
48
|
|
49 */
|
|
50
|
|
51 c_hashtable big_profile_table;
|
|
52
|
|
53 int default_profiling_interval;
|
|
54
|
|
55 int profiling_active;
|
|
56
|
|
57 /* The normal flag in_display is used as a critical-section flag
|
|
58 and is not set the whole time we're in redisplay. */
|
|
59 int profiling_redisplay_flag;
|
|
60
|
|
61 Lisp_Object QSin_redisplay;
|
|
62 Lisp_Object QSin_garbage_collection;
|
|
63 Lisp_Object QSprocessing_events_at_top_level;
|
|
64 Lisp_Object QSunknown;
|
|
65
|
116
|
66 /* We use inside_profiling to prevent the handler from writing to
|
|
67 the table while another routine is operating on it. We also set
|
|
68 inside_profiling in case the timeout between signal calls is short
|
|
69 enough to catch us while we're already in there. */
|
149
|
70 static volatile int inside_profiling;
|
116
|
71
|
0
|
72 static SIGTYPE
|
|
73 sigprof_handler (int signo)
|
|
74 {
|
116
|
75 /* Don't do anything if we are shutting down, or are doing a maphash
|
|
76 or clrhash on the table. */
|
|
77 if (!inside_profiling && !preparing_for_armageddon)
|
|
78 {
|
|
79 Lisp_Object fun;
|
|
80
|
|
81 /* If something below causes an error to be signaled, we'll
|
|
82 not correctly reset this flag. But we'll be in worse shape
|
|
83 than that anyways, since we'll longjmp back to the last
|
|
84 condition case. */
|
|
85 inside_profiling = 1;
|
|
86
|
|
87 if (profiling_redisplay_flag)
|
|
88 fun = QSin_redisplay;
|
|
89 else if (gc_in_progress)
|
|
90 fun = QSin_garbage_collection;
|
|
91 else if (backtrace_list)
|
|
92 {
|
|
93 fun = *backtrace_list->function;
|
44
|
94
|
116
|
95 if (!GC_SYMBOLP (fun) && !GC_COMPILED_FUNCTIONP (fun))
|
|
96 fun = QSunknown;
|
|
97 }
|
|
98 else
|
|
99 fun = QSprocessing_events_at_top_level;
|
0
|
100
|
116
|
101 {
|
|
102 /* #### see comment about memory allocation in start-profiling.
|
|
103 Allocating memory in a signal handler is BAD BAD BAD.
|
|
104 If you are using the non-mmap rel-alloc code, you might
|
185
|
105 lose because of this. Even worse, if the memory allocation
|
116
|
106 fails, the `error' generated whacks everything hard. */
|
|
107 long count;
|
|
108 CONST void *vval;
|
185
|
109
|
116
|
110 if (gethash (LISP_TO_VOID (fun), big_profile_table, &vval))
|
|
111 count = (long) vval;
|
|
112 else
|
|
113 count = 0;
|
|
114 count++;
|
|
115 vval = (CONST void *) count;
|
|
116 puthash (LISP_TO_VOID (fun), (void *) vval, big_profile_table);
|
|
117 }
|
185
|
118
|
116
|
119 inside_profiling = 0;
|
70
|
120 }
|
0
|
121 }
|
|
122
|
20
|
123 DEFUN ("start-profiling", Fstart_profiling, 0, 1, 0, /*
|
0
|
124 Start profiling, with profile queries every MICROSECS.
|
|
125 If MICROSECS is nil or omitted, the value of `default-profiling-interval'
|
|
126 is used.
|
|
127
|
|
128 You can retrieve the recorded profiling info using `get-profiling-info'.
|
|
129
|
|
130 Starting and stopping profiling does not clear the currently recorded
|
|
131 info. Thus you can start and stop as many times as you want and everything
|
20
|
132 will be properly accumulated.
|
|
133 */
|
|
134 (microsecs))
|
0
|
135 {
|
116
|
136 /* This function can GC */
|
0
|
137 int msecs;
|
|
138 struct itimerval foo;
|
|
139
|
|
140 /* #### The hash code can safely be called from a signal handler
|
|
141 except when it has to grow the hashtable. In this case, it calls
|
|
142 realloc(), which is not (in general) re-entrant. We just be
|
|
143 sleazy and make the table large enough that it (hopefully) won't
|
|
144 need to be realloc()ed. */
|
|
145 if (!big_profile_table)
|
|
146 big_profile_table = make_hashtable (10000);
|
|
147 if (NILP (microsecs))
|
|
148 msecs = default_profiling_interval;
|
|
149 else
|
|
150 {
|
|
151 CHECK_NATNUM (microsecs);
|
|
152 msecs = XINT (microsecs);
|
|
153 }
|
|
154 if (msecs <= 0)
|
|
155 msecs = 1000;
|
|
156
|
|
157 signal (SIGPROF, sigprof_handler);
|
|
158 foo.it_value.tv_sec = 0;
|
|
159 foo.it_value.tv_usec = msecs;
|
|
160 EMACS_NORMALIZE_TIME (foo.it_value);
|
|
161 foo.it_interval = foo.it_value;
|
|
162 profiling_active = 1;
|
116
|
163 inside_profiling = 0;
|
0
|
164 setitimer (ITIMER_PROF, &foo, 0);
|
|
165 return Qnil;
|
|
166 }
|
|
167
|
20
|
168 DEFUN ("stop-profiling", Fstop_profiling, 0, 0, 0, /*
|
0
|
169 Stop profiling.
|
20
|
170 */
|
|
171 ())
|
0
|
172 {
|
116
|
173 /* This function does not GC */
|
0
|
174 struct itimerval foo;
|
|
175
|
|
176 foo.it_value.tv_sec = 0;
|
|
177 foo.it_value.tv_usec = 0;
|
|
178 foo.it_interval = foo.it_value;
|
|
179 setitimer (ITIMER_PROF, &foo, 0);
|
|
180 profiling_active = 0;
|
|
181 signal (SIGPROF, fatal_error_signal);
|
|
182 return Qnil;
|
|
183 }
|
|
184
|
116
|
185 static Lisp_Object
|
|
186 profile_lock_unwind (Lisp_Object ignore)
|
|
187 {
|
185
|
188 inside_profiling = 0;
|
116
|
189 return Qnil;
|
|
190 }
|
|
191
|
0
|
192 struct get_profiling_info_closure
|
|
193 {
|
|
194 Lisp_Object accum;
|
|
195 };
|
|
196
|
|
197 static void
|
|
198 get_profiling_info_maphash (CONST void *void_key,
|
|
199 void *void_val,
|
|
200 void *void_closure)
|
|
201 {
|
116
|
202 /* This function does not GC */
|
0
|
203 Lisp_Object key;
|
185
|
204 struct get_profiling_info_closure *closure
|
|
205 = (struct get_profiling_info_closure *) void_closure;
|
0
|
206 EMACS_INT val;
|
|
207
|
|
208 CVOID_TO_LISP (key, void_key);
|
|
209 val = (EMACS_INT) void_val;
|
|
210
|
185
|
211 closure->accum = Fcons (Fcons (key, make_int (val)), closure->accum);
|
0
|
212 }
|
|
213
|
20
|
214 DEFUN ("get-profiling-info", Fget_profiling_info, 0, 0, 0, /*
|
0
|
215 Return the profiling info as an alist.
|
20
|
216 */
|
|
217 ())
|
0
|
218 {
|
116
|
219 /* This function does not GC */
|
0
|
220 struct get_profiling_info_closure closure;
|
|
221
|
|
222 closure.accum = Qnil;
|
|
223 if (big_profile_table)
|
116
|
224 {
|
|
225 int count = specpdl_depth ();
|
|
226 record_unwind_protect (profile_lock_unwind, Qnil);
|
|
227 inside_profiling = 1;
|
|
228 maphash (get_profiling_info_maphash, big_profile_table, &closure);
|
|
229 unbind_to (count, Qnil);
|
|
230 }
|
0
|
231 return closure.accum;
|
|
232 }
|
|
233
|
|
234 struct mark_profiling_info_closure
|
|
235 {
|
|
236 void (*markfun) (Lisp_Object);
|
|
237 };
|
|
238
|
|
239 static void
|
|
240 mark_profiling_info_maphash (CONST void *void_key,
|
|
241 void *void_val,
|
|
242 void *void_closure)
|
|
243 {
|
|
244 Lisp_Object key;
|
|
245
|
|
246 CVOID_TO_LISP (key, void_key);
|
185
|
247 (((struct mark_profiling_info_closure *) void_closure)->markfun) (key);
|
0
|
248 }
|
|
249
|
|
250 void
|
|
251 mark_profiling_info (void (*markfun) (Lisp_Object))
|
|
252 {
|
116
|
253 /* This function does not GC (if markfun doesn't) */
|
0
|
254 struct mark_profiling_info_closure closure;
|
|
255
|
|
256 closure.markfun = markfun;
|
|
257 if (big_profile_table)
|
116
|
258 {
|
|
259 inside_profiling = 1;
|
|
260 maphash (mark_profiling_info_maphash, big_profile_table, &closure);
|
|
261 inside_profiling = 0;
|
|
262 }
|
0
|
263 }
|
|
264
|
199
|
265 DEFUN ("clear-profiling-info", Fclear_profiling_info, 0, 0, "", /*
|
0
|
266 Clear out the recorded profiling info.
|
20
|
267 */
|
|
268 ())
|
0
|
269 {
|
116
|
270 /* This function does not GC */
|
0
|
271 if (big_profile_table)
|
116
|
272 {
|
|
273 inside_profiling = 1;
|
|
274 clrhash (big_profile_table);
|
|
275 inside_profiling = 0;
|
|
276 }
|
0
|
277 return Qnil;
|
|
278 }
|
|
279
|
20
|
280 DEFUN ("profiling-active-p", Fprofiling_active_p, 0, 0, 0, /*
|
0
|
281 Return non-nil if profiling information is currently being recorded.
|
20
|
282 */
|
|
283 ())
|
0
|
284 {
|
|
285 return profiling_active ? Qt : Qnil;
|
|
286 }
|
|
287
|
|
288 void
|
|
289 syms_of_profile (void)
|
|
290 {
|
20
|
291 DEFSUBR (Fstart_profiling);
|
|
292 DEFSUBR (Fstop_profiling);
|
|
293 DEFSUBR (Fget_profiling_info);
|
|
294 DEFSUBR (Fclear_profiling_info);
|
|
295 DEFSUBR (Fprofiling_active_p);
|
0
|
296 }
|
|
297
|
|
298 void
|
|
299 vars_of_profile (void)
|
|
300 {
|
|
301 DEFVAR_INT ("default-profiling-interval", &default_profiling_interval /*
|
|
302 Default time in microseconds between profiling queries.
|
|
303 Used when the argument to `start-profiling' is nil or omitted.
|
|
304 Note that the time in question is CPU time (when the program is executing
|
|
305 or the kernel is executing on behalf of the program) and not real time.
|
|
306 */ );
|
|
307 default_profiling_interval = 1000;
|
|
308
|
116
|
309 inside_profiling = 0;
|
|
310
|
0
|
311 QSin_redisplay = build_string ("(in redisplay)");
|
|
312 staticpro (&QSin_redisplay);
|
|
313 QSin_garbage_collection = build_string ("(in garbage collection)");
|
|
314 staticpro (&QSin_garbage_collection);
|
|
315 QSunknown = build_string ("(unknown)");
|
|
316 staticpro (&QSunknown);
|
|
317 QSprocessing_events_at_top_level =
|
|
318 build_string ("(processing events at top level)");
|
|
319 staticpro (&QSprocessing_events_at_top_level);
|
|
320 }
|