46 name(std::move(name)) {}
47 ~WriteCosimChannelPort() =
default;
50 if (desc.dir != RpcClient::ChannelDirection::ToServer)
51 throw std::runtime_error(
"Channel '" + name +
52 "' is not a to server channel");
58 for (
const auto &frame : frames) {
59 conn.getLogger().trace(
61 &data](std::string &subsystem, std::string &msg,
62 std::unique_ptr<std::map<std::string, std::any>> &details) {
63 subsystem =
"cosim_write";
64 msg =
"Writing message to channel '" + name +
"'";
65 details = std::make_unique<std::map<std::string, std::any>>();
66 (*details)[
"channel"] = name;
67 (*details)[
"data_size"] =
data.getSize();
68 (*details)[
"message_data"] =
data.toHex();
71 client.writeToServer(name, frame);
102 name(std::move(name)) {}
104 ~ReadCosimChannelPort() =
default;
107 if (desc.dir != RpcClient::ChannelDirection::ToClient)
108 throw std::runtime_error(
"Channel '" + name +
109 "' is not a to client channel");
112 connection = client.connectClientReceiver(
113 name, [
this](std::unique_ptr<SegmentedMessageData> &data) {
115 conn.getLogger().trace(
117 std::string &subsystem, std::string &msg,
118 std::unique_ptr<std::map<std::string, std::any>> &details) {
119 subsystem =
"cosim_read";
120 msg =
"Received message from channel '" + name +
"'";
121 details = std::make_unique<std::map<std::string, std::any>>();
123 (*details)[
"channel"] = name;
124 (*details)[
"data_size"] = flat.getSize();
125 (*details)[
"message_data"] = flat.toHex();
132 conn.getLogger().trace(
134 std::string &subsystem, std::string &msg,
135 std::unique_ptr<std::map<std::string, std::any>> &details) {
136 subsystem =
"cosim_read";
137 msg =
"Message from channel '" + name +
"' consumed";
146 conn.getLogger().debug(
"cosim_read",
"Disconnecting channel " + name);
148 connection->disconnect();
159 std::unique_ptr<RpcClient::ReadChannelConnection> connection;
172std::unique_ptr<AcceleratorConnection>
175 std::string host =
"localhost";
178 if ((colon = connectionString.find(
':')) != std::string::npos) {
179 portStr = connectionString.substr(colon + 1);
180 host = connectionString.substr(0, colon);
181 }
else if (connectionString.ends_with(
"cosim.cfg")) {
182 std::ifstream cfg(connectionString);
183 std::string line, key, value;
185 while (getline(cfg, line))
186 if ((colon = line.find(
":")) != std::string::npos) {
187 key = line.substr(0, colon);
188 value = line.substr(colon + 1);
191 else if (key ==
"host")
195 if (portStr.size() == 0)
196 throw std::runtime_error(
"port line not found in file");
197 }
else if (connectionString ==
"env") {
198 char *hostEnv = getenv(
"ESI_COSIM_HOST");
203 char *portEnv = getenv(
"ESI_COSIM_PORT");
207 throw std::runtime_error(
"ESI_COSIM_PORT environment variable not set");
209 throw std::runtime_error(
"Invalid connection std::string '" +
210 connectionString +
"'");
212 uint16_t port = stoul(portStr);
213 auto conn = make_unique<CosimAccelerator>(
ctxt, host, port);
237class CosimSysInfo :
public SysInfo {
247 :
SysInfo(conn), rpcClient(rpcClient) {
251 if (!rpcClient->
getChannelDesc(
"__cosim_cycle_count.arg", argDesc) ||
252 !rpcClient->
getChannelDesc(
"__cosim_cycle_count.result", resultDesc))
255 Context &ctxt = conn.getCtxt();
260 {{
"cycle", i64Type}, {
"freq", i64Type}}));
262 reqPort = std::make_unique<WriteCosimChannelPort>(
263 conn, *rpcClient, argDesc, i1Type,
"__cosim_cycle_count.arg");
264 respPort = std::make_unique<ReadCosimChannelPort>(
265 conn, *rpcClient, resultDesc, resultType,
"__cosim_cycle_count.result");
268 {{
"arg", BundleType::Direction::To, i1Type},
269 {
"result", BundleType::Direction::From, resultType}});
271 bundleType, *reqPort, *respPort));
275 uint32_t getEsiVersion()
const override {
return rpcClient->
getEsiVersion(); }
276 std::optional<uint64_t> getCycleCount()
const override {
279 return getCycleInfo().cycle;
281 std::optional<uint64_t> getCoreClockFrequency()
const override {
284 return getCycleInfo().freq;
287 std::vector<uint8_t> getCompressedManifest()
const override {
302 std::unique_ptr<WriteCosimChannelPort> reqPort;
303 std::unique_ptr<ReadCosimChannelPort> respPort;
304 std::unique_ptr<FuncService::Function> func;
306 CycleInfo getCycleInfo()
const {
308 std::future<MessageData> result = func->call(arg);
311 return *respMsg.
as<CycleInfo>();
317class CosimMMIO :
public MMIO {
325 if (!rpcClient->
getChannelDesc(
"__cosim_mmio_read_write.arg", cmdArg) ||
326 !rpcClient->
getChannelDesc(
"__cosim_mmio_read_write.result", cmdResp))
327 throw std::runtime_error(
"Could not find MMIO channels");
331 ctxt,
new StructType(cmdArg.
type, {{
"write", new BitsType(
"i1", 1)},
332 {
"offset", new UIntType(
"ui32", 32)},
333 {
"data", new BitsType(
"i64", 64)}}));
336 cmdArgPort = std::make_unique<WriteCosimChannelPort>(
337 conn, *rpcClient, cmdArg, cmdType,
"__cosim_mmio_read_write.arg");
338 cmdRespPort = std::make_unique<ReadCosimChannelPort>(
339 conn, *rpcClient, cmdResp, i64Type,
"__cosim_mmio_read_write.result");
341 "cosimMMIO", {{
"arg", BundleType::Direction::To, cmdType},
342 {
"result", BundleType::Direction::From, i64Type}});
344 *cmdArgPort, *cmdRespPort));
357 uint64_t
read(uint32_t addr)
const override {
358 MMIOCmd cmd{.data = 0, .offset =
addr, .write =
false};
360 std::future<MessageData> result = cmdMMIO->call(arg);
362 uint64_t ret = *result.get().as<uint64_t>();
363 conn.getLogger().trace(
364 [addr, ret](std::string &subsystem, std::string &msg,
365 std::unique_ptr<std::map<std::string, std::any>> &details) {
366 subsystem =
"cosim_mmio";
367 msg =
"MMIO[0x" +
toHex(addr) +
"] = 0x" +
toHex(ret);
372 void write(uint32_t addr, uint64_t data)
override {
373 conn.getLogger().trace(
375 data](std::string &subsystem, std::string &msg,
376 std::unique_ptr<std::map<std::string, std::any>> &details) {
377 subsystem =
"cosim_mmio";
378 msg =
"MMIO[0x" +
toHex(addr) +
"] <- 0x" +
toHex(data);
380 MMIOCmd cmd{.data =
data, .offset =
addr, .write =
true};
382 std::future<MessageData> result = cmdMMIO->call(arg);
395 std::unique_ptr<WriteCosimChannelPort> cmdArgPort;
396 std::unique_ptr<ReadCosimChannelPort> cmdRespPort;
397 std::unique_ptr<FuncService::Function> cmdMMIO;
401struct HostMemReadReq {
407struct HostMemReadResp {
412struct HostMemWriteReq {
419using HostMemWriteResp = uint8_t;
422class CosimHostMem :
public HostMem {
427 void start()
override {
439 if (!rpcClient->
getChannelDesc(
"__cosim_hostmem_read_req.data", readArg) ||
440 !rpcClient->
getChannelDesc(
"__cosim_hostmem_read_resp.data", readResp))
441 throw std::runtime_error(
"Could not find HostMem read channels");
445 {{
"tag", new UIntType(
"ui8", 8)},
446 {
"data", new BitsType(
"i64", 64)}}));
449 {{
"address", new UIntType(
"ui64", 64)},
450 {
"length", new UIntType(
"ui32", 32)},
451 {
"tag", new UIntType(
"ui8", 8)}}));
455 readRespPort = std::make_unique<WriteCosimChannelPort>(
456 conn, *rpcClient, readResp, readRespType,
457 "__cosim_hostmem_read_resp.data");
458 readReqPort = std::make_unique<ReadCosimChannelPort>(
459 conn, *rpcClient, readArg, readReqType,
460 "__cosim_hostmem_read_req.data");
461 readReqPort->connect(
462 [
this](
const MessageData &req) {
return serviceRead(req); });
466 if (!rpcClient->
getChannelDesc(
"__cosim_hostmem_write.arg", writeArg) ||
467 !rpcClient->
getChannelDesc(
"__cosim_hostmem_write.result", writeResp))
468 throw std::runtime_error(
"Could not find HostMem write channels");
474 {{
"address", new UIntType(
"ui64", 64)},
475 {
"tag", new UIntType(
"ui8", 8)},
476 {
"data", new BitsType(
"i64", 64)}}));
479 writeRespPort = std::make_unique<WriteCosimChannelPort>(
480 conn, *rpcClient, writeResp, writeRespType,
481 "__cosim_hostmem_write.result");
482 writeReqPort = std::make_unique<ReadCosimChannelPort>(
483 conn, *rpcClient, writeArg, writeReqType,
"__cosim_hostmem_write.arg");
489 bundleType, *writeRespPort,
491 write->connect([
this](
const MessageData &req) {
return serviceWrite(req); },
498 const HostMemReadReq *req = reqBytes.
as<HostMemReadReq>();
499 acc.getLogger().trace(
500 [&](std::string &subsystem, std::string &msg,
501 std::unique_ptr<std::map<std::string, std::any>> &details) {
502 subsystem =
"hostmem";
503 msg =
"Read request: addr=0x" +
toHex(req->address) +
504 " len=" + std::to_string(req->length) +
505 " tag=" + std::to_string(req->tag);
510 uint64_t *dataPtr =
reinterpret_cast<uint64_t *
>(req->address);
511 uint32_t numDataResps = (req->length + 7) / 8;
512 if (numDataResps == 0)
513 acc.getLogger().error(
515 std::format(
"Read request with length=0 from addr=0x{} tag={}. "
516 "Reads of length 0 are not valid and indicate a bug "
518 toHex(req->address), req->tag));
519 uint32_t numResps = std::max(numDataResps, 1u);
520 for (uint32_t i = 0; i < numResps; ++i) {
521 HostMemReadResp
resp{.data = i < numDataResps ? dataPtr[i] : 0,
523 acc.getLogger().trace(
524 [&](std::string &subsystem, std::string &msg,
525 std::unique_ptr<std::map<std::string, std::any>> &details) {
526 subsystem =
"HostMem";
527 msg =
"Read result: data=0x" +
toHex(
resp.data) +
528 " tag=" + std::to_string(
resp.tag);
538 const HostMemWriteReq *req = reqBytes.
as<HostMemWriteReq>();
539 acc.getLogger().trace(
540 [&](std::string &subsystem, std::string &msg,
541 std::unique_ptr<std::map<std::string, std::any>> &details) {
542 subsystem =
"hostmem";
543 msg =
"Write request: addr=0x" +
toHex(req->address) +
" data=0x" +
545 " valid_bytes=" + std::to_string(req->valid_bytes) +
546 " tag=" + std::to_string(req->tag);
548 uint8_t *dataPtr =
reinterpret_cast<uint8_t *
>(req->address);
549 for (uint8_t i = 0; i < req->valid_bytes; ++i)
550 dataPtr[i] = (req->data >> (i * 8)) & 0xFF;
551 HostMemWriteResp
resp = req->tag;
555 struct CosimHostMemRegion :
public HostMemRegion {
556 CosimHostMemRegion(std::size_t size) {
558 memset(ptr, 0xFF, size);
561 virtual ~CosimHostMemRegion() { free(ptr); }
562 virtual void *getPtr()
const override {
return ptr; }
563 virtual std::size_t getSize()
const override {
return size; }
570 virtual std::unique_ptr<HostMemRegion>
572 auto ret = std::unique_ptr<HostMemRegion>(
new CosimHostMemRegion(size));
573 acc.getLogger().debug(
574 [&](std::string &subsystem, std::string &msg,
575 std::unique_ptr<std::map<std::string, std::any>> &details) {
576 subsystem =
"HostMem";
577 msg =
"Allocated host memory region at 0x" +
toHex(ret->getPtr()) +
578 " of size " + std::to_string(size);
582 virtual bool mapMemory(
void *ptr, std::size_t size,
586 virtual void unmapMemory(
void *ptr)
const override {}
600 std::unique_ptr<WriteCosimChannelPort> readRespPort;
601 std::unique_ptr<ReadCosimChannelPort> readReqPort;
602 std::unique_ptr<CallService::Callback>
read;
603 std::unique_ptr<WriteCosimChannelPort> writeRespPort;
604 std::unique_ptr<ReadCosimChannelPort> writeReqPort;
605 std::unique_ptr<CallService::Callback>
write;
618 if (prefix.size() > 0)
621 for (
auto client : clients) {
622 AppIDPath fullClientPath = prefix + client.relPath;
623 std::map<std::string, std::string> channelAssignments;
624 for (
auto assignment : client.channelAssignments)
625 if (assignment.second.type ==
"cosim")
626 channelAssignments[assignment.first] = std::any_cast<std::string>(
627 assignment.second.implOptions.at(
"name"));
633 const std::string &channelName,
635 const Type *type)
override;
639 std::map<AppIDPath, std::map<std::string, std::string>>
644std::unique_ptr<ChannelPort>
651 throw std::runtime_error(
"Could not find port for '" + idPath.
toStr() +
652 "." + channelName +
"'");
653 const std::map<std::string, std::string> &channelAssignments = f->second;
654 auto cosimChannelNameIter = channelAssignments.find(channelName);
655 if (cosimChannelNameIter == channelAssignments.end())
656 throw std::runtime_error(
"Could not find channel '" + idPath.
toStr() +
"." +
657 channelName +
"' in cosimulation");
662 if (!
conn.
rpcClient->getChannelDesc(cosimChannelNameIter->second, chDesc))
663 throw std::runtime_error(
"Could not find channel '" + idPath.
toStr() +
"." +
664 channelName +
"' in cosimulation");
666 std::unique_ptr<ChannelPort> port;
667 std::string fullChannelName = idPath.
toStr() +
"." + channelName;
669 port = std::make_unique<WriteCosimChannelPort>(
673 type, fullChannelName);
682 std::unique_ptr<Engine> engine =
nullptr;
683 if (engineTypeName ==
"cosim")
684 engine = std::make_unique<CosimEngine>(*
this, idPath, details, clients);
698 }
else if (svcType ==
typeid(
SysInfo)) {
701 return new CosimSysInfo(*
this,
rpcClient.get());
703 return new MMIOSysInfo(getService<services::MMIO>());
#define REGISTER_ACCELERATOR(Name, TAccelerator)
Abstract class representing a connection to an accelerator.
Context & getCtxt() const
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...
virtual void disconnect()
Disconnect from the accelerator cleanly.
Logger & getLogger() const
std::string toStr() const
Bits are just an array of bits.
static bool isWrite(BundleType::Direction bundleDir)
Compute the direction of a channel given the bundle direction and the bundle port's direction.
Bundles represent a collection of channels.
virtual void connectImpl(const ConnectOptions &options)
Called by all connect methods to let backends initiate the underlying connections.
AcceleratorConnections, Accelerators, and Manifests must all share a context.
std::optional< const Type * > getType(Type::ID id) const
Resolve a type id to the type.
void registerType(Type *type)
Register a type with the context. Takes ownership of the pointer type.
Engines implement the actual channel communication between the host and the accelerator.
A concrete flat message backed by a single vector of bytes.
const T * as() const
Cast to a type.
static MessageData from(T &t)
Cast from a type to its raw bytes.
A ChannelPort which reads data from the accelerator.
virtual void disconnect() override
Disconnect the channel.
bool invokeCallback(std::unique_ptr< SegmentedMessageData > &msg)
Invoke the currently registered callback.
Structs are an ordered collection of fields, each with a name and a type.
Root class of the ESI type system.
A ChannelPort which sends data to the accelerator.
virtual bool tryWriteImpl(const MessageData &data)=0
Implementation for tryWrite(). Subclasses must implement this.
std::vector< MessageData > getMessageFrames(const MessageData &data)
Break a message into its frames.
virtual void writeImpl(const MessageData &)=0
Implementation for write(). Subclasses must implement this.
Connect to an ESI simulation.
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.
std::unique_ptr< RpcClient > rpcClient
void setManifestMethod(ManifestMethod method)
static std::unique_ptr< AcceleratorConnection > connect(Context &, std::string connectionString)
Parse the connection std::string and instantiate the accelerator.
virtual Service * createService(Service::Type service, AppIDPath path, std::string implName, const ServiceImplDetails &details, const HWClientDetails &clients) override
Called by getServiceImpl exclusively.
ManifestMethod manifestMethod
CosimAccelerator(Context &, std::string hostname, uint16_t port)
Construct and connect to a cosim server.
std::set< std::unique_ptr< ChannelPort > > channels
Implement the magic cosim channel communication.
CosimEngine(CosimAccelerator &conn, AppIDPath idPath, const ServiceImplDetails &details, const HWClientDetails &clients)
std::map< AppIDPath, std::map< std::string, std::string > > clientChannelAssignments
std::unique_ptr< ChannelPort > createPort(AppIDPath idPath, const std::string &channelName, BundleType::Direction dir, const Type *type) override
Each engine needs to know how to create a ports.
A client for the cosim RPC server.
std::vector< uint8_t > getCompressedManifest() const
Get the compressed manifest from the server.
uint32_t getEsiVersion() const
Get the ESI version from the manifest.
bool getChannelDesc(const std::string &channelName, ChannelDesc &desc) const
Get the channel description for a channel name.
static Callback * get(AcceleratorConnection &acc, AppID id, const BundleType *type, WriteChannelPort &result, ReadChannelPort &arg)
static Function * get(AppID id, BundleType *type, WriteChannelPort &arg, ReadChannelPort &result)
Implement the SysInfo API for a standard MMIO protocol.
Parent class of all APIs modeled as 'services'.
const std::type_info & Type
Information about the Accelerator system.
std::unique_ptr< Engine > createEngine(AcceleratorConnection &conn, const std::string &dmaEngineName, AppIDPath idPath, const ServiceImplDetails &details, const HWClientDetails &clients)
Create an engine by name.
std::map< std::string, std::any > ServiceImplDetails
std::string toHex(void *val)
std::vector< HWClientDetail > HWClientDetails
Description of a channel from the server.
Options for allocating host memory.