428
|
1 /* Why the hell is XEmacs so fucking slow?
|
1292
|
2 Copyright (C) 1996, 2002, 2003 Ben Wing.
|
428
|
3 Copyright (C) 1998 Free Software Foundation, Inc.
|
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 #include <config.h>
|
|
23 #include "lisp.h"
|
|
24
|
|
25 #include "backtrace.h"
|
|
26 #include "bytecode.h"
|
|
27 #include "elhash.h"
|
|
28 #include "hash.h"
|
|
29
|
|
30 #include "syssignal.h"
|
|
31 #include "systime.h"
|
|
32
|
611
|
33 #ifndef HAVE_SETITIMER
|
|
34 #error Sorry charlie. We need a scalpel and all we have is a lawnmower.
|
|
35 #endif
|
|
36
|
1292
|
37 #ifdef WIN32_ANY
|
|
38 int mswindows_is_blocking;
|
|
39 #endif
|
|
40
|
|
41 /* Written by Ben Wing.
|
|
42
|
|
43 We implement our own profiling scheme so that we can determine
|
428
|
44 things like which Lisp functions are occupying the most time. Any
|
|
45 standard OS-provided profiling works on C functions, which is
|
1292
|
46 not always that useful -- and inconvenient, since it requires compiling
|
|
47 with profile info and can't be retrieved dynamically, as XEmacs is
|
|
48 running.
|
428
|
49
|
|
50 The basic idea is simple. We set a profiling timer using setitimer
|
1292
|
51 (ITIMER_PROF), which generates a SIGPROF every so often. (This runs not
|
|
52 in real time but rather when the process is executing or the system is
|
|
53 running on behalf of the process.) When the signal goes off, we see what
|
|
54 we're in, and add 1 to the count associated with that function.
|
428
|
55
|
1292
|
56 It would be nice to use the Lisp allocation mechanism etc. to keep track
|
|
57 of the profiling information (i.e. to use Lisp hash tables), but we
|
|
58 can't because that's not safe -- updating the timing information happens
|
|
59 inside of a signal handler, so we can't rely on not being in the middle
|
|
60 of Lisp allocation, garbage collection, malloc(), etc. Trying to make
|
|
61 it work would be much more work than it's worth. Instead we use a basic
|
|
62 (non-Lisp) hash table, which will not conflict with garbage collection
|
|
63 or anything else as long as it doesn't try to resize itself. Resizing
|
|
64 itself, however (which happens as a result of a puthash()), could be
|
|
65 deadly. To avoid this, we make sure, at points where it's safe
|
|
66 (e.g. profile_record_about_to_call() -- recording the entry into a
|
|
67 function call), that the table always has some breathing room in it so
|
|
68 that no resizes will occur until at least that many items are added.
|
|
69 This is safe because any new item to be added in the sigprof would
|
|
70 likely have the profile_record_about_to_call() called just before it,
|
|
71 and the breathing room is checked.
|
428
|
72
|
1292
|
73 In general: any entry that the sigprof handler puts into the table comes
|
|
74 from a backtrace frame (except "Processing Events at Top Level", and
|
|
75 there's only one of those). Either that backtrace frame was added when
|
|
76 profiling was on (in which case profile_record_about_to_call() was
|
|
77 called and the breathing space updated), or when it was off -- and in
|
|
78 this case, no such frames can have been added since the last time
|
|
79 `start-profile' was called, so when `start-profile' is called we make
|
|
80 sure there is sufficient breathing room to account for all entries
|
|
81 currently on the stack.
|
|
82
|
|
83 Jan 1998: In addition to timing info, I have added code to remember call
|
428
|
84 counts of Lisp funcalls. The profile_increase_call_count()
|
|
85 function is called from Ffuncall(), and serves to add data to
|
|
86 Vcall_count_profile_table. This mechanism is much simpler and
|
|
87 independent of the SIGPROF-driven one. It uses the Lisp allocation
|
|
88 mechanism normally, since it is not called from a handler. It may
|
|
89 even be useful to provide a way to turn on only one profiling
|
1292
|
90 mechanism, but I haven't done so yet. --hniksic
|
|
91
|
|
92 Dec 2002: Total overhaul of the interface, making it sane and easier to
|
|
93 use. --ben
|
|
94
|
|
95 Feb 2003: Lots of rewriting of the internal code. Add GC-consing-usage,
|
|
96 total GC usage, and total timing to the information tracked. Track
|
|
97 profiling overhead and allow the ability to have internal sections
|
|
98 (e.g. internal-external conversion, byte-char conversion) that are
|
|
99 treated like Lisp functions for the purpose of profiling. --ben
|
428
|
100
|
1292
|
101 BEWARE: If you are modifying this file, be *very* careful. Correctly
|
|
102 implementing the "total" values is very tricky due to the possibility of
|
|
103 recursion and of functions already on the stack when starting to
|
|
104 profile/still on the stack when stopping.
|
|
105 */
|
|
106
|
|
107 /* We use a plain table here because we're recording inside of a signal
|
|
108 handler. */
|
428
|
109 static struct hash_table *big_profile_table;
|
1292
|
110 Lisp_Object Vtotal_timing_profile_table;
|
428
|
111 Lisp_Object Vcall_count_profile_table;
|
1292
|
112 Lisp_Object Vtotal_gc_usage_profile_table;
|
|
113 Lisp_Object Vgc_usage_profile_table;
|
|
114
|
|
115 extern int lisp_eval_depth;
|
|
116
|
|
117 extern EMACS_UINT total_consing;
|
|
118 static volatile EMACS_UINT total_ticks;
|
428
|
119
|
458
|
120 Fixnum default_profiling_interval;
|
428
|
121
|
|
122 int profiling_active;
|
|
123
|
1292
|
124 static Lisp_Object QSprocessing_events_at_top_level;
|
|
125 static Lisp_Object QSunknown, QSprofile_overhead;
|
|
126
|
|
127 static Lisp_Object Qtiming, Qtotal_timing, Qcall_count;
|
|
128 static Lisp_Object Qgc_usage, Qtotal_gc_usage;
|
|
129
|
|
130 /* This needs to be >= the total number of defined internal sections,
|
|
131 plus 1 or 2?? Set it extra big just to be ultra-paranoid. */
|
|
132 #define EXTRA_BREATHING_ROOM 100
|
428
|
133
|
1292
|
134 /* We use profiling_lock to prevent the signal handler from writing to
|
|
135 the table while another routine is operating on it. We also set
|
|
136 profiling_lock in case the timeout between signal calls is short
|
|
137 enough to catch us while we're already in there. */
|
|
138 static volatile int profiling_lock;
|
428
|
139
|
1292
|
140 /* Whether we're in the process of doing *any* profiling-related stuff.
|
|
141 Used to indicate amount of time spent profiling. */
|
|
142 static int in_profiling;
|
|
143
|
|
144 #if 0 /* #### for KKCC, eventually */
|
1123
|
145
|
1292
|
146 static const struct memory_description hentry_description_1[] = {
|
|
147 { XD_LISP_OBJECT, offsetof (hentry, key) },
|
|
148 { XD_END }
|
|
149 };
|
|
150
|
|
151 static const struct sized_memory_description hentry_description = {
|
|
152 sizeof (hentry),
|
|
153 hentry_description_1
|
|
154 };
|
428
|
155
|
1292
|
156 static const struct memory_description plain_hash_table_description_1[] = {
|
|
157 { XD_ELEMCOUNT, offsetof (struct hash_table, size) },
|
|
158 { XD_STRUCT_PTR, offsetof (struct hash_table, harray), XD_INDIRECT (0, 0),
|
|
159 &hentry_description },
|
|
160 { XD_END }
|
|
161 };
|
|
162
|
|
163 static const struct sized_memory_description plain_hash_table_description = {
|
|
164 sizeof (struct hash_table),
|
|
165 plain_hash_table_description_1
|
|
166 };
|
|
167
|
|
168 #endif /* 0 */
|
1123
|
169
|
|
170 static void
|
|
171 create_timing_profile_table (void)
|
|
172 {
|
1292
|
173 /* The hash code can safely be called from a signal handler except when
|
|
174 it has to grow the hash table. In this case, it calls realloc(),
|
|
175 which is not (in general) re-entrant. The way we deal with this is
|
|
176 documented at the top of this file. */
|
1123
|
177 if (!big_profile_table)
|
1292
|
178 big_profile_table = make_hash_table (2000);
|
|
179 }
|
|
180
|
|
181 static void
|
|
182 create_profile_tables (void)
|
|
183 {
|
|
184 create_timing_profile_table ();
|
|
185 if (NILP (Vtotal_timing_profile_table))
|
|
186 Vtotal_timing_profile_table =
|
|
187 make_lisp_hash_table (100, HASH_TABLE_NON_WEAK, HASH_TABLE_EQ);
|
|
188 if (NILP (Vcall_count_profile_table))
|
|
189 Vcall_count_profile_table =
|
|
190 make_lisp_hash_table (100, HASH_TABLE_NON_WEAK, HASH_TABLE_EQ);
|
|
191 if (NILP (Vgc_usage_profile_table))
|
|
192 Vgc_usage_profile_table =
|
|
193 make_lisp_hash_table (100, HASH_TABLE_NON_WEAK, HASH_TABLE_EQ);
|
|
194 if (NILP (Vtotal_gc_usage_profile_table))
|
|
195 Vtotal_gc_usage_profile_table =
|
|
196 make_lisp_hash_table (100, HASH_TABLE_NON_WEAK, HASH_TABLE_EQ);
|
|
197 }
|
|
198
|
|
199 static Lisp_Object
|
|
200 current_profile_function (void)
|
|
201 {
|
|
202 Lisp_Object fun;
|
|
203 struct backtrace *bt = backtrace_list;
|
|
204
|
|
205 /* 2 because we set in_profiling when we entered the current routine. */
|
|
206 if (in_profiling >= 2)
|
|
207 return QSprofile_overhead;
|
|
208
|
|
209 /* Find a function actually being called. Potentially (?) there could be
|
|
210 a number of non-calling funs -- calling foo autoloads, which tries to
|
|
211 call bar, but requires evalling its args first, which calls baz, ...
|
|
212 If profiling was not enabled when the function was called, just treat
|
|
213 the function as actually called, because the info about whether we've
|
|
214 finished the preamble will not have been recorded. */
|
|
215 for (; bt && !bt->function_being_called; bt = bt->next)
|
|
216 ;
|
|
217
|
|
218 if (bt)
|
|
219 {
|
|
220 fun = *bt->function;
|
|
221
|
|
222 if (!SYMBOLP (fun)
|
|
223 && !COMPILED_FUNCTIONP (fun)
|
|
224 && !SUBRP (fun)
|
|
225 && !CONSP (fun)
|
|
226 && !STRINGP (fun))
|
|
227 fun = QSunknown;
|
|
228 }
|
|
229 else
|
|
230 fun = QSprocessing_events_at_top_level;
|
|
231 return fun;
|
|
232 }
|
|
233
|
|
234 void
|
|
235 profile_record_consing (EMACS_INT size)
|
|
236 {
|
|
237 Lisp_Object fun;
|
|
238 Lisp_Object count;
|
|
239
|
|
240 in_profiling++;
|
|
241 fun = current_profile_function ();
|
|
242 count = Fgethash (fun, Vgc_usage_profile_table, Qzero);
|
|
243 Fputhash (fun, make_int (size + XINT (count)), Vgc_usage_profile_table);
|
|
244 in_profiling--;
|
|
245 }
|
|
246
|
|
247 void
|
|
248 profile_record_unconsing (EMACS_INT size)
|
|
249 {
|
|
250 /* If we don't want to record values less than 0, change this; but then
|
|
251 the totals won't be accurate. */
|
|
252 profile_record_consing (-size);
|
1123
|
253 }
|
|
254
|
1292
|
255 inline static void
|
|
256 profile_sow_backtrace (struct backtrace *bt)
|
428
|
257 {
|
1292
|
258 bt->current_total_timing_val =
|
|
259 XINT (Fgethash (*bt->function, Vtotal_timing_profile_table, Qzero));
|
|
260 bt->current_total_gc_usage_val =
|
|
261 XINT (Fgethash (*bt->function, Vtotal_gc_usage_profile_table, Qzero));
|
|
262 bt->function_being_called = 1;
|
|
263 /* Need to think carefully about the exact order of operations here
|
|
264 so that we don't end up with totals being less than function-only
|
|
265 values; */
|
|
266 bt->total_consing_at_start = total_consing;
|
|
267 /* Order of operation is tricky here because we want the total function
|
|
268 time to be as close as possible to (and absolutely not less than) the
|
|
269 function-only time. From the sigprof-handler's perspective, the
|
|
270 function is "entered" the moment we finish executing the
|
|
271 in_profiling-- statement below, and ends the moment we finish
|
|
272 executing the in_profiling++ statement in
|
|
273 profile_record_just_called(). By recording the tick value as close as
|
|
274 possible to the "in-function" window but not in it, we satisfy the
|
|
275 conditions just mentioned. */
|
|
276 bt->total_ticks_at_start = total_ticks;
|
|
277 }
|
428
|
278
|
1292
|
279 void
|
|
280 profile_record_about_to_call (struct backtrace *bt)
|
|
281 {
|
|
282 in_profiling++;
|
|
283 profiling_lock = 1;
|
|
284 /* See comments in create_timing_profile_table(). */
|
|
285 pregrow_hash_table_if_necessary (big_profile_table, EXTRA_BREATHING_ROOM);
|
|
286 profiling_lock = 0;
|
|
287 Fputhash (*bt->function,
|
|
288 make_int (1 + XINT (Fgethash (*bt->function,
|
|
289 Vcall_count_profile_table,
|
|
290 Qzero))),
|
|
291 Vcall_count_profile_table);
|
|
292 /* This may be set if the function was in its preamble at the time that
|
|
293 `start-profiling' was called. If so, we shouldn't reset the values
|
|
294 because we may get inconsistent results, since we have already started
|
|
295 recording ticks and consing for the function. */
|
|
296 if (!bt->function_being_called)
|
|
297 profile_sow_backtrace (bt);
|
|
298 in_profiling--;
|
|
299 }
|
428
|
300
|
1292
|
301 inline static void
|
|
302 profile_reap_backtrace (struct backtrace *bt)
|
|
303 {
|
|
304 EMACS_UINT ticks;
|
|
305 /* The following statement *MUST* come directly after the preceding one!
|
|
306 See the comment above. */
|
|
307 ticks = total_ticks;
|
|
308 /* We need to reset the "in-function" flag here. Otherwise the sigprof
|
|
309 handler will record more ticks for the function while the post-amble
|
|
310 is executing, and its value will be > our total value. */
|
|
311 bt->function_being_called = 0;
|
|
312 Fputhash (*bt->function,
|
|
313 /* This works even when the total_ticks value has overwrapped.
|
|
314 Same for total_consing below. */
|
|
315 make_int ((EMACS_INT) (ticks - bt->total_ticks_at_start)
|
|
316 + bt->current_total_timing_val),
|
|
317 Vtotal_timing_profile_table);
|
|
318 Fputhash (*bt->function,
|
|
319 make_int ((EMACS_INT)
|
|
320 (total_consing - bt->total_consing_at_start)
|
|
321 + bt->current_total_gc_usage_val),
|
|
322 Vtotal_gc_usage_profile_table);
|
|
323 }
|
|
324
|
|
325 void
|
|
326 profile_record_just_called (struct backtrace *bt)
|
|
327 {
|
|
328 in_profiling++;
|
|
329 profile_reap_backtrace (bt);
|
|
330 in_profiling--;
|
|
331 }
|
|
332
|
|
333 /* Called when unwinding the catch stack after a throw or signal, to
|
|
334 note that we are exiting the function. */
|
|
335 void
|
|
336 profile_record_unwind (struct backtrace *bt)
|
|
337 {
|
|
338 /* We may have thrown while still in a function's preamble. */
|
|
339 if (bt->function_being_called)
|
|
340 profile_record_just_called (bt);
|
428
|
341 }
|
|
342
|
|
343 static SIGTYPE
|
|
344 sigprof_handler (int signo)
|
|
345 {
|
1292
|
346 #ifdef WIN32_ANY
|
|
347 /* Windows unfortunately does not have any such thing as setitimer
|
|
348 (ITIMER_PROF, ...), which runs in process time. Everything is real
|
|
349 time. So to get slightly more reasonable results, ignore completely
|
|
350 the times when we're blocking. Same applies, of course, to Cygwin. */
|
|
351 if (mswindows_is_blocking)
|
|
352 return;
|
|
353 #endif
|
|
354
|
|
355 in_profiling++;
|
|
356 total_ticks++;
|
|
357
|
428
|
358 /* Don't do anything if we are shutting down, or are doing a maphash
|
|
359 or clrhash on the table. */
|
1292
|
360 if (!profiling_lock && !preparing_for_armageddon)
|
428
|
361 {
|
1292
|
362 Lisp_Object fun = current_profile_function ();
|
428
|
363
|
|
364 /* If something below causes an error to be signaled, we'll
|
|
365 not correctly reset this flag. But we'll be in worse shape
|
|
366 than that anyways, since we'll longjmp back to the last
|
|
367 condition case. */
|
1292
|
368 profiling_lock = 1;
|
428
|
369
|
|
370 {
|
|
371 long count;
|
442
|
372 const void *vval;
|
428
|
373
|
|
374 if (gethash (LISP_TO_VOID (fun), big_profile_table, &vval))
|
|
375 count = (long) vval;
|
|
376 else
|
|
377 count = 0;
|
|
378 count++;
|
442
|
379 vval = (const void *) count;
|
428
|
380 puthash (LISP_TO_VOID (fun), (void *) vval, big_profile_table);
|
|
381 }
|
|
382
|
1292
|
383 profiling_lock = 0;
|
428
|
384 }
|
1292
|
385 in_profiling--;
|
428
|
386 }
|
|
387
|
1292
|
388 DEFUN ("start-profiling", Fstart_profiling, 0, 1, "", /*
|
428
|
389 Start profiling, with profile queries every MICROSECS.
|
|
390 If MICROSECS is nil or omitted, the value of `default-profiling-interval'
|
|
391 is used.
|
|
392
|
1123
|
393 Information on function timings and call counts is currently recorded.
|
1292
|
394 You can retrieve the recorded profiling info using `get-profiling-info',
|
|
395 or the higher-level function `profile-results'.
|
428
|
396
|
|
397 Starting and stopping profiling does not clear the currently recorded
|
|
398 info. Thus you can start and stop as many times as you want and everything
|
1292
|
399 will be properly accumulated. (To clear, use `clear-profiling-info'.)
|
428
|
400 */
|
|
401 (microsecs))
|
|
402 {
|
|
403 /* This function can GC */
|
|
404 int msecs;
|
|
405 struct itimerval foo;
|
1292
|
406 int depth;
|
428
|
407
|
1292
|
408 if (profiling_active)
|
|
409 return Qnil;
|
|
410 depth = internal_bind_int (&in_profiling, 1 + in_profiling);
|
|
411
|
|
412 create_profile_tables ();
|
|
413 /* See comments at top of file and in create_timing_profile_table().
|
|
414 We ensure enough breathing room for all entries currently on the
|
|
415 stack. */
|
|
416 pregrow_hash_table_if_necessary (big_profile_table,
|
|
417 EXTRA_BREATHING_ROOM + lisp_eval_depth);
|
428
|
418
|
|
419 if (NILP (microsecs))
|
|
420 msecs = default_profiling_interval;
|
|
421 else
|
|
422 {
|
|
423 CHECK_NATNUM (microsecs);
|
|
424 msecs = XINT (microsecs);
|
|
425 }
|
|
426 if (msecs <= 0)
|
|
427 msecs = 1000;
|
|
428
|
613
|
429 set_timeout_signal (SIGPROF, sigprof_handler);
|
1292
|
430 {
|
|
431 struct backtrace *bt = backtrace_list;
|
|
432
|
|
433 /* When we begin profiling, pretend like we just entered all the
|
|
434 functions currently on the stack. When we stop profiling, do the
|
|
435 opposite. This ensures consistent values being recorded for both
|
|
436 function-only and total in such cases. */
|
|
437 for (; bt; bt = bt->next)
|
|
438 profile_sow_backtrace (bt);
|
|
439 }
|
|
440 profiling_active = 1;
|
|
441 profiling_lock = 0;
|
428
|
442 foo.it_value.tv_sec = 0;
|
|
443 foo.it_value.tv_usec = msecs;
|
|
444 EMACS_NORMALIZE_TIME (foo.it_value);
|
|
445 foo.it_interval = foo.it_value;
|
611
|
446 qxe_setitimer (ITIMER_PROF, &foo, 0);
|
1292
|
447 unbind_to (depth);
|
428
|
448 return Qnil;
|
|
449 }
|
|
450
|
1292
|
451 DEFUN ("stop-profiling", Fstop_profiling, 0, 0, "", /*
|
428
|
452 Stop profiling.
|
|
453 */
|
|
454 ())
|
|
455 {
|
|
456 /* This function does not GC */
|
|
457 struct itimerval foo;
|
|
458
|
1292
|
459 if (!profiling_active)
|
|
460 return Qnil;
|
|
461 in_profiling++;
|
428
|
462 foo.it_value.tv_sec = 0;
|
|
463 foo.it_value.tv_usec = 0;
|
|
464 foo.it_interval = foo.it_value;
|
611
|
465 qxe_setitimer (ITIMER_PROF, &foo, 0);
|
428
|
466 profiling_active = 0;
|
1292
|
467 {
|
|
468 struct backtrace *bt = backtrace_list;
|
|
469
|
|
470 for (; bt; bt = bt->next)
|
|
471 profile_reap_backtrace (bt);
|
|
472 }
|
613
|
473 set_timeout_signal (SIGPROF, fatal_error_signal);
|
1292
|
474 in_profiling--;
|
428
|
475 return Qnil;
|
|
476 }
|
|
477
|
1123
|
478 DEFUN ("clear-profiling-info", Fclear_profiling_info, 0, 0, "", /*
|
|
479 Clear out the recorded profiling info.
|
|
480 This clears both the internal timing information and the call counts in
|
|
481 `call-count-profile-table'.
|
|
482 */
|
|
483 ())
|
|
484 {
|
1292
|
485 in_profiling++;
|
1123
|
486 /* This function does not GC */
|
|
487 if (big_profile_table)
|
|
488 {
|
1292
|
489 profiling_lock = 1;
|
1123
|
490 clrhash (big_profile_table);
|
1292
|
491 profiling_lock = 0;
|
1123
|
492 }
|
1292
|
493 if (!NILP (Vtotal_timing_profile_table))
|
|
494 Fclrhash (Vtotal_timing_profile_table);
|
1123
|
495 if (!NILP (Vcall_count_profile_table))
|
|
496 Fclrhash (Vcall_count_profile_table);
|
1292
|
497 if (!NILP (Vgc_usage_profile_table))
|
|
498 Fclrhash (Vgc_usage_profile_table);
|
|
499 if (!NILP (Vtotal_gc_usage_profile_table))
|
|
500 Fclrhash (Vtotal_gc_usage_profile_table);
|
|
501 in_profiling--;
|
|
502
|
1123
|
503 return Qnil;
|
|
504 }
|
|
505
|
428
|
506 struct get_profiling_info_closure
|
|
507 {
|
1123
|
508 Lisp_Object timing;
|
428
|
509 };
|
|
510
|
|
511 static int
|
1123
|
512 get_profiling_info_timing_maphash (const void *void_key,
|
|
513 void *void_val,
|
|
514 void *void_closure)
|
428
|
515 {
|
|
516 /* This function does not GC */
|
|
517 Lisp_Object key;
|
|
518 struct get_profiling_info_closure *closure
|
|
519 = (struct get_profiling_info_closure *) void_closure;
|
|
520 EMACS_INT val;
|
|
521
|
826
|
522 key = VOID_TO_LISP (void_key);
|
428
|
523 val = (EMACS_INT) void_val;
|
|
524
|
1123
|
525 Fputhash (key, make_int (val), closure->timing);
|
428
|
526 return 0;
|
|
527 }
|
|
528
|
1292
|
529 static Lisp_Object
|
|
530 copy_hash_table_or_blank (Lisp_Object table)
|
|
531 {
|
|
532 return !NILP (table) ? Fcopy_hash_table (table) :
|
|
533 make_lisp_hash_table (100, HASH_TABLE_NON_WEAK,
|
|
534 HASH_TABLE_EQ);
|
|
535 }
|
|
536
|
428
|
537 DEFUN ("get-profiling-info", Fget_profiling_info, 0, 0, 0, /*
|
1123
|
538 Return the currently recorded profiling info.
|
|
539 The format is a plist of symbols describing type of info recorded and
|
|
540 an associated type-specific entry. Currently, the following info types
|
|
541 are recorded
|
|
542
|
|
543 `timing'
|
1292
|
544 A hash table of function descriptions (funcallable objects or strings
|
|
545 describing internal processing operations -- redisplay, garbage
|
|
546 collection, etc.), along with associated tick counts (the frequency of
|
|
547 ticks is controlled by `default-profiling-interval' or the argument to
|
|
548 `start-profiling').
|
|
549
|
|
550 `total-timing'
|
|
551 A hash table of function descriptions and associated timing count for
|
|
552 the function and all descendants.
|
1123
|
553
|
|
554 `call-count'
|
1292
|
555 A hash table of function descriptions and associated call counts.
|
|
556
|
|
557 `gc-usage'
|
|
558 A hash table of function descriptions and associated amount of consing.
|
|
559
|
|
560 `total-gc-usage'
|
|
561 A hash table of function descriptions and associated amount of consing
|
|
562 in the function and all descendants.
|
428
|
563 */
|
|
564 ())
|
|
565 {
|
|
566 /* This function does not GC */
|
|
567 struct get_profiling_info_closure closure;
|
1292
|
568 Lisp_Object retv;
|
|
569 int depth = internal_bind_int (&in_profiling, 1 + in_profiling);
|
|
570 const void *overhead;
|
428
|
571
|
1123
|
572 closure.timing =
|
|
573 make_lisp_hash_table (100, HASH_TABLE_NON_WEAK, HASH_TABLE_EQUAL);
|
|
574
|
428
|
575 if (big_profile_table)
|
|
576 {
|
1292
|
577 int count = internal_bind_int ((int *) &profiling_lock, 1);
|
1123
|
578 maphash (get_profiling_info_timing_maphash, big_profile_table, &closure);
|
1292
|
579
|
|
580 /* OK, OK ... the total-timing table is not going to have an entry
|
|
581 for profile overhead, and it looks strange for it to come out 0,
|
|
582 so make sure it looks reasonable. */
|
|
583 if (!gethash (LISP_TO_VOID (QSprofile_overhead), big_profile_table,
|
|
584 &overhead))
|
|
585 overhead = 0;
|
|
586 Fputhash (QSprofile_overhead, make_int ((EMACS_INT) overhead),
|
|
587 Vtotal_timing_profile_table);
|
|
588
|
771
|
589 unbind_to (count);
|
428
|
590 }
|
1123
|
591
|
1292
|
592 retv = nconc2 (list6 (Qtiming, closure.timing, Qtotal_timing,
|
|
593 copy_hash_table_or_blank (Vtotal_timing_profile_table),
|
|
594 Qcall_count,
|
|
595 copy_hash_table_or_blank (Vcall_count_profile_table)),
|
|
596 list4 (Qgc_usage,
|
|
597 copy_hash_table_or_blank (Vgc_usage_profile_table),
|
|
598 Qtotal_gc_usage,
|
|
599 copy_hash_table_or_blank (Vtotal_gc_usage_profile_table
|
|
600 )));
|
|
601 unbind_to (depth);
|
|
602 return retv;
|
1123
|
603 }
|
|
604
|
|
605 static int
|
|
606 set_profiling_info_timing_maphash (Lisp_Object key,
|
|
607 Lisp_Object val,
|
|
608 void *void_closure)
|
|
609 {
|
|
610 /* This function does not GC */
|
|
611 if (!INTP (val))
|
|
612 invalid_argument_2
|
|
613 ("Function timing count is not an integer in given entry",
|
|
614 key, val);
|
|
615
|
|
616 puthash (LISP_TO_VOID (key), (void *) XINT (val), big_profile_table);
|
|
617
|
|
618 return 0;
|
|
619 }
|
|
620
|
|
621 DEFUN ("set-profiling-info", Fset_profiling_info, 1, 1, 0, /*
|
|
622 Set the currently recorded profiling info.
|
|
623 INFO should be in the same format returned by `get-profiling-info',
|
|
624 as described there.
|
|
625 */
|
|
626 (info))
|
|
627 {
|
1292
|
628 int depth;
|
1123
|
629 /* This function does not GC */
|
|
630 Fclear_profiling_info ();
|
|
631
|
1292
|
632 depth = internal_bind_int (&in_profiling, 1 + in_profiling);
|
1123
|
633 {
|
|
634 EXTERNAL_PROPERTY_LIST_LOOP_3 (key, value, info)
|
|
635 {
|
|
636 if (EQ (key, Qtiming))
|
|
637 {
|
|
638 CHECK_HASH_TABLE (value);
|
|
639 create_timing_profile_table ();
|
1292
|
640 profiling_lock = 1;
|
1123
|
641 elisp_maphash_unsafe (set_profiling_info_timing_maphash, value,
|
|
642 NULL);
|
1292
|
643 profiling_lock = 0;
|
1123
|
644 }
|
|
645 else if (EQ (key, Qcall_count))
|
1292
|
646 Vcall_count_profile_table = Fcopy_hash_table (value);
|
|
647 else if (EQ (key, Qtotal_timing))
|
|
648 Vtotal_timing_profile_table = Fcopy_hash_table (value);
|
|
649 else if (EQ (key, Qgc_usage))
|
|
650 Vgc_usage_profile_table = Fcopy_hash_table (value);
|
|
651 else if (EQ (key, Qtotal_gc_usage))
|
|
652 Vtotal_gc_usage_profile_table = Fcopy_hash_table (value);
|
1123
|
653 else
|
|
654 invalid_constant ("Unrecognized profiling-info keyword", key);
|
|
655 }
|
|
656 }
|
|
657
|
1292
|
658 unbind_to (depth);
|
1123
|
659 return Qnil;
|
428
|
660 }
|
|
661
|
|
662 static int
|
442
|
663 mark_profiling_info_maphash (const void *void_key,
|
428
|
664 void *void_val,
|
|
665 void *void_closure)
|
|
666 {
|
1292
|
667 mark_object (VOID_TO_LISP (void_key));
|
428
|
668 return 0;
|
|
669 }
|
|
670
|
|
671 void
|
|
672 mark_profiling_info (void)
|
|
673 {
|
|
674 /* This function does not GC */
|
|
675 if (big_profile_table)
|
|
676 {
|
1292
|
677 profiling_lock = 1;
|
428
|
678 maphash (mark_profiling_info_maphash, big_profile_table, 0);
|
1292
|
679 profiling_lock = 0;
|
428
|
680 }
|
|
681 }
|
|
682
|
|
683 DEFUN ("profiling-active-p", Fprofiling_active_p, 0, 0, 0, /*
|
|
684 Return non-nil if profiling information is currently being recorded.
|
|
685 */
|
|
686 ())
|
|
687 {
|
|
688 return profiling_active ? Qt : Qnil;
|
|
689 }
|
|
690
|
|
691 void
|
|
692 syms_of_profile (void)
|
|
693 {
|
|
694 DEFSUBR (Fstart_profiling);
|
|
695 DEFSUBR (Fstop_profiling);
|
|
696 DEFSUBR (Fget_profiling_info);
|
1123
|
697 DEFSUBR (Fset_profiling_info);
|
428
|
698 DEFSUBR (Fclear_profiling_info);
|
|
699 DEFSUBR (Fprofiling_active_p);
|
|
700 }
|
|
701
|
|
702 void
|
|
703 vars_of_profile (void)
|
|
704 {
|
|
705 DEFVAR_INT ("default-profiling-interval", &default_profiling_interval /*
|
|
706 Default CPU time in microseconds between profiling sampling.
|
|
707 Used when the argument to `start-profiling' is nil or omitted.
|
|
708 Note that the time in question is CPU time (when the program is executing
|
1123
|
709 or the kernel is executing on behalf of the program) and not real time, and
|
|
710 there is usually a machine-dependent limit on how small this value can be.
|
428
|
711 */ );
|
|
712 default_profiling_interval = 1000;
|
|
713
|
1123
|
714 staticpro (&Vcall_count_profile_table);
|
428
|
715 Vcall_count_profile_table = Qnil;
|
|
716
|
1292
|
717 staticpro (&Vgc_usage_profile_table);
|
|
718 Vgc_usage_profile_table = Qnil;
|
|
719
|
|
720 staticpro (&Vtotal_gc_usage_profile_table);
|
|
721 Vtotal_gc_usage_profile_table = Qnil;
|
|
722
|
|
723 staticpro (&Vtotal_timing_profile_table);
|
|
724 Vtotal_timing_profile_table = Qnil;
|
428
|
725
|
1292
|
726 #if 0
|
|
727 /* #### This is supposed to be for KKCC but KKCC doesn't use this stuff
|
|
728 currently. */
|
|
729 dump_add_root_struct_ptr (&big_profile_table, &plain_hash_table_description);
|
|
730 #endif /* 0 */
|
|
731
|
|
732 profiling_lock = 0;
|
|
733
|
771
|
734 QSunknown = build_msg_string ("(unknown)");
|
428
|
735 staticpro (&QSunknown);
|
|
736 QSprocessing_events_at_top_level =
|
771
|
737 build_msg_string ("(processing events at top level)");
|
428
|
738 staticpro (&QSprocessing_events_at_top_level);
|
1292
|
739 QSprofile_overhead = build_msg_string ("(profile overhead)");
|
|
740 staticpro (&QSprofile_overhead);
|
1123
|
741
|
|
742 DEFSYMBOL (Qtiming);
|
1292
|
743 DEFSYMBOL (Qtotal_timing);
|
1123
|
744 DEFSYMBOL (Qcall_count);
|
1292
|
745 DEFSYMBOL (Qgc_usage);
|
|
746 DEFSYMBOL (Qtotal_gc_usage);
|
428
|
747 }
|