forked from ultmaster/runexe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRun.cpp
255 lines (210 loc) · 9.22 KB
/
Run.cpp
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
#include "Run.h"
#include "Strings.h"
#include <string>
#include <vector>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <iostream>
using namespace runexe;
using namespace std;
void runexe::quit(unsigned int exitCode) {
exit(exitCode);
}
void runexe::showInfo() {
cout << endl
<< "RunExe for *NIX, Version " << version << endl
<< "Copyright(c) Saratov State U Codecenter Development Team, "
<< copyrightYears << endl
<< "Based on runlib by Paul P. Komkoff Jr." << endl
<< "Written by Dmitry Levshunov, Mike Mirzayanov" << endl
<< "Modified by ultmaster, 2019" << endl
<< "Use \"runexe -h\" to get help information" << endl;
}
void runexe::showHelp() {
showInfo();
cout << endl
<< "This program runs other program for given period of time with specified" << endl
<< " memory restrictions" << endl << endl
<< "See readme.md for help instructions" << endl;
}
void runexe::fail(const string &comment) {
Configuration &configuration = Configuration::getConfiguration();
if (configuration.isScreenOutput()) {
cout << "Invocation failed" << endl
<< "Comment: " << comment << endl << endl
<< "Use \"runexe -h\" to get help information" << endl;
}
InvocationResult invocationResult = InvocationResult(FAIL, comment);
if (configuration.isXmlOutput())
printXmlInvocationResult(invocationResult, configuration.getXmlFileName());
quit();
}
void runexe::crash(const string &comment) {
Configuration &configuration = Configuration::getConfiguration();
if (configuration.isScreenOutput()) {
cout << "Invocation crashed" << endl
<< "Comment: " << comment << endl << endl
<< "Use \"runexe -h\" to get help information" << endl;
}
InvocationResult invocationResult = InvocationResult(CRASH, comment);
if (configuration.isXmlOutput())
printXmlInvocationResult(invocationResult, configuration.getXmlFileName());
if (configuration.isReturnExitCode())
quit();
else
quit(0);
}
InvocationParams runexe::processParams(const vector<string>& params) {
if (params.size() > 1 && params[1] == "-h") {
showHelp();
quit();
}
// get rid of the first argument before hand
vector<string> args;
for (size_t i = 1; i < params.size(); ++i)
args.push_back(params[i]);
return InvocationParams(args);
}
InvocationParams runexe::processCommandLine(int argc, char *argv[]) {
if (argc <= 1) {
showInfo();
quit();
}
vector<string> params(argc);
for (int i = 0; i < argc; ++i)
params[i] = argv[i];
return processParams(params);
}
void runexe::printTimes(double userTime, double kernelTime,
double timeLimit, bool isTimeLimitExceeded) {
if (Configuration::getConfiguration().isShowKernelModeTime()) {
if (isTimeLimitExceeded)
cout << " time consumed:" << endl
<< " user mode: " << userTime << " of " << timeLimit << " sec" << endl
<< " kernel mode: " << kernelTime << " sec" << endl;
else
cout << " time consumed:" << endl
<< " user mode: " << userTime << " sec" << endl
<< " kernel mode: " << kernelTime << " sec" << endl;
} else {
if (isTimeLimitExceeded)
cout << " time consumed: " << userTime << " of " << timeLimit << " sec" << endl;
else
cout << " time consumed: " << userTime << " sec" << endl;
}
}
void runexe::printXmlInvocationResult(const InvocationResult &invocationResult, const std::string &fileName) {
vector<InvocationResult> results;
results.push_back(invocationResult);
printXmlInvocationResult(results, fileName);
}
void runexe::printInvocationResult(const InvocationParams &invocationParams,
const InvocationResult &invocationResult) {
InvocationVerdict invocationVerdict = invocationResult.getInvocationVerdict();
int exitCode = invocationResult.getExitCode();
double userTime = (double) invocationResult.getUserTime() / 1000.0;
double kernelTime = (double) invocationResult.getKernelTime() / 1000.0;
double passedTime = (double) invocationResult.getPassedTime() / 1000.0;
long long memory = invocationResult.getMemory();
double timeLimit = (double) invocationParams.getTimeLimit() / 1000.0;
long long memoryLimit = invocationParams.getMemoryLimit();
cout.precision(2);
cout.setf(ios::fixed);
switch (invocationVerdict) {
case SUCCESS :
cout << invocationResult.getProgramName() << " successfully terminated" << endl
<< " exit code: " << exitCode << endl,
printTimes(userTime, kernelTime),
cout << " time passed: " << passedTime << " sec" << endl
<< " peak memory: " << memory << " bytes" << endl;
break;
case FAIL :
fail(invocationResult.getComment());
break;
case CRASH :
crash(invocationResult.getComment());
break;
case TIME_LIMIT_EXCEEDED :
cout << "Time limit exceeded" << endl
<< "Program failed to terminate within " << timeLimit << " sec" << endl,
printTimes(userTime, kernelTime, timeLimit, true),
cout << " time passed: " << passedTime << " sec" << endl
<< " peak memory: " << memory << " bytes" << endl;
break;
case MEMORY_LIMIT_EXCEEDED :
cout << "Memory limit exceeded" << endl
<< "Program tried to allocate more than " << memoryLimit << " bytes" << endl,
printTimes(userTime, kernelTime),
cout << " time passed: " << passedTime << " sec" << endl
<< " peak memory: " << memory << " of " << memoryLimit << " bytes" << endl;
break;
case IDLENESS_LIMIT_EXCEEDED :
cout << "Idleness limit exceeded" << endl
<< "Detected program idle " << endl,
printTimes(userTime, kernelTime),
cout << " time passed: " << passedTime << " sec" << endl
<< " peak memory: " << memory << " bytes" << endl;
break;
case SECURITY_VIOLATION :
cout << "Security violation" << endl
<< "Program tried to do some forbidden action" << endl,
printTimes(userTime, kernelTime),
cout << " time passed: " << passedTime << " sec" << endl
<< " peak memory: " << memory << " bytes" << endl;
break;
default :
fail("unknown invocation verdict");
}
}
void runexe::printXmlInvocationResult(const vector<InvocationResult> &invocationResults,
const string &fileName) {
static bool firstCall = true;
if (!firstCall)
return;
else
firstCall = false;
vector<string> result;
result.push_back("<?xml version=\"1.1\" encoding=\"UTF-8\"?>");
result.push_back("<invocationResults>");
for (size_t i = 0; i < invocationResults.size(); ++i) {
const InvocationResult &invocationResult = invocationResults[i];
result.push_back("<invocationResult id=\"" + invocationResult.getProgramName() + "\">");
result.push_back(" <invocationVerdict>" +
invocationVerdictToString(invocationResult.getInvocationVerdict()) +
"</invocationVerdict>");
result.push_back(" <exitCode>" +
Strings::integerToString(invocationResult.getExitCode()) +
"</exitCode>");
result.push_back(" <processorUserModeTime>" +
Strings::integerToString(invocationResult.getUserTime()) +
"</processorUserModeTime>");
result.push_back(" <processorKernelModeTime>" +
Strings::integerToString(invocationResult.getKernelTime()) +
"</processorKernelModeTime>");
result.push_back(" <passedTime>" +
Strings::integerToString(invocationResult.getPassedTime()) +
"</passedTime>");
result.push_back(" <consumedMemory>" +
Strings::integerToString(invocationResult.getMemory()) +
"</consumedMemory>");
result.push_back(" <comment>" + invocationResult.getComment() +
"</comment>");
result.push_back("</invocationResult>");
}
result.push_back("</invocationResults>");
int resultSize = (int) result.size();
if (!fileName.empty()) {
FILE *file = fopen(fileName.c_str(), "wt");
if (NULL != file) {
for (int i = 0; i < resultSize; ++i)
fprintf(file, "%s\n", result[i].c_str());
fclose(file);
} else {
fail("can't open file '" + fileName + "'");
}
} else {
for (int i = 0; i < resultSize; ++i)
printf("%s\n", result[i].c_str());
}
}