diff options
| author | Stefan Boberg <[email protected]> | 2024-12-02 12:21:53 +0100 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2024-12-02 12:21:53 +0100 |
| commit | e6f44577f469e891ed8dab1492a4c53224e0b765 (patch) | |
| tree | 47334606ce62fb6bf1975cdc09276ced335599f4 /src | |
| parent | 5.5.15-pre0 (diff) | |
| download | zen-e6f44577f469e891ed8dab1492a4c53224e0b765.tar.xz zen-e6f44577f469e891ed8dab1492a4c53224e0b765.zip | |
added support for dynamic LLM tags (#245)
* added FLLMTag which can be used to register memory tags outside of core
* changed `UE_MEMSCOPE` -> `ZEN_MEMSCOPE` for consistency
* instrumented some subsystems with dynamic tags
Diffstat (limited to 'src')
27 files changed, 449 insertions, 34 deletions
diff --git a/src/zencore/include/zencore/memory/llm.h b/src/zencore/include/zencore/memory/llm.h index 4f1c9de77..ea7f68cc6 100644 --- a/src/zencore/include/zencore/memory/llm.h +++ b/src/zencore/include/zencore/memory/llm.h @@ -28,4 +28,20 @@ enum class ELLMTag : uint8_t GenericTagCount }; +struct FLLMTag +{ +public: + FLLMTag(const char* TagName); + FLLMTag(const char* TagName, const FLLMTag& ParentTag); + + inline int32_t GetTag() const { return m_Tag; } + inline int32_t GetParentTag() const { return m_ParentTag; } + +private: + int32_t m_Tag = -1; + int32_t m_ParentTag = -1; + + void AssignAndAnnounceNewTag(const char* TagName); +}; + } // namespace zen diff --git a/src/zencore/include/zencore/memory/tagtrace.h b/src/zencore/include/zencore/memory/tagtrace.h index f51b21466..8b5fc0e67 100644 --- a/src/zencore/include/zencore/memory/tagtrace.h +++ b/src/zencore/include/zencore/memory/tagtrace.h @@ -9,6 +9,7 @@ namespace zen { enum class ELLMTag : uint8_t; +struct FLLMTag; int32_t MemoryTrace_AnnounceCustomTag(int32_t Tag, int32_t ParentTag, const char* Display); int32_t MemoryTrace_GetActiveTag(); @@ -42,6 +43,7 @@ class FMemScope public: FMemScope(); // Used with SetTagAndActivate FMemScope(int32_t InTag, bool bShouldActivate = true); + FMemScope(FLLMTag InTag, bool bShouldActivate = true); FMemScope(ELLMTag InTag, bool bShouldActivate = true); ~FMemScope(); @@ -75,19 +77,18 @@ private: }; //////////////////////////////////////////////////////////////////////////////// -# define UE_MEMSCOPE(InTag) FMemScope PREPROCESSOR_JOIN(MemScope, __LINE__)(InTag); -# define UE_MEMSCOPE_PTR(InPtr) FMemScopePtr PREPROCESSOR_JOIN(MemPtrScope, __LINE__)((uint64)InPtr); -# define UE_MEMSCOPE_DEFAULT(InTag) FDefaultMemScope PREPROCESSOR_JOIN(MemScope, __LINE__)(InTag); -# define UE_MEMSCOPE_UNINITIALIZED(Line) FMemScope PREPROCESSOR_JOIN(MemScope, Line); +# define ZEN_MEMSCOPE(InTag) FMemScope PREPROCESSOR_JOIN(MemScope, __LINE__)(InTag); +# define ZEN_MEMSCOPE_PTR(InPtr) FMemScopePtr PREPROCESSOR_JOIN(MemPtrScope, __LINE__)((uint64)InPtr); +# define ZEN_MEMSCOPE_DEFAULT(InTag) FDefaultMemScope PREPROCESSOR_JOIN(MemScope, __LINE__)(InTag); +# define ZEN_MEMSCOPE_UNINITIALIZED(Line) FMemScope PREPROCESSOR_JOIN(MemScope, Line); #else // UE_MEMORY_TAGS_TRACE_ENABLED //////////////////////////////////////////////////////////////////////////////// -# define UE_MEMSCOPE(...) -# define UE_MEMSCOPE_PTR(...) -# define UE_MEMSCOPE_DEFAULT(...) -# define UE_MEMSCOPE_UNINITIALIZED(...) -# define UE_MEMSCOPE_ACTIVATE(...) +# define ZEN_MEMSCOPE(...) +# define ZEN_MEMSCOPE_PTR(...) +# define ZEN_MEMSCOPE_DEFAULT(...) +# define ZEN_MEMSCOPE_UNINITIALIZED(...) #endif // UE_MEMORY_TAGS_TRACE_ENABLED } diff --git a/src/zencore/iobuffer.cpp b/src/zencore/iobuffer.cpp index 02f85a9d9..3d9d6706a 100644 --- a/src/zencore/iobuffer.cpp +++ b/src/zencore/iobuffer.cpp @@ -39,7 +39,7 @@ namespace zen { void IoBufferCore::AllocateBuffer(size_t InSize, size_t Alignment) const { - UE_MEMSCOPE(ELLMTag::IoBufferMemory); + ZEN_MEMSCOPE(ELLMTag::IoBufferMemory); void* Ptr = Memory::Alloc(InSize, Alignment); @@ -64,7 +64,7 @@ IoBufferCore::FreeBuffer() void* IoBufferCore::operator new(size_t Size) { - UE_MEMSCOPE(ELLMTag::IoBufferCore); + ZEN_MEMSCOPE(ELLMTag::IoBufferCore); return Memory::Malloc(Size); } diff --git a/src/zencore/logging.cpp b/src/zencore/logging.cpp index 7bd500b3b..5885587ad 100644 --- a/src/zencore/logging.cpp +++ b/src/zencore/logging.cpp @@ -68,7 +68,7 @@ static_assert(offsetof(spdlog::source_loc, funcname) == offsetof(SourceLocation, void EmitLogMessage(LoggerRef& Logger, int LogLevel, const std::string_view Message) { - UE_MEMSCOPE(ELLMTag::Logging); + ZEN_MEMSCOPE(ELLMTag::Logging); const spdlog::level::level_enum InLevel = (spdlog::level::level_enum)LogLevel; Logger.SpdLogger->log(InLevel, Message); if (IsErrorLevel(LogLevel)) @@ -83,7 +83,7 @@ EmitLogMessage(LoggerRef& Logger, int LogLevel, const std::string_view Message) void EmitLogMessage(LoggerRef& Logger, int LogLevel, std::string_view Format, fmt::format_args Args) { - UE_MEMSCOPE(ELLMTag::Logging); + ZEN_MEMSCOPE(ELLMTag::Logging); zen::logging::LoggingContext LogCtx; fmt::vformat_to(fmt::appender(LogCtx.MessageBuffer), Format, Args); zen::logging::EmitLogMessage(Logger, LogLevel, LogCtx.Message()); @@ -92,7 +92,7 @@ EmitLogMessage(LoggerRef& Logger, int LogLevel, std::string_view Format, fmt::fo void EmitLogMessage(LoggerRef& Logger, const SourceLocation& InLocation, int LogLevel, const std::string_view Message) { - UE_MEMSCOPE(ELLMTag::Logging); + ZEN_MEMSCOPE(ELLMTag::Logging); const spdlog::source_loc& Location = *reinterpret_cast<const spdlog::source_loc*>(&InLocation); const spdlog::level::level_enum InLevel = (spdlog::level::level_enum)LogLevel; Logger.SpdLogger->log(Location, InLevel, Message); @@ -108,7 +108,7 @@ EmitLogMessage(LoggerRef& Logger, const SourceLocation& InLocation, int LogLevel void EmitLogMessage(LoggerRef& Logger, const SourceLocation& InLocation, int LogLevel, std::string_view Format, fmt::format_args Args) { - UE_MEMSCOPE(ELLMTag::Logging); + ZEN_MEMSCOPE(ELLMTag::Logging); zen::logging::LoggingContext LogCtx; fmt::vformat_to(fmt::appender(LogCtx.MessageBuffer), Format, Args); zen::logging::EmitLogMessage(Logger, InLocation, LogLevel, LogCtx.Message()); @@ -117,7 +117,7 @@ EmitLogMessage(LoggerRef& Logger, const SourceLocation& InLocation, int LogLevel void EmitConsoleLogMessage(int LogLevel, const std::string_view Message) { - UE_MEMSCOPE(ELLMTag::Logging); + ZEN_MEMSCOPE(ELLMTag::Logging); const spdlog::level::level_enum InLevel = (spdlog::level::level_enum)LogLevel; ConsoleLog().SpdLogger->log(InLevel, Message); } @@ -125,7 +125,7 @@ EmitConsoleLogMessage(int LogLevel, const std::string_view Message) void EmitConsoleLogMessage(int LogLevel, std::string_view Format, fmt::format_args Args) { - UE_MEMSCOPE(ELLMTag::Logging); + ZEN_MEMSCOPE(ELLMTag::Logging); zen::logging::LoggingContext LogCtx; fmt::vformat_to(fmt::appender(LogCtx.MessageBuffer), Format, Args); zen::logging::EmitConsoleLogMessage(LogLevel, LogCtx.Message()); @@ -200,7 +200,7 @@ std::string LogLevels[level::LogLevelCount]; void ConfigureLogLevels(level::LogLevel Level, std::string_view Loggers) { - UE_MEMSCOPE(ELLMTag::Logging); + ZEN_MEMSCOPE(ELLMTag::Logging); RwLock::ExclusiveLockScope _(LogLevelsLock); LogLevels[Level] = Loggers; @@ -209,7 +209,7 @@ ConfigureLogLevels(level::LogLevel Level, std::string_view Loggers) void RefreshLogLevels(level::LogLevel* DefaultLevel) { - UE_MEMSCOPE(ELLMTag::Logging); + ZEN_MEMSCOPE(ELLMTag::Logging); spdlog::details::registry::log_levels Levels; @@ -287,7 +287,7 @@ Default() void SetDefault(std::string_view NewDefaultLoggerId) { - UE_MEMSCOPE(ELLMTag::Logging); + ZEN_MEMSCOPE(ELLMTag::Logging); auto NewDefaultLogger = spdlog::get(std::string(NewDefaultLoggerId)); ZEN_ASSERT(NewDefaultLogger); @@ -307,7 +307,7 @@ ErrorLog() void SetErrorLog(std::string_view NewErrorLoggerId) { - UE_MEMSCOPE(ELLMTag::Logging); + ZEN_MEMSCOPE(ELLMTag::Logging); if (NewErrorLoggerId.empty()) { @@ -326,7 +326,7 @@ SetErrorLog(std::string_view NewErrorLoggerId) LoggerRef Get(std::string_view Name) { - UE_MEMSCOPE(ELLMTag::Logging); + ZEN_MEMSCOPE(ELLMTag::Logging); std::shared_ptr<spdlog::logger> Logger = spdlog::get(std::string(Name)); @@ -357,7 +357,7 @@ SuppressConsoleLog() LoggerRef ConsoleLog() { - UE_MEMSCOPE(ELLMTag::Logging); + ZEN_MEMSCOPE(ELLMTag::Logging); std::call_once(ConsoleInitFlag, [&] { if (!ConLogger) @@ -375,7 +375,7 @@ ConsoleLog() void InitializeLogging() { - UE_MEMSCOPE(ELLMTag::Logging); + ZEN_MEMSCOPE(ELLMTag::Logging); TheDefaultLogger = *spdlog::default_logger_raw(); } diff --git a/src/zencore/memory/llm.cpp b/src/zencore/memory/llm.cpp new file mode 100644 index 000000000..fe4853d49 --- /dev/null +++ b/src/zencore/memory/llm.cpp @@ -0,0 +1,77 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#include <zencore/memory/llm.h> + +#include <zencore/string.h> +#include <zencore/thread.h> + +#include <atomic> + +namespace zen { + +static std::atomic<int32_t> CustomTagCounter = 257; // NOTE: hard-coded TRACE_TAG = 257 + +static const int32_t TagNamesBaseIndex = 256; +static const int32_t TrackedTagNameCount = 256; +static const char* TagNames[TrackedTagNameCount]; +static uint32_t TagNameHashes[TrackedTagNameCount]; + +static RwLock TableLock; + +FLLMTag::FLLMTag(const char* TagName) +{ + // NOTE: should add verification to prevent multiple definitions of same name? + + AssignAndAnnounceNewTag(TagName); +} + +FLLMTag::FLLMTag(const char* TagName, const FLLMTag& ParentTag) +{ + // NOTE: should add verification to prevent multiple definitions of same name? + + m_ParentTag = ParentTag.GetTag(); + + AssignAndAnnounceNewTag(TagName); +} + +void +FLLMTag::AssignAndAnnounceNewTag(const char* TagName) +{ + const uint32_t TagNameHash = HashStringDjb2(TagName); + + { + RwLock::ExclusiveLockScope _(TableLock); + + const int32_t CurrentMaxTagIndex = CustomTagCounter - TagNamesBaseIndex; + + for (int TagIndex = 0; TagIndex <= CurrentMaxTagIndex; ++TagIndex) + { + if (TagNameHashes[TagIndex] == TagNameHash) + { + m_Tag = TagIndex + TagNamesBaseIndex; + // could verify the string matches here to catch hash collisions + + // return early, no need to announce the tag as it is already known + return; + } + } + + m_Tag = ++CustomTagCounter; + + const int TagIndex = m_Tag - TagNamesBaseIndex; + + if (TagIndex < TrackedTagNameCount) + { + TagNameHashes[TagIndex] = TagNameHash; + TagNames[TagIndex] = TagName; + } + else + { + // should really let user know there's an overflow + } + } + + MemoryTrace_AnnounceCustomTag(m_Tag, m_ParentTag, TagName); +} + +} // namespace zen diff --git a/src/zencore/memtrack/memorytrace.cpp b/src/zencore/memtrack/memorytrace.cpp index b147aee91..7089c356a 100644 --- a/src/zencore/memtrack/memorytrace.cpp +++ b/src/zencore/memtrack/memorytrace.cpp @@ -755,7 +755,7 @@ FTraceMalloc::Malloc(SIZE_T Count, uint32_t Alignment) { #if UE_MEMORY_TRACE_ENABLED // UE_TRACE_METADATA_CLEAR_SCOPE(); - UE_MEMSCOPE(TRACE_TAG); + ZEN_MEMSCOPE(TRACE_TAG); void* NewPtr; { @@ -783,7 +783,7 @@ FTraceMalloc::Realloc(void* Original, SIZE_T Count, uint32_t Alignment) { #if UE_MEMORY_TRACE_ENABLED // UE_TRACE_METADATA_CLEAR_SCOPE(); - UE_MEMSCOPE(TRACE_TAG); + ZEN_MEMSCOPE(TRACE_TAG); UE_TRACE_LOG(Memory, ReallocFree, MemAllocChannel) << ReallocFree.Address(uint64_t(Original)) << ReallocFree.RootHeap(uint8(EMemoryTraceRootHeap::SystemMemory)); diff --git a/src/zencore/memtrack/moduletrace.cpp b/src/zencore/memtrack/moduletrace.cpp index 51280ff3a..cf37c5932 100644 --- a/src/zencore/memtrack/moduletrace.cpp +++ b/src/zencore/memtrack/moduletrace.cpp @@ -228,7 +228,7 @@ FModuleTrace::OnDllLoaded(const UNICODE_STRING& Name, UPTRINT Base) # if UE_MEMORY_TRACE_ENABLED { - UE_MEMSCOPE(ELLMTag::ProgramSize); + ZEN_MEMSCOPE(ELLMTag::ProgramSize); MemoryTrace_Alloc(Base, OptionalHeader.SizeOfImage, 4 * 1024, EMemoryTraceRootHeap::SystemMemory); MemoryTrace_MarkAllocAsHeap(Base, ProgramHeapId); MemoryTrace_Alloc(Base, OptionalHeader.SizeOfImage, 4 * 1024, EMemoryTraceRootHeap::SystemMemory); diff --git a/src/zencore/memtrack/tagtrace.cpp b/src/zencore/memtrack/tagtrace.cpp index 15ba78ae4..797da0fab 100644 --- a/src/zencore/memtrack/tagtrace.cpp +++ b/src/zencore/memtrack/tagtrace.cpp @@ -51,7 +51,15 @@ FMemScope::FMemScope(ELLMTag InTag, bool bShouldActivate /*= true*/) { if (UE_TRACE_CHANNELEXPR_IS_ENABLED(MemAllocChannel) & bShouldActivate) { - ActivateScope(static_cast<int32>(InTag)); + ActivateScope(static_cast<int32_t>(InTag)); + } +} + +FMemScope::FMemScope(FLLMTag InTag, bool bShouldActivate /*= true*/) +{ + if (UE_TRACE_CHANNELEXPR_IS_ENABLED(MemAllocChannel) & bShouldActivate) + { + ActivateScope(static_cast<int32_t>(InTag.GetTag())); } } @@ -212,7 +220,6 @@ int32_t MemoryTrace_AnnounceCustomTag(int32_t Tag, int32_t ParentTag, const char* Display) { #if UE_MEMORY_TAGS_TRACE_ENABLED && UE_TRACE_ENABLED - // todo: How do we check if tag trace is active? if (GTagTrace) { return GTagTrace->AnnounceCustomTag(Tag, ParentTag, Display); diff --git a/src/zencore/stats.cpp b/src/zencore/stats.cpp index 6be16688b..8a424c5ad 100644 --- a/src/zencore/stats.cpp +++ b/src/zencore/stats.cpp @@ -226,7 +226,7 @@ thread_local xoshiro256 ThreadLocalRng; UniformSample::UniformSample(uint32_t ReservoirSize) { - UE_MEMSCOPE(ELLMTag::Metrics); + ZEN_MEMSCOPE(ELLMTag::Metrics); m_Values = std::vector<std::atomic<int64_t>>(ReservoirSize); } @@ -277,7 +277,7 @@ UniformSample::Update(int64_t Value) SampleSnapshot UniformSample::Snapshot() const { - UE_MEMSCOPE(ELLMTag::Metrics); + ZEN_MEMSCOPE(ELLMTag::Metrics); uint64_t ValuesSize = Size(); std::vector<double> Values(ValuesSize); diff --git a/src/zenhttp/servers/httpsys.cpp b/src/zenhttp/servers/httpsys.cpp index ac17d3ba0..9e8044a58 100644 --- a/src/zenhttp/servers/httpsys.cpp +++ b/src/zenhttp/servers/httpsys.cpp @@ -9,6 +9,7 @@ #include <zencore/filesystem.h> #include <zencore/fmtutils.h> #include <zencore/logging.h> +#include <zencore/memory/llm.h> #include <zencore/scopeguard.h> #include <zencore/string.h> #include <zencore/timer.h> @@ -25,6 +26,14 @@ namespace zen { +const FLLMTag& +GetHttpsysTag() +{ + static FLLMTag HttpsysTag("httpsys"); + + return HttpsysTag; +} + /** * @brief Windows implementation of HTTP server based on http.sys * @@ -901,6 +910,8 @@ HttpSysServer::HttpSysServer(const HttpSysConfig& InConfig) , m_IsAsyncResponseEnabled(InConfig.IsAsyncResponseEnabled) , m_InitialConfig(InConfig) { + ZEN_MEMSCOPE(GetHttpsysTag()); + // Initialize thread pool int MinThreadCount; @@ -976,6 +987,8 @@ HttpSysServer::Close() int HttpSysServer::InitializeServer(int BasePort) { + ZEN_MEMSCOPE(GetHttpsysTag()); + using namespace std::literals; WideStringBuilder<64> WildcardUrlPath; @@ -1220,6 +1233,8 @@ HttpSysServer::Cleanup() WorkerThreadPool& HttpSysServer::WorkPool() { + ZEN_MEMSCOPE(GetHttpsysTag()); + if (!m_AsyncWorkPool) { RwLock::ExclusiveLockScope _(m_AsyncWorkPoolInitLock); @@ -1304,6 +1319,8 @@ HttpSysServer::IssueNewRequestMaybe() return; } + ZEN_MEMSCOPE(GetHttpsysTag()); + std::unique_ptr<HttpSysTransaction> Request = std::make_unique<HttpSysTransaction>(*this); std::error_code ErrorCode; @@ -1327,6 +1344,8 @@ HttpSysServer::IssueNewRequestMaybe() void HttpSysServer::RegisterService(const char* UrlPath, HttpService& Service) { + ZEN_MEMSCOPE(GetHttpsysTag()); + if (UrlPath[0] == '/') { ++UrlPath; diff --git a/src/zenserver/cache/httpstructuredcache.cpp b/src/zenserver/cache/httpstructuredcache.cpp index f49f6a645..a6606c7ad 100644 --- a/src/zenserver/cache/httpstructuredcache.cpp +++ b/src/zenserver/cache/httpstructuredcache.cpp @@ -10,6 +10,7 @@ #include <zencore/enumflags.h> #include <zencore/fmtutils.h> #include <zencore/logging.h> +#include <zencore/memory/llm.h> #include <zencore/scopeguard.h> #include <zencore/stream.h> #include <zencore/timer.h> @@ -42,6 +43,14 @@ namespace zen { +const FLLMTag& +GetCacheHttpTag() +{ + static FLLMTag CacheHttpTag("http", FLLMTag("cache")); + + return CacheHttpTag; +} + using namespace std::literals; ////////////////////////////////////////////////////////////////////////// @@ -365,6 +374,8 @@ HttpStructuredCacheService::HandleRequest(HttpServerRequest& Request) { ZEN_TRACE_CPU("z$::Http::HandleRequest"); + ZEN_MEMSCOPE(GetCacheHttpTag()); + metrics::OperationTiming::Scope $(m_HttpRequests); const std::string_view Key = Request.RelativeUri(); @@ -1742,6 +1753,8 @@ HttpStructuredCacheService::HandleRpcRequest(HttpServerRequest& Request, std::st void HttpStructuredCacheService::HandleStatsRequest(HttpServerRequest& Request) { + ZEN_MEMSCOPE(GetCacheHttpTag()); + CbObjectWriter Cbo; EmitSnapshot("requests", m_HttpRequests, Cbo); diff --git a/src/zenserver/diag/diagsvcs.cpp b/src/zenserver/diag/diagsvcs.cpp index f0aec98ab..8abf6e8a3 100644 --- a/src/zenserver/diag/diagsvcs.cpp +++ b/src/zenserver/diag/diagsvcs.cpp @@ -7,6 +7,7 @@ #include <zencore/config.h> #include <zencore/filesystem.h> #include <zencore/logging.h> +#include <zencore/memory/llm.h> #include <zencore/string.h> #include <fstream> #include <sstream> @@ -17,6 +18,14 @@ ZEN_THIRD_PARTY_INCLUDES_END namespace zen { +const FLLMTag& +GetHealthTag() +{ + static FLLMTag CacheHttpTag("health"); + + return CacheHttpTag; +} + using namespace std::literals; static bool @@ -45,6 +54,8 @@ ReadLogFile(const std::string& Path, StringBuilderBase& Out) HttpHealthService::HttpHealthService() { + ZEN_MEMSCOPE(GetHealthTag()); + m_Router.RegisterRoute( "", [](HttpRouterRequest& RoutedReq) { @@ -115,6 +126,7 @@ HttpHealthService::HttpHealthService() void HttpHealthService::SetHealthInfo(HealthServiceInfo&& Info) { + ZEN_MEMSCOPE(GetHealthTag()); RwLock::ExclusiveLockScope _(m_InfoLock); m_HealthInfo = std::move(Info); } @@ -128,6 +140,7 @@ HttpHealthService::BaseUri() const void HttpHealthService::HandleRequest(HttpServerRequest& Request) { + ZEN_MEMSCOPE(GetHealthTag()); if (!m_Router.HandleRequest(Request)) { Request.WriteResponse(HttpResponseCode::OK, HttpContentType::kText, u8"OK!"sv); diff --git a/src/zenserver/diag/logging.cpp b/src/zenserver/diag/logging.cpp index 0d96cd8d6..f3d3377b0 100644 --- a/src/zenserver/diag/logging.cpp +++ b/src/zenserver/diag/logging.cpp @@ -21,7 +21,7 @@ namespace zen { void InitializeServerLogging(const ZenServerOptions& InOptions) { - UE_MEMSCOPE(ELLMTag::Logging); + ZEN_MEMSCOPE(ELLMTag::Logging); const LoggingOptions LogOptions = {.IsDebug = InOptions.IsDebug, .IsVerbose = false, @@ -76,13 +76,16 @@ InitializeServerLogging(const ZenServerOptions& InOptions) const zen::Oid ServerSessionId = zen::GetSessionId(); - spdlog::apply_all([&](auto Logger) { Logger->info("server session id: {}", ServerSessionId); }); + spdlog::apply_all([&](auto Logger) { + ZEN_MEMSCOPE(ELLMTag::Logging); + Logger->info("server session id: {}", ServerSessionId); + }); } void ShutdownServerLogging() { - UE_MEMSCOPE(ELLMTag::Logging); + ZEN_MEMSCOPE(ELLMTag::Logging); zen::ShutdownLogging(); } diff --git a/src/zenserver/main.cpp b/src/zenserver/main.cpp index 4444241cc..d5419d342 100644 --- a/src/zenserver/main.cpp +++ b/src/zenserver/main.cpp @@ -18,6 +18,7 @@ #include <zenhttp/httpserver.h> #include <zencore/memory/fmalloc.h> +#include <zencore/memory/llm.h> #include <zencore/memory/memory.h> #include <zencore/memory/memorytrace.h> #include <zencore/memory/newdelete.h> @@ -58,6 +59,14 @@ SignalCallbackHandler(int SigNum) namespace zen { +static const FLLMTag& +GetZenserverTag() +{ + static FLLMTag _("zenserver"); + + return _; +} + using namespace std::literals; //////////////////////////////////////////////////////////////////////////////// @@ -440,6 +449,8 @@ main(int argc, char* argv[]) } #endif + ZEN_MEMSCOPE(GetZenserverTag()); + #if ZEN_PLATFORM_WINDOWS if (ServerOptions.InstallService) { diff --git a/src/zenserver/projectstore/httpprojectstore.cpp b/src/zenserver/projectstore/httpprojectstore.cpp index 4babcd224..c06ad8918 100644 --- a/src/zenserver/projectstore/httpprojectstore.cpp +++ b/src/zenserver/projectstore/httpprojectstore.cpp @@ -11,6 +11,7 @@ #include <zencore/filesystem.h> #include <zencore/fmtutils.h> #include <zencore/logging.h> +#include <zencore/memory/llm.h> #include <zencore/stream.h> #include <zencore/trace.h> #include <zenstore/zenstore.h> @@ -18,6 +19,14 @@ namespace zen { +const FLLMTag& +GetProjectHttpTag() +{ + static FLLMTag CacheHttpTag("http", FLLMTag("project")); + + return CacheHttpTag; +} + void CSVHeader(bool Details, bool AttachmentDetails, StringBuilderBase& CSVWriter) { @@ -372,6 +381,8 @@ HttpProjectService::HandleRequest(HttpServerRequest& Request) { m_ProjectStats.RequestCount++; + ZEN_MEMSCOPE(GetProjectHttpTag()); + metrics::OperationTiming::Scope $(m_HttpRequests); if (m_Router.HandleRequest(Request) == false) diff --git a/src/zenserver/zenserver.cpp b/src/zenserver/zenserver.cpp index f6d6556a0..3714dfaeb 100644 --- a/src/zenserver/zenserver.cpp +++ b/src/zenserver/zenserver.cpp @@ -54,8 +54,18 @@ ZEN_THIRD_PARTY_INCLUDES_END #include "config.h" #include "diag/logging.h" +#include <zencore/memory/llm.h> + namespace zen { +static const FLLMTag& +GetZenserverTag() +{ + static FLLMTag _("zenserver"); + + return _; +} + namespace utils { extern std::atomic_uint32_t SignalCounter[NSIG]; } @@ -117,6 +127,7 @@ ZenServer::OnReady() int ZenServer::Initialize(const ZenServerOptions& ServerOptions, ZenServerState::ZenServerEntry* ServerEntry) { + ZEN_MEMSCOPE(GetZenserverTag()); const std::string MutexName = fmt::format("zen_{}", ServerOptions.BasePort); if (NamedMutex::Exists(MutexName)) @@ -806,6 +817,7 @@ ZenServer::Cleanup() void ZenServer::EnsureIoRunner() { + ZEN_MEMSCOPE(GetZenserverTag()); if (!m_IoRunner.joinable()) { m_IoRunner = std::thread{[this] { @@ -818,6 +830,7 @@ ZenServer::EnsureIoRunner() void ZenServer::EnqueueProcessMonitorTimer() { + ZEN_MEMSCOPE(GetZenserverTag()); m_PidCheckTimer.expires_after(std::chrono::seconds(1)); m_PidCheckTimer.async_wait([this](const asio::error_code&) { CheckOwnerPid(); }); @@ -827,6 +840,7 @@ ZenServer::EnqueueProcessMonitorTimer() void ZenServer::EnqueueStateMarkerTimer() { + ZEN_MEMSCOPE(GetZenserverTag()); m_StateMakerTimer.expires_after(std::chrono::seconds(5)); m_StateMakerTimer.async_wait([this](const asio::error_code&) { CheckStateMarker(); }); EnsureIoRunner(); @@ -835,6 +849,7 @@ ZenServer::EnqueueStateMarkerTimer() void ZenServer::EnqueueSigIntTimer() { + ZEN_MEMSCOPE(GetZenserverTag()); m_SigIntTimer.expires_after(std::chrono::milliseconds(500)); m_SigIntTimer.async_wait([this](const asio::error_code&) { CheckSigInt(); }); EnsureIoRunner(); @@ -843,6 +858,7 @@ ZenServer::EnqueueSigIntTimer() void ZenServer::EnqueueStateExitFlagTimer() { + ZEN_MEMSCOPE(GetZenserverTag()); m_StateExitFlagTimer.expires_after(std::chrono::milliseconds(500)); m_StateExitFlagTimer.async_wait([this](const asio::error_code&) { CheckStateExitFlag(); }); EnsureIoRunner(); @@ -851,6 +867,7 @@ ZenServer::EnqueueStateExitFlagTimer() void ZenServer::EnqueueStatsReportingTimer() { + ZEN_MEMSCOPE(GetZenserverTag()); m_StatsReportingTimer.expires_after(std::chrono::milliseconds(500)); m_StatsReportingTimer.async_wait([this](const asio::error_code& Ec) { if (!Ec) @@ -865,6 +882,7 @@ ZenServer::EnqueueStatsReportingTimer() void ZenServer::CheckStateMarker() { + ZEN_MEMSCOPE(GetZenserverTag()); std::filesystem::path StateMarkerPath = m_DataRoot / "state_marker"; try { diff --git a/src/zenstore/blockstore.cpp b/src/zenstore/blockstore.cpp index 5f2f5cba0..9ad672060 100644 --- a/src/zenstore/blockstore.cpp +++ b/src/zenstore/blockstore.cpp @@ -26,10 +26,20 @@ ZEN_THIRD_PARTY_INCLUDES_END # include <random> #endif +#include <zencore/memory/llm.h> + ////////////////////////////////////////////////////////////////////////// namespace zen { +const FLLMTag& +GetBlocksTag() +{ + static FLLMTag _("blocks"); + + return _; +} + ////////////////////////////////////////////////////////////////////////// BlockStoreFile::BlockStoreFile(const std::filesystem::path& BlockPath) : m_Path(BlockPath) @@ -266,6 +276,7 @@ BlockStore::~BlockStore() void BlockStore::Initialize(const std::filesystem::path& BlocksBasePath, uint64_t MaxBlockSize, uint64_t MaxBlockCount) { + ZEN_MEMSCOPE(GetBlocksTag()); ZEN_TRACE_CPU("BlockStore::Initialize"); ZEN_ASSERT(MaxBlockSize > 0); @@ -332,6 +343,7 @@ BlockStore::Initialize(const std::filesystem::path& BlocksBasePath, uint64_t Max void BlockStore::SyncExistingBlocksOnDisk(const BlockIndexSet& KnownBlocks) { + ZEN_MEMSCOPE(GetBlocksTag()); ZEN_TRACE_CPU("BlockStore::SyncExistingBlocksOnDisk"); RwLock::ExclusiveLockScope InsertLock(m_InsertLock); @@ -378,6 +390,7 @@ BlockStore::SyncExistingBlocksOnDisk(const BlockIndexSet& KnownBlocks) BlockStore::BlockEntryCountMap BlockStore::GetBlocksToCompact(const BlockUsageMap& BlockUsage, uint32_t BlockUsageThresholdPercent) { + ZEN_MEMSCOPE(GetBlocksTag()); ZEN_TRACE_CPU("BlockStoreFile::GetBlocksToCompact"); BlockEntryCountMap Result; { @@ -507,6 +520,7 @@ BlockStore::GetFreeBlockIndex(uint32_t ProbeIndex, RwLock::ExclusiveLockScope&, void BlockStore::WriteChunk(const void* Data, uint64_t Size, uint32_t Alignment, const WriteChunkCallback& Callback) { + ZEN_MEMSCOPE(GetBlocksTag()); ZEN_TRACE_CPU("BlockStore::WriteChunk"); ZEN_ASSERT(Data != nullptr); @@ -565,6 +579,7 @@ BlockStore::WriteChunk(const void* Data, uint64_t Size, uint32_t Alignment, cons void BlockStore::WriteChunks(std::span<IoBuffer> Datas, uint32_t Alignment, const WriteChunksCallback& Callback) { + ZEN_MEMSCOPE(GetBlocksTag()); ZEN_TRACE_CPU("BlockStore::WriteChunks"); ZEN_ASSERT(!Datas.empty()); @@ -680,6 +695,7 @@ BlockStore::TryGetChunk(const BlockStoreLocation& Location) const void BlockStore::Flush(bool ForceNewBlock) { + ZEN_MEMSCOPE(GetBlocksTag()); ZEN_TRACE_CPU("BlockStore::Flush"); if (ForceNewBlock) @@ -710,6 +726,7 @@ BlockStore::IterateBlock(std::span<const BlockStoreLocation> ChunkLocations, const IterateChunksLargeSizeCallback& LargeSizeCallback, uint64_t LargeSizeLimit) { + ZEN_MEMSCOPE(GetBlocksTag()); ZEN_TRACE_CPU("BlockStore::IterateBlock"); if (InChunkIndexes.empty()) @@ -861,6 +878,7 @@ BlockStore::IterateBlock(std::span<const BlockStoreLocation> ChunkLocations, bool BlockStore::IterateChunks(const std::span<const BlockStoreLocation>& ChunkLocations, const IterateChunksCallback& Callback) { + ZEN_MEMSCOPE(GetBlocksTag()); ZEN_TRACE_CPU("BlockStore::IterateChunks"); Stopwatch Timer; @@ -914,6 +932,7 @@ BlockStore::CompactBlocks(const BlockStoreCompactState& CompactState, const ClaimDiskReserveCallback& DiskReserveCallback, std::string_view LogPrefix) { + ZEN_MEMSCOPE(GetBlocksTag()); ZEN_TRACE_CPU("BlockStore::CompactBlocks"); uint64_t DeletedSize = 0; diff --git a/src/zenstore/cache/cachedisklayer.cpp b/src/zenstore/cache/cachedisklayer.cpp index 3046ab87c..a4f9fe78b 100644 --- a/src/zenstore/cache/cachedisklayer.cpp +++ b/src/zenstore/cache/cachedisklayer.cpp @@ -22,8 +22,18 @@ ////////////////////////////////////////////////////////////////////////// +#include <zencore/memory/llm.h> + namespace zen { +const FLLMTag& +GetCacheDiskTag() +{ + static FLLMTag _("disk", FLLMTag("cache")); + + return _; +} + namespace { #pragma pack(push) @@ -3894,6 +3904,8 @@ ZenCacheDiskLayer::DiscoverBuckets() { WorkLatch.AddCount(1); Pool.ScheduleWork([this, &WorkLatch, &SyncLock, BucketPath]() { + ZEN_MEMSCOPE(GetCacheDiskTag()); + auto _ = MakeGuard([&]() { WorkLatch.CountDown(); }); const std::string BucketName = PathToUtf8(BucketPath.stem()); try @@ -3980,6 +3992,7 @@ ZenCacheDiskLayer::Drop() void ZenCacheDiskLayer::Flush() { + ZEN_MEMSCOPE(GetCacheDiskTag()); ZEN_TRACE_CPU("Z$::Flush"); std::vector<CacheBucket*> Buckets; @@ -4010,6 +4023,8 @@ ZenCacheDiskLayer::Flush() { WorkLatch.AddCount(1); Pool.ScheduleWork([&WorkLatch, Bucket]() { + ZEN_MEMSCOPE(GetCacheDiskTag()); + auto _ = MakeGuard([&]() { WorkLatch.CountDown(); }); try { diff --git a/src/zenstore/cache/cacherpc.cpp b/src/zenstore/cache/cacherpc.cpp index 2a7721fe2..54c2ff7d0 100644 --- a/src/zenstore/cache/cacherpc.cpp +++ b/src/zenstore/cache/cacherpc.cpp @@ -17,10 +17,28 @@ #include <zenutil/cache/cacherequests.h> #include <zenutil/packageformat.h> +#include <zencore/memory/llm.h> + ////////////////////////////////////////////////////////////////////////// namespace zen { +const FLLMTag& +GetCacheTag() +{ + static FLLMTag CacheTag("cache"); + + return CacheTag; +} + +const FLLMTag& +GetCacheRpcTag() +{ + static FLLMTag CacheRpcTag("rpc", GetCacheTag()); + + return CacheRpcTag; +} + using namespace std::literals; std::optional<std::string> @@ -165,6 +183,8 @@ CacheRpcHandler::HandleRpcRequest(const CacheRequestContext& Context, { ZEN_TRACE_CPU("Z$::HandleRpcRequest"); + ZEN_MEMSCOPE(GetCacheRpcTag()); + m_CacheStats.RpcRequests.fetch_add(1); CbPackage Package; diff --git a/src/zenstore/cache/structuredcachestore.cpp b/src/zenstore/cache/structuredcachestore.cpp index 512f1d7f2..c14ea73a8 100644 --- a/src/zenstore/cache/structuredcachestore.cpp +++ b/src/zenstore/cache/structuredcachestore.cpp @@ -42,8 +42,20 @@ ZEN_THIRD_PARTY_INCLUDES_END # include <unordered_map> #endif +#include <zencore/memory/llm.h> + +////////////////////////////////////////////////////////////////////////// + namespace zen { +const FLLMTag& +GetCacheStoreTag() +{ + static FLLMTag _("store", FLLMTag("cache")); + + return _; +} + bool IsKnownBadBucketName(std::string_view Bucket) { @@ -122,6 +134,7 @@ ZenCacheNamespace::ZenCacheNamespace(GcManager& Gc, JobQueue& JobQueue, const st , m_Configuration(Config) , m_DiskLayer(m_Gc, m_JobQueue, m_RootDir, m_Configuration.DiskLayerConfig) { + ZEN_MEMSCOPE(GetCacheStoreTag()); ZEN_INFO("initializing structured cache at '{}'", m_RootDir); CreateDirectories(m_RootDir); @@ -403,6 +416,8 @@ ZenCacheStore::ZenCacheStore(GcManager& Gc, , m_Configuration(Configuration) , m_ExitLogging(false) { + ZEN_MEMSCOPE(GetCacheStoreTag()); + SetLoggingConfig(m_Configuration.Logging); CreateDirectories(m_BasePath); @@ -459,6 +474,8 @@ ZenCacheStore::LogWorker() { SetCurrentThreadName("ZenCacheStore::LogWorker"); + ZEN_MEMSCOPE(GetCacheStoreTag()); + LoggerRef ZCacheLog(logging::Get("z$")); auto Log = [&ZCacheLog]() -> LoggerRef { return ZCacheLog; }; @@ -539,6 +556,7 @@ ZenCacheStore::LogWorker() ZenCacheStore::PutBatch::PutBatch(ZenCacheStore& CacheStore, std::string_view InNamespace, std::vector<bool>& OutResult) : m_CacheStore(CacheStore) { + ZEN_MEMSCOPE(GetCacheStoreTag()); if (m_Store = m_CacheStore.GetNamespace(InNamespace); m_Store) { m_NamespaceBatchHandle = m_Store->BeginPutBatch(OutResult); @@ -551,6 +569,7 @@ ZenCacheStore::PutBatch::~PutBatch() { if (m_Store) { + ZEN_MEMSCOPE(GetCacheStoreTag()); ZEN_ASSERT(m_NamespaceBatchHandle); m_Store->EndPutBatch(m_NamespaceBatchHandle); } @@ -565,6 +584,7 @@ ZenCacheStore::GetBatch::GetBatch(ZenCacheStore& CacheStore, std::string_view In : m_CacheStore(CacheStore) , Results(OutResult) { + ZEN_MEMSCOPE(GetCacheStoreTag()); if (m_Store = m_CacheStore.GetNamespace(InNamespace); m_Store) { m_NamespaceBatchHandle = m_Store->BeginGetBatch(OutResult); @@ -577,6 +597,7 @@ ZenCacheStore::GetBatch::~GetBatch() { if (m_Store) { + ZEN_MEMSCOPE(GetCacheStoreTag()); ZEN_ASSERT(m_NamespaceBatchHandle); m_Store->EndGetBatch(m_NamespaceBatchHandle); @@ -613,6 +634,7 @@ ZenCacheStore::Get(const CacheRequestContext& Context, return false; } + ZEN_MEMSCOPE(GetCacheStoreTag()); ZEN_TRACE_CPU("Z$::Get"); metrics::RequestStats::Scope OpScope(m_GetOps, 0); @@ -673,6 +695,8 @@ ZenCacheStore::Get(const CacheRequestContext& Context, m_RejectedReadCount++; return; } + + ZEN_MEMSCOPE(GetCacheStoreTag()); ZEN_TRACE_CPU("Z$::Get"); metrics::RequestStats::Scope OpScope(m_GetOps, 0); @@ -709,6 +733,7 @@ ZenCacheStore::Put(const CacheRequestContext& Context, return; } + ZEN_MEMSCOPE(GetCacheStoreTag()); ZEN_TRACE_CPU("Z$::Put"); metrics::RequestStats::Scope $(m_PutOps, Value.Value.GetSize()); @@ -776,6 +801,8 @@ ZenCacheStore::DropNamespace(std::string_view InNamespace) void ZenCacheStore::Flush() { + ZEN_MEMSCOPE(GetCacheStoreTag()); + ZEN_INFO("flushing cache store at '{}'", m_BasePath); IterateNamespaces([&](std::string_view, ZenCacheNamespace& Store) { Store.Flush(); }); } diff --git a/src/zenstore/cas.cpp b/src/zenstore/cas.cpp index 4c1836cf7..7beac31e1 100644 --- a/src/zenstore/cas.cpp +++ b/src/zenstore/cas.cpp @@ -33,8 +33,20 @@ ////////////////////////////////////////////////////////////////////////// +#include <zencore/memory/llm.h> + +////////////////////////////////////////////////////////////////////////// + namespace zen { +const FLLMTag& +GetCasTag() +{ + static FLLMTag _("cas"); + + return _; +} + /** * CAS store implementation * @@ -97,6 +109,8 @@ CasImpl::~CasImpl() void CasImpl::Initialize(const CidStoreConfiguration& InConfig) { + ZEN_MEMSCOPE(GetCasTag()); + ZEN_TRACE_CPU("CAS::Initialize"); m_Config = InConfig; @@ -226,6 +240,7 @@ CasImpl::UpdateManifest() CasStore::InsertResult CasImpl::InsertChunk(IoBuffer Chunk, const IoHash& ChunkHash, InsertMode Mode) { + ZEN_MEMSCOPE(GetCasTag()); ZEN_TRACE_CPU("CAS::InsertChunk"); const uint64_t ChunkSize = Chunk.Size(); @@ -294,6 +309,7 @@ GetFileCasResults(FileCasStrategy& Strategy, std::vector<CasStore::InsertResult> CasImpl::InsertChunks(std::span<IoBuffer> Data, std::span<IoHash> ChunkHashes, CasStore::InsertMode Mode) { + ZEN_MEMSCOPE(GetCasTag()); ZEN_TRACE_CPU("CAS::InsertChunks"); ZEN_ASSERT(Data.size() == ChunkHashes.size()); @@ -448,6 +464,8 @@ CasImpl::Flush() void CasImpl::ScrubStorage(ScrubContext& Ctx) { + ZEN_MEMSCOPE(GetCasTag()); + ZEN_TRACE_CPU("Cas::ScrubStorage"); if (m_LastScrubTime == Ctx.ScrubTimestamp()) @@ -477,6 +495,8 @@ CasImpl::TotalSize() const std::unique_ptr<CasStore> CreateCasStore(GcManager& Gc) { + ZEN_MEMSCOPE(GetCasTag()); + return std::make_unique<CasImpl>(Gc); } diff --git a/src/zenstore/compactcas.cpp b/src/zenstore/compactcas.cpp index 9982e7571..f85b19264 100644 --- a/src/zenstore/compactcas.cpp +++ b/src/zenstore/compactcas.cpp @@ -10,6 +10,7 @@ #include <zencore/fmtutils.h> #include <zencore/iobuffer.h> #include <zencore/logging.h> +#include <zencore/memory/llm.h> #include <zencore/scopeguard.h> #include <zencore/trace.h> #include <zencore/workthreadpool.h> @@ -36,6 +37,14 @@ ZEN_THIRD_PARTY_INCLUDES_END namespace zen { +const FLLMTag& +GetCasContainerTag() +{ + static FLLMTag _("container", FLLMTag("cas")); + + return _; +} + struct CasDiskIndexHeader { static constexpr uint32_t ExpectedMagic = 0x75696478; // 'uidx'; @@ -124,6 +133,8 @@ static const float IndexMaxLoadFactor = 0.7f; CasContainerStrategy::CasContainerStrategy(GcManager& Gc) : m_Log(logging::Get("containercas")), m_Gc(Gc) { + ZEN_MEMSCOPE(GetCasContainerTag()); + m_LocationMap.min_load_factor(IndexMinLoadFactor); m_LocationMap.max_load_factor(IndexMaxLoadFactor); @@ -144,6 +155,8 @@ CasContainerStrategy::Initialize(const std::filesystem::path& RootDirectory, uint32_t Alignment, bool IsNewStore) { + ZEN_MEMSCOPE(GetCasContainerTag()); + ZEN_ASSERT(IsPow2(Alignment)); ZEN_ASSERT(!m_IsInitialized); ZEN_ASSERT(MaxBlockSize > 0); @@ -171,6 +184,8 @@ CasContainerStrategy::InsertChunk(const void* ChunkData, size_t ChunkSize, const } } + ZEN_MEMSCOPE(GetCasContainerTag()); + // We can end up in a situation that InsertChunk writes the same chunk data in // different locations. // We release the insert lock once we have the correct WriteBlock ready and we know @@ -201,6 +216,8 @@ CasContainerStrategy::InsertChunk(const void* ChunkData, size_t ChunkSize, const CasStore::InsertResult CasContainerStrategy::InsertChunk(IoBuffer Chunk, const IoHash& ChunkHash) { + ZEN_MEMSCOPE(GetCasContainerTag()); + #if !ZEN_WITH_TESTS ZEN_ASSERT(Chunk.GetContentType() == ZenContentType::kCompressedBinary); #endif @@ -210,6 +227,8 @@ CasContainerStrategy::InsertChunk(IoBuffer Chunk, const IoHash& ChunkHash) std::vector<CasStore::InsertResult> CasContainerStrategy::InsertChunks(std::span<IoBuffer> Chunks, std::span<IoHash> ChunkHashes) { + ZEN_MEMSCOPE(GetCasContainerTag()); + ZEN_ASSERT(Chunks.size() == ChunkHashes.size()); std::vector<CasStore::InsertResult> Result(Chunks.size()); std::vector<size_t> NewChunkIndexes; @@ -308,6 +327,8 @@ CasContainerStrategy::IterateChunks(std::span<IoHash> ChunkHashes, WorkerThreadPool* OptionalWorkerPool, uint64_t LargeSizeLimit) { + ZEN_MEMSCOPE(GetCasContainerTag()); + const size_t ChunkCount = ChunkHashes.size(); if (ChunkCount < 3) { @@ -407,6 +428,8 @@ CasContainerStrategy::IterateChunks(std::span<IoHash> ChunkHashes, void CasContainerStrategy::Flush() { + ZEN_MEMSCOPE(GetCasContainerTag()); + ZEN_TRACE_CPU("CasContainer::Flush"); m_BlockStore.Flush(/*ForceNewBlock*/ false); m_CasLog.Flush(); @@ -416,6 +439,8 @@ CasContainerStrategy::Flush() void CasContainerStrategy::ScrubStorage(ScrubContext& Ctx) { + ZEN_MEMSCOPE(GetCasContainerTag()); + ZEN_TRACE_CPU("CasContainer::ScrubStorage"); if (Ctx.IsSkipCas()) @@ -563,6 +588,8 @@ public: virtual void CompactStore(GcCtx& Ctx, GcCompactStoreStats& Stats, const std::function<uint64_t()>& ClaimDiskReserveCallback) override { + ZEN_MEMSCOPE(GetCasContainerTag()); + ZEN_TRACE_CPU("CasContainer::CompactStore"); auto Log = [&Ctx]() { return Ctx.Logger; }; @@ -717,6 +744,7 @@ public: GcStats& Stats, const GetUnusedReferencesFunc& GetUnusedReferences) override { + ZEN_MEMSCOPE(GetCasContainerTag()); ZEN_TRACE_CPU("CasContainer::RemoveUnreferencedData"); auto Log = [&Ctx]() { return Ctx.Logger; }; @@ -799,6 +827,7 @@ CasContainerStrategy::GetGcName(GcCtx&) GcReferencePruner* CasContainerStrategy::CreateReferencePruner(GcCtx& Ctx, GcReferenceStoreStats&) { + ZEN_MEMSCOPE(GetCasContainerTag()); ZEN_TRACE_CPU("CasContainer::CreateReferencePruner"); auto Log = [&Ctx]() { return Ctx.Logger; }; @@ -842,6 +871,7 @@ CasContainerStrategy::CreateReferencePruner(GcCtx& Ctx, GcReferenceStoreStats&) void CasContainerStrategy::CompactIndex(RwLock::ExclusiveLockScope&) { + ZEN_MEMSCOPE(GetCasContainerTag()); ZEN_TRACE_CPU("CasContainer::CompactIndex"); size_t EntryCount = m_LocationMap.size(); @@ -868,6 +898,7 @@ CasContainerStrategy::StorageSize() const void CasContainerStrategy::MakeIndexSnapshot() { + ZEN_MEMSCOPE(GetCasContainerTag()); ZEN_TRACE_CPU("CasContainer::MakeIndexSnapshot"); uint64_t LogCount = m_CasLog.GetLogCount(); @@ -981,6 +1012,7 @@ CasContainerStrategy::MakeIndexSnapshot() uint64_t CasContainerStrategy::ReadIndexFile(const std::filesystem::path& IndexPath, uint32_t& OutVersion) { + ZEN_MEMSCOPE(GetCasContainerTag()); ZEN_TRACE_CPU("CasContainer::ReadIndexFile"); uint64_t EntryCount = 0; @@ -1050,6 +1082,7 @@ CasContainerStrategy::ReadIndexFile(const std::filesystem::path& IndexPath, uint uint64_t CasContainerStrategy::ReadLog(const std::filesystem::path& LogPath, uint64_t SkipEntryCount) { + ZEN_MEMSCOPE(GetCasContainerTag()); ZEN_TRACE_CPU("CasContainer::ReadLog"); if (!TCasLogFile<CasDiskIndexEntry>::IsValid(LogPath)) @@ -1106,6 +1139,7 @@ CasContainerStrategy::ReadLog(const std::filesystem::path& LogPath, uint64_t Ski void CasContainerStrategy::OpenContainer(bool IsNewStore) { + ZEN_MEMSCOPE(GetCasContainerTag()); ZEN_TRACE_CPU("CasContainer::OpenContainer"); // Add .running file and delete on clean on close to detect bad termination diff --git a/src/zenstore/filecas.cpp b/src/zenstore/filecas.cpp index f64427807..339e5de0a 100644 --- a/src/zenstore/filecas.cpp +++ b/src/zenstore/filecas.cpp @@ -43,8 +43,20 @@ ZEN_THIRD_PARTY_INCLUDES_START #endif ZEN_THIRD_PARTY_INCLUDES_END +#include <zencore/memory/llm.h> + +////////////////////////////////////////////////////////////////////////// + namespace zen { +const FLLMTag& +GetFileCasTag() +{ + static FLLMTag _("file", FLLMTag("cas")); + + return _; +} + namespace { template<typename T> void Reset(T& V) @@ -150,6 +162,7 @@ FileCasStrategy::~FileCasStrategy() void FileCasStrategy::Initialize(const std::filesystem::path& RootDirectory, bool IsNewStore) { + ZEN_MEMSCOPE(GetFileCasTag()); ZEN_TRACE_CPU("FileCas::Initialize"); using namespace filecas::impl; @@ -248,6 +261,8 @@ FileCasStrategy::Initialize(const std::filesystem::path& RootDirectory, bool IsN bool FileCasStrategy::UpdateIndex(const IoHash& ChunkHash, uint64_t ChunkSize) { + ZEN_MEMSCOPE(GetFileCasTag()); + uint64 OldChunkSize = 0; { RwLock::ExclusiveLockScope _(m_Lock); @@ -270,6 +285,8 @@ FileCasStrategy::UpdateIndex(const IoHash& ChunkHash, uint64_t ChunkSize) CasStore::InsertResult FileCasStrategy::InsertChunk(IoBuffer Chunk, const IoHash& ChunkHash, CasStore::InsertMode Mode) { + ZEN_MEMSCOPE(GetFileCasTag()); + ZEN_TRACE_CPU("FileCas::InsertChunk"); ZEN_ASSERT(m_IsInitialized); @@ -708,6 +725,8 @@ FileCasStrategy::IterateChunks(std::function<void(const IoHash& Hash, IoBuffer&& void FileCasStrategy::Flush() { + ZEN_MEMSCOPE(GetFileCasTag()); + ZEN_TRACE_CPU("FileCas::Flush"); m_CasLog.Flush(); @@ -717,6 +736,8 @@ FileCasStrategy::Flush() void FileCasStrategy::ScrubStorage(ScrubContext& Ctx) { + ZEN_MEMSCOPE(GetFileCasTag()); + ZEN_TRACE_CPU("FileCas::ScrubStorage"); if (Ctx.IsSkipCas()) @@ -896,6 +917,7 @@ FileCasStrategy::ValidateEntry(const FileCasIndexEntry& Entry, std::string& OutR void FileCasStrategy::MakeIndexSnapshot() { + ZEN_MEMSCOPE(GetFileCasTag()); ZEN_TRACE_CPU("FileCas::MakeIndexSnapshot"); using namespace filecas::impl; @@ -1208,6 +1230,7 @@ public: virtual void CompactStore(GcCtx& Ctx, GcCompactStoreStats& Stats, const std::function<uint64_t()>&) override { + ZEN_MEMSCOPE(GetFileCasTag()); ZEN_TRACE_CPU("FileCas::CompactStore"); auto Log = [&Ctx]() { return Ctx.Logger; }; @@ -1351,6 +1374,7 @@ public: GcStats& Stats, const GetUnusedReferencesFunc& GetUnusedReferences) override { + ZEN_MEMSCOPE(GetFileCasTag()); ZEN_TRACE_CPU("FileCas::RemoveUnreferencedData"); auto Log = [&Ctx]() { return Ctx.Logger; }; @@ -1432,6 +1456,8 @@ FileCasStrategy::GetGcName(GcCtx&) GcReferencePruner* FileCasStrategy::CreateReferencePruner(GcCtx& Ctx, GcReferenceStoreStats&) { + ZEN_MEMSCOPE(GetFileCasTag()); + ZEN_TRACE_CPU("FileCas::CreateReferencePruner"); auto Log = [&Ctx]() { return Ctx.Logger; }; diff --git a/src/zenstore/gc.cpp b/src/zenstore/gc.cpp index 15dff40f5..f2901c4ee 100644 --- a/src/zenstore/gc.cpp +++ b/src/zenstore/gc.cpp @@ -9,6 +9,7 @@ #include <zencore/filesystem.h> #include <zencore/fmtutils.h> #include <zencore/logging.h> +#include <zencore/memory/llm.h> #include <zencore/scopeguard.h> #include <zencore/string.h> #include <zencore/testing.h> @@ -43,6 +44,14 @@ namespace zen { +const FLLMTag& +GetGcTag() +{ + static FLLMTag _("gc"); + + return _; +} + using namespace std::literals; namespace fs = std::filesystem; @@ -633,6 +642,8 @@ KeepUnusedReferences(std::span<const IoHash> SortedUsedReferences, std::span<IoH void GcManager::AddGcReferencer(GcReferencer& Referencer) { + ZEN_MEMSCOPE(GetGcTag()); + RwLock::ExclusiveLockScope _(m_Lock); m_GcReferencers.push_back(&Referencer); } @@ -646,12 +657,14 @@ GcManager::RemoveGcReferencer(GcReferencer& Referencer) void GcManager::AddGcReferenceLocker(GcReferenceLocker& ReferenceLocker) { + ZEN_MEMSCOPE(GetGcTag()); RwLock::ExclusiveLockScope _(m_Lock); m_GcReferencerLockers.push_back(&ReferenceLocker); } void GcManager::RemoveGcReferenceLocker(GcReferenceLocker& ReferenceLocker) { + ZEN_MEMSCOPE(GetGcTag()); RwLock::ExclusiveLockScope _(m_Lock); std::erase_if(m_GcReferencerLockers, [&](GcReferenceLocker* $) { return $ == &ReferenceLocker; }); } @@ -659,6 +672,7 @@ GcManager::RemoveGcReferenceLocker(GcReferenceLocker& ReferenceLocker) void GcManager::AddGcReferenceStore(GcReferenceStore& ReferenceStore) { + ZEN_MEMSCOPE(GetGcTag()); RwLock::ExclusiveLockScope _(m_Lock); m_GcReferenceStores.push_back(&ReferenceStore); } @@ -676,6 +690,7 @@ GcManager::RemoveGcReferenceStore(GcReferenceStore& ReferenceStore) GcResult GcManager::CollectGarbage(const GcSettings& Settings) { + ZEN_MEMSCOPE(GetGcTag()); ZEN_TRACE_CPU("GcV2::CollectGarbage"); GcCtx Ctx{.Settings = Settings, .IsCancelledFlag = m_CancelGC, .Logger = Log()}; @@ -1333,6 +1348,8 @@ GcManager::SetCancelGC(bool CancelFlag) void GcManager::AddGcStorage(GcStorage* Storage) { + ZEN_MEMSCOPE(GetGcTag()); + ZEN_ASSERT(Storage != nullptr); RwLock::ExclusiveLockScope _(m_Lock); m_GcStorage.push_back(Storage); @@ -1348,6 +1365,8 @@ GcManager::RemoveGcStorage(GcStorage* Storage) void GcManager::ScrubStorage(ScrubContext& GcCtx) { + ZEN_MEMSCOPE(GetGcTag()); + RwLock::SharedLockScope _(m_Lock); for (GcStorage* Storage : m_GcStorage) @@ -1359,6 +1378,7 @@ GcManager::ScrubStorage(ScrubContext& GcCtx) GcStorageSize GcManager::TotalStorageSize() const { + ZEN_MEMSCOPE(GetGcTag()); ZEN_TRACE_CPU("Gc::TotalStorageSize"); RwLock::SharedLockScope _(m_Lock); @@ -1507,6 +1527,8 @@ GcScheduler::~GcScheduler() void GcScheduler::Initialize(const GcSchedulerConfig& Config) { + ZEN_MEMSCOPE(GetGcTag()); + using namespace std::chrono; m_Config = Config; @@ -1571,6 +1593,8 @@ GcScheduler::Initialize(const GcSchedulerConfig& Config) void GcScheduler::Shutdown() { + ZEN_MEMSCOPE(GetGcTag()); + if (static_cast<uint32_t>(GcSchedulerStatus::kStopped) != m_Status) { bool GcIsRunning = m_Status == static_cast<uint32_t>(GcSchedulerStatus::kRunning); @@ -1598,6 +1622,7 @@ GcScheduler::Shutdown() bool GcScheduler::TriggerGc(const GcScheduler::TriggerGcParams& Params) { + ZEN_MEMSCOPE(GetGcTag()); std::unique_lock Lock(m_GcMutex); if (static_cast<uint32_t>(GcSchedulerStatus::kIdle) == m_Status) { @@ -1617,6 +1642,7 @@ GcScheduler::TriggerGc(const GcScheduler::TriggerGcParams& Params) bool GcScheduler::TriggerScrub(const TriggerScrubParams& Params) { + ZEN_MEMSCOPE(GetGcTag()); std::unique_lock Lock(m_GcMutex); if (static_cast<uint32_t>(GcSchedulerStatus::kIdle) == m_Status) @@ -1652,6 +1678,7 @@ GcScheduler::CancelGC() DiskSpace GcScheduler::CheckDiskSpace() { + ZEN_MEMSCOPE(GetGcTag()); std::error_code Ec; DiskSpace Space = DiskSpaceInfo(m_Config.RootDirectory, Ec); if (Ec) @@ -1685,6 +1712,7 @@ GcScheduler::CheckDiskSpace() void GcScheduler::AppendGCLog(std::string_view Id, GcClock::TimePoint StartTime, const GcSettings& Settings, const GcResult& Result) { + ZEN_MEMSCOPE(GetGcTag()); try { std::vector<uint8_t> Blob; @@ -1784,6 +1812,7 @@ GcScheduler::AppendGCLog(std::string_view Id, GcClock::TimePoint StartTime, cons GcSchedulerState GcScheduler::GetState() const { + ZEN_MEMSCOPE(GetGcTag()); GcClock::TimePoint Now = GcClock::Now(); const GcStorageSize TotalSize = m_GcManager.TotalStorageSize(); @@ -1854,6 +1883,7 @@ GcScheduler::GetState() const void GcScheduler::SchedulerThread() { + ZEN_MEMSCOPE(GetGcTag()); SetCurrentThreadName("GcScheduler"); std::chrono::seconds WaitTime{0}; diff --git a/src/zenutil/include/zenutil/logging/rotatingfilesink.h b/src/zenutil/include/zenutil/logging/rotatingfilesink.h index ca4649ba8..e01b86cb7 100644 --- a/src/zenutil/include/zenutil/logging/rotatingfilesink.h +++ b/src/zenutil/include/zenutil/logging/rotatingfilesink.h @@ -2,6 +2,7 @@ #pragma once +#include <zencore/memory/llm.h> #include <zenutil/basicfile.h> ZEN_THIRD_PARTY_INCLUDES_START @@ -24,6 +25,7 @@ public: , m_MaxSize(MaxSize) , m_MaxFiles(MaxFiles) { + ZEN_MEMSCOPE(ELLMTag::Logging); std::error_code Ec; if (RotateOnOpen) { @@ -73,6 +75,8 @@ public: virtual void log(const spdlog::details::log_msg& msg) override { + ZEN_MEMSCOPE(ELLMTag::Logging); + try { spdlog::memory_buf_t Formatted; @@ -108,6 +112,8 @@ public: } virtual void flush() override { + ZEN_MEMSCOPE(ELLMTag::Logging); + try { RwLock::SharedLockScope Lock(m_Lock); @@ -124,6 +130,8 @@ public: virtual void set_pattern(const std::string& pattern) override { + ZEN_MEMSCOPE(ELLMTag::Logging); + try { RwLock::ExclusiveLockScope _(m_Lock); @@ -136,6 +144,8 @@ public: } virtual void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override { + ZEN_MEMSCOPE(ELLMTag::Logging); + try { RwLock::ExclusiveLockScope _(m_Lock); @@ -150,6 +160,8 @@ public: private: void Rotate(RwLock::ExclusiveLockScope&, std::error_code& OutEc) { + ZEN_MEMSCOPE(ELLMTag::Logging); + m_CurrentFile.Close(); OutEc = RotateFiles(m_BaseFilename, m_MaxFiles); @@ -170,6 +182,8 @@ private: bool TrySinkIt(const spdlog::details::log_msg& msg, spdlog::memory_buf_t& OutFormatted) { + ZEN_MEMSCOPE(ELLMTag::Logging); + RwLock::SharedLockScope Lock(m_Lock); if (!m_CurrentFile.IsOpen()) { @@ -193,6 +207,8 @@ private: bool TrySinkIt(const spdlog::memory_buf_t& Formatted) { + ZEN_MEMSCOPE(ELLMTag::Logging); + RwLock::SharedLockScope Lock(m_Lock); if (!m_CurrentFile.IsOpen()) { diff --git a/src/zenutil/logging.cpp b/src/zenutil/logging.cpp index 4f8c5e59e..6314c407f 100644 --- a/src/zenutil/logging.cpp +++ b/src/zenutil/logging.cpp @@ -13,6 +13,7 @@ ZEN_THIRD_PARTY_INCLUDES_END #include <zencore/compactbinary.h> #include <zencore/filesystem.h> #include <zencore/logging.h> +#include <zencore/memory/llm.h> #include <zencore/string.h> #include <zenutil/logging/fullformatter.h> #include <zenutil/logging/jsonformatter.h> @@ -42,6 +43,8 @@ InitializeLogging(const LoggingOptions& LogOptions) void BeginInitializeLogging(const LoggingOptions& LogOptions) { + ZEN_MEMSCOPE(ELLMTag::Logging); + zen::logging::InitializeLogging(); zen::logging::EnableVTMode(); @@ -162,6 +165,8 @@ BeginInitializeLogging(const LoggingOptions& LogOptions) void FinishInitializeLogging(const LoggingOptions& LogOptions) { + ZEN_MEMSCOPE(ELLMTag::Logging); + logging::level::LogLevel LogLevel = logging::level::Info; if (LogOptions.IsDebug) diff --git a/src/zenutil/openprocesscache.cpp b/src/zenutil/openprocesscache.cpp index 256d5fb4a..88bbce8a6 100644 --- a/src/zenutil/openprocesscache.cpp +++ b/src/zenutil/openprocesscache.cpp @@ -3,6 +3,7 @@ #include "zenutil/openprocesscache.h" #include <zencore/fmtutils.h> #include <zencore/logging.h> +#include <zencore/memory/llm.h> #if ZEN_PLATFORM_WINDOWS # include <zencore/windows.h> @@ -14,6 +15,14 @@ namespace zen { +const FLLMTag& +GetProcCacheTag() +{ + static FLLMTag CacheHttpTag("proccache"); + + return CacheHttpTag; +} + OpenProcessCache::OpenProcessCache() #if ZEN_PLATFORM_WINDOWS : m_GcThread(&OpenProcessCache::GcWorker, this) @@ -60,6 +69,9 @@ OpenProcessCache::GetProcessHandle(Oid SessionId, int ProcessPid) { return nullptr; } + + ZEN_MEMSCOPE(GetProcCacheTag()); + #if ZEN_PLATFORM_WINDOWS ZEN_ASSERT(ProcessPid != 0); { @@ -171,6 +183,8 @@ OpenProcessCache::GCHandles() void OpenProcessCache::GcWorker() { + ZEN_MEMSCOPE(GetProcCacheTag()); + SetCurrentThreadName("ProcessCache_GC"); while (!m_GcExitEvent.Wait(500)) |