1 /* -----------------------------------------------------------------------------
2 * $Id: Itimer.c,v 1.24 2001/11/13 13:38:02 simonmar 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))
65 handle_tick(UINT uID STG_UNUSED, UINT uMsg STG_UNUSED, DWORD dwUser STG_UNUSED,
66 DWORD dw1 STG_UNUSED, DWORD d STG_UNUSED)
68 handle_tick(int unused STG_UNUSED)
77 if (RtsFlags.ConcFlags.ctxtSwitchTicks > 0) {
78 ticks_to_ctxt_switch--;
79 if (ticks_to_ctxt_switch <= 0) {
80 ticks_to_ctxt_switch = RtsFlags.ConcFlags.ctxtSwitchTicks;
81 context_switch = 1; /* schedule a context switch */
88 * Handling timer events under cygwin32 is not done with signal/setitimer.
89 * Instead of the two steps of first registering a signal handler to handle
90 * \tr{SIGVTALRM} and then start generating them via @setitimer()@, we use
91 * the Multimedia API (MM) and its @timeSetEvent@. (Internally, the MM API
92 * creates a separate thread that will notify the main thread of timer
93 * expiry). -- SOF 7/96
95 * 11/98: if the cygwin DLL supports setitimer(), then use it instead.
98 #if defined(mingw32_TARGET_OS) || (defined(cygwin32_TARGET_OS) && !defined(HAVE_SETITIMER))
100 LPTIMECALLBACK vtalrm_cback;
103 initialize_virtual_timer(nat ms)
105 /* On Win32 setups that don't have support for
106 setitimer(), we use the MultiMedia API's timer
109 The delivery of ticks isn't free; the performance hit should be checked.
112 static unsigned int vtalrm_id;
115 delay = timeBeginPeriod(1);
116 if (delay == TIMERR_NOCANDO) { /* error of some sort. */
120 timeSetEvent(ms, /* event every `delay' milliseconds. */
121 1, /* precision is within 1 ms */
123 TIME_CALLBACK_FUNCTION, /* ordinary callback */
126 timeKillEvent(vtalrm_id);
136 initialize_virtual_timer(nat ms)
138 # ifndef HAVE_SETITIMER
139 /* fprintf(stderr, "No virtual timer on this system\n"); */
144 timestamp = getourtimeofday();
146 it.it_value.tv_sec = ms / 1000;
147 it.it_value.tv_usec = 1000 * (ms - (1000 * it.it_value.tv_sec));
148 it.it_interval = it.it_value;
149 return (setitimer(ITIMER_VIRTUAL, &it, NULL));
153 #endif /* !cygwin32_TARGET_OS */
156 /* This is a potential POSIX version */
158 initialize_virtual_timer(nat ms)
161 struct itimerspec it;
164 timestamp = getourtimeofday();
166 se.sigev_notify = SIGEV_SIGNAL;
167 se.sigev_signo = SIGVTALRM;
168 se.sigev_value.sival_int = SIGVTALRM;
169 if (timer_create(CLOCK_VIRTUAL, &se, &tid)) {
170 barf("can't create virtual timer");
172 it.it_value.tv_sec = ms / 1000;
173 it.it_value.tv_nsec = 1000000 * (ms - 1000 * it.it_value.tv_sec);
174 it.it_interval = it.it_value;
175 timer_settime(tid, TIMER_RELTIME, &it, NULL);
179 #if defined(mingw32_TARGET_OS) || (defined(cygwin32_TARGET_OS) && !defined(HAVE_SETITIMER))
181 install_vtalrm_handler(void)
183 vtalrm_cback = handle_tick;
189 install_vtalrm_handler(void)
191 struct sigaction action;
193 action.sa_handler = handle_tick;
195 sigemptyset(&action.sa_mask);
198 return sigaction(SIGVTALRM, &action, NULL);
202 block_vtalrm_signal(void)
206 sigemptyset(&signals);
207 sigaddset(&signals, SIGVTALRM);
209 (void) sigprocmask(SIG_BLOCK, &signals, NULL);
213 unblock_vtalrm_signal(void)
217 sigemptyset(&signals);
218 sigaddset(&signals, SIGVTALRM);
220 (void) sigprocmask(SIG_UNBLOCK, &signals, NULL);
224 /* gettimeofday() takes around 1us on our 500MHz PIII. Since we're
225 * only calling it 50 times/s, it shouldn't have any great impact.
227 #if !defined(mingw32_TARGET_OS)
229 getourtimeofday(void)
232 gettimeofday(&tv, (struct timezone *) NULL);
233 return (tv.tv_sec * TICK_FREQUENCY +
234 tv.tv_usec * TICK_FREQUENCY / 1000000);
238 getourtimeofday(void)
240 return ((unsigned int)GetTickCount() * TICK_FREQUENCY) / 1000;