1 /* -----------------------------------------------------------------------------
2 * $Id: RtsUtils.c,v 1.30 2003/03/19 18:41:19 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;
134 _stgAssert (char *filename, unsigned int linenum)
137 fprintf(stderr, "ASSERTION FAILED: file %s, line %u\n", filename, linenum);
142 /* -----------------------------------------------------------------------------
145 Not sure if this belongs here.
146 -------------------------------------------------------------------------- */
151 StackOverflowHook(RtsFlags.GcFlags.maxStkSize * sizeof(W_));
153 #if defined(TICKY_TICKY)
154 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
161 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
162 OutOfHeapHook(0/*unknown request size*/,
163 RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
165 #if defined(TICKY_TICKY)
166 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
169 stg_exit(EXIT_HEAPOVERFLOW);
172 /* -----------------------------------------------------------------------------
175 Used in addr2Integer because the C compiler on x86 chokes on
176 strlen, trying to inline it with not enough registers available.
177 -------------------------------------------------------------------------- */
179 nat stg_strlen(char *s)
188 /* -----------------------------------------------------------------------------
189 genSym stuff, used by GHC itself for its splitting unique supply.
191 ToDo: put this somewhere sensible.
192 ------------------------------------------------------------------------- */
194 static I_ __GenSymCounter = 0;
199 return(__GenSymCounter++);
202 resetGenSymZh(void) /* it's your funeral */
205 return(__GenSymCounter);
208 /* -----------------------------------------------------------------------------
209 Get the current time as a string. Used in profiling reports.
210 -------------------------------------------------------------------------- */
212 #if defined(PROFILING) || defined(DEBUG) || defined(PAR) || defined(GRAN)
216 static time_t now = 0;
217 static char nowstr[26];
221 strcpy(nowstr, ctime(&now));
222 strcpy(nowstr+16,nowstr+19);
229 /* -----------------------------------------------------------------------------
230 * Reset a file handle to blocking mode. We do this for the standard
231 * file descriptors before exiting, because the shell doesn't always
233 * -------------------------------------------------------------------------- */
235 #if !defined(mingw32_TARGET_OS)
237 resetNonBlockingFd(int fd)
241 /* clear the non-blocking flag on this file descriptor */
242 fd_flags = fcntl(fd, F_GETFL);
243 if (fd_flags & O_NONBLOCK) {
244 fcntl(fd, F_SETFL, fd_flags & ~O_NONBLOCK);
249 setNonBlockingFd(int fd)
253 /* clear the non-blocking flag on this file descriptor */
254 fd_flags = fcntl(fd, F_GETFL);
255 fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
258 /* Don't support non-blocking FDs (yet) on mingw */
259 void resetNonBlockingFd(int fd STG_UNUSED) {}
260 void setNonBlockingFd(int fd STG_UNUSED) {}
263 static ullong startTime = 0;
265 /* used in a parallel setup */
269 # if defined(HAVE_GETCLOCK) && !defined(alpha_TARGET_ARCH) && !defined(hppa1_1_TARGET_ARCH)
272 if (getclock(TIMEOFDAY, &tv) != 0) {
274 fprintf(stderr, "Clock failed\n");
275 stg_exit(EXIT_FAILURE);
277 return tv.tv_sec * LL(1000) + tv.tv_nsec / LL(1000000) - startTime;
278 # elif HAVE_GETTIMEOFDAY && !defined(alpha_TARGET_ARCH)
281 if (gettimeofday(&tv, NULL) != 0) {
283 fprintf(stderr, "Clock failed\n");
284 stg_exit(EXIT_FAILURE);
286 return tv.tv_sec * LL(1000) + tv.tv_usec / LL(1000) - startTime;
289 if ((t = time(NULL)) == (time_t) -1) {
291 fprintf(stderr, "Clock failed\n");
292 stg_exit(EXIT_FAILURE);
294 return t * LL(1000) - startTime;
298 /* -----------------------------------------------------------------------------
299 Print large numbers, with punctuation.
300 -------------------------------------------------------------------------- */
303 ullong_format_string(ullong x, char *s, rtsBool with_commas)
305 if (x < (ullong)1000)
306 sprintf(s, "%lu", (lnat)x);
307 else if (x < (ullong)1000000)
308 sprintf(s, (with_commas) ? "%lu,%3.3lu" : "%lu%3.3lu",
309 (lnat)((x)/(ullong)1000),
310 (lnat)((x)%(ullong)1000));
311 else if (x < (ullong)1000000000)
312 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu",
313 (lnat)((x)/(ullong)1000000),
314 (lnat)((x)/(ullong)1000%(ullong)1000),
315 (lnat)((x)%(ullong)1000));
317 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu%3.3lu",
318 (lnat)((x)/(ullong)1000000000),
319 (lnat)((x)/(ullong)1000000%(ullong)1000),
320 (lnat)((x)/(ullong)1000%(ullong)1000),
321 (lnat)((x)%(ullong)1000));
326 // Can be used as a breakpoint to set on every heap check failure.
329 heapCheckFail( void )