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 if ((space = (char *) calloc((size_t) n, (size_t) m)) == NULL) {
97 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
98 MallocFailHook((W_) n*m, msg); /*msg*/
99 stg_exit(EXIT_INTERNAL_ERROR);
104 /* To simplify changing the underlying allocator used
105 * by stgMallocBytes(), provide stgFree() as well.
113 /* -----------------------------------------------------------------------------
116 Not sure if this belongs here.
117 -------------------------------------------------------------------------- */
122 StackOverflowHook(RtsFlags.GcFlags.maxStkSize * sizeof(W_));
124 #if defined(TICKY_TICKY)
125 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
132 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
133 OutOfHeapHook(0/*unknown request size*/,
134 RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
136 #if defined(TICKY_TICKY)
137 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
140 stg_exit(EXIT_HEAPOVERFLOW);
143 /* -----------------------------------------------------------------------------
146 Used in addr2Integer because the C compiler on x86 chokes on
147 strlen, trying to inline it with not enough registers available.
148 -------------------------------------------------------------------------- */
150 nat stg_strlen(char *s)
159 /* -----------------------------------------------------------------------------
160 genSym stuff, used by GHC itself for its splitting unique supply.
162 ToDo: put this somewhere sensible.
163 ------------------------------------------------------------------------- */
165 static I_ __GenSymCounter = 0;
170 return(__GenSymCounter++);
173 resetGenSymZh(void) /* it's your funeral */
176 return(__GenSymCounter);
179 /* -----------------------------------------------------------------------------
180 Get the current time as a string. Used in profiling reports.
181 -------------------------------------------------------------------------- */
183 #if defined(PROFILING) || defined(DEBUG) || defined(PAR) || defined(GRAN)
187 static time_t now = 0;
188 static char nowstr[26];
192 strcpy(nowstr, ctime(&now));
193 strcpy(nowstr+16,nowstr+19);
200 /* -----------------------------------------------------------------------------
201 * Reset a file handle to blocking mode. We do this for the standard
202 * file descriptors before exiting, because the shell doesn't always
204 * -------------------------------------------------------------------------- */
206 #if !defined(mingw32_HOST_OS)
208 resetNonBlockingFd(int fd)
212 /* clear the non-blocking flag on this file descriptor */
213 fd_flags = fcntl(fd, F_GETFL);
214 if (fd_flags & O_NONBLOCK) {
215 fcntl(fd, F_SETFL, fd_flags & ~O_NONBLOCK);
220 setNonBlockingFd(int fd)
224 /* clear the non-blocking flag on this file descriptor */
225 fd_flags = fcntl(fd, F_GETFL);
226 if (!(fd_flags & O_NONBLOCK)) {
227 fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
231 /* Stub defns -- async / non-blocking IO is not done
232 * via O_NONBLOCK and select() under Win32.
234 void resetNonBlockingFd(int fd STG_UNUSED) {}
235 void setNonBlockingFd(int fd STG_UNUSED) {}
239 static ullong startTime = 0;
241 /* used in a parallel setup */
245 # if defined(HAVE_GETCLOCK) && !defined(alpha_HOST_ARCH) && !defined(hppa1_1_HOST_ARCH)
248 if (getclock(TIMEOFDAY, &tv) != 0) {
250 fprintf(stderr, "Clock failed\n");
251 stg_exit(EXIT_FAILURE);
253 return tv.tv_sec * LL(1000) + tv.tv_nsec / LL(1000000) - startTime;
254 # elif HAVE_GETTIMEOFDAY && !defined(alpha_HOST_ARCH)
257 if (gettimeofday(&tv, NULL) != 0) {
259 fprintf(stderr, "Clock failed\n");
260 stg_exit(EXIT_FAILURE);
262 return tv.tv_sec * LL(1000) + tv.tv_usec / LL(1000) - startTime;
265 if ((t = time(NULL)) == (time_t) -1) {
267 fprintf(stderr, "Clock failed\n");
268 stg_exit(EXIT_FAILURE);
270 return t * LL(1000) - startTime;
275 /* -----------------------------------------------------------------------------
276 Print large numbers, with punctuation.
277 -------------------------------------------------------------------------- */
280 ullong_format_string(ullong x, char *s, rtsBool with_commas)
282 if (x < (ullong)1000)
283 sprintf(s, "%lu", (lnat)x);
284 else if (x < (ullong)1000000)
285 sprintf(s, (with_commas) ? "%lu,%3.3lu" : "%lu%3.3lu",
286 (lnat)((x)/(ullong)1000),
287 (lnat)((x)%(ullong)1000));
288 else if (x < (ullong)1000000000)
289 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu",
290 (lnat)((x)/(ullong)1000000),
291 (lnat)((x)/(ullong)1000%(ullong)1000),
292 (lnat)((x)%(ullong)1000));
294 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu%3.3lu",
295 (lnat)((x)/(ullong)1000000000),
296 (lnat)((x)/(ullong)1000000%(ullong)1000),
297 (lnat)((x)/(ullong)1000%(ullong)1000),
298 (lnat)((x)%(ullong)1000));
303 // Can be used as a breakpoint to set on every heap check failure.
306 heapCheckFail( void )
312 * It seems that pthreads and signals interact oddly in OpenBSD & FreeBSD
313 * pthreads (and possibly others). When linking with -lpthreads, we
314 * have to use pthread_kill to send blockable signals. So use that
315 * when we have a threaded rts. So System.Posix.Signals will call
316 * genericRaise(), rather than raise(3).
318 int genericRaise(int sig) {
319 #if defined(THREADED_RTS) && (defined(openbsd_HOST_OS) || defined(freebsd_HOST_OS))
320 return pthread_kill(pthread_self(), sig);
326 /* -----------------------------------------------------------------------------
327 Allocating executable memory
328 -------------------------------------------------------------------------- */
330 /* Heavily arch-specific, I'm afraid.. */
333 * Allocate len bytes which are readable, writable, and executable.
335 * ToDo: If this turns out to be a performance bottleneck, one could
336 * e.g. cache the last VirtualProtect/mprotect-ed region and do
337 * nothing in case of a cache hit.
340 stgMallocBytesRWX(int len)
342 void *addr = stgMallocBytes(len, "mallocBytesRWX");
343 #if defined(i386_HOST_ARCH) && defined(_WIN32)
344 /* This could be necessary for processors which distinguish between READ and
345 EXECUTE memory accesses, e.g. Itaniums. */
346 DWORD dwOldProtect = 0;
347 if (VirtualProtect (addr, len, PAGE_EXECUTE_READWRITE, &dwOldProtect) == 0) {
348 barf("mallocBytesRWX: failed to protect 0x%p; error=%lu; old protection: %lu\n",
349 addr, (unsigned long)GetLastError(), (unsigned long)dwOldProtect);
351 #elif defined(openbsd_HOST_OS) || defined(linux_HOST_OS)
352 /* malloced memory isn't executable by default on OpenBSD */
353 my_uintptr_t pageSize = sysconf(_SC_PAGESIZE);
354 my_uintptr_t mask = ~(pageSize - 1);
355 my_uintptr_t startOfFirstPage = ((my_uintptr_t)addr ) & mask;
356 my_uintptr_t startOfLastPage = ((my_uintptr_t)addr + len - 1) & mask;
357 my_uintptr_t size = startOfLastPage - startOfFirstPage + pageSize;
358 if (mprotect((void*)startOfFirstPage, (size_t)size, PROT_EXEC | PROT_READ | PROT_WRITE) != 0) {
359 barf("mallocBytesRWX: failed to protect 0x%p\n", addr);