-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0xhaust.c
120 lines (107 loc) · 3.24 KB
/
0xhaust.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
#include <getopt.h>
#include <stdio.h>
#include <jemalloc/jemalloc.h>
#include <stdlib.h>
#include <time.h>
#include "asm.h"
#include "sim.h"
#include "types.h"
int rand_lim(int lower, int upper) {
/* return a random number between lower and limit inclusive.
*/
int divisor = RAND_MAX / (upper + 1);
int retval;
do {
retval = rand() / divisor;
} while (retval + lower > upper);
return retval + lower;
}
int main(int argc, char **argv) {
srand(time(NULL));
unsigned int rounds = 1;
unsigned int cycles = 80000;
int w2_position = -1;
unsigned int coresize = 8000;
unsigned int maxprocs = 8000;
unsigned int minsep = 100;
int opt;
while ((opt = getopt(argc, argv, "r:c:F:s:p:d:")) != -1) {
switch (opt) {
case 'r':
rounds = strtol(optarg, NULL, 10);
break;
case 'c':
cycles = strtol(optarg, NULL, 10);
break;
case 'F':
w2_position = strtol(optarg, NULL, 10);
break;
case 's':
coresize = strtol(optarg, NULL, 10);
break;
case 'p':
maxprocs = strtol(optarg, NULL, 10);
break;
case 'd':
minsep = strtol(optarg, NULL, 10);
break;
}
}
if (optind == argc) {
printf(
"0xhaust v0.1\n"
"usage: 0xhaust [opts] warriors...\n"
"0xhaust runs one-on-one round robin tournaments\n"
"use exhaust or pmers for multiwarrior tournaments\n"
"opts: -r <rounds>, -c <cycles>, -F <pos>, -s <coresize>, -p"
"<maxprocs>, -d <minsep>\n");
return 0;
}
int nwarriors = argc - optind;
// precompile all warriors
warrior_t *warriors = calloc(nwarriors, sizeof(warrior_t));
for (int i = 0; i < nwarriors; i++) {
asm_fname(argv[optind + i], &warriors[i], coresize);
}
int *tally = calloc(2 * nwarriors, sizeof(int));
SimState_t *s = sim_alloc(2, coresize, maxprocs, cycles, coresize / 16);
/* Main Loop simulation loops */
for (int i = 0; i < nwarriors; i++) {
for (int j = i + 1; j < nwarriors; j++) {
for (int round = 0; round < rounds; round++) {
/* Ready the simulator */
sim_reset_round(s);
sim_load_warrior(s, 0, warriors[i].code, warriors[i].len);
int w2_pos = w2_position == -1
? rand_lim((warriors[j].len + minsep) % coresize,
coresize - minsep)
: w2_position;
sim_load_warrior(s, w2_pos, warriors[j].code, warriors[j].len);
field_t positions[2] = {0 + warriors[i].start,
w2_pos + warriors[j].start};
unsigned int results[2] = {};
int alive_count = sim_simulate(s, positions, results);
if (alive_count == 2) {
tally[2 * i + 1]++;
tally[2 * j + 1]++;
} else if (results[0] == 0) {
/* the first warrior died first */
tally[2 * j]++;
} else if (results[0] == 1) {
/* the second warrior died first */
tally[2 * i]++;
} else {
printf("Simulator error\n");
return 1;
}
}
}
sim_reset_round(s);
}
sim_free(s);
for (int i = 0; i < nwarriors; i++) {
printf("%d %d\n", tally[2 * i], tally[2 * i + 1]);
}
free(warriors);
free(tally);
}