From 2d0a2c0acabc326fcbe20e7bab1a2d0ec94b1046 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Mon, 6 Dec 2021 07:49:42 +0000 Subject: [PATCH 1/7] std::chrono::seconds Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_persister.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 6a7e389..82c7d92 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -296,7 +296,8 @@ int LogPersister::ThreadFunc() } if (!hilogBuffer->Query(shared_from_this())) { unique_lock lk(cvMutex); - if (condVariable.wait_for(lk, sleepTime * 1s) == + + if (condVariable.wait_for(lk, std::chrono::seconds(sleepTime)) == cv_status::timeout) { if (toExit) { break; -- Gitee From ecb6052b76926ef8b66825f025b8a2d4e8023c53 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Mon, 6 Dec 2021 10:14:46 +0000 Subject: [PATCH 2/7] va_list is not recoginized in gcc add timestamp Signed-off-by: aajwy <13051180828@163.com> --- frameworks/native/hilog_inner.h | 1 + frameworks/native/hilog_printf.cpp | 11 +++++ services/hilogd/include/log_time_stamp.h | 54 ++++++++++++++++++++++++ services/hilogd/log_buffer.cpp | 8 ++-- 4 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 services/hilogd/include/log_time_stamp.h diff --git a/frameworks/native/hilog_inner.h b/frameworks/native/hilog_inner.h index 611d189..2095b0f 100644 --- a/frameworks/native/hilog_inner.h +++ b/frameworks/native/hilog_inner.h @@ -17,6 +17,7 @@ #define HILOG_INNER_H #include "hilog/log.h" +#include #ifdef __cplusplus extern "C" { diff --git a/frameworks/native/hilog_printf.cpp b/frameworks/native/hilog_printf.cpp index 2541ec9..cbf7947 100644 --- a/frameworks/native/hilog_printf.cpp +++ b/frameworks/native/hilog_printf.cpp @@ -243,10 +243,21 @@ int HiLogPrintArgs(const LogType type, const LogLevel level, const unsigned int /* format log string */ debug = IsDebugOn(); priv = (!debug) && IsPrivateSwitchOn(); +#ifdef __clang__ +/*code specific to clang compiler*/ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wformat-nonliteral" +#elif __GNUC__ +/*code for GNU C compiler */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat-nonliteral" +#endif ret = vsnprintfp_s(logBuf, MAX_LOG_LEN - traceBufLen, MAX_LOG_LEN - traceBufLen - 1, priv, fmt, ap); +#ifdef __clang__ #pragma clang diagnostic pop +#elif __GNUC__ +#pragma GCC diagnostic pop +#endif /* fill header info */ int tagLen = strnlen(tag, MAX_TAG_LEN - 1); diff --git a/services/hilogd/include/log_time_stamp.h b/services/hilogd/include/log_time_stamp.h new file mode 100644 index 0000000..4022dfb --- /dev/null +++ b/services/hilogd/include/log_time_stamp.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef LOG_TIME_STAMP_H +#define LOG_TIME_STAMP_H + +namespace OHOS { +namespace HiviewDFX { +class LogTimeStamp { +public: + LogTimeStamp() {}; + ~LogTimeStamp() = default; + explicit LogTimeStamp(uint32_t sec, uint32_t nsec = 0) + : tv_sec(sec), tv_nsec(nsec) { + }; + /* LogTimeStamp */ + bool operator==(const LogTimeStamp& T) const { + return (tv_sec == T.tv_sec) && (tv_nsec == T.tv_nsec); + } + bool operator!=(const LogTimeStamp& T) const { + return !(*this == T); + } + bool operator<(const LogTimeStamp& T) const { + return (tv_sec < T.tv_sec) || + ((tv_sec == T.tv_sec) && (tv_nsec < T.tv_nsec)); + } + bool operator>=(const LogTimeStamp& T) const { + return !(*this < T); + } + bool operator>(const LogTimeStamp& T) const { + return (tv_sec > T.tv_sec) || + ((tv_sec == T.tv_sec) && (tv_nsec > T.tv_nsec)); + } + bool operator<=(const LogTimeStamp& T) const { + return !(*this > T); + } +private: + uint32_t tv_sec = 0; + uint32_t tv_nsec = 0; +}; +} // namespace HiviewDFX +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 5c46df0..8a6bafe 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -26,7 +26,7 @@ #include "hilog_common.h" #include "flow_control_init.h" - +#include "log_time_stamp.h" namespace OHOS { namespace HiviewDFX { using namespace std; @@ -96,12 +96,14 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) // Insert new log into HilogBuffer std::list::reverse_iterator rit = hilogDataList.rbegin(); - if (msg.tv_sec >= (rit->tv_sec)) { + LogTimeStamp msgTimeStamp(msg.tv_sec, msg.tv_nsec); + LogTimeStamp ritTimeStamp(rit->tv_sec, rit->tv_nsec); + if (msgTimeStamp >= ritTimeStamp) { hilogDataList.emplace_back(msg); } else { // Find the place with right timestamp ++rit; - for (; rit != hilogDataList.rend() && msg.tv_sec < rit->tv_sec; ++rit) { + for (; rit != hilogDataList.rend() && msgTimeStamp < ritTimeStamp; ++rit) { logReaderListMutex.lock_shared(); for (auto &itr :logReaderList) { if (itr.lock()->readPos == std::prev(rit.base())) { -- Gitee From 1b884a6b570dd6a8c2fab78f4c560dd7a61b4693 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 7 Dec 2021 09:59:43 +0000 Subject: [PATCH 3/7] olddata insert Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/include/log_time_stamp.h | 45 +++++++++++++++++++----- services/hilogd/log_buffer.cpp | 27 +++----------- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/services/hilogd/include/log_time_stamp.h b/services/hilogd/include/log_time_stamp.h index 4022dfb..b33b7af 100644 --- a/services/hilogd/include/log_time_stamp.h +++ b/services/hilogd/include/log_time_stamp.h @@ -15,37 +15,64 @@ #ifndef LOG_TIME_STAMP_H #define LOG_TIME_STAMP_H +#define NS_PER_SEC 1000000000ULL namespace OHOS { namespace HiviewDFX { class LogTimeStamp { + public: LogTimeStamp() {}; ~LogTimeStamp() = default; + explicit LogTimeStamp(const timespec& T) + : tv_sec(static_cast(T.tv_sec)), tv_nsec(static_cast(T.tv_nsec)) {} explicit LogTimeStamp(uint32_t sec, uint32_t nsec = 0) - : tv_sec(sec), tv_nsec(nsec) { + : tv_sec(sec), tv_nsec(nsec) { }; /* LogTimeStamp */ bool operator==(const LogTimeStamp& T) const { - return (tv_sec == T.tv_sec) && (tv_nsec == T.tv_nsec); + return (tv_sec == T.tv_sec) && (tv_nsec == T.tv_nsec); } bool operator!=(const LogTimeStamp& T) const { - return !(*this == T); + return !(*this == T); } bool operator<(const LogTimeStamp& T) const { - return (tv_sec < T.tv_sec) || - ((tv_sec == T.tv_sec) && (tv_nsec < T.tv_nsec)); + return (tv_sec < T.tv_sec) || + ((tv_sec == T.tv_sec) && (tv_nsec < T.tv_nsec)); } bool operator>=(const LogTimeStamp& T) const { - return !(*this < T); + return !(*this < T); } bool operator>(const LogTimeStamp& T) const { - return (tv_sec > T.tv_sec) || - ((tv_sec == T.tv_sec) && (tv_nsec > T.tv_nsec)); + return (tv_sec > T.tv_sec) || + ((tv_sec == T.tv_sec) && (tv_nsec > T.tv_nsec)); } bool operator<=(const LogTimeStamp& T) const { - return !(*this > T); + return !(*this > T); + } + LogTimeStamp operator-=(const LogTimeStamp& T) { + if (*this <= T) { + return *this = LogTimeStamp(epoch); + } + if (this->tv_nsec < T.tv_nsec) { + --this->tv_sec; + this->tv_nsec = NS_PER_SEC + this->tv_nsec - T.tv_nsec; + } else { + this->tv_nsec -= T.tv_nsec; + } + this->tv_sec -= T.tv_sec; + return *this; + } + LogTimeStamp operator+=(const LogTimeStamp& T) { + this->tv_nsec += T.tv_nsec; + if (this->tv_nsec >= NS_PER_SEC) { + this->tv_nsec -= NS_PER_SEC; + ++this->tv_sec; + } + this->tv_sec += T.tv_sec; + return *this; } private: + static constexpr timespec epoch = {0, 0}; uint32_t tv_sec = 0; uint32_t tv_nsec = 0; }; diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 8a6bafe..445f455 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -98,19 +98,15 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) std::list::reverse_iterator rit = hilogDataList.rbegin(); LogTimeStamp msgTimeStamp(msg.tv_sec, msg.tv_nsec); LogTimeStamp ritTimeStamp(rit->tv_sec, rit->tv_nsec); + LogTimeStamp measureTimeStamp(rit->tv_sec, rit->tv_nsec); if (msgTimeStamp >= ritTimeStamp) { hilogDataList.emplace_back(msg); } else { // Find the place with right timestamp ++rit; - for (; rit != hilogDataList.rend() && msgTimeStamp < ritTimeStamp; ++rit) { - logReaderListMutex.lock_shared(); - for (auto &itr :logReaderList) { - if (itr.lock()->readPos == std::prev(rit.base())) { - itr.lock()->oldData.emplace_front(msg); - } - } - logReaderListMutex.unlock_shared(); + for (; rit != hilogDataList.rend() && (msgTimeStamp < measureTimeStamp + || (ritTimeStamp -= msgTimeStamp) > LogTimeStamp(5)); ++rit) { + hilogDataList.emplace_front(msg); } hilogDataList.emplace(rit.base(), msg); } @@ -141,21 +137,6 @@ bool HilogBuffer::Query(std::shared_ptr reader) reader->readPos = std::next(reader->lastPos); } } - // Look up in oldData first - if (!reader->oldData.empty()) { - reader->SetSendId(SENDIDA); - reader->WriteData(&(reader->oldData.back())); - printLenByType[(reader->oldData.back()).type] += strlen(reader->oldData.back().content); - if (printLenByDomain.count(reader->oldData.back().domain) == 0) { - printLenByDomain.insert(pair(reader->oldData.back().domain, - strlen(reader->oldData.back().content))); - } else { - printLenByDomain[reader->oldData.back().domain] += strlen(reader->oldData.back().content); - } - reader->oldData.pop_back(); - hilogBufferMutex.unlock_shared(); - return true; - } while (reader->readPos != hilogDataList.end()) { reader->lastPos = reader->readPos; if (ConditionMatch(reader)) { -- Gitee From 509a21afef5994c90f6cbd87a2cde12fd7335f72 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Wed, 8 Dec 2021 01:36:43 +0000 Subject: [PATCH 4/7] setTIME Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/include/log_time_stamp.h | 4 ++++ services/hilogd/log_buffer.cpp | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/services/hilogd/include/log_time_stamp.h b/services/hilogd/include/log_time_stamp.h index b33b7af..0c95051 100644 --- a/services/hilogd/include/log_time_stamp.h +++ b/services/hilogd/include/log_time_stamp.h @@ -71,6 +71,10 @@ public: this->tv_sec += T.tv_sec; return *this; } + void SetTimeStamp(uint32_t sec, uint32_t nsec) { + this->tv_sec = sec; + this->tv_nsec = nsec; + } private: static constexpr timespec epoch = {0, 0}; uint32_t tv_sec = 0; diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 445f455..e40645e 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -104,9 +104,17 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) } else { // Find the place with right timestamp ++rit; + ritTimeStamp.SetTimeStamp(rit->tv_sec, rit->tv_nsec); for (; rit != hilogDataList.rend() && (msgTimeStamp < measureTimeStamp || (ritTimeStamp -= msgTimeStamp) > LogTimeStamp(5)); ++rit) { - hilogDataList.emplace_front(msg); + ritTimeStamp.SetTimeStamp(rit->tv_sec, rit->tv_nsec); + logReaderListMutex.lock_shared(); + for (auto &itr :logReaderList) { + if (itr.lock()->readPos == std::prev(rit.base())) { + hilogDataList.emplace_front(msg); + } + } + logReaderListMutex.unlock_shared(); } hilogDataList.emplace(rit.base(), msg); } -- Gitee From ab02fd61bea34a0aab25d62053e77dad7a52e4bf Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Wed, 8 Dec 2021 03:02:23 +0000 Subject: [PATCH 5/7] rm olddata Signed-off-by: aajwy <13051180828@163.com> --- frameworks/native/hilog_inner.h | 1 - frameworks/native/hilog_printf.cpp | 24 ++----- .../native}/include/log_time_stamp.h | 71 ++++++++++--------- services/hilogd/flow_control_init.cpp | 20 ++---- services/hilogd/include/log_reader.h | 1 - services/hilogd/log_buffer.cpp | 17 ++--- services/hilogd/log_reader.cpp | 1 - 7 files changed, 56 insertions(+), 79 deletions(-) rename {services/hilogd => frameworks/native}/include/log_time_stamp.h (46%) diff --git a/frameworks/native/hilog_inner.h b/frameworks/native/hilog_inner.h index 2095b0f..611d189 100644 --- a/frameworks/native/hilog_inner.h +++ b/frameworks/native/hilog_inner.h @@ -17,7 +17,6 @@ #define HILOG_INNER_H #include "hilog/log.h" -#include #ifdef __cplusplus extern "C" { diff --git a/frameworks/native/hilog_printf.cpp b/frameworks/native/hilog_printf.cpp index cbf7947..98d1e0d 100644 --- a/frameworks/native/hilog_printf.cpp +++ b/frameworks/native/hilog_printf.cpp @@ -25,6 +25,7 @@ #include #include +#include "log_time_stamp.h" #include "hilog_trace.h" #include "hilog_inner.h" #include "hilog/log.h" @@ -34,7 +35,6 @@ using namespace std; static RegisterFunc g_registerFunc = nullptr; static atomic_int g_hiLogGetIdCallCount = 0; -static const long long NSEC_PER_SEC = 1000000000ULL; static const char P_LIMIT_TAG[] = "LOGLIMITP"; #ifdef DEBUG static const int MAX_PATH_LEN = 1024; @@ -64,13 +64,6 @@ void HiLogUnregisterGetIdFun(RegisterFunc registerFunc) return; } -static long long HiLogTimespecSub(struct timespec a, struct timespec b) -{ - long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec; - - ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec; - return ret; -} static uint16_t GetFinalLevel(unsigned int domain, const std::string& tag) { uint16_t domainLevel = GetDomainLevel(domain); @@ -135,20 +128,16 @@ static int HiLogFlowCtrlProcess(int len, uint16_t logType, bool debug) static uint32_t processQuota = DEFAULT_QUOTA; static atomic_int gSumLen = 0; static atomic_int gDropped = 0; - static atomic gStartTime = atomic({ - .tv_sec = 0, .tv_nsec = 0 - }); + LogTimeStamp startTime(0, 0); + static atomic gStartTime(startTime); static std::atomic_flag isFirstFlag = ATOMIC_FLAG_INIT; if (!isFirstFlag.test_and_set()) { processQuota = ParseProcessQuota(); } - - struct timespec tsNow = { 0, 0 }; - struct timespec tsStart = atomic_load(&gStartTime); - clock_gettime(CLOCK_MONOTONIC, &tsNow); - long long ns = HiLogTimespecSub(tsStart, tsNow); + LogTimeStamp tsStart = atomic_load(&gStartTime); + LogTimeStamp tsNow(CLOCK_MONOTONIC); /* in statistic period(1 second) */ - if (ns <= NSEC_PER_SEC) { + if ((tsStart -= tsNow) <= LogTimeStamp(1)) { uint32_t sumLen = (uint32_t)atomic_load(&gSumLen); if (sumLen > processQuota) { /* over quota, -1 means don't print */ atomic_fetch_add_explicit(&gDropped, 1, memory_order_relaxed); @@ -163,7 +152,6 @@ static int HiLogFlowCtrlProcess(int len, uint16_t logType, bool debug) atomic_store(&gSumLen, len); return dropped; } - return 0; } diff --git a/services/hilogd/include/log_time_stamp.h b/frameworks/native/include/log_time_stamp.h similarity index 46% rename from services/hilogd/include/log_time_stamp.h rename to frameworks/native/include/log_time_stamp.h index 0c95051..43e69b3 100644 --- a/services/hilogd/include/log_time_stamp.h +++ b/frameworks/native/include/log_time_stamp.h @@ -16,70 +16,75 @@ #define LOG_TIME_STAMP_H #define NS_PER_SEC 1000000000ULL -namespace OHOS { -namespace HiviewDFX { + class LogTimeStamp { public: - LogTimeStamp() {}; + LogTimeStamp() = default; ~LogTimeStamp() = default; - explicit LogTimeStamp(const timespec& T) - : tv_sec(static_cast(T.tv_sec)), tv_nsec(static_cast(T.tv_nsec)) {} +#ifdef __linux__ + explicit LogTimeStamp(clockid_t id) { + timespec time; + clock_gettime(id, &time); + tv_sec = static_cast(time.tv_sec); + tv_nsec = static_cast(time.tv_nsec); + } +#endif + explicit LogTimeStamp(const timespec& time) + : tv_sec(static_cast(time.tv_sec)), + tv_nsec(static_cast(time.tv_nsec)) {}; explicit LogTimeStamp(uint32_t sec, uint32_t nsec = 0) - : tv_sec(sec), tv_nsec(nsec) { - }; + : tv_sec(sec), tv_nsec(nsec) {}; /* LogTimeStamp */ - bool operator==(const LogTimeStamp& T) const { - return (tv_sec == T.tv_sec) && (tv_nsec == T.tv_nsec); + bool operator == (const LogTimeStamp& time) const { + return (tv_sec == time.tv_sec) && (tv_nsec == time.tv_nsec); } - bool operator!=(const LogTimeStamp& T) const { - return !(*this == T); + bool operator != (const LogTimeStamp& time) const { + return !(*this == time); } - bool operator<(const LogTimeStamp& T) const { - return (tv_sec < T.tv_sec) || - ((tv_sec == T.tv_sec) && (tv_nsec < T.tv_nsec)); + bool operator < (const LogTimeStamp& time) const { + return (tv_sec < time.tv_sec) || + ((tv_sec == time.tv_sec) && (tv_nsec < time.tv_nsec)); } - bool operator>=(const LogTimeStamp& T) const { - return !(*this < T); + bool operator >= (const LogTimeStamp& time) const { + return !(*this < time); } - bool operator>(const LogTimeStamp& T) const { - return (tv_sec > T.tv_sec) || - ((tv_sec == T.tv_sec) && (tv_nsec > T.tv_nsec)); + bool operator > (const LogTimeStamp& time) const { + return (tv_sec > time.tv_sec) || + ((tv_sec == time.tv_sec) && (tv_nsec > time.tv_nsec)); } - bool operator<=(const LogTimeStamp& T) const { - return !(*this > T); + bool operator <= (const LogTimeStamp& time) const { + return !(*this > time); } - LogTimeStamp operator-=(const LogTimeStamp& T) { - if (*this <= T) { + LogTimeStamp operator -= (const LogTimeStamp& time) { + if (*this <= time) { return *this = LogTimeStamp(epoch); } - if (this->tv_nsec < T.tv_nsec) { + if (this->tv_nsec < time.tv_nsec) { --this->tv_sec; - this->tv_nsec = NS_PER_SEC + this->tv_nsec - T.tv_nsec; + this->tv_nsec = NS_PER_SEC + this->tv_nsec - time.tv_nsec; } else { - this->tv_nsec -= T.tv_nsec; + this->tv_nsec -= time.tv_nsec; } - this->tv_sec -= T.tv_sec; + this->tv_sec -= time.tv_sec; return *this; } - LogTimeStamp operator+=(const LogTimeStamp& T) { - this->tv_nsec += T.tv_nsec; + LogTimeStamp operator += (const LogTimeStamp& time) { + this->tv_nsec += time.tv_nsec; if (this->tv_nsec >= NS_PER_SEC) { this->tv_nsec -= NS_PER_SEC; ++this->tv_sec; } - this->tv_sec += T.tv_sec; + this->tv_sec += time.tv_sec; return *this; } void SetTimeStamp(uint32_t sec, uint32_t nsec) { this->tv_sec = sec; this->tv_nsec = nsec; } -private: static constexpr timespec epoch = {0, 0}; uint32_t tv_sec = 0; uint32_t tv_nsec = 0; }; -} // namespace HiviewDFX -} // namespace OHOS + #endif \ No newline at end of file diff --git a/services/hilogd/flow_control_init.cpp b/services/hilogd/flow_control_init.cpp index 5f4f47e..606457d 100644 --- a/services/hilogd/flow_control_init.cpp +++ b/services/hilogd/flow_control_init.cpp @@ -24,12 +24,12 @@ #include #include #include "properties.h" +#include "log_time_stamp.h" namespace OHOS { namespace HiviewDFX { static const int DOMAIN_FILTER = 0x00fffff; static const int DOMAIN_FILTER_SUBSYSTEM = 8; -static const long long NSEC_PER_SEC = 1000000000LL; using DomainInfo = struct { std::string domain; @@ -37,7 +37,7 @@ using DomainInfo = struct { uint32_t domainQuota; uint32_t sumLen; uint32_t dropped; - struct timespec startTime; + LogTimeStamp startTime; }; static std::unordered_map g_domainMap; @@ -125,7 +125,7 @@ void ParseDomainQuota(std::string &domainStr) domainInfo->domain = domainName; domainInfo->domainId = domainId; domainInfo->domainQuota = peak; - domainInfo->startTime = { .tv_sec = 0, .tv_nsec = 0}; + domainInfo->startTime.SetTimeStamp(0, 0); domainInfo->sumLen = 0; domainInfo->dropped = 0; g_domainMap.insert({ domainId, domainInfo }); @@ -158,20 +158,12 @@ int32_t InitDomainFlowCtrl() return 0; } -static long long TimespecSub(struct timespec a, struct timespec b) -{ - long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec; - - ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec; - return ret; -} - int FlowCtrlDomain(HilogMsg* hilogMsg) { if (hilogMsg->type == LOG_APP || !IsDomainSwitchOn() || IsDebugOn()) { return 0; } - struct timespec tsNow = { 0, 0 }; + LogTimeStamp tsNow(0, 0); std::unordered_map::iterator it; uint32_t domain = hilogMsg->domain; uint32_t domainId = (domain & DOMAIN_FILTER) >> DOMAIN_FILTER_SUBSYSTEM; @@ -179,9 +171,9 @@ int FlowCtrlDomain(HilogMsg* hilogMsg) int ret = 0; it = g_domainMap.find(domainId); if (it != g_domainMap.end()) { - clock_gettime(CLOCK_MONOTONIC, &tsNow); + LogTimeStamp tsNow(CLOCK_MONOTONIC); /* in statistic period(1 second) */ - if (TimespecSub(it->second->startTime, tsNow) < NSEC_PER_SEC) { + if ((it->second->startTime -= tsNow) < LogTimeStamp(1)) { if (it->second->sumLen <= it->second->domainQuota) { /* under quota */ it->second->sumLen += logLen; ret = 0; diff --git a/services/hilogd/include/log_reader.h b/services/hilogd/include/log_reader.h index 24290fa..75642dd 100644 --- a/services/hilogd/include/log_reader.h +++ b/services/hilogd/include/log_reader.h @@ -56,7 +56,6 @@ class LogReader : public std::enable_shared_from_this { public: std::list::iterator readPos; std::list::iterator lastPos; - std::list oldData; QueryCondition queryCondition; std::unique_ptr hilogtoolConnectSocket; bool isNotified; diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index e40645e..488fa8f 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -37,6 +37,7 @@ static int g_maxBufferSizeByType[LOG_TYPE_MAX] = {262144, 262144, 262144, 262144 const int DOMAIN_STRICT_MASK = 0xd000000; const int DOMAIN_FUZZY_MASK = 0xdffff; const int DOMAIN_MODULE_BITS = 8; +const int MAX_TIME_DIFF = 5; HilogBuffer::HilogBuffer() { @@ -96,25 +97,19 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) // Insert new log into HilogBuffer std::list::reverse_iterator rit = hilogDataList.rbegin(); + std::list::reverse_iterator ritEnd = hilogDataList.rend(); LogTimeStamp msgTimeStamp(msg.tv_sec, msg.tv_nsec); LogTimeStamp ritTimeStamp(rit->tv_sec, rit->tv_nsec); - LogTimeStamp measureTimeStamp(rit->tv_sec, rit->tv_nsec); - if (msgTimeStamp >= ritTimeStamp) { + LogTimeStamp measureTimeStamp(ritEnd->tv_sec, ritEnd->tv_nsec); + if (msgTimeStamp >= ritTimeStamp || msgTimeStamp < measureTimeStamp || + (ritTimeStamp -= msgTimeStamp) > LogTimeStamp(MAX_TIME_DIFF)) { hilogDataList.emplace_back(msg); } else { // Find the place with right timestamp ++rit; ritTimeStamp.SetTimeStamp(rit->tv_sec, rit->tv_nsec); - for (; rit != hilogDataList.rend() && (msgTimeStamp < measureTimeStamp - || (ritTimeStamp -= msgTimeStamp) > LogTimeStamp(5)); ++rit) { + for (; rit != hilogDataList.rend() && (msgTimeStamp < ritTimeStamp); ++rit) { ritTimeStamp.SetTimeStamp(rit->tv_sec, rit->tv_nsec); - logReaderListMutex.lock_shared(); - for (auto &itr :logReaderList) { - if (itr.lock()->readPos == std::prev(rit.base())) { - hilogDataList.emplace_front(msg); - } - } - logReaderListMutex.unlock_shared(); } hilogDataList.emplace(rit.base(), msg); } diff --git a/services/hilogd/log_reader.cpp b/services/hilogd/log_reader.cpp index a34e34e..b3c7b17 100644 --- a/services/hilogd/log_reader.cpp +++ b/services/hilogd/log_reader.cpp @@ -29,7 +29,6 @@ using namespace std; HilogBuffer* LogReader::hilogBuffer = nullptr; LogReader::LogReader() { - oldData = {}; isNotified = false; } -- Gitee From ead6e30f89ef8a93e6abe0247a9e44fde3ea05c7 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Thu, 9 Dec 2021 07:40:18 +0000 Subject: [PATCH 6/7] now -start Signed-off-by: aajwy <13051180828@163.com> --- frameworks/native/hilog_printf.cpp | 6 +++--- frameworks/native/include/log_time_stamp.h | 1 - services/hilogd/flow_control_init.cpp | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/frameworks/native/hilog_printf.cpp b/frameworks/native/hilog_printf.cpp index 98d1e0d..aee30ad 100644 --- a/frameworks/native/hilog_printf.cpp +++ b/frameworks/native/hilog_printf.cpp @@ -137,7 +137,7 @@ static int HiLogFlowCtrlProcess(int len, uint16_t logType, bool debug) LogTimeStamp tsStart = atomic_load(&gStartTime); LogTimeStamp tsNow(CLOCK_MONOTONIC); /* in statistic period(1 second) */ - if ((tsStart -= tsNow) <= LogTimeStamp(1)) { + if ((tsNow -= tsStart) <= LogTimeStamp(1)) { uint32_t sumLen = (uint32_t)atomic_load(&gSumLen); if (sumLen > processQuota) { /* over quota, -1 means don't print */ atomic_fetch_add_explicit(&gDropped, 1, memory_order_relaxed); @@ -232,11 +232,11 @@ int HiLogPrintArgs(const LogType type, const LogLevel level, const unsigned int debug = IsDebugOn(); priv = (!debug) && IsPrivateSwitchOn(); #ifdef __clang__ -/*code specific to clang compiler*/ +/* code specific to clang compiler */ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wformat-nonliteral" #elif __GNUC__ -/*code for GNU C compiler */ +/* code for GNU C compiler */ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wformat-nonliteral" #endif diff --git a/frameworks/native/include/log_time_stamp.h b/frameworks/native/include/log_time_stamp.h index 43e69b3..baa41b4 100644 --- a/frameworks/native/include/log_time_stamp.h +++ b/frameworks/native/include/log_time_stamp.h @@ -16,7 +16,6 @@ #define LOG_TIME_STAMP_H #define NS_PER_SEC 1000000000ULL - class LogTimeStamp { public: diff --git a/services/hilogd/flow_control_init.cpp b/services/hilogd/flow_control_init.cpp index 606457d..caf40b9 100644 --- a/services/hilogd/flow_control_init.cpp +++ b/services/hilogd/flow_control_init.cpp @@ -173,7 +173,7 @@ int FlowCtrlDomain(HilogMsg* hilogMsg) if (it != g_domainMap.end()) { LogTimeStamp tsNow(CLOCK_MONOTONIC); /* in statistic period(1 second) */ - if ((it->second->startTime -= tsNow) < LogTimeStamp(1)) { + if ((tsNow -= it->second->startTime) < LogTimeStamp(1)) { if (it->second->sumLen <= it->second->domainQuota) { /* under quota */ it->second->sumLen += logLen; ret = 0; -- Gitee From 3dd8c2bda6c5b3f1fea459b6e1d1b8141dd051ea Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Thu, 9 Dec 2021 09:46:45 +0000 Subject: [PATCH 7/7] code check Signed-off-by: aajwy <13051180828@163.com> --- frameworks/native/include/log_time_stamp.h | 30 ++++++++++++++-------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/frameworks/native/include/log_time_stamp.h b/frameworks/native/include/log_time_stamp.h index baa41b4..4c8df97 100644 --- a/frameworks/native/include/log_time_stamp.h +++ b/frameworks/native/include/log_time_stamp.h @@ -22,7 +22,8 @@ public: LogTimeStamp() = default; ~LogTimeStamp() = default; #ifdef __linux__ - explicit LogTimeStamp(clockid_t id) { + explicit LogTimeStamp(clockid_t id) + { timespec time; clock_gettime(id, &time); tv_sec = static_cast(time.tv_sec); @@ -35,27 +36,34 @@ public: explicit LogTimeStamp(uint32_t sec, uint32_t nsec = 0) : tv_sec(sec), tv_nsec(nsec) {}; /* LogTimeStamp */ - bool operator == (const LogTimeStamp& time) const { + bool operator == (const LogTimeStamp& time) const + { return (tv_sec == time.tv_sec) && (tv_nsec == time.tv_nsec); } - bool operator != (const LogTimeStamp& time) const { + bool operator != (const LogTimeStamp& time) const + { return !(*this == time); } - bool operator < (const LogTimeStamp& time) const { + bool operator < (const LogTimeStamp& time) const + { return (tv_sec < time.tv_sec) || ((tv_sec == time.tv_sec) && (tv_nsec < time.tv_nsec)); } - bool operator >= (const LogTimeStamp& time) const { + bool operator >= (const LogTimeStamp& time) const + { return !(*this < time); } - bool operator > (const LogTimeStamp& time) const { + bool operator > (const LogTimeStamp& time) const + { return (tv_sec > time.tv_sec) || ((tv_sec == time.tv_sec) && (tv_nsec > time.tv_nsec)); } - bool operator <= (const LogTimeStamp& time) const { + bool operator <= (const LogTimeStamp& time) const + { return !(*this > time); } - LogTimeStamp operator -= (const LogTimeStamp& time) { + LogTimeStamp operator -= (const LogTimeStamp& time) + { if (*this <= time) { return *this = LogTimeStamp(epoch); } @@ -68,7 +76,8 @@ public: this->tv_sec -= time.tv_sec; return *this; } - LogTimeStamp operator += (const LogTimeStamp& time) { + LogTimeStamp operator += (const LogTimeStamp& time) + { this->tv_nsec += time.tv_nsec; if (this->tv_nsec >= NS_PER_SEC) { this->tv_nsec -= NS_PER_SEC; @@ -77,7 +86,8 @@ public: this->tv_sec += time.tv_sec; return *this; } - void SetTimeStamp(uint32_t sec, uint32_t nsec) { + void SetTimeStamp(uint32_t sec, uint32_t nsec) + { this->tv_sec = sec; this->tv_nsec = nsec; } -- Gitee