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;
57 /* -----------------------------------------------------------------------------
58 Result-checking malloc wrappers.
59 -------------------------------------------------------------------------- */
62 stgMallocBytes (int n, char *msg)
66 if ((space = (char *) malloc((size_t) n)) == NULL) {
67 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
68 MallocFailHook((W_) n, msg); /*msg*/
69 stg_exit(EXIT_INTERNAL_ERROR);
75 stgReallocBytes (void *p, int n, char *msg)
79 if ((space = (char *) realloc(p, (size_t) n)) == NULL) {
80 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
81 MallocFailHook((W_) n, msg); /*msg*/
82 stg_exit(EXIT_INTERNAL_ERROR);
88 stgCallocBytes (int n, int m, char *msg)
92 char* p = stgMallocBytes(sz, msg);
93 for (i = 0; i < sz; i++) p[i] = 0;
97 /* To simplify changing the underlying allocator used
98 * by stgMallocBytes(), provide stgFree() as well.
106 /* -----------------------------------------------------------------------------
109 Not sure if this belongs here.
110 -------------------------------------------------------------------------- */
115 StackOverflowHook(RtsFlags.GcFlags.maxStkSize * sizeof(W_));
117 #if defined(TICKY_TICKY)
118 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
125 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
126 OutOfHeapHook(0/*unknown request size*/,
127 RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
129 #if defined(TICKY_TICKY)
130 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
133 stg_exit(EXIT_HEAPOVERFLOW);
136 /* -----------------------------------------------------------------------------
139 Used in addr2Integer because the C compiler on x86 chokes on
140 strlen, trying to inline it with not enough registers available.
141 -------------------------------------------------------------------------- */
143 nat stg_strlen(char *s)
152 /* -----------------------------------------------------------------------------
153 genSym stuff, used by GHC itself for its splitting unique supply.
155 ToDo: put this somewhere sensible.
156 ------------------------------------------------------------------------- */
158 static I_ __GenSymCounter = 0;
163 return(__GenSymCounter++);
166 resetGenSymZh(void) /* it's your funeral */
169 return(__GenSymCounter);
172 /* -----------------------------------------------------------------------------
173 Get the current time as a string. Used in profiling reports.
174 -------------------------------------------------------------------------- */
176 #if defined(PROFILING) || defined(DEBUG) || defined(PAR) || defined(GRAN)
180 static time_t now = 0;
181 static char nowstr[26];
185 strcpy(nowstr, ctime(&now));
186 strcpy(nowstr+16,nowstr+19);
193 /* -----------------------------------------------------------------------------
194 * Reset a file handle to blocking mode. We do this for the standard
195 * file descriptors before exiting, because the shell doesn't always
197 * -------------------------------------------------------------------------- */
199 #if !defined(mingw32_HOST_OS)
201 resetNonBlockingFd(int fd)
205 /* clear the non-blocking flag on this file descriptor */
206 fd_flags = fcntl(fd, F_GETFL);
207 if (fd_flags & O_NONBLOCK) {
208 fcntl(fd, F_SETFL, fd_flags & ~O_NONBLOCK);
213 setNonBlockingFd(int fd)
217 /* clear the non-blocking flag on this file descriptor */
218 fd_flags = fcntl(fd, F_GETFL);
219 if (!(fd_flags & O_NONBLOCK)) {
220 fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
224 /* Stub defns -- async / non-blocking IO is not done
225 * via O_NONBLOCK and select() under Win32.
227 void resetNonBlockingFd(int fd STG_UNUSED) {}
228 void setNonBlockingFd(int fd STG_UNUSED) {}
232 static ullong startTime = 0;
234 /* used in a parallel setup */
238 # if defined(HAVE_GETCLOCK) && !defined(alpha_HOST_ARCH) && !defined(hppa1_1_HOST_ARCH)
241 if (getclock(TIMEOFDAY, &tv) != 0) {
243 fprintf(stderr, "Clock failed\n");
244 stg_exit(EXIT_FAILURE);
246 return tv.tv_sec * LL(1000) + tv.tv_nsec / LL(1000000) - startTime;
247 # elif HAVE_GETTIMEOFDAY && !defined(alpha_HOST_ARCH)
250 if (gettimeofday(&tv, NULL) != 0) {
252 fprintf(stderr, "Clock failed\n");
253 stg_exit(EXIT_FAILURE);
255 return tv.tv_sec * LL(1000) + tv.tv_usec / LL(1000) - startTime;
258 if ((t = time(NULL)) == (time_t) -1) {
260 fprintf(stderr, "Clock failed\n");
261 stg_exit(EXIT_FAILURE);
263 return t * LL(1000) - startTime;
268 /* -----------------------------------------------------------------------------
269 Print large numbers, with punctuation.
270 -------------------------------------------------------------------------- */
273 ullong_format_string(ullong x, char *s, rtsBool with_commas)
275 if (x < (ullong)1000)
276 sprintf(s, "%lu", (lnat)x);
277 else if (x < (ullong)1000000)
278 sprintf(s, (with_commas) ? "%lu,%3.3lu" : "%lu%3.3lu",
279 (lnat)((x)/(ullong)1000),
280 (lnat)((x)%(ullong)1000));
281 else if (x < (ullong)1000000000)
282 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu",
283 (lnat)((x)/(ullong)1000000),
284 (lnat)((x)/(ullong)1000%(ullong)1000),
285 (lnat)((x)%(ullong)1000));
287 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu%3.3lu",
288 (lnat)((x)/(ullong)1000000000),
289 (lnat)((x)/(ullong)1000000%(ullong)1000),
290 (lnat)((x)/(ullong)1000%(ullong)1000),
291 (lnat)((x)%(ullong)1000));
296 // Can be used as a breakpoint to set on every heap check failure.
299 heapCheckFail( void )
305 * It seems that pthreads and signals interact oddly in OpenBSD & FreeBSD
306 * pthreads (and possibly others). When linking with -lpthreads, we
307 * have to use pthread_kill to send blockable signals. So use that
308 * when we have a threaded rts. So System.Posix.Signals will call
309 * genericRaise(), rather than raise(3).
311 int genericRaise(int sig) {
312 #if defined(THREADED_RTS) && (defined(openbsd_HOST_OS) || defined(freebsd_HOST_OS))
313 return pthread_kill(pthread_self(), sig);
319 /* -----------------------------------------------------------------------------
320 Allocating executable memory
321 -------------------------------------------------------------------------- */
323 /* Heavily arch-specific, I'm afraid.. */
326 * Allocate len bytes which are readable, writable, and executable.
328 * ToDo: If this turns out to be a performance bottleneck, one could
329 * e.g. cache the last VirtualProtect/mprotect-ed region and do
330 * nothing in case of a cache hit.
333 stgMallocBytesRWX(int len)
335 void *addr = stgMallocBytes(len, "mallocBytesRWX");
336 #if defined(i386_HOST_ARCH) && defined(_WIN32)
337 /* This could be necessary for processors which distinguish between READ and
338 EXECUTE memory accesses, e.g. Itaniums. */
339 DWORD dwOldProtect = 0;
340 if (VirtualProtect (addr, len, PAGE_EXECUTE_READWRITE, &dwOldProtect) == 0) {
341 barf("mallocBytesRWX: failed to protect 0x%p; error=%lu; old protection: %lu\n",
342 addr, (unsigned long)GetLastError(), (unsigned long)dwOldProtect);
344 #elif defined(openbsd_HOST_OS) || defined(linux_HOST_OS)
345 /* malloced memory isn't executable by default on OpenBSD */
346 my_uintptr_t pageSize = sysconf(_SC_PAGESIZE);
347 my_uintptr_t mask = ~(pageSize - 1);
348 my_uintptr_t startOfFirstPage = ((my_uintptr_t)addr ) & mask;
349 my_uintptr_t startOfLastPage = ((my_uintptr_t)addr + len - 1) & mask;
350 my_uintptr_t size = startOfLastPage - startOfFirstPage + pageSize;
351 if (mprotect((void*)startOfFirstPage, (size_t)size, PROT_EXEC | PROT_READ | PROT_WRITE) != 0) {
352 barf("mallocBytesRWX: failed to protect 0x%p\n", addr);