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_HOST_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.
92 /* -----------------------------------------------------------------------------
95 Not sure if this belongs here.
96 -------------------------------------------------------------------------- */
101 StackOverflowHook(RtsFlags.GcFlags.maxStkSize * sizeof(W_));
103 #if defined(TICKY_TICKY)
104 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
111 /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
112 OutOfHeapHook(0/*unknown request size*/,
113 RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
115 #if defined(TICKY_TICKY)
116 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
119 stg_exit(EXIT_HEAPOVERFLOW);
122 /* -----------------------------------------------------------------------------
125 Used in addr2Integer because the C compiler on x86 chokes on
126 strlen, trying to inline it with not enough registers available.
127 -------------------------------------------------------------------------- */
129 nat stg_strlen(char *s)
138 /* -----------------------------------------------------------------------------
139 genSym stuff, used by GHC itself for its splitting unique supply.
141 ToDo: put this somewhere sensible.
142 ------------------------------------------------------------------------- */
144 static I_ __GenSymCounter = 0;
149 return(__GenSymCounter++);
152 resetGenSymZh(void) /* it's your funeral */
155 return(__GenSymCounter);
158 /* -----------------------------------------------------------------------------
159 Get the current time as a string. Used in profiling reports.
160 -------------------------------------------------------------------------- */
162 #if defined(PROFILING) || defined(DEBUG) || defined(PAR) || defined(GRAN)
166 static time_t now = 0;
167 static char nowstr[26];
171 strcpy(nowstr, ctime(&now));
172 strcpy(nowstr+16,nowstr+19);
179 /* -----------------------------------------------------------------------------
180 * Reset a file handle to blocking mode. We do this for the standard
181 * file descriptors before exiting, because the shell doesn't always
183 * -------------------------------------------------------------------------- */
185 #if !defined(mingw32_HOST_OS)
187 resetNonBlockingFd(int fd)
191 /* clear the non-blocking flag on this file descriptor */
192 fd_flags = fcntl(fd, F_GETFL);
193 if (fd_flags & O_NONBLOCK) {
194 fcntl(fd, F_SETFL, fd_flags & ~O_NONBLOCK);
199 setNonBlockingFd(int fd)
203 /* clear the non-blocking flag on this file descriptor */
204 fd_flags = fcntl(fd, F_GETFL);
205 if (!(fd_flags & O_NONBLOCK)) {
206 fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
210 /* Stub defns -- async / non-blocking IO is not done
211 * via O_NONBLOCK and select() under Win32.
213 void resetNonBlockingFd(int fd STG_UNUSED) {}
214 void setNonBlockingFd(int fd STG_UNUSED) {}
218 static ullong startTime = 0;
220 /* used in a parallel setup */
224 # if defined(HAVE_GETCLOCK) && !defined(alpha_HOST_ARCH) && !defined(hppa1_1_HOST_ARCH)
227 if (getclock(TIMEOFDAY, &tv) != 0) {
229 fprintf(stderr, "Clock failed\n");
230 stg_exit(EXIT_FAILURE);
232 return tv.tv_sec * LL(1000) + tv.tv_nsec / LL(1000000) - startTime;
233 # elif HAVE_GETTIMEOFDAY && !defined(alpha_HOST_ARCH)
236 if (gettimeofday(&tv, NULL) != 0) {
238 fprintf(stderr, "Clock failed\n");
239 stg_exit(EXIT_FAILURE);
241 return tv.tv_sec * LL(1000) + tv.tv_usec / LL(1000) - startTime;
244 if ((t = time(NULL)) == (time_t) -1) {
246 fprintf(stderr, "Clock failed\n");
247 stg_exit(EXIT_FAILURE);
249 return t * LL(1000) - startTime;
254 /* -----------------------------------------------------------------------------
255 Print large numbers, with punctuation.
256 -------------------------------------------------------------------------- */
259 ullong_format_string(ullong x, char *s, rtsBool with_commas)
261 if (x < (ullong)1000)
262 sprintf(s, "%lu", (lnat)x);
263 else if (x < (ullong)1000000)
264 sprintf(s, (with_commas) ? "%lu,%3.3lu" : "%lu%3.3lu",
265 (lnat)((x)/(ullong)1000),
266 (lnat)((x)%(ullong)1000));
267 else if (x < (ullong)1000000000)
268 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu",
269 (lnat)((x)/(ullong)1000000),
270 (lnat)((x)/(ullong)1000%(ullong)1000),
271 (lnat)((x)%(ullong)1000));
273 sprintf(s, (with_commas) ? "%lu,%3.3lu,%3.3lu,%3.3lu" : "%lu%3.3lu%3.3lu%3.3lu",
274 (lnat)((x)/(ullong)1000000000),
275 (lnat)((x)/(ullong)1000000%(ullong)1000),
276 (lnat)((x)/(ullong)1000%(ullong)1000),
277 (lnat)((x)%(ullong)1000));
282 // Can be used as a breakpoint to set on every heap check failure.
285 heapCheckFail( void )
291 * It seems that pthreads and signals interact oddly in OpenBSD & FreeBSD
292 * pthreads (and possibly others). When linking with -lpthreads, we
293 * have to use pthread_kill to send blockable signals. So use that
294 * when we have a threaded rts. So System.Posix.Signals will call
295 * genericRaise(), rather than raise(3).
297 int genericRaise(int sig) {
298 #if defined(THREADED_RTS) && (defined(openbsd_HOST_OS) || defined(freebsd_HOST_OS))
299 return pthread_kill(pthread_self(), sig);