rts/posix/Itimer.c: Handle EINTR when reading timerfd
[ghc.git] / rts / RtsUtils.c
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1998-2004
4 *
5 * General utility functions used in the RTS.
6 *
7 * ---------------------------------------------------------------------------*/
8
9 #include "PosixSource.h"
10 #include "Rts.h"
11 #include "RtsAPI.h"
12
13 #include "RtsUtils.h"
14 #include "Ticky.h"
15 #include "Schedule.h"
16 #include "RtsFlags.h"
17
18 #ifdef HAVE_TIME_H
19 #include <time.h>
20 #endif
21
22 /* HACK: On Mac OS X 10.4 (at least), time.h doesn't declare ctime_r with
23 * _POSIX_C_SOURCE. If this is the case, we declare it ourselves.
24 */
25 #if HAVE_CTIME_R && !HAVE_DECL_CTIME_R
26 extern char *ctime_r(const time_t *, char *);
27 #endif
28
29 #ifdef HAVE_FCNTL_H
30 #include <fcntl.h>
31 #endif
32
33 #ifdef HAVE_GETTIMEOFDAY
34 #include <sys/time.h>
35 #endif
36
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41
42 #ifdef HAVE_SIGNAL_H
43 #include <signal.h>
44 #endif
45
46 #if defined(THREADED_RTS) && defined(openbsd_HOST_OS) && defined(HAVE_PTHREAD_H)
47 #include <pthread.h>
48 #endif
49
50
51 #if defined(_WIN32)
52 #include <windows.h>
53 #endif
54
55 /* -----------------------------------------------------------------------------
56 Result-checking malloc wrappers.
57 -------------------------------------------------------------------------- */
58
59 void *
60 stgMallocBytes (int n, char *msg)
61 {
62 char *space;
63 size_t n2;
64
65 n2 = (size_t) n;
66 if ((space = (char *) malloc(n2)) == NULL) {
67 /* Quoting POSIX.1-2008 (which says more or less the same as ISO C99):
68 *
69 * "Upon successful completion with size not equal to 0, malloc() shall
70 * return a pointer to the allocated space. If size is 0, either a null
71 * pointer or a unique pointer that can be successfully passed to free()
72 * shall be returned. Otherwise, it shall return a null pointer and set
73 * errno to indicate the error."
74 *
75 * Consequently, a NULL pointer being returned by `malloc()` for a 0-size
76 * allocation is *not* to be considered an error.
77 */
78 if (n == 0) return NULL;
79
80 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
81 rtsConfig.mallocFailHook((W_) n, msg); /*msg*/
82 stg_exit(EXIT_INTERNAL_ERROR);
83 }
84 return space;
85 }
86
87 void *
88 stgReallocBytes (void *p, int n, char *msg)
89 {
90 char *space;
91 size_t n2;
92
93 n2 = (size_t) n;
94 if ((space = (char *) realloc(p, (size_t) n2)) == NULL) {
95 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
96 rtsConfig.mallocFailHook((W_) n, msg); /*msg*/
97 stg_exit(EXIT_INTERNAL_ERROR);
98 }
99 return space;
100 }
101
102 void *
103 stgCallocBytes (int n, int m, char *msg)
104 {
105 char *space;
106
107 if ((space = (char *) calloc((size_t) n, (size_t) m)) == NULL) {
108 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
109 rtsConfig.mallocFailHook((W_) n*m, msg); /*msg*/
110 stg_exit(EXIT_INTERNAL_ERROR);
111 }
112 return space;
113 }
114
115 /* To simplify changing the underlying allocator used
116 * by stgMallocBytes(), provide stgFree() as well.
117 */
118 void
119 stgFree(void* p)
120 {
121 free(p);
122 }
123
124 /* -----------------------------------------------------------------------------
125 Stack overflow
126
127 Not sure if this belongs here.
128 -------------------------------------------------------------------------- */
129
130 void
131 stackOverflow(StgTSO* tso)
132 {
133 rtsConfig.stackOverflowHook(tso->tot_stack_size * sizeof(W_));
134
135 #if defined(TICKY_TICKY)
136 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
137 #endif
138 }
139
140 void
141 heapOverflow(void)
142 {
143 if (!heap_overflow)
144 {
145 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
146 rtsConfig.outOfHeapHook(0/*unknown request size*/,
147 (W_)RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
148
149 heap_overflow = rtsTrue;
150 }
151 }
152
153 /* -----------------------------------------------------------------------------
154 Get the current time as a string. Used in profiling reports.
155 -------------------------------------------------------------------------- */
156
157 char *
158 time_str(void)
159 {
160 static time_t now = 0;
161 static char nowstr[26];
162
163 if (now == 0) {
164 time(&now);
165 #if HAVE_CTIME_R
166 ctime_r(&now, nowstr);
167 #else
168 strcpy(nowstr, ctime(&now));
169 #endif
170 memmove(nowstr+16,nowstr+19,7);
171 nowstr[21] = '\0'; // removes the \n
172 }
173 return nowstr;
174 }
175
176 /* -----------------------------------------------------------------------------
177 Print large numbers, with punctuation.
178 -------------------------------------------------------------------------- */
179
180 char *
181 showStgWord64(StgWord64 x, char *s, rtsBool with_commas)
182 {
183 if (with_commas) {
184 if (x < (StgWord64)1e3)
185 sprintf(s, "%" FMT_Word64, (StgWord64)x);
186 else if (x < (StgWord64)1e6)
187 sprintf(s, "%" FMT_Word64 ",%03" FMT_Word64,
188 (StgWord64)(x / 1000),
189 (StgWord64)(x % 1000));
190 else if (x < (StgWord64)1e9)
191 sprintf(s, "%" FMT_Word64
192 ",%03" FMT_Word64
193 ",%03" FMT_Word64,
194 (StgWord64)(x / 1e6),
195 (StgWord64)((x / 1000) % 1000),
196 (StgWord64)(x % 1000));
197 else if (x < (StgWord64)1e12)
198 sprintf(s, "%" FMT_Word64
199 ",%03" FMT_Word64
200 ",%03" FMT_Word64
201 ",%03" FMT_Word64,
202 (StgWord64)(x / (StgWord64)1e9),
203 (StgWord64)((x / (StgWord64)1e6) % 1000),
204 (StgWord64)((x / (StgWord64)1e3) % 1000),
205 (StgWord64)(x % 1000));
206 else if (x < (StgWord64)1e15)
207 sprintf(s, "%" FMT_Word64
208 ",%03" FMT_Word64
209 ",%03" FMT_Word64
210 ",%03" FMT_Word64
211 ",%03" FMT_Word64,
212 (StgWord64)(x / (StgWord64)1e12),
213 (StgWord64)((x / (StgWord64)1e9) % 1000),
214 (StgWord64)((x / (StgWord64)1e6) % 1000),
215 (StgWord64)((x / (StgWord64)1e3) % 1000),
216 (StgWord64)(x % 1000));
217 else if (x < (StgWord64)1e18)
218 sprintf(s, "%" FMT_Word64
219 ",%03" FMT_Word64
220 ",%03" FMT_Word64
221 ",%03" FMT_Word64
222 ",%03" FMT_Word64
223 ",%03" FMT_Word64,
224 (StgWord64)(x / (StgWord64)1e15),
225 (StgWord64)((x / (StgWord64)1e12) % 1000),
226 (StgWord64)((x / (StgWord64)1e9) % 1000),
227 (StgWord64)((x / (StgWord64)1e6) % 1000),
228 (StgWord64)((x / (StgWord64)1e3) % 1000),
229 (StgWord64)(x % 1000));
230 else
231 sprintf(s, "%" FMT_Word64
232 ",%03" FMT_Word64
233 ",%03" FMT_Word64
234 ",%03" FMT_Word64
235 ",%03" FMT_Word64
236 ",%03" FMT_Word64
237 ",%03" FMT_Word64,
238 (StgWord64)(x / (StgWord64)1e18),
239 (StgWord64)((x / (StgWord64)1e15) % 1000),
240 (StgWord64)((x / (StgWord64)1e12) % 1000),
241 (StgWord64)((x / (StgWord64)1e9) % 1000),
242 (StgWord64)((x / (StgWord64)1e6) % 1000),
243 (StgWord64)((x / (StgWord64)1e3) % 1000),
244 (StgWord64)(x % 1000));
245 }
246 else {
247 sprintf(s, "%" FMT_Word64, x);
248 }
249 return s;
250 }
251
252
253 // Can be used as a breakpoint to set on every heap check failure.
254 #ifdef DEBUG
255 void
256 heapCheckFail( void )
257 {
258 }
259 #endif
260
261 /*
262 * It seems that pthreads and signals interact oddly in OpenBSD & FreeBSD
263 * pthreads (and possibly others). When linking with -lpthreads, we
264 * have to use pthread_kill to send blockable signals. So use that
265 * when we have a threaded rts. So System.Posix.Signals will call
266 * genericRaise(), rather than raise(3).
267 */
268 int genericRaise(int sig) {
269 #if defined(THREADED_RTS) && (defined(openbsd_HOST_OS) || defined(freebsd_HOST_OS) || defined(dragonfly_HOST_OS) || defined(netbsd_HOST_OS) || defined(darwin_HOST_OS))
270 return pthread_kill(pthread_self(), sig);
271 #else
272 return raise(sig);
273 #endif
274 }
275
276 static void mkRtsInfoPair(char *key, char *val) {
277 /* XXX should check for "s, \s etc in key and val */
278 printf(" ,(\"%s\", \"%s\")\n", key, val);
279 }
280
281 /* This little bit of magic allows us to say TOSTRING(SYM) and get
282 * "5" if SYM is 5 */
283 #define TOSTRING2(x) #x
284 #define TOSTRING(x) TOSTRING2(x)
285
286 void printRtsInfo(void) {
287 /* The first entry is just a hack to make it easy to get the
288 * commas right */
289 printf(" [(\"GHC RTS\", \"YES\")\n");
290 mkRtsInfoPair("GHC version", ProjectVersion);
291 mkRtsInfoPair("RTS way", RtsWay);
292 mkRtsInfoPair("Build platform", BuildPlatform);
293 mkRtsInfoPair("Build architecture", BuildArch);
294 mkRtsInfoPair("Build OS", BuildOS);
295 mkRtsInfoPair("Build vendor", BuildVendor);
296 mkRtsInfoPair("Host platform", HostPlatform);
297 mkRtsInfoPair("Host architecture", HostArch);
298 mkRtsInfoPair("Host OS", HostOS);
299 mkRtsInfoPair("Host vendor", HostVendor);
300 mkRtsInfoPair("Target platform", TargetPlatform);
301 mkRtsInfoPair("Target architecture", TargetArch);
302 mkRtsInfoPair("Target OS", TargetOS);
303 mkRtsInfoPair("Target vendor", TargetVendor);
304 mkRtsInfoPair("Word size", TOSTRING(WORD_SIZE_IN_BITS));
305 mkRtsInfoPair("Compiler unregisterised", GhcUnregisterised);
306 mkRtsInfoPair("Tables next to code", GhcEnableTablesNextToCode);
307 printf(" ]\n");
308 }
309
310 // Provides a way for Haskell programs to tell whether they're being
311 // profiled or not. GHCi uses it (see #2197).
312 int rts_isProfiled(void)
313 {
314 #ifdef PROFILING
315 return 1;
316 #else
317 return 0;
318 #endif
319 }
320
321 // Provides a way for Haskell programs to tell whether they're
322 // dynamically-linked or not.
323 int rts_isDynamic(void)
324 {
325 #ifdef DYNAMIC
326 return 1;
327 #else
328 return 0;
329 #endif
330 }
331
332 // Used for detecting a non-empty FPU stack on x86 (see #4914)
333 void checkFPUStack(void)
334 {
335 #ifdef i386_HOST_ARCH
336 static unsigned char buf[108];
337 asm("FSAVE %0":"=m" (buf));
338
339 if(buf[8]!=255 || buf[9]!=255) {
340 errorBelch("NONEMPTY FPU Stack, TAG = %x %x\n",buf[8],buf[9]);
341 abort();
342 }
343 #endif
344 }
345