[project @ 2002-12-11 15:36:20 by simonmar]
[ghc-hetmet.git] / ghc / rts / RtsUtils.c
1 /* -----------------------------------------------------------------------------
2  * $Id: RtsUtils.c,v 1.29 2002/12/11 15:36:48 simonmar Exp $
3  *
4  * (c) The GHC Team, 1998-2002
5  *
6  * General utility functions used in the RTS.
7  *
8  * ---------------------------------------------------------------------------*/
9
10 /* gettimeofday isn't POSIX */
11 /* #include "PosixSource.h" */
12
13 #include "Rts.h"
14 #include "RtsTypes.h"
15 #include "RtsAPI.h"
16 #include "RtsFlags.h"
17 #include "Hooks.h"
18 #include "Main.h"
19 #include "RtsUtils.h"
20 #include "Ticky.h"
21
22 #ifdef HAVE_TIME_H
23 #include <time.h>
24 #endif
25
26 #ifdef HAVE_FCNTL_H
27 #include <fcntl.h>
28 #endif
29
30 #ifdef HAVE_GETTIMEOFDAY
31 #include <sys/time.h>
32 #endif
33
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdarg.h>
37
38 /* variable-argument internal error function. */
39
40 void
41 barf(char *s, ...)
42 {
43   va_list ap;
44   va_start(ap,s);
45   /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
46   if (prog_argv != NULL && prog_argv[0] != NULL) {
47     fprintf(stderr, "%s: internal error: ", prog_argv[0]);
48   } else {
49     fprintf(stderr, "internal error: ");
50   }
51   vfprintf(stderr, s, ap);
52   fprintf(stderr, "\n");
53   fprintf(stderr, "    Please report this as a bug to glasgow-haskell-bugs@haskell.org,\n    or http://www.sourceforge.net/projects/ghc/\n");
54   fflush(stderr);
55   stg_exit(EXIT_INTERNAL_ERROR);
56   va_end(ap);
57 }
58
59 void
60 prog_belch(char *s, ...)
61 {
62   va_list ap;
63   va_start(ap,s);
64   /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
65   if (prog_argv != NULL && prog_argv[0] != NULL) {
66     fprintf(stderr, "%s: ", prog_argv[0]);
67   } 
68   vfprintf(stderr, s, ap);
69   fprintf(stderr, "\n");
70   va_end(ap);
71 }
72
73 void
74 belch(char *s, ...)
75 {
76   va_list ap;
77   va_start(ap,s);
78   /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
79   vfprintf(stderr, s, ap);
80   fprintf(stderr, "\n");
81   va_end(ap);
82 }
83
84 /* result-checking malloc wrappers. */
85
86 void *
87 stgMallocBytes (int n, char *msg)
88 {
89     char *space;
90
91     if ((space = (char *) malloc((size_t) n)) == NULL) {
92       /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
93       MallocFailHook((W_) n, msg); /*msg*/
94       stg_exit(EXIT_INTERNAL_ERROR);
95     }
96     return space;
97 }
98
99 void *
100 stgReallocBytes (void *p, int n, char *msg)
101 {
102     char *space;
103
104     if ((space = (char *) realloc(p, (size_t) n)) == NULL) {
105       /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
106       MallocFailHook((W_) n, msg); /*msg*/
107       stg_exit(EXIT_INTERNAL_ERROR);
108     }
109     return space;
110 }
111
112 void *
113 stgMallocWords (int n, char *msg)
114 {
115   return(stgMallocBytes(n * sizeof(W_), msg));
116 }
117
118 void *
119 stgReallocWords (void *p, int n, char *msg)
120 {
121   return(stgReallocBytes(p, n * sizeof(W_), msg));
122 }
123
124 void *
125 stgCallocBytes (int n, int m, char *msg)
126 {
127   int   i;
128   int   sz = n * m;
129   char* p  = stgMallocBytes(sz, msg);
130   for (i = 0; i < sz; i++) p[i] = 0;
131   return p;
132 }
133
134 void 
135 _stgAssert (char *filename, unsigned int linenum)
136 {
137   fflush(stdout);
138   fprintf(stderr, "ASSERTION FAILED: file %s, line %u\n", filename, linenum);
139   fflush(stderr);
140   abort();
141 }
142
143 /* -----------------------------------------------------------------------------
144    Stack overflow
145    
146    Not sure if this belongs here.
147    -------------------------------------------------------------------------- */
148
149 void
150 stackOverflow(void)
151 {
152   StackOverflowHook(RtsFlags.GcFlags.maxStkSize * sizeof(W_));
153
154 #if defined(TICKY_TICKY)
155   if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
156 #endif
157 }
158
159 void
160 heapOverflow(void)
161 {
162   /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
163   OutOfHeapHook(0/*unknown request size*/, 
164                 RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
165   
166 #if defined(TICKY_TICKY)
167   if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
168 #endif
169
170   stg_exit(EXIT_HEAPOVERFLOW);
171 }
172
173 /* -----------------------------------------------------------------------------
174    Out-of-line strlen.
175
176    Used in addr2Integer because the C compiler on x86 chokes on
177    strlen, trying to inline it with not enough registers available.
178    -------------------------------------------------------------------------- */
179
180 nat stg_strlen(char *s)
181 {
182    char *p = s;
183
184    while (*p) p++;
185    return p-s;
186 }
187
188
189 /* -----------------------------------------------------------------------------
190    genSym stuff, used by GHC itself for its splitting unique supply.
191
192    ToDo: put this somewhere sensible.
193    -------------------------------------------------------------------------  */
194
195 static I_ __GenSymCounter = 0;
196
197 I_
198 genSymZh(void)
199 {
200     return(__GenSymCounter++);
201 }
202 I_
203 resetGenSymZh(void) /* it's your funeral */
204 {
205     __GenSymCounter=0;
206     return(__GenSymCounter);
207 }
208
209 /* -----------------------------------------------------------------------------
210    Get the current time as a string.  Used in profiling reports.
211    -------------------------------------------------------------------------- */
212
213 #if defined(PROFILING) || defined(DEBUG) || defined(PAR) || defined(GRAN)
214 char *
215 time_str(void)
216 {
217     static time_t now = 0;
218     static char nowstr[26];
219
220     if (now == 0) {
221         time(&now);
222         strcpy(nowstr, ctime(&now));
223         strcpy(nowstr+16,nowstr+19);
224         nowstr[21] = '\0';
225     }
226     return nowstr;
227 }
228 #endif
229
230 /* -----------------------------------------------------------------------------
231  * Reset a file handle to blocking mode.  We do this for the standard
232  * file descriptors before exiting, because the shell doesn't always
233  * clean up for us.
234  * -------------------------------------------------------------------------- */
235
236 #if !defined(mingw32_TARGET_OS)
237 void
238 resetNonBlockingFd(int fd)
239 {
240   long fd_flags;
241
242   /* clear the non-blocking flag on this file descriptor */
243   fd_flags = fcntl(fd, F_GETFL);
244   if (fd_flags & O_NONBLOCK) {
245     fcntl(fd, F_SETFL, fd_flags & ~O_NONBLOCK);
246   }
247 }
248
249 void
250 setNonBlockingFd(int fd)
251 {
252   long fd_flags;
253
254   /* clear the non-blocking flag on this file descriptor */
255   fd_flags = fcntl(fd, F_GETFL);
256   fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
257 }
258 #else
259 /* Don't support non-blocking FDs (yet) on mingw */
260 void resetNonBlockingFd(int fd STG_UNUSED) {}
261 void setNonBlockingFd(int fd STG_UNUSED) {}
262 #endif
263
264 static ullong startTime = 0;
265
266 /* used in a parallel setup */
267 ullong
268 msTime(void)
269 {
270 # if defined(HAVE_GETCLOCK) && !defined(alpha_TARGET_ARCH) && !defined(hppa1_1_TARGET_ARCH)
271     struct timespec tv;
272
273     if (getclock(TIMEOFDAY, &tv) != 0) {
274         fflush(stdout);
275         fprintf(stderr, "Clock failed\n");
276         stg_exit(EXIT_FAILURE);
277     }
278     return tv.tv_sec * LL(1000) + tv.tv_nsec / LL(1000000) - startTime;
279 # elif HAVE_GETTIMEOFDAY && !defined(alpha_TARGET_ARCH)
280     struct timeval tv;
281  
282     if (gettimeofday(&tv, NULL) != 0) {
283         fflush(stdout);
284         fprintf(stderr, "Clock failed\n");
285         stg_exit(EXIT_FAILURE);
286     }
287     return tv.tv_sec * LL(1000) + tv.tv_usec / LL(1000) - startTime;
288 # else
289     time_t t;
290     if ((t = time(NULL)) == (time_t) -1) {
291         fflush(stdout);
292         fprintf(stderr, "Clock failed\n");
293         stg_exit(EXIT_FAILURE);
294     }
295     return t * LL(1000) - startTime;
296 # endif
297 }
298
299 /* -----------------------------------------------------------------------------
300    Print large numbers, with punctuation.
301    -------------------------------------------------------------------------- */
302
303 char *
304 ullong_format_string(ullong x, char *s, rtsBool with_commas)
305 {
306     if (x < (ullong)1000) 
307         sprintf(s, "%lu", (lnat)x);
308     else if (x < (ullong)1000000)
309         sprintf(s, (with_commas) ? "%lu,%3.3lu" : "%lu%3.3lu",
310                 (lnat)((x)/(ullong)1000),
311                 (lnat)((x)%(ullong)1000));
312     else if (x < (ullong)1000000000)
313         sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu" :  "%lu%3.3lu%3.3lu",
314                 (lnat)((x)/(ullong)1000000),
315                 (lnat)((x)/(ullong)1000%(ullong)1000),
316                 (lnat)((x)%(ullong)1000));
317     else
318         sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu%3.3lu",
319                 (lnat)((x)/(ullong)1000000000),
320                 (lnat)((x)/(ullong)1000000%(ullong)1000),
321                 (lnat)((x)/(ullong)1000%(ullong)1000), 
322                 (lnat)((x)%(ullong)1000));
323     return s;
324 }
325
326
327 // Can be used as a breakpoint to set on every heap check failure.
328 #ifdef DEBUG
329 void
330 heapCheckFail( void )
331 {
332 }
333 #endif
334