Add --timeout option to CLI
MODIFY
src/cmd/cmdoptions.cpp
src/cmd/cmdoptions.cpp
@@ -23,6 +23,11 @@ void addCmdOptions(QCommandLineParser &parser, Ripes::CmdModeOptions &options) {
"ISA extensions to enable (comma "
"separated)",
"isaexts", ""));
+ parser.addOption(QCommandLineOption(
+ "timeout",
+ "Simulation timeout in milliseconds. If simulation does not finish "
+ "within the specified time, it will be aborted.",
+ "timeout", "0"));
parser.addOption(QCommandLineOption("v", "Verbose output"));
parser.addOption(QCommandLineOption(
"output",
@@ -113,6 +118,15 @@ bool parseCmdOptions(QCommandLineParser &parser, QString &errorMessage,
}
}
+ if (parser.isSet("timeout")) {
+ bool ok;
+ options.timeout = parser.value("timeout").toUInt(&ok);
+ if (!ok) {
+ errorMessage = "Invalid timeout value specified (--timeout).";
+ return false;
+ }
+ }
+
options.outputFile = parser.value("output");
// Enable selected telemetry options.

MODIFY
src/cmd/cmdoptions.h
src/cmd/cmdoptions.h
@@ -16,6 +16,7 @@ struct CmdModeOptions {
bool verbose;
QString outputFile;
bool jsonOutput;
+ int timeout;
// A list of enabled telemetry options.
std::vector<std::shared_ptr<Telemetry>> telemetry;

MODIFY
src/cmd/cmdrunner.cpp
src/cmd/cmdrunner.cpp
@@ -103,14 +103,31 @@ int CmdRunner::processInput() {
int CmdRunner::runModel() {
info("Running model", false, true);
- ProcessorHandler::run();
// Wait until receiving ProcessorHandler::runFinished signal
// before proceeding.
QEventLoop loop;
QObject::connect(ProcessorHandler::get(), &ProcessorHandler::runFinished,
&loop, &QEventLoop::quit);
+ bool hadTimeout = false;
+ QTimer timeoutTimer;
+ timeoutTimer.setSingleShot(true);
+ QObject::connect(&timeoutTimer, &QTimer::timeout, &loop, &QEventLoop::quit);
+ QObject::connect(&timeoutTimer, &QTimer::timeout, &loop,
+ [&]() { hadTimeout = true; });
+
+ ProcessorHandler::run();
+ if (m_options.timeout != 0)
+ timeoutTimer.start(m_options.timeout);
loop.exec();
+
+ if (hadTimeout) {
+ ProcessorHandler::stopRun();
+ error("Simulation did not finish within the specified timeout (" +
+ QString::number(m_options.timeout) + " ms)");
+ return 1;
+ }
+
return 0;
}
@@ -157,7 +174,8 @@ int CmdRunner::postRun() {
return 0;
}
-void CmdRunner::info(QString msg, bool alwaysPrint, bool header) {
+void CmdRunner::info(QString msg, bool alwaysPrint, bool header,
+ const QString &prefix) {
if (m_options.verbose || alwaysPrint) {
if (header) {
@@ -171,13 +189,11 @@ void CmdRunner::info(QString msg, bool alwaysPrint, bool header) {
msg.append("=");
}
} else
- msg.prepend("INFO: ");
+ msg.prepend(prefix + ": ");
std::cout << msg.toStdString() << std::endl;
}
}
-void CmdRunner::error(const QString &msg) {
- info("ERROR: " + msg, true, false);
-}
+void CmdRunner::error(const QString &msg) { info(msg, true, false, "ERROR"); }
} // namespace Ripes

MODIFY
src/cmd/cmdrunner.h
src/cmd/cmdrunner.h
@@ -26,7 +26,8 @@ private:
/// Prints requested telemetry to the console/output file.
int postRun();
- void info(QString msg, bool alwaysPrint = false, bool header = false);
+ void info(QString msg, bool alwaysPrint = false, bool header = false,
+ const QString &prefix = "INFO");
void error(const QString &msg);
CmdModeOptions m_options;

Add --timeout option to CLI