1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2004
5 * General utility functions used in the RTS.
7 * ---------------------------------------------------------------------------*/
9 /* gettimeofday isn't POSIX */
10 /* #include "PosixSource.h" */
26 #ifdef HAVE_GETTIMEOFDAY
35 /* -----------------------------------------------------------------------------
36 Result-checking malloc wrappers.
37 -------------------------------------------------------------------------- */
40 stgMallocBytes (int n, char *msg)
44 if ((space = (char *) malloc((size_t) n)) == NULL) {
45 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
46 MallocFailHook((W_) n, msg); /*msg*/
47 stg_exit(EXIT_INTERNAL_ERROR);
53 stgReallocBytes (void *p, int n, char *msg)
57 if ((space = (char *) realloc(p, (size_t) n)) == NULL) {
58 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
59 MallocFailHook((W_) n, msg); /*msg*/
60 stg_exit(EXIT_INTERNAL_ERROR);
66 stgCallocBytes (int n, int m, char *msg)
70 char* p = stgMallocBytes(sz, msg);
71 for (i = 0; i < sz; i++) p[i] = 0;
75 /* To simplify changing the underlying allocator used
76 * by stgMallocBytes(), provide stgFree() as well.
85 _stgAssert (char *filename, unsigned int linenum)
88 fprintf(stderr, "ASSERTION FAILED: file %s, line %u\n", filename, linenum);
93 /* -----------------------------------------------------------------------------
96 Not sure if this belongs here.
97 -------------------------------------------------------------------------- */
102 StackOverflowHook(RtsFlags.GcFlags.maxStkSize * sizeof(W_));
104 #if defined(TICKY_TICKY)
105 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
112 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
113 OutOfHeapHook(0/*unknown request size*/,
114 RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
116 #if defined(TICKY_TICKY)
117 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
120 stg_exit(EXIT_HEAPOVERFLOW);
123 /* -----------------------------------------------------------------------------
126 Used in addr2Integer because the C compiler on x86 chokes on
127 strlen, trying to inline it with not enough registers available.
128 -------------------------------------------------------------------------- */
130 nat stg_strlen(char *s)
139 /* -----------------------------------------------------------------------------
140 genSym stuff, used by GHC itself for its splitting unique supply.
142 ToDo: put this somewhere sensible.
143 ------------------------------------------------------------------------- */
145 static I_ __GenSymCounter = 0;
150 return(__GenSymCounter++);
153 resetGenSymZh(void) /* it's your funeral */
156 return(__GenSymCounter);
159 /* -----------------------------------------------------------------------------
160 Get the current time as a string. Used in profiling reports.
161 -------------------------------------------------------------------------- */
163 #if defined(PROFILING) || defined(DEBUG) || defined(PAR) || defined(GRAN)
167 static time_t now = 0;
168 static char nowstr[26];
172 strcpy(nowstr, ctime(&now));
173 strcpy(nowstr+16,nowstr+19);
180 /* -----------------------------------------------------------------------------
181 * Reset a file handle to blocking mode. We do this for the standard
182 * file descriptors before exiting, because the shell doesn't always
184 * -------------------------------------------------------------------------- */
186 #if !defined(mingw32_TARGET_OS)
188 resetNonBlockingFd(int fd)
192 /* clear the non-blocking flag on this file descriptor */
193 fd_flags = fcntl(fd, F_GETFL);
194 if (fd_flags & O_NONBLOCK) {
195 fcntl(fd, F_SETFL, fd_flags & ~O_NONBLOCK);
200 setNonBlockingFd(int fd)
204 /* clear the non-blocking flag on this file descriptor */
205 fd_flags = fcntl(fd, F_GETFL);
206 if (!(fd_flags & O_NONBLOCK)) {
207 fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
211 /* Stub defns -- async / non-blocking IO is not done
212 * via O_NONBLOCK and select() under Win32.
214 void resetNonBlockingFd(int fd STG_UNUSED) {}
215 void setNonBlockingFd(int fd STG_UNUSED) {}
219 static ullong startTime = 0;
221 /* used in a parallel setup */
225 # if defined(HAVE_GETCLOCK) && !defined(alpha_TARGET_ARCH) && !defined(hppa1_1_TARGET_ARCH)
228 if (getclock(TIMEOFDAY, &tv) != 0) {
230 fprintf(stderr, "Clock failed\n");
231 stg_exit(EXIT_FAILURE);
233 return tv.tv_sec * LL(1000) + tv.tv_nsec / LL(1000000) - startTime;
234 # elif HAVE_GETTIMEOFDAY && !defined(alpha_TARGET_ARCH)
237 if (gettimeofday(&tv, NULL) != 0) {
239 fprintf(stderr, "Clock failed\n");
240 stg_exit(EXIT_FAILURE);
242 return tv.tv_sec * LL(1000) + tv.tv_usec / LL(1000) - startTime;
245 if ((t = time(NULL)) == (time_t) -1) {
247 fprintf(stderr, "Clock failed\n");
248 stg_exit(EXIT_FAILURE);
250 return t * LL(1000) - startTime;
255 /* -----------------------------------------------------------------------------
256 Print large numbers, with punctuation.
257 -------------------------------------------------------------------------- */
260 ullong_format_string(ullong x, char *s, rtsBool with_commas)
262 if (x < (ullong)1000)
263 sprintf(s, "%lu", (lnat)x);
264 else if (x < (ullong)1000000)
265 sprintf(s, (with_commas) ? "%lu,%3.3lu" : "%lu%3.3lu",
266 (lnat)((x)/(ullong)1000),
267 (lnat)((x)%(ullong)1000));
268 else if (x < (ullong)1000000000)
269 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu",
270 (lnat)((x)/(ullong)1000000),
271 (lnat)((x)/(ullong)1000%(ullong)1000),
272 (lnat)((x)%(ullong)1000));
274 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu%3.3lu",
275 (lnat)((x)/(ullong)1000000000),
276 (lnat)((x)/(ullong)1000000%(ullong)1000),
277 (lnat)((x)/(ullong)1000%(ullong)1000),
278 (lnat)((x)%(ullong)1000));
283 // Can be used as a breakpoint to set on every heap check failure.
286 heapCheckFail( void )