summaryrefslogtreecommitdiff
path: root/main.cpp
blob: 4f688aa5d35f6c86f4ffe4735765af761a3b4700 (plain)
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <utility>
#include <algorithm>
#include <optional>
#include <string_view>
#include <cstdint>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/time.h>
#include "scheduler.h"
#include "process.h"
#include "error.h"
#include "multilog.h"
#include "referee.h"

using namespace std;


const char *const matchcachedir = "comp_cache";
const char *const playerlogdir = "comp_playerlogs";
const char *const gamelogdir = "comp_gamelogs";


static char hexchar(int n) {
	return "0123456789ABCDEF"[n];
}

static string makeSafe(const string_view str) {
	string res;
	res.reserve(str.size() + 3);

	for (char c : str) {
		if (isalnum(c)) {
			res += c;
		} else {
			res += '_';
			res += hexchar((c >> 4) & 0xf);
			res += hexchar(c & 0xf);
		}
	}

	return res;
}

// Only creates a single component
static void mkdirp(const string_view name) {
	if (mkdir(name.data(), 0755) < 0) {
		if (errno != EEXIST) {
			perror("mkdir");
			exit(1);
		}
	}
}

static int64_t fileLastModified(const string_view fname) {
	struct stat st;
	if (stat(fname.data(), &st) < 0) {
		if (errno == EEXIST) return 0;
		perror("stat");
		exit(1);
	}
#ifdef __APPLE__
	return st.st_mtimespec.tv_sec * 1000000LL + st.st_mtimespec.tv_nsec / 1000;
#else
	return st.st_mtim.tv_sec * 1000000LL + st.st_mtim.tv_nsec / 1000;
#endif
}

static int64_t gettimestamp() {
	struct timeval tv;
	gettimeofday(&tv, nullptr);
	return tv.tv_sec * 1000000LL + tv.tv_usec;
}

struct Player;

// Methods represent results for player 1, unless inverted
struct MatchResult {
	enum class Win {
		win, loss, tie, timeout, crash
	};

	enum class Status {  // crash is not used yet
		ok, timeout, crash
	};

	Status status;
	int sc1, sc2;
	size_t ms1 = 0, ms2 = 0;

	int score() const;
	Win getWin() const;
	string describe(const Player &p1, const Player &p2) const;
	MatchResult inverted() const;
};

struct Player {
	mutex lock;
	string fname, safename;
	vector<MatchResult> results;
	int64_t lastModified;

	Player(const string &fname)
			: fname(fname), safename(makeSafe(fname)),
			lastModified(fileLastModified(fname)) {}

	// Do not call this in a threaded setting (wouldn't make sense anyway)
	Player(Player &&other)
		: fname{move(other.fname)}
		, safename{move(other.safename)}
		, results{move(other.results)}
		, lastModified{other.lastModified}
	{}
};

int MatchResult::score() const {
	return sc1;
}

MatchResult::Win MatchResult::getWin() const {
	switch (status) {
		case Status::ok:
			if (sc1 > sc2) return Win::win;
			else if (sc1 == sc2) return Win::tie;
			else return Win::loss;

		case Status::timeout:
			return Win::timeout;

		case Status::crash:
			return Win::crash;

		default:
			assert(false);
	}
}

string MatchResult::describe(const Player &p1, const Player &p2) const {
	string prefix;
	switch (getWin()) {
		case Win::win: prefix = p1.fname + " won: "; break;
		case Win::loss: prefix = p2.fname + " won: "; break;
		case Win::tie: prefix = "tie: "; break;
		case Win::timeout: prefix = "timeout: "; break;
		case Win::crash: prefix = "crash: "; break;
	}
	return prefix + to_string(sc1) + "-" + to_string(sc2);
}

MatchResult MatchResult::inverted() const {
	MatchResult r;
	r.status = status;
	r.sc2 = sc1; r.sc1 = sc2;
	r.ms2 = ms1; r.ms1 = ms2;
	return r;
}

