/* Profiler implementation.
-Copyright (C) 2012 Free Software Foundation, Inc.
+Copyright (C) 2012-2013 Free Software Foundation, Inc.
This file is part of GNU Emacs.
}
/* Record the current backtrace in LOG. COUNT is the weight of this
- current backtrace: milliseconds for CPU counts, and the allocation
- size for memory logs. */
+ current backtrace: interrupt counts for CPU, and the allocation
+ size for memory. */
static void
record_backtrace (log_t *log, EMACS_INT count)
/* The profiler timer and whether it was properly initialized, if
POSIX timers are available. */
-#ifdef HAVE_TIMER_SETTIME
+#ifdef HAVE_ITIMERSPEC
static timer_t profiler_timer;
static bool profiler_timer_ok;
#endif
/* Separate counter for the time spent in the GC. */
static EMACS_INT cpu_gc_count;
-/* The current sampling interval in milliseconds. */
+/* The current sampling interval in nanoseconds. */
static EMACS_INT current_sampling_interval;
/* Signal handler for sampling profiler. */
not expect the ARRAY_MARK_FLAG to be set. We could try and
harden the hash-table code, but it doesn't seem worth the
effort. */
- cpu_gc_count = saturated_add (cpu_gc_count, current_sampling_interval);
+ cpu_gc_count = saturated_add (cpu_gc_count, 1);
else
{
+ Lisp_Object oquit;
+ bool saved_pending_signals;
+ EMACS_INT count = 1;
+#ifdef HAVE_ITIMERSPEC
+ if (profiler_timer_ok)
+ {
+ int overruns = timer_getoverrun (profiler_timer);
+ eassert (0 <= overruns);
+ count += overruns;
+ }
+#endif
+ /* record_backtrace uses hash functions that call Fequal, which
+ uses QUIT, which can call malloc, which can cause disaster in
+ a signal handler. So inhibit QUIT. */
+ oquit = Vinhibit_quit;
+ saved_pending_signals = pending_signals;
+ Vinhibit_quit = Qt;
+ pending_signals = 0;
+
eassert (HASH_TABLE_P (cpu_log));
- record_backtrace (XHASH_TABLE (cpu_log), current_sampling_interval);
+ record_backtrace (XHASH_TABLE (cpu_log), count);
+
+ Vinhibit_quit = oquit;
+ pending_signals = saved_pending_signals;
}
}
struct sigaction action;
struct itimerval timer;
struct timespec interval;
+ int billion = 1000000000;
if (! RANGED_INTEGERP (1, sampling_interval,
- (TYPE_MAXIMUM (time_t) < EMACS_INT_MAX / 1000
- ? (EMACS_INT) TYPE_MAXIMUM (time_t) * 1000 + 999
+ (TYPE_MAXIMUM (time_t) < EMACS_INT_MAX / billion
+ ? ((EMACS_INT) TYPE_MAXIMUM (time_t) * billion
+ + (billion - 1))
: EMACS_INT_MAX)))
return NOT_RUNNING;
current_sampling_interval = XINT (sampling_interval);
- interval = make_emacs_time (current_sampling_interval / 1000,
- current_sampling_interval % 1000 * 1000000);
+ interval = make_emacs_time (current_sampling_interval / billion,
+ current_sampling_interval % billion);
emacs_sigaction_init (&action, deliver_profiler_signal);
sigaction (SIGPROF, &action, 0);
-#ifdef HAVE_TIMER_SETTIME
+#ifdef HAVE_ITIMERSPEC
if (! profiler_timer_ok)
{
/* System clocks to try, in decreasing order of desirability. */
{
struct itimerspec ispec;
ispec.it_value = ispec.it_interval = interval;
- timer_settime (profiler_timer, 0, &ispec, 0);
- return TIMER_SETTIME_RUNNING;
+ if (timer_settime (profiler_timer, 0, &ispec, 0) == 0)
+ return TIMER_SETTIME_RUNNING;
}
#endif
+#ifdef HAVE_SETITIMER
timer.it_value = timer.it_interval = make_timeval (interval);
- setitimer (ITIMER_PROF, &timer, 0);
- return SETITIMER_RUNNING;
+ if (setitimer (ITIMER_PROF, &timer, 0) == 0)
+ return SETITIMER_RUNNING;
+#endif
+
+ return NOT_RUNNING;
}
DEFUN ("profiler-cpu-start", Fprofiler_cpu_start, Sprofiler_cpu_start,
1, 1, 0,
doc: /* Start or restart the cpu profiler.
-It takes call-stack samples each SAMPLING-INTERVAL milliseconds.
+It takes call-stack samples each SAMPLING-INTERVAL nanoseconds, approximately.
See also `profiler-log-size' and `profiler-max-stack-depth'. */)
(Lisp_Object sampling_interval)
{
case NOT_RUNNING:
return Qnil;
-#ifdef HAVE_TIMER_SETTIME
+#ifdef HAVE_ITIMERSPEC
case TIMER_SETTIME_RUNNING:
{
struct itimerspec disable;
break;
#endif
+#ifdef HAVE_SETITIMER
case SETITIMER_RUNNING:
{
struct itimerval disable;
setitimer (ITIMER_PROF, &disable, 0);
}
break;
+#endif
}
signal (SIGPROF, SIG_IGN);