-
Notifications
You must be signed in to change notification settings - Fork 206
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
Open
briancoutinho
wants to merge
10
commits into
pytorch:main
Choose a base branch
from
briancoutinho:bcoutinho/dynamic_plugin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3f04acf
Add dynamic plugin along with counter events, cupti disable support
d33f6ce
add unit test
826f0ed
update file names, refactor src files in dynamic_plugin dir, and clan…
f3c6641
address comments
406fb9a
add interface to return supported activities and correctly handle the…
4cfd14d
rename plugin interface file
d02f45e
address comments, add pushpop corr interface, better test, complete e…
185ba17
make missing implementation log warning once
85acbd1
Enable passing enabled activities down the wire in the dynamic plugin…
676135f
add the device info API to trace builder
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do you plan to add support for Windows?
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.
@australopitek Not planning to in this PR but it should be an easy addition on top of this PR. Personally, I don't have a Windows setup to test things right now.