forked from sysprog21/lab0-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.c
379 lines (335 loc) · 8.98 KB
/
report.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include "report.h"
#define MAX(a, b) ((a) < (b) ? (b) : (a))
FILE *errfile = NULL;
FILE *verbfile = NULL;
FILE *logfile = NULL;
int verblevel = 0;
void init_files(FILE *efile, FILE *vfile)
{
errfile = efile;
verbfile = vfile;
}
static char fail_buf[1024] = "FATAL Error. Exiting\n";
volatile int rval = 0;
/* Default fatal function */
void default_fatal_fun()
{
/*
sprintf(fail_buf, "FATAL. Memory: allocated = %.3f GB, resident = %.3f
GB\n",
gigabytes(current_bytes), gigabytes(resident_bytes()));
*/
rval = write(STDOUT_FILENO, fail_buf, strlen(fail_buf) + 1);
if (logfile)
fputs(fail_buf, logfile);
}
/* Optional function to call when fatal error encountered */
void (*fatal_fun)() = default_fatal_fun;
void set_verblevel(int level)
{
verblevel = level;
}
bool set_logfile(char *file_name)
{
logfile = fopen(file_name, "w");
return logfile != NULL;
}
void report_event(message_t msg, char *fmt, ...)
{
va_list ap;
bool fatal = msg == MSG_FATAL;
char *msg_name = msg == MSG_WARN
? "WARNING"
: msg == MSG_ERROR ? "ERROR" : "FATAL ERROR";
int level = msg == MSG_WARN ? 2 : msg == MSG_ERROR ? 1 : 0;
if (verblevel < level)
return;
if (!errfile)
init_files(stdout, stdout);
va_start(ap, fmt);
fprintf(errfile, "%s: ", msg_name);
vfprintf(errfile, fmt, ap);
fprintf(errfile, "\n");
fflush(errfile);
va_end(ap);
if (logfile) {
va_start(ap, fmt);
fprintf(logfile, "Error: ");
vfprintf(logfile, fmt, ap);
fprintf(logfile, "\n");
fflush(logfile);
va_end(ap);
fclose(logfile);
}
if (fatal) {
if (fatal_fun)
fatal_fun();
exit(1);
}
}
void report(int level, char *fmt, ...)
{
va_list ap;
if (!verbfile)
init_files(stdout, stdout);
if (level <= verblevel) {
va_start(ap, fmt);
vfprintf(verbfile, fmt, ap);
fprintf(verbfile, "\n");
fflush(verbfile);
va_end(ap);
if (logfile) {
va_start(ap, fmt);
vfprintf(logfile, fmt, ap);
fprintf(logfile, "\n");
fflush(logfile);
va_end(ap);
}
}
}
void report_noreturn(int level, char *fmt, ...)
{
va_list ap;
if (!verbfile)
init_files(stdout, stdout);
if (level <= verblevel) {
va_start(ap, fmt);
vfprintf(verbfile, fmt, ap);
fflush(verbfile);
va_end(ap);
if (logfile) {
va_start(ap, fmt);
vfprintf(logfile, fmt, ap);
fflush(logfile);
va_end(ap);
}
}
}
void safe_report(int level, char *msg)
{
if (level > verblevel)
return;
if (!errfile)
init_files(stdout, stdout);
fputs(msg, errfile);
if (logfile) {
fputs(msg, logfile);
}
}
/* Functions denoting failures */
/* General failure */
/* Need to be able to print without using malloc */
void fail_fun(char *format, char *msg)
{
sprintf(fail_buf, format, msg);
/* Tack on return */
fail_buf[strlen(fail_buf)] = '\n';
/* Use write to avoid any buffering issues */
rval = write(STDOUT_FILENO, fail_buf, strlen(fail_buf) + 1);
if (logfile) {
/* Don't know file descriptor for logfile */
fputs(fail_buf, logfile);
}
if (fatal_fun)
fatal_fun();
if (logfile)
fclose(logfile);
exit(1);
}
/* Maximum number of megabytes that application can use (0 = unlimited) */
int mblimit = 0;
/* Maximum number of seconds that application can use. (0 = unlimited) */
int timelimit = 0;
/* Keeping track of memory allocation */
static size_t allocate_cnt = 0;
static size_t allocate_bytes = 0;
static size_t free_cnt = 0;
static size_t free_bytes = 0;
/* These are externally visible */
size_t peak_bytes = 0;
size_t last_peak_bytes = 0;
size_t current_bytes = 0;
static void check_exceed(size_t new_bytes)
{
size_t limit_bytes = (size_t) mblimit << 20;
size_t request_bytes = new_bytes + current_bytes;
if (mblimit > 0 && request_bytes > limit_bytes) {
report_event(MSG_FATAL,
"Exceeded memory limit of %u megabytes with %lu bytes",
mblimit, request_bytes);
}
}
/* Call malloc & exit if fails */
void *malloc_or_fail(size_t bytes, char *fun_name)
{
check_exceed(bytes);
void *p = malloc(bytes);
if (!p) {
fail_fun("Malloc returned NULL in %s", fun_name);
return NULL;
}
allocate_cnt++;
allocate_bytes += bytes;
current_bytes += bytes;
peak_bytes = MAX(peak_bytes, current_bytes);
last_peak_bytes = MAX(last_peak_bytes, current_bytes);
return p;
}
/* Call calloc returns NULL & exit if fails */
void *calloc_or_fail(size_t cnt, size_t bytes, char *fun_name)
{
check_exceed(cnt * bytes);
void *p = calloc(cnt, bytes);
if (!p) {
fail_fun("Calloc returned NULL in %s", fun_name);
return NULL;
}
allocate_cnt++;
allocate_bytes += cnt * bytes;
current_bytes += cnt * bytes;
peak_bytes = MAX(peak_bytes, current_bytes);
last_peak_bytes = MAX(last_peak_bytes, current_bytes);
return p;
}
/* Call realloc returns NULL & exit if fails.
Require explicit indication of current allocation */
void *realloc_or_fail(void *old,
size_t old_bytes,
size_t new_bytes,
char *fun_name)
{
if (new_bytes > old_bytes)
check_exceed(new_bytes - old_bytes);
void *p = realloc(old, new_bytes);
if (!p) {
fail_fun("Realloc returned NULL in %s", fun_name);
return NULL;
}
allocate_cnt++;
allocate_bytes += new_bytes;
current_bytes += (new_bytes - old_bytes);
peak_bytes = MAX(peak_bytes, current_bytes);
last_peak_bytes = MAX(last_peak_bytes, current_bytes);
free_cnt++;
free_bytes += old_bytes;
return p;
}
char *strsave_or_fail(char *s, char *fun_name)
{
if (!s)
return NULL;
size_t len = strlen(s);
check_exceed(len + 1);
char *ss = malloc(len + 1);
if (!ss) {
fail_fun("strsave failed in %s", fun_name);
}
allocate_cnt++;
allocate_bytes += len + 1;
current_bytes += len + 1;
peak_bytes = MAX(peak_bytes, current_bytes);
last_peak_bytes = MAX(last_peak_bytes, current_bytes);
return strcpy(ss, s);
}
/* Free block, as from malloc, realloc, or strsave */
void free_block(void *b, size_t bytes)
{
if (b == NULL) {
report_event(MSG_ERROR, "Attempting to free null block");
}
free(b);
free_cnt++;
free_bytes += bytes;
current_bytes -= bytes;
}
/* Free array, as from calloc */
void free_array(void *b, size_t cnt, size_t bytes)
{
if (b == NULL) {
report_event(MSG_ERROR, "Attempting to free null block");
}
free(b);
free_cnt++;
free_bytes += cnt * bytes;
current_bytes -= cnt * bytes;
}
/* Free string saved by strsave_or_fail */
void free_string(char *s)
{
if (s == NULL) {
report_event(MSG_ERROR, "Attempting to free null block");
}
free_block((void *) s, strlen(s) + 1);
}
/* Report current allocation status */
void mem_status(FILE *fp)
{
fprintf(fp,
"Allocated cnt/bytes: %lu/%lu. Freed cnt/bytes: %lu/%lu.\n"
" Peak bytes %lu, Last peak bytes %ld, Current bytes %ld\n",
(long unsigned) allocate_cnt, (long unsigned) allocate_bytes,
(long unsigned) free_cnt, (long unsigned) free_bytes,
(long unsigned) peak_bytes, (long unsigned) last_peak_bytes,
(long unsigned) current_bytes);
}
/* Initialization of timers */
void init_time(double *timep)
{
(void) delta_time(timep);
}
double delta_time(double *timep)
{
struct timeval tv;
gettimeofday(&tv, NULL);
double current_time = tv.tv_sec + 1.0E-6 * tv.tv_usec;
double delta = current_time - *timep;
*timep = current_time;
return delta;
}
/* Number of bytes resident in physical memory */
size_t resident_bytes()
{
struct rusage r;
size_t mem = 0;
int code = getrusage(RUSAGE_SELF, &r);
if (code < 0) {
report_event(MSG_ERROR, "Call to getrusage failed");
} else {
mem = r.ru_maxrss * 1024;
}
return mem;
}
double gigabytes(size_t n)
{
return (double) n / (1UL << 30);
}
void reset_peak_bytes()
{
last_peak_bytes = current_bytes;
}
#if 0
static char timeout_buf[256];
void sigalrmhandler(int sig) {
safe_report(true, timeout_buf);
}
void change_timeout(int oldval) {
sprintf(timeout_buf, "Program timed out after %d seconds\n", timelimit);
/* alarm function will correctly cancel existing alarms */
signal(SIGALRM, sigalrmhandler);
alarm(timelimit);
}
/* Handler for SIGTERM signals */
void sigterm_handler(int sig) {
safe_report(true, "SIGTERM signal received");
}
#endif