-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
166 lines (155 loc) · 4.07 KB
/
main.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
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "schaf.h"
#include "utils.h"
ATTR(noreturn)
static void usage(FILE *out)
{
fprintf(out, "Usage: schaf [-e <source>] [-pPTMh] <file>\n");
fprintf(out, " -e <source>\tevaluate <source> directly instead of <file>\n");
fprintf(out, " -H <MiB>\tspecify initial heap size\n");
fprintf(out, " -M\t\tprint memory usage (VmHWM) at exit\n");
fprintf(out, " -p\t\tprint last expression in the input\n");
fprintf(out, " -P\t\tonly parse then exit before evaluation. implies -p\n");
fprintf(out, " -S\t\tput stress on GC\n");
fprintf(out, " -T\t\tprint consumed CPU time at exit\n");
fprintf(out, " -h\t\tprint this help\n");
exit(out == stdout ? 0 : 2);
}
ATTR(format(printf, 1, 2))
static void opt_error(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "error: ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
usage(stderr);
}
typedef struct {
const char *path;
const char *script;
bool print;
bool parse_only;
bool cputime;
bool memory;
double init_heap_size_mib;
bool stress_gc;
} Option;
static double parse_posnum(const char *s)
{
char *ep;
double val = strtod(s, &ep);
if (val <= 0 || ep[0] != '\0')
error("invalid positive number '%s'", s);
return val;
}
static Option parse_opt(int argc, char *const *argv)
{
Option o = {
.path = NULL,
.script = NULL,
.print = false,
.parse_only = false,
.cputime = false,
.memory = false,
.init_heap_size_mib = 0.0,
.stress_gc = false,
};
int opt;
while ((opt = getopt(argc, argv, "e:H:MPpSTh")) != -1) {
switch (opt) {
case 'e':
o.script = optarg;
break;
case 'H':
o.init_heap_size_mib = parse_posnum(optarg);
break;
case 'M':
o.memory = true;
break;
case 'P':
o.parse_only = o.print = true;
break;
case 'p':
o.print = true;
break;
case 'S':
o.stress_gc = true;
break;
case 'T':
o.cputime = true;
break;
case 'h':
usage(stdout);
case '?':
default:
usage(stderr);
}
}
o.path = argv[optind];
if (o.path == NULL && o.script == NULL)
opt_error("no program provided");
if (o.path != NULL && o.script != NULL)
opt_error("filename %s given while option '-e' passed", o.path);
return o;
}
static double cputime_ms(void)
{
struct timespec ts;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
return ts.tv_sec * 1000.0 + ts.tv_nsec / (1000.0*1000.0);
}
static void print_cputime(void)
{
fprintf(stderr, "CPU: %.3lf ms\n", cputime_ms());
}
static void print_vmhwm(void)
{
static const char *const path = "/proc/self/status",
*const pat = "VmHWM";
FILE *status = fopen(path, "r");
if (status == NULL)
error("cannot open file %s", path);
char buf[BUFSIZ];
bool printed = false;
while (fgets(buf, sizeof(buf), status) != NULL) {
if (strncmp(buf, pat, strlen(pat)) == 0) {
fprintf(stderr, "%s", buf);
printed = true;
break;
}
}
fclose(status);
if (!printed)
error("memory usage not printed");
}
int main(int argc, char **argv)
{
Option o = parse_opt(argc, argv);
sch_set_gc_stress(o.stress_gc);
if (o.init_heap_size_mib > 0.0)
sch_set_gc_init_size(o.init_heap_size_mib);
SCH_INIT();
Value v;
if (o.parse_only)
v = o.script ? parse_string(o.script) : parse(o.path);
else
v = o.script ? eval_string(o.script) : load(o.path);
if (v == Qundef)
error("%s", error_message());
if (o.print) {
display(v);
printf("\n");
}
if (o.cputime)
print_cputime();
if (o.memory)
print_vmhwm();
return sch_fin();
}