CIRCT 21.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 <stdexcept>
25
26using namespace esi;
27
28void printInfo(std::ostream &os, AcceleratorConnection &acc, bool details);
29void printHier(std::ostream &os, AcceleratorConnection &acc, bool details);
30void printTelemetry(std::ostream &os, AcceleratorConnection &acc);
31
32int main(int argc, const char *argv[]) {
33 CliParser cli("esiquery");
34 cli.description("Query an ESI system for information from the manifest.");
35
36 CLI::App *versionSub =
37 cli.add_subcommand("version", "Print ESI system version");
38 bool infoDetails = false;
39 CLI::App *infoSub =
40 cli.add_subcommand("info", "Print ESI system information");
41 infoSub->add_flag("--details", infoDetails,
42 "Print detailed information about the system");
43 bool hierDetails = false;
44 CLI::App *hierSub = cli.add_subcommand("hier", "Print ESI system hierarchy");
45 hierSub->add_flag("--details", hierDetails,
46 "Print detailed information about the system");
47 CLI::App *telemetrySub =
48 cli.add_subcommand("telemetry", "Print ESI system telemetry information");
49
50 if (int rc = cli.esiParse(argc, argv))
51 return rc;
52 if (!cli.get_help_ptr()->empty())
53 return 0;
54
55 Context &ctxt = cli.getContext();
56 try {
57 std::unique_ptr<AcceleratorConnection> acc = cli.connect();
58 const auto &info = *acc->getService<services::SysInfo>();
59
60 if (*versionSub)
61 std::cout << info.getEsiVersion() << std::endl;
62 else if (*infoSub)
63 printInfo(std::cout, *acc, infoDetails);
64 else if (*hierSub)
65 printHier(std::cout, *acc, hierDetails);
66 else if (*telemetrySub)
67 printTelemetry(std::cout, *acc);
68 return 0;
69 } catch (std::exception &e) {
70 ctxt.getLogger().error("esiquery", e.what());
71 return -1;
72 }
73}
74
75void printInfo(std::ostream &os, AcceleratorConnection &acc, bool details) {
76 std::string jsonManifest =
77 acc.getService<services::SysInfo>()->getJsonManifest();
78 Manifest m(acc.getCtxt(), jsonManifest);
79 os << "API version: " << m.getApiVersion() << std::endl << std::endl;
80 os << "********************************" << std::endl;
81 os << "* Module information" << std::endl;
82 os << "********************************" << std::endl;
83 os << std::endl;
84 for (ModuleInfo mod : m.getModuleInfos())
85 os << "- " << mod;
86
87 if (!details)
88 return;
89
90 os << std::endl;
91 os << "********************************" << std::endl;
92 os << "* Type table" << std::endl;
93 os << "********************************" << std::endl;
94 os << std::endl;
95 size_t i = 0;
96 for (const Type *t : m.getTypeTable())
97 os << " " << i++ << ": " << t->getID() << std::endl;
98}
99
100static bool showPort(const BundlePort &port, bool details) {
101 return details ||
102 (!port.getID().name.starts_with("__") && !port.getChannels().empty());
103}
104
105void printPort(std::ostream &os, const BundlePort &port, std::string indent,
106 bool details) {
107 if (!showPort(port, details))
108 return;
109 os << indent << " " << port.getID() << ":";
110 if (auto svcPort = dynamic_cast<const services::ServicePort *>(&port))
111 if (auto svcPortStr = svcPort->toString()) {
112 os << " " << *svcPortStr << std::endl;
113 return;
114 }
115 os << std::endl;
116 for (const auto &[name, chan] : port.getChannels())
117 os << indent << " " << name << ": " << chan.getType()->getID()
118 << std::endl;
119}
120
121void printInstance(std::ostream &os, const HWModule *d, std::string indent,
122 bool details) {
123 bool hasPorts =
124 std::any_of(d->getPorts().begin(), d->getPorts().end(),
125 [&](const std::pair<const AppID, BundlePort &> port) {
126 return showPort(port.second, details);
127 });
128 if (!details && !hasPorts && d->getChildren().empty())
129 return;
130 os << indent << "* Instance: ";
131 if (auto inst = dynamic_cast<const Instance *>(d)) {
132 os << inst->getID() << std::endl;
133 if (inst->getInfo() && inst->getInfo()->name)
134 os << indent << "* Module: " << *inst->getInfo()->name << std::endl;
135 } else {
136 os << "top" << std::endl;
137 }
138
139 os << indent << "* Ports:" << std::endl;
140 for (const BundlePort &port : d->getPortsOrdered())
141 printPort(os, port, indent + " ", details);
142 std::vector<const Instance *> children = d->getChildrenOrdered();
143 if (!children.empty()) {
144 os << indent << "* Children:" << std::endl;
145 for (const Instance *child : d->getChildrenOrdered())
146 printInstance(os, child, indent + " ", details);
147 }
148 os << std::endl;
149}
150
151void printHier(std::ostream &os, AcceleratorConnection &acc, bool details) {
152 Manifest manifest(acc.getCtxt(),
154 Accelerator *design = manifest.buildAccelerator(acc);
155 os << "********************************" << std::endl;
156 os << "* Design hierarchy" << std::endl;
157 os << "********************************" << std::endl;
158 os << std::endl;
159 printInstance(os, design, /*indent=*/"", details);
160}
161
162void printTelemetry(std::ostream &os, AcceleratorConnection &acc) {
163 Manifest manifest(acc.getCtxt(),
165 auto accel = manifest.buildAccelerator(acc);
166 acc.getServiceThread()->addPoll(*accel);
167
168 auto telemetry = acc.getService<services::TelemetryService>();
169 if (!telemetry) {
170 os << "No telemetry service found" << std::endl;
171 return;
172 }
173 os << "********************************" << std::endl;
174 os << "* Telemetry" << std::endl;
175 os << "********************************" << std::endl;
176 os << std::endl;
177
178 const std::map<AppIDPath, services::TelemetryService::Telemetry *>
179 &telemetryPorts = telemetry->getTelemetryPorts();
180 for (const auto &[id, port] : telemetryPorts) {
181 port->connect();
182 os << id << ": ";
183 os.flush();
184 uint64_t value = *port->read().get().as<uint64_t>();
185 os << value << std::endl;
186 }
187}
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
std::unique_ptr< AcceleratorConnection > connect()
Connect to the accelerator using the specified backend and connection.
Definition CLI.h:58
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:31
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
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:27
Add a custom interface to a service client at a particular point in the design hierarchy.
Definition Services.h:36
Information about the Accelerator system.
Definition Services.h:100
virtual std::string getJsonManifest() const
Return the JSON-formatted system manifest.
Definition Services.cpp:40
Service for retrieving telemetry data from the accelerator.
Definition Services.h:341
const std::map< AppIDPath, Telemetry * > & getTelemetryPorts()
Definition Services.h:376
void printHier(std::ostream &os, AcceleratorConnection &acc, bool details)
Definition esiquery.cpp:151
void printInstance(std::ostream &os, const HWModule *d, std::string indent, bool details)
Definition esiquery.cpp:121
void printPort(std::ostream &os, const BundlePort &port, std::string indent, bool details)
Definition esiquery.cpp:105
void printInfo(std::ostream &os, AcceleratorConnection &acc, bool details)
Definition esiquery.cpp:75
void printTelemetry(std::ostream &os, AcceleratorConnection &acc)
Definition esiquery.cpp:162
static bool showPort(const BundlePort &port, bool details)
Definition esiquery.cpp:100
int main(int argc, const char *argv[])
Definition esiquery.cpp:32
Definition esi.py:1
std::string name
Definition Common.h:35