1 /* -----------------------------------------------------------------------------
2 * $Id: Itimer.c,v 1.29 2001/11/27 01:57:59 sof Exp $
4 * (c) The GHC Team, 1995-1999
6 * Interval timer for profiling and pre-emptive scheduling.
8 * ---------------------------------------------------------------------------*/
11 * The interval timer is used for profiling and for context switching in the
12 * threaded build. Though POSIX 1003.1b includes a standard interface for
13 * such things, no one really seems to be implementing them yet. Even
14 * Solaris 2.3 only seems to provide support for @CLOCK_REAL@, whereas we're
15 * keen on getting access to @CLOCK_VIRTUAL@.
17 * Hence, we use the old-fashioned @setitimer@ that just about everyone seems
18 * to support. So much for standards.
21 /* This is not posix compliant. */
22 /* #include "PosixSource.h" */
27 #include "Proftimer.h"
30 /* As recommended in the autoconf manual */
31 # ifdef TIME_WITH_SYS_TIME
32 # include <sys/time.h>
35 # ifdef HAVE_SYS_TIME_H
36 # include <sys/time.h>
48 /* ticks left before next pre-emptive context switch */
49 int ticks_to_ctxt_switch = 0;
51 /* -----------------------------------------------------------------------------
54 We use the ticker for time profiling.
56 SMP note: this signal could be delivered to *any* thread. We have
57 to ensure that it doesn't matter which thread actually runs the
59 -------------------------------------------------------------------------- */
63 #if defined(mingw32_TARGET_OS) || (defined(cygwin32_TARGET_OS) && !defined(HAVE_SETITIMER))
66 handle_tick(UINT uID STG_UNUSED, UINT uMsg STG_UNUSED, DWORD dwUser STG_UNUSED,
67 DWORD dw1 STG_UNUSED, DWORD d STG_UNUSED)
69 handle_tick(int unused STG_UNUSED)
78 if (RtsFlags.ConcFlags.ctxtSwitchTicks > 0) {
79 ticks_to_ctxt_switch--;
80 if (ticks_to_ctxt_switch <= 0) {
81 ticks_to_ctxt_switch = RtsFlags.ConcFlags.ctxtSwitchTicks;
82 context_switch = 1; /* schedule a context switch */
89 * Handling timer events under cygwin32 is not done with signal/setitimer.
90 * Instead of the two steps of first registering a signal handler to handle
91 * \tr{SIGVTALRM} and then start generating them via @setitimer()@, we use
92 * the Multimedia API (MM) and its @timeSetEvent@. (Internally, the MM API
93 * creates a separate thread that will notify the main thread of timer
94 * expiry). -- SOF 7/96
96 * 11/98: if the cygwin DLL supports setitimer(), then use it instead.
99 #if defined(mingw32_TARGET_OS) || (defined(cygwin32_TARGET_OS) && !defined(HAVE_SETITIMER))
101 static LPTIMECALLBACK vtalrm_cback;
102 static unsigned int vtalrm_id = 0;
103 static unsigned int period = -1;
106 startVirtTimer(nat ms)
108 /* On Win32 setups that don't have support for
109 setitimer(), we use the MultiMedia API's timer
112 The delivery of ticks isn't free; the performance hit should be checked.
117 vtalrm_cback = handle_tick;
119 if ( timeGetDevCaps(&tc, sizeof(TIMECAPS)) == TIMERR_NOERROR) {
120 period = tc.wPeriodMin;
121 delay = timeBeginPeriod(period);
122 if (delay == TIMERR_NOCANDO) { /* error of some sort. */
134 timeSetEvent(ms, /* event every `delay' milliseconds. */
135 1, /* precision is within 1 ms */
137 TIME_CALLBACK_FUNCTION, /* ordinary callback */
146 /* Shutdown the MM timer */
147 if ( vtalrm_id != 0 ) {
148 timeKillEvent(vtalrm_id);
151 timeEndPeriod(period);
160 install_vtalrm_handler(void)
162 struct sigaction action;
164 action.sa_handler = handle_tick;
166 sigemptyset(&action.sa_mask);
169 return sigaction(SIGVTALRM, &action, NULL);
173 startVirtTimer(nat ms)
175 # ifndef HAVE_SETITIMER
176 /* fprintf(stderr, "No virtual timer on this system\n"); */
181 install_vtalrm_handler();
183 timestamp = getourtimeofday();
189 it.it_value.tv_sec = ms / 1000;
190 it.it_value.tv_usec = 1000 * (ms - (1000 * it.it_value.tv_sec));
191 it.it_interval = it.it_value;
192 return (setitimer(ITIMER_VIRTUAL, &it, NULL));
199 # ifndef HAVE_SETITIMER
200 /* fprintf(stderr, "No virtual timer on this system\n"); */
205 it.it_value.tv_sec = 0;
206 it.it_value.tv_usec = 0;
207 it.it_interval = it.it_value;
208 return (setitimer(ITIMER_VIRTUAL, &it, NULL));
212 #endif /* !{mingw,cygwin32}_TARGET_OS */
215 /* This is a potential POSIX version */
217 startVirtTimer(nat ms)
220 struct itimerspec it;
223 timestamp = getourtimeofday();
229 se.sigev_notify = SIGEV_SIGNAL;
230 se.sigev_signo = SIGVTALRM;
231 se.sigev_value.sival_int = SIGVTALRM;
232 if (timer_create(CLOCK_VIRTUAL, &se, &tid)) {
233 barf("can't create virtual timer");
235 it.it_value.tv_sec = ms / 1000;
236 it.it_value.tv_nsec = 1000000 * (ms - 1000 * it.it_value.tv_sec);
237 it.it_interval = it.it_value;
238 return timer_settime(tid, TIMER_RELTIME, &it, NULL);
245 struct itimerspec it;
248 timestamp = getourtimeofday();
250 se.sigev_notify = SIGEV_SIGNAL;
251 se.sigev_signo = SIGVTALRM;
252 se.sigev_value.sival_int = SIGVTALRM;
253 if (timer_create(CLOCK_VIRTUAL, &se, &tid)) {
254 barf("can't create virtual timer");
256 it.it_value.tv_sec = 0;
257 it.it_value.tv_nsec = 0;
258 it.it_interval = it.it_value;
259 return timer_settime(tid, TIMER_RELTIME, &it, NULL);
264 #if defined(mingw32_TARGET_OS) || (defined(cygwin32_TARGET_OS) && !defined(HAVE_SETITIMER))
267 block_vtalrm_signal(void)
271 sigemptyset(&signals);
272 sigaddset(&signals, SIGVTALRM);
274 (void) sigprocmask(SIG_BLOCK, &signals, NULL);
278 unblock_vtalrm_signal(void)
282 sigemptyset(&signals);
283 sigaddset(&signals, SIGVTALRM);
285 (void) sigprocmask(SIG_UNBLOCK, &signals, NULL);
289 /* gettimeofday() takes around 1us on our 500MHz PIII. Since we're
290 * only calling it 50 times/s, it shouldn't have any great impact.
292 #if !defined(mingw32_TARGET_OS)
294 getourtimeofday(void)
297 gettimeofday(&tv, (struct timezone *) NULL);
298 return (tv.tv_sec * TICK_FREQUENCY +
299 tv.tv_usec * TICK_FREQUENCY / 1000000);
303 getourtimeofday(void)
305 return ((unsigned int)GetTickCount() * TICK_FREQUENCY) / 1000;