aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/logging
diff options
context:
space:
mode:
authorlukyan <lukyan@yandex-team.com>2024-04-26 02:21:44 +0300
committerlukyan <lukyan@yandex-team.com>2024-04-26 02:41:13 +0300
commit5bbe44ff4e12b6d5496d56ecca97b0c4db340509 (patch)
tree511f2114250a8a3da539995a2da71782c3f82883 /library/cpp/yt/logging
parent7bde5f1f7732fb9e9103ac1f54fe1de99bdb6be5 (diff)
downloadydb-5bbe44ff4e12b6d5496d56ecca97b0c4db340509.tar.gz
YT-21566: Access thread local variables via noinline functions
970c33b44a7bd166b2716d86d3d2053dcaf05d7d
Diffstat (limited to 'library/cpp/yt/logging')
-rw-r--r--library/cpp/yt/logging/logger-inl.h12
-rw-r--r--library/cpp/yt/logging/logger.cpp71
-rw-r--r--library/cpp/yt/logging/logger.h4
3 files changed, 42 insertions, 45 deletions
diff --git a/library/cpp/yt/logging/logger-inl.h b/library/cpp/yt/logging/logger-inl.h
index 5e2b6795dcc..d076b3690eb 100644
--- a/library/cpp/yt/logging/logger-inl.h
+++ b/library/cpp/yt/logging/logger-inl.h
@@ -90,20 +90,8 @@ protected:
void DoReserve(size_t newLength) override;
private:
- struct TPerThreadCache
- {
- ~TPerThreadCache();
-
- TSharedMutableRef Chunk;
- size_t ChunkOffset = 0;
- };
-
TSharedMutableRef Buffer_;
- static YT_THREAD_LOCAL(TPerThreadCache*) Cache_;
- static YT_THREAD_LOCAL(bool) CacheDestroyed_;
- static TPerThreadCache* GetCache();
-
static constexpr size_t ChunkSize = 128_KB - 64;
};
diff --git a/library/cpp/yt/logging/logger.cpp b/library/cpp/yt/logging/logger.cpp
index c11457f836f..58add384296 100644
--- a/library/cpp/yt/logging/logger.cpp
+++ b/library/cpp/yt/logging/logger.cpp
@@ -34,15 +34,45 @@ TSharedRef TMessageStringBuilder::Flush()
return Buffer_.Slice(0, GetLength());
}
-void TMessageStringBuilder::DisablePerThreadCache()
+void TMessageStringBuilder::DoReset()
{
- Cache_ = nullptr;
- CacheDestroyed_ = true;
+ Buffer_.Reset();
}
-void TMessageStringBuilder::DoReset()
+struct TPerThreadCache;
+
+YT_DEFINE_THREAD_LOCAL(TPerThreadCache*, Cache);
+YT_DEFINE_THREAD_LOCAL(bool, CacheDestroyed);
+
+struct TPerThreadCache
{
- Buffer_.Reset();
+ TSharedMutableRef Chunk;
+ size_t ChunkOffset = 0;
+
+ ~TPerThreadCache()
+ {
+ TMessageStringBuilder::DisablePerThreadCache();
+ }
+
+ static YT_PREVENT_TLS_CACHING TPerThreadCache* GetCache()
+ {
+ auto& cache = Cache();
+ if (Y_LIKELY(cache)) {
+ return cache;
+ }
+ if (CacheDestroyed()) {
+ return nullptr;
+ }
+ static thread_local TPerThreadCache CacheData;
+ cache = &CacheData;
+ return cache;
+ }
+};
+
+void TMessageStringBuilder::DisablePerThreadCache()
+{
+ Cache() = nullptr;
+ CacheDestroyed() = true;
}
void TMessageStringBuilder::DoReserve(size_t newCapacity)
@@ -53,7 +83,7 @@ void TMessageStringBuilder::DoReserve(size_t newCapacity)
auto newChunkSize = std::max(ChunkSize, newCapacity);
// Hold the old buffer until the data is copied.
auto oldBuffer = std::move(Buffer_);
- auto* cache = GetCache();
+ auto* cache = TPerThreadCache::GetCache();
if (Y_LIKELY(cache)) {
auto oldCapacity = End_ - Begin_;
auto deltaCapacity = newCapacity - oldCapacity;
@@ -85,27 +115,6 @@ void TMessageStringBuilder::DoReserve(size_t newCapacity)
End_ = Begin_ + newCapacity;
}
-TMessageStringBuilder::TPerThreadCache* TMessageStringBuilder::GetCache()
-{
- if (Y_LIKELY(Cache_)) {
- return Cache_;
- }
- if (CacheDestroyed_) {
- return nullptr;
- }
- static YT_THREAD_LOCAL(TPerThreadCache) Cache;
- Cache_ = &GetTlsRef(Cache);
- return Cache_;
-}
-
-TMessageStringBuilder::TPerThreadCache::~TPerThreadCache()
-{
- TMessageStringBuilder::DisablePerThreadCache();
-}
-
-YT_THREAD_LOCAL(TMessageStringBuilder::TPerThreadCache*) TMessageStringBuilder::Cache_;
-YT_THREAD_LOCAL(bool) TMessageStringBuilder::CacheDestroyed_;
-
} // namespace NDetail
////////////////////////////////////////////////////////////////////////////////
@@ -126,16 +135,16 @@ Y_WEAK ILogManager* GetDefaultLogManager()
////////////////////////////////////////////////////////////////////////////////
-YT_THREAD_LOCAL(ELogLevel) ThreadMinLogLevel = ELogLevel::Minimum;
+YT_DEFINE_THREAD_LOCAL(ELogLevel, ThreadMinLogLevel, ELogLevel::Minimum);
void SetThreadMinLogLevel(ELogLevel minLogLevel)
{
- ThreadMinLogLevel = minLogLevel;
+ ThreadMinLogLevel() = minLogLevel;
}
ELogLevel GetThreadMinLogLevel()
{
- return ThreadMinLogLevel;
+ return ThreadMinLogLevel();
}
////////////////////////////////////////////////////////////////////////////////
@@ -173,7 +182,7 @@ bool TLogger::IsLevelEnabledHeavy(ELogLevel level) const
return
level >= Category_->MinPlainTextLevel &&
- level >= ThreadMinLogLevel;
+ level >= ThreadMinLogLevel();
}
bool TLogger::GetAbortOnAlert() const
diff --git a/library/cpp/yt/logging/logger.h b/library/cpp/yt/logging/logger.h
index 082afcddd8c..0913b9ea08a 100644
--- a/library/cpp/yt/logging/logger.h
+++ b/library/cpp/yt/logging/logger.h
@@ -324,8 +324,8 @@ void LogStructuredEvent(
break; \
} \
\
- static YT_THREAD_LOCAL(i64) localByteCounter__; \
- static YT_THREAD_LOCAL(ui8) localMessageCounter__; \
+ static thread_local i64 localByteCounter__; \
+ static thread_local ui8 localMessageCounter__; \
\
localByteCounter__ += message__.MessageRef.Size(); \
if (Y_UNLIKELY(++localMessageCounter__ == 0)) { \