1 /* -----------------------------------------------------------------------------
2 * $Id: RtsUtils.c,v 1.23 2001/10/31 10:30:29 simonmar 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 stgCallocBytes (int n, int m, char *msg)
120 char* p = stgMallocBytes(sz, msg);
121 for (i = 0; i < sz; i++) p[i] = 0;
126 _stgAssert (char *filename, unsigned int linenum)
129 fprintf(stderr, "ASSERTION FAILED: file %s, line %u\n", filename, linenum);
134 /* -----------------------------------------------------------------------------
137 Not sure if this belongs here.
138 -------------------------------------------------------------------------- */
143 StackOverflowHook(RtsFlags.GcFlags.maxStkSize * sizeof(W_));
145 #if defined(TICKY_TICKY)
146 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
153 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
154 OutOfHeapHook(0/*unknown request size*/,
155 RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
157 #if defined(TICKY_TICKY)
158 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
161 stg_exit(EXIT_HEAPOVERFLOW);
164 /* -----------------------------------------------------------------------------
167 Used in addr2Integer because the C compiler on x86 chokes on
168 strlen, trying to inline it with not enough registers available.
169 -------------------------------------------------------------------------- */
171 nat stg_strlen(char *s)
180 /* -----------------------------------------------------------------------------
181 genSym stuff, used by GHC itself for its splitting unique supply.
183 ToDo: put this somewhere sensible.
184 ------------------------------------------------------------------------- */
186 I_ __GenSymCounter = 0;
191 return(__GenSymCounter++);
194 resetGenSymZh(void) /* it's your funeral */
197 return(__GenSymCounter);
200 /* -----------------------------------------------------------------------------
201 Get the current time as a string. Used in profiling reports.
202 -------------------------------------------------------------------------- */
204 #if defined(PROFILING) || defined(DEBUG) || defined(PAR) || defined(GRAN)
208 static time_t now = 0;
209 static char nowstr[26];
213 strcpy(nowstr, ctime(&now));
214 strcpy(nowstr+16,nowstr+19);
221 /* -----------------------------------------------------------------------------
222 * Reset a file handle to blocking mode. We do this for the standard
223 * file descriptors before exiting, because the shell doesn't always
225 * -------------------------------------------------------------------------- */
228 resetNonBlockingFd(int fd)
232 #if !defined(mingw32_TARGET_OS)
233 /* clear the non-blocking flag on this file descriptor */
234 fd_flags = fcntl(fd, F_GETFL);
235 if (fd_flags & O_NONBLOCK) {
236 fcntl(fd, F_SETFL, fd_flags & ~O_NONBLOCK);
242 setNonBlockingFd(int fd)
246 #if !defined(mingw32_TARGET_OS)
247 /* clear the non-blocking flag on this file descriptor */
248 fd_flags = fcntl(fd, F_GETFL);
249 fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
253 static ullong startTime = 0;
255 /* used in a parallel setup */
259 # if defined(HAVE_GETCLOCK) && !defined(alpha_TARGET_ARCH) && !defined(hppa1_1_TARGET_ARCH)
262 if (getclock(TIMEOFDAY, &tv) != 0) {
264 fprintf(stderr, "Clock failed\n");
265 stg_exit(EXIT_FAILURE);
267 return tv.tv_sec * LL(1000) + tv.tv_nsec / LL(1000000) - startTime;
268 # elif HAVE_GETTIMEOFDAY && !defined(alpha_TARGET_ARCH)
271 if (gettimeofday(&tv, NULL) != 0) {
273 fprintf(stderr, "Clock failed\n");
274 stg_exit(EXIT_FAILURE);
276 return tv.tv_sec * LL(1000) + tv.tv_usec / LL(1000) - startTime;
279 if ((t = time(NULL)) == (time_t) -1) {
281 fprintf(stderr, "Clock failed\n");
282 stg_exit(EXIT_FAILURE);
284 return t * LL(1000) - startTime;
288 /* -----------------------------------------------------------------------------
289 Print large numbers, with punctuation.
290 -------------------------------------------------------------------------- */
293 ullong_format_string(ullong x, char *s, rtsBool with_commas)
295 if (x < (ullong)1000)
296 sprintf(s, "%d", (nat)x);
297 else if (x < (ullong)1000000)
298 sprintf(s, (with_commas) ? "%ld,%3.3ld" : "%ld%3.3ld",
299 (nat)((x)/(ullong)1000),
300 (nat)((x)%(ullong)1000));
301 else if (x < (ullong)1000000000)
302 sprintf(s, (with_commas) ? "%ld,%3.3ld,%3.3ld" : "%ld%3.3ld%3.3ld",
303 (nat)((x)/(ullong)1000000),
304 (nat)((x)/(ullong)1000%(ullong)1000),
305 (nat)((x)%(ullong)1000));
307 sprintf(s, (with_commas) ? "%ld,%3.3ld,%3.3ld,%3.3ld" : "%ld%3.3ld%3.3ld%3.3ld",
308 (nat)((x)/(ullong)1000000000),
309 (nat)((x)/(ullong)1000000%(ullong)1000),
310 (nat)((x)/(ullong)1000%(ullong)1000),
311 (nat)((x)%(ullong)1000));