-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
524 lines (486 loc) · 10.6 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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#define E_NEEDS_SOCKET
#define E_NEEDS_SIGNAL
#include "system.h"
#include "vm_exp.h"
#include "types.h"
#include "iisc.h"
#include "trace.h"
#include "assert.h"
#include "trace.h"
#include "iset.h"
#include "globals.h"
#include "misc.h"
#include "read.h"
#include "io.h"
#include "dist.h"
#include "gaggle.h"
#include "mqueue.h"
#include "timer.h"
extern MQueue incoming;
ISet allProcesses;
ISet running;
extern int traceprocess;
#include "squeue.h"
SQueue ready;
extern char *gRootNode;
int doDistribution = 0;
#ifdef PROFILEINVOKES
IISc invokeprofile, opvectorentry2ct;
#endif
int beVerbose = 0, doInvokeProfiling = 0, dontExecute = 0;
int activelyInitialize = 1;
extern int errno;
int doCompression = 0, writeCP = 0;
int runningProcesses = 0;
int debugInteractively = 0;
int gotsigint = 0;
int checkSameUser = 1;
int offsetbyuserid = 1;
int fakeUnavailable = 0;
int debugFirst = 0;
extern int checkpointBuiltins;
extern void statistics(void);
void die(void)
{
statistics();
TraceFin();
exit(1);
}
#if !defined(EMERALD_MYRINET)
void sigdie(int signalnumber)
{
#ifdef SINGLESTEP
if (debugInteractively) {
if (ISetSize(running) > 0) {
if (instructionsToExecute != 1) {
instructionsToExecute = 1;
gotsigint = 1;
}
} else if (ISetSize(allProcesses) > 0) {
debug((State *)ISetSelect(allProcesses), "Interrupt");
} else {
fprintf(stderr, "Can't debug (no processes)\n");
beVerbose = 1;
die();
}
} else {
#endif
beVerbose = 1;
showAllProcesses(0, 1);
die();
#ifdef SINGLESTEP
}
#endif
}
#endif
extern int p_bytesPerGeneration, p_copyCount, p_old_size,
p_guaranteeInterGcInterval;
extern int bytesPerGeneration, copyCount, old_size;
extern void gc_init(void (*)(void), void (*)(void), void (*)(void), void (*)(void));
extern void gc_stats (int *tg, int *og, int *n, int *no, int *nd, int *ndr);
extern void init_nodeinfo(void), sysinit(void);
extern int parseTraceFlag(char *), init_upcall(void), interpret(struct State *);
int ac;
char **av;
void startProcesses(void)
{
IISc temp;
int why;
Object anElem;
if (!processes) return;
temp = processes;
processes = 0;
while (IIScSize(temp) > 0) {
anElem = (Object) IIScSelect(temp, &why);
run(anElem, why, 1);
IIScDelete(temp, (int)anElem);
}
IIScDestroy(temp);
{
extern int activelyInitialize;
extern IISc tobeinitialized;
int value;
Object o;
if (activelyInitialize && tobeinitialized) {
while (IIScSize(tobeinitialized) > 0) {
o = (Object)IIScSelect(tobeinitialized, &value);
tryToInit(o);
IIScDelete(tobeinitialized, (int)o);
}
IIScDestroy(tobeinitialized);
tobeinitialized = 0;
}
}
}
void processEverythingOnce(void)
{
State *r;
extern int timeAdvanced, inDebugger;
checkForIO(MQueueSize(incoming) == 0 && SQueueSize(ready) == 0 ? 1 : 0);
if (timeAdvanced) { timeAdvanced = 0 ; checkForTimeouts(); }
#ifdef DISTRIBUTED
if (MQueueSize(incoming) > 0) serveRequest();
#endif
if (SQueueSize(ready) > 0 && !inDebugger) {
r = SQueueRemove(ready);
assert(r);
TRACE(process, 3, ("Running process %x", r));
ISetInsert(running, (int)r);
interpret(r);
ISetDelete(running, (int)r);
}
}
extern int nProcessesDelayed;
int stuffToDo(void)
{
return SQueueSize(ready) > 0 || nProcessesDelayed > 0;
}
void processEverything(void)
{
while (doDistribution ? 1 : stuffToDo()) {
processEverythingOnce();
}
}
#ifdef DISTRIBUTED
int nMessagesSent, nMessagesReceived, nBytesSent, nBytesReceived;
#endif
void makeReady(State *state)
{
assert((int)state != -1);
if (RESDNT(state->op->flags)) {
SQueueInsert(ready, state);
#if DISTRIBUTED
} else {
returnToForeignObject(state, JNIL);
#endif
}
}
int isReady(State *state)
{
State *astate;
assert((int)state != -1);
SQueueForEach(ready, astate) {
if (astate == state) return 1;
} SQueueNext();
return 0;
}
void hack_mainp(void *arg)
{
int doDefault = 1, x, value;
assert(sizeof(Bits32) == 4);
assert(sizeof(Bits16) == 2);
assert(sizeof(Bits8) == 1);
assert(sizeof(void *) >= 4);
ready = SQueueCreate();
start_times();
CallInit();
ReadInit();
OIDToObjectInit();
#ifdef DISTRIBUTED
initGaggle();
#endif
allProcesses = ISetCreate();
running = ISetCreate();
ac--;
av++;
while (ac > 0) {
if (av[0][0] == '-') {
x = 1;
value = 1;
if (av[0][x] == '-') {
value = ! value;
x++;
}
switch (av[0][x]) {
case '1':
activelyInitialize = value;
break;
case 'f':
++x;
value = mstrtol(&av[0][x], 0, 0);
fakeUnavailable = value;
if (fakeUnavailable < 1) fakeUnavailable = 1;
if (fakeUnavailable > 100) fakeUnavailable = 100;
break;
case 'F':
SetTraceFile(&av[0][x+1]);
break;
case 'T':
parseTraceFlag(av[0]);
break;
case 'v':
beVerbose = value;
break;
case 'x':
dontExecute = value;
break;
case 'P':
p_guaranteeInterGcInterval = value;
break;
case 'p':
#ifdef PROFILEINVOKES
if (invokeprofile == NULL)
invokeprofile = IIScCreate();
if (opvectorentry2ct == NULL)
opvectorentry2ct = IIScCreate();
doInvokeProfiling = value;
#else
ftrace("Sorry, not configured for profiling");
#endif
break;
case 'b':
checkpointBuiltins = value;
break;
case 'B':
doDefault = ! value;
break;
case 'c':
doCompression = value;
break;
case 't':
x++;
if (av[0][x]) {
char *rest;
int t = mstrtol(&av[0][x], &rest, 0);
if (*rest == 'k' || *rest == 'K') t *= 1024;
if (*rest == 'm' || *rest == 'M') t *= 1024 * 1024;
SetTraceBufferSize(t);
}
break;
case 'g':
x++;
if (av[0][x]) {
char *rest;
int t = mstrtol(&av[0][x], &rest, 0);
if (*rest == 'k' || *rest == 'K') t *= 1024;
if (*rest == 'm' || *rest == 'M') t *= 1024 * 1024;
p_bytesPerGeneration = (t + 3) & ~3;
}
break;
case 's':
x++;
if (av[0][x]) {
char *rest;
int t = mstrtol(&av[0][x], &rest, 0);
if (*rest == 'k' || *rest == 'K') t *= 1024;
if (*rest == 'm' || *rest == 'M') t *= 1024 * 1024;
stackSize = (t + 1023) & ~1023;
}
break;
case 'O':
x++;
if (av[0][x]) {
char *rest;
int t = mstrtol(&av[0][x], &rest, 0);
if (*rest == 'k' || *rest == 'K') t *= 1024;
if (*rest == 'm' || *rest == 'M') t *= 1024 * 1024;
p_old_size = t;
}
break;
case 'G':
x++;
if (av[0][x]) {
char *rest;
int t = mstrtol(&av[0][x], &rest, 0);
p_copyCount = t;
}
break;
case 'i':
debugInteractively = value;
break;
case 'I':
debugFirst = value;
if (value) debugInteractively = 1;
break;
case 'u':
offsetbyuserid = value;
break;
case 'U':
checkSameUser = 0;
break;
case 'C':
if (doDefault) {
gc_init(0, 0, 0, 0);
DoCheckpointFromFile(0);
doDefault = 0;
}
writeCP = value;
doCompression = 1;
dontExecute = 1;
beVerbose = 1;
break;
case 'S':
#if defined(DISTRIBUTED)
doDistribution = 1;
gRootNode = "search";
#else
fprintf(stderr, "emx: not compiled for distribution (DISTRIBUTED)\n");
#endif
break;
case 'R':
#if defined(DISTRIBUTED)
doDistribution = 1;
x++;
if (av[0][x]) {
gRootNode = &av[0][x];
} else {
gRootNode = "here";
}
#else
fprintf(stderr, "emx: not compiled for distribution (DISTRIBUTED)\n");
#endif
break;
default:
ftrace("emx: Unknown flag \"%s\"", av[0]);
}
} else if ((av[0][0] == ' ' && av[0][1] == '\0') ||
(av[0][0] == ' ' && av[0][1] == ' ' && av[0][2] == '\0')) {
/* ignore it */
} else {
if (doDefault) {
gc_init(0, 0, 0, 0);
DoCheckpointFromFile(0);
doDefault = 0;
}
gc_init(0, 0, 0, 0);
DoCheckpointFromFile(av[0]);
}
ac--;
av++;
}
if (doDefault) {
gc_init(0, 0, 0, 0);
DoCheckpointFromFile(0);
doDefault = 0;
}
if (dontExecute) { return; }
initGlobals();
init_nodeinfo();
init_upcall();
sysinit();
runningProcesses = 1;
startProcesses();
/*
* Don't actively initialize moved in objects, this is only interesting
* for initially reading the checkpoint file.
*/
activelyInitialize = 0;
receivingObjects = 1;
processEverything();
statistics();
TraceFin();
exit(0);
}
int main( int argc, char **argv )
{
#ifdef alpha
/* Try to get these right */
int i;
ac = argc;
av = vmCalloc(ac, sizeof(char *));
for (i = 0; i < ac; i++) {
av[i] = argv[i];
}
#else
ac = argc; av = argv;
#endif
srandom(time(0));
#ifdef WIN32
{
WORD wVersionRequested = MAKEWORD(1, 1);
WSADATA wsaData;
if (WSAStartup(wVersionRequested, &wsaData)) {
fprintf(stderr, "WSA Startup failed.\n");
exit(1);
}
}
#endif /* WIN32 */
TraceInit();
DInit();
IOInit();
if( InitDist() < 0 ) abort();
TimerInit();
hack_mainp(0);
return 0;
}
IISc processes;
void scheduleProcess(Object o, int opIndex)
{
if (runningProcesses) {
run(o, opIndex, 1);
} else {
if (!(int)processes) processes = IIScCreate();
IIScInsert(processes, (int)o, opIndex);
}
}
#if defined(_SC_PAGE_SIZE) && !defined(ibm) && !defined(sgi) && !defined(alpha)
size_t getpagesize(void)
{
return sysconf(_SC_PAGE_SIZE);
}
#endif
#if defined(m88k)
getpagesize()
{
return 4096;
}
#endif
void statistics(void)
{
if (beVerbose) {
int tg, og, time;
int n, no, nd, ndremoved;
extern long long totalbytecodes;
time = currentCpuTime();
printf("Executed %lld bytecodes in %d.%02d seconds\n",
totalbytecodes, time/100, time%100);
gc_stats(&tg, &og, &n, &no, &nd, &ndremoved);
if (n > 0) {
if (bytesPerGeneration % 1024 == 0) {
printf("%d gcs in %d*%d*%dk = %d Kbytes, time %d.%02d\n", n,
copyCount==1?1:2, copyCount, bytesPerGeneration / 1024,
bytesPerGeneration * copyCount * (copyCount==1?1:2)/1024,
tg/100,tg%100);
} else {
printf("%d gcs in %d*%d*%d = %d bytes, time %d.%02d\n", n,
copyCount==1?1:2, copyCount, bytesPerGeneration,
bytesPerGeneration * copyCount * (copyCount==1?1:2),
tg/100,tg%100);
}
}
if (no > 0) {
if (old_size % 1024 == 0) {
printf("%d old generation gcs in %d Kbytes, time %d.%02d\n",
no, old_size/1024, og/100, og%100);
} else {
printf("%d old generation gcs in %d bytes, time %d.%02d\n",
no, old_size, og/100, og%100);
}
}
#ifdef DISTRIBUTED
if (nd > 0) {
printf("%d distributed gcs freed %d objects\n", nd, ndremoved);
}
printf("Received %d messages (%d bytes), sent %d messages (%d bytes)\n",
nMessagesReceived, nBytesReceived, nMessagesSent, nBytesSent);
#endif
#ifdef PROFILEINTERPRET
outputProfile();
#endif
}
#ifdef PROFILEINVOKES
if (doInvokeProfiling)
outputInvokeProfile();
#endif
return;
}
#undef abort
void myabort(void)
{
abort();
}
void FatalError(char *ErrorMessage)
{
perror(ErrorMessage);
die();
}