2021-12-26 23:18:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-11-01 09:18:23 +08:00
|
|
|
#include "ojproblemcasesrunner.h"
|
|
|
|
#include "../utils.h"
|
|
|
|
#include "../settings.h"
|
|
|
|
#include "../systemconsts.h"
|
2022-04-29 10:02:34 +08:00
|
|
|
#include <QElapsedTimer>
|
2021-11-01 09:18:23 +08:00
|
|
|
#include <QProcess>
|
2022-12-13 08:49:20 +08:00
|
|
|
#ifdef Q_OS_WINDOWS
|
|
|
|
#include <psapi.h>
|
|
|
|
#endif
|
2021-11-01 09:18:23 +08:00
|
|
|
|
2021-12-23 17:07:27 +08:00
|
|
|
|
2021-11-01 09:18:23 +08:00
|
|
|
OJProblemCasesRunner::OJProblemCasesRunner(const QString& filename, const QString& arguments, const QString& workDir,
|
|
|
|
const QVector<POJProblemCase>& problemCases, QObject *parent):
|
2022-03-27 11:44:52 +08:00
|
|
|
Runner(filename,arguments,workDir,parent),
|
2022-12-13 08:49:20 +08:00
|
|
|
mExecTimeout(0),
|
|
|
|
mMemoryLimit(0)
|
2021-11-01 09:18:23 +08:00
|
|
|
{
|
|
|
|
mProblemCases = problemCases;
|
2022-01-28 08:21:56 +08:00
|
|
|
mBufferSize = 8192;
|
|
|
|
mOutputRefreshTime = 1000;
|
|
|
|
setWaitForFinishTime(100);
|
2021-11-01 09:18:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
OJProblemCasesRunner::OJProblemCasesRunner(const QString& filename, const QString& arguments, const QString& workDir,
|
|
|
|
POJProblemCase problemCase, QObject *parent):
|
2022-03-27 11:44:52 +08:00
|
|
|
Runner(filename,arguments,workDir,parent),
|
2022-12-13 08:49:20 +08:00
|
|
|
mExecTimeout(0),
|
|
|
|
mMemoryLimit(0)
|
2021-11-01 09:18:23 +08:00
|
|
|
{
|
|
|
|
mProblemCases.append(problemCase);
|
2022-01-28 08:21:56 +08:00
|
|
|
mBufferSize = 8192;
|
|
|
|
mOutputRefreshTime = 1000;
|
|
|
|
setWaitForFinishTime(100);
|
2021-11-01 09:18:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void OJProblemCasesRunner::runCase(int index,POJProblemCase problemCase)
|
|
|
|
{
|
2021-11-02 01:07:37 +08:00
|
|
|
emit caseStarted(problemCase->getId(),index, mProblemCases.count());
|
2021-11-01 20:44:08 +08:00
|
|
|
auto action = finally([this,&index, &problemCase]{
|
2021-11-02 01:07:37 +08:00
|
|
|
emit caseFinished(problemCase->getId(), index, mProblemCases.count());
|
2021-11-01 09:18:23 +08:00
|
|
|
});
|
|
|
|
QProcess process;
|
|
|
|
bool errorOccurred = false;
|
2022-03-29 18:06:24 +08:00
|
|
|
QByteArray readed;
|
|
|
|
QByteArray buffer;
|
|
|
|
QByteArray output;
|
|
|
|
int noOutputTime = 0;
|
|
|
|
QElapsedTimer elapsedTimer;
|
2022-03-30 19:28:46 +08:00
|
|
|
bool execTimeouted = false;
|
2021-11-01 09:18:23 +08:00
|
|
|
process.setProgram(mFilename);
|
2022-01-04 16:50:54 +08:00
|
|
|
process.setArguments(splitProcessCommand(mArguments));
|
2021-11-01 09:18:23 +08:00
|
|
|
process.setWorkingDirectory(mWorkDir);
|
|
|
|
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
|
|
|
QString path = env.value("PATH");
|
|
|
|
QStringList pathAdded;
|
2022-03-30 19:28:46 +08:00
|
|
|
bool writeChannelClosed = false;
|
2021-11-01 09:18:23 +08:00
|
|
|
if (pSettings->compilerSets().defaultSet()) {
|
|
|
|
foreach(const QString& dir, pSettings->compilerSets().defaultSet()->binDirs()) {
|
|
|
|
pathAdded.append(dir);
|
|
|
|
}
|
|
|
|
}
|
2021-12-26 15:08:54 +08:00
|
|
|
pathAdded.append(pSettings->dirs().appDir());
|
2021-11-01 09:18:23 +08:00
|
|
|
if (!path.isEmpty()) {
|
2022-05-30 16:01:28 +08:00
|
|
|
path= pathAdded.join(PATH_SEPARATOR) + PATH_SEPARATOR + path;
|
2021-11-01 09:18:23 +08:00
|
|
|
} else {
|
|
|
|
path = pathAdded.join(PATH_SEPARATOR);
|
|
|
|
}
|
|
|
|
env.insert("PATH",path);
|
|
|
|
process.setProcessEnvironment(env);
|
2023-07-12 12:08:26 +08:00
|
|
|
if (pSettings->executor().redirectStderrToToolLog()) {
|
|
|
|
emit logStderrOutput("\n");
|
|
|
|
} else {
|
|
|
|
process.setProcessChannelMode(QProcess::MergedChannels);
|
|
|
|
process.setReadChannel(QProcess::StandardOutput);
|
|
|
|
}
|
2021-12-16 10:46:38 +08:00
|
|
|
process.connect(
|
|
|
|
&process, &QProcess::errorOccurred,
|
|
|
|
[&](){
|
|
|
|
errorOccurred= true;
|
|
|
|
});
|
2021-11-01 09:18:23 +08:00
|
|
|
problemCase->output.clear();
|
2021-12-16 10:46:38 +08:00
|
|
|
process.start();
|
2021-11-01 09:18:23 +08:00
|
|
|
process.waitForStarted(5000);
|
2022-12-13 08:49:20 +08:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
HANDLE hProcess = NULL;
|
|
|
|
if (process.processId()!=0) {
|
|
|
|
hProcess = OpenProcess(PROCESS_ALL_ACCESS,FALSE,process.processId());
|
|
|
|
}
|
|
|
|
#endif
|
2021-11-01 09:18:23 +08:00
|
|
|
if (process.state()==QProcess::Running) {
|
2022-03-29 18:06:24 +08:00
|
|
|
if (fileExists(problemCase->inputFileName))
|
|
|
|
process.write(readFileToByteArray(problemCase->inputFileName));
|
|
|
|
else
|
|
|
|
process.write(problemCase->input.toUtf8());
|
2022-03-31 09:56:49 +08:00
|
|
|
process.waitForFinished(0);
|
2021-11-01 09:18:23 +08:00
|
|
|
}
|
2022-03-29 18:06:24 +08:00
|
|
|
|
2022-03-27 11:44:52 +08:00
|
|
|
elapsedTimer.start();
|
2021-11-01 09:18:23 +08:00
|
|
|
while (true) {
|
2022-03-31 09:56:49 +08:00
|
|
|
if (process.bytesToWrite()==0 && !writeChannelClosed) {
|
|
|
|
writeChannelClosed = true;
|
|
|
|
process.closeWriteChannel();
|
|
|
|
}
|
2022-01-28 08:21:56 +08:00
|
|
|
process.waitForFinished(mWaitForFinishTime);
|
2021-11-01 09:18:23 +08:00
|
|
|
if (process.state()!=QProcess::Running) {
|
|
|
|
break;
|
|
|
|
}
|
2022-03-27 11:44:52 +08:00
|
|
|
if (mExecTimeout>0) {
|
|
|
|
int msec = elapsedTimer.elapsed();
|
|
|
|
if (msec>mExecTimeout) {
|
2022-03-30 19:28:46 +08:00
|
|
|
execTimeouted=true;
|
2022-03-27 11:44:52 +08:00
|
|
|
}
|
|
|
|
}
|
2022-03-30 19:28:46 +08:00
|
|
|
if (mStop || execTimeouted) {
|
2021-11-01 09:18:23 +08:00
|
|
|
process.terminate();
|
|
|
|
process.kill();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (errorOccurred)
|
|
|
|
break;
|
2023-07-12 12:08:26 +08:00
|
|
|
if (pSettings->executor().redirectStderrToToolLog()) {
|
|
|
|
QString s = QString::fromLocal8Bit(process.readAllStandardError());
|
|
|
|
if (!s.isEmpty())
|
|
|
|
emit logStderrOutput(s);
|
|
|
|
}
|
2022-03-30 19:28:46 +08:00
|
|
|
readed = process.read(mBufferSize);
|
|
|
|
buffer += readed;
|
2022-01-28 08:21:56 +08:00
|
|
|
if (buffer.length()>=mBufferSize || noOutputTime > mOutputRefreshTime) {
|
|
|
|
if (!buffer.isEmpty()) {
|
|
|
|
emit newOutputGetted(problemCase->getId(),QString::fromLocal8Bit(buffer));
|
|
|
|
output.append(buffer);
|
|
|
|
buffer.clear();
|
|
|
|
}
|
|
|
|
noOutputTime = 0;
|
|
|
|
} else {
|
|
|
|
noOutputTime += mWaitForFinishTime;
|
2021-12-15 19:12:16 +08:00
|
|
|
}
|
|
|
|
}
|
2022-03-29 18:06:24 +08:00
|
|
|
problemCase->runningTime=elapsedTimer.elapsed();
|
2022-12-13 08:49:20 +08:00
|
|
|
problemCase->runningMemory = 0;
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
if (hProcess!=NULL) {
|
|
|
|
PROCESS_MEMORY_COUNTERS counter{0};
|
|
|
|
counter.cb = sizeof(counter);
|
|
|
|
if (GetProcessMemoryInfo(hProcess,&counter,
|
|
|
|
sizeof(counter))){
|
|
|
|
problemCase->runningMemory = counter.PeakWorkingSetSize;
|
|
|
|
}
|
2023-02-07 20:27:31 +08:00
|
|
|
FILETIME creationTime;
|
|
|
|
FILETIME exitTime;
|
|
|
|
FILETIME kernelTime;
|
|
|
|
FILETIME userTime;
|
|
|
|
if (GetProcessTimes(hProcess,&creationTime,&exitTime,&kernelTime,&userTime)) {
|
|
|
|
LONGLONG t=((LONGLONG)kernelTime.dwHighDateTime<<32)
|
|
|
|
+((LONGLONG)userTime.dwHighDateTime<<32)
|
|
|
|
+(kernelTime.dwLowDateTime)+(userTime.dwLowDateTime);
|
|
|
|
problemCase->runningTime=(double)t/10000;
|
|
|
|
}
|
2022-12-13 08:49:20 +08:00
|
|
|
}
|
|
|
|
#endif
|
2022-03-30 19:28:46 +08:00
|
|
|
if (execTimeouted) {
|
2022-12-13 08:49:20 +08:00
|
|
|
problemCase->output = tr("Time limit exceeded!");
|
|
|
|
emit resetOutput(problemCase->getId(), problemCase->output);
|
|
|
|
} else if (mMemoryLimit>0 && problemCase->runningMemory>mMemoryLimit) {
|
|
|
|
problemCase->output = tr("Memory limit exceeded!");
|
2022-03-27 11:44:52 +08:00
|
|
|
emit resetOutput(problemCase->getId(), problemCase->output);
|
|
|
|
} else {
|
2023-07-12 12:08:26 +08:00
|
|
|
if (pSettings->executor().redirectStderrToToolLog()) {
|
|
|
|
QString s = QString::fromLocal8Bit(process.readAllStandardError());
|
|
|
|
if (!s.isEmpty())
|
|
|
|
emit logStderrOutput(s);
|
|
|
|
}
|
2022-03-27 11:44:52 +08:00
|
|
|
if (process.state() == QProcess::ProcessState::NotRunning)
|
|
|
|
buffer += process.readAll();
|
|
|
|
emit newOutputGetted(problemCase->getId(),QString::fromLocal8Bit(buffer));
|
|
|
|
output.append(buffer);
|
2022-03-30 19:28:46 +08:00
|
|
|
problemCase->output = QString::fromLocal8Bit(output);
|
2022-12-13 08:49:20 +08:00
|
|
|
|
2022-03-27 11:44:52 +08:00
|
|
|
if (errorOccurred) {
|
|
|
|
//qDebug()<<"process error:"<<process.error();
|
|
|
|
switch (process.error()) {
|
|
|
|
case QProcess::FailedToStart:
|
|
|
|
emit runErrorOccurred(tr("The runner process '%1' failed to start.").arg(mFilename));
|
|
|
|
break;
|
|
|
|
// case QProcess::Crashed:
|
|
|
|
// if (!mStop)
|
|
|
|
// emit runErrorOccurred(tr("The runner process crashed after starting successfully."));
|
|
|
|
// break;
|
|
|
|
case QProcess::Timedout:
|
|
|
|
emit runErrorOccurred(tr("The last waitFor...() function timed out."));
|
|
|
|
break;
|
|
|
|
case QProcess::WriteError:
|
|
|
|
emit runErrorOccurred(tr("An error occurred when attempting to write to the runner process."));
|
|
|
|
break;
|
|
|
|
case QProcess::ReadError:
|
|
|
|
emit runErrorOccurred(tr("An error occurred when attempting to read from the runner process."));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-11-01 09:18:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OJProblemCasesRunner::run()
|
|
|
|
{
|
|
|
|
emit started();
|
|
|
|
auto action = finally([this]{
|
|
|
|
emit terminated();
|
|
|
|
});
|
2021-12-16 10:46:38 +08:00
|
|
|
for (int i=0; i < mProblemCases.size(); i++) {
|
2021-11-01 09:18:23 +08:00
|
|
|
if (mStop)
|
|
|
|
break;
|
2021-12-16 10:46:38 +08:00
|
|
|
POJProblemCase problemCase = mProblemCases[i];
|
2021-11-01 09:18:23 +08:00
|
|
|
runCase(i,problemCase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-27 11:44:52 +08:00
|
|
|
int OJProblemCasesRunner::execTimeout() const
|
|
|
|
{
|
|
|
|
return mExecTimeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OJProblemCasesRunner::setExecTimeout(int newExecTimeout)
|
|
|
|
{
|
|
|
|
mExecTimeout = newExecTimeout;
|
|
|
|
}
|
|
|
|
|
2022-12-13 08:49:20 +08:00
|
|
|
void OJProblemCasesRunner::setMemoryLimit(size_t limit)
|
|
|
|
{
|
|
|
|
mMemoryLimit = limit;
|
|
|
|
}
|
|
|
|
|
2022-01-28 08:21:56 +08:00
|
|
|
int OJProblemCasesRunner::waitForFinishTime() const
|
|
|
|
{
|
|
|
|
return mWaitForFinishTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OJProblemCasesRunner::setWaitForFinishTime(int newWaitForFinishTime)
|
|
|
|
{
|
|
|
|
mWaitForFinishTime = newWaitForFinishTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
int OJProblemCasesRunner::outputRefreshTime() const
|
|
|
|
{
|
|
|
|
return mOutputRefreshTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OJProblemCasesRunner::setOutputRefreshTime(int newOutputRefreshTime)
|
|
|
|
{
|
|
|
|
mOutputRefreshTime = newOutputRefreshTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
int OJProblemCasesRunner::bufferSize() const
|
|
|
|
{
|
|
|
|
return mBufferSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OJProblemCasesRunner::setBufferSize(int newBufferSize)
|
|
|
|
{
|
|
|
|
mBufferSize = newBufferSize;
|
|
|
|
}
|
|
|
|
|
2021-11-01 09:18:23 +08:00
|
|
|
|