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>
47 * In the threaded RTS, we can't set the virtual timer because the
48 * thread which has the virtual timer might be sitting waiting for a
49 * capability, and the virtual timer only ticks in CPU time.
51 * So, possible solutions:
53 * (1) tick in realtime. Not very good, because this ticker is used for
54 * profiling, and this will give us unreliable time profiling
57 * (2) save/restore the virtual timer around excursions into STG land.
58 * Sounds great, but I tried it and the resolution of the virtual timer
59 * isn't good enough (on Linux) - most of our excursions fall
60 * within the timer's resolution and we never make any progress.
62 * (3) have a virtual timer in every OS thread. Might be reasonable,
63 * because most of the time there is only ever one of these
64 * threads running, so it approximates a single virtual timer.
65 * But still quite bogus (and I got crashes when I tried this).
67 * For now, we're using (1), but this needs a better solution. --SDM
70 #if defined(USE_TIMER_CREATE)
72 # define ITIMER_SIGNAL SIGVTALRM
74 # define TIMER_FLAVOUR CLOCK_REALTIME
76 # define TIMER_FLAVOUR CLOCK_PROCESS_CPUTIME_ID
79 #elif defined(HAVE_SETITIMER)
82 // Oh dear, we have to use SIGALRM if there's no timer_create and
83 // we're using the THREADED_RTS. This leads to problems, see bug #850.
84 # define ITIMER_SIGNAL SIGALRM
85 # define ITIMER_FLAVOUR ITIMER_REAL
87 # define ITIMER_SIGNAL SIGVTALRM
88 # define ITIMER_FLAVOUR ITIMER_VIRTUAL
93 # error No way to set an interval timer.
97 #if defined(USE_TIMER_CREATE)
101 static nat itimer_interval = 50;
105 install_vtalrm_handler(TickProc handle_tick)
107 struct sigaction action;
109 action.sa_handler = handle_tick;
111 sigemptyset(&action.sa_mask);
114 // specify SA_RESTART. One consequence if we don't do this is
115 // that readline gets confused by the -threaded RTS. It seems
116 // that if a SIGALRM handler is installed without SA_RESTART,
117 // readline installs its own SIGALRM signal handler (see
118 // readline's signals.c), and this somehow causes readline to go
119 // wrong when the input exceeds a single line (try it).
120 action.sa_flags = SA_RESTART;
125 if (sigaction(ITIMER_SIGNAL, &action, NULL) == -1) {
126 sysErrorBelch("sigaction");
127 stg_exit(EXIT_FAILURE);
132 initTicker (nat ms, TickProc handle_tick)
134 install_vtalrm_handler(handle_tick);
136 #if !defined(THREADED_RTS)
137 timestamp = getourtimeofday();
140 itimer_interval = ms;
142 #if defined(USE_TIMER_CREATE)
146 ev.sigev_notify = SIGEV_SIGNAL;
147 ev.sigev_signo = ITIMER_SIGNAL;
149 if (timer_create(TIMER_FLAVOUR, &ev, &timer) != 0) {
150 sysErrorBelch("timer_create");
151 stg_exit(EXIT_FAILURE);
160 #if defined(USE_TIMER_CREATE)
162 struct itimerspec it;
164 it.it_value.tv_sec = itimer_interval / 1000;
165 it.it_value.tv_nsec = (itimer_interval % 1000) * 1000000;
166 it.it_interval = it.it_value;
168 if (timer_settime(timer, 0, &it, NULL) != 0) {
169 sysErrorBelch("timer_settime");
170 stg_exit(EXIT_FAILURE);
177 it.it_value.tv_sec = itimer_interval / 1000;
178 it.it_value.tv_usec = (itimer_interval % 1000) * 1000;
179 it.it_interval = it.it_value;
181 if (setitimer(ITIMER_FLAVOUR, &it, NULL) != 0) {
182 sysErrorBelch("setitimer");
183 stg_exit(EXIT_FAILURE);
192 #if defined(USE_TIMER_CREATE)
193 struct itimerspec it;
195 it.it_value.tv_sec = 0;
196 it.it_value.tv_nsec = 0;
197 it.it_interval = it.it_value;
199 if (timer_settime(timer, 0, &it, NULL) != 0) {
200 sysErrorBelch("timer_settime");
201 stg_exit(EXIT_FAILURE);
206 it.it_value.tv_sec = 0;
207 it.it_value.tv_usec = 0;
208 it.it_interval = it.it_value;
210 if (setitimer(ITIMER_FLAVOUR, &it, NULL) != 0) {
211 sysErrorBelch("setitimer");
212 stg_exit(EXIT_FAILURE);
220 #if defined(USE_TIMER_CREATE)
222 // ignore errors - we don't really care if it fails.
227 /* Currently unused */
229 block_vtalrm_signal(void)
233 sigemptyset(&signals);
234 sigaddset(&signals, ITIMER_SIGNAL);
236 (void) sigprocmask(SIG_BLOCK, &signals, NULL);
240 unblock_vtalrm_signal(void)
244 sigemptyset(&signals);
245 sigaddset(&signals, ITIMER_SIGNAL);
247 (void) sigprocmask(SIG_UNBLOCK, &signals, NULL);
251 /* gettimeofday() takes around 1us on our 500MHz PIII. Since we're
252 * only calling it 50 times/s, it shouldn't have any great impact.
255 getourtimeofday(void)
259 interval = RtsFlags.MiscFlags.tickInterval;
260 if (interval == 0) { interval = 50; }
261 gettimeofday(&tv, (struct timezone *) NULL);
262 // cast to lnat because nat may be 64 bit when int is only 32 bit
263 return ((lnat)tv.tv_sec * 1000 / interval +
264 (lnat)tv.tv_usec / (interval * 1000));