51e08f82803a2f84e9b0a610174571557bd9f8b3
[ghc-hetmet.git] / rts / posix / Itimer.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1995-1999
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 /* Major bogosity:
46  * 
47  * In the threaded RTS, we can't set the virtual timer because the
48  * thread which has the virtual timer might be sitting waiting for a
49  * capability, and the virtual timer only ticks in CPU time.
50  *
51  * So, possible solutions:
52  *
53  * (1) tick in realtime.  Not very good, because this ticker is used for
54  *     profiling, and this will give us unreliable time profiling
55  *     results.
56  *
57  * (2) save/restore the virtual timer around excursions into STG land.
58  *     Sounds great, but I tried it and the resolution of the virtual timer
59  *     isn't good enough (on Linux) - most of our excursions fall
60  *     within the timer's resolution and we never make any progress.
61  *   
62  * (3) have a virtual timer in every OS thread.  Might be reasonable,
63  *     because most of the time there is only ever one of these
64  *     threads running, so it approximates a single virtual timer.
65  *     But still quite bogus (and I got crashes when I tried this).
66  *
67  * For now, we're using (1), but this needs a better solution. --SDM
68  */
69
70 #if defined(USE_TIMER_CREATE)
71
72 #  define ITIMER_SIGNAL SIGVTALRM
73 #  ifdef THREADED_RTS
74 #    define TIMER_FLAVOUR CLOCK_REALTIME
75 #  else
76 #    define TIMER_FLAVOUR CLOCK_PROCESS_CPUTIME_ID
77 #  endif
78
79 #elif defined(HAVE_SETITIMER)
80
81 #  ifdef THREADED_RTS
82 //   Oh dear, we have to use SIGALRM if there's no timer_create and
83 //   we're using the THREADED_RTS.  This leads to problems, see bug #850.
84 #    define ITIMER_SIGNAL  SIGALRM
85 #    define ITIMER_FLAVOUR ITIMER_REAL
86 #  else
87 #    define ITIMER_SIGNAL  SIGVTALRM
88 #    define ITIMER_FLAVOUR ITIMER_VIRTUAL
89 #  endif
90
91 #else
92
93 #  error No way to set an interval timer.
94
95 #endif
96
97 #if defined(USE_TIMER_CREATE)
98 timer_t timer;
99 #endif
100
101 static
102 void
103 install_vtalrm_handler(TickProc handle_tick)
104 {
105     struct sigaction action;
106
107     action.sa_handler = handle_tick;
108
109     sigemptyset(&action.sa_mask);
110
111 #ifdef SA_RESTART
112     // specify SA_RESTART.  One consequence if we don't do this is
113     // that readline gets confused by the -threaded RTS.  It seems
114     // that if a SIGALRM handler is installed without SA_RESTART,
115     // readline installs its own SIGALRM signal handler (see
116     // readline's signals.c), and this somehow causes readline to go
117     // wrong when the input exceeds a single line (try it).
118     action.sa_flags = SA_RESTART;
119 #else
120     action.sa_flags = 0;
121 #endif
122
123     if (sigaction(ITIMER_SIGNAL, &action, NULL) == -1) {
124         sysErrorBelch("sigaction");
125         stg_exit(EXIT_FAILURE);
126     }
127 }
128
129 void
130 initTicker (TickProc handle_tick)
131 {
132     install_vtalrm_handler(handle_tick);
133
134 #if !defined(THREADED_RTS)
135     timestamp = getourtimeofday();
136 #endif
137
138 #if defined(USE_TIMER_CREATE)
139     {
140         struct sigevent ev;
141
142         ev.sigev_notify = SIGEV_SIGNAL;
143         ev.sigev_signo  = ITIMER_SIGNAL;
144
145         if (timer_create(TIMER_FLAVOUR, &ev, &timer) != 0) {
146             sysErrorBelch("timer_create");
147             stg_exit(EXIT_FAILURE);
148         }
149     }
150 #endif
151 }
152
153 void
154 startTicker(nat ms)
155 {
156 #if defined(USE_TIMER_CREATE)
157     {
158         struct itimerspec it;
159         
160         it.it_value.tv_sec = ms / 1000;
161         it.it_value.tv_nsec = (ms % 1000) * 1000000;
162         it.it_interval = it.it_value;
163         
164         if (timer_settime(timer, 0, &it, NULL) != 0) {
165             sysErrorBelch("timer_settime");
166             stg_exit(EXIT_FAILURE);
167         }
168     }
169 #else
170     {
171         struct itimerval it;
172
173         it.it_value.tv_sec = ms / 1000;
174         it.it_value.tv_usec = (ms % 1000) * 1000;
175         it.it_interval = it.it_value;
176         
177         if (setitimer(ITIMER_FLAVOUR, &it, NULL) != 0) {
178             sysErrorBelch("setitimer");
179             stg_exit(EXIT_FAILURE);
180         }
181     }
182 #endif
183 }
184
185 void
186 stopTicker(void)
187 {
188 #if defined(USE_TIMER_CREATE)
189     struct itimerspec it;
190
191     it.it_value.tv_sec = 0;
192     it.it_value.tv_nsec = 0;
193     it.it_interval = it.it_value;
194
195     if (timer_settime(timer, 0, &it, NULL) != 0) {
196         sysErrorBelch("timer_settime");
197         stg_exit(EXIT_FAILURE);
198     }
199 #else
200     struct itimerval it;
201
202     it.it_value.tv_sec = 0;
203     it.it_value.tv_usec = 0;
204     it.it_interval = it.it_value;
205
206     if (setitimer(ITIMER_FLAVOUR, &it, NULL) != 0) {
207         sysErrorBelch("setitimer");
208         stg_exit(EXIT_FAILURE);
209     }
210 #endif
211 }
212
213 void
214 exitTicker(void)
215 {
216     timer_delete(timer);
217     // ignore errors - we don't really care if it fails.
218 }
219
220 #if 0
221 /* Currently unused */
222 void
223 block_vtalrm_signal(void)
224 {
225     sigset_t signals;
226     
227     sigemptyset(&signals);
228     sigaddset(&signals, ITIMER_SIGNAL);
229
230     (void) sigprocmask(SIG_BLOCK, &signals, NULL);
231 }
232
233 void
234 unblock_vtalrm_signal(void)
235 {
236     sigset_t signals;
237     
238     sigemptyset(&signals);
239     sigaddset(&signals, ITIMER_SIGNAL);
240
241     (void) sigprocmask(SIG_UNBLOCK, &signals, NULL);
242 }
243 #endif
244
245 /* gettimeofday() takes around 1us on our 500MHz PIII.  Since we're
246  * only calling it 50 times/s, it shouldn't have any great impact.
247  */
248 lnat
249 getourtimeofday(void)
250 {
251   struct timeval tv;
252   nat interval;
253   interval = RtsFlags.MiscFlags.tickInterval;
254   if (interval == 0) { interval = 50; }
255   gettimeofday(&tv, (struct timezone *) NULL);
256         // cast to lnat because nat may be 64 bit when int is only 32 bit
257   return ((lnat)tv.tv_sec * 1000 / interval +
258           (lnat)tv.tv_usec / (interval * 1000));
259 }