enum class Mode {
	competition,
	single,
};

struct Params {
	Mode mode;
	bool referee_verbose = false;
	string refereePath;
	size_t timeout_msec = 10000;
};

static string gameCodeName(const Player &p1, const Player &p2, int index) {
	return p1.safename + "-" + p2.safename + "-" + to_string(index);
}

static string playerLogPath(const Player &p1, const Player &p2, int index, int who) {
	return string(playerlogdir) + "/" + gameCodeName(p1, p2, index) + "-" + to_string(who) + ".txt";
}

static string matchCachePath(const Player &p1, const Player &p2, int index) {
	return string(matchcachedir) + "/" + gameCodeName(p1, p2, index) + ".txt";
}

static string gameLogPath(const Player &p1, const Player &p2, int index) {
	return string(gamelogdir) + "/" + gameCodeName(p1, p2, index) + ".txt";
}

static optional<MatchResult> readMatchCache(const Player &p1, const Player &p2, int index) {
	string path = matchCachePath(p1, p2, index);

	ifstream f(path);
	if (!f) return nullopt;

	int64_t cacheStamp = fileLastModified(path);
	if (p1.lastModified > cacheStamp || p2.lastModified > cacheStamp) {
		return nullopt;
	}

	MatchResult mres;
	string word;
	f >> word >> mres.sc1 >> mres.sc2 >> mres.ms1 >> mres.ms2;
	if (!f) {
		return nullopt;
	}

	if (word == "ok") mres.status = MatchResult::Status::ok;
	else if (word == "timeout") mres.status = MatchResult::Status::timeout;
	else if (word == "crash") mres.status = MatchResult::Status::crash;
	else return nullopt;

	return mres;
}

static void writeMatchCache(const Player &p1, const Player &p2, int index, const MatchResult &mres) {
	string path = matchCachePath(p1, p2, index);
	ofstream f(path);
	if (!f) {
		cout << endl << "ERROR: Cannot open match cache file '" << path << "'" << endl;
		throw StopCompetitionError();
	}

	switch (mres.status) {
		case MatchResult::Status::ok: f << "ok"; break;
		case MatchResult::Status::timeout: f << "timeout"; break;
		case MatchResult::Status::crash: f << "crash"; break;
	}

	f << ' ' << mres.sc1 << ' ' << mres.sc2;
	f << ' ' << mres.ms1 << ' ' << mres.ms2 << endl;
}

static void recordResult(Player &p1, Player &p2, const MatchResult &result) {
	{
		lock_guard guard{p1.lock};
		p1.results.push_back(result);
	}
	{
		lock_guard guard{p2.lock};
		p2.results.push_back(result.inverted());
	}
}

