Don't Terminate the ticker thread (#3748)
[ghc.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
20 #include "PosixSource.h"
21 #include "Rts.h"
22
23 #include "Ticker.h"
24 #include "Itimer.h"
25 #include "Proftimer.h"
26 #include "Schedule.h"
27 #include "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 # if defined(THREADED_RTS) || !defined(HAVE_SETITIMER_VIRTUAL)
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 // We also use it if we don't have a virtual timer (trac #2883).
87 # define ITIMER_SIGNAL SIGALRM
88 # define ITIMER_FLAVOUR ITIMER_REAL
89 # else
90 # define ITIMER_SIGNAL SIGVTALRM
91 # define ITIMER_FLAVOUR ITIMER_VIRTUAL
92 # endif
93
94 #else
95
96 # error No way to set an interval timer.
97
98 #endif
99
100 #if defined(USE_TIMER_CREATE)
101 static timer_t timer;
102 #endif
103
104 static nat itimer_interval = 50;
105
106 static
107 void
108 install_vtalrm_handler(TickProc handle_tick)
109 {
110 struct sigaction action;
111
112 action.sa_handler = handle_tick;
113
114 sigemptyset(&action.sa_mask);
115
116 #ifdef SA_RESTART
117 // specify SA_RESTART. One consequence if we don't do this is
118 // that readline gets confused by the -threaded RTS. It seems
119 // that if a SIGALRM handler is installed without SA_RESTART,
120 // readline installs its own SIGALRM signal handler (see
121 // readline's signals.c), and this somehow causes readline to go
122 // wrong when the input exceeds a single line (try it).
123 action.sa_flags = SA_RESTART;
124 #else
125 action.sa_flags = 0;
126 #endif
127
128 if (sigaction(ITIMER_SIGNAL, &action, NULL) == -1) {
129 sysErrorBelch("sigaction");
130 stg_exit(EXIT_FAILURE);
131 }
132 }
133
134 void
135 initTicker (nat ms, TickProc handle_tick)
136 {
137 install_vtalrm_handler(handle_tick);
138
139 #if !defined(THREADED_RTS)
140 timestamp = getourtimeofday();
141 #endif
142
143 itimer_interval = ms;
144
145 #if defined(USE_TIMER_CREATE)
146 {
147 struct sigevent ev;
148
149 // Keep programs like valgrind happy
150 memset(&ev, 0, sizeof(ev));
151
152 ev.sigev_notify = SIGEV_SIGNAL;
153 ev.sigev_signo = ITIMER_SIGNAL;
154
155 if (timer_create(TIMER_FLAVOUR, &ev, &timer) != 0) {
156 sysErrorBelch("timer_create");
157 stg_exit(EXIT_FAILURE);
158 }
159 }
160 #endif
161 }
162
163 void
164 startTicker(void)
165 {
166 #if defined(USE_TIMER_CREATE)
167 {
168 struct itimerspec it;
169
170 it.it_value.tv_sec = itimer_interval / 1000;
171 it.it_value.tv_nsec = (itimer_interval % 1000) * 1000000;
172 it.it_interval = it.it_value;
173
174 if (timer_settime(timer, 0, &it, NULL) != 0) {
175 sysErrorBelch("timer_settime");
176 stg_exit(EXIT_FAILURE);
177 }
178 }
179 #else
180 {
181 struct itimerval it;
182
183 it.it_value.tv_sec = itimer_interval / 1000;
184 it.it_value.tv_usec = (itimer_interval % 1000) * 1000;
185 it.it_interval = it.it_value;
186
187 if (setitimer(ITIMER_FLAVOUR, &it, NULL) != 0) {
188 sysErrorBelch("setitimer");
189 stg_exit(EXIT_FAILURE);
190 }
191 }
192 #endif
193 }
194
195 void
196 stopTicker(void)
197 {
198 #if defined(USE_TIMER_CREATE)
199 struct itimerspec it;
200
201 it.it_value.tv_sec = 0;
202 it.it_value.tv_nsec = 0;
203 it.it_interval = it.it_value;
204
205 if (timer_settime(timer, 0, &it, NULL) != 0) {
206 sysErrorBelch("timer_settime");
207 stg_exit(EXIT_FAILURE);
208 }
209 #else
210 struct itimerval it;
211
212 it.it_value.tv_sec = 0;
213 it.it_value.tv_usec = 0;
214 it.it_interval = it.it_value;
215
216 if (setitimer(ITIMER_FLAVOUR, &it, NULL) != 0) {
217 sysErrorBelch("setitimer");
218 stg_exit(EXIT_FAILURE);
219 }
220 #endif
221 }
222
223 void
224 exitTicker (rtsBool wait STG_UNUSED)
225 {
226 #if defined(USE_TIMER_CREATE)
227 timer_delete(timer);
228 // ignore errors - we don't really care if it fails.
229 #endif
230 }
231
232 /* gettimeofday() takes around 1us on our 500MHz PIII. Since we're
233 * only calling it 50 times/s, it shouldn't have any great impact.
234 */
235 lnat
236 getourtimeofday(void)
237 {
238 struct timeval tv;
239 nat interval;
240 interval = RtsFlags.MiscFlags.tickInterval;
241 if (interval == 0) { interval = 50; }
242 gettimeofday(&tv, (struct timezone *) NULL);
243
244 // Avoid overflow when we multiply seconds by 1000. See #2848
245 return (lnat)((StgWord64)tv.tv_sec * 1000 / interval +
246 (StgWord64)tv.tv_usec / (interval * 1000));
247 }