1 /* -----------------------------------------------------------------------------
2 * $Id: RtsUtils.c,v 1.17 2000/07/17 15:15:40 rrt Exp $
4 * (c) The GHC Team, 1998-1999
6 * General utility functions used in the RTS.
8 * ---------------------------------------------------------------------------*/
10 /* gettimeofday isn't POSIX */
11 #define NON_POSIX_SOURCE
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, nat linenum)
118 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
119 fprintf(stderr, "ASSERTION FAILED: file %s, line %u\n", filename, linenum);
123 /* -----------------------------------------------------------------------------
126 Not sure if this belongs here.
127 -------------------------------------------------------------------------- */
132 StackOverflowHook(RtsFlags.GcFlags.maxStkSize * sizeof(W_));
134 #if defined(TICKY_TICKY)
135 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
142 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
143 OutOfHeapHook(0/*unknown request size*/,
144 RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
146 #if defined(TICKY_TICKY)
147 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
150 stg_exit(EXIT_HEAPOVERFLOW);
153 /* -----------------------------------------------------------------------------
156 Used in addr2Integer because the C compiler on x86 chokes on
157 strlen, trying to inline it with not enough registers available.
158 -------------------------------------------------------------------------- */
160 nat stg_strlen(char *s)
169 /* -----------------------------------------------------------------------------
170 genSym stuff, used by GHC itself for its splitting unique supply.
172 ToDo: put this somewhere sensible.
173 ------------------------------------------------------------------------- */
175 I_ __GenSymCounter = 0;
180 return(__GenSymCounter++);
183 resetGenSymZh(void) /* it's your funeral */
186 return(__GenSymCounter);
189 /* -----------------------------------------------------------------------------
190 Get the current time as a string. Used in profiling reports.
191 -------------------------------------------------------------------------- */
193 #if defined(PROFILING) || defined(DEBUG) || defined(PAR) || defined(GRAN)
197 static time_t now = 0;
198 static char nowstr[26];
202 strcpy(nowstr, ctime(&now));
203 strcpy(nowstr+16,nowstr+19);
210 /* -----------------------------------------------------------------------------
211 * Reset a file handle to blocking mode. We do this for the standard
212 * file descriptors before exiting, because the shell doesn't always
214 * -------------------------------------------------------------------------- */
217 resetNonBlockingFd(int fd)
221 #if !defined(mingw32_TARGET_OS)
222 /* clear the non-blocking flag on this file descriptor */
223 fd_flags = fcntl(fd, F_GETFL);
224 if (fd_flags & O_NONBLOCK) {
225 fcntl(fd, F_SETFL, fd_flags & ~O_NONBLOCK);
230 static ullong startTime = 0;
232 /* used in a parallel setup */
236 # if defined(HAVE_GETCLOCK) && !defined(alpha_TARGET_ARCH) && !defined(hppa1_1_TARGET_ARCH)
239 if (getclock(TIMEOFDAY, &tv) != 0) {
241 fprintf(stderr, "Clock failed\n");
242 stg_exit(EXIT_FAILURE);
244 return tv.tv_sec * LL(1000) + tv.tv_nsec / LL(1000000) - startTime;
245 # elif HAVE_GETTIMEOFDAY && !defined(alpha_TARGET_ARCH)
248 if (gettimeofday(&tv, NULL) != 0) {
250 fprintf(stderr, "Clock failed\n");
251 stg_exit(EXIT_FAILURE);
253 return tv.tv_sec * LL(1000) + tv.tv_usec / LL(1000) - startTime;
256 if ((t = time(NULL)) == (time_t) -1) {
258 fprintf(stderr, "Clock failed\n");
259 stg_exit(EXIT_FAILURE);
261 return t * LL(1000) - startTime;
265 /* -----------------------------------------------------------------------------
266 Print large numbers, with punctuation.
267 -------------------------------------------------------------------------- */
270 ullong_format_string(ullong x, char *s, rtsBool with_commas)
272 if (x < (ullong)1000)
273 sprintf(s, "%d", (nat)x);
274 else if (x < (ullong)1000000)
275 sprintf(s, (with_commas) ? "%ld,%3.3ld" : "%ld%3.3ld",
276 (nat)((x)/(ullong)1000),
277 (nat)((x)%(ullong)1000));
278 else if (x < (ullong)1000000000)
279 sprintf(s, (with_commas) ? "%ld,%3.3ld,%3.3ld" : "%ld%3.3ld%3.3ld",
280 (nat)((x)/(ullong)1000000),
281 (nat)((x)/(ullong)1000%(ullong)1000),
282 (nat)((x)%(ullong)1000));
284 sprintf(s, (with_commas) ? "%ld,%3.3ld,%3.3ld,%3.3ld" : "%ld%3.3ld%3.3ld%3.3ld",
285 (nat)((x)/(ullong)1000000000),
286 (nat)((x)/(ullong)1000000%(ullong)1000),
287 (nat)((x)/(ullong)1000%(ullong)1000),
288 (nat)((x)%(ullong)1000));