1 /* -----------------------------------------------------------------------------
2 * $Id: RtsUtils.c,v 1.32 2003/03/25 17:58:49 sof Exp $
4 * (c) The GHC Team, 1998-2002
6 * General utility functions used in the RTS.
8 * ---------------------------------------------------------------------------*/
10 /* gettimeofday isn't POSIX */
11 /* #include "PosixSource.h" */
29 #ifdef HAVE_GETTIMEOFDAY
37 /* variable-argument internal error function. */
44 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
45 if (prog_argv != NULL && prog_argv[0] != NULL) {
46 fprintf(stderr, "%s: internal error: ", prog_argv[0]);
48 fprintf(stderr, "internal error: ");
50 vfprintf(stderr, s, ap);
51 fprintf(stderr, "\n");
52 fprintf(stderr, " Please report this as a bug to glasgow-haskell-bugs@haskell.org,\n or http://www.sourceforge.net/projects/ghc/\n");
54 stg_exit(EXIT_INTERNAL_ERROR);
59 prog_belch(char *s, ...)
63 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
64 if (prog_argv != NULL && prog_argv[0] != NULL) {
65 fprintf(stderr, "%s: ", prog_argv[0]);
67 vfprintf(stderr, s, ap);
68 fprintf(stderr, "\n");
77 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
78 vfprintf(stderr, s, ap);
79 fprintf(stderr, "\n");
83 /* result-checking malloc wrappers. */
86 stgMallocBytes (int n, char *msg)
90 if ((space = (char *) malloc((size_t) n)) == NULL) {
91 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
92 MallocFailHook((W_) n, msg); /*msg*/
93 stg_exit(EXIT_INTERNAL_ERROR);
99 stgReallocBytes (void *p, int n, char *msg)
103 if ((space = (char *) realloc(p, (size_t) n)) == NULL) {
104 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
105 MallocFailHook((W_) n, msg); /*msg*/
106 stg_exit(EXIT_INTERNAL_ERROR);
112 stgMallocWords (int n, char *msg)
114 return(stgMallocBytes(n * sizeof(W_), msg));
118 stgReallocWords (void *p, int n, char *msg)
120 return(stgReallocBytes(p, n * sizeof(W_), msg));
124 stgCallocBytes (int n, int m, char *msg)
128 char* p = stgMallocBytes(sz, msg);
129 for (i = 0; i < sz; i++) p[i] = 0;
133 /* To simplify changing the underlying allocator used
134 * by stgMallocBytes(), provide stgFree() as well.
143 _stgAssert (char *filename, unsigned int linenum)
146 fprintf(stderr, "ASSERTION FAILED: file %s, line %u\n", filename, linenum);
151 /* -----------------------------------------------------------------------------
154 Not sure if this belongs here.
155 -------------------------------------------------------------------------- */
160 StackOverflowHook(RtsFlags.GcFlags.maxStkSize * sizeof(W_));
162 #if defined(TICKY_TICKY)
163 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
170 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
171 OutOfHeapHook(0/*unknown request size*/,
172 RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
174 #if defined(TICKY_TICKY)
175 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
178 stg_exit(EXIT_HEAPOVERFLOW);
181 /* -----------------------------------------------------------------------------
184 Used in addr2Integer because the C compiler on x86 chokes on
185 strlen, trying to inline it with not enough registers available.
186 -------------------------------------------------------------------------- */
188 nat stg_strlen(char *s)
197 /* -----------------------------------------------------------------------------
198 genSym stuff, used by GHC itself for its splitting unique supply.
200 ToDo: put this somewhere sensible.
201 ------------------------------------------------------------------------- */
203 static I_ __GenSymCounter = 0;
208 return(__GenSymCounter++);
211 resetGenSymZh(void) /* it's your funeral */
214 return(__GenSymCounter);
217 /* -----------------------------------------------------------------------------
218 Get the current time as a string. Used in profiling reports.
219 -------------------------------------------------------------------------- */
221 #if defined(PROFILING) || defined(DEBUG) || defined(PAR) || defined(GRAN)
225 static time_t now = 0;
226 static char nowstr[26];
230 strcpy(nowstr, ctime(&now));
231 strcpy(nowstr+16,nowstr+19);
238 /* -----------------------------------------------------------------------------
239 * Reset a file handle to blocking mode. We do this for the standard
240 * file descriptors before exiting, because the shell doesn't always
242 * -------------------------------------------------------------------------- */
244 #if !defined(mingw32_TARGET_OS)
246 resetNonBlockingFd(int fd)
250 /* clear the non-blocking flag on this file descriptor */
251 fd_flags = fcntl(fd, F_GETFL);
252 if (fd_flags & O_NONBLOCK) {
253 fcntl(fd, F_SETFL, fd_flags & ~O_NONBLOCK);
258 setNonBlockingFd(int fd)
262 /* clear the non-blocking flag on this file descriptor */
263 fd_flags = fcntl(fd, F_GETFL);
264 fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
267 /* Don't support non-blocking FDs (yet) on mingw */
268 void resetNonBlockingFd(int fd STG_UNUSED) {}
269 void setNonBlockingFd(int fd STG_UNUSED) {}
273 static ullong startTime = 0;
275 /* used in a parallel setup */
279 # if defined(HAVE_GETCLOCK) && !defined(alpha_TARGET_ARCH) && !defined(hppa1_1_TARGET_ARCH)
282 if (getclock(TIMEOFDAY, &tv) != 0) {
284 fprintf(stderr, "Clock failed\n");
285 stg_exit(EXIT_FAILURE);
287 return tv.tv_sec * LL(1000) + tv.tv_nsec / LL(1000000) - startTime;
288 # elif HAVE_GETTIMEOFDAY && !defined(alpha_TARGET_ARCH)
291 if (gettimeofday(&tv, NULL) != 0) {
293 fprintf(stderr, "Clock failed\n");
294 stg_exit(EXIT_FAILURE);
296 return tv.tv_sec * LL(1000) + tv.tv_usec / LL(1000) - startTime;
299 if ((t = time(NULL)) == (time_t) -1) {
301 fprintf(stderr, "Clock failed\n");
302 stg_exit(EXIT_FAILURE);
304 return t * LL(1000) - startTime;
309 /* -----------------------------------------------------------------------------
310 Print large numbers, with punctuation.
311 -------------------------------------------------------------------------- */
314 ullong_format_string(ullong x, char *s, rtsBool with_commas)
316 if (x < (ullong)1000)
317 sprintf(s, "%lu", (lnat)x);
318 else if (x < (ullong)1000000)
319 sprintf(s, (with_commas) ? "%lu,%3.3lu" : "%lu%3.3lu",
320 (lnat)((x)/(ullong)1000),
321 (lnat)((x)%(ullong)1000));
322 else if (x < (ullong)1000000000)
323 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu",
324 (lnat)((x)/(ullong)1000000),
325 (lnat)((x)/(ullong)1000%(ullong)1000),
326 (lnat)((x)%(ullong)1000));
328 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu%3.3lu",
329 (lnat)((x)/(ullong)1000000000),
330 (lnat)((x)/(ullong)1000000%(ullong)1000),
331 (lnat)((x)/(ullong)1000%(ullong)1000),
332 (lnat)((x)%(ullong)1000));
337 // Can be used as a breakpoint to set on every heap check failure.
340 heapCheckFail( void )