1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2004
5 * General utility functions used in the RTS.
7 * ---------------------------------------------------------------------------*/
9 #include "PosixSource.h"
25 #ifdef HAVE_GETTIMEOFDAY
38 #if defined(THREADED_RTS) && defined(openbsd_HOST_OS) && defined(HAVE_PTHREAD_H)
47 /* -----------------------------------------------------------------------------
49 -------------------------------------------------------------------------- */
53 typedef struct Allocated_ {
56 struct Allocated_ *next;
59 static Allocated *allocs = NULL;
62 static Mutex allocator_mutex;
72 initMutex(&allocator_mutex);
74 alloc_size = sizeof(Allocated);
75 if ((a = (Allocated *) malloc(alloc_size)) == NULL) {
76 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
77 MallocFailHook((W_) alloc_size, "initialising debugging allocator");
78 stg_exit(EXIT_INTERNAL_ERROR);
87 shutdownAllocator(void)
90 closeMutex(&allocator_mutex);
94 static void addAllocation(void *addr, size_t len) {
99 barf("addAllocation: allocator debugger not initialised");
101 alloc_size = sizeof(Allocated);
102 if ((a = (Allocated *) malloc(alloc_size)) == NULL) {
103 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
104 MallocFailHook((W_) alloc_size, "creating info for debugging allocator");
105 stg_exit(EXIT_INTERNAL_ERROR);
109 ACQUIRE_LOCK(&allocator_mutex);
110 a->next = allocs->next;
112 RELEASE_LOCK(&allocator_mutex);
115 static void removeAllocation(void *addr) {
118 if (allocs == NULL) {
119 barf("addAllocation: allocator debugger not initialised");
122 barf("Freeing NULL!");
125 ACQUIRE_LOCK(&allocator_mutex);
129 if (a->addr == addr) {
130 prev->next = a->next;
131 memset(addr, 0xaa, a->len);
133 RELEASE_LOCK(&allocator_mutex);
139 barf("Freeing non-allocated memory at %p", addr);
143 /* -----------------------------------------------------------------------------
144 Result-checking malloc wrappers.
145 -------------------------------------------------------------------------- */
148 stgMallocBytes (int n, char *msg)
154 if ((space = (char *) malloc(n2)) == NULL) {
155 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
156 MallocFailHook((W_) n, msg); /*msg*/
157 stg_exit(EXIT_INTERNAL_ERROR);
160 addAllocation(space, n2);
166 stgReallocBytes (void *p, int n, char *msg)
172 if ((space = (char *) realloc(p, (size_t) n2)) == NULL) {
173 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
174 MallocFailHook((W_) n, msg); /*msg*/
175 stg_exit(EXIT_INTERNAL_ERROR);
179 addAllocation(space, n2);
185 stgCallocBytes (int n, int m, char *msg)
189 if ((space = (char *) calloc((size_t) n, (size_t) m)) == NULL) {
190 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
191 MallocFailHook((W_) n*m, msg); /*msg*/
192 stg_exit(EXIT_INTERNAL_ERROR);
195 addAllocation(space, (size_t) n * (size_t) m);
200 /* To simplify changing the underlying allocator used
201 * by stgMallocBytes(), provide stgFree() as well.
212 /* -----------------------------------------------------------------------------
215 Not sure if this belongs here.
216 -------------------------------------------------------------------------- */
221 StackOverflowHook(RtsFlags.GcFlags.maxStkSize * sizeof(W_));
223 #if defined(TICKY_TICKY)
224 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
231 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
232 OutOfHeapHook(0/*unknown request size*/,
233 RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
235 #if defined(TICKY_TICKY)
236 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
239 stg_exit(EXIT_HEAPOVERFLOW);
242 /* -----------------------------------------------------------------------------
245 Used in addr2Integer because the C compiler on x86 chokes on
246 strlen, trying to inline it with not enough registers available.
247 -------------------------------------------------------------------------- */
249 nat stg_strlen(char *s)
258 /* -----------------------------------------------------------------------------
259 genSym stuff, used by GHC itself for its splitting unique supply.
261 ToDo: put this somewhere sensible.
262 ------------------------------------------------------------------------- */
264 static I_ __GenSymCounter = 0;
269 return(__GenSymCounter++);
272 resetGenSymZh(void) /* it's your funeral */
275 return(__GenSymCounter);
278 /* -----------------------------------------------------------------------------
279 Get the current time as a string. Used in profiling reports.
280 -------------------------------------------------------------------------- */
282 #if defined(PROFILING) || defined(DEBUG) || defined(PAR) || defined(GRAN)
286 static time_t now = 0;
287 static char nowstr[26];
292 ctime_r(&now, nowstr);
294 strcpy(nowstr, ctime(&now));
296 memmove(nowstr+16,nowstr+19,7);
297 nowstr[21] = '\0'; // removes the \n
303 /* -----------------------------------------------------------------------------
304 * Reset a file handle to blocking mode. We do this for the standard
305 * file descriptors before exiting, because the shell doesn't always
307 * -------------------------------------------------------------------------- */
309 #if !defined(mingw32_HOST_OS)
311 resetNonBlockingFd(int fd)
315 /* clear the non-blocking flag on this file descriptor */
316 fd_flags = fcntl(fd, F_GETFL);
317 if (fd_flags & O_NONBLOCK) {
318 fcntl(fd, F_SETFL, fd_flags & ~O_NONBLOCK);
323 setNonBlockingFd(int fd)
327 /* clear the non-blocking flag on this file descriptor */
328 fd_flags = fcntl(fd, F_GETFL);
329 if (!(fd_flags & O_NONBLOCK)) {
330 fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
334 /* Stub defns -- async / non-blocking IO is not done
335 * via O_NONBLOCK and select() under Win32.
337 void resetNonBlockingFd(int fd STG_UNUSED) {}
338 void setNonBlockingFd(int fd STG_UNUSED) {}
342 static ullong startTime = 0;
344 /* used in a parallel setup */
348 # if defined(HAVE_GETCLOCK) && !defined(alpha_HOST_ARCH) && !defined(hppa1_1_HOST_ARCH)
351 if (getclock(TIMEOFDAY, &tv) != 0) {
353 fprintf(stderr, "Clock failed\n");
354 stg_exit(EXIT_FAILURE);
356 return tv.tv_sec * LL(1000) + tv.tv_nsec / LL(1000000) - startTime;
357 # elif HAVE_GETTIMEOFDAY && !defined(alpha_HOST_ARCH)
360 if (gettimeofday(&tv, NULL) != 0) {
362 fprintf(stderr, "Clock failed\n");
363 stg_exit(EXIT_FAILURE);
365 return tv.tv_sec * LL(1000) + tv.tv_usec / LL(1000) - startTime;
368 if ((t = time(NULL)) == (time_t) -1) {
370 fprintf(stderr, "Clock failed\n");
371 stg_exit(EXIT_FAILURE);
373 return t * LL(1000) - startTime;
378 /* -----------------------------------------------------------------------------
379 Print large numbers, with punctuation.
380 -------------------------------------------------------------------------- */
383 ullong_format_string(ullong x, char *s, rtsBool with_commas)
385 if (x < (ullong)1000)
386 sprintf(s, "%lu", (lnat)x);
387 else if (x < (ullong)1000000)
388 sprintf(s, (with_commas) ? "%lu,%3.3lu" : "%lu%3.3lu",
389 (lnat)((x)/(ullong)1000),
390 (lnat)((x)%(ullong)1000));
391 else if (x < (ullong)1000000000)
392 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu",
393 (lnat)((x)/(ullong)1000000),
394 (lnat)((x)/(ullong)1000%(ullong)1000),
395 (lnat)((x)%(ullong)1000));
397 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu%3.3lu",
398 (lnat)((x)/(ullong)1000000000),
399 (lnat)((x)/(ullong)1000000%(ullong)1000),
400 (lnat)((x)/(ullong)1000%(ullong)1000),
401 (lnat)((x)%(ullong)1000));
406 // Can be used as a breakpoint to set on every heap check failure.
409 heapCheckFail( void )
415 * It seems that pthreads and signals interact oddly in OpenBSD & FreeBSD
416 * pthreads (and possibly others). When linking with -lpthreads, we
417 * have to use pthread_kill to send blockable signals. So use that
418 * when we have a threaded rts. So System.Posix.Signals will call
419 * genericRaise(), rather than raise(3).
421 int genericRaise(int sig) {
422 #if defined(THREADED_RTS) && (defined(openbsd_HOST_OS) || defined(freebsd_HOST_OS))
423 return pthread_kill(pthread_self(), sig);