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.
20 #include "PosixSource.h"
25 #include "Proftimer.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)
83 # if defined(THREADED_RTS) || !defined(HAVE_SETITIMER_VIRTUAL)
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 // We also use it if we don't have a virtual timer (trac #2883).
87 # define ITIMER_SIGNAL SIGALRM
88 # define ITIMER_FLAVOUR ITIMER_REAL
90 # define ITIMER_SIGNAL SIGVTALRM
91 # define ITIMER_FLAVOUR ITIMER_VIRTUAL
96 # error No way to set an interval timer.
100 #if defined(USE_TIMER_CREATE)
101 static timer_t timer;
104 static nat itimer_interval = 50;
108 install_vtalrm_handler(TickProc handle_tick)
110 struct sigaction action;
112 action.sa_handler = handle_tick;
114 sigemptyset(&action.sa_mask);
117 // specify SA_RESTART. One consequence if we don't do this is
118 // that readline gets confused by the -threaded RTS. It seems
119 // that if a SIGALRM handler is installed without SA_RESTART,
120 // readline installs its own SIGALRM signal handler (see
121 // readline's signals.c), and this somehow causes readline to go
122 // wrong when the input exceeds a single line (try it).
123 action.sa_flags = SA_RESTART;
128 if (sigaction(ITIMER_SIGNAL, &action, NULL) == -1) {
129 sysErrorBelch("sigaction");
130 stg_exit(EXIT_FAILURE);
135 initTicker (nat ms, TickProc handle_tick)
137 install_vtalrm_handler(handle_tick);
139 #if !defined(THREADED_RTS)
140 timestamp = getourtimeofday();
143 itimer_interval = ms;
145 #if defined(USE_TIMER_CREATE)
149 // Keep programs like valgrind happy
150 memset(&ev, 0, sizeof(ev));
152 ev.sigev_notify = SIGEV_SIGNAL;
153 ev.sigev_signo = ITIMER_SIGNAL;
155 if (timer_create(TIMER_FLAVOUR, &ev, &timer) != 0) {
156 sysErrorBelch("timer_create");
157 stg_exit(EXIT_FAILURE);
166 #if defined(USE_TIMER_CREATE)
168 struct itimerspec it;
170 it.it_value.tv_sec = itimer_interval / 1000;
171 it.it_value.tv_nsec = (itimer_interval % 1000) * 1000000;
172 it.it_interval = it.it_value;
174 if (timer_settime(timer, 0, &it, NULL) != 0) {
175 sysErrorBelch("timer_settime");
176 stg_exit(EXIT_FAILURE);
183 it.it_value.tv_sec = itimer_interval / 1000;
184 it.it_value.tv_usec = (itimer_interval % 1000) * 1000;
185 it.it_interval = it.it_value;
187 if (setitimer(ITIMER_FLAVOUR, &it, NULL) != 0) {
188 sysErrorBelch("setitimer");
189 stg_exit(EXIT_FAILURE);
198 #if defined(USE_TIMER_CREATE)
199 struct itimerspec it;
201 it.it_value.tv_sec = 0;
202 it.it_value.tv_nsec = 0;
203 it.it_interval = it.it_value;
205 if (timer_settime(timer, 0, &it, NULL) != 0) {
206 sysErrorBelch("timer_settime");
207 stg_exit(EXIT_FAILURE);
212 it.it_value.tv_sec = 0;
213 it.it_value.tv_usec = 0;
214 it.it_interval = it.it_value;
216 if (setitimer(ITIMER_FLAVOUR, &it, NULL) != 0) {
217 sysErrorBelch("setitimer");
218 stg_exit(EXIT_FAILURE);
226 #if defined(USE_TIMER_CREATE)
228 // ignore errors - we don't really care if it fails.
232 /* gettimeofday() takes around 1us on our 500MHz PIII. Since we're
233 * only calling it 50 times/s, it shouldn't have any great impact.
236 getourtimeofday(void)
240 interval = RtsFlags.MiscFlags.tickInterval;
241 if (interval == 0) { interval = 50; }
242 gettimeofday(&tv, (struct timezone *) NULL);
244 // Avoid overflow when we multiply seconds by 1000. See #2848
245 return (lnat)((StgWord64)tv.tv_sec * 1000 / interval +
246 (StgWord64)tv.tv_usec / (interval * 1000));