1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1995-2007
5 * Interval timer for profiling and pre-emptive scheduling.
7 * ---------------------------------------------------------------------------*/
10 * The interval timer is used for profiling and for context switching in the
11 * threaded build. Though POSIX 1003.1b includes a standard interface for
12 * such things, no one really seems to be implementing them yet. Even
13 * Solaris 2.3 only seems to provide support for @CLOCK_REAL@, whereas we're
14 * keen on getting access to @CLOCK_VIRTUAL@.
16 * Hence, we use the old-fashioned @setitimer@ that just about everyone seems
17 * to support. So much for standards.
23 #include "posix/Itimer.h"
24 #include "Proftimer.h"
27 #include "posix/Select.h"
29 /* As recommended in the autoconf manual */
30 # ifdef TIME_WITH_SYS_TIME
31 # include <sys/time.h>
34 # ifdef HAVE_SYS_TIME_H
35 # include <sys/time.h>
49 * In the threaded RTS, we can't set the virtual timer because the
50 * thread which has the virtual timer might be sitting waiting for a
51 * capability, and the virtual timer only ticks in CPU time.
53 * So, possible solutions:
55 * (1) tick in realtime. Not very good, because this ticker is used for
56 * profiling, and this will give us unreliable time profiling
59 * (2) save/restore the virtual timer around excursions into STG land.
60 * Sounds great, but I tried it and the resolution of the virtual timer
61 * isn't good enough (on Linux) - most of our excursions fall
62 * within the timer's resolution and we never make any progress.
64 * (3) have a virtual timer in every OS thread. Might be reasonable,
65 * because most of the time there is only ever one of these
66 * threads running, so it approximates a single virtual timer.
67 * But still quite bogus (and I got crashes when I tried this).
69 * For now, we're using (1), but this needs a better solution. --SDM
72 #if defined(USE_TIMER_CREATE)
74 # define ITIMER_SIGNAL SIGVTALRM
76 # define TIMER_FLAVOUR CLOCK_REALTIME
78 # define TIMER_FLAVOUR CLOCK_PROCESS_CPUTIME_ID
81 #elif defined(HAVE_SETITIMER)
84 // Oh dear, we have to use SIGALRM if there's no timer_create and
85 // we're using the THREADED_RTS. This leads to problems, see bug #850.
86 # define ITIMER_SIGNAL SIGALRM
87 # define ITIMER_FLAVOUR ITIMER_REAL
89 # define ITIMER_SIGNAL SIGVTALRM
90 # define ITIMER_FLAVOUR ITIMER_VIRTUAL
95 # error No way to set an interval timer.
99 #if defined(USE_TIMER_CREATE)
100 static timer_t timer;
103 static nat itimer_interval = 50;
107 install_vtalrm_handler(TickProc handle_tick)
109 struct sigaction action;
111 action.sa_handler = handle_tick;
113 sigemptyset(&action.sa_mask);
116 // specify SA_RESTART. One consequence if we don't do this is
117 // that readline gets confused by the -threaded RTS. It seems
118 // that if a SIGALRM handler is installed without SA_RESTART,
119 // readline installs its own SIGALRM signal handler (see
120 // readline's signals.c), and this somehow causes readline to go
121 // wrong when the input exceeds a single line (try it).
122 action.sa_flags = SA_RESTART;
127 if (sigaction(ITIMER_SIGNAL, &action, NULL) == -1) {
128 sysErrorBelch("sigaction");
129 stg_exit(EXIT_FAILURE);
134 initTicker (nat ms, TickProc handle_tick)
136 install_vtalrm_handler(handle_tick);
138 #if !defined(THREADED_RTS)
139 timestamp = getourtimeofday();
142 itimer_interval = ms;
144 #if defined(USE_TIMER_CREATE)
148 // Keep programs like valgrind happy
149 memset(&ev, 0, sizeof(ev));
151 ev.sigev_notify = SIGEV_SIGNAL;
152 ev.sigev_signo = ITIMER_SIGNAL;
154 if (timer_create(TIMER_FLAVOUR, &ev, &timer) != 0) {
155 sysErrorBelch("timer_create");
156 stg_exit(EXIT_FAILURE);
165 #if defined(USE_TIMER_CREATE)
167 struct itimerspec it;
169 it.it_value.tv_sec = itimer_interval / 1000;
170 it.it_value.tv_nsec = (itimer_interval % 1000) * 1000000;
171 it.it_interval = it.it_value;
173 if (timer_settime(timer, 0, &it, NULL) != 0) {
174 sysErrorBelch("timer_settime");
175 stg_exit(EXIT_FAILURE);
182 it.it_value.tv_sec = itimer_interval / 1000;
183 it.it_value.tv_usec = (itimer_interval % 1000) * 1000;
184 it.it_interval = it.it_value;
186 if (setitimer(ITIMER_FLAVOUR, &it, NULL) != 0) {
187 sysErrorBelch("setitimer");
188 stg_exit(EXIT_FAILURE);
197 #if defined(USE_TIMER_CREATE)
198 struct itimerspec it;
200 it.it_value.tv_sec = 0;
201 it.it_value.tv_nsec = 0;
202 it.it_interval = it.it_value;
204 if (timer_settime(timer, 0, &it, NULL) != 0) {
205 sysErrorBelch("timer_settime");
206 stg_exit(EXIT_FAILURE);
211 it.it_value.tv_sec = 0;
212 it.it_value.tv_usec = 0;
213 it.it_interval = it.it_value;
215 if (setitimer(ITIMER_FLAVOUR, &it, NULL) != 0) {
216 sysErrorBelch("setitimer");
217 stg_exit(EXIT_FAILURE);
225 #if defined(USE_TIMER_CREATE)
227 // ignore errors - we don't really care if it fails.
232 /* Currently unused */
234 block_vtalrm_signal(void)
238 sigemptyset(&signals);
239 sigaddset(&signals, ITIMER_SIGNAL);
241 (void) sigprocmask(SIG_BLOCK, &signals, NULL);
245 unblock_vtalrm_signal(void)
249 sigemptyset(&signals);
250 sigaddset(&signals, ITIMER_SIGNAL);
252 (void) sigprocmask(SIG_UNBLOCK, &signals, NULL);
256 /* gettimeofday() takes around 1us on our 500MHz PIII. Since we're
257 * only calling it 50 times/s, it shouldn't have any great impact.
260 getourtimeofday(void)
264 interval = RtsFlags.MiscFlags.tickInterval;
265 if (interval == 0) { interval = 50; }
266 gettimeofday(&tv, (struct timezone *) NULL);
267 // cast to lnat because nat may be 64 bit when int is only 32 bit
268 return ((lnat)tv.tv_sec * 1000 / interval +
269 (lnat)tv.tv_usec / (interval * 1000));