Skip to content

Commit

Permalink
change initialization vars to enum, and handle the failed case to avo…
Browse files Browse the repository at this point in the history
…id reinitializing logger.
  • Loading branch information
Sheil Kumar committed Jul 30, 2024
1 parent dbac3b9 commit 5c2d1bd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
27 changes: 13 additions & 14 deletions onnxruntime/core/platform/windows/logging/etw_sink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,43 +137,42 @@ void NTAPI EtwRegistrationManager::ORT_TL_EtwEnableCallback(
EtwRegistrationManager::~EtwRegistrationManager() {
std::lock_guard<OrtMutex> lock(callbacks_mutex_);
callbacks_.clear();
if (initialized_ || initializing_) {
if (initialization_status_ == InitializationStatus::Intialized ||
initialization_status_ == InitializationStatus::Initializing) {
std::lock_guard<OrtMutex> init_lock(init_mutex_);
assert(!initializing_);
if (initialized_) {
::TraceLoggingUnregister(etw_provider_handle);
initialized_ = false;
}
assert(initialization_status_ != InitializationStatus::Initializing);
if (initialization_status_ == InitializationStatus::Intialized) {
::TraceLoggingUnregister(etw_provider_handle);
initialization_status_ = InitializationStatus::NotInitialized;
}
}
}

EtwRegistrationManager::EtwRegistrationManager() {
}

void EtwRegistrationManager::LazyInitialize() try {
if (!initialized_ && !initializing_) {
if (initialization_status_ == InitializationStatus::NoitIntialized) {
std::lock_guard<OrtMutex> lock(init_mutex_);
if (!initialized_ && !initializing_) { // Double-check locking pattern
initializing_ = true;
if (initialization_status_ == InitializationStatus::NotInitialized) { // Double-check locking pattern
initialization_status_ == InitializationStatus::Initializing;
etw_status_ = ::TraceLoggingRegisterEx(etw_provider_handle, ORT_TL_EtwEnableCallback, nullptr);
if (FAILED(etw_status_)) {
ORT_THROW("ETW registration failed. Logging will be broken: " + std::to_string(etw_status_));
}
initialized_ = true;
initializing_ = false;
initialization_status_ == InitializationStatus::Initialized;
}
}
} catch (...)
{

Check warning on line 167 in onnxruntime/core/platform/windows/logging/etw_sink.cc

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 { should almost always be at the end of the previous line [whitespace/braces] [4] Raw Output: onnxruntime/core/platform/windows/logging/etw_sink.cc:167: { should almost always be at the end of the previous line [whitespace/braces] [4]
initialized_ = false;
initializing_ = false;
initialization_status_ == InitializationStatus::Failed;
throw;
}

void EtwRegistrationManager::InvokeCallbacks(LPCGUID SourceId, ULONG IsEnabled, UCHAR Level, ULONGLONG MatchAnyKeyword,
ULONGLONG MatchAllKeyword, PEVENT_FILTER_DESCRIPTOR FilterData,
PVOID CallbackContext) {
if (!initialized_) {
if (initialization_status_ != InitializationStatus::Initialized) {
// Drop messages until manager is fully initialized.
return;
}
Expand Down
5 changes: 3 additions & 2 deletions onnxruntime/core/platform/windows/logging/etw_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class EtwSink : public ISink {
};

class EtwRegistrationManager {
enum class InitializationStatus { NotInitialized, Initializing, Initialized, Failed};

public:
using EtwInternalCallback = std::function<void(LPCGUID SourceId, ULONG IsEnabled, UCHAR Level,
ULONGLONG MatchAnyKeyword, ULONGLONG MatchAllKeyword,
Expand Down Expand Up @@ -96,8 +98,7 @@ class EtwRegistrationManager {
OrtMutex callbacks_mutex_;
mutable OrtMutex provider_change_mutex_;
OrtMutex init_mutex_;
bool initialized_ = false;
bool initializing_ = false;
InitializationStatus initialization_status_ = InitializationStatus::NotInitialized;
bool is_enabled_;
UCHAR level_;
ULONGLONG keyword_;
Expand Down

0 comments on commit 5c2d1bd

Please sign in to comment.