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