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];
193 ctime_r(&now, nowstr);
195 strcpy(nowstr, ctime(&now));
197 memmove(nowstr+16,nowstr+19,7);
198 nowstr[21] = '\0'; // removes the \n
204 /* -----------------------------------------------------------------------------
205 * Reset a file handle to blocking mode. We do this for the standard
206 * file descriptors before exiting, because the shell doesn't always
208 * -------------------------------------------------------------------------- */
210 #if !defined(mingw32_HOST_OS)
212 resetNonBlockingFd(int fd)
216 /* clear the non-blocking flag on this file descriptor */
217 fd_flags = fcntl(fd, F_GETFL);
218 if (fd_flags & O_NONBLOCK) {
219 fcntl(fd, F_SETFL, fd_flags & ~O_NONBLOCK);
224 setNonBlockingFd(int fd)
228 /* clear the non-blocking flag on this file descriptor */
229 fd_flags = fcntl(fd, F_GETFL);
230 if (!(fd_flags & O_NONBLOCK)) {
231 fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
235 /* Stub defns -- async / non-blocking IO is not done
236 * via O_NONBLOCK and select() under Win32.
238 void resetNonBlockingFd(int fd STG_UNUSED) {}
239 void setNonBlockingFd(int fd STG_UNUSED) {}
243 static ullong startTime = 0;
245 /* used in a parallel setup */
249 # if defined(HAVE_GETCLOCK) && !defined(alpha_HOST_ARCH) && !defined(hppa1_1_HOST_ARCH)
252 if (getclock(TIMEOFDAY, &tv) != 0) {
254 fprintf(stderr, "Clock failed\n");
255 stg_exit(EXIT_FAILURE);
257 return tv.tv_sec * LL(1000) + tv.tv_nsec / LL(1000000) - startTime;
258 # elif HAVE_GETTIMEOFDAY && !defined(alpha_HOST_ARCH)
261 if (gettimeofday(&tv, NULL) != 0) {
263 fprintf(stderr, "Clock failed\n");
264 stg_exit(EXIT_FAILURE);
266 return tv.tv_sec * LL(1000) + tv.tv_usec / LL(1000) - startTime;
269 if ((t = time(NULL)) == (time_t) -1) {
271 fprintf(stderr, "Clock failed\n");
272 stg_exit(EXIT_FAILURE);
274 return t * LL(1000) - startTime;
279 /* -----------------------------------------------------------------------------
280 Print large numbers, with punctuation.
281 -------------------------------------------------------------------------- */
284 ullong_format_string(ullong x, char *s, rtsBool with_commas)
286 if (x < (ullong)1000)
287 sprintf(s, "%lu", (lnat)x);
288 else if (x < (ullong)1000000)
289 sprintf(s, (with_commas) ? "%lu,%3.3lu" : "%lu%3.3lu",
290 (lnat)((x)/(ullong)1000),
291 (lnat)((x)%(ullong)1000));
292 else if (x < (ullong)1000000000)
293 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu",
294 (lnat)((x)/(ullong)1000000),
295 (lnat)((x)/(ullong)1000%(ullong)1000),
296 (lnat)((x)%(ullong)1000));
298 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu%3.3lu",
299 (lnat)((x)/(ullong)1000000000),
300 (lnat)((x)/(ullong)1000000%(ullong)1000),
301 (lnat)((x)/(ullong)1000%(ullong)1000),
302 (lnat)((x)%(ullong)1000));
307 // Can be used as a breakpoint to set on every heap check failure.
310 heapCheckFail( void )
316 * It seems that pthreads and signals interact oddly in OpenBSD & FreeBSD
317 * pthreads (and possibly others). When linking with -lpthreads, we
318 * have to use pthread_kill to send blockable signals. So use that
319 * when we have a threaded rts. So System.Posix.Signals will call
320 * genericRaise(), rather than raise(3).
322 int genericRaise(int sig) {
323 #if defined(THREADED_RTS) && (defined(openbsd_HOST_OS) || defined(freebsd_HOST_OS))
324 return pthread_kill(pthread_self(), sig);
330 /* -----------------------------------------------------------------------------
331 Allocating executable memory
332 -------------------------------------------------------------------------- */
334 /* Heavily arch-specific, I'm afraid.. */
337 * Allocate len bytes which are readable, writable, and executable.
339 * ToDo: If this turns out to be a performance bottleneck, one could
340 * e.g. cache the last VirtualProtect/mprotect-ed region and do
341 * nothing in case of a cache hit.
344 stgMallocBytesRWX(int len)
346 void *addr = stgMallocBytes(len, "mallocBytesRWX");
347 #if defined(i386_HOST_ARCH) && defined(_WIN32)
348 /* This could be necessary for processors which distinguish between READ and
349 EXECUTE memory accesses, e.g. Itaniums. */
350 DWORD dwOldProtect = 0;
351 if (VirtualProtect (addr, len, PAGE_EXECUTE_READWRITE, &dwOldProtect) == 0) {
352 barf("mallocBytesRWX: failed to protect 0x%p; error=%lu; old protection: %lu\n",
353 addr, (unsigned long)GetLastError(), (unsigned long)dwOldProtect);
355 #elif defined(openbsd_HOST_OS) || defined(linux_HOST_OS)
356 /* malloced memory isn't executable by default on OpenBSD */
357 my_uintptr_t pageSize = sysconf(_SC_PAGESIZE);
358 my_uintptr_t mask = ~(pageSize - 1);
359 my_uintptr_t startOfFirstPage = ((my_uintptr_t)addr ) & mask;
360 my_uintptr_t startOfLastPage = ((my_uintptr_t)addr + len - 1) & mask;
361 my_uintptr_t size = startOfLastPage - startOfFirstPage + pageSize;
362 if (mprotect((void*)startOfFirstPage, (size_t)size, PROT_EXEC | PROT_READ | PROT_WRITE) != 0) {
363 barf("mallocBytesRWX: failed to protect 0x%p\n", addr);