-
Notifications
You must be signed in to change notification settings - Fork 106
/
kernel_tracing.cpp
358 lines (327 loc) · 12.4 KB
/
kernel_tracing.cpp
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*************************************************************************
* Copyright (C) [2024] by Cambricon, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*************************************************************************/
#include <cxxabi.h>
#include <dlfcn.h>
#include <pthread.h> // If we support c++17, we could use std::shared_mutex instead
#include <stdio.h>
#include <stdlib.h>
#include <tuple>
#include <vector>
#include <map>
#include <mutex> // NOLINT
#include "config_env.h"
#include "cnrt.h"
#include "logging.h"
#include "macros.h"
#include "mlu_op_internal_api.h"
#include "subscriber.hpp"
#include "tool.h"
#if MLUOP_TRACE_WITH_DLOPEN
#define WRAP_FUNC(func) func
#else
#define WRAP_FUNC(func) \
MLUOP_ATTRIBUTE_WEAK MLUOP_ATTRIBUTE_VISIBILITY_HIDDEN __wrap_##func
#endif
#ifdef DBG_LOG
#undef DBG_LOG
#endif
#if MLUOP_TRACE_WITH_DLOPEN
#define DBG_LOG VLOG(1) << "[KERNEL_HOOK_DLOPEN] "
#else
#define DBG_LOG VLOG(8) << "[KERNEL_HOOK] "
#endif
#if MLUOP_TRACE_WITH_DLOPEN
#include <sstream>
#include <errno.h> // NOLINT
#include <dlfcn.h> // NOLINT
#include <sys/types.h> // NOLINT
#include <sys/syscall.h> // NOLINT
#include <unistd.h> // NOLINT
#define gettid() ((pid_t)syscall(SYS_gettid))
#define DLOPEN_TRACE_LOG(msg) \
do { \
std::ostringstream oss; \
oss << "[KERNEL_HOOK_DLOPEN] sid: " << getsid(0) \
<< ", pgid: " << getpgid(0) << ", ppid: " << getppid() \
<< ", pid: " << getpid() << ", tid: " << gettid() << ", " \
<< program_invocation_name << ": " << msg << ";"; \
fprintf(stderr, "%s\n", oss.str().c_str()); \
} while (0)
#else
#define DLOPEN_TRACE_LOG(...)
#endif
using mluop::cfg::Config;
using mluop::cfg::ConfigEnvType;
inline static bool load_config_from_env_kernel_tracing() {
// NOTE should be true by default
// only set false for special debug purpose, that will disable tracing /
// crop_tool completely
static bool flag =
mluop::getBoolEnvVar(CFG_ENUM_TO_STR(MLUOP_DEBUG_KERNEL_TRACING), true);
return flag;
}
namespace {
class kernelMapping {
public:
static kernelMapping &instance() {
static kernelMapping kernel_mapping;
return kernel_mapping;
}
static void addKernel(const void *key, const char *symbol) {
WriteLock lock(instance().rwlock_);
instance().kernel_mapping_raw_[key] = symbol;
int status = 0;
char *name = abi::__cxa_demangle(symbol, NULL, NULL, &status);
if (status != 0) {
LOG(ERROR) << "demangle kernel symbol failed for " << symbol
<< ", status is " << status;
instance().kernel_mapping_[key] = symbol;
} else {
DBG_LOG << " add device kernel function: " << name;
instance().kernel_mapping_[key] = name;
free(name);
}
}
static inline const char *getKernelName(const void *key) {
ReadLock lock(instance().rwlock_);
return instance().kernel_mapping_[key].c_str();
}
~kernelMapping() { pthread_rwlock_destroy(&rwlock_); }
private:
kernelMapping() = default;
std::map<const void *, std::string> kernel_mapping_raw_;
std::map<const void *, std::string> kernel_mapping_;
pthread_rwlock_t rwlock_ = PTHREAD_RWLOCK_INITIALIZER;
};
} // namespace
extern "C" {
MLUOP_WIN_API mluOpStatus_t mluOpInternalGetKernelName(const void *kernel,
const char **name,
int *) {
*name = kernelMapping::getKernelName(kernel);
return MLUOP_STATUS_SUCCESS;
}
}
static void addKernel(const mluop::pubsub::ParamBangRegisterFunction *param,
void *) {
const char *symbol = param->deviceName;
// TODO(NONE): if we are under LD_PRELOAD, we could consider calling addKernel
// inside mluOp.so
kernelMapping::addKernel(param->hostFunc, symbol);
}
static int initTracing(bool enable) {
#if MLUOP_TRACE_WITH_DLOPEN
DLOPEN_TRACE_LOG("initTracing");
#else
DBG_LOG << "initTracing " << (int)enable;
#endif
Config::set_event<ConfigEnvType::MLUOP_EVENT_ENABLE_KERNEL>(enable);
if (!enable) return -1;
// TODO(NONE): call once
auto idx = mluop::pubsub::Publisher::subscribe(
mluop::pubsub::EventType::BANG_REGISTER_FUNCTION,
(void (*)(const void *, void *))addKernel, nullptr);
mluop::pubsub::Publisher::save_internal_subscriber(
mluop::pubsub::EventType::BANG_REGISTER_FUNCTION, idx);
return 0;
}
// just a demo method, to log kernel invoked, could be func for kernel tracing
// crop
static void logCnrtInvokeKernel(
const struct mluOpEventParamCnrtInvokeKernel *param, void *) {
const char *name = kernelMapping::getKernelName(param->kernel);
DBG_LOG << "invoking " << name;
}
static void __attribute__((constructor)) _my_init() {
DBG_LOG << __FILE__ << " ctor";
if (VLOG_IS_ON(7)) {
auto idx = mluop::pubsub::Publisher::subscribe(
mluop::pubsub::EventType::CNRT_INVOKE_KERNEL,
(void (*)(const void *, void *))logCnrtInvokeKernel, nullptr);
mluop::pubsub::Publisher::save_internal_subscriber(
mluop::pubsub::EventType::CNRT_INVOKE_KERNEL, idx);
}
}
#if MLUOP_TRACE_WITH_DLOPEN
// workaround for linaro gcc 6 which does not support `constexpr if` and type
// trait _v suffix
#if __cpp_if_constexpr >= 201606
#define IMPL_HOOK_WITH_IF_CONSTEXPR 1
#else
#define IMPL_HOOK_WITH_IF_CONSTEXPR 0
#endif
namespace {
#if IMPL_HOOK_WITH_IF_CONSTEXPR == 0
template <typename RetType>
RetType on_failed() {
return (RetType)0;
}
template <>
void on_failed() {}
template <>
cnrtRet_t on_failed() {
return cnrtErrorInvalidSymbol;
}
#endif
#ifdef DEF_LIBCNRT_WRAPPER
#undef DEF_LIBCNRT_WRAPPER
#endif
#define DEF_LIBCNRT_WRAPPER(RetType, Func) \
template <typename... ArgType> \
static RetType Func(ArgType... args) { \
return dispatch<RetType, ArgType...>(#Func, args...); \
}
class libcnrt {
public:
DEF_LIBCNRT_WRAPPER(void, __bangRegisterFunction);
DEF_LIBCNRT_WRAPPER(cnrtRet_t, cnrtInvokeKernel);
template <typename RetType, typename... ArgType>
static RetType dispatch(const char fname[], ArgType... args) {
static auto fptr = getInstance().getFuncImpl<RetType, ArgType...>(fname);
return fptr(args...);
}
template <typename RetType, typename... ArgType>
static RetType dispatch_failed(ArgType...) {
#if IMPL_HOOK_WITH_IF_CONSTEXPR
if constexpr (std::is_same_v<RetType, void>) {
return;
} else if constexpr (std::is_same_v<RetType, cnrtRet_t>) {
return cnrtErrorInvalidSymbol;
}
return (RetType)0;
#else
return on_failed<RetType>();
#endif
}
const std::string name() const {
// XXX just a backdoor, to dlopen libcnrt.so with another name
const char *cnrt_name = std::getenv("LIBCNRT_NAME");
if (cnrt_name != nullptr && strlen(cnrt_name)) {
return cnrt_name;
}
return "libcnrt.so";
}
static libcnrt &getInstance() {
static libcnrt lib;
return lib;
}
static void *getHandle() {
static void *handle = getInstance().getHandleImpl();
return handle;
}
private:
template <typename RetType, typename... ArgType>
using F = RetType (*)(ArgType...);
template <typename RetType, typename... ArgType>
F<RetType, ArgType...> getFuncImpl(const char fname[]) {
DBG_LOG << "dlsym " << fname;
void *symbol = dlsym(getHandle(), fname);
if (symbol == nullptr) {
LOG(ERROR) << "dlsym " << fname << " failed, reason: " << dlerror();
return dispatch_failed<RetType, ArgType...>;
}
return (F<RetType, ArgType...>)symbol;
}
void *getHandleImpl() {
void *handle = dlopen(name().c_str(), RTLD_LAZY | RTLD_LOCAL);
if (handle == nullptr) {
std::ostringstream err;
err << "dlopen " << name() << " failed, reason: " << dlerror();
LOG(ERROR) << err.str();
}
return handle;
}
libcnrt() = default;
}; // class libcnrt
} // namespace
#endif // MLUOP_TRACE_WITH_DLOPEN
extern "C" {
#if MLUOP_TRACE_WITH_DLOPEN
static void __real___bangRegisterFunction(void **module, const char *hostFunc,
char *deviceFunc,
const char *deviceName,
int unionLimit, cnrtDim3_t *taskId,
cnrtDim3_t *taskDim, int *wSize) {
return libcnrt::__bangRegisterFunction(module, hostFunc, deviceFunc,
deviceName, unionLimit, taskId,
taskDim, wSize);
}
static cnrtRet_t __real_cnrtInvokeKernel(const void *kernel, cnrtDim3_t dim,
cnrtFunctionType_t ktype, void **args,
size_t reserved, cnrtQueue_t queue) {
return libcnrt::cnrtInvokeKernel(kernel, dim, ktype, args, reserved, queue);
}
#else // MLUOP_TRACE_WITH_DLOPEN
#if !defined(MLUOP_EXPERIMENTAL_TRACE)
__attribute__((weak))
#endif
extern void
__real___bangRegisterFunction(void **module, const char *hostFunc,
char *deviceFunc, const char *deviceName,
int unionLimit, cnrtDim3_t *taskId,
cnrtDim3_t *taskDim, int *wSize);
#if !defined(MLUOP_EXPERIMENTAL_TRACE)
__attribute__((weak))
#endif
extern cnrtRet_t
__real_cnrtInvokeKernel(const void *kernel, cnrtDim3_t dim,
cnrtFunctionType_t ktype, void **args, size_t reserved,
cnrtQueue_t queue);
#endif // MLUOP_TRACE_WITH_DLOPEN
void WRAP_FUNC(__bangRegisterFunction)(void **module, const char *hostFunc,
char *deviceFunc, const char *deviceName,
int unionLimit, cnrtDim3_t *taskId,
cnrtDim3_t *taskDim, int *wSize) {
#if MLUOP_TRACE_WITH_DLOPEN
DBG_LOG << __func__ << ": " << deviceName;
#endif
// this function is invoked before global static data initialization and .ctor
static bool flag_hook = load_config_from_env_kernel_tracing();
static int __attribute__((unused)) init_tracing = initTracing(flag_hook);
if (flag_hook) {
mluop::pubsub::ParamBangRegisterFunction params{module, hostFunc,
deviceName, wSize};
mluop::pubsub::Publisher::publish(
mluop::pubsub::EventType::BANG_REGISTER_FUNCTION, ¶ms);
}
return __real___bangRegisterFunction(module, hostFunc, deviceFunc, deviceName,
unionLimit, taskId, taskDim, wSize);
}
// could be used together with '-Wl,--wrap' linker flag, could override
// cnrtInvokeKernel
cnrtRet_t WRAP_FUNC(cnrtInvokeKernel)(const void *kernel, cnrtDim3_t dim,
cnrtFunctionType_t ktype, void **args,
size_t reserved, cnrtQueue_t queue) {
// TODO(NONE): add flag to disable event publish (like cnpapi's enable
// callback)
#if MLUOP_TRACE_WITH_DLOPEN
DBG_LOG << __func__ << ": " << kernelMapping::getKernelName(kernel);
#endif
if (Config::get_event<ConfigEnvType::MLUOP_EVENT_ENABLE_KERNEL>()) {
mluOpEventParamCnrtInvokeKernel params{kernel, dim, ktype, args};
mluop::pubsub::Publisher::publish(
mluop::pubsub::EventType::CNRT_INVOKE_KERNEL, ¶ms);
}
return __real_cnrtInvokeKernel(kernel, dim, ktype, args, reserved, queue);
}
}