static MatchResult playMatch(const Player &p1, const Player &p2, const Params &params, const string &gamecode, ostream &gamelog, const string &p1logpath, const string &p2logpath) {
	MatchResult mres;

	Referee referee(params.referee_verbose, params.refereePath, {p1.fname, p2.fname});

	gamelog << "Player 1: " << p1.fname << "\n"
	        << "Player 2: " << p2.fname << "\n\n" << flush;

	Process procs[2] = {Process(p1.fname), Process(p2.fname)};
	procs[0].redirectStderr(p1logpath);
	procs[1].redirectStderr(p2logpath);
	procs[0].run();
	procs[1].run();

	while (true) {
		auto event = referee.nextEvent();

		if (auto readEvent = get_if<Referee::ReadEvent>(&event)) {
			Process &proc = procs[readEvent->player];

			proc.unStop();
			int64_t start = gettimestamp();

			optional<string> oline = proc.readLine();
			if (!oline) {
				cout << "ERROR reading move from player " << readEvent->player + 1
				     << " (game " << gamecode << ")" << endl;
				cout << "(process exit code: " << proc.waitPoll() << ")" << endl;
				throw StopCompetitionError();
			}

			(readEvent->player == 0 ? mres.ms1 : mres.ms2) += gettimestamp() - start;
			proc.stop();

			readEvent->callback(*oline);

			if (mres.ms1 / 1000 > params.timeout_msec || mres.ms2 / 1000 > params.timeout_msec) {
				mres.status = MatchResult::Status::timeout;
				mres.sc1 = mres.sc2 = 0;
				referee.terminate();
				break;
			}

		} else if (auto writeEvent = get_if<Referee::WriteEvent>(&event)) {
			if (!procs[writeEvent->player].writeLine(writeEvent->line, writeEvent->allowBrokenPipe)) {
				cout << "ERROR writing move to player " << writeEvent->player + 1
					 << " (game " << gamecode << ")" << endl;
				throw StopCompetitionError();
			}

		} else if (auto gamelogEvent = get_if<Referee::GamelogEvent>(&event)) {
			gamelog << "P" << gamelogEvent->player + 1 << ": "
			        << gamelogEvent->line << endl;

		} else if (auto endEvent = get_if<Referee::EndEvent>(&event)) {
			mres.status = MatchResult::Status::ok;

			assert(endEvent->scores.size() == 2);
			mres.sc1 = endEvent->scores[0];
			mres.sc2 = endEvent->scores[1];
			break;

		} else if (auto errorEvent = get_if<Referee::ErrorEvent>(&event)) {
			cout << "ERROR in player " << errorEvent->player + 1 << ": " << errorEvent->message
				 << " (game " << gamecode << ")" << endl;
			gamelog << endl
			        << "ERROR in P" << errorEvent->player + 1 << ": " << errorEvent->message << endl;
			throw StopCompetitionError();

		} else {
			assert(false);
		}
	}

	for (int i = 0; i < 2; i++) {
		procs[i].unStop();
		if (procs[i].waitPoll() == -1) usleep(10000);
		int ret = procs[i].terminate();
		if (ret != 0 && ret != 1009) {
			cout << "Player " << i+1 << " exited with code " << ret << endl;
		}
	}

	gamelog << "\nResult: " << mres.describe(p1, p2) << "\n"
	        << "P1 took " << mres.ms1 / 1000000.0 << " seconds\n"
	        << "P2 took " << mres.ms2 / 1000000.0 << " seconds\n" << flush;

	return mres;
}

static void playMatchInCompetition(MultiLog &multiLog, Player &p1, Player &p2, int index, const Params &params) {
	const int logId = multiLog.add(p1.fname + " - " + p2.fname + ": ");

	if (optional<MatchResult> optres = readMatchCache(p1, p2, index)) {
		multiLog.append(logId, optres->describe(p1, p2) + " (cached)");
		multiLog.complete(logId);
		recordResult(p1, p2, *optres);
		return;
	}

	const string gamelog_path = gameLogPath(p1, p2, index);
	ofstream gamelog(gamelog_path);
	if (!gamelog) {
		cout << "ERROR opening game log file " << gamelog_path << endl;
		throw StopCompetitionError();
	}

	const MatchResult mres = playMatch(
		p1, p2, params,
		gameCodeName(p1, p2, index),
		gamelog,
		playerLogPath(p1, p2, index, 1),
		playerLogPath(p1, p2, index, 2)
	);

	multiLog.append(logId, mres.describe(p1, p2));
	multiLog.complete(logId);

	writeMatchCache(p1, p2, index, mres);
	recordResult(p1, p2, mres);
}

struct CompParams {
	Scheduler *scheduler;
	MultiLog *multiLog;
	int num_matches = 5;
};

static void playerPit(CompParams &compParams, Player &p1, Player &p2, const Params &params) {
	for (int i = 0; i < compParams.num_matches; i++) {
		compParams.scheduler->submit([i, &p1, &p2, &params, &compParams]() {
			playMatchInCompetition(*compParams.multiLog, p1, p2, i + 1, params);
		});
	}
}

