RtsUtils.c 7 KB
Newer Older
1
/* -----------------------------------------------------------------------------
2
 * $Id: RtsUtils.c,v 1.21 2001/08/29 14:55:58 sewardj Exp $
3
4
 *
 * (c) The GHC Team, 1998-1999
5
6
7
8
9
 *
 * General utility functions used in the RTS.
 *
 * ---------------------------------------------------------------------------*/

10
/* gettimeofday isn't POSIX */
11
/* #include "PosixSource.h" */
12

13
#include "Rts.h"
14
#include "RtsTypes.h"
15
16
17
18
19
#include "RtsAPI.h"
#include "RtsFlags.h"
#include "Hooks.h"
#include "Main.h"
#include "RtsUtils.h"
20
#include "Ticky.h"
21
22
23
24
25

#ifdef HAVE_TIME_H
#include <time.h>
#endif

26
27
28
29
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif

30
31
32
33
#ifdef HAVE_GETTIMEOFDAY
#include <sys/time.h>
#endif

34
35
36
37
38
39
40
41
#include <stdarg.h>

/* variable-argument error function. */

void barf(char *s, ...)
{
  va_list ap;
  va_start(ap,s);
42
  /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
43
44
45
46
47
48
49
  if (prog_argv != NULL && prog_argv[0] != NULL) {
    fprintf(stderr, "%s: fatal error: ", prog_argv[0]);
  } else {
    fprintf(stderr, "fatal error: ");
  }
  vfprintf(stderr, s, ap);
  fprintf(stderr, "\n");
sof's avatar
sof committed
50
  fflush(stderr);
51
  stg_exit(EXIT_INTERNAL_ERROR);
52
53
}

54
55
56
57
58
59
60
61
62
63
64
65
void prog_belch(char *s, ...)
{
  va_list ap;
  va_start(ap,s);
  /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
  if (prog_argv != NULL && prog_argv[0] != NULL) {
    fprintf(stderr, "%s: ", prog_argv[0]);
  } 
  vfprintf(stderr, s, ap);
  fprintf(stderr, "\n");
}

66
67
68
69
void belch(char *s, ...)
{
  va_list ap;
  va_start(ap,s);
70
  /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
71
72
73
74
75
76
77
78
79
80
81
82
  vfprintf(stderr, s, ap);
  fprintf(stderr, "\n");
}

/* result-checking malloc wrappers. */

void *
stgMallocBytes (int n, char *msg)
{
    char *space;

    if ((space = (char *) malloc((size_t) n)) == NULL) {
83
      /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
84
85
      MallocFailHook((W_) n, msg); /*msg*/
      stg_exit(EXIT_INTERNAL_ERROR);
86
87
88
89
90
91
92
93
94
95
    }
    return space;
}

void *
stgReallocBytes (void *p, int n, char *msg)
{
    char *space;

    if ((space = (char *) realloc(p, (size_t) n)) == NULL) {
96
      /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
97
98
      MallocFailHook((W_) n, msg); /*msg*/
      stg_exit(EXIT_INTERNAL_ERROR);
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    }
    return space;
}

void *
stgMallocWords (int n, char *msg)
{
  return(stgMallocBytes(n * sizeof(W_), msg));
}

void *
stgReallocWords (void *p, int n, char *msg)
{
  return(stgReallocBytes(p, n * sizeof(W_), msg));
}

115
116
117
118
119
120
void *
stgCallocBytes (int n, int m, char *msg)
{
  return stgMallocBytes(n*m, msg);
}

121
void 
ken's avatar
ken committed
122
_stgAssert (char *filename, unsigned int linenum)
123
{
rrt's avatar
rrt committed
124
  fflush(stdout);
125
  fprintf(stderr, "ASSERTION FAILED: file %s, line %u\n", filename, linenum);
rrt's avatar
rrt committed
126
  fflush(stderr);
127
128
129
130
131
132
133
134
135
136
  abort();
}

/* -----------------------------------------------------------------------------
   Stack overflow
   
   Not sure if this belongs here.
   -------------------------------------------------------------------------- */

void
137
stackOverflow(void)
138
{
139
  StackOverflowHook(RtsFlags.GcFlags.maxStkSize * sizeof(W_));
140
141

#if defined(TICKY_TICKY)
142
  if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
143
144
145
146
147
148
#endif
}

void
heapOverflow(void)
{
149
150
151
152
  /* don't fflush(stdout); WORKAROUND bug in Linux glibc */
  OutOfHeapHook(0/*unknown request size*/, 
		RtsFlags.GcFlags.maxHeapSize * BLOCK_SIZE);
  
153
#if defined(TICKY_TICKY)
154
  if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
155
156
#endif

157
  stg_exit(EXIT_HEAPOVERFLOW);
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
}

