CIRCT 24.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
39/// The connection the probes are running against, for probes which need
40/// connection-level services (e.g. HostMem). Set by runProbes(); valid only
41/// while probes run.
43
45#if defined(_MSC_VER) && defined(_DEBUG)
46 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
47 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
48 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
49 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
50 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
51 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
52 _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
53#endif
54}
55
56/// Run the probe-runner main loop. Returns 0 on success, nonzero on failure.
57inline int runProbes(int argc, const char *argv[], const char *name,
58 const char *description,
59 const std::vector<ProbeEntry> &probes) {
60 esi::CliParser cli(name);
61 cli.description(description);
62 std::string probeName;
63 cli.add_option("--probe", probeName,
64 "Run only the named probe. Without this flag, every probe "
65 "runs in sequence.");
66 if (int rc = cli.esiParse(argc, argv))
67 return rc;
68 if (!cli.get_help_ptr()->empty())
69 return 0;
70
71 esi::Context &ctxt = cli.getContext();
73 probeConnection = conn;
74 try {
75 const auto &info = *conn->getService<esi::services::SysInfo>();
76 esi::Manifest manifest(ctxt, info.getJsonManifest());
77 esi::Accelerator *accel = manifest.buildAccelerator(*conn);
78 conn->getServiceThread()->addPoll(*accel);
79
80 int rc = 0;
81 if (probeName.empty()) {
82 for (const auto &[pname, fn] : probes) {
83 rc = fn(accel);
84 if (rc)
85 break;
86 }
87 } else {
88 ProbeFn fn = nullptr;
89 for (const auto &[pname, candidate] : probes) {
90 if (pname == probeName) {
91 fn = candidate;
92 break;
93 }
94 }
95 if (!fn) {
96 std::cerr << name << ": unknown probe '" << probeName << "'\n";
97 conn->disconnect();
98 return 2;
99 }
100 rc = fn(accel);
101 }
102
103 conn->disconnect();
104 return rc;
105 } catch (std::exception &e) {
106 ctxt.getLogger().error(name, e.what());
107 conn->disconnect();
108 return 1;
109 }
110}
111
112} // namespace esi_test
113
114/// Convenience macro: defines ``main()`` with a probe registry.
115/// The variadic arguments are ``{"name", &fn}`` pairs.
116#define ESI_PROBE_REGISTRY(name, description, ...) \
117 int main(int argc, const char *argv[]) { \
118 esi_test::configureMsvcDebugReports(); \
119 static const std::vector<esi_test::ProbeEntry> kProbes = {__VA_ARGS__}; \
120 return esi_test::runProbes(argc, argv, name, description, kProbes); \
121 }
Abstract class representing a connection to an accelerator.
Top level accelerator class.
Definition Accelerator.h:85
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()
std::pair< std::string, ProbeFn > ProbeEntry
int(*)(esi::Accelerator *) ProbeFn
esi::AcceleratorConnection * probeConnection
The connection the probes are running against, for probes which need connection-level services (e....