Split GC.c, and move storage manager into sm/ directory
[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.  Furthermore, this requires picking a single OS thread
56  *     to be the timekeeper, which is a bad idea because the thread in
57  *     question might just be making a temporary call into Haskell land.
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 #ifdef THREADED_RTS
72 #define ITIMER_FLAVOUR  ITIMER_REAL
73 #define ITIMER_SIGNAL   SIGALRM
74 #else
75 #define ITIMER_FLAVOUR  ITIMER_VIRTUAL
76 #define ITIMER_SIGNAL   SIGVTALRM
77 #endif
78
79 static
80 int
81 install_vtalrm_handler(TickProc handle_tick)
82 {
83     struct sigaction action;
84
85     action.sa_handler = handle_tick;
86
87     sigemptyset(&action.sa_mask);
88
89 #ifdef SA_RESTART
90     // specify SA_RESTART.  One consequence if we don't do this is
91     // that readline gets confused by the -threaded RTS.  It seems
92     // that if a SIGALRM handler is installed without SA_RESTART,
93     // readline installs its own SIGALRM signal handler (see
94     // readline's signals.c), and this somehow causes readline to go
95     // wrong when the input exceeds a single line (try it).
96     action.sa_flags = SA_RESTART;
97 #else
98     action.sa_flags = 0;
99 #endif
100
101     return sigaction(ITIMER_SIGNAL, &action, NULL);
102 }
103
104 int
105 startTicker(nat ms, TickProc handle_tick)
106 {
107 # ifndef HAVE_SETITIMER
108   /*    debugBelch("No virtual timer on this system\n"); */
109     return -1;
110 # else
111     struct itimerval it;
112
113     install_vtalrm_handler(handle_tick);
114
115 #if !defined(THREADED_RTS)
116     timestamp = getourtimeofday();
117 #endif
118
119     it.it_value.tv_sec = ms / 1000;
120     it.it_value.tv_usec = 1000 * (ms - (1000 * it.it_value.tv_sec));
121     it.it_interval = it.it_value;
122     return (setitimer(ITIMER_FLAVOUR, &it, NULL));
123 # endif
124 }
125
126 int
127 stopTicker()
128 {
129 # ifndef HAVE_SETITIMER
130   /*    debugBelch("No virtual timer on this system\n"); */
131     return -1;
132 # else
133     struct itimerval it;
134   
135     it.it_value.tv_sec = 0;
136     it.it_value.tv_usec = 0;
137     it.it_interval = it.it_value;
138     return (setitimer(ITIMER_FLAVOUR, &it, NULL));
139 # endif
140 }
141
142 # if 0
143 /* This is a potential POSIX version */
144 int
145 startTicker(nat ms)
146 {
147     struct sigevent se;
148     struct itimerspec it;
149     timer_t tid;
150
151 #if !defined(THREADED_RTS)
152     timestamp = getourtimeofday();
153 #endif
154
155     se.sigev_notify = SIGEV_SIGNAL;
156     se.sigev_signo = ITIMER_SIGNAL;
157     se.sigev_value.sival_int = ITIMER_SIGNAL;
158     if (timer_create(CLOCK_VIRTUAL, &se, &tid)) {
159         barf("can't create virtual timer");
160     }
161     it.it_value.tv_sec = ms / 1000;
162     it.it_value.tv_nsec = 1000000 * (ms - 1000 * it.it_value.tv_sec);
163     it.it_interval = it.it_value;
164     return timer_settime(tid, TIMER_RELTIME, &it, NULL);
165 }
166
167 int
168 stopTicker()
169 {
170     struct sigevent se;
171     struct itimerspec it;
172     timer_t tid;
173
174 #if !defined(THREADED_RTS)
175     timestamp = getourtimeofday();
176 #endif
177
178     se.sigev_notify = SIGEV_SIGNAL;
179     se.sigev_signo = ITIMER_SIGNAL;
180     se.sigev_value.sival_int = ITIMER_SIGNAL;
181     if (timer_create(CLOCK_VIRTUAL, &se, &tid)) {
182         barf("can't create virtual timer");
183     }
184     it.it_value.tv_sec = 0;
185     it.it_value.tv_nsec = 0;
186     it.it_interval = it.it_value;
187     return timer_settime(tid, TIMER_RELTIME, &it, NULL);
188 }
189 # endif
190
191 #if 0
192 /* Currently unused */
193 void
194 block_vtalrm_signal(void)
195 {
196     sigset_t signals;
197     
198     sigemptyset(&signals);
199     sigaddset(&signals, ITIMER_SIGNAL);
200
201     (void) sigprocmask(SIG_BLOCK, &signals, NULL);
202 }
203
204 void
205 unblock_vtalrm_signal(void)
206 {
207     sigset_t signals;
208     
209     sigemptyset(&signals);
210     sigaddset(&signals, ITIMER_SIGNAL);
211
212     (void) sigprocmask(SIG_UNBLOCK, &signals, NULL);
213 }
214 #endif
215
216 /* gettimeofday() takes around 1us on our 500MHz PIII.  Since we're
217  * only calling it 50 times/s, it shouldn't have any great impact.
218  */
219 lnat
220 getourtimeofday(void)
221 {
222   struct timeval tv;
223   gettimeofday(&tv, (struct timezone *) NULL);
224         // cast to lnat because nat may be 64 bit when int is only 32 bit
225   return ((lnat)tv.tv_sec * 1000 / RtsFlags.MiscFlags.tickInterval +
226           (lnat)tv.tv_usec / (RtsFlags.MiscFlags.tickInterval * 1000));
227 }