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#include <iostream>
26#include <string>
27#include <utility>
28#include <vector>
29
30namespace esi_test {
31
32using ProbeFn = int (*)(esi::Accelerator *);
33using ProbeEntry = std::pair<std::string, ProbeFn>;
34
35/// Run the probe-runner main loop. Returns 0 on success, nonzero on failure.
36inline int runProbes(int argc, const char *argv[], const char *name,
37 const char *description,
38 const std::vector<ProbeEntry> &probes) {
39 esi::CliParser cli(name);
40 cli.description(description);
41 std::string probeName;
42 cli.add_option("--probe", probeName,
43 "Run only the named probe. Without this flag, every probe "
44 "runs in sequence.");
45 if (int rc = cli.esiParse(argc, argv))
46 return rc;
47 if (!cli.get_help_ptr()->empty())
48 return 0;
49
50 esi::Context &ctxt = cli.getContext();
52 try {
53 const auto &info = *conn->getService<esi::services::SysInfo>();
54 esi::Manifest manifest(ctxt, info.getJsonManifest());
55 esi::Accelerator *accel = manifest.buildAccelerator(*conn);
56 conn->getServiceThread()->addPoll(*accel);
57
58 int rc = 0;
59 if (probeName.empty()) {
60 for (const auto &[pname, fn] : probes) {
61 rc = fn(accel);
62 if (rc)
63 break;
64 }
65 } else {
66 ProbeFn fn = nullptr;
67 for (const auto &[pname, candidate] : probes) {
68 if (pname == probeName) {
69 fn = candidate;
70 break;
71 }
72 }
73 if (!fn) {
74 std::cerr << name << ": unknown probe '" << probeName << "'\n";
75 conn->disconnect();
76 return 2;
77 }
78 rc = fn(accel);
79 }
80
81 conn->disconnect();
82 return rc;
83 } catch (std::exception &e) {
84 ctxt.getLogger().error(name, e.what());
85 conn->disconnect();
86 return 1;
87 }
88}
89
90} // namespace esi_test
91
92/// Convenience macro: defines ``main()`` with a probe registry.
93/// The variadic arguments are ``{"name", &fn}`` pairs.
94#define ESI_PROBE_REGISTRY(name, description, ...) \
95 int main(int argc, const char *argv[]) { \
96 static const std::vector<esi_test::ProbeEntry> kProbes = {__VA_ARGS__}; \
97 return esi_test::runProbes(argc, argv, name, description, kProbes); \
98 }
Abstract class representing a connection to an accelerator.
Definition Accelerator.h:89
Top level accelerator class.
Definition Accelerator.h:70
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.
int(*)(esi::Accelerator *) ProbeFn
std::pair< std::string, ProbeFn > ProbeEntry