RtsUtils.c 7.08 KB
Newer Older
1
/* -----------------------------------------------------------------------------
2
 * $Id: RtsUtils.c,v 1.22 2001/08/29 15:02:02 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
void *
stgCallocBytes (int n, int m, char *msg)
{
118
119
120
121
122
  int   i;
  int   sz = n * m;
  char* p  = stgMallocBytes(sz, msg);
  for (i = 0; i < sz; i++) p[i] = 0;
  return p;
123
124
}

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

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

void
141
stackOverflow(void)
142
{
143
  StackOverflowHook(RtsFlags.GcFlags.maxStkSize * sizeof(W_));
144
145

#if defined(TICKY_TICKY)
146
  if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
147
148
149
150
151
152
#endif
}

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

161
  stg_exit(EXIT_HEAPOVERFLOW);
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
}

/* -----------------------------------------------------------------------------
   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_
189
genSymZh(void)
190
191
192
193
{
    return(__GenSymCounter++);
}
I_
194
resetGenSymZh(void) /* it's your funeral */
195
196
197
198
199
200
201
202
203
{
    __GenSymCounter=0;
    return(__GenSymCounter);
}

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

204
#if defined(PROFILING) || defined(DEBUG) || defined(PAR) || defined(GRAN)
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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

221
222
223
224
225
226
227
228
229
230
231
/* -----------------------------------------------------------------------------
 * 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
232
#if !defined(mingw32_TARGET_OS)
233
234
235
236
237
238
239
240
  /* 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
}

241
242
243
244
245
246
static ullong startTime = 0;

/* used in a parallel setup */
ullong
msTime(void)
{
247
# if defined(HAVE_GETCLOCK) && !defined(alpha_TARGET_ARCH) && !defined(hppa1_1_TARGET_ARCH)
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
    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
}

276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* -----------------------------------------------------------------------------
   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;
}