forked from SevenChords/CipesAtHome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.c
297 lines (259 loc) · 8.41 KB
/
start.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
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <libconfig.h>
#include "base.h"
#ifndef _IS_WINDOWS
#error "base.h wasn't loaded correctly"
#endif
#include "thread_local_random.h"
#include "inventory.h"
#include "config.h"
#include "recipes.h"
#include "FTPManagement.h"
#include "start.h"
#include "calculator.h"
#include <time.h>
#include "cJSON.h"
#include <curl/curl.h>
#include "logger.h"
#include "shutdown.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>
#include <omp.h>
#include "absl/base/port.h"
#if !_IS_WINDOWS || defined(__MINGW32__)
#if _POSIX_C_SOURCE >= 200112L
#include <sys/select.h>
#endif
#include <unistd.h>
#include <errno.h>
#include <string.h>
#endif
#if _IS_WINDOWS
#include <direct.h>
#endif
ABSL_ATTRIBUTE_UNUSED ABSL_ATTRIBUTE_USED
static const char _recipesAtHomeVersion[] = "1.14.3-TechSY730";
#define WAIT_TIME_BEFORE_CONTINUE_ON_FAILED_UPDATE_CHECK_SECS 10
int current_frame_record;
const char *local_ver;
// May get a value <0 if local record was corrupt.
int getLocalRecord() {
int current_frame_record_orig = current_frame_record;
if (ABSL_PREDICT_FALSE(current_frame_record < 0)) {
printf("Current frame record is corrupt (less then 0 frames). Resetting (you may get false PBs for a while).\n");
current_frame_record = UNSET_FRAME_RECORD;
return current_frame_record_orig;
}
return current_frame_record;
}
void setLocalRecord(int frames) {
if (ABSL_PREDICT_FALSE(frames < 0)) {
printf("Got corrupt PB if %d frames. Ignoring\n", frames);
return;
}
current_frame_record = frames;
}
const char *getLocalVersion() {
return local_ver;
}
int numTimesExitRequest = 0;
#define NUM_TIMES_EXITED_BEFORE_HARD_QUIT 3
void countAndSetShutdown(bool isSignal) {
if (++numTimesExitRequest >= NUM_TIMES_EXITED_BEFORE_HARD_QUIT) {
if (!_IS_WINDOWS || !isSignal) {
printf("\nExit reqested %d times; shutting down now.\n", NUM_TIMES_EXITED_BEFORE_HARD_QUIT);
}
exit(1);
} else {
requestShutdown();
if (!_IS_WINDOWS || !isSignal) {
printf("\nExit requested, finishing up work. Should shutdown soon (CTRL-C %d times total to force exit)\n", NUM_TIMES_EXITED_BEFORE_HARD_QUIT);
}
}
}
void handleTermSignal(int signum) {
countAndSetShutdown(true);
}
void handleAbrtSignal(int signum) {
// First off, reset our signal handler to default in case we encounter another signal in trying to print out debugging info, we don't loop.
signal(signum, SIG_DFL);
if (signum != SIGABRT || (!_abrt_from_assert && signum == SIGABRT)) {
printStackTraceF(stderr);
}
raise(signum);
}
/*void handleSegvSignale(int signal) {
void *array[20];
size_t size;
// get void*'s for all entries on the stack
size = backtrace(array, 20);
// print out all the frames to stderr
fprintf(stderr, "Error: signal %d:\n", signal);
backtrace_symbols_fd(array, size, STDERR_FILENO);
exit(1);
}*/
#if _IS_WINDOWS
BOOL WINAPI windowsCtrlCHandler(DWORD fdwCtrlType) {
switch(fdwCtrlType) {
case CTRL_C_EVENT: ABSL_FALLTHROUGH_INTENDED;
case CTRL_CLOSE_EVENT:
countAndSetShutdown(false);
return TRUE;
default:
return FALSE;
}
}
#endif
void setSignalHandlers() {
signal(SIGTERM, handleTermSignal);
signal(SIGINT, handleTermSignal);
signal(SIGABRT, handleAbrtSignal);
signal(SIGSEGV, handleAbrtSignal);
signal(SIGILL, handleAbrtSignal);
#ifdef SIGQUIT
signal(SIGQUIT, handleAbrtSignal);
#endif
#ifdef SIGSYS
signal(SIGSYS, handleAbrtSignal);
#endif
#if _IS_WINDOWS
if (!SetConsoleCtrlHandler(windowsCtrlCHandler, TRUE)) {
printf("Unable to set CTRL-C handler. CTRL-C may cause unclean shutdown.\n");
}
#endif
}
int main(int argc, char **argv) {
int max_outer_loops = -1;
long max_branches = -1;
if (argc >= 2) {
max_branches = atol(argv[1]);
if (argc >= 3) {
max_outer_loops = atoi(argv[2]);
}
}
printf("Welcome to Recipes@Home!\n");
printf("Leave this program running as long as you want to search for new recipe orders.\n");
current_frame_record = UNSET_FRAME_RECORD;
initConfig();
// If select and randomise are both 0, the same roadmap will be calculated on every thread, so set threads = 1
// The debug setting can only be meaningfully used with one thread as well.
int workerCount = (getConfigInt("select") || getConfigInt("randomise"))
&& !getConfigInt("debug") ? getConfigInt("workerCount") : 1;
// Create workerCount threads
omp_set_num_threads(workerCount);
prepareStackTraces();
local_ver = getConfigStr("Version");
init_level_cfg();
curl_global_init(CURL_GLOBAL_DEFAULT); // Initialize libcurl
int update = checkForUpdates(local_ver);
if (update == -1) {
printf("Could not check version on Github. Please check your internet connection.\n");
printf("Otherwise, we can't submit completed roadmaps to the server!\n");
printf("Alternatively you may have been rate-limited. Please wait a while and try again.\n");
#if _IS_WINDOWS
printf("Will continue after %d seconds\n", WAIT_TIME_BEFORE_CONTINUE_ON_FAILED_UPDATE_CHECK_SECS);
Sleep(WAIT_TIME_BEFORE_CONTINUE_ON_FAILED_UPDATE_CHECK_SECS * 1000);
#elif _POSIX_C_SOURCE >= 200112L
fd_set stdin;
FD_SET(STDERR_FILENO, &stdin);
printf("Press ENTER to continue (will automatically continue in %d seconds).\n", WAIT_TIME_BEFORE_CONTINUE_ON_FAILED_UPDATE_CHECK_SECS);
struct timeval tv;
tv.tv_sec = WAIT_TIME_BEFORE_CONTINUE_ON_FAILED_UPDATE_CHECK_SECS;
tv.tv_usec = 0;
int retval = select(1, &stdin, NULL, NULL, &tv);
if (retval == -1) {
printf("Failure in waiting! %d (%s)\n", errno, strerror(errno));
return retval;
}
#else
printf("Continuing for now.\n");
#endif
}
else if (update == 1) {
printf("There is a newer version of Recipes@Home.\nTo continue, please visit https://github.com/SevenChords/CipesAtHome/releases to download the newest version of this program!\n");
printf("Press ENTER to quit.\n");
char exitChar = getchar();
return -1;
}
// Greeting message to user
int blob_record = getFastestRecordOnBlob();
if (blob_record == 0) {
printf("There was an error contacting the server to retrieve the fastest time.\n");
printf("Please check your internet connection, but we'll continue for now.\n");
}
else {
printf("The current fastest record is %d frames.\n", blob_record);
}
// Verify that the results folder exists
// If not, create the directory
#if _IS_WINDOWS
CreateDirectoryA("./results", NULL);
#else
mkdir("./results", 0777);
#endif
// To avoid generating roadmaps that are slower than the user's record best,
// use PB.txt to identify the user's current best
FILE* fp = fopen("results/PB.txt", "r");
// The PB file may not have been created yet, so ignore the case where it is missing
if (fp != NULL) {
int PB_record;
if (fscanf(fp, "%d", &PB_record) == 1) {
if (PB_record < 1000) {
printf("The record stored in PB.txt can't be right... Ignoring.\n");
} else {
current_frame_record = PB_record;
if (current_frame_record < UNSET_FRAME_RECORD) {
printf("Your current PB is %d frames.\n", current_frame_record);
}
// Submit the user's fastest roadmap to the server for leaderboard purposes
// in case this was not submitted upon initial discovery
testRecord(current_frame_record);
}
}
fclose(fp);
}
printf("Happy cooking!\n");
// Initialize global variables in calculator.c
// This does not need to be done in parallel, as these globals will
// persist through all parallel calls to calculator.c
initializeInvFrames();
initializeRecipeList();
setSignalHandlers();
// copying to const so OpenMP knows it doesn't have to have each thread
// recheck this.
const int max_outer_loops_fixed = max_outer_loops;
const long max_branches_fixed = max_branches;
#pragma omp parallel
{
long cycle_count = 0;
int rawID = omp_get_thread_num();
int displayID = rawID + 1;
threadlocal_rand_init();
#pragma omp critical(printing_on_failure)
{
printf("[Thread %d/%d][Started]\n", displayID, workerCount);
}
// Seed each thread's PRNG for the select and randomise config options
threadlocal_srand(((int)time(NULL)) ^ rawID);
while (max_outer_loops_fixed < 0 || cycle_count < max_outer_loops_fixed) {
if (askedToShutdown()) {
break;
}
++cycle_count;
struct Result result = calculateOrder(rawID, max_branches_fixed);
// result might store -1 frames for errors that might be recoverable
if (result.frames > -1) {
testRecord(result.frames);
}
}
#pragma omp critical(printing_on_failure)
{
printf("[Thread %d/%d][Done]\n", displayID, workerCount);
}
threadlocal_rand_destroy();
}
return 0;
}