1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2004
5 * General utility functions used in the RTS.
7 * ---------------------------------------------------------------------------*/
9 /* gettimeofday isn't POSIX */
10 /* #include "PosixSource.h" */
26 #ifdef HAVE_GETTIMEOFDAY
39 #if defined(THREADED_RTS) && defined(openbsd_HOST_OS) && defined(HAVE_PTHREAD_H)
43 #if defined(openbsd_HOST_OS) || defined(linux_HOST_OS)
45 #include <sys/types.h>
48 /* no C99 header stdint.h on OpenBSD? */
49 #if defined(openbsd_HOST_OS)
50 typedef unsigned long my_uintptr_t;
53 typedef uintptr_t my_uintptr_t;
61 /* -----------------------------------------------------------------------------
62 Result-checking malloc wrappers.
63 -------------------------------------------------------------------------- */
66 stgMallocBytes (int n, char *msg)
70 if ((space = (char *) malloc((size_t) n)) == NULL) {
71 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
72 MallocFailHook((W_) n, msg); /*msg*/
73 stg_exit(EXIT_INTERNAL_ERROR);
79 stgReallocBytes (void *p, int n, char *msg)
83 if ((space = (char *) realloc(p, (size_t) n)) == NULL) {
84 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
85 MallocFailHook((W_) n, msg); /*msg*/
86 stg_exit(EXIT_INTERNAL_ERROR);
92 stgCallocBytes (int n, int m, char *msg)
96 char* p = stgMallocBytes(sz, msg);
97 for (i = 0; i < sz; i++) p[i] = 0;
101 /* To simplify changing the underlying allocator used
102 * by stgMallocBytes(), provide stgFree() as well.
110 /* -----------------------------------------------------------------------------
113 Not sure if this belongs here.
114 -------------------------------------------------------------------------- */
119 StackOverflowHook(RtsFlags.GcFlags.maxStkSize * sizeof(W_));
121 #if defined(TICKY_TICKY)
122 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
129 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
130 OutOfHeapHook(0/*unknown request size*/,
131 RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
133 #if defined(TICKY_TICKY)
134 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
137 stg_exit(EXIT_HEAPOVERFLOW);
140 /* -----------------------------------------------------------------------------
143 Used in addr2Integer because the C compiler on x86 chokes on
144 strlen, trying to inline it with not enough registers available.
145 -------------------------------------------------------------------------- */
147 nat stg_strlen(char *s)
156 /* -----------------------------------------------------------------------------
157 genSym stuff, used by GHC itself for its splitting unique supply.
159 ToDo: put this somewhere sensible.
160 ------------------------------------------------------------------------- */
162 static I_ __GenSymCounter = 0;
167 return(__GenSymCounter++);
170 resetGenSymZh(void) /* it's your funeral */
173 return(__GenSymCounter);
176 /* -----------------------------------------------------------------------------
177 Get the current time as a string. Used in profiling reports.
178 -------------------------------------------------------------------------- */
180 #if defined(PROFILING) || defined(DEBUG) || defined(PAR) || defined(GRAN)
184 static time_t now = 0;
185 static char nowstr[26];
189 strcpy(nowstr, ctime(&now));
190 strcpy(nowstr+16,nowstr+19);
197 /* -----------------------------------------------------------------------------
198 * Reset a file handle to blocking mode. We do this for the standard
199 * file descriptors before exiting, because the shell doesn't always
201 * -------------------------------------------------------------------------- */
203 #if !defined(mingw32_HOST_OS)
205 resetNonBlockingFd(int fd)
209 /* clear the non-blocking flag on this file descriptor */
210 fd_flags = fcntl(fd, F_GETFL);
211 if (fd_flags & O_NONBLOCK) {
212 fcntl(fd, F_SETFL, fd_flags & ~O_NONBLOCK);
217 setNonBlockingFd(int fd)
221 /* clear the non-blocking flag on this file descriptor */
222 fd_flags = fcntl(fd, F_GETFL);
223 if (!(fd_flags & O_NONBLOCK)) {
224 fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
228 /* Stub defns -- async / non-blocking IO is not done
229 * via O_NONBLOCK and select() under Win32.
231 void resetNonBlockingFd(int fd STG_UNUSED) {}
232 void setNonBlockingFd(int fd STG_UNUSED) {}
236 static ullong startTime = 0;
238 /* used in a parallel setup */
242 # if defined(HAVE_GETCLOCK) && !defined(alpha_HOST_ARCH) && !defined(hppa1_1_HOST_ARCH)
245 if (getclock(TIMEOFDAY, &tv) != 0) {
247 fprintf(stderr, "Clock failed\n");
248 stg_exit(EXIT_FAILURE);
250 return tv.tv_sec * LL(1000) + tv.tv_nsec / LL(1000000) - startTime;
251 # elif HAVE_GETTIMEOFDAY && !defined(alpha_HOST_ARCH)
254 if (gettimeofday(&tv, NULL) != 0) {
256 fprintf(stderr, "Clock failed\n");
257 stg_exit(EXIT_FAILURE);
259 return tv.tv_sec * LL(1000) + tv.tv_usec / LL(1000) - startTime;
262 if ((t = time(NULL)) == (time_t) -1) {
264 fprintf(stderr, "Clock failed\n");
265 stg_exit(EXIT_FAILURE);
267 return t * LL(1000) - startTime;
272 /* -----------------------------------------------------------------------------
273 Print large numbers, with punctuation.
274 -------------------------------------------------------------------------- */
277 ullong_format_string(ullong x, char *s, rtsBool with_commas)
279 if (x < (ullong)1000)
280 sprintf(s, "%lu", (lnat)x);
281 else if (x < (ullong)1000000)
282 sprintf(s, (with_commas) ? "%lu,%3.3lu" : "%lu%3.3lu",
283 (lnat)((x)/(ullong)1000),
284 (lnat)((x)%(ullong)1000));
285 else if (x < (ullong)1000000000)
286 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu",
287 (lnat)((x)/(ullong)1000000),
288 (lnat)((x)/(ullong)1000%(ullong)1000),
289 (lnat)((x)%(ullong)1000));
291 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu%3.3lu",
292 (lnat)((x)/(ullong)1000000000),
293 (lnat)((x)/(ullong)1000000%(ullong)1000),
294 (lnat)((x)/(ullong)1000%(ullong)1000),
295 (lnat)((x)%(ullong)1000));
300 // Can be used as a breakpoint to set on every heap check failure.
303 heapCheckFail( void )
309 * It seems that pthreads and signals interact oddly in OpenBSD & FreeBSD
310 * pthreads (and possibly others). When linking with -lpthreads, we
311 * have to use pthread_kill to send blockable signals. So use that
312 * when we have a threaded rts. So System.Posix.Signals will call
313 * genericRaise(), rather than raise(3).
315 int genericRaise(int sig) {
316 #if defined(THREADED_RTS) && (defined(openbsd_HOST_OS) || defined(freebsd_HOST_OS))
317 return pthread_kill(pthread_self(), sig);
323 /* -----------------------------------------------------------------------------
324 Allocating executable memory
325 -------------------------------------------------------------------------- */
327 /* Heavily arch-specific, I'm afraid.. */
330 * Allocate len bytes which are readable, writable, and executable.
332 * ToDo: If this turns out to be a performance bottleneck, one could
333 * e.g. cache the last VirtualProtect/mprotect-ed region and do
334 * nothing in case of a cache hit.
337 stgMallocBytesRWX(int len)
339 void *addr = stgMallocBytes(len, "mallocBytesRWX");
340 #if defined(i386_HOST_ARCH) && defined(_WIN32)
341 /* This could be necessary for processors which distinguish between READ and
342 EXECUTE memory accesses, e.g. Itaniums. */
343 DWORD dwOldProtect = 0;
344 if (VirtualProtect (addr, len, PAGE_EXECUTE_READWRITE, &dwOldProtect) == 0) {
345 barf("mallocBytesRWX: failed to protect 0x%p; error=%lu; old protection: %lu\n",
346 addr, (unsigned long)GetLastError(), (unsigned long)dwOldProtect);
348 #elif defined(openbsd_HOST_OS) || defined(linux_HOST_OS)
349 /* malloced memory isn't executable by default on OpenBSD */
350 my_uintptr_t pageSize = sysconf(_SC_PAGESIZE);
351 my_uintptr_t mask = ~(pageSize - 1);
352 my_uintptr_t startOfFirstPage = ((my_uintptr_t)addr ) & mask;
353 my_uintptr_t startOfLastPage = ((my_uintptr_t)addr + len - 1) & mask;
354 my_uintptr_t size = startOfLastPage - startOfFirstPage + pageSize;
355 if (mprotect((void*)startOfFirstPage, (size_t)size, PROT_EXEC | PROT_READ | PROT_WRITE) != 0) {
356 barf("mallocBytesRWX: failed to protect 0x%p\n", addr);