Skip to content

Commit 5138022

Browse files
igchorkbenzie
authored andcommitted
Use leak checker and API logging (for v2) from loader (#17536)
1 parent b6d4998 commit 5138022

File tree

4 files changed

+42
-135
lines changed

4 files changed

+42
-135
lines changed

source/adapters/level_zero/adapter.cpp

Lines changed: 21 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -308,26 +308,25 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
308308

309309
if (UrL0Debug & UR_L0_DEBUG_BASIC) {
310310
logger.setLegacySink(std::make_unique<ur_legacy_sink>());
311+
#ifdef UR_ADAPTER_LEVEL_ZERO_V2
312+
setEnvVar("ZEL_ENABLE_LOADER_LOGGING", "1");
313+
setEnvVar("ZEL_LOADER_LOGGING_LEVEL", "trace");
314+
setEnvVar("ZEL_LOADER_LOG_CONSOLE", "1");
315+
setEnvVar("ZE_ENABLE_VALIDATION_LAYER", "1");
316+
#endif
311317
};
312318

313319
if (UrL0Debug & UR_L0_DEBUG_VALIDATION) {
314320
setEnvVar("ZE_ENABLE_VALIDATION_LAYER", "1");
315321
setEnvVar("ZE_ENABLE_PARAMETER_VALIDATION", "1");
316322
}
317323

318-
PlatformCache.Compute = [](Result<PlatformVec> &result) {
319-
static std::once_flag ZeCallCountInitialized;
320-
try {
321-
std::call_once(ZeCallCountInitialized, []() {
322-
if (UrL0LeaksDebug) {
323-
ZeCallCount = new std::map<std::string, int>;
324-
}
325-
});
326-
} catch (...) {
327-
result = exceptionToResult(std::current_exception());
328-
return;
329-
}
324+
if (UrL0LeaksDebug) {
325+
setEnvVar("ZE_ENABLE_VALIDATION_LAYER", "1");
326+
setEnvVar("ZEL_ENABLE_BASIC_LEAK_CHECKER", "1");
327+
}
330328

329+
PlatformCache.Compute = [](Result<PlatformVec> &result) {
331330
uint32_t UserForcedSysManInit = 0;
332331
// Check if the user has disabled the default L0 Env initialization.
333332
const int UrSysManEnvInitEnabled = [&UserForcedSysManInit] {
@@ -417,6 +416,16 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
417416
loader_version.patch >= 2)) {
418417
useInitDrivers = true;
419418
}
419+
420+
#ifdef UR_ADAPTER_LEVEL_ZERO_V2
421+
if ((loader_version.major == 1 && loader_version.minor < 21) ||
422+
(loader_version.major == 1 && loader_version.minor == 21 &&
423+
loader_version.patch < 2)) {
424+
logger::warning(
425+
"WARNING: Level Zero Loader version is older than 1.21.2. "
426+
"Please update to the latest version for API logging support.\n");
427+
}
428+
#endif
420429
}
421430

422431
if (useInitDrivers) {
@@ -533,97 +542,6 @@ void globalAdapterOnDemandCleanup() {
533542
}
534543

535544
ur_result_t adapterStateTeardown() {
536-
// Print the balance of various create/destroy native calls.
537-
// The idea is to verify if the number of create(+) and destroy(-) calls are
538-
// matched.
539-
if (ZeCallCount && (UrL0LeaksDebug) != 0) {
540-
bool LeakFound = false;
541-
// clang-format off
542-
//
543-
// The format of this table is such that each row accounts for a
544-
// specific type of objects, and all elements in the raw except the last
545-
// one are allocating objects of that type, while the last element is known
546-
// to deallocate objects of that type.
547-
//
548-
std::vector<std::vector<std::string>> CreateDestroySet = {
549-
{"zeContextCreate", "zeContextDestroy"},
550-
{"zeCommandQueueCreate", "zeCommandQueueDestroy"},
551-
{"zeModuleCreate", "zeModuleDestroy"},
552-
{"zeKernelCreate", "zeKernelDestroy"},
553-
{"zeEventPoolCreate", "zeEventPoolDestroy"},
554-
{"zeCommandListCreateImmediate", "zeCommandListCreate", "zeCommandListDestroy"},
555-
{"zeEventCreate", "zeEventDestroy"},
556-
{"zeFenceCreate", "zeFenceDestroy"},
557-
{"zeImageCreate","zeImageViewCreateExt", "zeImageDestroy"},
558-
{"zeSamplerCreate", "zeSamplerDestroy"},
559-
{"zeMemAllocDevice", "zeMemAllocHost", "zeMemAllocShared", "zeMemFree"},
560-
};
561-
562-
// A sample output aimed below is this:
563-
// ------------------------------------------------------------------------
564-
// zeContextCreate = 1 \---> zeContextDestroy = 1
565-
// zeCommandQueueCreate = 1 \---> zeCommandQueueDestroy = 1
566-
// zeModuleCreate = 1 \---> zeModuleDestroy = 1
567-
// zeKernelCreate = 1 \---> zeKernelDestroy = 1
568-
// zeEventPoolCreate = 1 \---> zeEventPoolDestroy = 1
569-
// zeCommandListCreateImmediate = 1 |
570-
// zeCommandListCreate = 1 \---> zeCommandListDestroy = 1 ---> LEAK = 1
571-
// zeEventCreate = 2 \---> zeEventDestroy = 2
572-
// zeFenceCreate = 1 \---> zeFenceDestroy = 1
573-
// zeImageCreate = 0 \---> zeImageDestroy = 0
574-
// zeSamplerCreate = 0 \---> zeSamplerDestroy = 0
575-
// zeMemAllocDevice = 0 |
576-
// zeMemAllocHost = 1 |
577-
// zeMemAllocShared = 0 \---> zeMemFree = 1
578-
//
579-
// clang-format on
580-
// TODO: use logger to print this messages
581-
std::cerr << "Check balance of create/destroy calls\n";
582-
std::cerr << "----------------------------------------------------------\n";
583-
std::stringstream ss;
584-
for (const auto &Row : CreateDestroySet) {
585-
int diff = 0;
586-
for (auto I = Row.begin(); I != Row.end();) {
587-
const char *ZeName = (*I).c_str();
588-
const auto &ZeCount = (*ZeCallCount)[*I];
589-
590-
bool First = (I == Row.begin());
591-
bool Last = (++I == Row.end());
592-
593-
if (Last) {
594-
ss << " \\--->";
595-
diff -= ZeCount;
596-
} else {
597-
diff += ZeCount;
598-
if (!First) {
599-
ss << " | ";
600-
std::cerr << ss.str() << "\n";
601-
ss.str("");
602-
ss.clear();
603-
}
604-
}
605-
ss << std::setw(30) << std::right << ZeName;
606-
ss << " = ";
607-
ss << std::setw(5) << std::left << ZeCount;
608-
}
609-
610-
if (diff) {
611-
LeakFound = true;
612-
ss << " ---> LEAK = " << diff;
613-
}
614-
615-
std::cerr << ss.str() << '\n';
616-
ss.str("");
617-
ss.clear();
618-
}
619-
620-
ZeCallCount->clear();
621-
delete ZeCallCount;
622-
ZeCallCount = nullptr;
623-
if (LeakFound)
624-
return UR_RESULT_ERROR_INVALID_MEM_OBJECT;
625-
}
626-
627545
// Due to multiple DLLMain definitions with SYCL, register to cleanup the
628546
// Global Adapter after refcnt is 0
629547
#if defined(_WIN32)

source/adapters/level_zero/common.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,18 @@ void zeParseError(ze_result_t ZeError, const char *&ErrorString) {
136136
} // switch
137137
}
138138

139+
#ifdef UR_ADAPTER_LEVEL_ZERO_V2
140+
ze_result_t ZeCall::doCall(ze_result_t ZeResult, const char *, const char *,
141+
bool) {
142+
return ZeResult;
143+
}
144+
#else
139145
ze_result_t ZeCall::doCall(ze_result_t ZeResult, const char *ZeName,
140146
const char *ZeArgs, bool TraceError) {
141147
logger::debug("ZE ---> {}{}", ZeName, ZeArgs);
142148

143149
if (ZeResult == ZE_RESULT_SUCCESS) {
144-
if (UrL0LeaksDebug) {
145-
++(*ZeCallCount)[ZeName];
146-
}
147-
return ZE_RESULT_SUCCESS;
150+
return ZeResult;
148151
}
149152

150153
if (TraceError) {
@@ -154,6 +157,7 @@ ze_result_t ZeCall::doCall(ze_result_t ZeResult, const char *ZeName,
154157
}
155158
return ZeResult;
156159
}
160+
#endif
157161

158162
// Specializations for various L0 structures
159163
template <> ze_structure_type_t getZeStructureType<ze_event_pool_desc_t>() {

source/adapters/level_zero/common.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const int UrL0LeaksDebug = [] {
7474
const char *UrRet = std::getenv("UR_L0_LEAKS_DEBUG");
7575
if (!UrRet)
7676
return 0;
77+
7778
return std::atoi(UrRet);
7879
}();
7980

source/adapters/level_zero/v2/common.hpp

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,8 @@
1515

1616
#include "../common.hpp"
1717
#include "logger/ur_logger.hpp"
18-
namespace {
19-
#define DECLARE_DESTROY_FUNCTION(name) \
20-
template <typename ZeHandleT> ze_result_t name##_wrapped(ZeHandleT handle) { \
21-
return ZE_CALL_NOCHECK_NAME(name, (handle), #name); \
22-
}
23-
24-
#define HANDLE_WRAPPER_TYPE(handle, destroy) \
25-
ze_handle_wrapper<handle, destroy##_wrapped<handle>>
26-
} // namespace
2718

2819
namespace v2 {
29-
30-
DECLARE_DESTROY_FUNCTION(zeKernelDestroy)
31-
DECLARE_DESTROY_FUNCTION(zeEventDestroy)
32-
DECLARE_DESTROY_FUNCTION(zeEventPoolDestroy)
33-
DECLARE_DESTROY_FUNCTION(zeContextDestroy)
34-
DECLARE_DESTROY_FUNCTION(zeCommandListDestroy)
35-
DECLARE_DESTROY_FUNCTION(zeImageDestroy)
3620
namespace raii {
3721

3822
template <typename ZeHandleT, ze_result_t (*destroy)(ZeHandleT)>
@@ -108,23 +92,23 @@ struct ze_handle_wrapper {
10892
bool ownZeHandle;
10993
};
11094

111-
using ze_kernel_handle_t = HANDLE_WRAPPER_TYPE(::ze_kernel_handle_t,
112-
zeKernelDestroy);
95+
using ze_kernel_handle_t =
96+
ze_handle_wrapper<::ze_kernel_handle_t, zeKernelDestroy>;
11397

114-
using ze_event_handle_t = HANDLE_WRAPPER_TYPE(::ze_event_handle_t,
115-
zeEventDestroy);
98+
using ze_event_handle_t =
99+
ze_handle_wrapper<::ze_event_handle_t, zeEventDestroy>;
116100

117-
using ze_event_pool_handle_t = HANDLE_WRAPPER_TYPE(::ze_event_pool_handle_t,
118-
zeEventPoolDestroy);
101+
using ze_event_pool_handle_t =
102+
ze_handle_wrapper<::ze_event_pool_handle_t, zeEventPoolDestroy>;
119103

120-
using ze_context_handle_t = HANDLE_WRAPPER_TYPE(::ze_context_handle_t,
121-
zeContextDestroy);
104+
using ze_context_handle_t =
105+
ze_handle_wrapper<::ze_context_handle_t, zeContextDestroy>;
122106

123-
using ze_command_list_handle_t = HANDLE_WRAPPER_TYPE(::ze_command_list_handle_t,
124-
zeCommandListDestroy);
107+
using ze_command_list_handle_t =
108+
ze_handle_wrapper<::ze_command_list_handle_t, zeCommandListDestroy>;
125109

126-
using ze_image_handle_t = HANDLE_WRAPPER_TYPE(::ze_image_handle_t,
127-
zeImageDestroy);
110+
using ze_image_handle_t =
111+
ze_handle_wrapper<::ze_image_handle_t, zeImageDestroy>;
128112

129113
} // namespace raii
130114
} // namespace v2

0 commit comments

Comments
 (0)