1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1995-1999
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"
26 #include "posix/Select.h"
28 /* As recommended in the autoconf manual */
29 # ifdef TIME_WITH_SYS_TIME
30 # include <sys/time.h>
33 # ifdef HAVE_SYS_TIME_H
34 # include <sys/time.h>
46 * In the threaded RTS, we can't set the virtual timer because the
47 * thread which has the virtual timer might be sitting waiting for a
48 * capability, and the virtual timer only ticks in CPU time.
50 * So, possible solutions:
52 * (1) tick in realtime. Not very good, because this ticker is used for
53 * profiling, and this will give us unreliable time profiling
54 * results. Furthermore, this requires picking a single OS thread
55 * to be the timekeeper, which is a bad idea because the thread in
56 * question might just be making a temporary call into Haskell land.
58 * (2) save/restore the virtual timer around excursions into STG land.
59 * Sounds great, but I tried it and the resolution of the virtual timer
60 * isn't good enough (on Linux) - most of our excursions fall
61 * within the timer's resolution and we never make any progress.
63 * (3) have a virtual timer in every OS thread. Might be reasonable,
64 * because most of the time there is only ever one of these
65 * threads running, so it approximates a single virtual timer.
66 * But still quite bogus (and I got crashes when I tried this).
68 * For now, we're using (1), but this needs a better solution. --SDM
71 #define ITIMER_FLAVOUR ITIMER_REAL
72 #define ITIMER_SIGNAL SIGALRM
74 #define ITIMER_FLAVOUR ITIMER_VIRTUAL
75 #define ITIMER_SIGNAL SIGVTALRM
80 install_vtalrm_handler(TickProc handle_tick)
82 struct sigaction action;
84 action.sa_handler = handle_tick;
86 sigemptyset(&action.sa_mask);
89 return sigaction(ITIMER_SIGNAL, &action, NULL);
93 startTicker(nat ms, TickProc handle_tick)
95 # ifndef HAVE_SETITIMER
96 /* debugBelch("No virtual timer on this system\n"); */
101 install_vtalrm_handler(handle_tick);
103 #if !defined(THREADED_RTS)
104 timestamp = getourtimeofday();
107 it.it_value.tv_sec = ms / 1000;
108 it.it_value.tv_usec = 1000 * (ms - (1000 * it.it_value.tv_sec));
109 it.it_interval = it.it_value;
110 return (setitimer(ITIMER_FLAVOUR, &it, NULL));
117 # ifndef HAVE_SETITIMER
118 /* debugBelch("No virtual timer on this system\n"); */
123 it.it_value.tv_sec = 0;
124 it.it_value.tv_usec = 0;
125 it.it_interval = it.it_value;
126 return (setitimer(ITIMER_FLAVOUR, &it, NULL));
131 /* This is a potential POSIX version */
136 struct itimerspec it;
139 #if !defined(THREADED_RTS)
140 timestamp = getourtimeofday();
143 se.sigev_notify = SIGEV_SIGNAL;
144 se.sigev_signo = ITIMER_SIGNAL;
145 se.sigev_value.sival_int = ITIMER_SIGNAL;
146 if (timer_create(CLOCK_VIRTUAL, &se, &tid)) {
147 barf("can't create virtual timer");
149 it.it_value.tv_sec = ms / 1000;
150 it.it_value.tv_nsec = 1000000 * (ms - 1000 * it.it_value.tv_sec);
151 it.it_interval = it.it_value;
152 return timer_settime(tid, TIMER_RELTIME, &it, NULL);
159 struct itimerspec it;
162 #if !defined(THREADED_RTS)
163 timestamp = getourtimeofday();
166 se.sigev_notify = SIGEV_SIGNAL;
167 se.sigev_signo = ITIMER_SIGNAL;
168 se.sigev_value.sival_int = ITIMER_SIGNAL;
169 if (timer_create(CLOCK_VIRTUAL, &se, &tid)) {
170 barf("can't create virtual timer");
172 it.it_value.tv_sec = 0;
173 it.it_value.tv_nsec = 0;
174 it.it_interval = it.it_value;
175 return timer_settime(tid, TIMER_RELTIME, &it, NULL);
180 /* Currently unused */
182 block_vtalrm_signal(void)
186 sigemptyset(&signals);
187 sigaddset(&signals, ITIMER_SIGNAL);
189 (void) sigprocmask(SIG_BLOCK, &signals, NULL);
193 unblock_vtalrm_signal(void)
197 sigemptyset(&signals);
198 sigaddset(&signals, ITIMER_SIGNAL);
200 (void) sigprocmask(SIG_UNBLOCK, &signals, NULL);
204 /* gettimeofday() takes around 1us on our 500MHz PIII. Since we're
205 * only calling it 50 times/s, it shouldn't have any great impact.
208 getourtimeofday(void)
211 gettimeofday(&tv, (struct timezone *) NULL);
212 // cast to lnat because nat may be 64 bit when int is only 32 bit
213 return ((lnat)tv.tv_sec * TICK_FREQUENCY +
214 (lnat)tv.tv_usec * TICK_FREQUENCY / 1000000);