/* -----------------------------------------------------------------------------
   Out-of-line strlen.

   Used in addr2Integer because the C compiler on x86 chokes on
   strlen, trying to inline it with not enough registers available.
   -------------------------------------------------------------------------- */

nat stg_strlen(char *s)
{
   char *p = s;

   while (*p) p++;
   return p-s;
}


/* -----------------------------------------------------------------------------
   genSym stuff, used by GHC itself for its splitting unique supply.

   ToDo: put this somewhere sensible.
   -------------------------------------------------------------------------  */

I_ __GenSymCounter = 0;

I_
185
genSymZh(void)
186
187
188
189
{
    return(__GenSymCounter++);
}
I_
190
resetGenSymZh(void) /* it's your funeral */
191
192
193
194
195
196
197
198
199
{
    __GenSymCounter=0;
    return(__GenSymCounter);
}

/* -----------------------------------------------------------------------------
   Get the current time as a string.  Used in profiling reports.
   -------------------------------------------------------------------------- */

200
#if defined(PROFILING) || defined(DEBUG) || defined(PAR) || defined(GRAN)
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
char *
time_str(void)
{
    static time_t now = 0;
    static char nowstr[26];

    if (now == 0) {
	time(&now);
	strcpy(nowstr, ctime(&now));
	strcpy(nowstr+16,nowstr+19);
	nowstr[21] = '\0';
    }
    return nowstr;
}
#endif

217
218
219
220
221
222
223
224
225
226
227
/* -----------------------------------------------------------------------------
 * Reset a file handle to blocking mode.  We do this for the standard
 * file descriptors before exiting, because the shell doesn't always
 * clean up for us.
 * -------------------------------------------------------------------------- */

void
resetNonBlockingFd(int fd)
{
  long fd_flags;

rrt's avatar
rrt committed
228
#if !defined(mingw32_TARGET_OS)
229
230
231
232
233
234
235
236
  /* clear the non-blocking flag on this file descriptor */
  fd_flags = fcntl(fd, F_GETFL);
  if (fd_flags & O_NONBLOCK) {
    fcntl(fd, F_SETFL, fd_flags & ~O_NONBLOCK);
  }
#endif
}

237
238
239
240
241
242
static ullong startTime = 0;

/* used in a parallel setup */
ullong
msTime(void)
{
243
# if defined(HAVE_GETCLOCK) && !defined(alpha_TARGET_ARCH) && !defined(hppa1_1_TARGET_ARCH)
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
    struct timespec tv;

    if (getclock(TIMEOFDAY, &tv) != 0) {
	fflush(stdout);
	fprintf(stderr, "Clock failed\n");
	stg_exit(EXIT_FAILURE);
    }
    return tv.tv_sec * LL(1000) + tv.tv_nsec / LL(1000000) - startTime;
# elif HAVE_GETTIMEOFDAY && !defined(alpha_TARGET_ARCH)
    struct timeval tv;
 
    if (gettimeofday(&tv, NULL) != 0) {
	fflush(stdout);
	fprintf(stderr, "Clock failed\n");
	stg_exit(EXIT_FAILURE);
    }
    return tv.tv_sec * LL(1000) + tv.tv_usec / LL(1000) - startTime;
# else
    time_t t;
    if ((t = time(NULL)) == (time_t) -1) {
	fflush(stdout);
	fprintf(stderr, "Clock failed\n");
	stg_exit(EXIT_FAILURE);
    }
    return t * LL(1000) - startTime;
# endif
}

272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* -----------------------------------------------------------------------------
   Print large numbers, with punctuation.
   -------------------------------------------------------------------------- */

char *
ullong_format_string(ullong x, char *s, rtsBool with_commas)
{
    if (x < (ullong)1000) 
	sprintf(s, "%d", (nat)x);
    else if (x < (ullong)1000000)
	sprintf(s, (with_commas) ? "%ld,%3.3ld" : "%ld%3.3ld",
		(nat)((x)/(ullong)1000),
		(nat)((x)%(ullong)1000));
    else if (x < (ullong)1000000000)
	sprintf(s, (with_commas) ? "%ld,%3.3ld,%3.3ld" :  "%ld%3.3ld%3.3ld",
		(nat)((x)/(ullong)1000000),
		(nat)((x)/(ullong)1000%(ullong)1000),
		(nat)((x)%(ullong)1000));
    else
	sprintf(s, (with_commas) ? "%ld,%3.3ld,%3.3ld,%3.3ld" : "%ld%3.3ld%3.3ld%3.3ld",
		(nat)((x)/(ullong)1000000000),
		(nat)((x)/(ullong)1000000%(ullong)1000),
		(nat)((x)/(ullong)1000%(ullong)1000), 
		(nat)((x)%(ullong)1000));
    return s;
}