[project @ 2001-11-05 14:08:28 by chak]
[ghc-hetmet.git] / ghc / rts / Itimer.c
1 /* -----------------------------------------------------------------------------
2  * $Id: Itimer.c,v 1.23 2001/08/14 13:40:09 sewardj Exp $
3  *
4  * (c) The GHC Team, 1995-1999
5  *
6  * Interval timer for profiling and pre-emptive scheduling.
7  *
8  * ---------------------------------------------------------------------------*/
9
10 /*
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@.
16  * 
17  * Hence, we use the old-fashioned @setitimer@ that just about everyone seems
18  * to support.  So much for standards.
19  */
20
21 /* This is not posix compliant. */
22 /* #include "PosixSource.h" */
23
24 #include "Rts.h"
25 #include "RtsFlags.h"
26 #include "Itimer.h"
27 #include "Proftimer.h"
28 #include "Schedule.h"
29
30 /* As recommended in the autoconf manual */
31 # ifdef TIME_WITH_SYS_TIME
32 #  include <sys/time.h>
33 #  include <time.h>
34 # else
35 #  ifdef HAVE_SYS_TIME_H
36 #   include <sys/time.h>
37 #  else
38 #   include <time.h>
39 #  endif
40 # endif
41
42 #if HAVE_WINDOWS_H
43 # include <windows.h>
44 #endif
45  
46 lnat total_ticks = 0;
47
48 /* ticks left before next pre-emptive context switch */
49 int ticks_to_ctxt_switch = 0;
50
51 /* -----------------------------------------------------------------------------
52    Tick handler
53
54    We use the ticker for time profiling.
55
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
58    signal handler.
59    -------------------------------------------------------------------------- */
60
61 static
62 void
63 #if defined(mingw32_TARGET_OS) || (defined(cygwin32_TARGET_OS) && !defined(HAVE_SETITIMER))
64 CALLBACK
65 handle_tick(UINT uID STG_UNUSED, UINT uMsg STG_UNUSED, DWORD dwUser STG_UNUSED,
66             DWORD dw1 STG_UNUSED, DWORD d STG_UNUSED)
67 #else
68 handle_tick(int unused STG_UNUSED)
69 #endif
70 {
71   total_ticks++;
72
73 #ifdef PROFILING
74   handleProfTick();
75 #endif
76
77   /* so we can get a rough indication of the current time at any point
78    * without having to call gettimeofday() (see Select.c):
79    */
80   ticks_since_timestamp++;
81
82   if (RtsFlags.ConcFlags.ctxtSwitchTicks > 0) {
83       ticks_to_ctxt_switch--;
84       if (ticks_to_ctxt_switch <= 0) {
85           ticks_to_ctxt_switch = RtsFlags.ConcFlags.ctxtSwitchTicks;
86           context_switch = 1;   /* schedule a context switch */
87       }
88   }
89 }
90
91
92 /*
93  * Handling timer events under cygwin32 is not done with signal/setitimer.
94  * Instead of the two steps of first registering a signal handler to handle
95  * \tr{SIGVTALRM} and then start generating them via @setitimer()@, we use
96  * the Multimedia API (MM) and its @timeSetEvent@. (Internally, the MM API
97  * creates a separate thread that will notify the main thread of timer
98  * expiry). -- SOF 7/96
99  *
100  * 11/98: if the cygwin DLL supports setitimer(), then use it instead.
101  */
102
103 #if defined(mingw32_TARGET_OS) || (defined(cygwin32_TARGET_OS) && !defined(HAVE_SETITIMER))
104
105 LPTIMECALLBACK vtalrm_cback;
106
107 nat
108 initialize_virtual_timer(nat ms)
109 {
110   /* On Win32 setups that don't have support for
111      setitimer(), we use the MultiMedia API's timer
112      support.
113      
114      The delivery of ticks isn't free; the performance hit should be checked.
115   */
116   unsigned int delay;
117   static unsigned int vtalrm_id;
118  
119   if (ms) {
120     delay = timeBeginPeriod(1);
121     if (delay == TIMERR_NOCANDO) { /* error of some sort. */
122       return delay;
123     }
124     vtalrm_id =
125       timeSetEvent(ms,      /* event every `delay' milliseconds. */
126                    1,       /* precision is within 1 ms */
127                    vtalrm_cback,
128                    TIME_CALLBACK_FUNCTION,     /* ordinary callback */
129                    TIME_PERIODIC);
130   } else {
131     timeKillEvent(vtalrm_id);
132     timeEndPeriod(1);
133   }
134
135   return 0;
136 }
137  
138 #else
139
140 nat
141 initialize_virtual_timer(nat ms)
142 {
143 # ifndef HAVE_SETITIMER
144   /*    fprintf(stderr, "No virtual timer on this system\n"); */
145     return -1;
146 # else
147     struct itimerval it;
148
149     timestamp = getourtimeofday();
150     ticks_since_timestamp = 0;
151
152     it.it_value.tv_sec = ms / 1000;
153     it.it_value.tv_usec = 1000 * (ms - (1000 * it.it_value.tv_sec));
154     it.it_interval = it.it_value;
155     return (setitimer(ITIMER_VIRTUAL, &it, NULL));
156 # endif
157 }
158
159 #endif /* !cygwin32_TARGET_OS */
160
161 # if 0
162 /* This is a potential POSIX version */
163 nat
164 initialize_virtual_timer(nat ms)
165 {
166     struct sigevent se;
167     struct itimerspec it;
168     timer_t tid;
169
170     timestamp = getourtimeofday();
171     ticks_since_timestamp = 0;
172
173     se.sigev_notify = SIGEV_SIGNAL;
174     se.sigev_signo = SIGVTALRM;
175     se.sigev_value.sival_int = SIGVTALRM;
176     if (timer_create(CLOCK_VIRTUAL, &se, &tid)) {
177         barf("can't create virtual timer");
178     }
179     it.it_value.tv_sec = ms / 1000;
180     it.it_value.tv_nsec = 1000000 * (ms - 1000 * it.it_value.tv_sec);
181     it.it_interval = it.it_value;
182     timer_settime(tid, TIMER_RELTIME, &it, NULL);
183 }
184 # endif
185
186 #if defined(mingw32_TARGET_OS) || (defined(cygwin32_TARGET_OS) && !defined(HAVE_SETITIMER))
187 int
188 install_vtalrm_handler(void)
189 {
190   vtalrm_cback = handle_tick;
191   return 0;
192 }
193
194 #else
195 int
196 install_vtalrm_handler(void)
197 {
198     struct sigaction action;
199
200     action.sa_handler = handle_tick;
201
202     sigemptyset(&action.sa_mask);
203     action.sa_flags = 0;
204
205     return sigaction(SIGVTALRM, &action, NULL);
206 }
207
208 void
209 block_vtalrm_signal(void)
210 {
211     sigset_t signals;
212     
213     sigemptyset(&signals);
214     sigaddset(&signals, SIGVTALRM);
215
216     (void) sigprocmask(SIG_BLOCK, &signals, NULL);
217 }
218
219 void
220 unblock_vtalrm_signal(void)
221 {
222     sigset_t signals;
223     
224     sigemptyset(&signals);
225     sigaddset(&signals, SIGVTALRM);
226
227     (void) sigprocmask(SIG_UNBLOCK, &signals, NULL);
228 }
229 #endif
230
231 /* gettimeofday() takes around 1us on our 500MHz PIII.  Since we're
232  * only calling it 50 times/s, it shouldn't have any great impact.
233  */
234 #if !defined(mingw32_TARGET_OS)
235 unsigned int 
236 getourtimeofday(void)
237 {
238   struct timeval tv;
239   gettimeofday(&tv, (struct timezone *) NULL);
240   return (tv.tv_sec * TICK_FREQUENCY +
241           tv.tv_usec * TICK_FREQUENCY / 1000000);
242 }
243 #else
244 unsigned int
245 getourtimeofday(void)
246 {
247   return ((unsigned int)GetTickCount() * TICK_FREQUENCY) / 1000;
248 }
249 #endif