1 /**
2 * Copyright 2021 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <numeric>
17 #include "minddata/dataset/plugin/plugin_loader.h"
18 #include "minddata/dataset/plugin/shared_lib_util.h"
19 #include "mindspore/core/utils/log_adapter.h"
20
21 namespace mindspore {
22 namespace dataset {
GetInstance()23 PluginLoader *PluginLoader::GetInstance() noexcept {
24 static PluginLoader pl;
25 return &pl;
26 }
27
~PluginLoader()28 PluginLoader::~PluginLoader() {
29 std::vector<std::string> keys;
30 // get the keys from map, this is to avoid concurrent iteration and delete
31 std::transform(plugins_.begin(), plugins_.end(), std::back_inserter(keys), [](const auto &p) { return p.first; });
32 for (std::string &key : keys) {
33 Status rc = UnloadPlugin(key);
34 MSLOG_IF(ERROR, rc.IsError(), mindspore::NoExceptionType) << rc.ToString();
35 }
36 }
37
38 // LoadPlugin() is NOT thread-safe. It is supposed to be called when Ops are being built. E.g. PluginOp should call this
39 // within constructor instead of in its Compute() which is parallel.
LoadPlugin(const std::string & filename,plugin::PluginManagerBase ** singleton_plugin)40 Status PluginLoader::LoadPlugin(const std::string &filename, plugin::PluginManagerBase **singleton_plugin) {
41 RETURN_UNEXPECTED_IF_NULL(singleton_plugin);
42 auto itr = plugins_.find(filename);
43 // return ok if this module is already loaded
44 if (itr != plugins_.end()) {
45 *singleton_plugin = itr->second.first;
46 return Status::OK();
47 }
48 // Open the .so file
49 void *handle = SharedLibUtil::Load(filename);
50 CHECK_FAIL_RETURN_UNEXPECTED(handle != nullptr, "fail to load:" + filename + ".\n" + SharedLibUtil::ErrMsg());
51
52 // Load GetInstance function ptr from the so file, so needs to be compiled with -fPIC
53 void *func_handle = SharedLibUtil::FindSym(handle, "GetInstance");
54 CHECK_FAIL_RETURN_UNEXPECTED(func_handle != nullptr, "fail to find GetInstance()\n" + SharedLibUtil::ErrMsg());
55
56 // cast the returned function ptr of type void* to the type of GetInstance
57 plugin::PluginManagerBase *(*get_instance)(plugin::MindDataManagerBase *) =
58 reinterpret_cast<plugin::PluginManagerBase *(*)(plugin::MindDataManagerBase *)>(func_handle);
59 RETURN_UNEXPECTED_IF_NULL(get_instance);
60
61 *singleton_plugin = get_instance(nullptr); // call function ptr to get instance
62 RETURN_UNEXPECTED_IF_NULL(*singleton_plugin);
63
64 std::string v1 = (*singleton_plugin)->GetPluginVersion(), v2(plugin::kSharedIncludeVersion);
65 if (v1 != v2) {
66 std::string err_msg = "[Plugin Version Error] expected:" + v2 + ", received:" + v1 + " please recompile.";
67 if (SharedLibUtil::Close(handle) != 0) err_msg += ("\ndlclose() error, err_msg:" + SharedLibUtil::ErrMsg() + ".");
68 RETURN_STATUS_UNEXPECTED(err_msg);
69 }
70
71 const std::map<std::string, std::set<std::string>> module_names = (*singleton_plugin)->GetModuleNames();
72 for (auto &p : module_names) {
73 std::string msg = "Plugin " + p.first + " has module:";
74 MS_LOG(DEBUG) << std::accumulate(p.second.begin(), p.second.end(), msg,
75 [](const std::string &msg, const std::string &nm) { return msg + " " + nm; });
76 }
77
78 // save the name and handle
79 std::pair<plugin::PluginManagerBase *, void *> plugin_new = std::make_pair(*singleton_plugin, handle);
80 plugins_.insert({filename, plugin_new});
81 return Status::OK();
82 }
83
UnloadPlugin(const std::string & filename)84 Status PluginLoader::UnloadPlugin(const std::string &filename) {
85 auto itr = plugins_.find(filename);
86 RETURN_OK_IF_TRUE(itr == plugins_.end()); // return true if this plugin was never loaded or already removed
87
88 void *func_handle = SharedLibUtil::FindSym(itr->second.second, "DestroyInstance");
89 CHECK_FAIL_RETURN_UNEXPECTED(func_handle != nullptr, "fail to find DestroyInstance()\n" + SharedLibUtil::ErrMsg());
90
91 void (*destroy_instance)() = reinterpret_cast<void (*)()>(func_handle);
92 RETURN_UNEXPECTED_IF_NULL(destroy_instance);
93
94 destroy_instance();
95 CHECK_FAIL_RETURN_UNEXPECTED(SharedLibUtil::Close(itr->second.second) == 0,
96 "dlclose() error: " + SharedLibUtil::ErrMsg());
97
98 plugins_.erase(filename);
99 return Status::OK();
100 }
101 } // namespace dataset
102 } // namespace mindspore
103