CIRCT 23.0.0git
Loading...
Searching...
No Matches
probe_runner.h
Go to the documentation of this file.
1// Shared probe-runner framework for ESI integration tests. Each test binary
2// defines a set of named probe functions and registers them via
3// ESI_PROBE_REGISTRY. The runner provides:
4//
5// * ``--probe NAME`` to run a single probe (used by pytest for isolation)
6// * ``--probe all`` or no ``--probe`` to run every probe in order
7// * Connection setup, manifest loading, and error handling boilerplate
8//
9// Usage:
10//
11// static int myProbe(esi::Accelerator *accel) { ... return 0; }
12//
13// ESI_PROBE_REGISTRY("my_binary", "Description.",
14// {"probe_a", &myProbe},
15// {"probe_b", &otherProbe},
16// );
17
18#pragma once
19
20#include "esi/Accelerator.h"
21#include "esi/CLI.h"
22#include "esi/Manifest.h"
23#include "esi/Services.h"
24
25#ifdef _MSC_VER
26#include <crtdbg.h>
27#include <cstdlib>
28#endif
29#include <iostream>
30#include <string>
31#include <utility>
32#include <vector>
33
34namespace esi_test {
35
36using ProbeFn = int (*)(esi::Accelerator *);
37using ProbeEntry = std::pair<std::string, ProbeFn>;
38
40#if defined(_MSC_VER) && defined(_DEBUG)
41 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
42 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
43 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
44 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
45 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
46 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
47 _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
48#endif
49}
50
51/// Run the probe-runner main loop. Returns 0 on success, nonzero on failure.
52inline int runProbes(int argc, const char *argv[], const char *name,
53 const char *description,
54 const std::vector<ProbeEntry> &probes) {
55 esi::CliParser cli(name);
56 cli.description(description);
57 std::string probeName;
58 cli.add_option("--probe", probeName,
59 "Run only the named probe. Without this flag, every probe "
60 "runs in sequence.");
61 if (int rc = cli.esiParse(argc, argv))
62 return rc;
63 if (!cli.get_help_ptr()->empty())
64 return 0;
65
66 esi::Context &ctxt = cli.getContext();
68 try {
69 const auto &info = *conn->getService<esi::services::SysInfo>();
70 esi::Manifest manifest(ctxt, info.getJsonManifest());
71 esi::Accelerator *accel = manifest.buildAccelerator(*conn);
72 conn->getServiceThread()->addPoll(*accel);
73
74 int rc = 0;
75 if (probeName.empty()) {
76 for (const auto &[pname, fn] : probes) {
77 rc = fn(accel);
78 if (rc)
79 break;
80 }
81 } else {
82 ProbeFn fn = nullptr;
83 for (const auto &[pname, candidate] : probes) {
84 if (pname == probeName) {
85 fn = candidate;
86 break;
87 }
88 }
89 if (!fn) {
90 std::cerr << name << ": unknown probe '" << probeName << "'\n";
91 conn->disconnect();
92 return 2;
93 }
94 rc = fn(accel);
95 }
96
97 conn->disconnect();
98 return rc;
99 } catch (std::exception &e) {
100 ctxt.getLogger().error(name, e.what());
101 conn->disconnect();
102 return 1;
103 }
104}
105
106} // namespace esi_test
107
108/// Convenience macro: defines ``main()`` with a probe registry.
109/// The variadic arguments are ``{"name", &fn}`` pairs.
110#define ESI_PROBE_REGISTRY(name, description, ...) \
111 int main(int argc, const char *argv[]) { \
112 esi_test::configureMsvcDebugReports(); \
113 static const std::vector<esi_test::ProbeEntry> kProbes = {__VA_ARGS__}; \
114 return esi_test::runProbes(argc, argv, name, description, kProbes); \
115 }
Abstract class representing a connection to an accelerator.
Definition Accelerator.h:96
Top level accelerator class.
Definition Accelerator.h:77
Common options and code for ESI runtime tools.
Definition CLI.h:29
Context & getContext()
Get the context.
Definition CLI.h:69
AcceleratorConnection * connect()
Connect to the accelerator using the specified backend and connection.
Definition CLI.h:66
int esiParse(int argc, const char **argv)
Run the parser.
Definition CLI.h:52
AcceleratorConnections, Accelerators, and Manifests must all share a context.
Definition Context.h:34
Logger & getLogger()
Definition Context.h:69
virtual void error(const std::string &subsystem, const std::string &msg, const std::map< std::string, std::any > *details=nullptr)
Report an error.
Definition Logging.h:64
Class to parse a manifest.
Definition Manifest.h:39
Information about the Accelerator system.
Definition Services.h:113
int runProbes(int argc, const char *argv[], const char *name, const char *description, const std::vector< ProbeEntry > &probes)
Run the probe-runner main loop. Returns 0 on success, nonzero on failure.
void configureMsvcDebugReports()
int(*)(esi::Accelerator *) ProbeFn
std::pair< std::string, ProbeFn > ProbeEntry