35class TraceChannelPort;
46 engine = std::make_unique<TraceEngine>(*
this);
48 throw std::runtime_error(
"manifest file '" +
manifestJson.string() +
55 throw std::runtime_error(
"failed to open trace file '" +
60 assert(
false &&
"not implemented");
77 void write(
const AppIDPath &
id,
const std::string &portName,
const void *data,
78 size_t size,
const std::string &prefix =
"");
79 std::ostream &
write(std::string service) {
90 std::vector<std::unique_ptr<ChannelPort>>
channels;
94void TraceAccelerator::Impl::write(
const AppIDPath &
id,
95 const std::string &portName,
96 const void *data,
size_t size,
97 const std::string &prefix) {
103 *
traceWrite << prefix << (prefix.empty() ?
"w" :
"W") <<
"rite " <<
id <<
'.'
104 << portName <<
": " << b64data << std::endl;
107std::unique_ptr<AcceleratorConnection>
110 std::string manifestPath;
111 std::string traceFile =
"trace.log";
115 std::regex connPattern(
"([\\w-]):([^:]+)(:(\\w+))?");
117 if (regex_search(connectionString, match, connPattern)) {
119 manifestPath = match[2];
120 if (match[3].matched)
121 traceFile = match[3];
123 throw std::runtime_error(
"connection std::string must be of the form "
124 "'<mode>:<manifest path>[:<traceFile>]'");
131 else if (modeStr ==
"-")
134 throw std::runtime_error(
"unknown mode '" + modeStr +
"'");
136 return std::make_unique<TraceAccelerator>(
ctxt, mode,
137 std::filesystem::path(manifestPath),
138 std::filesystem::path(traceFile));
142 std::filesystem::path manifestJson,
143 std::filesystem::path traceFile)
145 impl = std::make_unique<Impl>(mode, manifestJson, traceFile);
150class TraceSysInfo :
public SysInfo {
153 :
SysInfo(conn), manifestJson(manifestJson) {}
155 uint32_t getEsiVersion()
const override {
return ESIVersion; }
157 std::string getJsonManifest()
const override {
159 std::ifstream manifest(manifestJson);
160 if (!manifest.is_open())
161 throw std::runtime_error(
"failed to open manifest file '" +
162 manifestJson.string() +
"'");
163 std::stringstream buffer;
164 buffer << manifest.rdbuf();
169 std::vector<uint8_t> getCompressedManifest()
const override {
170 throw std::runtime_error(
171 "compressed manifest not supported by trace backend");
175 std::filesystem::path manifestJson;
183 const AppIDPath &
id,
const std::string &portName)
186 virtual void write(
const MessageData &data)
override {
187 impl.write(
id, portName,
data.getBytes(),
data.getSize());
191 impl.write(
id, portName,
data.getBytes(),
data.getSize(),
"try");
198 std::string portName;
207 ~ReadTraceChannelPort() { disconnect(); }
211 std::ptrdiff_t numBits = getType()->getBitWidth();
214 throw std::runtime_error(
"unsupported type for read: " +
217 std::ptrdiff_t size = (numBits + 7) / 8;
218 std::vector<uint8_t> bytes(size);
219 for (std::ptrdiff_t i = 0; i < size; ++i)
220 bytes[i] = rand() % 256;
224 bool pollImpl()
override {
return callback(genMessage()); }
229class TraceEngine :
public Engine {
233 std::unique_ptr<ChannelPort> createPort(
AppIDPath idPath,
234 const std::string &channelName,
236 const Type *type)
override {
237 std::unique_ptr<ChannelPort> port;
238 if (BundlePort::isWrite(dir))
239 port = std::make_unique<WriteTraceChannelPort>(impl, type, idPath,
242 port = std::make_unique<ReadTraceChannelPort>(impl, type);
261 :
MMIO(conn, clients), impl(conn.getImpl()) {}
263 virtual uint64_t
read(uint32_t addr)
const override {
264 uint64_t data = rand();
265 if (impl.isWriteable())
266 impl.write(
"MMIO") <<
"[" << std::hex << addr <<
"] -> " << data
270 virtual void write(uint32_t addr, uint64_t data)
override {
271 if (!impl.isWriteable())
273 impl.write(
"MMIO") <<
"[" << std::hex << addr <<
"] <- " << data
292 if (impl.isWriteable())
293 impl.write(
"HostMem") <<
"free " << ptr << std::endl;
296 virtual void *
getPtr()
const override {
return ptr; }
297 virtual std::size_t
getSize()
const override {
return size; }
305 virtual std::unique_ptr<HostMemRegion>
309 if (impl.isWriteable())
310 impl.write(
"HostMem 0x")
311 << ret->getPtr() <<
" allocate " << size
312 <<
" bytes. Writeable: " << opts.
writeable
319 if (impl.isWriteable())
320 impl.write(
"HostMem")
321 <<
"map 0x" << ptr <<
" size " << size
322 <<
" bytes. Writeable: " << opts.
writeable
327 if (impl.isWriteable())
328 impl.write(
"HostMem") <<
"unmap 0x" << ptr << std::endl;
339 if (svcType ==
typeid(
SysInfo))
340 return new TraceSysInfo(*
this,
getImpl().manifestJson);
341 if (svcType ==
typeid(
MMIO))
343 if (svcType ==
typeid(
HostMem))
#define REGISTER_ACCELERATOR(Name, TAccelerator)
assert(baseType &&"element must be base type")
constexpr uint32_t ESIVersion
virtual std::unique_ptr< HostMemRegion > allocate(std::size_t size, HostMem::Options opts) const override
Allocate a region of host memory in accelerator accessible address space.
TraceHostMem(TraceAccelerator &conn)
virtual bool mapMemory(void *ptr, std::size_t size, HostMem::Options opts) const override
Try to make a region of host memory accessible to the accelerator.
virtual void unmapMemory(void *ptr) const override
Unmap memory which was previously mapped with 'mapMemory'.
TraceAccelerator::Impl & impl
virtual uint64_t read(uint32_t addr) const override
Read a 64-bit value from the global MMIO space.
virtual void write(uint32_t addr, uint64_t data) override
Write a 64-bit value to the global MMIO space.
TraceAccelerator::Impl & impl
TraceMMIO(TraceAccelerator &conn, const HWClientDetails &clients)
Abstract class representing a connection to an accelerator.
virtual void disconnect()
Disconnect from the accelerator cleanly.
Context & ctxt
ESI accelerator context.
void registerEngine(AppIDPath idPath, std::unique_ptr< Engine > engine, const HWClientDetails &clients)
If createEngine is overridden, this method should be called to register the engine and all of the cha...
Unidirectional channels are the basic communication primitive between the host and accelerator.
AcceleratorConnections, Accelerators, and Manifests must all share a context.
Engines implement the actual channel communication between the host and the accelerator.
A logical chunk of data representing serialized data.
A ChannelPort which reads data from the accelerator.
Root class of the ESI type system.
A ChannelPort which sends data to the accelerator.
Connect to an ESI simulation.
std::unique_ptr< Impl > impl
TraceAccelerator(Context &, Mode mode, std::filesystem::path manifestJson, std::filesystem::path traceFile)
Create a trace-based accelerator backend.
void createEngine(const std::string &engineTypeName, AppIDPath idPath, const ServiceImplDetails &details, const HWClientDetails &clients) override
Create a new engine for channel communication with the accelerator.
~TraceAccelerator() override
virtual Service * createService(Service::Type service, AppIDPath idPath, std::string implName, const ServiceImplDetails &details, const HWClientDetails &clients) override
Called by getServiceImpl exclusively.
static std::unique_ptr< AcceleratorConnection > connect(Context &, std::string connectionString)
Parse the connection string and instantiate the accelerator.
Parent class of all APIs modeled as 'services'.
const std::type_info & Type
Information about the Accelerator system.
void encodeBase64(const void *data, size_t size, std::string &out)
std::map< std::string, std::any > ServiceImplDetails
std::vector< HWClientDetail > HWClientDetails
virtual std::size_t getSize() const override
TraceHostMemRegion(std::size_t size, TraceAccelerator::Impl &impl)
virtual ~TraceHostMemRegion()
virtual void * getPtr() const override
Get a pointer to the host memory.
TraceAccelerator::Impl & impl
std::ofstream * traceWrite
void write(const AppIDPath &id, const std::string &portName, const void *data, size_t size, const std::string &prefix="")
std::vector< std::unique_ptr< ChannelPort > > channels
std::filesystem::path manifestJson
std::ostream & write(std::string service)
void adoptChannelPort(ChannelPort *port)
Service * createService(TraceAccelerator &conn, Service::Type svcType, AppIDPath idPath, const ServiceImplDetails &details, const HWClientDetails &clients)
std::filesystem::path traceFile
Impl(Mode mode, std::filesystem::path manifestJson, std::filesystem::path traceFile)
std::unique_ptr< TraceEngine > engine
RAII memory region for host memory.
Options for allocating host memory.