Skip to content

Commit

Permalink
Add a silent mode to P4Tools. Add performance counting at the top level.
Browse files Browse the repository at this point in the history
  • Loading branch information
fruffy committed Feb 8, 2024
1 parent 04fc28a commit 44b677b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
8 changes: 8 additions & 0 deletions backends/p4tools/common/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ AbstractP4cToolOptions::AbstractP4cToolOptions(cstring message) : Options(messag
},
"Provides a randomization seed");

registerOption(
"--disable-info-logging", nullptr,
[this](const char * /*arg*/) {
disableInformationLogging = true;
return true;
},
"Disable printing of information messages to standard output.");

for (const auto &optionSpec : inheritedCompilerOptions) {
registerOption(
optionSpec.option, optionSpec.argName,
Expand Down
3 changes: 3 additions & 0 deletions backends/p4tools/common/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class AbstractP4cToolOptions : protected Util::Options {
/// A seed for the PRNG.
std::optional<uint32_t> seed = std::nullopt;

/// Disable information logging.
bool disableInformationLogging = false;

/// Processes options.
///
/// @returns a compilation context on success, std::nullopt on error.
Expand Down
14 changes: 12 additions & 2 deletions backends/p4tools/common/p4ctool.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
#define BACKENDS_P4TOOLS_COMMON_P4CTOOL_H_

#include <cstdlib>
#include <type_traits>
#include <vector>

#include "backends/p4tools/common/compiler/compiler_target.h"
#include "backends/p4tools/common/lib/logging.h"
#include "backends/p4tools/common/options.h"

namespace P4Tools {

/// Abstract class for all compiler-based tools. Implementations should instantiate this template
/// on a subclass of AbstractP4cToolOptions.
//
// Because of limitations of templates, method implementations must be inlined here.
template <class Options>
template <class Options,
typename = std::enable_if_t<std::is_base_of_v<AbstractP4cToolOptions, Options>>>
class AbstractP4cTool {
protected:
/// Provides the implementation of the tool.
Expand All @@ -31,14 +35,20 @@ class AbstractP4cTool {
registerTarget();

// Process command-line options.
auto compileContext = Options::get().process(args);
auto &toolOptions = Options::get();
auto compileContext = toolOptions.process(args);
if (!compileContext) {
return 1;
}

// Set up the compilation context.
AutoCompileContext autoContext(*compileContext);

// If not explicitly disabled, print basic information to standard output.
if (!toolOptions.disableInformationLogging) {
enableInformationLogging();
}

// Run the compiler to get an IR and invoke the tool.
const auto compilerResult = P4Tools::CompilerTarget::runCompiler();
if (!compilerResult.has_value()) {
Expand Down
18 changes: 12 additions & 6 deletions backends/p4tools/modules/testgen/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include <vector>

#include "lib/crash.h"
#include "lib/timer.h"

#include "backends/p4tools/modules/testgen/lib/logging.h"
#include "backends/p4tools/modules/testgen/testgen.h"

std::string updateErrorMsg(std::string errorMsg) {
Expand All @@ -26,27 +28,31 @@ int main(int argc, char **argv) {
args.push_back(argv[i]);
}

int result = EXIT_SUCCESS;
try {
return P4Tools::P4Testgen::Testgen().main(args);
Util::ScopedTimer timer("P4Testgen Main");
result = P4Tools::P4Testgen::Testgen().main(args);
} catch (const Util::CompilerBug &e) {
std::cerr << "Internal error: " << updateErrorMsg(e.what()) << "\n";
std::cerr << "Please submit a bug report with your code."
<< "\n";
return EXIT_FAILURE;
result = EXIT_FAILURE;
} catch (const Util::CompilerUnimplemented &e) {
std::cerr << updateErrorMsg(e.what()) << "\n";
return EXIT_FAILURE;
result = EXIT_FAILURE;
} catch (const Util::CompilationError &e) {
std::cerr << updateErrorMsg(e.what()) << "\n";
return EXIT_FAILURE;
result = EXIT_FAILURE;
} catch (const std::exception &e) {
std::cerr << "Internal error: " << updateErrorMsg(e.what()) << "\n";
std::cerr << "Please submit a bug report with your code."
<< "\n";
return EXIT_FAILURE;
result = EXIT_FAILURE;
} catch (...) {
std::cerr << "Internal error. Please submit a bug report with your code."
<< "\n";
return EXIT_FAILURE;
result = EXIT_FAILURE;
}
P4Tools::printPerformanceReport();
return result;
}
8 changes: 0 additions & 8 deletions backends/p4tools/modules/testgen/testgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
#include "backends/p4tools/modules/testgen/core/symbolic_executor/selected_branches.h"
#include "backends/p4tools/modules/testgen/core/symbolic_executor/symbolic_executor.h"
#include "backends/p4tools/modules/testgen/core/target.h"
#include "backends/p4tools/modules/testgen/lib/logging.h"
#include "backends/p4tools/modules/testgen/lib/test_backend.h"
#include "backends/p4tools/modules/testgen/options.h"
#include "backends/p4tools/modules/testgen/register.h"

Expand Down Expand Up @@ -81,9 +79,6 @@ int generateAbstractTests(const TestgenOptions &testgenOptions, const ProgramInf

symbex.run(callBack);

// Emit a performance report, if desired.
printPerformanceReport(testPath);

// Do not print this warning if assertion mode is enabled.
if (testBackend->getTestCount() == 0 && !testgenOptions.assertionModeEnabled) {
::warning(
Expand Down Expand Up @@ -116,9 +111,6 @@ int Testgen::mainImpl(const CompilerResult &compilerResult) {
return EXIT_FAILURE;
}

// Print basic information for each test.
enableInformationLogging();

// Get the options and the seed.
const auto &testgenOptions = TestgenOptions::get();
auto seed = Utils::getCurrentSeed();
Expand Down

0 comments on commit 44b677b

Please sign in to comment.