Skip to content

Commit 7f8285c

Browse files
igchorgithub-actions[bot]
authored andcommitted
Unify logging and leak checking for L0 v1 and v2 (#19328)
This is needed so that we can enable V2 adapter by default on certain platforms: intel/llvm#19333 The reason is that we need to load both adapters (legacy and v2) to check the device version. However, loading v2 adapter causes L0 loader to emit logs for all API calls (if ZE_DEBUG=1 is set). Since the legacy adapter used different logic for printing API calls, this would result in printing the same logs twice. This patch fixes that.
1 parent cd4188b commit 7f8285c

File tree

4 files changed

+7
-133
lines changed

4 files changed

+7
-133
lines changed

source/adapters/level_zero/adapter.cpp

Lines changed: 5 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -309,32 +309,23 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
309309

310310
if (UrL0Debug & UR_L0_DEBUG_BASIC) {
311311
logger.setLegacySink(std::make_unique<ur_legacy_sink>());
312-
#ifdef UR_ADAPTER_LEVEL_ZERO_V2
313312
setEnvVar("ZEL_ENABLE_LOADER_LOGGING", "1");
314313
setEnvVar("ZEL_LOADER_LOGGING_LEVEL", "trace");
315314
setEnvVar("ZEL_LOADER_LOG_CONSOLE", "1");
316315
setEnvVar("ZE_ENABLE_VALIDATION_LAYER", "1");
317-
#endif
318316
};
319317

320318
if (UrL0Debug & UR_L0_DEBUG_VALIDATION) {
321319
setEnvVar("ZE_ENABLE_VALIDATION_LAYER", "1");
322320
setEnvVar("ZE_ENABLE_PARAMETER_VALIDATION", "1");
323321
}
324322

325-
PlatformCache.Compute = [](Result<PlatformVec> &result) {
326-
static std::once_flag ZeCallCountInitialized;
327-
try {
328-
std::call_once(ZeCallCountInitialized, []() {
329-
if (UrL0LeaksDebug) {
330-
ZeCallCount = new std::map<std::string, int>;
331-
}
332-
});
333-
} catch (...) {
334-
result = exceptionToResult(std::current_exception());
335-
return;
336-
}
323+
if (UrL0LeaksDebug) {
324+
setEnvVar("ZE_ENABLE_VALIDATION_LAYER", "1");
325+
setEnvVar("ZEL_ENABLE_BASIC_LEAK_CHECKER", "1");
326+
}
337327

328+
PlatformCache.Compute = [](Result<PlatformVec> &result) {
338329
uint32_t UserForcedSysManInit = 0;
339330
// Check if the user has disabled the default L0 Env initialization.
340331
const int UrSysManEnvInitEnabled = [&UserForcedSysManInit] {
@@ -426,7 +417,6 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
426417
useInitDrivers = true;
427418
}
428419

429-
#ifdef UR_ADAPTER_LEVEL_ZERO_V2
430420
if ((loader_version.major == 1 && loader_version.minor < 21) ||
431421
(loader_version.major == 1 && loader_version.minor == 21 &&
432422
loader_version.patch < 2)) {
@@ -435,7 +425,6 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
435425
"WARNING: Level Zero Loader version is older than 1.21.2. "
436426
"Please update to the latest version for API logging support.\n");
437427
}
438-
#endif
439428
}
440429

441430
if (useInitDrivers) {
@@ -552,97 +541,6 @@ void globalAdapterOnDemandCleanup() {
552541
}
553542

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

source/adapters/level_zero/common.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ bool setEnvVar(const char *name, const char *value) {
8686

8787
ZeUSMImportExtension ZeUSMImport;
8888

89-
std::map<std::string, int> *ZeCallCount = nullptr;
90-
9189
void zeParseError(ze_result_t ZeError, const char *&ErrorString) {
9290
switch (ZeError) {
9391
#define ZE_ERRCASE(ERR) \
@@ -137,31 +135,10 @@ void zeParseError(ze_result_t ZeError, const char *&ErrorString) {
137135
} // switch
138136
}
139137

140-
#ifdef UR_ADAPTER_LEVEL_ZERO_V2
141138
ze_result_t ZeCall::doCall(ze_result_t ZeResult, const char *, const char *,
142139
bool) {
143140
return ZeResult;
144141
}
145-
#else
146-
ze_result_t ZeCall::doCall(ze_result_t ZeResult, const char *ZeName,
147-
const char *ZeArgs, bool TraceError) {
148-
UR_LOG(DEBUG, "ZE ---> {}{}", ZeName, ZeArgs);
149-
150-
if (ZeResult == ZE_RESULT_SUCCESS) {
151-
if (UrL0LeaksDebug) {
152-
++(*ZeCallCount)[ZeName];
153-
}
154-
return ZE_RESULT_SUCCESS;
155-
}
156-
157-
if (TraceError) {
158-
const char *ErrorString = "Unknown";
159-
zeParseError(ZeResult, ErrorString);
160-
UR_LOG(ERR, "Error ({}) in {}", ErrorString, ZeName);
161-
}
162-
return ZeResult;
163-
}
164-
#endif
165142

166143
// Specializations for various L0 structures
167144
template <> ze_structure_type_t getZeStructureType<ze_event_pool_desc_t>() {

source/adapters/level_zero/common.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,6 @@ class ZeUSMImportExtension {
328328
// Helper wrapper for working with USM import extension in Level Zero.
329329
extern ZeUSMImportExtension ZeUSMImport;
330330

331-
// This will count the calls to Level-Zero
332-
extern std::map<std::string, int> *ZeCallCount;
333-
334331
// Some opencl extensions we know are supported by all Level Zero devices.
335332
constexpr char ZE_SUPPORTED_EXTENSIONS[] =
336333
"cl_khr_il_program cl_khr_subgroups cl_intel_subgroups "

source/adapters/level_zero/v2/command_buffer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ ur_result_t ur_exp_command_buffer_handle_t_::registerExecutionEventUnlocked(
167167
}
168168

169169
ur_exp_command_buffer_handle_t_::~ur_exp_command_buffer_handle_t_() {
170+
UR_CALL_NOCHECK(commandListManager.lock()->releaseSubmittedKernels());
171+
170172
if (currentExecution) {
171173
currentExecution->release();
172174
}

0 commit comments

Comments
 (0)