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"
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
55 * results. Furthermore, this requires picking a single OS thread
56 * to be the timekeeper, which is a bad idea because the thread in
57 * question might just be making a temporary call into Haskell land.
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 #define ITIMER_FLAVOUR ITIMER_REAL
73 #define ITIMER_SIGNAL SIGALRM
75 #define ITIMER_FLAVOUR ITIMER_VIRTUAL
76 #define ITIMER_SIGNAL SIGVTALRM
81 install_vtalrm_handler(TickProc handle_tick)
83 struct sigaction action;
85 action.sa_handler = handle_tick;
87 sigemptyset(&action.sa_mask);
90 // specify SA_RESTART. One consequence if we don't do this is
91 // that readline gets confused by the -threaded RTS. It seems
92 // that if a SIGALRM handler is installed without SA_RESTART,
93 // readline installs its own SIGALRM signal handler (see
94 // readline's signals.c), and this somehow causes readline to go
95 // wrong when the input exceeds a single line (try it).
96 action.sa_flags = SA_RESTART;
101 return sigaction(ITIMER_SIGNAL, &action, NULL);
105 startTicker(nat ms, TickProc handle_tick)
107 # ifndef HAVE_SETITIMER
108 /* debugBelch("No virtual timer on this system\n"); */
113 install_vtalrm_handler(handle_tick);
115 #if !defined(THREADED_RTS)
116 timestamp = getourtimeofday();
119 it.it_value.tv_sec = ms / 1000;
120 it.it_value.tv_usec = 1000 * (ms - (1000 * it.it_value.tv_sec));
121 it.it_interval = it.it_value;
122 return (setitimer(ITIMER_FLAVOUR, &it, NULL));
129 # ifndef HAVE_SETITIMER
130 /* debugBelch("No virtual timer on this system\n"); */
135 it.it_value.tv_sec = 0;
136 it.it_value.tv_usec = 0;
137 it.it_interval = it.it_value;
138 return (setitimer(ITIMER_FLAVOUR, &it, NULL));
143 /* This is a potential POSIX version */
148 struct itimerspec it;
151 #if !defined(THREADED_RTS)
152 timestamp = getourtimeofday();
155 se.sigev_notify = SIGEV_SIGNAL;
156 se.sigev_signo = ITIMER_SIGNAL;
157 se.sigev_value.sival_int = ITIMER_SIGNAL;
158 if (timer_create(CLOCK_VIRTUAL, &se, &tid)) {
159 barf("can't create virtual timer");
161 it.it_value.tv_sec = ms / 1000;
162 it.it_value.tv_nsec = 1000000 * (ms - 1000 * it.it_value.tv_sec);
163 it.it_interval = it.it_value;
164 return timer_settime(tid, TIMER_RELTIME, &it, NULL);
171 struct itimerspec it;
174 #if !defined(THREADED_RTS)
175 timestamp = getourtimeofday();
178 se.sigev_notify = SIGEV_SIGNAL;
179 se.sigev_signo = ITIMER_SIGNAL;
180 se.sigev_value.sival_int = ITIMER_SIGNAL;
181 if (timer_create(CLOCK_VIRTUAL, &se, &tid)) {
182 barf("can't create virtual timer");
184 it.it_value.tv_sec = 0;
185 it.it_value.tv_nsec = 0;
186 it.it_interval = it.it_value;
187 return timer_settime(tid, TIMER_RELTIME, &it, NULL);
192 /* Currently unused */
194 block_vtalrm_signal(void)
198 sigemptyset(&signals);
199 sigaddset(&signals, ITIMER_SIGNAL);
201 (void) sigprocmask(SIG_BLOCK, &signals, NULL);
205 unblock_vtalrm_signal(void)
209 sigemptyset(&signals);
210 sigaddset(&signals, ITIMER_SIGNAL);
212 (void) sigprocmask(SIG_UNBLOCK, &signals, NULL);
216 /* gettimeofday() takes around 1us on our 500MHz PIII. Since we're
217 * only calling it 50 times/s, it shouldn't have any great impact.
220 getourtimeofday(void)
223 gettimeofday(&tv, (struct timezone *) NULL);
224 // cast to lnat because nat may be 64 bit when int is only 32 bit
225 return ((lnat)tv.tv_sec * 1000 / RtsFlags.MiscFlags.tickInterval +
226 (lnat)tv.tv_usec / (RtsFlags.MiscFlags.tickInterval * 1000));