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
39 #if defined(THREADED_RTS) && defined(openbsd_TARGET_OS) && defined(HAVE_PTHREAD_H)
43 /* -----------------------------------------------------------------------------
44 Result-checking malloc wrappers.
45 -------------------------------------------------------------------------- */
48 stgMallocBytes (int n, char *msg)
52 if ((space = (char *) malloc((size_t) n)) == NULL) {
53 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
54 MallocFailHook((W_) n, msg); /*msg*/
55 stg_exit(EXIT_INTERNAL_ERROR);
61 stgReallocBytes (void *p, int n, char *msg)
65 if ((space = (char *) realloc(p, (size_t) n)) == NULL) {
66 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
67 MallocFailHook((W_) n, msg); /*msg*/
68 stg_exit(EXIT_INTERNAL_ERROR);
74 stgCallocBytes (int n, int m, char *msg)
78 char* p = stgMallocBytes(sz, msg);
79 for (i = 0; i < sz; i++) p[i] = 0;
83 /* To simplify changing the underlying allocator used
84 * by stgMallocBytes(), provide stgFree() as well.
93 _stgAssert (char *filename, unsigned int linenum)
96 fprintf(stderr, "ASSERTION FAILED: file %s, line %u\n", filename, linenum);
101 /* -----------------------------------------------------------------------------
104 Not sure if this belongs here.
105 -------------------------------------------------------------------------- */
110 StackOverflowHook(RtsFlags.GcFlags.maxStkSize * sizeof(W_));
112 #if defined(TICKY_TICKY)
113 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
120 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
121 OutOfHeapHook(0/*unknown request size*/,
122 RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
124 #if defined(TICKY_TICKY)
125 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
128 stg_exit(EXIT_HEAPOVERFLOW);
131 /* -----------------------------------------------------------------------------
134 Used in addr2Integer because the C compiler on x86 chokes on
135 strlen, trying to inline it with not enough registers available.
136 -------------------------------------------------------------------------- */
138 nat stg_strlen(char *s)
147 /* -----------------------------------------------------------------------------
148 genSym stuff, used by GHC itself for its splitting unique supply.
150 ToDo: put this somewhere sensible.
151 ------------------------------------------------------------------------- */
153 static I_ __GenSymCounter = 0;
158 return(__GenSymCounter++);
161 resetGenSymZh(void) /* it's your funeral */
164 return(__GenSymCounter);
167 /* -----------------------------------------------------------------------------
168 Get the current time as a string. Used in profiling reports.
169 -------------------------------------------------------------------------- */
171 #if defined(PROFILING) || defined(DEBUG) || defined(PAR) || defined(GRAN)
175 static time_t now = 0;
176 static char nowstr[26];
180 strcpy(nowstr, ctime(&now));
181 strcpy(nowstr+16,nowstr+19);
188 /* -----------------------------------------------------------------------------
189 * Reset a file handle to blocking mode. We do this for the standard
190 * file descriptors before exiting, because the shell doesn't always
192 * -------------------------------------------------------------------------- */
194 #if !defined(mingw32_TARGET_OS)
196 resetNonBlockingFd(int fd)
200 /* clear the non-blocking flag on this file descriptor */
201 fd_flags = fcntl(fd, F_GETFL);
202 if (fd_flags & O_NONBLOCK) {
203 fcntl(fd, F_SETFL, fd_flags & ~O_NONBLOCK);
208 setNonBlockingFd(int fd)
212 /* clear the non-blocking flag on this file descriptor */
213 fd_flags = fcntl(fd, F_GETFL);
214 if (!(fd_flags & O_NONBLOCK)) {
215 fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
219 /* Stub defns -- async / non-blocking IO is not done
220 * via O_NONBLOCK and select() under Win32.
222 void resetNonBlockingFd(int fd STG_UNUSED) {}
223 void setNonBlockingFd(int fd STG_UNUSED) {}
227 static ullong startTime = 0;
229 /* used in a parallel setup */
233 # if defined(HAVE_GETCLOCK) && !defined(alpha_TARGET_ARCH) && !defined(hppa1_1_TARGET_ARCH)
236 if (getclock(TIMEOFDAY, &tv) != 0) {
238 fprintf(stderr, "Clock failed\n");
239 stg_exit(EXIT_FAILURE);
241 return tv.tv_sec * LL(1000) + tv.tv_nsec / LL(1000000) - startTime;
242 # elif HAVE_GETTIMEOFDAY && !defined(alpha_TARGET_ARCH)
245 if (gettimeofday(&tv, NULL) != 0) {
247 fprintf(stderr, "Clock failed\n");
248 stg_exit(EXIT_FAILURE);
250 return tv.tv_sec * LL(1000) + tv.tv_usec / LL(1000) - startTime;
253 if ((t = time(NULL)) == (time_t) -1) {
255 fprintf(stderr, "Clock failed\n");
256 stg_exit(EXIT_FAILURE);
258 return t * LL(1000) - startTime;
263 /* -----------------------------------------------------------------------------
264 Print large numbers, with punctuation.
265 -------------------------------------------------------------------------- */
268 ullong_format_string(ullong x, char *s, rtsBool with_commas)
270 if (x < (ullong)1000)
271 sprintf(s, "%lu", (lnat)x);
272 else if (x < (ullong)1000000)
273 sprintf(s, (with_commas) ? "%lu,%3.3lu" : "%lu%3.3lu",
274 (lnat)((x)/(ullong)1000),
275 (lnat)((x)%(ullong)1000));
276 else if (x < (ullong)1000000000)
277 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu",
278 (lnat)((x)/(ullong)1000000),
279 (lnat)((x)/(ullong)1000%(ullong)1000),
280 (lnat)((x)%(ullong)1000));
282 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu%3.3lu",
283 (lnat)((x)/(ullong)1000000000),
284 (lnat)((x)/(ullong)1000000%(ullong)1000),
285 (lnat)((x)/(ullong)1000%(ullong)1000),
286 (lnat)((x)%(ullong)1000));
291 // Can be used as a breakpoint to set on every heap check failure.
294 heapCheckFail( void )
300 * It seems that pthreads and signals interact oddly in OpenBSD & FreeBSD
301 * pthreads (and possibly others). When linking with -lpthreads, we
302 * have to use pthread_kill to send blockable signals. So use that
303 * when we have a threaded rts. So System.Posix.Signals will call
304 * genericRaise(), rather than raise(3).
306 int genericRaise(int sig) {
307 #if defined(THREADED_RTS) && (defined(openbsd_TARGET_OS) || defined(freebsd_TARGET_OS))
308 return pthread_kill(pthread_self(), sig);