CIRCT  20.0.0git
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/Manifest.h"
18 #include "esi/Services.h"
19 
20 #include <iostream>
21 #include <map>
22 #include <stdexcept>
23 
24 using namespace esi;
25 
26 void printInfo(std::ostream &os, AcceleratorConnection &acc);
27 void printHier(std::ostream &os, AcceleratorConnection &acc);
28 
29 int main(int argc, const char *argv[]) {
30  // TODO: find a command line parser library rather than doing this by hand.
31  if (argc < 3) {
32  std::cerr << "Expected usage: " << argv[0]
33  << " <backend> <connection specifier> [command]" << std::endl;
34  return -1;
35  }
36 
37  Context ctxt;
38 
39  const char *backend = argv[1];
40  const char *conn = argv[2];
41  std::string cmd;
42  if (argc > 3)
43  cmd = argv[3];
44 
45  try {
46  std::unique_ptr<AcceleratorConnection> acc = ctxt.connect(backend, conn);
47  const auto &info = *acc->getService<services::SysInfo>();
48 
49  if (cmd == "version")
50  std::cout << "ESI system version: " << info.getEsiVersion() << std::endl;
51  else if (cmd == "json_manifest")
52  std::cout << info.getJsonManifest() << std::endl;
53  else if (cmd == "info")
54  printInfo(std::cout, *acc);
55  else if (cmd == "hier")
56  printHier(std::cout, *acc);
57  else {
58  std::cout << "Connection successful." << std::endl;
59  if (!cmd.empty()) {
60  std::cerr << "Unknown command: " << cmd << std::endl;
61  return 1;
62  }
63  }
64  return 0;
65  } catch (std::exception &e) {
66  std::cerr << "Error: " << e.what() << std::endl;
67  return -1;
68  }
69 }
70 
71 void printInfo(std::ostream &os, AcceleratorConnection &acc) {
72  std::string jsonManifest =
73  acc.getService<services::SysInfo>()->getJsonManifest();
74  Manifest m(acc.getCtxt(), jsonManifest);
75  os << "API version: " << m.getApiVersion() << std::endl << std::endl;
76  os << "********************************" << std::endl;
77  os << "* Module information" << std::endl;
78  os << "********************************" << std::endl;
79  os << std::endl;
80  for (ModuleInfo mod : m.getModuleInfos())
81  os << "- " << mod;
82 
83  os << std::endl;
84  os << "********************************" << std::endl;
85  os << "* Type table" << std::endl;
86  os << "********************************" << std::endl;
87  os << std::endl;
88  size_t i = 0;
89  for (const Type *t : m.getTypeTable())
90  os << " " << i++ << ": " << t->getID() << std::endl;
91 }
92 
93 void printPort(std::ostream &os, const BundlePort &port,
94  std::string indent = "") {
95  os << indent << " " << port.getID() << ":";
96  if (auto svcPort = dynamic_cast<const services::ServicePort *>(&port))
97  if (auto svcPortStr = svcPort->toString()) {
98  os << " " << *svcPortStr << std::endl;
99  return;
100  }
101  os << std::endl;
102  for (const auto &[name, chan] : port.getChannels())
103  os << indent << " " << name << ": " << chan.getType()->getID()
104  << std::endl;
105 }
106 
107 void printInstance(std::ostream &os, const HWModule *d,
108  std::string indent = "") {
109  os << indent << "* Instance:";
110  if (auto inst = dynamic_cast<const Instance *>(d))
111  os << inst->getID() << std::endl;
112  else
113  os << "top" << std::endl;
114  os << indent << "* Ports:" << std::endl;
115  for (const BundlePort &port : d->getPortsOrdered())
116  printPort(os, port, indent + " ");
117  std::vector<const Instance *> children = d->getChildrenOrdered();
118  if (!children.empty()) {
119  os << indent << "* Children:" << std::endl;
120  for (const Instance *child : d->getChildrenOrdered())
121  printInstance(os, child, indent + " ");
122  }
123  os << std::endl;
124 }
125 
126 void printHier(std::ostream &os, AcceleratorConnection &acc) {
127  Manifest manifest(acc.getCtxt(),
129  Accelerator *design = manifest.buildAccelerator(acc);
130  os << "********************************" << std::endl;
131  os << "* Design hierarchy" << std::endl;
132  os << "********************************" << std::endl;
133  os << std::endl;
134  printInstance(os, design);
135 }
Abstract class representing a connection to an accelerator.
Definition: Accelerator.h:78
Context & getCtxt() const
Definition: Accelerator.h:82
ServiceClass * getService(AppIDPath id={}, std::string implName={}, ServiceImplDetails details={}, HWClientDetails clients={})
Get a typed reference to a particular service type.
Definition: Accelerator.h:112
Top level accelerator class.
Definition: Accelerator.h:59
Services provide connections to 'bundles' – collections of named, unidirectional communication channe...
Definition: Ports.h:184
const std::map< std::string, ChannelPort & > & getChannels() const
Definition: Ports.h:205
AppID getID() const
Get the ID of the port.
Definition: Ports.h:197
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< const Instance * > getChildrenOrdered() const
Get a vector of the module's children in a deterministic order.
Definition: Design.h:60
std::vector< std::reference_wrapper< BundlePort > > getPortsOrdered() const
Get the module's ports in a deterministic order.
Definition: Design.h:69
Subclass of HWModule which represents a submodule instance.
Definition: Design.h:100
Class to parse a manifest.
Definition: Manifest.h:39
const std::vector< const Type * > & getTypeTable() const
The Type Table is an ordered list of types.
uint32_t getApiVersion() const
std::vector< ModuleInfo > getModuleInfos() const
Accelerator * buildAccelerator(AcceleratorConnection &acc) 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:35
Information about the Accelerator system.
Definition: Services.h:93
virtual std::string getJsonManifest() const
Return the JSON-formatted system manifest.
Definition: Services.cpp:40
virtual uint32_t getEsiVersion() const =0
Get the ESI version number to check version compatibility.
void printHier(std::ostream &os, AcceleratorConnection &acc)
Definition: esiquery.cpp:126
void printInfo(std::ostream &os, AcceleratorConnection &acc)
Definition: esiquery.cpp:71
void printInstance(std::ostream &os, const HWModule *d, std::string indent="")
Definition: esiquery.cpp:107
int main(int argc, const char *argv[])
Definition: esiquery.cpp:29
void printPort(std::ostream &os, const BundlePort &port, std::string indent="")
Definition: esiquery.cpp:93
Definition: esi.py:1