forked from triton-inference-server/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared_library.cc
245 lines (214 loc) · 7.5 KB
/
shared_library.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "shared_library.h"
#include "filesystem/api.h"
#include "mutex"
#include "triton/common/logging.h"
#ifdef TRITON_ENABLE_GPU
#include <cuda_runtime_api.h>
#endif
#ifdef _WIN32
// suppress the min and max definitions in Windef.h.
#define NOMINMAX
#include <Windows.h>
#else
#include <dlfcn.h>
#endif
namespace triton { namespace core {
static std::mutex mu_;
Status
SharedLibrary::Acquire(std::unique_ptr<SharedLibrary>* slib)
{
mu_.lock();
slib->reset(new SharedLibrary());
return Status::Success;
}
SharedLibrary::~SharedLibrary()
{
mu_.unlock();
}
Status
SharedLibrary::SetLibraryDirectory(const std::string& path)
{
#ifdef _WIN32
LOG_VERBOSE(1) << "SetLibraryDirectory: path = " << path;
if (!SetDllDirectory(path.c_str())) {
LPSTR err_buffer = nullptr;
size_t size = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&err_buffer, 0, NULL);
std::string errstr(err_buffer, size);
LocalFree(err_buffer);
return Status(
Status::Code::NOT_FOUND,
"unable to set dll path " + path + ": " + errstr);
}
#endif
return Status::Success;
}
Status
SharedLibrary::ResetLibraryDirectory()
{
#ifdef _WIN32
LOG_VERBOSE(1) << "ResetLibraryDirectory";
if (!SetDllDirectory(NULL)) {
LPSTR err_buffer = nullptr;
size_t size = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&err_buffer, 0, NULL);
std::string errstr(err_buffer, size);
LocalFree(err_buffer);
return Status(
Status::Code::NOT_FOUND, "unable to reset dll path: " + errstr);
}
#endif
return Status::Success;
}
Status
SharedLibrary::OpenLibraryHandle(const std::string& path, void** handle)
{
LOG_VERBOSE(1) << "OpenLibraryHandle: " << path;
#ifdef TRITON_ENABLE_GPU
// This call is to prevent a deadlock issue with dlopening backend shared
// libraries and calling CUDA APIs in other threads. Since CUDA API also
// dlopens some libraries and dlopen has an internal lock, it can create a
// deadlock. Intentionally ignore the CUDA_ERROR (if any) for containers
// running on a CPU.
int device_count;
cudaGetDeviceCount(&device_count);
#endif
#ifdef _WIN32
// Need to put shared library directory on the DLL path so that any
// dependencies of the shared library are found
const std::string library_dir = DirName(path);
RETURN_IF_ERROR(SetLibraryDirectory(library_dir));
// HMODULE is typedef of void*
// https://docs.microsoft.com/en-us/windows/win32/winprog/windows-data-types
LOG_VERBOSE(1) << "OpenLibraryHandle: path = " << path;
*handle = LoadLibrary(path.c_str());
// Remove the dll path added above... do this unconditionally before
// check for failure in dll load.
RETURN_IF_ERROR(ResetLibraryDirectory());
if (*handle == nullptr) {
LPSTR err_buffer = nullptr;
size_t size = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&err_buffer, 0, NULL);
std::string errstr(err_buffer, size);
LocalFree(err_buffer);
return Status(
Status::Code::NOT_FOUND, "unable to load shared library: " + errstr);
}
#else
*handle = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
if (*handle == nullptr) {
return Status(
Status::Code::NOT_FOUND,
"unable to load shared library: " + std::string(dlerror()));
}
#endif
return Status::Success;
}
Status
SharedLibrary::CloseLibraryHandle(void* handle)
{
if (handle != nullptr) {
#ifdef _WIN32
if (FreeLibrary((HMODULE)handle) == 0) {
LPSTR err_buffer = nullptr;
size_t size = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&err_buffer, 0, NULL);
std::string errstr(err_buffer, size);
LocalFree(err_buffer);
return Status(
Status::Code::INTERNAL, "unable to unload shared library: " + errstr);
}
#else
if (dlclose(handle) != 0) {
return Status(
Status::Code::INTERNAL,
"unable to unload shared library: " + std::string(dlerror()));
}
#endif
}
return Status::Success;
}
Status
SharedLibrary::GetEntrypoint(
void* handle, const std::string& name, const bool optional, void** befn)
{
*befn = nullptr;
#ifdef _WIN32
void* fn = GetProcAddress((HMODULE)handle, name.c_str());
if ((fn == nullptr) && !optional) {
LPSTR err_buffer = nullptr;
size_t size = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&err_buffer, 0, NULL);
std::string errstr(err_buffer, size);
LocalFree(err_buffer);
return Status(
Status::Code::NOT_FOUND,
"unable to find '" + name +
"' entrypoint in custom library: " + errstr);
}
#else
dlerror();
void* fn = dlsym(handle, name.c_str());
const char* dlsym_error = dlerror();
if (dlsym_error != nullptr) {
if (optional) {
return Status::Success;
}
std::string errstr(dlsym_error); // need copy as dlclose overwrites
return Status(
Status::Code::NOT_FOUND, "unable to find required entrypoint '" + name +
"' in shared library: " + errstr);
}
if (fn == nullptr) {
if (optional) {
return Status::Success;
}
return Status(
Status::Code::NOT_FOUND,
"unable to find required entrypoint '" + name + "' in shared library");
}
#endif
*befn = fn;
return Status::Success;
}
}} // namespace triton::core