1 /* -----------------------------------------------------------------------------
2 * $Id: RtsUtils.c,v 1.36 2003/10/21 11:51:15 stolz 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_name != NULL) {
46 fprintf(stderr, "%s: internal error: ", prog_name);
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_name != NULL) {
65 fprintf(stderr, "%s: ", prog_name);
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 stgCallocBytes (int n, int m, char *msg)
116 char* p = stgMallocBytes(sz, msg);
117 for (i = 0; i < sz; i++) p[i] = 0;
121 /* To simplify changing the underlying allocator used
122 * by stgMallocBytes(), provide stgFree() as well.
131 _stgAssert (char *filename, unsigned int linenum)
134 fprintf(stderr, "ASSERTION FAILED: file %s, line %u\n", filename, linenum);
139 /* -----------------------------------------------------------------------------
142 Not sure if this belongs here.
143 -------------------------------------------------------------------------- */
148 StackOverflowHook(RtsFlags.GcFlags.maxStkSize * sizeof(W_));
150 #if defined(TICKY_TICKY)
151 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
158 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
159 OutOfHeapHook(0/*unknown request size*/,
160 RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
162 #if defined(TICKY_TICKY)
163 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
166 stg_exit(EXIT_HEAPOVERFLOW);
169 /* -----------------------------------------------------------------------------
172 Used in addr2Integer because the C compiler on x86 chokes on
173 strlen, trying to inline it with not enough registers available.
174 -------------------------------------------------------------------------- */
176 nat stg_strlen(char *s)
185 /* -----------------------------------------------------------------------------
186 genSym stuff, used by GHC itself for its splitting unique supply.
188 ToDo: put this somewhere sensible.
189 ------------------------------------------------------------------------- */
191 static I_ __GenSymCounter = 0;
196 return(__GenSymCounter++);
199 resetGenSymZh(void) /* it's your funeral */
202 return(__GenSymCounter);
205 /* -----------------------------------------------------------------------------
206 Get the current time as a string. Used in profiling reports.
207 -------------------------------------------------------------------------- */
209 #if defined(PROFILING) || defined(DEBUG) || defined(PAR) || defined(GRAN)
213 static time_t now = 0;
214 static char nowstr[26];
218 strcpy(nowstr, ctime(&now));
219 strcpy(nowstr+16,nowstr+19);
226 /* -----------------------------------------------------------------------------
227 * Reset a file handle to blocking mode. We do this for the standard
228 * file descriptors before exiting, because the shell doesn't always
230 * -------------------------------------------------------------------------- */
232 #if !defined(mingw32_TARGET_OS)
234 resetNonBlockingFd(int fd)
238 /* clear the non-blocking flag on this file descriptor */
239 fd_flags = fcntl(fd, F_GETFL);
240 if (fd_flags & O_NONBLOCK) {
241 fcntl(fd, F_SETFL, fd_flags & ~O_NONBLOCK);
246 setNonBlockingFd(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);
257 /* Stub defns -- async / non-blocking IO is not done
258 * via O_NONBLOCK and select() under Win32.
260 void resetNonBlockingFd(int fd STG_UNUSED) {}
261 void setNonBlockingFd(int fd STG_UNUSED) {}
265 static ullong startTime = 0;
267 /* used in a parallel setup */
271 # if defined(HAVE_GETCLOCK) && !defined(alpha_TARGET_ARCH) && !defined(hppa1_1_TARGET_ARCH)
274 if (getclock(TIMEOFDAY, &tv) != 0) {
276 fprintf(stderr, "Clock failed\n");
277 stg_exit(EXIT_FAILURE);
279 return tv.tv_sec * LL(1000) + tv.tv_nsec / LL(1000000) - startTime;
280 # elif HAVE_GETTIMEOFDAY && !defined(alpha_TARGET_ARCH)
283 if (gettimeofday(&tv, NULL) != 0) {
285 fprintf(stderr, "Clock failed\n");
286 stg_exit(EXIT_FAILURE);
288 return tv.tv_sec * LL(1000) + tv.tv_usec / LL(1000) - startTime;
291 if ((t = time(NULL)) == (time_t) -1) {
293 fprintf(stderr, "Clock failed\n");
294 stg_exit(EXIT_FAILURE);
296 return t * LL(1000) - startTime;
301 /* -----------------------------------------------------------------------------
302 Print large numbers, with punctuation.
303 -------------------------------------------------------------------------- */
306 ullong_format_string(ullong x, char *s, rtsBool with_commas)
308 if (x < (ullong)1000)
309 sprintf(s, "%lu", (lnat)x);
310 else if (x < (ullong)1000000)
311 sprintf(s, (with_commas) ? "%lu,%3.3lu" : "%lu%3.3lu",
312 (lnat)((x)/(ullong)1000),
313 (lnat)((x)%(ullong)1000));
314 else if (x < (ullong)1000000000)
315 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu",
316 (lnat)((x)/(ullong)1000000),
317 (lnat)((x)/(ullong)1000%(ullong)1000),
318 (lnat)((x)%(ullong)1000));
320 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu%3.3lu",
321 (lnat)((x)/(ullong)1000000000),
322 (lnat)((x)/(ullong)1000000%(ullong)1000),
323 (lnat)((x)/(ullong)1000%(ullong)1000),
324 (lnat)((x)%(ullong)1000));
329 // Can be used as a breakpoint to set on every heap check failure.
332 heapCheckFail( void )