static void fullCompetition(CompParams compParams, vector<Player> &players, const Params &params) {
	for (size_t p1i = 0; p1i < players.size(); p1i++) {
		for (size_t p2i = p1i + 1; p2i < players.size(); p2i++) {
			playerPit(compParams, players[p1i], players[p2i], params);
			playerPit(compParams, players[p2i], players[p1i], params);
		}
	}
}

static void usage(const char *argv0) {
	cerr <<
		"Usage: " << argv0 << " comp [OPTIONS] <referee> <players...>\n"
		"Run a full competition. Gamelogs in " << gamelogdir << ", playerlogs in\n"
		<< playerlogdir << ". Caches in " << matchcachedir << "; already-played games\n"
		"as determined by caches will not be run again.\n"
		"  --noansi   Don't use fancy back-updating log output\n"
		"  --vs       Verbose scheduler output (use with --noansi)\n"
		"  -j <NUM>   Set number of parallel games (default ncores/2)\n"
		"  -n <NUM>   Set number of matches per player pairing (default "
			<< CompParams{}.num_matches << ")\n"
		"\n"
		"Usage: " << argv0 << " single [OPTIONS] <referee> <player1> <player2>\n"
		"Run only a single game. Gamelog from referee is sent to stdout.\n"
		"  --log1 <FILE>  Playerlog file for player 1\n"
		"  --log2 <FILE>  Playerlog file for player 2\n"
		"\n"
		"Options that always apply:\n"
		"  --vr       Verbose referee-handling output (use with --noansi if comp mode)\n"
		"  -T <MSEC>  Set player timeout in milliseconds (default "
			<< Params{}.timeout_msec << ")\n"
		;
}

static Params parseCommonOptions(vector<const char*> &argv) {
	Params params;

	if (argv.size() <= 1) {
		usage(argv[0]);
		exit(1);
	}

	if (strcmp(argv[1], "comp") == 0) params.mode = Mode::competition;
	else if (strcmp(argv[1], "single") == 0) params.mode = Mode::single;
	else {
		usage(argv[0]);
		exit(1);
	}

	argv.erase(argv.begin() + 1);

	for (size_t i = 1; i < argv.size(); i++) {
		if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
			usage(argv[0]);
			exit(0);
		} else if (strcmp(argv[i], "--vr") == 0) {
			params.referee_verbose = true;
			argv.erase(argv.begin() + i);
			i--;
		} else if (strcmp(argv[i], "-T") == 0 && i + 1 < argv.size()) {
			char *endp;
			params.timeout_msec = strtoull(argv[i+1], &endp, 10);
			if (!argv[i][0] || *endp || params.timeout_msec <= 0) {
				cerr << "Invalid number to -T" << endl;
				exit(1);
			}
			argv.erase(argv.begin() + i, argv.begin() + i + 2);
			i--;
		} else if (strcmp(argv[i], "-j") == 0 ||
					strcmp(argv[i], "-n") == 0 ||
					(strlen(argv[i]) >= strlen("--log1") &&
						memcmp(argv[i], "--log", strlen("--log")) == 0)) {
			i++;  // skip argument of option
		} else if (params.refereePath.empty()) {
			params.refereePath = argv[i];
			argv.erase(argv.begin() + i);
			i--;
		}
	}

	return params;
}

