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
35 #if defined(openbsd_TARGET_OS)
39 # ifdef HAVE_PTHREAD_H
44 /* -----------------------------------------------------------------------------
45 Result-checking malloc wrappers.
46 -------------------------------------------------------------------------- */
49 stgMallocBytes (int n, char *msg)
53 if ((space = (char *) malloc((size_t) n)) == NULL) {
54 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
55 MallocFailHook((W_) n, msg); /*msg*/
56 stg_exit(EXIT_INTERNAL_ERROR);
62 stgReallocBytes (void *p, int n, char *msg)
66 if ((space = (char *) realloc(p, (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 stgCallocBytes (int n, int m, char *msg)
79 char* p = stgMallocBytes(sz, msg);
80 for (i = 0; i < sz; i++) p[i] = 0;
84 /* To simplify changing the underlying allocator used
85 * by stgMallocBytes(), provide stgFree() as well.
94 _stgAssert (char *filename, unsigned int linenum)
97 fprintf(stderr, "ASSERTION FAILED: file %s, line %u\n", filename, linenum);
102 /* -----------------------------------------------------------------------------
105 Not sure if this belongs here.
106 -------------------------------------------------------------------------- */
111 StackOverflowHook(RtsFlags.GcFlags.maxStkSize * sizeof(W_));
113 #if defined(TICKY_TICKY)
114 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
121 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
122 OutOfHeapHook(0/*unknown request size*/,
123 RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
125 #if defined(TICKY_TICKY)
126 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
129 stg_exit(EXIT_HEAPOVERFLOW);
132 /* -----------------------------------------------------------------------------
135 Used in addr2Integer because the C compiler on x86 chokes on
136 strlen, trying to inline it with not enough registers available.
137 -------------------------------------------------------------------------- */
139 nat stg_strlen(char *s)
148 /* -----------------------------------------------------------------------------
149 genSym stuff, used by GHC itself for its splitting unique supply.
151 ToDo: put this somewhere sensible.
152 ------------------------------------------------------------------------- */
154 static I_ __GenSymCounter = 0;
159 return(__GenSymCounter++);
162 resetGenSymZh(void) /* it's your funeral */
165 return(__GenSymCounter);
168 /* -----------------------------------------------------------------------------
169 Get the current time as a string. Used in profiling reports.
170 -------------------------------------------------------------------------- */
172 #if defined(PROFILING) || defined(DEBUG) || defined(PAR) || defined(GRAN)
176 static time_t now = 0;
177 static char nowstr[26];
181 strcpy(nowstr, ctime(&now));
182 strcpy(nowstr+16,nowstr+19);
189 /* -----------------------------------------------------------------------------
190 * Reset a file handle to blocking mode. We do this for the standard
191 * file descriptors before exiting, because the shell doesn't always
193 * -------------------------------------------------------------------------- */
195 #if !defined(mingw32_TARGET_OS)
197 resetNonBlockingFd(int fd)
201 /* clear the non-blocking flag on this file descriptor */
202 fd_flags = fcntl(fd, F_GETFL);
203 if (fd_flags & O_NONBLOCK) {
204 fcntl(fd, F_SETFL, fd_flags & ~O_NONBLOCK);
209 setNonBlockingFd(int fd)
213 /* clear the non-blocking flag on this file descriptor */
214 fd_flags = fcntl(fd, F_GETFL);
215 if (!(fd_flags & O_NONBLOCK)) {
216 fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
220 /* Stub defns -- async / non-blocking IO is not done
221 * via O_NONBLOCK and select() under Win32.
223 void resetNonBlockingFd(int fd STG_UNUSED) {}
224 void setNonBlockingFd(int fd STG_UNUSED) {}
228 static ullong startTime = 0;
230 /* used in a parallel setup */
234 # if defined(HAVE_GETCLOCK) && !defined(alpha_TARGET_ARCH) && !defined(hppa1_1_TARGET_ARCH)
237 if (getclock(TIMEOFDAY, &tv) != 0) {
239 fprintf(stderr, "Clock failed\n");
240 stg_exit(EXIT_FAILURE);
242 return tv.tv_sec * LL(1000) + tv.tv_nsec / LL(1000000) - startTime;
243 # elif HAVE_GETTIMEOFDAY && !defined(alpha_TARGET_ARCH)
246 if (gettimeofday(&tv, NULL) != 0) {
248 fprintf(stderr, "Clock failed\n");
249 stg_exit(EXIT_FAILURE);
251 return tv.tv_sec * LL(1000) + tv.tv_usec / LL(1000) - startTime;
254 if ((t = time(NULL)) == (time_t) -1) {
256 fprintf(stderr, "Clock failed\n");
257 stg_exit(EXIT_FAILURE);
259 return t * LL(1000) - startTime;
264 /* -----------------------------------------------------------------------------
265 Print large numbers, with punctuation.
266 -------------------------------------------------------------------------- */
269 ullong_format_string(ullong x, char *s, rtsBool with_commas)
271 if (x < (ullong)1000)
272 sprintf(s, "%lu", (lnat)x);
273 else if (x < (ullong)1000000)
274 sprintf(s, (with_commas) ? "%lu,%3.3lu" : "%lu%3.3lu",
275 (lnat)((x)/(ullong)1000),
276 (lnat)((x)%(ullong)1000));
277 else if (x < (ullong)1000000000)
278 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu",
279 (lnat)((x)/(ullong)1000000),
280 (lnat)((x)/(ullong)1000%(ullong)1000),
281 (lnat)((x)%(ullong)1000));
283 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu%3.3lu",
284 (lnat)((x)/(ullong)1000000000),
285 (lnat)((x)/(ullong)1000000%(ullong)1000),
286 (lnat)((x)/(ullong)1000%(ullong)1000),
287 (lnat)((x)%(ullong)1000));
292 // Can be used as a breakpoint to set on every heap check failure.
295 heapCheckFail( void )
301 * It seems that pthreads and signals interact oddly in OpenBSD
302 * pthreads (and possibly FreeBSD). When linking with -lpthreads, we
303 * have to use pthread_kill to send blockable signals. So use that
304 * when we have a threaded rts. So System.Posix.Signals will call
305 * genericRaise(), rather than raise(3).
307 #if defined(openbsd_TARGET_OS)
308 int genericRaise(int sig) {
309 # if defined(THREADED_RTS)
310 return pthread_kill(pthread_self(), sig);