29#include <linux/limits.h>
40 : ctxt(ctxt), serviceThread(nullptr) {}
52 }
catch (
const std::exception &e) {
54 std::string(
"failed to request reset: ") + e.what());
70 *
this, engineTypeName, idPath, details, clients);
75 std::unique_ptr<Engine> engine,
78 auto [engineIter, _] =
ownedEngines.emplace(idPath, std::move(engine));
82 Engine *enginePtr = engineIter->second.get();
85 if (prefix.size() > 0)
88 for (
const auto &client : clients) {
89 AppIDPath fullClientPath = prefix + client.relPath;
90 for (
const auto &channel : client.channelAssignments)
91 clientEngines[fullClientPath].setEngine(channel.first, enginePtr);
100 std::unique_ptr<Service> &cacheEntry =
101 serviceCache[make_tuple(std::string(svcType.name()),
id)];
102 if (cacheEntry ==
nullptr) {
109 cacheEntry = std::unique_ptr<Service>(svc);
111 return cacheEntry.get();
117 throw std::runtime_error(
118 "AcceleratorConnection already owns an accelerator");
133 char result[PATH_MAX];
134 ssize_t count = readlink(
"/proc/self/exe", result, PATH_MAX);
136 throw std::runtime_error(
"Could not get executable path");
137 return std::filesystem::path(std::string(result, count));
139 char buffer[MAX_PATH];
140 DWORD length = GetModuleFileNameA(NULL, buffer, MAX_PATH);
142 throw std::runtime_error(
"Could not get executable path");
143 return std::filesystem::path(std::string(buffer, length));
145#eror "Unsupported platform"
154 return std::filesystem::path(std::string(dl_info.dli_fname));
156 HMODULE hModule = NULL;
157 if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
158 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
159 reinterpret_cast<LPCSTR
>(&
getLibPath), &hModule)) {
161 return std::filesystem::path();
164 char buffer[MAX_PATH];
165 DWORD length = GetModuleFileNameA(hModule, buffer, MAX_PATH);
167 throw std::runtime_error(
"Could not get library path");
169 return std::filesystem::path(std::string(buffer, length));
171#eror "Unsupported platform"
177 std::vector<std::filesystem::path> directories;
180 directories.push_back(std::filesystem::current_path());
183 const char *esiBackends = std::getenv(
"ESI_BACKENDS");
187 const char separator =
';';
189 const char separator =
':';
192 std::string pathsStr(esiBackends);
193 std::stringstream ss(pathsStr);
196 while (std::getline(ss, path, separator))
198 directories.emplace_back(path);
202 directories.push_back(
getExePath().parent_path());
204 directories.push_back(
getLibPath().parent_path());
215 backend[0] = toupper(backend[0]);
219 std::string backendFileName =
"lib" + backend +
"Backend.so";
223#if defined(_MSC_VER) && defined(_DEBUG)
224 std::string backendFileName = backend +
"Backend_d.dll";
226 std::string backendFileName = backend +
"Backend.dll";
229#error "Unsupported platform"
233 std::filesystem::path backendPath;
235 std::vector<std::filesystem::path> esiBackendDirs =
238 for (
const auto &dir : esiBackendDirs) {
239 backendPath = dir / backendFileName;
240 logger.
debug(
"CONNECT",
241 "trying to find backend plugin: " + backendPath.string());
242 if (std::filesystem::exists(backendPath)) {
250 backendPath = std::filesystem::absolute(backendPath);
251 logger.
debug(
"CONNECT",
"found backend plugin: " + backendPath.string());
254 backendPath = backendFileName;
255 logger.
debug(
"CONNECT",
256 "trying to find backend plugin: " + backendFileName);
261 void *handle = dlopen(backendPath.string().c_str(), RTLD_NOW | RTLD_GLOBAL);
263 std::string error(dlerror());
264 logger.
error(
"CONNECT",
265 "while attempting to load backend plugin: " + error);
266 throw std::runtime_error(
"While attempting to load backend plugin: " +
273 std::filesystem::path backendPathParent = backendPath.parent_path();
278 if (backendPathParent.empty())
279 backendPathParent = std::filesystem::current_path();
280 logger.
debug(
"CONNECT",
"setting DLL search directory to: " +
281 backendPathParent.string());
282 if (SetDllDirectoryA(backendPathParent.string().c_str()) == 0)
283 throw std::runtime_error(
"While setting DLL directory: " +
284 std::to_string(GetLastError()));
288 HMODULE handle = LoadLibraryA(backendPath.string().c_str());
290 DWORD error = GetLastError();
292 LPSTR messageBuffer =
nullptr;
293 size_t size = FormatMessageA(
294 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
295 FORMAT_MESSAGE_IGNORE_INSERTS,
296 nullptr, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
297 (LPSTR)&messageBuffer, 0,
nullptr);
299 std::string errorMessage;
300 if (size > 0 && messageBuffer !=
nullptr) {
301 errorMessage = std::string(messageBuffer, size);
302 LocalFree(messageBuffer);
304 errorMessage =
"Unknown error";
307 std::string fullError =
"While attempting to load backend plugin '" +
308 backendPath.string() +
"': " + errorMessage +
309 " (error code: " + std::to_string(error) +
")";
311 logger.
error(
"CONNECT", fullError);
312 throw std::runtime_error(fullError);
315#eror "Unsupported platform"
317 logger.
info(
"CONNECT",
"loaded backend plugin: " + backendPath.string());
325 static std::map<std::string, BackendCreate> &
get() {
336 if (registry.count(name))
337 throw std::runtime_error(
"Backend already exists in registry");
338 registry[name] = create;
345 std::string connection) {
347 auto f = registry.find(backend);
348 if (f == registry.end()) {
351 f = registry.find(backend);
352 if (f == registry.end()) {
354 details[
"backend"] = backend;
355 std::ostringstream loaded_backends;
357 for (
const auto &b : registry) {
359 loaded_backends <<
", ";
360 loaded_backends << b.first;
363 details[
"loaded_backends"] = loaded_backends.str();
364 getLogger().
error(
"CONNECT",
"backend '" + backend +
"' not found",
366 throw std::runtime_error(
"Backend '" + backend +
"' not found");
369 getLogger().
info(
"CONNECT",
"connecting to backend " + backend +
" via '" +
371 auto conn = f->second(*
this, connection);
372 auto *connPtr = conn.get();
379 void start() {
me = std::thread(&Impl::loop,
this); }
387 addListener(std::initializer_list<ReadChannelPort *> listenPorts,
390 void addTask(std::function<
void(
void)> task) {
391 std::lock_guard<std::mutex> g(
m);
406 std::future<MessageData>>>
413void AcceleratorServiceThread::Impl::loop() {
420 std::vector<std::function<void(
void)>> taskListCopy;
428 std::this_thread::yield();
433 std::lock_guard<std::mutex> g(
m);
434 for (
auto &[channel, cbfPair] :
listeners) {
435 assert(channel &&
"Null channel in listener list");
436 std::future<MessageData> &f = cbfPair.second;
437 if (f.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
438 portUnlockWorkList.emplace_back(channel, cbfPair.first, f.get());
439 f = channel->readAsync();
445 for (
auto [channel, cb, data] : portUnlockWorkList)
446 cb(channel, std::move(data));
449 portUnlockWorkList.clear();
454 std::lock_guard<std::mutex> g(
m);
457 for (
auto &task : taskListCopy)
462void AcceleratorServiceThread::Impl::addListener(
463 std::initializer_list<ReadChannelPort *> listenPorts,
465 std::lock_guard<std::mutex> g(m);
466 for (
auto port : listenPorts) {
467 if (listeners.count(port))
468 throw std::runtime_error(
"Port already has a listener");
469 listeners[port] = std::make_pair(callback, port->readAsync());
490 std::initializer_list<ReadChannelPort *> listenPorts,
493 impl->addListener(listenPorts, callback);
498 impl->addTask([&module]() {
module.poll(); });
assert(baseType &&"element must be base type")
Abstract class representing a connection to an accelerator.
virtual Service * createService(Service::Type service, AppIDPath idPath, std::string implName, const ServiceImplDetails &details, const HWClientDetails &clients)=0
Called by getServiceImpl exclusively.
ServiceClass * getService(AppIDPath id={}, std::string implName={}, ServiceImplDetails details={}, HWClientDetails clients={})
Get a typed reference to a particular service type.
virtual bool reset()
Request a reset of the accelerator design.
void clearOwnedObjects()
Drop accelerator-owned objects before a derived backend destroys resources that those objects may ref...
std::map< AppIDPath, BundleEngineMap > clientEngines
Mapping of clients to their servicing engines.
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...
std::map< ServiceCacheKey, std::unique_ptr< Service > > serviceCache
std::unique_ptr< AcceleratorServiceThread > serviceThread
std::unique_ptr< Accelerator > ownedAccelerator
Accelerator object owned by this connection.
virtual void disconnect()
Disconnect from the accelerator cleanly.
std::map< AppIDPath, std::unique_ptr< Engine > > ownedEngines
Collection of owned engines.
virtual void createEngine(const std::string &engineTypeName, AppIDPath idPath, const ServiceImplDetails &details, const HWClientDetails &clients)
Create a new engine for channel communication with the accelerator.
virtual ~AcceleratorConnection()
Logger & getLogger() const
AcceleratorServiceThread * getServiceThread()
Return a pointer to the accelerator 'service' thread (or threads).
AcceleratorConnection(Context &ctxt)
Accelerator * takeOwnership(std::unique_ptr< Accelerator > accel)
Assume ownership of an accelerator object.
Background thread which services various requests.
AcceleratorServiceThread()
std::unique_ptr< Impl > impl
void stop()
Instruct the service thread to stop running.
void addPoll(HWModule &module)
Poll this module.
~AcceleratorServiceThread()
void addListener(std::initializer_list< ReadChannelPort * > listenPorts, std::function< void(ReadChannelPort *, MessageData)> callback)
When there's data on any of the listenPorts, call the callback.
Top level accelerator class.
AcceleratorConnections, Accelerators, and Manifests must all share a context.
std::vector< std::unique_ptr< AcceleratorConnection > > connections
AcceleratorConnection * connect(std::string backend, std::string connection)
Connect to an accelerator backend.
Engines implement the actual channel communication between the host and the accelerator.
Represents either the top level or an instance of a hardware module.
virtual void error(const std::string &subsystem, const std::string &msg, const std::map< std::string, std::any > *details=nullptr)
Report an error.
virtual void info(const std::string &subsystem, const std::string &msg, const std::map< std::string, std::any > *details=nullptr)
Report an informational message.
void debug(const std::string &subsystem, const std::string &msg, const std::map< std::string, std::any > *details=nullptr)
Report a debug message.
A concrete flat message backed by a single vector of bytes.
A ChannelPort which reads data from the accelerator.
std::map< std::string, BackendCreate > backendRegistry
static std::map< std::string, BackendCreate > & get()
virtual void write(uint32_t addr, uint64_t data)=0
Write a 64-bit value to the global MMIO space.
static Service * createService(AcceleratorConnection *acc, Service::Type svcType, AppIDPath id, std::string implName, ServiceImplDetails details, HWClientDetails clients)
Create a service instance from the given details.
Parent class of all APIs modeled as 'services'.
const std::type_info & Type
void registerBackend(const std::string &name, BackendCreate create)
std::function< std::unique_ptr< AcceleratorConnection >(Context &, std::string)> BackendCreate
Backends can register themselves to be connected via a connection string.
std::unique_ptr< Engine > createEngine(AcceleratorConnection &conn, const std::string &dmaEngineName, AppIDPath idPath, const ServiceImplDetails &details, const HWClientDetails &clients)
Create an engine by name.
static std::filesystem::path getExePath()
Get the path to the currently running executable.
std::map< std::string, std::any > ServiceImplDetails
static void loadBackend(Context &ctxt, std::string backend)
Load a backend plugin dynamically.
constexpr uint64_t ResetMagicNumber
Magic value which, when written to MMIO offset ResetRequestOffset, requests a design reset.
static std::filesystem::path getLibPath()
Get the path to the currently running shared library.
constexpr uint32_t ResetRequestOffset
Offset into the (global) MMIO space at which to request a design reset.
static std::vector< std::filesystem::path > getESIBackendDirectories()
Get the list of directories to search for backend plugins.
std::vector< HWClientDetail > HWClientDetails
std::map< ReadChannelPort *, std::pair< std::function< void(ReadChannelPort *, MessageData)>, std::future< MessageData > > > listeners
void addTask(std::function< void(void)> task)
void addListener(std::initializer_list< ReadChannelPort * > listenPorts, std::function< void(ReadChannelPort *, MessageData)> callback)
When there's data on any of the listenPorts, call the callback.
std::vector< std::function< void(void)> > taskList
Tasks which should be called on every loop iteration.