new RTS flag: -V to modify the resolution of the RTS timer
[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 "Schedule.h"
26 #include "posix/Select.h"
27
28 /* As recommended in the autoconf manual */
29 # ifdef TIME_WITH_SYS_TIME
30 #  include <sys/time.h>
31 #  include <time.h>
32 # else
33 #  ifdef HAVE_SYS_TIME_H
34 #   include <sys/time.h>
35 #  else
36 #   include <time.h>
37 #  endif
38 # endif
39
40 #ifdef HAVE_SIGNAL_H
41 # include <signal.h>
42 #endif
43
44 /* Major bogosity:
45  * 
46  * In the threaded RTS, we can't set the virtual timer because the
47  * thread which has the virtual timer might be sitting waiting for a
48  * capability, and the virtual timer only ticks in CPU time.
49  *
50  * So, possible solutions:
51  *
52  * (1) tick in realtime.  Not very good, because this ticker is used for
53  *     profiling, and this will give us unreliable time profiling
54  *     results.  Furthermore, this requires picking a single OS thread
55  *     to be the timekeeper, which is a bad idea because the thread in
56  *     question might just be making a temporary call into Haskell land.
57  *
58  * (2) save/restore the virtual timer around excursions into STG land.
59  *     Sounds great, but I tried it and the resolution of the virtual timer
60  *     isn't good enough (on Linux) - most of our excursions fall
61  *     within the timer's resolution and we never make any progress.
62  *   
63  * (3) have a virtual timer in every OS thread.  Might be reasonable,
64  *     because most of the time there is only ever one of these
65  *     threads running, so it approximates a single virtual timer.
66  *     But still quite bogus (and I got crashes when I tried this).
67  *
68  * For now, we're using (1), but this needs a better solution. --SDM
69  */
70 #ifdef THREADED_RTS
71 #define ITIMER_FLAVOUR  ITIMER_REAL
72 #define ITIMER_SIGNAL   SIGALRM
73 #else
74 #define ITIMER_FLAVOUR  ITIMER_VIRTUAL
75 #define ITIMER_SIGNAL   SIGVTALRM
76 #endif
77
78 static
79 int
80 install_vtalrm_handler(TickProc handle_tick)
81 {
82     struct sigaction action;
83
84     action.sa_handler = handle_tick;
85
86     sigemptyset(&action.sa_mask);
87
88 #ifdef SA_RESTART
89     // specify SA_RESTART.  One consequence if we don't do this is
90     // that readline gets confused by the -threaded RTS.  It seems
91     // that if a SIGALRM handler is installed without SA_RESTART,
92     // readline installs its own SIGALRM signal handler (see
93     // readline's signals.c), and this somehow causes readline to go
94     // wrong when the input exceeds a single line (try it).
95     action.sa_flags = SA_RESTART;
96 #else
97     action.sa_flags = 0;
98 #endif
99
100     return sigaction(ITIMER_SIGNAL, &action, NULL);
101 }
102
103 int
104 startTicker(nat ms, TickProc handle_tick)
105 {
106 # ifndef HAVE_SETITIMER
107   /*    debugBelch("No virtual timer on this system\n"); */
108     return -1;
109 # else
110     struct itimerval it;
111
112     install_vtalrm_handler(handle_tick);
113
114 #if !defined(THREADED_RTS)
115     timestamp = getourtimeofday();
116 #endif
117
118     it.it_value.tv_sec = ms / 1000;
119     it.it_value.tv_usec = 1000 * (ms - (1000 * it.it_value.tv_sec));
120     it.it_interval = it.it_value;
121     return (setitimer(ITIMER_FLAVOUR, &it, NULL));
122 # endif
123 }
124
125 int
126 stopTicker()
127 {
128 # ifndef HAVE_SETITIMER
129   /*    debugBelch("No virtual timer on this system\n"); */
130     return -1;
131 # else
132     struct itimerval it;
133   
134     it.it_value.tv_sec = 0;
135     it.it_value.tv_usec = 0;
136     it.it_interval = it.it_value;
137     return (setitimer(ITIMER_FLAVOUR, &it, NULL));
138 # endif
139 }
140
141 # if 0
142 /* This is a potential POSIX version */
143 int
144 startTicker(nat ms)
145 {
146     struct sigevent se;
147     struct itimerspec it;
148     timer_t tid;
149
150 #if !defined(THREADED_RTS)
151     timestamp = getourtimeofday();
152 #endif
153
154     se.sigev_notify = SIGEV_SIGNAL;
155     se.sigev_signo = ITIMER_SIGNAL;
156     se.sigev_value.sival_int = ITIMER_SIGNAL;
157     if (timer_create(CLOCK_VIRTUAL, &se, &tid)) {
158         barf("can't create virtual timer");
159     }
160     it.it_value.tv_sec = ms / 1000;
161     it.it_value.tv_nsec = 1000000 * (ms - 1000 * it.it_value.tv_sec);
162     it.it_interval = it.it_value;
163     return timer_settime(tid, TIMER_RELTIME, &it, NULL);
164 }
165
166 int
167 stopTicker()
168 {
169     struct sigevent se;
170     struct itimerspec it;
171     timer_t tid;
172
173 #if !defined(THREADED_RTS)
174     timestamp = getourtimeofday();
175 #endif
176
177     se.sigev_notify = SIGEV_SIGNAL;
178     se.sigev_signo = ITIMER_SIGNAL;
179     se.sigev_value.sival_int = ITIMER_SIGNAL;
180     if (timer_create(CLOCK_VIRTUAL, &se, &tid)) {
181         barf("can't create virtual timer");
182     }
183     it.it_value.tv_sec = 0;
184     it.it_value.tv_nsec = 0;
185     it.it_interval = it.it_value;
186     return timer_settime(tid, TIMER_RELTIME, &it, NULL);
187 }
188 # endif
189
190 #if 0
191 /* Currently unused */
192 void
193 block_vtalrm_signal(void)
194 {
195     sigset_t signals;
196     
197     sigemptyset(&signals);
198     sigaddset(&signals, ITIMER_SIGNAL);
199
200     (void) sigprocmask(SIG_BLOCK, &signals, NULL);
201 }
202
203 void
204 unblock_vtalrm_signal(void)
205 {
206     sigset_t signals;
207     
208     sigemptyset(&signals);
209     sigaddset(&signals, ITIMER_SIGNAL);
210
211     (void) sigprocmask(SIG_UNBLOCK, &signals, NULL);
212 }
213 #endif
214
215 /* gettimeofday() takes around 1us on our 500MHz PIII.  Since we're
216  * only calling it 50 times/s, it shouldn't have any great impact.
217  */
218 lnat
219 getourtimeofday(void)
220 {
221   struct timeval tv;
222   gettimeofday(&tv, (struct timezone *) NULL);
223         // cast to lnat because nat may be 64 bit when int is only 32 bit
224   return ((lnat)tv.tv_sec * 1000 / RtsFlags.MiscFlags.tickInterval +
225           (lnat)tv.tv_usec / (RtsFlags.MiscFlags.tickInterval * 1000));
226 }