1 /* -----------------------------------------------------------------------------
2 * $Id: RtsUtils.c,v 1.20 2001/08/14 13:40:09 sewardj Exp $
4 * (c) The GHC Team, 1998-1999
6 * General utility functions used in the RTS.
8 * ---------------------------------------------------------------------------*/
10 /* gettimeofday isn't POSIX */
11 /* #include "PosixSource.h" */
30 #ifdef HAVE_GETTIMEOFDAY
36 /* variable-argument error function. */
38 void barf(char *s, ...)
42 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
43 if (prog_argv != NULL && prog_argv[0] != NULL) {
44 fprintf(stderr, "%s: fatal error: ", prog_argv[0]);
46 fprintf(stderr, "fatal error: ");
48 vfprintf(stderr, s, ap);
49 fprintf(stderr, "\n");
51 stg_exit(EXIT_INTERNAL_ERROR);
54 void prog_belch(char *s, ...)
58 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
59 if (prog_argv != NULL && prog_argv[0] != NULL) {
60 fprintf(stderr, "%s: ", prog_argv[0]);
62 vfprintf(stderr, s, ap);
63 fprintf(stderr, "\n");
66 void belch(char *s, ...)
70 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
71 vfprintf(stderr, s, ap);
72 fprintf(stderr, "\n");
75 /* result-checking malloc wrappers. */
78 stgMallocBytes (int n, char *msg)
82 if ((space = (char *) malloc((size_t) n)) == NULL) {
83 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
84 MallocFailHook((W_) n, msg); /*msg*/
85 stg_exit(EXIT_INTERNAL_ERROR);
91 stgReallocBytes (void *p, int n, char *msg)
95 if ((space = (char *) realloc(p, (size_t) n)) == NULL) {
96 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
97 MallocFailHook((W_) n, msg); /*msg*/
98 stg_exit(EXIT_INTERNAL_ERROR);
104 stgMallocWords (int n, char *msg)
106 return(stgMallocBytes(n * sizeof(W_), msg));
110 stgReallocWords (void *p, int n, char *msg)
112 return(stgReallocBytes(p, n * sizeof(W_), msg));
116 _stgAssert (char *filename, unsigned int linenum)
119 fprintf(stderr, "ASSERTION FAILED: file %s, line %u\n", filename, linenum);
124 /* -----------------------------------------------------------------------------
127 Not sure if this belongs here.
128 -------------------------------------------------------------------------- */
133 StackOverflowHook(RtsFlags.GcFlags.maxStkSize * sizeof(W_));
135 #if defined(TICKY_TICKY)
136 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
143 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
144 OutOfHeapHook(0/*unknown request size*/,
145 RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
147 #if defined(TICKY_TICKY)
148 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
151 stg_exit(EXIT_HEAPOVERFLOW);
154 /* -----------------------------------------------------------------------------
157 Used in addr2Integer because the C compiler on x86 chokes on
158 strlen, trying to inline it with not enough registers available.
159 -------------------------------------------------------------------------- */
161 nat stg_strlen(char *s)
170 /* -----------------------------------------------------------------------------
171 genSym stuff, used by GHC itself for its splitting unique supply.
173 ToDo: put this somewhere sensible.
174 ------------------------------------------------------------------------- */
176 I_ __GenSymCounter = 0;
181 return(__GenSymCounter++);
184 resetGenSymZh(void) /* it's your funeral */
187 return(__GenSymCounter);
190 /* -----------------------------------------------------------------------------
191 Get the current time as a string. Used in profiling reports.
192 -------------------------------------------------------------------------- */
194 #if defined(PROFILING) || defined(DEBUG) || defined(PAR) || defined(GRAN)
198 static time_t now = 0;
199 static char nowstr[26];
203 strcpy(nowstr, ctime(&now));
204 strcpy(nowstr+16,nowstr+19);
211 /* -----------------------------------------------------------------------------
212 * Reset a file handle to blocking mode. We do this for the standard
213 * file descriptors before exiting, because the shell doesn't always
215 * -------------------------------------------------------------------------- */
218 resetNonBlockingFd(int fd)
222 #if !defined(mingw32_TARGET_OS)
223 /* clear the non-blocking flag on this file descriptor */
224 fd_flags = fcntl(fd, F_GETFL);
225 if (fd_flags & O_NONBLOCK) {
226 fcntl(fd, F_SETFL, fd_flags & ~O_NONBLOCK);
231 static ullong startTime = 0;
233 /* used in a parallel setup */
237 # if defined(HAVE_GETCLOCK) && !defined(alpha_TARGET_ARCH) && !defined(hppa1_1_TARGET_ARCH)
240 if (getclock(TIMEOFDAY, &tv) != 0) {
242 fprintf(stderr, "Clock failed\n");
243 stg_exit(EXIT_FAILURE);
245 return tv.tv_sec * LL(1000) + tv.tv_nsec / LL(1000000) - startTime;
246 # elif HAVE_GETTIMEOFDAY && !defined(alpha_TARGET_ARCH)
249 if (gettimeofday(&tv, NULL) != 0) {
251 fprintf(stderr, "Clock failed\n");
252 stg_exit(EXIT_FAILURE);
254 return tv.tv_sec * LL(1000) + tv.tv_usec / LL(1000) - startTime;
257 if ((t = time(NULL)) == (time_t) -1) {
259 fprintf(stderr, "Clock failed\n");
260 stg_exit(EXIT_FAILURE);
262 return t * LL(1000) - startTime;
266 /* -----------------------------------------------------------------------------
267 Print large numbers, with punctuation.
268 -------------------------------------------------------------------------- */
271 ullong_format_string(ullong x, char *s, rtsBool with_commas)
273 if (x < (ullong)1000)
274 sprintf(s, "%d", (nat)x);
275 else if (x < (ullong)1000000)
276 sprintf(s, (with_commas) ? "%ld,%3.3ld" : "%ld%3.3ld",
277 (nat)((x)/(ullong)1000),
278 (nat)((x)%(ullong)1000));
279 else if (x < (ullong)1000000000)
280 sprintf(s, (with_commas) ? "%ld,%3.3ld,%3.3ld" : "%ld%3.3ld%3.3ld",
281 (nat)((x)/(ullong)1000000),
282 (nat)((x)/(ullong)1000%(ullong)1000),
283 (nat)((x)%(ullong)1000));
285 sprintf(s, (with_commas) ? "%ld,%3.3ld,%3.3ld,%3.3ld" : "%ld%3.3ld%3.3ld%3.3ld",
286 (nat)((x)/(ullong)1000000000),
287 (nat)((x)/(ullong)1000000%(ullong)1000),
288 (nat)((x)/(ullong)1000%(ullong)1000),
289 (nat)((x)%(ullong)1000));