static int mainCompetition(const vector<const char*> &argv, const Params &params) {
	vector<Player> players;
	bool multilog_fancy = true;
	bool scheduler_verbose = false;
	int num_threads = std::thread::hardware_concurrency() / 2;
	CompParams compParams;

	for (size_t i = 1; i < argv.size(); i++) {
		if (strcmp(argv[i], "--noansi") == 0) {
			multilog_fancy = false;
		} else if (strcmp(argv[i], "--vs") == 0) {
			scheduler_verbose = true;
		} else if (strcmp(argv[i], "-j") == 0 && i + 1 < argv.size()) {
			char *endp;
			num_threads = strtol(argv[i+1], &endp, 10);
			if (!argv[i][0] || *endp || num_threads <= 0) {
				cerr << "Invalid number to -j" << endl;
				return 1;
			}
			i++;
		} else if (strcmp(argv[i], "-n") == 0 && i + 1 < argv.size()) {
			char *endp;
			compParams.num_matches = strtol(argv[i+1], &endp, 10);
			if (!argv[i][0] || *endp || compParams.num_matches <= 0) {
				cerr << "Invalid number to -n" << endl;
				return 1;
			}
			i++;
		} else if (argv[i][0] == '-') {
			cerr << "Unknown option/flag '" << argv[i] << "'" << endl;
			return 1;
		} else {
			players.emplace_back(argv[i]);
		}
	}

	if (players.empty()) {
		usage(argv[0]);
		return 1;
	}

	mkdirp(matchcachedir);
	mkdirp(playerlogdir);
	mkdirp(gamelogdir);

	signal(SIGPIPE, [](int){});

	MultiLog multiLog{multilog_fancy};
	compParams.multiLog = &multiLog;

	Scheduler scheduler(scheduler_verbose, num_threads);
	compParams.scheduler = &scheduler;

	fullCompetition(compParams, players, params);
	scheduler.finish();

	vector<pair<string, int>> scores;
	for (const Player &player : players) {
		int score = 0;
		for (const MatchResult &result : player.results) {
			score += result.score();
		}
		scores.emplace_back(player.fname, score);
	}

	sort(scores.begin(), scores.end(),
			[](const pair<string, int> &a, const pair<string, int> &b) { return a.second > b.second; });

	size_t maxlen = strlen("Player");
	for (const Player &player : players) {
		maxlen = max(maxlen, player.fname.size());
	}

	cout << endl << setw(maxlen) << "Player" << " | " << "Score" << endl;
	cout << string(maxlen, '-') << "-+------" << endl;

	for (const auto &p : scores) {
		cout << setw(maxlen) << p.first << " | " << p.second << endl;
	}

	return 0;
}

static int mainSingle(const vector<const char*> &argv, const Params &params) {
	vector<Player> players;
	unordered_map<int, string> logfnames;

	for (size_t i = 1; i < argv.size(); i++) {
		if (strlen(argv[i]) >= strlen("--log1") &&
				memcmp(argv[i], "--log", strlen("--log")) == 0 &&
				i + 1 < argv.size()) {
			const char *idxstring = argv[i] + strlen("--log");
			char *endp;
			int playeridx = strtol(idxstring, &endp, 10);
			if (!idxstring[0] || *endp || playeridx <= 0) {
				cerr << "--logN index invalid" << endl;
				return 1;
			}
			logfnames[playeridx] = argv[i+1];
			i++;
		} else if (argv[i][0] == '-') {
			cerr << "Unknown option/flag '" << argv[i] << "'" << endl;
			return 1;
		} else {
			players.emplace_back(argv[i]);
		}
	}

	if (players.size() != 2) {
		usage(argv[0]);
		return 1;
	}

	vector<string> playerlogs(players.size());
	for (size_t i = 0; i < players.size(); i++) {
		auto it = logfnames.find(i + 1);
		if (it == logfnames.end()) playerlogs[i] = "/dev/null";
		else playerlogs[i] = it->second;
	}

	signal(SIGPIPE, [](int){});

	playMatch(players[0], players[1], params, "single", cout, playerlogs[0], playerlogs[1]);

	return 0;
}

int main(int argc, char **argv) {
	if (argc == 0) {
		usage("competition");
		return 1;
	}

	vector<const char*> arguments{argv, argv + argc};
	const Params params = parseCommonOptions(arguments);

	switch (params.mode) {
		case Mode::competition:
			return mainCompetition(arguments, params);

		case Mode::single:
			return mainSingle(arguments, params);
	}
}