From 10dde5f3cbe80ab9599ba084c343ffa9ad7fff3e Mon Sep 17 00:00:00 2001 From: liuyue137 Date: Tue, 9 Jun 2026 11:32:45 +0800 Subject: [PATCH 1/7] Rename info to tlogInfo_ in PRINT_TLOG macro to avoid symbol conflict Signed-off-by: liuyue137 --- utils/include/window_manager_hilog.h | 60 ++++++++++++---------------- utils/src/window_manager_hilog.cpp | 35 ++++++++++++++++ 2 files changed, 61 insertions(+), 34 deletions(-) diff --git a/utils/include/window_manager_hilog.h b/utils/include/window_manager_hilog.h index e4637401d2..b4db9b0608 100644 --- a/utils/include/window_manager_hilog.h +++ b/utils/include/window_manager_hilog.h @@ -16,6 +16,7 @@ #ifndef OHOS_WM_INCLUDE_WINDOW_MANAGER_HILOG_H #define OHOS_WM_INCLUDE_WINDOW_MANAGER_HILOG_H +#include #include #include #include "hilog/log.h" @@ -80,6 +81,23 @@ enum class WmsLogTag : uint8_t { }; extern const char* g_domainContents[static_cast(WmsLogTag::END)]; + +struct TLogInfo { + uint32_t domain; + const char* content; +}; + +TLogInfo GetTLogInfo(WmsLogTag tag); + +struct WinPrintLimitState { + std::chrono::time_point last{}; + uint32_t supressed = 0; + int printCount = 0; +}; + +bool WinPrintLimit(WmsLogTag tag, LogLevel level, uint32_t intervals, + WinPrintLimitState& state, uint32_t frequency, const char* funcName); + #ifdef IS_RELEASE_VERSION #define WMS_FILE_NAME "" #define FMT_PREFIX "%{public}s%{public}s: " @@ -92,12 +110,10 @@ extern const char* g_domainContents[static_cast(WmsLogTag::END)]; #endif #define WMS_NO_FILE_NAME "" -#define PRINT_TLOG(level, tag, ...) \ - do { \ - uint32_t hilogDomain = HILOG_DOMAIN_WINDOW + static_cast(tag); \ - const char *domainContent = (tag >= WmsLogTag::DEFAULT && tag < WmsLogTag::END) ? \ - g_domainContents[static_cast(tag)] : ""; \ - HILOG_IMPL(LOG_CORE, level, hilogDomain, domainContent, ##__VA_ARGS__); \ +#define PRINT_TLOG(level, tag, fmt, ...) \ + do { \ + auto tlogInfo_ = GetTLogInfo(tag); \ + HiLogPrint(LOG_CORE, level, tlogInfo_.domain, tlogInfo_.content, fmt, ##__VA_ARGS__); \ } while (0) #define TLOGD(tag, fmt, ...) \ @@ -131,34 +147,10 @@ PRINT_TLOG(LOG_WARN, tag, FMT_PREFIX fmt, WMS_NO_FILE_NAME, C_W_FUNC, ##__VA_ARG #define TLOGNFE(tag, fmt, ...) \ PRINT_TLOG(LOG_ERROR, tag, FMT_PREFIX fmt, WMS_NO_FILE_NAME, C_W_FUNC, ##__VA_ARGS__) -#define WIN_PRINT_LIMIT(tag, level, intervals, canPrint, frequency) \ - do { \ - uint32_t hilogDomain = HILOG_DOMAIN_WINDOW + static_cast(tag); \ - const char *domainContent = ((tag) >= WmsLogTag::DEFAULT && (tag) < WmsLogTag::END) ? \ - g_domainContents[static_cast(tag)] : ""; \ - static auto last = std::chrono::time_point(); \ - static uint32_t supressed = 0; \ - static int printCount = 0; \ - auto now = std::chrono::time_point_cast(std::chrono::system_clock::now()); \ - auto duration = now - last; \ - if (duration.count() >= (intervals)) { \ - last = now; \ - uint32_t supressedCnt = supressed; \ - supressed = 0; \ - printCount = 1; \ - if (supressedCnt != 0) { \ - ((void)HILOG_IMPL(LOG_CORE, (level), hilogDomain, domainContent, \ - "%{public}s log suppressed cnt %{public}u", __func__, supressedCnt)); \ - } \ - (canPrint) = true; \ - } else { \ - if ((printCount++) < (frequency)) { \ - (canPrint) = true; \ - } else { \ - supressed++; \ - (canPrint) = false; \ - } \ - } \ +#define WIN_PRINT_LIMIT(tag, level, intervals, canPrint, frequency) \ + do { \ + static WinPrintLimitState state; \ + (canPrint) = WinPrintLimit(tag, level, intervals, state, frequency, __func__); \ } while (0) #define TLOGI_LIMITN_HOUR(tag, freq, fmt, ...) \ diff --git a/utils/src/window_manager_hilog.cpp b/utils/src/window_manager_hilog.cpp index 15ce7a9ddb..83ab4859a2 100644 --- a/utils/src/window_manager_hilog.cpp +++ b/utils/src/window_manager_hilog.cpp @@ -14,6 +14,7 @@ */ #include "window_manager_hilog.h" +#include namespace OHOS { namespace Rosen { @@ -50,5 +51,39 @@ const char* g_domainContents[static_cast(WmsLogTag::END)] = { "WMSRotation", "WMSAnimation", }; + +TLogInfo GetTLogInfo(WmsLogTag tag) +{ + uint32_t domain = HILOG_DOMAIN_WINDOW + static_cast(tag); + const char* content = (tag >= WmsLogTag::DEFAULT && tag < WmsLogTag::END) ? + g_domainContents[static_cast(tag)] : ""; + return {domain, content}; +} + +bool WinPrintLimit(WmsLogTag tag, LogLevel level, uint32_t intervals, + WinPrintLimitState& state, uint32_t frequency, const char* funcName) +{ + auto info = GetTLogInfo(tag); + auto now = std::chrono::time_point_cast(std::chrono::system_clock::now()); + auto duration = now - state.last; + if (duration.count() >= intervals) { + state.last = now; + uint32_t supressedCnt = state.supressed; + state.supressed = 0; + state.printCount = 1; + if (supressedCnt != 0) { + HiLogPrint(LOG_CORE, level, info.domain, info.content, + "%{public}s log suppressed cnt %{public}u", funcName, supressedCnt); + } + return true; + } else { + if (state.printCount++ < frequency) { + return true; + } else { + state.supressed++; + return false; + } + } +} } // namespace OHOS } \ No newline at end of file From c8678694b8242d59aa6fa2c72535205a9fc75dc5 Mon Sep 17 00:00:00 2001 From: liuyue137 Date: Tue, 9 Jun 2026 16:42:44 +0800 Subject: [PATCH 2/7] Rename info to tlogInfo_ in PRINT_TLOG macro to avoid symbol conflict Signed-off-by: liuyue137 Signed-off-by: liuyue137 --- utils/include/window_manager_hilog.h | 29 +++++++++++++++++++++++++--- utils/src/window_manager_hilog.cpp | 13 ++++++------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/utils/include/window_manager_hilog.h b/utils/include/window_manager_hilog.h index b4db9b0608..84861347f5 100644 --- a/utils/include/window_manager_hilog.h +++ b/utils/include/window_manager_hilog.h @@ -95,8 +95,30 @@ struct WinPrintLimitState { int printCount = 0; }; -bool WinPrintLimit(WmsLogTag tag, LogLevel level, uint32_t intervals, - WinPrintLimitState& state, uint32_t frequency, const char* funcName); +struct WinPrintLimitConfig { + WmsLogTag logTag; + LogLevel logLevel; + uint32_t timeIntervals; + uint32_t printFrequency; + const char* functionName; + + WinPrintLimitConfig() + : logTag(WmsLogTag::DEFAULT) + , logLevel(LOG_INFO) + , timeIntervals(WIN_LOG_LIMIT_MINUTE) + , printFrequency(TEN_TIMES) + , functionName("") {} + + WinPrintLimitConfig(WmsLogTag tag, LogLevel level, uint32_t intervals, + uint32_t frequency, const char* funcName) + : logTag(tag) + , logLevel(level) + , timeIntervals(intervals) + , printFrequency(frequency) + , functionName(funcName) {} +}; + +bool WinPrintLimit(const WinPrintLimitConfig& config, WinPrintLimitState& state); #ifdef IS_RELEASE_VERSION #define WMS_FILE_NAME "" @@ -150,7 +172,8 @@ PRINT_TLOG(LOG_ERROR, tag, FMT_PREFIX fmt, WMS_NO_FILE_NAME, C_W_FUNC, ##__VA_AR #define WIN_PRINT_LIMIT(tag, level, intervals, canPrint, frequency) \ do { \ static WinPrintLimitState state; \ - (canPrint) = WinPrintLimit(tag, level, intervals, state, frequency, __func__); \ + WinPrintLimitConfig config(tag, level, intervals, frequency, __func__); \ + (canPrint) = WinPrintLimit(config, state); \ } while (0) #define TLOGI_LIMITN_HOUR(tag, freq, fmt, ...) \ diff --git a/utils/src/window_manager_hilog.cpp b/utils/src/window_manager_hilog.cpp index 83ab4859a2..1801ec22f5 100644 --- a/utils/src/window_manager_hilog.cpp +++ b/utils/src/window_manager_hilog.cpp @@ -60,24 +60,23 @@ TLogInfo GetTLogInfo(WmsLogTag tag) return {domain, content}; } -bool WinPrintLimit(WmsLogTag tag, LogLevel level, uint32_t intervals, - WinPrintLimitState& state, uint32_t frequency, const char* funcName) +bool WinPrintLimit(const WinPrintLimitConfig& config, WinPrintLimitState& state) { - auto info = GetTLogInfo(tag); + auto info = GetTLogInfo(config.logTag); auto now = std::chrono::time_point_cast(std::chrono::system_clock::now()); auto duration = now - state.last; - if (duration.count() >= intervals) { + if (duration.count() >= config.timeIntervals) { state.last = now; uint32_t supressedCnt = state.supressed; state.supressed = 0; state.printCount = 1; if (supressedCnt != 0) { - HiLogPrint(LOG_CORE, level, info.domain, info.content, - "%{public}s log suppressed cnt %{public}u", funcName, supressedCnt); + HiLogPrint(LOG_CORE, config.logLevel, info.domain, info.content, + "%{public}s log suppressed cnt %{public}u", config.functionName, supressedCnt); } return true; } else { - if (state.printCount++ < frequency) { + if (state.printCount++ < config.printFrequency) { return true; } else { state.supressed++; From 4bd09a2aa90f3ce9466b83c1d045c45e1e1726c2 Mon Sep 17 00:00:00 2001 From: liuyue137 Date: Tue, 9 Jun 2026 17:25:28 +0800 Subject: [PATCH 3/7] Rename info to tlogInfo_ in PRINT_TLOG macro to avoid symbol conflict Signed-off-by: liuyue137 Signed-off-by: liuyue137 --- utils/include/window_manager_hilog.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/utils/include/window_manager_hilog.h b/utils/include/window_manager_hilog.h index 84861347f5..fae59d7eec 100644 --- a/utils/include/window_manager_hilog.h +++ b/utils/include/window_manager_hilog.h @@ -102,20 +102,20 @@ struct WinPrintLimitConfig { uint32_t printFrequency; const char* functionName; - WinPrintLimitConfig() - : logTag(WmsLogTag::DEFAULT) - , logLevel(LOG_INFO) - , timeIntervals(WIN_LOG_LIMIT_MINUTE) - , printFrequency(TEN_TIMES) - , functionName("") {} + WinPrintLimitConfig() + : logTag(WmsLogTag::DEFAULT), + logLevel(LOG_INFO), + timeIntervals(WIN_LOG_LIMIT_MINUTE), + printFrequency(TEN_TIMES), + functionName("") {} WinPrintLimitConfig(WmsLogTag tag, LogLevel level, uint32_t intervals, - uint32_t frequency, const char* funcName) - : logTag(tag) - , logLevel(level) - , timeIntervals(intervals) - , printFrequency(frequency) - , functionName(funcName) {} + uint32_t frequency, const char* funcName) + : logTag(tag), + logLevel(level), + timeIntervals(intervals), + printFrequency(frequency), + functionName(funcName) {} }; bool WinPrintLimit(const WinPrintLimitConfig& config, WinPrintLimitState& state); From 2f47273a2e1b8e5f3abb6a10858794c00d777182 Mon Sep 17 00:00:00 2001 From: liuyue137 Date: Thu, 18 Jun 2026 09:47:11 +0800 Subject: [PATCH 4/7] Rename info to tlogInfo_ in PRINT_TLOG macro to avoid symbol conflict Signed-off-by: liuyue137 Signed-off-by: liuyue137 --- utils/include/window_manager_hilog.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/include/window_manager_hilog.h b/utils/include/window_manager_hilog.h index fae59d7eec..266dfd580c 100644 --- a/utils/include/window_manager_hilog.h +++ b/utils/include/window_manager_hilog.h @@ -171,7 +171,7 @@ PRINT_TLOG(LOG_ERROR, tag, FMT_PREFIX fmt, WMS_NO_FILE_NAME, C_W_FUNC, ##__VA_AR #define WIN_PRINT_LIMIT(tag, level, intervals, canPrint, frequency) \ do { \ - static WinPrintLimitState state; \ + static thread_local WinPrintLimitState state; \ WinPrintLimitConfig config(tag, level, intervals, frequency, __func__); \ (canPrint) = WinPrintLimit(config, state); \ } while (0) From 72ced98ba27601004871b668211e078c8dbe6e34 Mon Sep 17 00:00:00 2001 From: liuyue137 Date: Thu, 18 Jun 2026 10:51:31 +0800 Subject: [PATCH 5/7] Rename info to tlogInfo_ in PRINT_TLOG macro to avoid symbol conflict Signed-off-by: liuyue137 Signed-off-by: liuyue137 --- utils/include/window_manager_hilog.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/include/window_manager_hilog.h b/utils/include/window_manager_hilog.h index 266dfd580c..fae59d7eec 100644 --- a/utils/include/window_manager_hilog.h +++ b/utils/include/window_manager_hilog.h @@ -171,7 +171,7 @@ PRINT_TLOG(LOG_ERROR, tag, FMT_PREFIX fmt, WMS_NO_FILE_NAME, C_W_FUNC, ##__VA_AR #define WIN_PRINT_LIMIT(tag, level, intervals, canPrint, frequency) \ do { \ - static thread_local WinPrintLimitState state; \ + static WinPrintLimitState state; \ WinPrintLimitConfig config(tag, level, intervals, frequency, __func__); \ (canPrint) = WinPrintLimit(config, state); \ } while (0) From fad00cba9a4067b364036d7539c0db82f74989fc Mon Sep 17 00:00:00 2001 From: liuyue137 Date: Thu, 18 Jun 2026 10:54:39 +0800 Subject: [PATCH 6/7] fix(utils): use atomic to fix WinPrintLimit multi-thread race condition - Change supressed and printCount to atomic type - Use atomic operations (load/store/fetch_add) to avoid race condition - Keep static to maintain global rate limit spec - Solve RMW (Read-Modify-Write) race condition problem Signed-off-by: liuyue137 Co-Authored-by: Agent Signed-off-by: liuyue137 --- utils/include/window_manager_hilog.h | 5 +++-- utils/src/window_manager_hilog.cpp | 12 +++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/utils/include/window_manager_hilog.h b/utils/include/window_manager_hilog.h index fae59d7eec..2a5c592498 100644 --- a/utils/include/window_manager_hilog.h +++ b/utils/include/window_manager_hilog.h @@ -19,6 +19,7 @@ #include #include #include +#include #include "hilog/log.h" namespace OHOS { @@ -91,8 +92,8 @@ TLogInfo GetTLogInfo(WmsLogTag tag); struct WinPrintLimitState { std::chrono::time_point last{}; - uint32_t supressed = 0; - int printCount = 0; + std::atomic supressed{0}; + std::atomic printCount{0}; }; struct WinPrintLimitConfig { diff --git a/utils/src/window_manager_hilog.cpp b/utils/src/window_manager_hilog.cpp index 1801ec22f5..76a94a5d39 100644 --- a/utils/src/window_manager_hilog.cpp +++ b/utils/src/window_manager_hilog.cpp @@ -67,19 +67,21 @@ bool WinPrintLimit(const WinPrintLimitConfig& config, WinPrintLimitState& state) auto duration = now - state.last; if (duration.count() >= config.timeIntervals) { state.last = now; - uint32_t supressedCnt = state.supressed; - state.supressed = 0; - state.printCount = 1; + uint32_t supressedCnt = state.supressed.load(); + state.supressed.store(0); + state.printCount.store(1); if (supressedCnt != 0) { HiLogPrint(LOG_CORE, config.logLevel, info.domain, info.content, "%{public}s log suppressed cnt %{public}u", config.functionName, supressedCnt); } return true; } else { - if (state.printCount++ < config.printFrequency) { + int count = state.printCount.load(); + if (count < config.printFrequency) { + state.printCount.store(count + 1); return true; } else { - state.supressed++; + state.supressed.fetch_add(1); return false; } } From 3a230cfcf0ae7c2c63a84b60738ded00b0b75e02 Mon Sep 17 00:00:00 2001 From: liuyue137 Date: Thu, 18 Jun 2026 11:13:12 +0800 Subject: [PATCH 7/7] revert(utils): remove atomic operations from WinPrintLimit - Revert atomic type changes to plain types - Revert atomic operations to normal operations - Accept multi-thread race condition risk - Keep static for global rate limiting Signed-off-by: liuyue137 Co-Authored-by: Agent Signed-off-by: liuyue137 --- utils/include/window_manager_hilog.h | 5 ++--- utils/src/window_manager_hilog.cpp | 12 +++++------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/utils/include/window_manager_hilog.h b/utils/include/window_manager_hilog.h index 2a5c592498..fae59d7eec 100644 --- a/utils/include/window_manager_hilog.h +++ b/utils/include/window_manager_hilog.h @@ -19,7 +19,6 @@ #include #include #include -#include #include "hilog/log.h" namespace OHOS { @@ -92,8 +91,8 @@ TLogInfo GetTLogInfo(WmsLogTag tag); struct WinPrintLimitState { std::chrono::time_point last{}; - std::atomic supressed{0}; - std::atomic printCount{0}; + uint32_t supressed = 0; + int printCount = 0; }; struct WinPrintLimitConfig { diff --git a/utils/src/window_manager_hilog.cpp b/utils/src/window_manager_hilog.cpp index 76a94a5d39..1801ec22f5 100644 --- a/utils/src/window_manager_hilog.cpp +++ b/utils/src/window_manager_hilog.cpp @@ -67,21 +67,19 @@ bool WinPrintLimit(const WinPrintLimitConfig& config, WinPrintLimitState& state) auto duration = now - state.last; if (duration.count() >= config.timeIntervals) { state.last = now; - uint32_t supressedCnt = state.supressed.load(); - state.supressed.store(0); - state.printCount.store(1); + uint32_t supressedCnt = state.supressed; + state.supressed = 0; + state.printCount = 1; if (supressedCnt != 0) { HiLogPrint(LOG_CORE, config.logLevel, info.domain, info.content, "%{public}s log suppressed cnt %{public}u", config.functionName, supressedCnt); } return true; } else { - int count = state.printCount.load(); - if (count < config.printFrequency) { - state.printCount.store(count + 1); + if (state.printCount++ < config.printFrequency) { return true; } else { - state.supressed.fetch_add(1); + state.supressed++; return false; } }