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)
42 #if defined(openbsd_HOST_OS) || defined(linux_HOST_OS) || defined(darwin_HOST_OS)
44 #include <sys/types.h>
47 /* no C99 header stdint.h on OpenBSD? */
48 #if defined(openbsd_HOST_OS)
49 typedef unsigned long my_uintptr_t;
52 typedef uintptr_t my_uintptr_t;
60 /* -----------------------------------------------------------------------------
61 Result-checking malloc wrappers.
62 -------------------------------------------------------------------------- */
65 stgMallocBytes (int n, char *msg)
69 if ((space = (char *) malloc((size_t) n)) == NULL) {
70 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
71 MallocFailHook((W_) n, msg); /*msg*/
72 stg_exit(EXIT_INTERNAL_ERROR);
78 stgReallocBytes (void *p, int n, char *msg)
82 if ((space = (char *) realloc(p, (size_t) n)) == NULL) {
83 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
84 MallocFailHook((W_) n, msg); /*msg*/
85 stg_exit(EXIT_INTERNAL_ERROR);
91 stgCallocBytes (int n, int m, char *msg)
95 if ((space = (char *) calloc((size_t) n, (size_t) m)) == NULL) {
96 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
97 MallocFailHook((W_) n*m, msg); /*msg*/
98 stg_exit(EXIT_INTERNAL_ERROR);
103 /* To simplify changing the underlying allocator used
104 * by stgMallocBytes(), provide stgFree() as well.
112 /* -----------------------------------------------------------------------------
115 Not sure if this belongs here.
116 -------------------------------------------------------------------------- */
121 StackOverflowHook(RtsFlags.GcFlags.maxStkSize * sizeof(W_));
123 #if defined(TICKY_TICKY)
124 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
131 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
132 OutOfHeapHook(0/*unknown request size*/,
133 RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
135 #if defined(TICKY_TICKY)
136 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
139 stg_exit(EXIT_HEAPOVERFLOW);
142 /* -----------------------------------------------------------------------------
145 Used in addr2Integer because the C compiler on x86 chokes on
146 strlen, trying to inline it with not enough registers available.
147 -------------------------------------------------------------------------- */
149 nat stg_strlen(char *s)
158 /* -----------------------------------------------------------------------------
159 genSym stuff, used by GHC itself for its splitting unique supply.
161 ToDo: put this somewhere sensible.
162 ------------------------------------------------------------------------- */
164 static I_ __GenSymCounter = 0;
169 return(__GenSymCounter++);
172 resetGenSymZh(void) /* it's your funeral */
175 return(__GenSymCounter);
178 /* -----------------------------------------------------------------------------
179 Get the current time as a string. Used in profiling reports.
180 -------------------------------------------------------------------------- */
182 #if defined(PROFILING) || defined(DEBUG) || defined(PAR) || defined(GRAN)
186 static time_t now = 0;
187 static char nowstr[26];
192 ctime_r(&now, nowstr);
194 strcpy(nowstr, ctime(&now));
196 memmove(nowstr+16,nowstr+19,7);
197 nowstr[21] = '\0'; // removes the \n
203 /* -----------------------------------------------------------------------------
204 * Reset a file handle to blocking mode. We do this for the standard
205 * file descriptors before exiting, because the shell doesn't always
207 * -------------------------------------------------------------------------- */
209 #if !defined(mingw32_HOST_OS)
211 resetNonBlockingFd(int fd)
215 /* clear the non-blocking flag on this file descriptor */
216 fd_flags = fcntl(fd, F_GETFL);
217 if (fd_flags & O_NONBLOCK) {
218 fcntl(fd, F_SETFL, fd_flags & ~O_NONBLOCK);
223 setNonBlockingFd(int fd)
227 /* clear the non-blocking flag on this file descriptor */
228 fd_flags = fcntl(fd, F_GETFL);
229 if (!(fd_flags & O_NONBLOCK)) {
230 fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
234 /* Stub defns -- async / non-blocking IO is not done
235 * via O_NONBLOCK and select() under Win32.
237 void resetNonBlockingFd(int fd STG_UNUSED) {}
238 void setNonBlockingFd(int fd STG_UNUSED) {}
242 static ullong startTime = 0;
244 /* used in a parallel setup */
248 # if defined(HAVE_GETCLOCK) && !defined(alpha_HOST_ARCH) && !defined(hppa1_1_HOST_ARCH)
251 if (getclock(TIMEOFDAY, &tv) != 0) {
253 fprintf(stderr, "Clock failed\n");
254 stg_exit(EXIT_FAILURE);
256 return tv.tv_sec * LL(1000) + tv.tv_nsec / LL(1000000) - startTime;
257 # elif HAVE_GETTIMEOFDAY && !defined(alpha_HOST_ARCH)
260 if (gettimeofday(&tv, NULL) != 0) {
262 fprintf(stderr, "Clock failed\n");
263 stg_exit(EXIT_FAILURE);
265 return tv.tv_sec * LL(1000) + tv.tv_usec / LL(1000) - startTime;
268 if ((t = time(NULL)) == (time_t) -1) {
270 fprintf(stderr, "Clock failed\n");
271 stg_exit(EXIT_FAILURE);
273 return t * LL(1000) - startTime;
278 /* -----------------------------------------------------------------------------
279 Print large numbers, with punctuation.
280 -------------------------------------------------------------------------- */
283 ullong_format_string(ullong x, char *s, rtsBool with_commas)
285 if (x < (ullong)1000)
286 sprintf(s, "%lu", (lnat)x);
287 else if (x < (ullong)1000000)
288 sprintf(s, (with_commas) ? "%lu,%3.3lu" : "%lu%3.3lu",
289 (lnat)((x)/(ullong)1000),
290 (lnat)((x)%(ullong)1000));
291 else if (x < (ullong)1000000000)
292 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu",
293 (lnat)((x)/(ullong)1000000),
294 (lnat)((x)/(ullong)1000%(ullong)1000),
295 (lnat)((x)%(ullong)1000));
297 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu%3.3lu",
298 (lnat)((x)/(ullong)1000000000),
299 (lnat)((x)/(ullong)1000000%(ullong)1000),
300 (lnat)((x)/(ullong)1000%(ullong)1000),
301 (lnat)((x)%(ullong)1000));
306 // Can be used as a breakpoint to set on every heap check failure.
309 heapCheckFail( void )
315 * It seems that pthreads and signals interact oddly in OpenBSD & FreeBSD
316 * pthreads (and possibly others). When linking with -lpthreads, we
317 * have to use pthread_kill to send blockable signals. So use that
318 * when we have a threaded rts. So System.Posix.Signals will call
319 * genericRaise(), rather than raise(3).
321 int genericRaise(int sig) {
322 #if defined(THREADED_RTS) && (defined(openbsd_HOST_OS) || defined(freebsd_HOST_OS))
323 return pthread_kill(pthread_self(), sig);
329 /* -----------------------------------------------------------------------------
330 Allocating executable memory
331 -------------------------------------------------------------------------- */
333 /* Heavily arch-specific, I'm afraid.. */
336 * Allocate len bytes which are readable, writable, and executable.
338 * ToDo: If this turns out to be a performance bottleneck, one could
339 * e.g. cache the last VirtualProtect/mprotect-ed region and do
340 * nothing in case of a cache hit.
343 stgMallocBytesRWX(int len)
345 void *addr = stgMallocBytes(len, "mallocBytesRWX");
346 #if defined(i386_HOST_ARCH) && defined(_WIN32)
347 /* This could be necessary for processors which distinguish between READ and
348 EXECUTE memory accesses, e.g. Itaniums. */
349 DWORD dwOldProtect = 0;
350 if (VirtualProtect (addr, len, PAGE_EXECUTE_READWRITE, &dwOldProtect) == 0) {
351 barf("mallocBytesRWX: failed to protect 0x%p; error=%lu; old protection: %lu\n",
352 addr, (unsigned long)GetLastError(), (unsigned long)dwOldProtect);
354 #elif defined(openbsd_HOST_OS) || defined(linux_HOST_OS) || defined(darwin_HOST_OS)
355 /* malloced memory isn't executable by default on OpenBSD */
356 my_uintptr_t pageSize = sysconf(_SC_PAGESIZE);
357 my_uintptr_t mask = ~(pageSize - 1);
358 my_uintptr_t startOfFirstPage = ((my_uintptr_t)addr ) & mask;
359 my_uintptr_t startOfLastPage = ((my_uintptr_t)addr + len - 1) & mask;
360 my_uintptr_t size = startOfLastPage - startOfFirstPage + pageSize;
361 if (mprotect((void*)startOfFirstPage, (size_t)size, PROT_EXEC | PROT_READ | PROT_WRITE) != 0) {
362 barf("mallocBytesRWX: failed to protect 0x%p\n", addr);