diff --git a/source/loader/layers/sanitizer/asan_interceptor.cpp b/source/loader/layers/sanitizer/asan_interceptor.cpp index 3b8ad505f4..65c524ac35 100644 --- a/source/loader/layers/sanitizer/asan_interceptor.cpp +++ b/source/loader/layers/sanitizer/asan_interceptor.cpp @@ -177,9 +177,7 @@ ur_result_t enqueueMemSetShadow(std::shared_ptr &ContextInfo, } // namespace ContextInfo::~ContextInfo() { - if (getContext()->interceptor->getOptions().PrintStats) { - Stats.Print(Handle); - } + Stats.Print(Handle); [[maybe_unused]] auto Result = getContext()->urDdiTable.Context.pfnRelease(Handle); diff --git a/source/loader/layers/sanitizer/asan_interceptor.hpp b/source/loader/layers/sanitizer/asan_interceptor.hpp index f6ae179e1d..c9d8f76acd 100644 --- a/source/loader/layers/sanitizer/asan_interceptor.hpp +++ b/source/loader/layers/sanitizer/asan_interceptor.hpp @@ -113,7 +113,7 @@ struct ContextInfo { std::vector DeviceList; std::unordered_map AllocInfosMap; - AsanStats Stats; + AsanStatsWrapper Stats; explicit ContextInfo(ur_context_handle_t Context) : Handle(Context) { [[maybe_unused]] auto Result = diff --git a/source/loader/layers/sanitizer/asan_statistics.cpp b/source/loader/layers/sanitizer/asan_statistics.cpp index e906ae7da2..6dd847c75f 100644 --- a/source/loader/layers/sanitizer/asan_statistics.cpp +++ b/source/loader/layers/sanitizer/asan_statistics.cpp @@ -16,6 +16,32 @@ namespace ur_sanitizer_layer { +struct AsanStats { + void UpdateUSMMalloced(uptr MallocedSize, uptr RedzoneSize); + void UpdateUSMFreed(uptr FreedSize); + void UpdateUSMRealFreed(uptr FreedSize, uptr RedzoneSize); + + void UpdateShadowMmaped(uptr ShadowSize); + void UpdateShadowMalloced(uptr ShadowSize); + void UpdateShadowFreed(uptr ShadowSize); + + void Print(ur_context_handle_t Context); + + private: + std::atomic UsmMalloced; + std::atomic UsmMallocedRedzones; + + // Quarantined memory + std::atomic UsmFreed; + + std::atomic ShadowMmaped; + std::atomic ShadowMalloced; + + double Overhead = 0.0; + + void UpdateOverhead(); +}; + void AsanStats::Print(ur_context_handle_t Context) { getContext()->logger.always("Stats: Context {}", (void *)Context); getContext()->logger.always("Stats: peak memory overhead: {}%", @@ -79,4 +105,54 @@ void AsanStats::UpdateOverhead() { Overhead = std::max(Overhead, NewOverhead); } +void AsanStatsWrapper::UpdateUSMMalloced(uptr MallocedSize, uptr RedzoneSize) { + if (Stat) { + Stat->UpdateUSMMalloced(MallocedSize, RedzoneSize); + } +} + +void AsanStatsWrapper::UpdateUSMFreed(uptr FreedSize) { + if (Stat) { + Stat->UpdateUSMFreed(FreedSize); + } +} + +void AsanStatsWrapper::UpdateUSMRealFreed(uptr FreedSize, uptr RedzoneSize) { + if (Stat) { + Stat->UpdateUSMRealFreed(FreedSize, RedzoneSize); + } +} + +void AsanStatsWrapper::UpdateShadowMmaped(uptr ShadowSize) { + if (Stat) { + Stat->UpdateShadowMmaped(ShadowSize); + } +} + +void AsanStatsWrapper::UpdateShadowMalloced(uptr ShadowSize) { + if (Stat) { + Stat->UpdateShadowMalloced(ShadowSize); + } +} + +void AsanStatsWrapper::UpdateShadowFreed(uptr ShadowSize) { + if (Stat) { + Stat->UpdateShadowFreed(ShadowSize); + } +} + +void AsanStatsWrapper::Print(ur_context_handle_t Context) { + if (Stat) { + Stat->Print(Context); + } +} + +AsanStatsWrapper::AsanStatsWrapper() : Stat(nullptr) { + if (getContext()->interceptor->getOptions().PrintStats) { + Stat = new AsanStats; + } +} + +AsanStatsWrapper::~AsanStatsWrapper() { delete Stat; } + } // namespace ur_sanitizer_layer diff --git a/source/loader/layers/sanitizer/asan_statistics.hpp b/source/loader/layers/sanitizer/asan_statistics.hpp index ab883dbdc2..74884ef1d3 100644 --- a/source/loader/layers/sanitizer/asan_statistics.hpp +++ b/source/loader/layers/sanitizer/asan_statistics.hpp @@ -17,7 +17,13 @@ namespace ur_sanitizer_layer { -struct AsanStats { +struct AsanStats; + +struct AsanStatsWrapper { + + AsanStatsWrapper(); + ~AsanStatsWrapper(); + void UpdateUSMMalloced(uptr MallocedSize, uptr RedzoneSize); void UpdateUSMFreed(uptr FreedSize); void UpdateUSMRealFreed(uptr FreedSize, uptr RedzoneSize); @@ -29,18 +35,7 @@ struct AsanStats { void Print(ur_context_handle_t Context); private: - std::atomic UsmMalloced; - std::atomic UsmMallocedRedzones; - - // Quarantined memory - std::atomic UsmFreed; - - std::atomic ShadowMmaped; - std::atomic ShadowMalloced; - - double Overhead = 0.0; - - void UpdateOverhead(); + AsanStats *Stat; }; } // namespace ur_sanitizer_layer