CIRCT 22.0.0git
Loading...
Searching...
No Matches
esiquery.cpp
Go to the documentation of this file.
1//===- esiquery.cpp - ESI accelerator system query tool -------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// DO NOT EDIT!
10// This file is distributed as part of an ESI package. The source for this file
11// should always be modified within CIRCT
12// (lib/dialect/ESI/runtime/cpp/lib/backends/Cosim.cpp).
13//
14//===----------------------------------------------------------------------===//
15
16#include "esi/Accelerator.h"
17#include "esi/CLI.h"
18#include "esi/Manifest.h"
19#include "esi/Services.h"
20
21#include <algorithm>
22#include <iostream>
23#include <map>
24#include <nlohmann/json.hpp>
25#include <stdexcept>
26#include <string>
27
28using namespace esi;
29
30void printInfo(std::ostream &os, AcceleratorConnection &acc, bool details);
31void printHier(std::ostream &os, AcceleratorConnection &acc, bool details);
32void printTelemetry(std::ostream &os, AcceleratorConnection &acc);
33void printTelemetryJson(std::ostream &os, AcceleratorConnection &acc);
34
35int main(int argc, const char *argv[]) {
36 CliParser cli("esiquery");
37 cli.description("Query an ESI system for information from the manifest.");
38
39 CLI::App *versionSub =
40 cli.add_subcommand("version", "Print ESI system version");
41 bool infoDetails = false;
42 CLI::App *infoSub =
43 cli.add_subcommand("info", "Print ESI system information");
44 infoSub->add_flag("--details", infoDetails,
45 "Print detailed information about the system");
46 bool hierDetails = false;
47 CLI::App *hierSub = cli.add_subcommand("hier", "Print ESI system hierarchy");
48 hierSub->add_flag("--details", hierDetails,
49 "Print detailed information about the system");
50 bool telemetryJson = false;
51 CLI::App *telemetrySub =
52 cli.add_subcommand("telemetry", "Print ESI system telemetry information");
53 telemetrySub->add_flag("--json", telemetryJson,
54 "Dump telemetry information as JSON");
55
56 if (int rc = cli.esiParse(argc, argv))
57 return rc;
58 if (!cli.get_help_ptr()->empty())
59 return 0;
60
61 Context &ctxt = cli.getContext();
62 try {
63 AcceleratorConnection *acc = cli.connect();
64 const auto &info = *acc->getService<services::SysInfo>();
65
66 if (*versionSub)
67 std::cout << info.getEsiVersion() << std::endl;
68 else if (*infoSub)
69 printInfo(std::cout, *acc, infoDetails);
70 else if (*hierSub)
71 printHier(std::cout, *acc, hierDetails);
72 else if (*telemetrySub) {
73 if (telemetryJson)
74 printTelemetryJson(std::cout, *acc);
75 else
76 printTelemetry(std::cout, *acc);
77 }
78 return 0;
79 } catch (std::exception &e) {
80 ctxt.getLogger().error("esiquery", e.what());
81 return -1;
82 }
83}
84
85void printInfo(std::ostream &os, AcceleratorConnection &acc, bool details) {
86 std::string jsonManifest =
87 acc.getService<services::SysInfo>()->getJsonManifest();
88 Manifest m(acc.getCtxt(), jsonManifest);
89 os << "API version: " << m.getApiVersion() << std::endl << std::endl;
90 os << "********************************" << std::endl;
91 os << "* Module information" << std::endl;
92 os << "********************************" << std::endl;
93 os << std::endl;
94 for (ModuleInfo mod : m.getModuleInfos())
95 os << "- " << mod;
96
97 if (!details)
98 return;
99
100 os << std::endl;
101 os << "********************************" << std::endl;
102 os << "* Type table" << std::endl;
103 os << "********************************" << std::endl;
104 os << std::endl;
105 size_t i = 0;
106 for (const Type *t : m.getTypeTable())
107 os << " " << i++ << ": " << t->getID() << std::endl;
108}
109
110static bool showPort(const BundlePort &port, bool details) {
111 return details ||
112 (!port.getID().name.starts_with("__") && !port.getChannels().empty());
113}
114
115void printPort(std::ostream &os, const BundlePort &port, std::string indent,
116 bool details) {
117 if (!showPort(port, details))
118 return;
119 os << indent << " " << port.getID() << ":";
120 if (auto svcPort = dynamic_cast<const services::ServicePort *>(&port))
121 if (auto svcPortStr = svcPort->toString()) {
122 os << " " << *svcPortStr << std::endl;
123 return;
124 }
125 os << std::endl;
126 for (const auto &[name, chan] : port.getChannels())
127 os << indent << " " << name << ": " << chan.getType()->getID()
128 << std::endl;
129}
130
131void printInstance(std::ostream &os, const HWModule *d, std::string indent,
132 bool details) {
133 bool hasPorts =
134 std::any_of(d->getPorts().begin(), d->getPorts().end(),
135 [&](const std::pair<const AppID, BundlePort &> port) {
136 return showPort(port.second, details);
137 });
138 if (!details && !hasPorts && d->getChildren().empty())
139 return;
140 os << indent << "* Instance: ";
141 if (auto inst = dynamic_cast<const Instance *>(d)) {
142 os << inst->getID() << std::endl;
143 if (inst->getInfo() && inst->getInfo()->name)
144 os << indent << "* Module: " << *inst->getInfo()->name << std::endl;
145 } else {
146 os << "top" << std::endl;
147 }
148
149 os << indent << "* Ports:" << std::endl;
150 for (const BundlePort &port : d->getPortsOrdered())
151 printPort(os, port, indent + " ", details);
152 std::vector<const Instance *> children = d->getChildrenOrdered();
153 if (!children.empty()) {
154 os << indent << "* Children:" << std::endl;
155 for (const Instance *child : d->getChildrenOrdered())
156 printInstance(os, child, indent + " ", details);
157 }
158 os << std::endl;
159}
160
161void printHier(std::ostream &os, AcceleratorConnection &acc, bool details) {
162 Manifest manifest(acc.getCtxt(),
164 Accelerator *design = manifest.buildAccelerator(acc);
165 os << "********************************" << std::endl;
166 os << "* Design hierarchy" << std::endl;
167 os << "********************************" << std::endl;
168 os << std::endl;
169 printInstance(os, design, /*indent=*/"", details);
170}
171
172// Recursively collect telemetry metrics into a hierarchical JSON structure.
173static bool collectTelemetryJson(const HWModule &module, nlohmann::json &node) {
174 bool hasData = false;
175
176 for (const auto &portRef : module.getPortsOrdered()) {
177 BundlePort &port = portRef.get();
178 if (auto *metric =
179 dynamic_cast<services::TelemetryService::Metric *>(&port)) {
180 metric->connect();
181 node[metric->getID().toString()] = metric->readInt();
182 hasData = true;
183 }
184 }
185
186 for (const Instance *child : module.getChildrenOrdered()) {
187 nlohmann::json childNode = nlohmann::json::object();
188 if (collectTelemetryJson(*child, childNode)) {
189 node[child->getID().toString()] = childNode;
190 hasData = true;
191 }
192 }
193
194 return hasData;
195}
196
197void printTelemetryJson(std::ostream &os, AcceleratorConnection &acc) {
198 Manifest manifest(acc.getCtxt(),
200 auto accel = manifest.buildAccelerator(acc);
201 acc.getServiceThread()->addPoll(*accel);
202
203 nlohmann::json root = nlohmann::json::object();
204 if (!collectTelemetryJson(*accel, root))
205 root = nlohmann::json{{"error", "No telemetry metrics found"}};
206
207 os << root.dump(2) << std::endl;
208}
209
210void printTelemetry(std::ostream &os, AcceleratorConnection &acc) {
211 Manifest manifest(acc.getCtxt(),
213 auto accel = manifest.buildAccelerator(acc);
214 acc.getServiceThread()->addPoll(*accel);
215
216 auto *telemetry = acc.getService<services::TelemetryService>();
217 if (!telemetry) {
218 os << "No telemetry service found" << std::endl;
219 return;
220 }
221 os << "********************************" << std::endl;
222 os << "* Telemetry" << std::endl;
223 os << "********************************" << std::endl;
224 os << std::endl;
225
226 const std::map<AppIDPath, services::TelemetryService::Metric *>
227 &telemetryPorts = telemetry->getTelemetryPorts();
228 for (const auto &[id, port] : telemetryPorts) {
229 port->connect();
230 os << id << ": ";
231 os.flush();
232 uint64_t value = port->readInt();
233 os << value << std::endl;
234 }
235}
Abstract class representing a connection to an accelerator.
Definition Accelerator.h:79
ServiceClass * getService(AppIDPath id={}, std::string implName={}, ServiceImplDetails details={}, HWClientDetails clients={})
Get a typed reference to a particular service type.
Context & getCtxt() const
Definition Accelerator.h:83
AcceleratorServiceThread * getServiceThread()
Return a pointer to the accelerator 'service' thread (or threads).
void addPoll(HWModule &module)
Poll this module.
Top level accelerator class.
Definition Accelerator.h:60
Services provide connections to 'bundles' – collections of named, unidirectional communication channe...
Definition Ports.h:226
const PortMap & getChannels() const
Definition Ports.h:247
AppID getID() const
Get the ID of the port.
Definition Ports.h:239
Common options and code for ESI runtime tools.
Definition CLI.h:29
Context & getContext()
Get the context.
Definition CLI.h:63
AcceleratorConnection * connect()
Connect to the accelerator using the specified backend and connection.
Definition CLI.h:60
int esiParse(int argc, const char **argv)
Run the parser.
Definition CLI.h:46
AcceleratorConnections, Accelerators, and Manifests must all share a context.
Definition Context.h:34
Logger & getLogger()
Definition Context.h:69
Represents either the top level or an instance of a hardware module.
Definition Design.h:47
std::vector< std::reference_wrapper< BundlePort > > getPortsOrdered() const
Get the module's ports in a deterministic order.
Definition Design.h:69
std::vector< const Instance * > getChildrenOrdered() const
Get a vector of the module's children in a deterministic order.
Definition Design.h:60
const std::map< AppID, BundlePort & > & getPorts() const
Access the module's ports by ID.
Definition Design.h:76
const std::map< AppID, Instance * > & getChildren() const
Access the module's children by ID.
Definition Design.h:67
Subclass of HWModule which represents a submodule instance.
Definition Design.h:107
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
uint32_t getApiVersion() const
Accelerator * buildAccelerator(AcceleratorConnection &acc) const
const std::vector< const Type * > & getTypeTable() const
The Type Table is an ordered list of types.
std::vector< ModuleInfo > getModuleInfos() const
Root class of the ESI type system.
Definition Types.h:34
Add a custom interface to a service client at a particular point in the design hierarchy.
Definition Services.h:47
Information about the Accelerator system.
Definition Services.h:111
virtual std::string getJsonManifest() const
Return the JSON-formatted system manifest.
Definition Services.cpp:40
A telemetry port which gets attached to a service port.
Definition Services.h:386
Service for retrieving telemetry data from the accelerator.
Definition Services.h:369
std::map< AppIDPath, Metric * > getTelemetryPorts()
Definition Services.h:410
void printHier(std::ostream &os, AcceleratorConnection &acc, bool details)
Definition esiquery.cpp:161
void printInstance(std::ostream &os, const HWModule *d, std::string indent, bool details)
Definition esiquery.cpp:131
void printPort(std::ostream &os, const BundlePort &port, std::string indent, bool details)
Definition esiquery.cpp:115
void printInfo(std::ostream &os, AcceleratorConnection &acc, bool details)
Definition esiquery.cpp:85
void printTelemetry(std::ostream &os, AcceleratorConnection &acc)
Definition esiquery.cpp:210
static bool showPort(const BundlePort &port, bool details)
Definition esiquery.cpp:110
static bool collectTelemetryJson(const HWModule &module, nlohmann::json &node)
Definition esiquery.cpp:173
int main(int argc, const char *argv[])
Definition esiquery.cpp:35
void printTelemetryJson(std::ostream &os, AcceleratorConnection &acc)
Definition esiquery.cpp:197
Definition esi.py:1
std::string name
Definition Common.h:36