Add LoadableLibraryManager and refactor StimBuffApiManager to use it.
Centralize dlopen/search in LoadableLibraryManager so typed library managers can share one loaded-shlib registry without duplicating load/unload logic. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <optional>
|
||||
#include <filesystem>
|
||||
#include <algorithm>
|
||||
#include <stimBuffApis/stimBuffApiManager.h>
|
||||
#include <stimBuffApis/stimBuffApiLib.h>
|
||||
#include <loadableLib/loadableLibraryManager.h>
|
||||
#include <body/bodyThread.h>
|
||||
#include <componentThread.h>
|
||||
#include <opts.h>
|
||||
@@ -13,9 +14,6 @@
|
||||
#include <marionette/marionette.h>
|
||||
#include <computeManager/computeManager.h>
|
||||
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace smo {
|
||||
namespace stim_buff {
|
||||
|
||||
@@ -32,109 +30,55 @@ void assertBodyThread()
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
/**
|
||||
* @brief Searches for a library in predefined locations
|
||||
* @param libraryPath The name or path of the library to find
|
||||
* @return Optional containing the full path if found in search paths, nullopt
|
||||
* if not
|
||||
*
|
||||
* Searches for the library in the following locations in order:
|
||||
* 1. Custom path specified by --sense-api-lib-path option (if provided)
|
||||
* 2. Current working directory
|
||||
* 3. Directory containing the executable
|
||||
*
|
||||
* If the library is not found in any of these locations, returns nullopt and
|
||||
* falls back to system default library search paths (LD_LIBRARY_PATH, etc.)
|
||||
*/
|
||||
static std::optional<std::string> searchForLibInSmoSearchPaths(
|
||||
const std::string& libraryPath)
|
||||
{
|
||||
std::vector<std::string> searchPaths = {
|
||||
fs::current_path().string(),
|
||||
fs::path("/proc/self/exe").parent_path().string()
|
||||
};
|
||||
|
||||
const auto& options = OptionParser::getOptions();
|
||||
if (!options.senseApiLibPath.empty())
|
||||
{
|
||||
// Insert all stim buff API library paths at the beginning of search paths
|
||||
searchPaths.insert(
|
||||
searchPaths.begin(),
|
||||
options.senseApiLibPath.begin(),
|
||||
options.senseApiLibPath.end());
|
||||
}
|
||||
|
||||
for (const auto& path : searchPaths)
|
||||
{
|
||||
fs::path fullPath = fs::path(path) / libraryPath;
|
||||
if (fs::exists(fullPath))
|
||||
{
|
||||
return fullPath.string();
|
||||
}
|
||||
}
|
||||
|
||||
std::cerr << std::string(__func__) + ": library '"
|
||||
+ libraryPath + "' isn't in search bespoke search paths: ";
|
||||
for (const auto& path : searchPaths) {
|
||||
std::cerr << path << " ";
|
||||
}
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "Trying to load " + libraryPath + " from system default "
|
||||
"search paths\n";
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
/* Local static function to wrap sscl::ComponentThread::getSelf for SmoCallbacks */
|
||||
static std::shared_ptr<sscl::ComponentThread> ComponentThread_getSelf()
|
||||
std::shared_ptr<sscl::ComponentThread> ComponentThread_getSelf()
|
||||
{
|
||||
return sscl::ComponentThread::getSelf();
|
||||
}
|
||||
|
||||
/* Local static function to wrap OptionParser::getOptions for SmoCallbacks */
|
||||
static OptionParser& OptionParser_getOptions()
|
||||
OptionParser& OptionParser_getOptions()
|
||||
{
|
||||
return OptionParser::getOptions();
|
||||
}
|
||||
|
||||
/* Local static functions to wrap ComputeManager methods for SmoCallbacks */
|
||||
static std::shared_ptr<smo::compute::ClBuffer>
|
||||
std::shared_ptr<smo::compute::ClBuffer>
|
||||
ComputeManager_createUseHostPtrBuffer(
|
||||
void* hostPtr, size_t size, cl_mem_flags flags
|
||||
)
|
||||
void* hostPtr, size_t size, cl_mem_flags flags)
|
||||
{
|
||||
return smo::compute::ComputeManager::getInstance().createUseHostPtrBuffer(
|
||||
hostPtr, size, flags);
|
||||
}
|
||||
|
||||
static void ComputeManager_releaseUseHostPtrBuffer(
|
||||
std::shared_ptr<smo::compute::ClBuffer> buffer
|
||||
)
|
||||
void ComputeManager_releaseUseHostPtrBuffer(
|
||||
std::shared_ptr<smo::compute::ClBuffer> buffer)
|
||||
{
|
||||
smo::compute::ComputeManager::getInstance().releaseUseHostPtrBuffer(
|
||||
buffer);
|
||||
}
|
||||
|
||||
static std::shared_ptr<smo::compute::ComputeDevice> ComputeManager_getDevice()
|
||||
std::shared_ptr<smo::compute::ComputeDevice> ComputeManager_getDevice()
|
||||
{
|
||||
return smo::compute::ComputeManager::getInstance().getDevice();
|
||||
}
|
||||
|
||||
static void ComputeManager_releaseDevice(
|
||||
std::shared_ptr<smo::compute::ComputeDevice> device
|
||||
)
|
||||
void ComputeManager_releaseDevice(
|
||||
std::shared_ptr<smo::compute::ComputeDevice> device)
|
||||
{
|
||||
smo::compute::ComputeManager::getInstance().releaseDevice(device);
|
||||
}
|
||||
|
||||
std::optional<std::string> searchForLibInSmoSearchPathsHook(
|
||||
const std::string& libraryPath)
|
||||
{
|
||||
return loadable_lib::LoadableLibraryManager::getInstance()
|
||||
.searchForLibInSmoSearchPaths(libraryPath);
|
||||
}
|
||||
|
||||
/* Hooks to be provided to stimBuffApiLibs, enabling them to call into Salmanoff
|
||||
* code.
|
||||
*/
|
||||
static SmoCallbacks smoCallbacks =
|
||||
SmoCallbacks smoCallbacks =
|
||||
{
|
||||
.searchForLibInSmoSearchPaths = searchForLibInSmoSearchPaths,
|
||||
.searchForLibInSmoSearchPaths = searchForLibInSmoSearchPathsHook,
|
||||
.ComponentThread_getSelf = ComponentThread_getSelf,
|
||||
.OptionParser_getOptions = OptionParser_getOptions,
|
||||
.ComputeManager_createUseHostPtrBuffer =
|
||||
@@ -146,104 +90,78 @@ static SmoCallbacks smoCallbacks =
|
||||
};
|
||||
|
||||
/* Static file-scope threading model object for senseApi libraries */
|
||||
static SmoThreadingModelDesc smoThreadingModelDesc = {
|
||||
SmoThreadingModelDesc smoThreadingModelDesc = {
|
||||
.componentThread = nullptr
|
||||
};
|
||||
|
||||
std::optional<std::string> StimBuffApiManager::searchForLibInSmoSearchPaths(
|
||||
const std::string& libraryPath)
|
||||
{
|
||||
return ::smo::stim_buff::searchForLibInSmoSearchPaths(libraryPath);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
StimBuffApiLib& StimBuffApiManager::loadStimBuffApiLib(
|
||||
const std::string& libraryPath,
|
||||
const std::shared_ptr<sscl::ComponentThread>& componentThread
|
||||
)
|
||||
const std::shared_ptr<sscl::ComponentThread>& componentThread)
|
||||
{
|
||||
std::optional<std::string> fullPath = searchForLibInSmoSearchPaths(
|
||||
libraryPath);
|
||||
std::string resolvedPath = fullPath.value_or(libraryPath);
|
||||
loadable_lib::LoadableLibraryManager& llm =
|
||||
loadable_lib::LoadableLibraryManager::getInstance();
|
||||
|
||||
// Clear any existing error
|
||||
dlerror();
|
||||
auto dlopen_handle = std::unique_ptr<void, StimBuffApiLib::DlCloser>(
|
||||
dlopen(resolvedPath.c_str(), RTLD_LAZY));
|
||||
if (!dlopen_handle && fullPath.has_value())
|
||||
{
|
||||
// Fallback to using the supplied libraryPath
|
||||
dlerror();
|
||||
dlopen_handle.reset(dlopen(libraryPath.c_str(), RTLD_LAZY));
|
||||
}
|
||||
if (findStimBuffApiLibByLibraryPath(libraryPath))
|
||||
{
|
||||
throw std::runtime_error(
|
||||
std::string(__func__) + ": StimBuffApiLib already loaded: "
|
||||
+ libraryPath);
|
||||
}
|
||||
|
||||
if (!dlopen_handle)
|
||||
{
|
||||
const char *dlerr = dlerror();
|
||||
std::shared_ptr<loadable_lib::LoadableLibraryManager::LoadedSharedLibrary>
|
||||
loadedLibrary = llm.loadSharedLibrary(libraryPath);
|
||||
|
||||
std::string error = (dlerr
|
||||
? dlerr
|
||||
: "Unknown error while opening shlib");
|
||||
throw std::runtime_error(
|
||||
std::string(__func__) + ": Cannot load library '"
|
||||
+ libraryPath + "': "
|
||||
+ error);
|
||||
}
|
||||
auto descFn = loadable_lib::LoadableLibraryManager::resolveSymbol<
|
||||
SMO_GET_STIM_BUFF_API_DESC_FN_TYPEDEF *>(
|
||||
loadedLibrary->getDlopenHandle(),
|
||||
SMO_GET_STIM_BUFF_API_DESC_FN_NAME_STR);
|
||||
|
||||
// Initialize getSenseApiDescriptor
|
||||
auto func = reinterpret_cast<SMO_GET_STIM_BUFF_API_DESC_FN_TYPEDEF *>(
|
||||
dlsym(dlopen_handle.get(), SMO_GET_STIM_BUFF_API_DESC_FN_NAME_STR));
|
||||
if (!func)
|
||||
{
|
||||
throw std::runtime_error(
|
||||
std::string(__func__) + ": dlsym('"
|
||||
SMO_GET_STIM_BUFF_API_DESC_FN_NAME_STR "') failed for library '"
|
||||
+ libraryPath + "'");
|
||||
}
|
||||
if (!smoThreadingModelDesc.componentThread) {
|
||||
smoThreadingModelDesc.componentThread = componentThread;
|
||||
}
|
||||
|
||||
// Check if the static threading model obj is null and initialize if needed
|
||||
if (!smoThreadingModelDesc.componentThread) {
|
||||
smoThreadingModelDesc.componentThread = componentThread;
|
||||
}
|
||||
|
||||
const StimBuffApiDesc &libApiDesc = func(
|
||||
const StimBuffApiDesc& libApiDesc = descFn(
|
||||
smoCallbacks, smoThreadingModelDesc);
|
||||
|
||||
auto lib = std::make_shared<StimBuffApiLib>(
|
||||
libraryPath, dlopen_handle.release(), func);
|
||||
lib->setStimBuffApiDesc(libApiDesc);
|
||||
getInstance().s.rsrc.stimBuffApiLibs.push_back(lib);
|
||||
return *getInstance().s.rsrc.stimBuffApiLibs.back();
|
||||
auto lib = std::make_shared<StimBuffApiLib>(loadedLibrary, descFn);
|
||||
lib->setStimBuffApiDesc(libApiDesc);
|
||||
s.rsrc.libs.push_back(lib);
|
||||
|
||||
return *lib;
|
||||
}
|
||||
|
||||
std::optional<std::shared_ptr<StimBuffApiLib>>
|
||||
StimBuffApiManager::getStimBuffApiLib(const std::string& libraryPath)
|
||||
StimBuffApiManager::findStimBuffApiLibByLibraryPath(
|
||||
const std::string& libraryPath)
|
||||
{
|
||||
auto &libs = getInstance().s.rsrc.stimBuffApiLibs;
|
||||
auto it = std::find_if(libs.begin(), libs.end(),
|
||||
[&libPath = libraryPath](const std::shared_ptr<StimBuffApiLib>& lib) {
|
||||
return lib->libraryPath == libPath;
|
||||
}
|
||||
);
|
||||
auto& libs = s.rsrc.libs;
|
||||
auto it = std::find_if(
|
||||
libs.begin(), libs.end(),
|
||||
[&libraryPath](const std::shared_ptr<StimBuffApiLib>& lib) {
|
||||
return lib->loadedSharedLibrary->libraryPath == libraryPath;
|
||||
});
|
||||
|
||||
if (it != libs.end()) { return *it; }
|
||||
return std::nullopt;
|
||||
if (it != libs.end()) { return *it; }
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<std::shared_ptr<StimBuffApiLib>>
|
||||
StimBuffApiManager::findStimBuffApiLibByApiName(const std::string& apiName)
|
||||
{
|
||||
auto &libs = getInstance().s.rsrc.stimBuffApiLibs;
|
||||
auto it = std::find_if(libs.begin(), libs.end(),
|
||||
[&apiName](const std::shared_ptr<StimBuffApiLib>& lib) {
|
||||
return lib->stimBuffApiDesc.name == apiName;
|
||||
}
|
||||
);
|
||||
auto& libs = s.rsrc.libs;
|
||||
auto it = std::find_if(
|
||||
libs.begin(), libs.end(),
|
||||
[&apiName](const std::shared_ptr<StimBuffApiLib>& lib) {
|
||||
return lib->stimBuffApiDesc.name == apiName;
|
||||
});
|
||||
|
||||
if (it != libs.end()) { return *it; }
|
||||
return std::nullopt;
|
||||
if (it != libs.end()) { return *it; }
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
StimBuffApiLib &StimBuffApiManager::getStimBuffApiLibByApiName(
|
||||
StimBuffApiLib& StimBuffApiManager::getStimBuffApiLibByApiName(
|
||||
const std::string& apiName)
|
||||
{
|
||||
auto libOpt = findStimBuffApiLibByApiName(apiName);
|
||||
@@ -258,66 +176,76 @@ StimBuffApiLib &StimBuffApiManager::getStimBuffApiLibByApiName(
|
||||
|
||||
void StimBuffApiManager::unloadStimBuffApiLib(const std::string& libraryPath)
|
||||
{
|
||||
auto &libs = getInstance().s.rsrc.stimBuffApiLibs;
|
||||
auto it = std::find_if(libs.begin(), libs.end(),
|
||||
[&lpath = libraryPath](const std::shared_ptr<StimBuffApiLib>& lib) {
|
||||
return lib->libraryPath == lpath;
|
||||
}
|
||||
);
|
||||
auto& libs = s.rsrc.libs;
|
||||
auto it = std::find_if(
|
||||
libs.begin(), libs.end(),
|
||||
[&libraryPath](const std::shared_ptr<StimBuffApiLib>& lib) {
|
||||
return lib->loadedSharedLibrary->libraryPath == libraryPath;
|
||||
});
|
||||
|
||||
if (it != libs.end())
|
||||
{
|
||||
libs.erase(it);
|
||||
return;
|
||||
}
|
||||
if (it == libs.end())
|
||||
{
|
||||
std::cerr << std::string(__func__) + ": Library not found: "
|
||||
<< libraryPath << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
std::cerr << std::string(__func__) + ": Library not found: "
|
||||
<< libraryPath << '\n';
|
||||
std::shared_ptr<loadable_lib::LoadableLibraryManager::LoadedSharedLibrary>
|
||||
loadedLibrary = (*it)->loadedSharedLibrary;
|
||||
libs.erase(it);
|
||||
|
||||
loadable_lib::LoadableLibraryManager::getInstance()
|
||||
.unloadSharedLibrary(loadedLibrary);
|
||||
}
|
||||
|
||||
void StimBuffApiManager::unloadAllStimBuffApiLibs(void)
|
||||
{
|
||||
getInstance().s.rsrc.stimBuffApiLibs.clear();
|
||||
auto libs = s.rsrc.libs;
|
||||
s.rsrc.libs.clear();
|
||||
|
||||
for (const auto& lib : libs)
|
||||
{
|
||||
loadable_lib::LoadableLibraryManager::getInstance()
|
||||
.unloadSharedLibrary(lib->loadedSharedLibrary);
|
||||
}
|
||||
}
|
||||
|
||||
void StimBuffApiManager::loadAllStimBuffApiLibsFromOptions(
|
||||
const std::shared_ptr<sscl::ComponentThread>& componentThread
|
||||
)
|
||||
const std::shared_ptr<sscl::ComponentThread>& componentThread)
|
||||
{
|
||||
const auto& options = OptionParser::getOptions();
|
||||
for (const auto& libPath : options.senseApiLibs) {
|
||||
loadStimBuffApiLib(libPath, componentThread);
|
||||
}
|
||||
const auto& options = OptionParser::getOptions();
|
||||
for (const auto& libPath : options.senseApiLibs) {
|
||||
loadStimBuffApiLib(libPath, componentThread);
|
||||
}
|
||||
}
|
||||
|
||||
std::string StimBuffApiManager::stringifyLibs() const
|
||||
{
|
||||
std::string result;
|
||||
for (const auto& lib : getInstance().s.rsrc.stimBuffApiLibs) {
|
||||
result += lib->stringify() + "\n";
|
||||
}
|
||||
return result;
|
||||
std::string result;
|
||||
for (const auto& lib : s.rsrc.libs) {
|
||||
result += lib->stringify() + "\n";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
body::BodyViralPostingInvoker<void>
|
||||
StimBuffApiManager::initializeStimBuffApiLibCReq(
|
||||
StimBuffApiLib &lib, bool acquireSbamLock)
|
||||
StimBuffApiLib& lib, bool acquireListLock)
|
||||
{
|
||||
assertBodyThread();
|
||||
|
||||
StimBuffApiManager &sbam = getInstance();
|
||||
std::optional<sscl::co::CoQutex::ReleaseHandle> sbamGuard;
|
||||
if (acquireSbamLock)
|
||||
std::optional<sscl::co::CoQutex::ReleaseHandle> listGuard;
|
||||
if (acquireListLock)
|
||||
{
|
||||
sbamGuard.emplace(
|
||||
co_await sbam.s.lock.getAcquireInvocationAndSuspensionPolicy());
|
||||
listGuard.emplace(
|
||||
co_await s.lock.getAcquireInvocationAndSuspensionPolicy());
|
||||
}
|
||||
|
||||
if (!lib.stimBuffApiDesc.sal_mgmt_libOps.initializeCInd)
|
||||
{
|
||||
throw std::runtime_error(
|
||||
std::string(__func__) + ": initializeCInd() is NULL for library '"
|
||||
+ lib.libraryPath + "'");
|
||||
+ lib.loadedSharedLibrary->libraryPath + "'");
|
||||
}
|
||||
|
||||
sscl::co::CoQutex::ReleaseHandle libGuard =
|
||||
@@ -330,29 +258,28 @@ StimBuffApiManager::initializeStimBuffApiLibCReq(
|
||||
|
||||
body::BodyViralPostingInvoker<void>
|
||||
StimBuffApiManager::finalizeStimBuffApiLibCReq(
|
||||
StimBuffApiLib &lib, bool acquireSbamLock)
|
||||
StimBuffApiLib& lib, bool acquireListLock)
|
||||
{
|
||||
assertBodyThread();
|
||||
|
||||
StimBuffApiManager &sbam = getInstance();
|
||||
std::optional<sscl::co::CoQutex::ReleaseHandle> sbamGuard;
|
||||
if (acquireSbamLock)
|
||||
std::optional<sscl::co::CoQutex::ReleaseHandle> listGuard;
|
||||
if (acquireListLock)
|
||||
{
|
||||
sbamGuard.emplace(
|
||||
co_await sbam.s.lock.getAcquireInvocationAndSuspensionPolicy());
|
||||
listGuard.emplace(
|
||||
co_await s.lock.getAcquireInvocationAndSuspensionPolicy());
|
||||
}
|
||||
|
||||
if (!lib.stimBuffApiDesc.sal_mgmt_libOps.finalizeCInd)
|
||||
{
|
||||
throw std::runtime_error(
|
||||
std::string(__func__) + ": finalizeCInd() is NULL for library '"
|
||||
+ lib.libraryPath + "'");
|
||||
+ lib.loadedSharedLibrary->libraryPath + "'");
|
||||
}
|
||||
|
||||
sscl::co::CoQutex::ReleaseHandle libGuard =
|
||||
co_await lib.s.lock.getAcquireInvocationAndSuspensionPolicy();
|
||||
|
||||
lib.isBeingDestroyed.store(true);
|
||||
lib.loadedSharedLibrary->isBeingDestroyed.store(true);
|
||||
co_await lib.stimBuffApiDesc.sal_mgmt_libOps.finalizeCInd();
|
||||
|
||||
co_return;
|
||||
@@ -363,11 +290,10 @@ StimBuffApiManager::initializeAllStimBuffApiLibsCReq()
|
||||
{
|
||||
assertBodyThread();
|
||||
|
||||
StimBuffApiManager &sbam = getInstance();
|
||||
sscl::co::CoQutex::ReleaseHandle sbamGuard =
|
||||
co_await sbam.s.lock.getAcquireInvocationAndSuspensionPolicy();
|
||||
sscl::co::CoQutex::ReleaseHandle listGuard =
|
||||
co_await s.lock.getAcquireInvocationAndSuspensionPolicy();
|
||||
|
||||
for (auto &lib : sbam.s.rsrc.stimBuffApiLibs) {
|
||||
for (auto& lib : s.rsrc.libs) {
|
||||
co_await initializeStimBuffApiLibCReq(*lib, false);
|
||||
}
|
||||
|
||||
@@ -379,17 +305,15 @@ StimBuffApiManager::finalizeAllStimBuffApiLibsCReq()
|
||||
{
|
||||
assertBodyThread();
|
||||
|
||||
StimBuffApiManager &sbam = getInstance();
|
||||
sscl::co::CoQutex::ReleaseHandle sbamGuard =
|
||||
co_await sbam.s.lock.getAcquireInvocationAndSuspensionPolicy();
|
||||
sscl::co::CoQutex::ReleaseHandle listGuard =
|
||||
co_await s.lock.getAcquireInvocationAndSuspensionPolicy();
|
||||
|
||||
for (auto &lib : sbam.s.rsrc.stimBuffApiLibs) {
|
||||
for (auto& lib : s.rsrc.libs) {
|
||||
co_await finalizeStimBuffApiLibCReq(*lib, false);
|
||||
}
|
||||
|
||||
co_return;
|
||||
}
|
||||
|
||||
|
||||
} // namespace stim_buff
|
||||
} // namespace smo
|
||||
|
||||
Reference in New Issue
Block a user