Ensure runhaskell is rebuild in stage2
[ghc-hetmet.git] / rts / posix / Itimer.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1995-2007
4  *
5  * Interval timer for profiling and pre-emptive scheduling.
6  *
7  * ---------------------------------------------------------------------------*/
8
9 /*
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@.
15  * 
16  * Hence, we use the old-fashioned @setitimer@ that just about everyone seems
17  * to support.  So much for standards.
18  */
19 #include "Rts.h"
20 #include "RtsFlags.h"
21 #include "Timer.h"
22 #include "Ticker.h"
23 #include "posix/Itimer.h"
24 #include "Proftimer.h"
25 #include "Storage.h"
26 #include "Schedule.h"
27 #include "posix/Select.h"
28
29 /* As recommended in the autoconf manual */
30 # ifdef TIME_WITH_SYS_TIME
31 #  include <sys/time.h>
32 #  include <time.h>
33 # else
34 #  ifdef HAVE_SYS_TIME_H
35 #   include <sys/time.h>
36 #  else
37 #   include <time.h>
38 #  endif
39 # endif
40
41 #ifdef HAVE_SIGNAL_H
42 # include <signal.h>
43 #endif
44
45 #include <string.h>
46
47 /* Major bogosity:
48  * 
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.
52  *
53  * So, possible solutions:
54  *
55  * (1) tick in realtime.  Not very good, because this ticker is used for
56  *     profiling, and this will give us unreliable time profiling
57  *     results.
58  *
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.
63  *   
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).
68  *
69  * For now, we're using (1), but this needs a better solution. --SDM
70  */
71
72 #if defined(USE_TIMER_CREATE)
73
74 #  define ITIMER_SIGNAL SIGVTALRM
75 #  ifdef THREADED_RTS
76 #    define TIMER_FLAVOUR CLOCK_REALTIME
77 #  else
78 #    define TIMER_FLAVOUR CLOCK_PROCESS_CPUTIME_ID
79 #  endif
80
81 #elif defined(HAVE_SETITIMER)
82
83 #  ifdef THREADED_RTS
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 #    define ITIMER_SIGNAL  SIGALRM
87 #    define ITIMER_FLAVOUR ITIMER_REAL
88 #  else
89 #    define ITIMER_SIGNAL  SIGVTALRM
90 #    define ITIMER_FLAVOUR ITIMER_VIRTUAL
91 #  endif
92
93 #else
94
95 #  error No way to set an interval timer.
96
97 #endif
98
99 #if defined(USE_TIMER_CREATE)
100 static timer_t timer;
101 #endif
102
103 static nat itimer_interval = 50;
104
105 static
106 void
107 install_vtalrm_handler(TickProc handle_tick)
108 {
109     struct sigaction action;
110
111     action.sa_handler = handle_tick;
112
113     sigemptyset(&action.sa_mask);
114
115 #ifdef SA_RESTART
116     // specify SA_RESTART.  One consequence if we don't do this is
117     // that readline gets confused by the -threaded RTS.  It seems
118     // that if a SIGALRM handler is installed without SA_RESTART,
119     // readline installs its own SIGALRM signal handler (see
120     // readline's signals.c), and this somehow causes readline to go
121     // wrong when the input exceeds a single line (try it).
122     action.sa_flags = SA_RESTART;
123 #else
124     action.sa_flags = 0;
125 #endif
126
127     if (sigaction(ITIMER_SIGNAL, &action, NULL) == -1) {
128         sysErrorBelch("sigaction");
129         stg_exit(EXIT_FAILURE);
130     }
131 }
132
133 void
134 initTicker (nat ms, TickProc handle_tick)
135 {
136     install_vtalrm_handler(handle_tick);
137
138 #if !defined(THREADED_RTS)
139     timestamp = getourtimeofday();
140 #endif
141
142     itimer_interval = ms;
143
144 #if defined(USE_TIMER_CREATE)
145     {
146         struct sigevent ev;
147
148         // Keep programs like valgrind happy
149         memset(&ev, 0, sizeof(ev));
150
151         ev.sigev_notify = SIGEV_SIGNAL;
152         ev.sigev_signo  = ITIMER_SIGNAL;
153
154         if (timer_create(TIMER_FLAVOUR, &ev, &timer) != 0) {
155             sysErrorBelch("timer_create");
156             stg_exit(EXIT_FAILURE);
157         }
158     }
159 #endif
160 }
161
162 void
163 startTicker(void)
164 {
165 #if defined(USE_TIMER_CREATE)
166     {
167         struct itimerspec it;
168         
169         it.it_value.tv_sec = itimer_interval / 1000;
170         it.it_value.tv_nsec = (itimer_interval % 1000) * 1000000;
171         it.it_interval = it.it_value;
172         
173         if (timer_settime(timer, 0, &it, NULL) != 0) {
174             sysErrorBelch("timer_settime");
175             stg_exit(EXIT_FAILURE);
176         }
177     }
178 #else
179     {
180         struct itimerval it;
181
182         it.it_value.tv_sec = itimer_interval / 1000;
183         it.it_value.tv_usec = (itimer_interval % 1000) * 1000;
184         it.it_interval = it.it_value;
185         
186         if (setitimer(ITIMER_FLAVOUR, &it, NULL) != 0) {
187             sysErrorBelch("setitimer");
188             stg_exit(EXIT_FAILURE);
189         }
190     }
191 #endif
192 }
193
194 void
195 stopTicker(void)
196 {
197 #if defined(USE_TIMER_CREATE)
198     struct itimerspec it;
199
200     it.it_value.tv_sec = 0;
201     it.it_value.tv_nsec = 0;
202     it.it_interval = it.it_value;
203
204     if (timer_settime(timer, 0, &it, NULL) != 0) {
205         sysErrorBelch("timer_settime");
206         stg_exit(EXIT_FAILURE);
207     }
208 #else
209     struct itimerval it;
210
211     it.it_value.tv_sec = 0;
212     it.it_value.tv_usec = 0;
213     it.it_interval = it.it_value;
214
215     if (setitimer(ITIMER_FLAVOUR, &it, NULL) != 0) {
216         sysErrorBelch("setitimer");
217         stg_exit(EXIT_FAILURE);
218     }
219 #endif
220 }
221
222 void
223 exitTicker(void)
224 {
225 #if defined(USE_TIMER_CREATE)
226     timer_delete(timer);
227     // ignore errors - we don't really care if it fails.
228 #endif
229 }
230
231 #if 0
232 /* Currently unused */
233 void
234 block_vtalrm_signal(void)
235 {
236     sigset_t signals;
237     
238     sigemptyset(&signals);
239     sigaddset(&signals, ITIMER_SIGNAL);
240
241     (void) sigprocmask(SIG_BLOCK, &signals, NULL);
242 }
243
244 void
245 unblock_vtalrm_signal(void)
246 {
247     sigset_t signals;
248     
249     sigemptyset(&signals);
250     sigaddset(&signals, ITIMER_SIGNAL);
251
252     (void) sigprocmask(SIG_UNBLOCK, &signals, NULL);
253 }
254 #endif
255
256 /* gettimeofday() takes around 1us on our 500MHz PIII.  Since we're
257  * only calling it 50 times/s, it shouldn't have any great impact.
258  */
259 lnat
260 getourtimeofday(void)
261 {
262   struct timeval tv;
263   nat interval;
264   interval = RtsFlags.MiscFlags.tickInterval;
265   if (interval == 0) { interval = 50; }
266   gettimeofday(&tv, (struct timezone *) NULL);
267         // cast to lnat because nat may be 64 bit when int is only 32 bit
268   return ((lnat)tv.tv_sec * 1000 / interval +
269           (lnat)tv.tv_usec / (interval * 1000));
270 }