-
Notifications
You must be signed in to change notification settings - Fork 207
Add dynamic plugin along with counter events, cupti disable support #1148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 10 commits
3f04acf
d33f6ce
826f0ed
f3c6641
406fb9a
4cfd14d
d02f45e
185ba17
85acbd1
676135f
b7d91ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| #pragma once | ||
|
|
||
| #include <dlfcn.h> | ||
| #include <filesystem> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| #include "Logger.h" | ||
| #include "KinetoDynamicPluginInterface.h" | ||
| #include "PluginProfiler.h" | ||
|
|
||
| namespace libkineto { | ||
|
|
||
| class PluginRegistry { | ||
| public: | ||
| static PluginRegistry &instance() { | ||
| static PluginRegistry instance; | ||
| return instance; | ||
| } | ||
|
|
||
| int registerPluginProfiler(const KinetoPlugin_ProfilerInterface *pProfiler) { | ||
| if (pProfiler == nullptr) { | ||
| LOG(ERROR) << "Failed to register plugin profiler of nullptr"; | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| // Store in raw registry | ||
| rawPluginProfilers_.push_back(*pProfiler); | ||
|
|
||
| // Pass to internal registry | ||
| const auto &profiler = rawPluginProfilers_.back(); | ||
| libkineto::api().registerProfilerFactory( | ||
| [profiler]() -> std::unique_ptr<IActivityProfiler> { | ||
| return std::make_unique<PluginProfiler>(profiler); | ||
| }); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| const KinetoPlugin_Registry toCRegistry() { | ||
| return KinetoPlugin_Registry{ | ||
| .unpaddedStructSize = KINETO_PLUGIN_REGISTRY_UNPADDED_STRUCT_SIZE, | ||
| .pRegistryHandle = | ||
| reinterpret_cast<KinetoPlugin_RegistryHandle *>(this), | ||
| .registerProfiler = cRegisterProfiler}; | ||
| } | ||
|
|
||
| private: | ||
| PluginRegistry() = default; | ||
| ~PluginRegistry() = default; | ||
| PluginRegistry(const PluginRegistry &) = delete; | ||
| PluginRegistry &operator=(const PluginRegistry &) = delete; | ||
|
|
||
| static int | ||
| cRegisterProfiler(KinetoPlugin_RegistryHandle *pRegistryHandle, | ||
| const KinetoPlugin_ProfilerInterface *pProfiler) { | ||
| auto pPluginRegistry = reinterpret_cast<PluginRegistry *>(pRegistryHandle); | ||
| return pPluginRegistry->registerPluginProfiler(pProfiler); | ||
| } | ||
|
|
||
| std::vector<KinetoPlugin_ProfilerInterface> rawPluginProfilers_; | ||
| }; | ||
|
|
||
| inline void loadPlugins() { | ||
| const char *pPluginLibDirPathEnvVar = KINETO_PLUGIN_LIB_DIR_PATH_ENV_VARIABLE; | ||
|
|
||
| const char *pPluginLibDirPath = getenv(pPluginLibDirPathEnvVar); | ||
|
|
||
| if (pPluginLibDirPath == nullptr) { | ||
| LOG(VERBOSE) << "Environment variable " << pPluginLibDirPathEnvVar | ||
| << " not set"; | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (unsetenv(pPluginLibDirPathEnvVar) == -1) { | ||
| LOG(ERROR) << "Failed to unset environment variable " | ||
| << pPluginLibDirPathEnvVar << " at unsetenv() with error " | ||
| << strerror(errno); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| std::vector<std::string> libFilePaths; | ||
| try { | ||
| for (const auto &entry : | ||
| std::filesystem::directory_iterator(pPluginLibDirPath)) { | ||
| if (entry.is_regular_file() && entry.path().extension() == ".so") { | ||
| libFilePaths.push_back(entry.path().string()); | ||
| } | ||
| } | ||
| } catch (const std::filesystem::filesystem_error &e) { | ||
| LOG(ERROR) << "Error: " << e.what(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| PluginRegistry &pluginRegistry = PluginRegistry::instance(); | ||
| KinetoPlugin_Registry cPluginRegistry = pluginRegistry.toCRegistry(); | ||
|
|
||
| for (const auto &libFilePath : libFilePaths) { | ||
| // Clear error state | ||
| dlerror(); | ||
|
|
||
| void *pHandle = dlopen(libFilePath.c_str(), RTLD_LAZY); | ||
|
||
| if (pHandle == nullptr) { | ||
| char *pError = dlerror(); | ||
| LOG(WARNING) << "Failed to open " << libFilePath | ||
| << " at dlopen() with error " << pError; | ||
| continue; | ||
| } | ||
|
|
||
| int (*pfxRegister)(const KinetoPlugin_Registry *pRegistry) = | ||
| reinterpret_cast<int (*)(const KinetoPlugin_Registry *pRegistry)>( | ||
| dlsym(pHandle, "KinetoPlugin_register")); | ||
|
|
||
| if (pfxRegister == nullptr) { | ||
| char *pError = dlerror(); | ||
| LOG(VERBOSE) << "Failed to find symbol KinetoPlugin_register() from " | ||
| << libFilePath << " at dlsym() with error " << pError; | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| LOG(INFO) << "Found symbol KinetoPlugin_register() from " << libFilePath; | ||
|
|
||
| int errorCode = pfxRegister(&cPluginRegistry); | ||
| if (errorCode != 0) { | ||
| LOG(ERROR) << "Failed to register plugin profiler from " << libFilePath | ||
| << " at pfxRegister() with error " << errorCode; | ||
| } | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| } // namespace libkineto | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are we not adding pluginutils.h?