From 3d55600fc3be4968668d43d890a5271a5893f7c3 Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Wed, 15 Dec 2021 02:20:34 -0500 Subject: [PATCH 01/28] statistics info & property Signed-off-by: youyouyuai --- adapter/BUILD_bak.gn | 42 ++ adapter/properties_bak.cpp | 580 ++++++++++++++++++++ frameworks/native/include/hilogtool_msg.h | 10 +- services/hilogd/BUILD.gn | 1 + services/hilogd/flow_control_init.cpp | 117 +++- services/hilogd/include/flow_control_init.h | 8 +- services/hilogd/include/log_buffer.h | 12 +- services/hilogd/include/statistics.h | 47 ++ services/hilogd/log_buffer.cpp | 78 ++- services/hilogd/log_querier.cpp | 73 ++- services/hilogd/statistics.cpp | 158 ++++++ services/hilogtool/log_controller.cpp | 4 +- services/hilogtool/log_display.cpp | 39 +- 13 files changed, 1101 insertions(+), 68 deletions(-) create mode 100644 adapter/BUILD_bak.gn create mode 100644 adapter/properties_bak.cpp create mode 100644 services/hilogd/include/statistics.h create mode 100644 services/hilogd/statistics.cpp diff --git a/adapter/BUILD_bak.gn b/adapter/BUILD_bak.gn new file mode 100644 index 0000000..85cc4b1 --- /dev/null +++ b/adapter/BUILD_bak.gn @@ -0,0 +1,42 @@ +# 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. + +import("//build/ohos.gni") + +ohos_shared_library("libhilog_os_adapter") { + sources = [ + "properties.cpp", + "socket_server_adapter.cpp", + ] + + deps = [ + "//utils/native/base:utilsecurec_shared", + #"//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", + ] + + defines = [ "USING_EXISTING_SOCKET" ] + + include_dirs = [ + "//base/hiviewdfx/hilog/interfaces/native/innerkits/include", + "//base/hiviewdfx/hilog/adapter", + "//base/startup/syspara_lite/interfaces/innerkits/native/syspara/include", + ] + + install_enable = true + part_name = "hilog_native" + subsystem_name = "hiviewdfx" + install_images = [ + "system", + "updater", + ] +} diff --git a/adapter/properties_bak.cpp b/adapter/properties_bak.cpp new file mode 100644 index 0000000..9825109 --- /dev/null +++ b/adapter/properties_bak.cpp @@ -0,0 +1,580 @@ +/* + * 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. + */ + +#include "properties.h" +#include "hilog/log.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +using ReadLock = shared_lock; +using InsertLock = unique_lock; + + +static pthread_mutex_t g_globalLevelLock = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t g_tagLevelLock = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t g_domainLevelLock = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t g_debugLock = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t g_persistDebugLock = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t g_privateLock = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t g_processFlowLock = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t g_domainFlowLock = PTHREAD_MUTEX_INITIALIZER; + +using PropertyCache = struct { + const void* pinfo; + uint32_t serial; + char propertyValue[HILOG_PROP_VALUE_MAX]; +}; + +/* +using SwitchCache = struct { + PropertyCache cache; + bool isOn; +}; + +using LogLevelCache = struct { + PropertyCache cache; + uint16_t logLevel; +}; + +using ProcessInfo = struct { + PropertyCache cache; + string propertyKey; + string process; + string processHashPre; + uint32_t processQuota; +}; +*/ + +using ContextInfo = struct { + uint32_t propType; + bool switchOn; + uint16_t logLevel; + std::string key; + char value[HILOG_PROP_VALUE_MAX]; +}; + +void PropertyGet(const string &key, char *value, int len) +{ + if (len < HILOG_PROP_VALUE_MAX) { + return; + } + /* use OHOS interface */ + GetParameter(key.c_str(), nullptr, value, HILOG_PROP_VALUE_MAX); + +} + +void PropertySet(const string &key, const char* value) +{ + /* use OHOS interface */ + SetParameter(key.c_str(), value); +} + +string GetProgName() +{ + return program_invocation_short_name; /* use HOS interface */ +} + +string GetPropertyName(uint32_t propType) +{ + string key; + switch (propType) { + case PROP_PRIVATE: + key = "hilog.private.on"; + break; + case PROP_PROCESS_FLOWCTRL: + key = "hilog.flowctrl.pid.on"; + break; + case PROP_DOMAIN_FLOWCTRL: + key = "hilog.flowctrl.domain.on"; + break; + case PROP_GLOBAL_LOG_LEVEL: + key = "hilog.loggable.global"; + break; + case PROP_DOMAIN_LOG_LEVEL: + key = "hilog.loggable.domain."; + break; + case PROP_TAG_LOG_LEVEL: + key = "hilog.loggable.tag."; + break; + case PROP_SINGLE_DEBUG: + key = "hilog.debug.on"; + break; + case PROP_PERSIST_DEBUG: + key = "persist.sys.hilog.debug.on"; + break; + default: + break; + } + return key; +} + +static int LockByProp(uint32_t propType) +{ + switch (propType) { + case PROP_PRIVATE: + return pthread_mutex_trylock(&g_privateLock); + case PROP_PROCESS_FLOWCTRL: + return pthread_mutex_trylock(&g_processFlowLock); + case PROP_DOMAIN_FLOWCTRL: + return pthread_mutex_trylock(&g_domainFlowLock); + case PROP_GLOBAL_LOG_LEVEL: + return pthread_mutex_trylock(&g_globalLevelLock); + case PROP_DOMAIN_LOG_LEVEL: + return pthread_mutex_trylock(&g_domainLevelLock); + case PROP_TAG_LOG_LEVEL: + return pthread_mutex_trylock(&g_tagLevelLock); + case PROP_SINGLE_DEBUG: + return pthread_mutex_trylock(&g_debugLock); + case PROP_PERSIST_DEBUG: + return pthread_mutex_trylock(&g_persistDebugLock); + default: + return -1; + } +} + +static void UnlockByProp(uint32_t propType) +{ + switch (propType) { + case PROP_PRIVATE: + pthread_mutex_unlock(&g_privateLock); + break; + case PROP_PROCESS_FLOWCTRL: + pthread_mutex_unlock(&g_processFlowLock); + break; + case PROP_DOMAIN_FLOWCTRL: + pthread_mutex_unlock(&g_domainFlowLock); + break; + case PROP_GLOBAL_LOG_LEVEL: + pthread_mutex_unlock(&g_globalLevelLock); + break; + case PROP_DOMAIN_LOG_LEVEL: + pthread_mutex_unlock(&g_domainLevelLock); + break; + case PROP_TAG_LOG_LEVEL: + pthread_mutex_unlock(&g_tagLevelLock); + break; + case PROP_SINGLE_DEBUG: + pthread_mutex_unlock(&g_debugLock); + break; + case PROP_PERSIST_DEBUG: + pthread_mutex_unlock(&g_persistDebugLock); + break; + default: + break; + } +} +#if 0 +static void RefreshCacheBuf(PropertyCache *cache, const char *key) +{ + /* use OHOS interface */ +} + +static bool CheckCache(const PropertyCache *cache) +{ + /* use OHOS interface */ + return true; +} + +/* + check & refresh +*/ +#endif + +/* +static bool GetSwitchCache(bool isFirst, SwitchCache& switchCache, uint32_t propType, bool defaultValue) +{ + int notLocked; + string key = GetPropertyName(propType); + + if (isFirst || CheckCache(&switchCache.cache)) { + notLocked = LockByProp(propType); + if (!notLocked) { + RefreshCacheBuf(&switchCache.cache, key.c_str()); + if (strcmp(switchCache.cache.propertyValue, "true") == 0) { + switchCache.isOn = true; + } else if (strcmp(switchCache.cache.propertyValue, "false") == 0) { + switchCache.isOn = false; + } else { + switchCache.isOn = defaultValue; + } + UnlockByProp(propType); + return switchCache.isOn; + } else { + SwitchCache tmpCache = {{nullptr, 0xffffffff, ""}, defaultValue}; + RefreshCacheBuf(&tmpCache.cache, key.c_str()); + if (strcmp(tmpCache.cache.propertyValue, "true") == 0) { + tmpCache.isOn = true; + } else if (strcmp(tmpCache.cache.propertyValue, "false") == 0) { + tmpCache.isOn = false; + } else { + tmpCache.isOn = defaultValue; + } + return tmpCache.isOn; + } + } else { + return switchCache.isOn; + } +} +*/ +static bool GetSwitchCache(const char* propValue, bool defaultSwitch) +{ + if (strcmp(propValue, "true") == 0) { + return true; + } else if (strcmp(propValue, "false") == 0) { + return false; + } else { + return defaultSwitch; + } +} + + +static uint16_t GetCacheLevel(char propertyChar, uint16_t defaultLevel) +{ + uint16_t cacheLevel = defaultLevel; + switch (propertyChar) { + case 'D': + case 'd': + cacheLevel = LOG_DEBUG; + break; + case 'I': + case 'i': + cacheLevel = LOG_INFO; + break; + case 'W': + case 'w': + cacheLevel = LOG_WARN; + break; + case 'E': + case 'e': + cacheLevel = LOG_ERROR; + break; + case 'F': + case 'f': + cacheLevel = LOG_FATAL; + break; + default: + break; + } + return cacheLevel; +} + +static void ChangeCallback(const char *key, const char *value, void *context) +{ + int notLocked; + ContextInfo* contextInfo = reinterpret_cast(context); + notLocked = LockByProp(contextInfo->propType); + if (!notLocked && strcmp(key, contextInfo->key.c_str()) == 0) { + if (strncpy_s(contextInfo->value, HILOG_PROP_VALUE_MAX, value, strlen(value))) { + UnlockByProp(contextInfo->propType); + return; + } + switch (contextInfo->propType) { + case PROP_PRIVATE: + contextInfo->switchOn = GetSwitchCache(contextInfo->value, true); + break; + case PROP_PROCESS_FLOWCTRL: + contextInfo->switchOn = GetSwitchCache(contextInfo->value, false); + break; + case PROP_DOMAIN_FLOWCTRL: + contextInfo->switchOn = GetSwitchCache(contextInfo->value, false); + break; + case PROP_GLOBAL_LOG_LEVEL: + contextInfo->logLevel = GetCacheLevel(contextInfo->value[0], LOG_LEVEL_MIN); + break; + case PROP_DOMAIN_LOG_LEVEL: + contextInfo->logLevel = GetCacheLevel(contextInfo->value[0], LOG_LEVEL_MIN); + break; + case PROP_TAG_LOG_LEVEL: + contextInfo->logLevel = GetCacheLevel(contextInfo->value[0], LOG_LEVEL_MIN); + break; + case PROP_SINGLE_DEBUG: + contextInfo->logLevel = GetSwitchCache(contextInfo->value, false); + break; + case PROP_PERSIST_DEBUG: + contextInfo->logLevel = GetSwitchCache(contextInfo->value, false); + break; + } + UnlockByProp(contextInfo->propType); + } +} + +static void WatchProp(const std::string& keyprefix, ContextInfo& contextInfo) +{ + WatchParameter(keyprefix.c_str(), ChangeCallback, reinterpret_cast(&contextInfo)); +} + +bool IsDebugOn() +{ + return IsSingleDebugOn() || IsPersistDebugOn(); +} +/* +bool IsSingleDebugOn() +{ + static SwitchCache *switchCache = new SwitchCache {{nullptr, 0xffffffff, ""}, false}; + static atomic_flag isFirstFlag = ATOMIC_FLAG_INIT; + bool isFirst = !isFirstFlag.test_and_set(); + return GetSwitchCache(isFirst, *switchCache, PROP_SINGLE_DEBUG, false); +} + +bool IsPersistDebugOn() +{ + static SwitchCache *switchCache = new SwitchCache {{nullptr, 0xffffffff, ""}, false}; + static atomic_flag isFirstFlag = ATOMIC_FLAG_INIT; + bool isFirst = !isFirstFlag.test_and_set(); + return GetSwitchCache(isFirst, *switchCache, PROP_PERSIST_DEBUG, false); +} + +bool IsPrivateSwitchOn() +{ + static SwitchCache *switchCache = new SwitchCache {{nullptr, 0xffffffff, ""}, true}; + static atomic_flag isFirstFlag = ATOMIC_FLAG_INIT; + bool isFirst = !isFirstFlag.test_and_set(); + return GetSwitchCache(isFirst, *switchCache, PROP_PRIVATE, true); +} + +bool IsProcessSwitchOn() +{ + static SwitchCache *switchCache = new SwitchCache {{nullptr, 0xffffffff, ""}, false}; + static atomic_flag isFirstFlag = ATOMIC_FLAG_INIT; + bool isFirst = !isFirstFlag.test_and_set(); + return GetSwitchCache(isFirst, *switchCache, PROP_PROCESS_FLOWCTRL, false); +} + +bool IsDomainSwitchOn() +{ + static SwitchCache *switchCache = new SwitchCache {{nullptr, 0xffffffff, ""}, false}; + static atomic_flag isFirstFlag = ATOMIC_FLAG_INIT; + bool isFirst = !isFirstFlag.test_and_set(); + return GetSwitchCache(isFirst, *switchCache, PROP_DOMAIN_FLOWCTRL, false); +} +*/ +bool IsSingleDebugOn() +{ + std::string key = GetPropertyName(PROP_SINGLE_DEBUG); + static ContextInfo *contextInfo = new ContextInfo{PROP_SINGLE_DEBUG, false, LOG_LEVEL_MIN, key, ""}; + WatchProp(key, *contextInfo); + return contextInfo->switchOn; +} + +bool IsPersistDebugOn() +{ + std::string key = GetPropertyName(PROP_PERSIST_DEBUG); + static ContextInfo *contextInfo = new ContextInfo{PROP_PERSIST_DEBUG, false, LOG_LEVEL_MIN, key, ""}; + WatchProp(key, *contextInfo); + return contextInfo->switchOn; +} + +bool IsPrivateSwitchOn() +{ + std::string key = GetPropertyName(PROP_PRIVATE); + static ContextInfo *contextInfo = new ContextInfo{PROP_PRIVATE, true, LOG_LEVEL_MIN, key, ""}; + WatchProp(key, *contextInfo); + return contextInfo->switchOn; +} + +bool IsProcessSwitchOn() +{ + std::string key = GetPropertyName(PROP_PROCESS_FLOWCTRL); + static ContextInfo *contextInfo = new ContextInfo{PROP_PROCESS_FLOWCTRL, false, LOG_LEVEL_MIN, key, ""}; + WatchProp(key, *contextInfo); + return contextInfo->switchOn; +} + +bool IsDomainSwitchOn() +{ + std::string key = GetPropertyName(PROP_DOMAIN_FLOWCTRL); + static ContextInfo *contextInfo = new ContextInfo{PROP_DOMAIN_FLOWCTRL, false, LOG_LEVEL_MIN, key, ""}; + WatchProp(key, *contextInfo); + return contextInfo->switchOn; +} + +/* +uint16_t GetGlobalLevel() +{ + string key = GetPropertyName(PROP_GLOBAL_LOG_LEVEL); + static LogLevelCache *levelCache = new LogLevelCache{{nullptr, 0xffffffff, ""}, LOG_LEVEL_MIN}; + static atomic_flag isFirstFlag = ATOMIC_FLAG_INIT; + int notLocked; + + if (!isFirstFlag.test_and_set() || CheckCache(&levelCache->cache)) { + notLocked = LockByProp(PROP_GLOBAL_LOG_LEVEL); + if (!notLocked) { + RefreshCacheBuf(&levelCache->cache, key.c_str()); + levelCache->logLevel = GetCacheLevel(levelCache->cache.propertyValue[0]); + UnlockByProp(PROP_GLOBAL_LOG_LEVEL); + return levelCache->logLevel; + } else { + LogLevelCache tmpCache = {{nullptr, 0xffffffff, ""}, LOG_LEVEL_MIN}; + RefreshCacheBuf(&tmpCache.cache, key.c_str()); + tmpCache.logLevel = GetCacheLevel(tmpCache.cache.propertyValue[0]); + return tmpCache.logLevel; + } + } else { + return levelCache->logLevel; + } +} + +uint16_t GetDomainLevel(uint32_t domain) +{ + static unordered_map *domainMap = new unordered_map(); + unordered_map::iterator it; + string key = GetPropertyName(PROP_DOMAIN_LOG_LEVEL) + to_string(domain); + + static shared_timed_mutex* mtx = new shared_timed_mutex; + { + ReadLock lock(*mtx); + it = domainMap->find(domain); + } + if (it == domainMap->end()) { // new domain + InsertLock lock(*mtx); + LogLevelCache* levelCache = new LogLevelCache{{nullptr, 0xffffffff, ""}, LOG_LEVEL_MIN}; + RefreshCacheBuf(&levelCache->cache, key.c_str()); + levelCache->logLevel = GetCacheLevel(levelCache->cache.propertyValue[0]); + uint16_t lvl = levelCache->logLevel; + pair::iterator, bool> ret = domainMap->insert({ domain, levelCache }); + if (!ret.second) { + delete levelCache; + levelCache = nullptr; + } + return lvl; + } else { // existed domain + if (CheckCache(&it->second->cache)) { // changed + InsertLock lock(*mtx); + RefreshCacheBuf(&it->second->cache, key.c_str()); + it->second->logLevel = GetCacheLevel(it->second->cache.propertyValue[0]); + return it->second->logLevel; + } else { // not changed + return it->second->logLevel; + } + } +} + +uint16_t GetTagLevel(const string& tag) +{ + static unordered_map *tagMap = new unordered_map(); + unordered_map::iterator it; + string key = GetPropertyName(PROP_TAG_LOG_LEVEL) + tag; + + static shared_timed_mutex* mtx = new shared_timed_mutex; + { + ReadLock lock(*mtx); + it = tagMap->find(tag); + } + if (it == tagMap->end()) { // new tag + InsertLock lock(*mtx); + LogLevelCache* levelCache = new LogLevelCache{{nullptr, 0xffffffff, ""}, LOG_LEVEL_MIN}; + RefreshCacheBuf(&levelCache->cache, key.c_str()); + levelCache->logLevel = GetCacheLevel(levelCache->cache.propertyValue[0]); + uint16_t lvl = levelCache->logLevel; + pair::iterator, bool> ret = tagMap->insert({ tag, levelCache }); + if (!ret.second) { + delete(levelCache); + levelCache = nullptr; + } + return lvl; + } else { // existed tag + if (CheckCache(&it->second->cache)) { // changed + InsertLock lock(*mtx); + RefreshCacheBuf(&it->second->cache, key.c_str()); + it->second->logLevel = GetCacheLevel(it->second->cache.propertyValue[0]); + return it->second->logLevel; + } else { // not changed + return it->second->logLevel; + } + } +} +*/ +uint16_t GetGlobalLevel() +{ + string key = GetPropertyName(PROP_GLOBAL_LOG_LEVEL); + static ContextInfo *contextInfo = new ContextInfo{PROP_GLOBAL_LOG_LEVEL, false, LOG_LEVEL_MIN, key, ""}; + WatchProp(key, *contextInfo); + return contextInfo->logLevel; +} + +uint16_t GetDomainLevel(uint32_t domain) +{ + static unordered_map *domainMap = new unordered_map(); + unordered_map::iterator it; + string key = GetPropertyName(PROP_DOMAIN_LOG_LEVEL) + to_string(domain); + + static shared_timed_mutex* mtx = new shared_timed_mutex; + { + ReadLock lock(*mtx); + it = domainMap->find(domain); + } + if (it == domainMap->end()) { // new domain + ContextInfo* contextInfo = new ContextInfo{PROP_DOMAIN_LOG_LEVEL, false, LOG_LEVEL_MIN, key, ""}; + WatchProp(key, *contextInfo); + uint16_t lvl = contextInfo->logLevel; + InsertLock lock(*mtx); + pair::iterator, bool> ret = domainMap->insert({ domain, contextInfo }); + if (!ret.second) { + delete contextInfo; + contextInfo = nullptr; + } + return lvl; + } else { // existed domain + WatchProp(key, *(it->second)); + return it->second->logLevel; + } +} + +uint16_t GetTagLevel(const string& tag) +{ + static unordered_map *tagMap = new unordered_map(); + unordered_map::iterator it; + string key = GetPropertyName(PROP_TAG_LOG_LEVEL) + tag; + + static shared_timed_mutex* mtx = new shared_timed_mutex; + { + ReadLock lock(*mtx); + it = tagMap->find(tag); + } + if (it == tagMap->end()) { // new tag + ContextInfo* contextInfo = new ContextInfo{PROP_TAG_LOG_LEVEL, false, LOG_LEVEL_MIN, key, ""}; + WatchProp(key, *contextInfo); + uint16_t lvl = contextInfo->logLevel; + InsertLock lock(*mtx); + pair::iterator, bool> ret = tagMap->insert({ tag, contextInfo }); + if (!ret.second) { + delete(contextInfo); + contextInfo = nullptr; + } + return lvl; + } else { // existed tag + WatchProp(key, *(it->second)); + return it->second->logLevel; + } +} \ No newline at end of file diff --git a/frameworks/native/include/hilogtool_msg.h b/frameworks/native/include/hilogtool_msg.h index bf3b9f9..ff79a23 100644 --- a/frameworks/native/include/hilogtool_msg.h +++ b/frameworks/native/include/hilogtool_msg.h @@ -197,13 +197,17 @@ typedef struct { } StatisticInfoQueryRequest; typedef struct { - MessageHeader msgHeader; int32_t result; uint16_t logType; uint32_t domain; - uint64_t printLen; - uint64_t cacheLen; + int64_t printLen; + int64_t cacheLen; int32_t dropped; +} StatisticInfoResult; + +typedef struct { + MessageHeader msgHeader; + StatisticInfoResult statisticInfoRst[]; } StatisticInfoQueryResponse; typedef struct { diff --git a/services/hilogd/BUILD.gn b/services/hilogd/BUILD.gn index e241cd5..6931def 100644 --- a/services/hilogd/BUILD.gn +++ b/services/hilogd/BUILD.gn @@ -31,6 +31,7 @@ ohos_executable("hilogd") { "log_querier.cpp", "log_reader.cpp", "main.cpp", + "statistics.cpp", ] configs = [ ":hilogd_config" ] defines = [ "__RECV_MSG_WITH_UCRED_" ] diff --git a/services/hilogd/flow_control_init.cpp b/services/hilogd/flow_control_init.cpp index 5f4f47e..931ef12 100644 --- a/services/hilogd/flow_control_init.cpp +++ b/services/hilogd/flow_control_init.cpp @@ -24,7 +24,7 @@ #include #include #include "properties.h" - +#include "statistics.h" namespace OHOS { namespace HiviewDFX { static const int DOMAIN_FILTER = 0x00fffff; @@ -41,26 +41,75 @@ using DomainInfo = struct { }; static std::unordered_map g_domainMap; - +#if 0 static int32_t g_typeDropped[LOG_TYPE_MAX]; static std::unordered_map g_domainDropped; -void ClearDroppedByType() +static uint32_t g_typePrintLen[LOG_TYPE_MAX]; +static std::unordered_map g_domainPrintLen; + +void InitDropped() +{ + int i; + std::unordered_map::iterator it; + for (i = 0; i < LOG_TYPE_MAX; i++) { + g_typeDropped[i] = 0; + } + for (it = g_domainDropped.begin(); it != g_domainDropped.end(); ++it) { + it->second = 0; + } +} + +void InitPrintLen() { + int i; + std::unordered_map::iterator it; + for (i = 0; i < LOG_TYPE_MAX; i++) { + g_typePrintLen[i] = 0; + } + for (it = g_domainPrintLen.begin(); it != g_domainPrintLen.end(); ++it) { + it->second = 0; + } +} + +void ClearDroppedByType(uint16_t logType) +{ + /* int i; for (i = 0; i < LOG_TYPE_MAX; i++) { g_typeDropped[i] = 0; } + */ + if (logType >= LOG_TYPE_MAX) { + return; + } + g_typeDropped[logType] = 0; } -void ClearDroppedByDomain() +void ClearDroppedByDomain(uint32_t domainId) { + /* std::unordered_map::iterator it; for (it = g_domainDropped.begin(); it != g_domainDropped.end(); ++it) { it->second = 0; } + */ + g_domainDropped[domainId] = 0; } +void ClearPrintByType(uint16_t logType) +{ + if (logType >= LOG_TYPE_MAX) { + return; + } + g_typePrintLen[logType] = 0; +} + +void ClearPrintByDomain(uint32_t domainId) +{ + g_domainPrintLen[domainId] = 0; +} +/* void IncreaseDropped(uint32_t domainId, uint16_t logType) { std::unordered_map::iterator it; @@ -72,9 +121,12 @@ void IncreaseDropped(uint32_t domainId, uint16_t logType) g_domainDropped.insert({ domainId, 1 }); } } - +*/ int32_t GetDroppedByType(uint16_t logType) { + if (logType >= LOG_TYPE_MAX) { + return 0; + } return g_typeDropped[logType]; } @@ -86,7 +138,36 @@ int32_t GetDroppedByDomain(uint32_t domainId) } return 0; } +/* +void IncreasePrint(uint32_t domainId, uint16_t logType) +{ + std::unordered_map::iterator it; + g_typePrintLen[logType]++; + it = g_domainPrintLen.find(domainId); + if (it != g_domainPrintLen.end()) { + it->second++; + } else { + g_domainPrintLen.insert({ domainId, 1 }); + } +} +*/ +uint32_t GetPrintByType(uint16_t logType) +{ + if (logType >= LOG_TYPE_MAX) { + return 0; + } + return g_typePrintLen[logType]; +} +uint32_t GetPrintByDomain(uint32_t domainId) +{ + std::unordered_map::iterator it = g_domainPrintLen.find(domainId); + if (it != g_domainPrintLen.end()) { + return it->second; + } + return 0; +} +#endif void ParseDomainQuota(std::string &domainStr) { if (domainStr.empty() || domainStr.at(0) == '#') { @@ -139,6 +220,9 @@ void ParseDomainQuota(std::string &domainStr) int32_t InitDomainFlowCtrl() { + //InitDropped(); + //InitPrintLen(); + static constexpr char domainFile[] = "/system/etc/hilog_domains.conf"; std::ifstream ifs(domainFile, std::ifstream::in); if (!ifs.is_open()) { @@ -153,8 +237,8 @@ int32_t InitDomainFlowCtrl() ParseDomainQuota(line); } ifs.close(); - ClearDroppedByType(); - ClearDroppedByDomain(); + //ClearDroppedByType(); + //ClearDroppedByDomain(); return 0; } @@ -168,13 +252,22 @@ static long long TimespecSub(struct timespec a, struct timespec b) int FlowCtrlDomain(HilogMsg* hilogMsg) { + uint32_t domain = hilogMsg->domain; + uint32_t domainId = (domain & DOMAIN_FILTER) >> DOMAIN_FILTER_SUBSYSTEM; + //IncreasePrint(domainId, hilogMsg->type); + StatisticInfo info; + info.dropped = 0; + info.printLen = hilogMsg->len; + info.cacheLen = 0; + UpdateStatisticInfo(hilogMsg->type, domain, info); + if (hilogMsg->type == LOG_APP || !IsDomainSwitchOn() || IsDebugOn()) { return 0; } struct timespec tsNow = { 0, 0 }; std::unordered_map::iterator it; - uint32_t domain = hilogMsg->domain; - uint32_t domainId = (domain & DOMAIN_FILTER) >> DOMAIN_FILTER_SUBSYSTEM; + //uint32_t domain = hilogMsg->domain; + //uint32_t domainId = (domain & DOMAIN_FILTER) >> DOMAIN_FILTER_SUBSYSTEM; int logLen = hilogMsg->len - sizeof(HilogMsg) - 1 - 1; /* quota length exclude '\0' of tag and log content */ int ret = 0; it = g_domainMap.find(domainId); @@ -186,7 +279,11 @@ int FlowCtrlDomain(HilogMsg* hilogMsg) it->second->sumLen += logLen; ret = 0; } else { /* over quota */ - IncreaseDropped(domainId, hilogMsg->type); + //IncreaseDropped(domainId, hilogMsg->type); + info.dropped = 1; + info.printLen = 0; + info.cacheLen = 0; + UpdateStatisticInfo(hilogMsg->type, domain, info); it->second->dropped++; ret = -1; } diff --git a/services/hilogd/include/flow_control_init.h b/services/hilogd/include/flow_control_init.h index 4278202..b8f591f 100644 --- a/services/hilogd/include/flow_control_init.h +++ b/services/hilogd/include/flow_control_init.h @@ -24,8 +24,12 @@ int32_t InitDomainFlowCtrl(); int FlowCtrlDomain(HilogMsg* hilogMsg); int32_t GetDroppedByType(uint16_t logType); int32_t GetDroppedByDomain(uint32_t domainId); -void ClearDroppedByType(); -void ClearDroppedByDomain(); +uint32_t GetPrintByType(uint16_t logType); +uint32_t GetPrintByDomain(uint32_t domainId); +void ClearDroppedByType(uint16_t logType); +void ClearDroppedByDomain(uint32_t domainId); +void ClearPrintByType(uint16_t logType); +void ClearPrintByDomain(uint32_t domainId); } } #endif diff --git a/services/hilogd/include/log_buffer.h b/services/hilogd/include/log_buffer.h index a0ad2dd..60df86d 100644 --- a/services/hilogd/include/log_buffer.h +++ b/services/hilogd/include/log_buffer.h @@ -43,10 +43,12 @@ public: size_t Delete(uint16_t logType); size_t GetBuffLen(uint16_t logType); size_t SetBuffLen(uint16_t logType, uint64_t buffSize); + /* int32_t GetStatisticInfoByLog(uint16_t logType, uint64_t& printLen, uint64_t& cacheLen, int32_t& dropped); int32_t GetStatisticInfoByDomain(uint32_t domain, uint64_t& printLen, uint64_t& cacheLen, int32_t& dropped); int32_t ClearStatisticInfoByLog(uint16_t logType); int32_t ClearStatisticInfoByDomain(uint32_t domain); + */ void GetBufferLock(); void ReleaseBufferLock(); private: @@ -55,11 +57,11 @@ private: std::list hilogDataList; std::shared_mutex hilogBufferMutex; std::map cacheLenByDomain; - std::map printLenByDomain; - std::map droppedByDomain; - uint64_t cacheLenByType[LOG_TYPE_MAX]; - uint64_t droppedByType[LOG_TYPE_MAX]; - uint64_t printLenByType[LOG_TYPE_MAX]; + //std::map printLenByDomain; + //std::map droppedByDomain; + //uint64_t cacheLenByType[LOG_TYPE_MAX]; + //uint64_t droppedByType[LOG_TYPE_MAX]; + //uint64_t printLenByType[LOG_TYPE_MAX]; bool ConditionMatch(std::shared_ptr reader); void ReturnNoLog(std::shared_ptr reader); }; diff --git a/services/hilogd/include/statistics.h b/services/hilogd/include/statistics.h new file mode 100644 index 0000000..b09cc3b --- /dev/null +++ b/services/hilogd/include/statistics.h @@ -0,0 +1,47 @@ +/* + * 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 STATISTICS_H +#define STATISTICS_H + +#include +#include "hilog_common.h" +#include + + +namespace OHOS { +namespace HiviewDFX { + +using StatisticInfo = struct { + int32_t dropped; + int64_t printLen; + int64_t cacheLen; +}; + + + +//Statistics(); +//~Statistics(); +int32_t GetStatisticInfoByLog(uint16_t logType, StatisticInfo& result); +int32_t GetStatisticInfoByDomain(uint32_t domain, StatisticInfo& result); +int32_t ClearStatisticInfoByLog(uint16_t logType); +int32_t ClearStatisticInfoByDomain(uint32_t domain); +int32_t GetStatisticInfo(std::unordered_map>& result); +int32_t ClearStatisticInfo(); +int32_t UpdateStatisticInfo(uint16_t logType, uint32_t domain, StatisticInfo& info); + + +} +} +#endif diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 7a4e683..6db30a4 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 "statistics.h" namespace OHOS { namespace HiviewDFX { using namespace std; @@ -43,9 +43,9 @@ HilogBuffer::HilogBuffer() size = 0; for (int i = 0; i < LOG_TYPE_MAX; i++) { sizeByType[i] = 0; - cacheLenByType[i] = 0; - printLenByType[i] = 0; - droppedByType[i] = 0; + //cacheLenByType[i] = 0; + //printLenByType[i] = 0; + //droppedByType[i] = 0; } } @@ -84,6 +84,19 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) size_t cLen = it->len - it->tag_len; size -= cLen; sizeByType[(*it).type] -= cLen; + /* + cacheLenByType[(*it).type] -= cLen; + if (cacheLenByDomain.count((*it).domain) == 0) { + std::cout << "Domain not exist"; + } else { + cacheLenByDomain[(*it).domain] -= cLen; + } + */ + StatisticInfo info; + info.dropped = 0; + info.printLen = 0; + info.cacheLen = 0 - cLen; + UpdateStatisticInfo((*it).type, (*it).domain, info); it = hilogDataList.erase(it); } @@ -115,12 +128,19 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) // Update current size of HilogBuffer size += eleSize; sizeByType[msg.type] += eleSize; + /* cacheLenByType[msg.type] += eleSize; if (cacheLenByDomain.count(msg.domain) == 0) { cacheLenByDomain.insert(pair(msg.domain, eleSize)); } else { cacheLenByDomain[msg.domain] += eleSize; } + */ + StatisticInfo info; + info.dropped = 0; + info.printLen = 0; + info.cacheLen = eleSize; + UpdateStatisticInfo(msg.type, msg.domain, info); return eleSize; } @@ -143,6 +163,7 @@ bool HilogBuffer::Query(std::shared_ptr reader) 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, @@ -150,6 +171,7 @@ bool HilogBuffer::Query(std::shared_ptr reader) } else { printLenByDomain[reader->oldData.back().domain] += strlen(reader->oldData.back().content); } + */ reader->oldData.pop_back(); hilogBufferMutex.unlock_shared(); return true; @@ -159,6 +181,7 @@ bool HilogBuffer::Query(std::shared_ptr reader) if (ConditionMatch(reader)) { reader->SetSendId(SENDIDA); reader->WriteData(&*(reader->readPos)); + /* printLenByType[reader->readPos->type] += strlen(reader->readPos->content); if (printLenByDomain.count(reader->readPos->domain) == 0) { printLenByDomain.insert(pair(reader->readPos->domain, @@ -166,6 +189,7 @@ bool HilogBuffer::Query(std::shared_ptr reader) } else { printLenByDomain[reader->readPos->domain] += strlen(reader->readPos->content); } + */ reader->readPos++; hilogBufferMutex.unlock_shared(); return true; @@ -211,6 +235,19 @@ size_t HilogBuffer::Delete(uint16_t logType) sum += cLen; sizeByType[(*it).type] -= cLen; size -= cLen; + /* + cacheLenByType[(*it).type] -= cLen; + if (cacheLenByDomain.count((*it).domain) == 0) { + std::cout << "Domain not exist"; + } else { + cacheLenByDomain[(*it).domain] -= cLen; + } + */ + StatisticInfo info; + info.dropped = 0; + info.printLen = 0; + info.cacheLen = 0 - cLen; + UpdateStatisticInfo((*it).type, (*it).domain, info); it = hilogDataList.erase(it); } @@ -299,13 +336,14 @@ size_t HilogBuffer::SetBuffLen(uint16_t logType, uint64_t buffSize) hilogBufferMutex.unlock(); return buffSize; } - +#if 0 int32_t HilogBuffer::GetStatisticInfoByLog(uint16_t logType, uint64_t& printLen, uint64_t& cacheLen, int32_t& dropped) { if (logType >= LOG_TYPE_MAX) { return ERR_LOG_TYPE_INVALID; } - printLen = printLenByType[logType]; + //printLen = printLenByType[logType]; + printLen = GetPrintByType(logType); cacheLen = cacheLenByType[logType]; dropped = GetDroppedByType(logType); return 0; @@ -314,33 +352,45 @@ int32_t HilogBuffer::GetStatisticInfoByLog(uint16_t logType, uint64_t& printLen, int32_t HilogBuffer::GetStatisticInfoByDomain(uint32_t domain, uint64_t& printLen, uint64_t& cacheLen, int32_t& dropped) { - printLen = printLenByDomain[domain]; + //printLen = printLenByDomain[domain]; + printLen = GetPrintByDomain(domain); cacheLen = cacheLenByDomain[domain]; dropped = GetDroppedByDomain(domain); return 0; } +int32_t HilogBuffer::GetStatisticInfo(uint16_t logType, uint32_t domain, StatisticInfoResult& result) +{ + //printLen = GetPrintByDomain(domain); + //cacheLen = cacheLenByDomain[domain]; + //dropped = GetDroppedByDomain(domain); + return 0; +} + + int32_t HilogBuffer::ClearStatisticInfoByLog(uint16_t logType) { if (logType >= LOG_TYPE_MAX) { return ERR_LOG_TYPE_INVALID; } - ClearDroppedByType(); - printLenByType[logType] = 0; + ClearDroppedByType(logType); + //printLenByType[logType] = 0; + ClearPrintByType(logType); cacheLenByType[logType] = 0; - droppedByType[logType] = 0; + //droppedByType[logType] = 0; return 0; } int32_t HilogBuffer::ClearStatisticInfoByDomain(uint32_t domain) { - ClearDroppedByDomain(); - printLenByDomain[domain] = 0; + ClearDroppedByDomain(domain); + //printLenByDomain[domain] = 0; + ClearPrintByDomain(domain); cacheLenByDomain[domain] = 0; - droppedByDomain[domain] = 0; + //droppedByDomain[domain] = 0; return 0; } - +#endif bool HilogBuffer::ConditionMatch(std::shared_ptr reader) { /* domain patterns: diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 167bf92..eac69bb 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -36,6 +36,7 @@ #include "log_buffer.h" #include "log_persister.h" #include "log_reader.h" +#include "statistics.h" namespace OHOS { namespace HiviewDFX { @@ -355,25 +356,52 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, { char msgToSend[MAX_DATA_LEN]; int32_t rst = 0; + uint32_t rstNum = 0; memset_s(msgToSend, MAX_DATA_LEN, 0, MAX_DATA_LEN); StatisticInfoQueryRequest* pStatisticInfoQueryReq = reinterpret_cast(reqMsg); StatisticInfoQueryResponse* pStatisticInfoQueryRsp = reinterpret_cast(msgToSend); - if (pStatisticInfoQueryReq->domain == 0xffffffff) { - pStatisticInfoQueryRsp->logType = pStatisticInfoQueryReq->logType; - pStatisticInfoQueryRsp->domain = pStatisticInfoQueryReq->domain; - rst = buffer->GetStatisticInfoByLog(pStatisticInfoQueryReq->logType, pStatisticInfoQueryRsp->printLen, - pStatisticInfoQueryRsp->cacheLen, pStatisticInfoQueryRsp->dropped); - pStatisticInfoQueryRsp->result = (rst < 0) ? rst : RET_SUCCESS; + StatisticInfoResult* pRst = reinterpret_cast(pStatisticInfoQueryRsp->statisticInfoRst); + if (pStatisticInfoQueryReq->logType != 0xffff) { + pRst->logType = pStatisticInfoQueryReq->logType; + pRst->domain = pStatisticInfoQueryReq->domain; + StatisticInfo info; + rst = GetStatisticInfoByLog(pStatisticInfoQueryReq->logType, info); + pRst->dropped = info.dropped; + pRst->printLen = info.printLen; + pRst->cacheLen = info.cacheLen; + pRst->result = (rst < 0) ? rst : RET_SUCCESS; + rstNum ++; + } else if (pStatisticInfoQueryReq->domain != 0xffffffff) { + pRst->logType = pStatisticInfoQueryReq->logType; + pRst->domain = pStatisticInfoQueryReq->domain; + StatisticInfo info; + rst = GetStatisticInfoByDomain(pStatisticInfoQueryReq->domain, info); + pRst->dropped = info.dropped; + pRst->printLen = info.printLen; + pRst->cacheLen = info.cacheLen; + pRst->result = (rst < 0) ? rst : RET_SUCCESS; + rstNum ++; } else { - pStatisticInfoQueryRsp->logType = pStatisticInfoQueryReq->logType; - pStatisticInfoQueryRsp->domain = pStatisticInfoQueryReq->domain; - rst = buffer->GetStatisticInfoByDomain(pStatisticInfoQueryReq->domain, pStatisticInfoQueryRsp->printLen, - pStatisticInfoQueryRsp->cacheLen, pStatisticInfoQueryRsp->dropped); - pStatisticInfoQueryRsp->result = (rst < 0) ? rst : RET_SUCCESS; + std::unordered_map> infoMap; + rst = GetStatisticInfo(infoMap); + std::unordered_map>::iterator it; + for (it = infoMap.begin(); it != infoMap.end(); it++) { + std::unordered_map::iterator iter; + for (iter = it->second.begin(); iter != it->second.end(); iter++) { + pRst->result = rst; + pRst->logType = it->first; + pRst->domain = iter->first; + pRst->dropped = iter->second->dropped; + pRst->printLen = iter->second->printLen; + pRst->cacheLen = iter->second->cacheLen; + rstNum ++; + pRst ++; + } + } } - SetMsgHead(&pStatisticInfoQueryRsp->msgHeader, MC_RSP_STATISTIC_INFO_QUERY, sizeof(StatisticInfoQueryResponse) - - sizeof(MessageHeader)); - logReader->hilogtoolConnectSocket->Write(msgToSend, sizeof(StatisticInfoQueryResponse)); + uint16_t sendMsgLen = sizeof(StatisticInfoResult) * rstNum; + SetMsgHead(&pStatisticInfoQueryRsp->msgHeader, MC_RSP_STATISTIC_INFO_QUERY, sendMsgLen); + logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); } void HandleInfoClearRequest(char* reqMsg, std::shared_ptr logReader, HilogBuffer* buffer) @@ -383,17 +411,16 @@ void HandleInfoClearRequest(char* reqMsg, std::shared_ptr logReader, memset_s(msgToSend, MAX_DATA_LEN, 0, MAX_DATA_LEN); StatisticInfoClearRequest* pStatisticInfoClearReq = reinterpret_cast(reqMsg); StatisticInfoClearResponse* pStatisticInfoClearRsp = reinterpret_cast(msgToSend); - if (pStatisticInfoClearReq->domain == 0xffffffff) { - pStatisticInfoClearRsp->logType = pStatisticInfoClearReq->logType; - pStatisticInfoClearRsp->domain = pStatisticInfoClearReq->domain; - rst = buffer->ClearStatisticInfoByLog(pStatisticInfoClearReq->logType); - pStatisticInfoClearRsp->result = (rst < 0) ? rst : RET_SUCCESS; + pStatisticInfoClearRsp->logType = pStatisticInfoClearReq->logType; + pStatisticInfoClearRsp->domain = pStatisticInfoClearReq->domain; + if (pStatisticInfoClearReq->logType != 0xffff) { + rst = ClearStatisticInfoByLog(pStatisticInfoClearReq->logType); + } else if (pStatisticInfoClearReq->domain == 0xffffffff) { + rst = ClearStatisticInfoByDomain(pStatisticInfoClearReq->domain); } else { - pStatisticInfoClearRsp->logType = pStatisticInfoClearReq->logType; - pStatisticInfoClearRsp->domain = pStatisticInfoClearReq->domain; - rst = buffer->ClearStatisticInfoByDomain(pStatisticInfoClearReq->domain); - pStatisticInfoClearRsp->result = (rst < 0) ? rst : RET_SUCCESS; + rst = ClearStatisticInfo(); } + pStatisticInfoClearRsp->result = (rst < 0) ? rst : RET_SUCCESS; SetMsgHead(&pStatisticInfoClearRsp->msgHeader, MC_RSP_STATISTIC_INFO_CLEAR, sizeof(StatisticInfoClearResponse) - sizeof(MessageHeader)); logReader->hilogtoolConnectSocket->Write(msgToSend, sizeof(StatisticInfoClearResponse)); diff --git a/services/hilogd/statistics.cpp b/services/hilogd/statistics.cpp new file mode 100644 index 0000000..c2c1dfe --- /dev/null +++ b/services/hilogd/statistics.cpp @@ -0,0 +1,158 @@ +/* + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "statistics.h" + +namespace OHOS { +namespace HiviewDFX { +//static const int DOMAIN_FILTER = 0x00fffff; +//static const int DOMAIN_FILTER_SUBSYSTEM = 8; + +static std::unordered_map> statisticInfo; + + +int32_t GetStatisticInfoByLog(uint16_t logType, StatisticInfo& result) +{ + std::unordered_map>::iterator it; + for (it = statisticInfo.begin(); it != statisticInfo.end(); it++) { + if (it->first == logType) { + std::unordered_map::iterator iter; + for (iter = it->second.begin(); iter != it->second.end(); iter++) { + result.dropped = iter->second->dropped; + result.printLen = iter->second->printLen; + result.cacheLen = iter->second->cacheLen; + } + + } + } + return 0; +} + +int32_t GetStatisticInfoByDomain(uint32_t domain, StatisticInfo& result) +{ + //uint32_t domainId = (domain & DOMAIN_FILTER) >> DOMAIN_FILTER_SUBSYSTEM; + std::unordered_map>::iterator it; + for (it = statisticInfo.begin(); it != statisticInfo.end(); it++) { + std::unordered_map::iterator iter; + for (iter = it->second.begin(); iter != it->second.end(); iter++) { + if (iter->first == domain) { + result.dropped = iter->second->dropped; + result.printLen = iter->second->printLen; + result.cacheLen = iter->second->cacheLen; + } + } + + } + return 0; +} + +int32_t ClearStatisticInfoByLog(uint16_t logType) +{ + std::unordered_map>::iterator it; + for (it = statisticInfo.begin(); it != statisticInfo.end(); it++) { + if (it->first == logType) { + std::unordered_map::iterator iter; + for (iter = it->second.begin(); iter != it->second.end(); iter++) { + iter->second->dropped = 0; + iter->second->printLen = 0; + iter->second->cacheLen = 0; + } + } + } + return 0; +} + +int32_t ClearStatisticInfoByDomain(uint32_t domain) +{ + //uint32_t domainId = (domain & DOMAIN_FILTER) >> DOMAIN_FILTER_SUBSYSTEM; + std::unordered_map>::iterator it; + for (it = statisticInfo.begin(); it != statisticInfo.end(); it++) { + std::unordered_map::iterator iter; + if (iter->first == domain) { + iter->second->dropped = 0; + iter->second->printLen = 0; + iter->second->cacheLen = 0; + } + } + return 0; +} + +int32_t GetStatisticInfo(std::unordered_map>& result) +{ + result = statisticInfo; + return 0; +} + +int32_t ClearStatisticInfo() +{ + std::unordered_map>::iterator it; + for (it = statisticInfo.begin(); it != statisticInfo.end(); it++) { + std::unordered_map::iterator iter; + for (iter = it->second.begin(); iter != it->second.end(); iter++) { + iter->second->dropped = 0; + iter->second->printLen = 0; + iter->second->cacheLen = 0; + } + } + + return 0; +} + + +int32_t UpdateStatisticInfo(uint16_t logType, uint32_t domain, StatisticInfo& info) +{ + //uint32_t domainId = (domain & DOMAIN_FILTER) >> DOMAIN_FILTER_SUBSYSTEM; + std::unordered_map>::iterator it = statisticInfo.find(logType); + if (it != statisticInfo.end()) { + std::unordered_map::iterator iter = it->second.find(domain); + if (iter != it->second.end()) { + iter->second->dropped += info.dropped; + iter->second->printLen += info.printLen; + iter->second->cacheLen += info.cacheLen; + } else { + StatisticInfo* newInfo = new StatisticInfo; + if (!newInfo) { + return -1; + } + newInfo->dropped = info.dropped; + newInfo->printLen = info.printLen; + newInfo->cacheLen = info.cacheLen; + it->second.insert({domain, newInfo}); + } + } else { + StatisticInfo* newInfo = new StatisticInfo; + if (!newInfo) { + return -1; + } + newInfo->dropped = info.dropped; + newInfo->printLen = info.printLen; + newInfo->cacheLen = info.cacheLen; + statisticInfo[logType].insert({domain, newInfo}); + } + return 0; +} + + +} +} diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 994eec7..a0d09ea 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -311,17 +311,19 @@ int32_t BufferSizeOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const st int32_t StatisticInfoOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const std::string& logTypeStr, const std::string& domainStr) { - if ((logTypeStr != "" && domainStr != "") || (logTypeStr == "" && domainStr == "")) { + if ((logTypeStr != "" && domainStr != "")/* || (logTypeStr == "" && domainStr == "")*/) { return RET_FAIL; } uint16_t logType = GetLogType(logTypeStr); uint32_t domain; if (domainStr == "") { domain = 0xffffffff; + /* if (logType == 0xffff) { cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; return RET_FAIL; } + */ } else { std::istringstream(domainStr) >> domain; if (domain == 0 || domain > DOMAIN_MAX_SCOPE) { diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 59f496e..44e3d76 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -228,31 +228,48 @@ int32_t ControlCmdResult(const char* message) } case MC_RSP_STATISTIC_INFO_QUERY: { StatisticInfoQueryResponse* staInfoQueryRsp = (StatisticInfoQueryResponse*)message; + StatisticInfoResult* infoRst = (StatisticInfoResult*)staInfoQueryRsp->statisticInfoRst; string logOrDomain; if (!staInfoQueryRsp) { return RET_FAIL; } - if (staInfoQueryRsp->domain != 0xffffffff) { - logOrDomain = to_string(staInfoQueryRsp->domain); + if (infoRst->logType == 0xffff) { + logOrDomain = to_string(infoRst->domain); + } else if (infoRst->domain == 0xffffffff){ + logOrDomain = GetLogTypeStr(infoRst->logType); } else { - logOrDomain = GetLogTypeStr(staInfoQueryRsp->logType); + while (infoRst) { + if (infoRst->result < 0) { + } else { + outputStr += infoRst->logType; + outputStr += " "; + outputStr += infoRst->domain; + outputStr += " "; + outputStr += GetByteLenStr(infoRst->printLen); + outputStr += " "; + outputStr += GetByteLenStr(infoRst->cacheLen); + outputStr += " "; + outputStr += infoRst->dropped; + } + infoRst++; + } } - if (staInfoQueryRsp->result == RET_SUCCESS) { + if (infoRst->result == RET_SUCCESS) { outputStr += logOrDomain; outputStr += " print log length is "; - outputStr += GetByteLenStr(staInfoQueryRsp->printLen); + outputStr += GetByteLenStr(infoRst->printLen); outputStr += "\n"; outputStr += logOrDomain; outputStr += " cache log length is "; - outputStr += GetByteLenStr(staInfoQueryRsp->cacheLen); + outputStr += GetByteLenStr(infoRst->cacheLen); outputStr += "\n"; outputStr += logOrDomain; outputStr += " dropped log lines is "; - outputStr += GetByteLenStr(staInfoQueryRsp->dropped); - } else if (staInfoQueryRsp->result < 0) { + outputStr += GetByteLenStr(infoRst->dropped); + } else if (infoRst->result < 0) { outputStr += logOrDomain; outputStr += " statistic info query fail\n"; - outputStr += ParseErrorCode((ErrorCode)staInfoQueryRsp->result); + outputStr += ParseErrorCode((ErrorCode)infoRst->result); } break; } @@ -264,8 +281,10 @@ int32_t ControlCmdResult(const char* message) } if (staInfoClearRsp->domain != 0xffffffff) { logOrDomain = to_string(staInfoClearRsp->domain); - } else { + } else if (staInfoClearRsp->logType != 0xffff) { logOrDomain = GetLogTypeStr(staInfoClearRsp->logType); + } else { + logOrDomain = "all"; } if (staInfoClearRsp->result == RET_SUCCESS) { outputStr += logOrDomain; -- Gitee From 3a27a6918edab6d00831f1388aebbb0673f46e35 Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Thu, 16 Dec 2021 21:33:02 -0500 Subject: [PATCH 02/28] fix statistics Signed-off-by: youyouyuai --- services/hilogd/flow_control_init.cpp | 2 +- services/hilogd/include/statistics.h | 6 +++--- services/hilogd/log_buffer.cpp | 7 ++++--- services/hilogd/log_querier.cpp | 4 ++-- services/hilogd/statistics.cpp | 23 +++++++++++++++++------ 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/services/hilogd/flow_control_init.cpp b/services/hilogd/flow_control_init.cpp index f4a3de1..bf70b2d 100644 --- a/services/hilogd/flow_control_init.cpp +++ b/services/hilogd/flow_control_init.cpp @@ -250,7 +250,7 @@ int FlowCtrlDomain(HilogMsg* hilogMsg) //IncreasePrint(domainId, hilogMsg->type); StatisticInfo info; info.dropped = 0; - info.printLen = hilogMsg->len; + info.printLen = CONTENT_LEN((hilogMsg)); info.cacheLen = 0; UpdateStatisticInfo(hilogMsg->type, domain, info); diff --git a/services/hilogd/include/statistics.h b/services/hilogd/include/statistics.h index b09cc3b..30a5ce7 100644 --- a/services/hilogd/include/statistics.h +++ b/services/hilogd/include/statistics.h @@ -24,9 +24,9 @@ namespace OHOS { namespace HiviewDFX { using StatisticInfo = struct { - int32_t dropped; - int64_t printLen; - int64_t cacheLen; + size_t dropped; + size_t printLen; + size_t cacheLen; }; diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 2faf523..76e0b0e 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -20,7 +20,7 @@ #include "flow_control_init.h" #include "statistics.h" #include "log_time_stamp.h" ->>>>>>> 35bb82e598f1adc2f20913ea6d7c806fe930b3a5 + namespace OHOS { namespace HiviewDFX { using namespace std; @@ -94,8 +94,7 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) info.cacheLen = 0 - cLen; UpdateStatisticInfo((*it).type, (*it).domain, info); it = msgList.erase(it); - } - + } // Re-confirm if enough elements has been removed if (sizeByType[msg.type] >= (size_t)g_maxBufferSizeByType[msg.type]) { std::cout << "Failed to clean old logs." << std::endl; @@ -162,6 +161,7 @@ bool HilogBuffer::Query(std::shared_ptr reader) if (ConditionMatch(reader)) { reader->SetSendId(SENDIDA); reader->WriteData(*(reader->readPos)); + /* printLenByType[reader->readPos->type] += strlen(reader->readPos->content); if (printLenByDomain.count(reader->readPos->domain) == 0) { printLenByDomain.insert(pair(reader->readPos->domain, @@ -169,6 +169,7 @@ bool HilogBuffer::Query(std::shared_ptr reader) } else { printLenByDomain[reader->readPos->domain] += strlen(reader->readPos->content); } + */ reader->readPos++; hilogBufferMutex.unlock_shared(); return true; diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 816c214..65f72da 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -415,7 +415,7 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, } } uint16_t sendMsgLen = sizeof(StatisticInfoResult) * rstNum; - SetMsgHead(&pStatisticInfoQueryRsp->msgHeader, MC_RSP_STATISTIC_INFO_QUERY, sendMsgLen); + SetMsgHead(pStatisticInfoQueryRsp->msgHeader, MC_RSP_STATISTIC_INFO_QUERY, sendMsgLen); logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); } @@ -436,7 +436,7 @@ void HandleInfoClearRequest(char* reqMsg, std::shared_ptr logReader, rst = ClearStatisticInfo(); } pStatisticInfoClearRsp->result = (rst < 0) ? rst : RET_SUCCESS; - SetMsgHead(&pStatisticInfoClearRsp->msgHeader, MC_RSP_STATISTIC_INFO_CLEAR, sizeof(StatisticInfoClearResponse) - + SetMsgHead(pStatisticInfoClearRsp->msgHeader, MC_RSP_STATISTIC_INFO_CLEAR, sizeof(StatisticInfoClearResponse) - sizeof(MessageHeader)); logReader->hilogtoolConnectSocket->Write(msgToSend, sizeof(StatisticInfoClearResponse)); } diff --git a/services/hilogd/statistics.cpp b/services/hilogd/statistics.cpp index c2c1dfe..37c497b 100644 --- a/services/hilogd/statistics.cpp +++ b/services/hilogd/statistics.cpp @@ -34,14 +34,18 @@ static std::unordered_map int32_t GetStatisticInfoByLog(uint16_t logType, StatisticInfo& result) { + result.dropped = 0; + result.printLen = 0; + result.cacheLen = 0; std::unordered_map>::iterator it; for (it = statisticInfo.begin(); it != statisticInfo.end(); it++) { if (it->first == logType) { std::unordered_map::iterator iter; for (iter = it->second.begin(); iter != it->second.end(); iter++) { - result.dropped = iter->second->dropped; - result.printLen = iter->second->printLen; - result.cacheLen = iter->second->cacheLen; + result.dropped += iter->second->dropped; + result.printLen += iter->second->printLen; + result.cacheLen += iter->second->cacheLen; + //printf("after get logtype: %d, domain: %d, cacheLen: %lld\n", logType, iter->first, result.cacheLen); } } @@ -52,14 +56,17 @@ int32_t GetStatisticInfoByLog(uint16_t logType, StatisticInfo& result) int32_t GetStatisticInfoByDomain(uint32_t domain, StatisticInfo& result) { //uint32_t domainId = (domain & DOMAIN_FILTER) >> DOMAIN_FILTER_SUBSYSTEM; + result.dropped = 0; + result.printLen = 0; + result.cacheLen = 0; std::unordered_map>::iterator it; for (it = statisticInfo.begin(); it != statisticInfo.end(); it++) { std::unordered_map::iterator iter; for (iter = it->second.begin(); iter != it->second.end(); iter++) { if (iter->first == domain) { - result.dropped = iter->second->dropped; - result.printLen = iter->second->printLen; - result.cacheLen = iter->second->cacheLen; + result.dropped += iter->second->dropped; + result.printLen += iter->second->printLen; + result.cacheLen += iter->second->cacheLen; } } @@ -123,6 +130,9 @@ int32_t ClearStatisticInfo() int32_t UpdateStatisticInfo(uint16_t logType, uint32_t domain, StatisticInfo& info) { //uint32_t domainId = (domain & DOMAIN_FILTER) >> DOMAIN_FILTER_SUBSYSTEM; + if (logType >= LOG_TYPE_MAX) { + return -1; + } std::unordered_map>::iterator it = statisticInfo.find(logType); if (it != statisticInfo.end()) { std::unordered_map::iterator iter = it->second.find(domain); @@ -150,6 +160,7 @@ int32_t UpdateStatisticInfo(uint16_t logType, uint32_t domain, StatisticInfo& in newInfo->cacheLen = info.cacheLen; statisticInfo[logType].insert({domain, newInfo}); } + //printf("after update logtype: %d, domain: %d, cacheLen: %lld\n", logType, domain, statisticInfo[logType][domain]->cacheLen); return 0; } -- Gitee From 73a19d72b7449c00862c3684c6a1701661588eee Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Mon, 20 Dec 2021 02:34:24 -0500 Subject: [PATCH 03/28] fix statistics Signed-off-by: youyouyuai --- frameworks/native/include/hilogtool_msg.h | 6 +++--- services/hilogd/statistics.cpp | 8 ++++---- services/hilogtool/log_display.cpp | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/frameworks/native/include/hilogtool_msg.h b/frameworks/native/include/hilogtool_msg.h index d60b2f8..946d73b 100644 --- a/frameworks/native/include/hilogtool_msg.h +++ b/frameworks/native/include/hilogtool_msg.h @@ -201,9 +201,9 @@ typedef struct { int32_t result; uint16_t logType; uint32_t domain; - int64_t printLen; - int64_t cacheLen; - int32_t dropped; + size_t printLen; + size_t cacheLen; + size_t dropped; } StatisticInfoResult; typedef struct { diff --git a/services/hilogd/statistics.cpp b/services/hilogd/statistics.cpp index 37c497b..436bfd0 100644 --- a/services/hilogd/statistics.cpp +++ b/services/hilogd/statistics.cpp @@ -45,7 +45,7 @@ int32_t GetStatisticInfoByLog(uint16_t logType, StatisticInfo& result) result.dropped += iter->second->dropped; result.printLen += iter->second->printLen; result.cacheLen += iter->second->cacheLen; - //printf("after get logtype: %d, domain: %d, cacheLen: %lld\n", logType, iter->first, result.cacheLen); + printf("after get logtype: %d, domain: %d, cacheLen: %d\n", logType, iter->first, result.cacheLen); } } @@ -83,7 +83,7 @@ int32_t ClearStatisticInfoByLog(uint16_t logType) for (iter = it->second.begin(); iter != it->second.end(); iter++) { iter->second->dropped = 0; iter->second->printLen = 0; - iter->second->cacheLen = 0; + //iter->second->cacheLen = 0; } } } @@ -99,7 +99,7 @@ int32_t ClearStatisticInfoByDomain(uint32_t domain) if (iter->first == domain) { iter->second->dropped = 0; iter->second->printLen = 0; - iter->second->cacheLen = 0; + //iter->second->cacheLen = 0; } } return 0; @@ -119,7 +119,7 @@ int32_t ClearStatisticInfo() for (iter = it->second.begin(); iter != it->second.end(); iter++) { iter->second->dropped = 0; iter->second->printLen = 0; - iter->second->cacheLen = 0; + //iter->second->cacheLen = 0; } } diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index c160214..6508598 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -251,7 +251,7 @@ int32_t ControlCmdResult(const char* message) outputStr += " "; outputStr += GetByteLenStr(infoRst->cacheLen); outputStr += " "; - outputStr += infoRst->dropped; + outputStr += GetByteLenStr(infoRst->dropped); } infoRst++; } -- Gitee From 19d8ad02be8b08118beea052cf08aafaa2acdf6e Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Mon, 20 Dec 2021 04:50:43 -0500 Subject: [PATCH 04/28] fix statistics Signed-off-by: youyouyuai --- services/hilogd/log_querier.cpp | 4 +++- services/hilogtool/log_display.cpp | 28 +++++++++++++++------------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 65f72da..5f3c063 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -409,6 +409,8 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, pRst->dropped = iter->second->dropped; pRst->printLen = iter->second->printLen; pRst->cacheLen = iter->second->cacheLen; + //printf("logtype %d, domain %d, print %d, cache %d, drop %d\n", pRst->logType, pRst->domain, + //pRst->printLen, pRst->cacheLen, pRst->dropped); rstNum ++; pRst ++; } @@ -430,7 +432,7 @@ void HandleInfoClearRequest(char* reqMsg, std::shared_ptr logReader, pStatisticInfoClearRsp->domain = pStatisticInfoClearReq->domain; if (pStatisticInfoClearReq->logType != 0xffff) { rst = ClearStatisticInfoByLog(pStatisticInfoClearReq->logType); - } else if (pStatisticInfoClearReq->domain == 0xffffffff) { + } else if (pStatisticInfoClearReq->domain != 0xffffffff) { rst = ClearStatisticInfoByDomain(pStatisticInfoClearReq->domain); } else { rst = ClearStatisticInfo(); diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 6508598..f6d87b9 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -240,21 +240,23 @@ int32_t ControlCmdResult(const char* message) } else if (infoRst->domain == 0xffffffff){ logOrDomain = GetLogTypeStr(infoRst->logType); } else { - while (infoRst) { - if (infoRst->result < 0) { - } else { - outputStr += infoRst->logType; - outputStr += " "; - outputStr += infoRst->domain; - outputStr += " "; - outputStr += GetByteLenStr(infoRst->printLen); - outputStr += " "; - outputStr += GetByteLenStr(infoRst->cacheLen); - outputStr += " "; - outputStr += GetByteLenStr(infoRst->dropped); - } + while (infoRst && resultLen < msgLen) { + //printf("logtype %d, domain %d, print %d, cache %d, drop %d\n", infoRst->logType, infoRst->domain, infoRst->logType, infoRst->cacheLen, infoRst->dropped); + outputStr += "logType:"; + outputStr += GetLogTypeStr(infoRst->logType); + outputStr += " domain:"; + outputStr += to_string(infoRst->domain); + outputStr += " printLen:"; + outputStr += GetByteLenStr(infoRst->printLen); + outputStr += " cacheLen:"; + outputStr += GetByteLenStr(infoRst->cacheLen); + outputStr += " dropped num:"; + outputStr += GetByteLenStr(infoRst->dropped); + outputStr += "\n"; infoRst++; + resultLen += sizeof(StatisticInfoResult); } + break; } if (infoRst->result == RET_SUCCESS) { outputStr += logOrDomain; -- Gitee From 403c38471efb70ef8aa1db6889ef5e4c8702e307 Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Mon, 20 Dec 2021 22:09:00 -0500 Subject: [PATCH 05/28] fix statistics format Signed-off-by: youyouyuai --- services/hilogtool/log_display.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index f6d87b9..3fc4f92 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -19,6 +19,9 @@ #include #include #include +#include +#include +#include #include "hilog/log.h" #include "format.h" #include "log_controller.h" @@ -240,8 +243,19 @@ int32_t ControlCmdResult(const char* message) } else if (infoRst->domain == 0xffffffff){ logOrDomain = GetLogTypeStr(infoRst->logType); } else { + bool isOut[LOG_TYPE_MAX] = {false}; while (infoRst && resultLen < msgLen) { //printf("logtype %d, domain %d, print %d, cache %d, drop %d\n", infoRst->logType, infoRst->domain, infoRst->logType, infoRst->cacheLen, infoRst->dropped); + std::ostringstream oss; + if (!isOut[infoRst->logType]) { + oss << std::left << std::setw(20) << GetLogTypeStr(infoRst->logType) << std::endl; + isOut[infoRst->logType] = true; + } + oss << std::left << std::setw(5) << "" << std::setw(20) << "domain:" + to_string(infoRst->domain) + << std::setw(20) << "printLen:" + GetByteLenStr(infoRst->printLen) << std::setw(20) << "cacheLen:" + + GetByteLenStr(infoRst->cacheLen) << std::setw(20) << "dropped lines:" + GetByteLenStr(infoRst->dropped) << std::endl; + std::cout << oss.str(); + /* outputStr += "logType:"; outputStr += GetLogTypeStr(infoRst->logType); outputStr += " domain:"; @@ -253,6 +267,7 @@ int32_t ControlCmdResult(const char* message) outputStr += " dropped num:"; outputStr += GetByteLenStr(infoRst->dropped); outputStr += "\n"; + */ infoRst++; resultLen += sizeof(StatisticInfoResult); } -- Gitee From 25d8bb4f46e9f255ffc4ffd7aa8d31b7a228ebc2 Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Tue, 21 Dec 2021 01:59:33 -0500 Subject: [PATCH 06/28] rm unused code Signed-off-by: youyouyuai --- services/hilogd/flow_control_init.cpp | 134 -------------------------- services/hilogd/include/log_buffer.h | 11 --- services/hilogd/include/statistics.h | 5 - services/hilogd/log_buffer.cpp | 90 ----------------- services/hilogd/log_querier.cpp | 2 - services/hilogd/statistics.cpp | 15 +-- services/hilogtool/log_controller.cpp | 6 -- services/hilogtool/log_display.cpp | 14 --- 8 files changed, 1 insertion(+), 276 deletions(-) diff --git a/services/hilogd/flow_control_init.cpp b/services/hilogd/flow_control_init.cpp index bf70b2d..92be534 100644 --- a/services/hilogd/flow_control_init.cpp +++ b/services/hilogd/flow_control_init.cpp @@ -42,133 +42,7 @@ using DomainInfo = struct { }; static std::unordered_map g_domainMap; -#if 0 -static int32_t g_typeDropped[LOG_TYPE_MAX]; -static std::unordered_map g_domainDropped; -static uint32_t g_typePrintLen[LOG_TYPE_MAX]; -static std::unordered_map g_domainPrintLen; - -void InitDropped() -{ - int i; - std::unordered_map::iterator it; - for (i = 0; i < LOG_TYPE_MAX; i++) { - g_typeDropped[i] = 0; - } - for (it = g_domainDropped.begin(); it != g_domainDropped.end(); ++it) { - it->second = 0; - } -} - -void InitPrintLen() -{ - int i; - std::unordered_map::iterator it; - for (i = 0; i < LOG_TYPE_MAX; i++) { - g_typePrintLen[i] = 0; - } - for (it = g_domainPrintLen.begin(); it != g_domainPrintLen.end(); ++it) { - it->second = 0; - } -} - -void ClearDroppedByType(uint16_t logType) -{ - /* - int i; - for (i = 0; i < LOG_TYPE_MAX; i++) { - g_typeDropped[i] = 0; - } - */ - if (logType >= LOG_TYPE_MAX) { - return; - } - g_typeDropped[logType] = 0; -} - -void ClearDroppedByDomain(uint32_t domainId) -{ - /* - std::unordered_map::iterator it; - for (it = g_domainDropped.begin(); it != g_domainDropped.end(); ++it) { - it->second = 0; - } - */ - g_domainDropped[domainId] = 0; -} - -void ClearPrintByType(uint16_t logType) -{ - if (logType >= LOG_TYPE_MAX) { - return; - } - g_typePrintLen[logType] = 0; -} - -void ClearPrintByDomain(uint32_t domainId) -{ - g_domainPrintLen[domainId] = 0; -} -/* -void IncreaseDropped(uint32_t domainId, uint16_t logType) -{ - std::unordered_map::iterator it; - g_typeDropped[logType]++; - it = g_domainDropped.find(domainId); - if (it != g_domainDropped.end()) { - it->second++; - } else { - g_domainDropped.insert({ domainId, 1 }); - } -} -*/ -int32_t GetDroppedByType(uint16_t logType) -{ - if (logType >= LOG_TYPE_MAX) { - return 0; - } - return g_typeDropped[logType]; -} - -int32_t GetDroppedByDomain(uint32_t domainId) -{ - std::unordered_map::iterator it = g_domainDropped.find(domainId); - if (it != g_domainDropped.end()) { - return it->second; - } - return 0; -} -/* -void IncreasePrint(uint32_t domainId, uint16_t logType) -{ - std::unordered_map::iterator it; - g_typePrintLen[logType]++; - it = g_domainPrintLen.find(domainId); - if (it != g_domainPrintLen.end()) { - it->second++; - } else { - g_domainPrintLen.insert({ domainId, 1 }); - } -} -*/ -uint32_t GetPrintByType(uint16_t logType) -{ - if (logType >= LOG_TYPE_MAX) { - return 0; - } - return g_typePrintLen[logType]; -} - -uint32_t GetPrintByDomain(uint32_t domainId) -{ - std::unordered_map::iterator it = g_domainPrintLen.find(domainId); - if (it != g_domainPrintLen.end()) { - return it->second; - } - return 0; -} -#endif void ParseDomainQuota(std::string &domainStr) { if (domainStr.empty() || domainStr.at(0) == '#') { @@ -221,9 +95,6 @@ void ParseDomainQuota(std::string &domainStr) int32_t InitDomainFlowCtrl() { - //InitDropped(); - //InitPrintLen(); - static constexpr char domainFile[] = "/system/etc/hilog_domains.conf"; std::ifstream ifs(domainFile, std::ifstream::in); if (!ifs.is_open()) { @@ -238,8 +109,6 @@ int32_t InitDomainFlowCtrl() ParseDomainQuota(line); } ifs.close(); - //ClearDroppedByType(); - //ClearDroppedByDomain(); return 0; } @@ -247,7 +116,6 @@ int FlowCtrlDomain(HilogMsg* hilogMsg) { uint32_t domain = hilogMsg->domain; uint32_t domainId = (domain & DOMAIN_FILTER) >> DOMAIN_FILTER_SUBSYSTEM; - //IncreasePrint(domainId, hilogMsg->type); StatisticInfo info; info.dropped = 0; info.printLen = CONTENT_LEN((hilogMsg)); @@ -259,8 +127,6 @@ int FlowCtrlDomain(HilogMsg* hilogMsg) } LogTimeStamp tsNow(0, 0); std::unordered_map::iterator it; - //uint32_t domain = hilogMsg->domain; - //uint32_t domainId = (domain & DOMAIN_FILTER) >> DOMAIN_FILTER_SUBSYSTEM; int logLen = hilogMsg->len - sizeof(HilogMsg) - 1 - 1; /* quota length exclude '\0' of tag and log content */ int ret = 0; it = g_domainMap.find(domainId); diff --git a/services/hilogd/include/log_buffer.h b/services/hilogd/include/log_buffer.h index e769c6a..217f5d6 100644 --- a/services/hilogd/include/log_buffer.h +++ b/services/hilogd/include/log_buffer.h @@ -43,12 +43,6 @@ public: size_t Delete(uint16_t logType); size_t GetBuffLen(uint16_t logType); size_t SetBuffLen(uint16_t logType, uint64_t buffSize); - /* - int32_t GetStatisticInfoByLog(uint16_t logType, uint64_t& printLen, uint64_t& cacheLen, int32_t& dropped); - int32_t GetStatisticInfoByDomain(uint32_t domain, uint64_t& printLen, uint64_t& cacheLen, int32_t& dropped); - int32_t ClearStatisticInfoByLog(uint16_t logType); - int32_t ClearStatisticInfoByDomain(uint32_t domain); - */ void GetBufferLock(); void ReleaseBufferLock(); private: @@ -58,11 +52,6 @@ private: std::list hilogKlogList; std::shared_mutex hilogBufferMutex; std::map cacheLenByDomain; - //std::map printLenByDomain; - //std::map droppedByDomain; - //uint64_t cacheLenByType[LOG_TYPE_MAX]; - //uint64_t droppedByType[LOG_TYPE_MAX]; - //uint64_t printLenByType[LOG_TYPE_MAX]; bool ConditionMatch(std::shared_ptr reader); void ReturnNoLog(std::shared_ptr reader); }; diff --git a/services/hilogd/include/statistics.h b/services/hilogd/include/statistics.h index 30a5ce7..cd14292 100644 --- a/services/hilogd/include/statistics.h +++ b/services/hilogd/include/statistics.h @@ -15,8 +15,6 @@ #ifndef STATISTICS_H #define STATISTICS_H -#include -#include "hilog_common.h" #include @@ -30,9 +28,6 @@ using StatisticInfo = struct { }; - -//Statistics(); -//~Statistics(); int32_t GetStatisticInfoByLog(uint16_t logType, StatisticInfo& result); int32_t GetStatisticInfoByDomain(uint32_t domain, StatisticInfo& result); int32_t ClearStatisticInfoByLog(uint16_t logType); diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 76e0b0e..9a5e6fc 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -38,9 +38,6 @@ HilogBuffer::HilogBuffer() size = 0; for (int i = 0; i < LOG_TYPE_MAX; i++) { sizeByType[i] = 0; - //cacheLenByType[i] = 0; - //printLenByType[i] = 0; - //droppedByType[i] = 0; } } @@ -80,14 +77,6 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) size_t cLen = it->len - it->tag_len; size -= cLen; sizeByType[(*it).type] -= cLen; - /* - cacheLenByType[(*it).type] -= cLen; - if (cacheLenByDomain.count((*it).domain) == 0) { - std::cout << "Domain not exist"; - } else { - cacheLenByDomain[(*it).domain] -= cLen; - } - */ StatisticInfo info; info.dropped = 0; info.printLen = 0; @@ -123,14 +112,6 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) // Update current size of HilogBuffer size += eleSize; sizeByType[msg.type] += eleSize; - /* - cacheLenByType[msg.type] += eleSize; - if (cacheLenByDomain.count(msg.domain) == 0) { - cacheLenByDomain.insert(pair(msg.domain, eleSize)); - } else { - cacheLenByDomain[msg.domain] += eleSize; - } - */ StatisticInfo info; info.dropped = 0; info.printLen = 0; @@ -161,15 +142,6 @@ bool HilogBuffer::Query(std::shared_ptr reader) if (ConditionMatch(reader)) { reader->SetSendId(SENDIDA); reader->WriteData(*(reader->readPos)); - /* - printLenByType[reader->readPos->type] += strlen(reader->readPos->content); - if (printLenByDomain.count(reader->readPos->domain) == 0) { - printLenByDomain.insert(pair(reader->readPos->domain, - strlen(reader->readPos->content))); - } else { - printLenByDomain[reader->readPos->domain] += strlen(reader->readPos->content); - } - */ reader->readPos++; hilogBufferMutex.unlock_shared(); return true; @@ -216,14 +188,6 @@ size_t HilogBuffer::Delete(uint16_t logType) sum += cLen; sizeByType[(*it).type] -= cLen; size -= cLen; - /* - cacheLenByType[(*it).type] -= cLen; - if (cacheLenByDomain.count((*it).domain) == 0) { - std::cout << "Domain not exist"; - } else { - cacheLenByDomain[(*it).domain] -= cLen; - } - */ StatisticInfo info; info.dropped = 0; info.printLen = 0; @@ -286,61 +250,7 @@ size_t HilogBuffer::SetBuffLen(uint16_t logType, uint64_t buffSize) g_maxBufferSize += (buffSize - sizeByType[logType]); return buffSize; } -#if 0 -int32_t HilogBuffer::GetStatisticInfoByLog(uint16_t logType, uint64_t& printLen, uint64_t& cacheLen, int32_t& dropped) -{ - if (logType >= LOG_TYPE_MAX) { - return ERR_LOG_TYPE_INVALID; - } - //printLen = printLenByType[logType]; - printLen = GetPrintByType(logType); - cacheLen = cacheLenByType[logType]; - dropped = GetDroppedByType(logType); - return 0; -} - -int32_t HilogBuffer::GetStatisticInfoByDomain(uint32_t domain, uint64_t& printLen, uint64_t& cacheLen, - int32_t& dropped) -{ - //printLen = printLenByDomain[domain]; - printLen = GetPrintByDomain(domain); - cacheLen = cacheLenByDomain[domain]; - dropped = GetDroppedByDomain(domain); - return 0; -} -int32_t HilogBuffer::GetStatisticInfo(uint16_t logType, uint32_t domain, StatisticInfoResult& result) -{ - //printLen = GetPrintByDomain(domain); - //cacheLen = cacheLenByDomain[domain]; - //dropped = GetDroppedByDomain(domain); - return 0; -} - - -int32_t HilogBuffer::ClearStatisticInfoByLog(uint16_t logType) -{ - if (logType >= LOG_TYPE_MAX) { - return ERR_LOG_TYPE_INVALID; - } - ClearDroppedByType(logType); - //printLenByType[logType] = 0; - ClearPrintByType(logType); - cacheLenByType[logType] = 0; - //droppedByType[logType] = 0; - return 0; -} - -int32_t HilogBuffer::ClearStatisticInfoByDomain(uint32_t domain) -{ - ClearDroppedByDomain(domain); - //printLenByDomain[domain] = 0; - ClearPrintByDomain(domain); - cacheLenByDomain[domain] = 0; - //droppedByDomain[domain] = 0; - return 0; -} -#endif bool HilogBuffer::ConditionMatch(std::shared_ptr reader) { /* domain patterns: diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 5f3c063..233844e 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -409,8 +409,6 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, pRst->dropped = iter->second->dropped; pRst->printLen = iter->second->printLen; pRst->cacheLen = iter->second->cacheLen; - //printf("logtype %d, domain %d, print %d, cache %d, drop %d\n", pRst->logType, pRst->domain, - //pRst->printLen, pRst->cacheLen, pRst->dropped); rstNum ++; pRst ++; } diff --git a/services/hilogd/statistics.cpp b/services/hilogd/statistics.cpp index 436bfd0..857dd11 100644 --- a/services/hilogd/statistics.cpp +++ b/services/hilogd/statistics.cpp @@ -17,17 +17,12 @@ #include #include #include -#include -#include -#include -#include #include #include "statistics.h" namespace OHOS { namespace HiviewDFX { -//static const int DOMAIN_FILTER = 0x00fffff; -//static const int DOMAIN_FILTER_SUBSYSTEM = 8; + static std::unordered_map> statisticInfo; @@ -45,7 +40,6 @@ int32_t GetStatisticInfoByLog(uint16_t logType, StatisticInfo& result) result.dropped += iter->second->dropped; result.printLen += iter->second->printLen; result.cacheLen += iter->second->cacheLen; - printf("after get logtype: %d, domain: %d, cacheLen: %d\n", logType, iter->first, result.cacheLen); } } @@ -55,7 +49,6 @@ int32_t GetStatisticInfoByLog(uint16_t logType, StatisticInfo& result) int32_t GetStatisticInfoByDomain(uint32_t domain, StatisticInfo& result) { - //uint32_t domainId = (domain & DOMAIN_FILTER) >> DOMAIN_FILTER_SUBSYSTEM; result.dropped = 0; result.printLen = 0; result.cacheLen = 0; @@ -83,7 +76,6 @@ int32_t ClearStatisticInfoByLog(uint16_t logType) for (iter = it->second.begin(); iter != it->second.end(); iter++) { iter->second->dropped = 0; iter->second->printLen = 0; - //iter->second->cacheLen = 0; } } } @@ -92,14 +84,12 @@ int32_t ClearStatisticInfoByLog(uint16_t logType) int32_t ClearStatisticInfoByDomain(uint32_t domain) { - //uint32_t domainId = (domain & DOMAIN_FILTER) >> DOMAIN_FILTER_SUBSYSTEM; std::unordered_map>::iterator it; for (it = statisticInfo.begin(); it != statisticInfo.end(); it++) { std::unordered_map::iterator iter; if (iter->first == domain) { iter->second->dropped = 0; iter->second->printLen = 0; - //iter->second->cacheLen = 0; } } return 0; @@ -119,7 +109,6 @@ int32_t ClearStatisticInfo() for (iter = it->second.begin(); iter != it->second.end(); iter++) { iter->second->dropped = 0; iter->second->printLen = 0; - //iter->second->cacheLen = 0; } } @@ -129,7 +118,6 @@ int32_t ClearStatisticInfo() int32_t UpdateStatisticInfo(uint16_t logType, uint32_t domain, StatisticInfo& info) { - //uint32_t domainId = (domain & DOMAIN_FILTER) >> DOMAIN_FILTER_SUBSYSTEM; if (logType >= LOG_TYPE_MAX) { return -1; } @@ -160,7 +148,6 @@ int32_t UpdateStatisticInfo(uint16_t logType, uint32_t domain, StatisticInfo& in newInfo->cacheLen = info.cacheLen; statisticInfo[logType].insert({domain, newInfo}); } - //printf("after update logtype: %d, domain: %d, cacheLen: %lld\n", logType, domain, statisticInfo[logType][domain]->cacheLen); return 0; } diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 18e6d19..125e5dc 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -318,12 +318,6 @@ int32_t StatisticInfoOp(SeqPacketSocketClient& controller, uint8_t msgCmd, uint32_t domain; if (domainStr == "") { domain = 0xffffffff; - /* - if (logType == 0xffff) { - cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; - return RET_FAIL; - } - */ } else { std::istringstream(domainStr) >> domain; if (domain == 0 || domain > DOMAIN_MAX_SCOPE) { diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 3fc4f92..899ba4f 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -245,7 +245,6 @@ int32_t ControlCmdResult(const char* message) } else { bool isOut[LOG_TYPE_MAX] = {false}; while (infoRst && resultLen < msgLen) { - //printf("logtype %d, domain %d, print %d, cache %d, drop %d\n", infoRst->logType, infoRst->domain, infoRst->logType, infoRst->cacheLen, infoRst->dropped); std::ostringstream oss; if (!isOut[infoRst->logType]) { oss << std::left << std::setw(20) << GetLogTypeStr(infoRst->logType) << std::endl; @@ -255,19 +254,6 @@ int32_t ControlCmdResult(const char* message) << std::setw(20) << "printLen:" + GetByteLenStr(infoRst->printLen) << std::setw(20) << "cacheLen:" + GetByteLenStr(infoRst->cacheLen) << std::setw(20) << "dropped lines:" + GetByteLenStr(infoRst->dropped) << std::endl; std::cout << oss.str(); - /* - outputStr += "logType:"; - outputStr += GetLogTypeStr(infoRst->logType); - outputStr += " domain:"; - outputStr += to_string(infoRst->domain); - outputStr += " printLen:"; - outputStr += GetByteLenStr(infoRst->printLen); - outputStr += " cacheLen:"; - outputStr += GetByteLenStr(infoRst->cacheLen); - outputStr += " dropped num:"; - outputStr += GetByteLenStr(infoRst->dropped); - outputStr += "\n"; - */ infoRst++; resultLen += sizeof(StatisticInfoResult); } -- Gitee From 8a85d85cccca45d525ac540e40a0e0cb90e58354 Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Tue, 21 Dec 2021 03:50:27 -0500 Subject: [PATCH 07/28] size support float Signed-off-by: youyouyuai --- services/hilogtool/log_display.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 899ba4f..969d0b8 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -155,20 +155,24 @@ string GetByteLenStr(uint64_t buffSize) buffSizeStr += to_string(buffSize); buffSizeStr += "B"; } else if (buffSize < ONE_MB) { - buffSize = buffSize / ONE_KB; - buffSizeStr += to_string(buffSize); + std::ostringstream oss; + oss << setiosflags(ios::fixed) << setprecision(2) << std::fixed << (double)buffSize / ONE_KB; + buffSizeStr += oss.str(); buffSizeStr += "K"; } else if (buffSize < ONE_GB) { - buffSize = buffSize / ONE_MB; - buffSizeStr += to_string(buffSize); + std::ostringstream oss; + oss << setiosflags(ios::fixed) << setprecision(2) << std::fixed << (double)buffSize / ONE_MB; + buffSizeStr += oss.str(); buffSizeStr += "M"; } else if (buffSize < ONE_TB) { - buffSize = buffSize / ONE_GB; - buffSizeStr += to_string(buffSize); + std::ostringstream oss; + oss << setiosflags(ios::fixed) << setprecision(2) << std::fixed << (double)buffSize / ONE_GB; + buffSizeStr += oss.str(); buffSizeStr += "G"; } else { - buffSize = buffSize / ONE_TB; - buffSizeStr += to_string(buffSize); + std::ostringstream oss; + oss << setiosflags(ios::fixed) << setprecision(2) << std::fixed << (double)buffSize / ONE_TB; + buffSizeStr += oss.str(); buffSizeStr += "T"; } return buffSizeStr; -- Gitee From 2fdc04e860423604bdf909d7a17cc1c1125ae84a Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Tue, 21 Dec 2021 04:19:54 -0500 Subject: [PATCH 08/28] fix size presise Signed-off-by: youyouyuai --- services/hilogtool/log_display.cpp | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 969d0b8..652aa16 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -149,6 +149,32 @@ string GetPressAlgStr(uint16_t pressAlg) } string GetByteLenStr(uint64_t buffSize) +{ + string buffSizeStr; + if (buffSize < ONE_KB) { + buffSizeStr += to_string(buffSize); + buffSizeStr += "B"; + } else if (buffSize < ONE_MB) { + buffSize = buffSize / ONE_KB; + buffSizeStr += to_string(buffSize); + buffSizeStr += "K"; + } else if (buffSize < ONE_GB) { + buffSize = buffSize / ONE_MB; + buffSizeStr += to_string(buffSize); + buffSizeStr += "M"; + } else if (buffSize < ONE_TB) { + buffSize = buffSize / ONE_GB; + buffSizeStr += to_string(buffSize); + buffSizeStr += "G"; + } else { + buffSize = buffSize / ONE_TB; + buffSizeStr += to_string(buffSize); + buffSizeStr += "T"; + } + return buffSizeStr; +} + +string GetByteLenPrecise(uint64_t buffSize) { string buffSizeStr; if (buffSize < ONE_KB) { @@ -255,8 +281,8 @@ int32_t ControlCmdResult(const char* message) isOut[infoRst->logType] = true; } oss << std::left << std::setw(5) << "" << std::setw(20) << "domain:" + to_string(infoRst->domain) - << std::setw(20) << "printLen:" + GetByteLenStr(infoRst->printLen) << std::setw(20) << "cacheLen:" + - GetByteLenStr(infoRst->cacheLen) << std::setw(20) << "dropped lines:" + GetByteLenStr(infoRst->dropped) << std::endl; + << std::setw(20) << "printLen:" + GetByteLenPrecise(infoRst->printLen) << std::setw(20) << "cacheLen:" + + GetByteLenPrecise(infoRst->cacheLen) << std::setw(20) << "dropped lines:" + GetByteLenPrecise(infoRst->dropped) << std::endl; std::cout << oss.str(); infoRst++; resultLen += sizeof(StatisticInfoResult); -- Gitee From 51bed763f00d4c86a83e4aeaf9e4968af255bcf7 Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Tue, 21 Dec 2021 04:40:24 -0500 Subject: [PATCH 09/28] format statistics Signed-off-by: youyouyuai --- services/hilogtool/log_display.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 652aa16..4fc6970 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -273,20 +273,18 @@ int32_t ControlCmdResult(const char* message) } else if (infoRst->domain == 0xffffffff){ logOrDomain = GetLogTypeStr(infoRst->logType); } else { - bool isOut[LOG_TYPE_MAX] = {false}; + std::ostringstream oss; + oss << std::left << std::setw(20) << "TYPE" << std::setw(20) << "DOMAINID" << std::setw(20) << + "DOMAINNAME" << std::setw(20) << "PRINTED" << std::setw(20) << "CACHED" << std::setw(20) << "DROPPED" + << std::endl; while (infoRst && resultLen < msgLen) { - std::ostringstream oss; - if (!isOut[infoRst->logType]) { - oss << std::left << std::setw(20) << GetLogTypeStr(infoRst->logType) << std::endl; - isOut[infoRst->logType] = true; - } - oss << std::left << std::setw(5) << "" << std::setw(20) << "domain:" + to_string(infoRst->domain) - << std::setw(20) << "printLen:" + GetByteLenPrecise(infoRst->printLen) << std::setw(20) << "cacheLen:" + - GetByteLenPrecise(infoRst->cacheLen) << std::setw(20) << "dropped lines:" + GetByteLenPrecise(infoRst->dropped) << std::endl; - std::cout << oss.str(); + oss << std::setw(20) << GetLogTypeStr(infoRst->logType) << std::setw(20) << to_string(infoRst->domain) + << std::setw(20) << "" << std::setw(20) << GetByteLenPrecise(infoRst->printLen) << std::setw(20) << + GetByteLenPrecise(infoRst->cacheLen) << std::setw(20) << to_string(infoRst->dropped) + "lines" << std::endl; infoRst++; resultLen += sizeof(StatisticInfoResult); } + std::cout << oss.str(); break; } if (infoRst->result == RET_SUCCESS) { -- Gitee From a6267293f61de0c3e7b37c96012a3ff930f928db Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Tue, 21 Dec 2021 20:41:08 -0500 Subject: [PATCH 10/28] domain hex Signed-off-by: youyouyuai --- services/hilogtool/log_display.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 4fc6970..6d41b84 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -278,7 +278,7 @@ int32_t ControlCmdResult(const char* message) "DOMAINNAME" << std::setw(20) << "PRINTED" << std::setw(20) << "CACHED" << std::setw(20) << "DROPPED" << std::endl; while (infoRst && resultLen < msgLen) { - oss << std::setw(20) << GetLogTypeStr(infoRst->logType) << std::setw(20) << to_string(infoRst->domain) + oss << std::setw(20) << GetLogTypeStr(infoRst->logType) << "0x" << std::setw(18) << hex << infoRst->domain << std::setw(20) << "" << std::setw(20) << GetByteLenPrecise(infoRst->printLen) << std::setw(20) << GetByteLenPrecise(infoRst->cacheLen) << std::setw(20) << to_string(infoRst->dropped) + "lines" << std::endl; infoRst++; -- Gitee From 2cf3239da8186e4ae0e7d54f42f76a689df9e564 Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Mon, 27 Dec 2021 21:29:19 -0500 Subject: [PATCH 11/28] statistic class Signed-off-by: youyouyuai --- services/hilogd/BUILD.gn | 2 +- services/hilogd/cmd_executor.cpp | 2 +- services/hilogd/flow_control_init.cpp | 11 -- services/hilogd/include/cmd_executor.h | 5 +- services/hilogd/include/log_buffer.h | 6 +- services/hilogd/include/log_collector.h | 4 +- services/hilogd/include/log_querier.h | 2 +- services/hilogd/include/log_reader.h | 2 + .../include/{statistics.h => log_statistic.h} | 42 +++-- services/hilogd/log_buffer.cpp | 23 +-- services/hilogd/log_collector.cpp | 2 + services/hilogd/log_querier.cpp | 61 +++---- services/hilogd/log_reader.cpp | 2 + services/hilogd/log_statistic.cpp | 169 ++++++++++++++++++ services/hilogd/main.cpp | 19 +- services/hilogd/statistics.cpp | 156 ---------------- services/hilogtool/log_controller.cpp | 6 +- services/hilogtool/log_display.cpp | 44 ++--- 18 files changed, 281 insertions(+), 277 deletions(-) rename services/hilogd/include/{statistics.h => log_statistic.h} (46%) create mode 100644 services/hilogd/log_statistic.cpp delete mode 100644 services/hilogd/statistics.cpp diff --git a/services/hilogd/BUILD.gn b/services/hilogd/BUILD.gn index 269b838..3d7ec33 100644 --- a/services/hilogd/BUILD.gn +++ b/services/hilogd/BUILD.gn @@ -36,7 +36,7 @@ ohos_executable("hilogd") { "log_querier.cpp", "log_reader.cpp", "main.cpp", - "statistics.cpp", + "log_statistic.cpp", ] configs = [ ":hilogd_config" ] defines = [ "__RECV_MSG_WITH_UCRED_" ] diff --git a/services/hilogd/cmd_executor.cpp b/services/hilogd/cmd_executor.cpp index 6c3fd6f..a3891d2 100644 --- a/services/hilogd/cmd_executor.cpp +++ b/services/hilogd/cmd_executor.cpp @@ -120,7 +120,7 @@ void CmdExecutor::ClientEventLoop(std::unique_ptr handler) assert(clientInfoIt != m_clients.end()); prctl(PR_SET_NAME, "hilogd.query"); - auto logQuerier = std::make_shared(std::move(handler), m_hilogBuffer); + auto logQuerier = std::make_shared(std::move(handler), m_hilogBuffer, logSta); logQuerier->LogQuerierThreadFunc(logQuerier); std::lock_guard ul(m_finishedClientAccess); diff --git a/services/hilogd/flow_control_init.cpp b/services/hilogd/flow_control_init.cpp index 92be534..10a502e 100644 --- a/services/hilogd/flow_control_init.cpp +++ b/services/hilogd/flow_control_init.cpp @@ -24,7 +24,6 @@ #include #include #include "properties.h" -#include "statistics.h" #include "log_time_stamp.h" namespace OHOS { @@ -116,11 +115,6 @@ int FlowCtrlDomain(HilogMsg* hilogMsg) { uint32_t domain = hilogMsg->domain; uint32_t domainId = (domain & DOMAIN_FILTER) >> DOMAIN_FILTER_SUBSYSTEM; - StatisticInfo info; - info.dropped = 0; - info.printLen = CONTENT_LEN((hilogMsg)); - info.cacheLen = 0; - UpdateStatisticInfo(hilogMsg->type, domain, info); if (hilogMsg->type == LOG_APP || !IsDomainSwitchOn() || IsDebugOn()) { return 0; @@ -138,11 +132,6 @@ int FlowCtrlDomain(HilogMsg* hilogMsg) it->second->sumLen += logLen; ret = 0; } else { /* over quota */ - //IncreaseDropped(domainId, hilogMsg->type); - info.dropped = 1; - info.printLen = 0; - info.cacheLen = 0; - UpdateStatisticInfo(hilogMsg->type, domain, info); it->second->dropped++; ret = -1; } diff --git a/services/hilogd/include/cmd_executor.h b/services/hilogd/include/cmd_executor.h index 1ae86b7..471b159 100644 --- a/services/hilogd/include/cmd_executor.h +++ b/services/hilogd/include/cmd_executor.h @@ -22,6 +22,8 @@ #include #include "log_buffer.h" +#include "log_statistic.h" + namespace OHOS { namespace HiviewDFX { @@ -32,7 +34,7 @@ struct ClientThread { class CmdExecutor { public: - CmdExecutor(HilogBuffer& buffer) : m_hilogBuffer(buffer) {} + CmdExecutor(HilogBuffer& buffer, LogStatistic& logStatistic) : m_hilogBuffer(buffer), logSta(logStatistic) {} ~CmdExecutor(); void MainLoop(); private: @@ -45,6 +47,7 @@ private: std::mutex m_clientAccess; std::vector m_finishedClients; std::mutex m_finishedClientAccess; + LogStatistic& logSta; }; } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/include/log_buffer.h b/services/hilogd/include/log_buffer.h index 217f5d6..5fa5fc6 100644 --- a/services/hilogd/include/log_buffer.h +++ b/services/hilogd/include/log_buffer.h @@ -25,12 +25,13 @@ #include #include "log_reader.h" +#include "log_statistic.h" namespace OHOS { namespace HiviewDFX { class HilogBuffer { public: - HilogBuffer(); + HilogBuffer(LogStatistic& logStatistic); ~HilogBuffer(); std::vector> logReaderList; @@ -45,15 +46,16 @@ public: size_t SetBuffLen(uint16_t logType, uint64_t buffSize); void GetBufferLock(); void ReleaseBufferLock(); + private: size_t size; size_t sizeByType[LOG_TYPE_MAX]; std::list hilogDataList; std::list hilogKlogList; std::shared_mutex hilogBufferMutex; - std::map cacheLenByDomain; bool ConditionMatch(std::shared_ptr reader); void ReturnNoLog(std::shared_ptr reader); + LogStatistic& logSta; }; } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/include/log_collector.h b/services/hilogd/include/log_collector.h index aa55b2f..a2e4faf 100644 --- a/services/hilogd/include/log_collector.h +++ b/services/hilogd/include/log_collector.h @@ -18,12 +18,13 @@ #include "log_buffer.h" #include "hilog_input_socket_server.h" +#include "log_statistic.h" namespace OHOS { namespace HiviewDFX { class LogCollector { public: - LogCollector(HilogBuffer& buffer) : m_hilogBuffer(buffer) {} + LogCollector(HilogBuffer& buffer, LogStatistic& logStatistic) : m_hilogBuffer(buffer), logSta(logStatistic) {} void InsertDropInfo(const HilogMsg &msg, int droppedCount); size_t InsertLogToBuffer(const HilogMsg& msg); #ifndef __RECV_MSG_WITH_UCRED_ @@ -34,6 +35,7 @@ public: ~LogCollector() = default; private: HilogBuffer& m_hilogBuffer; + LogStatistic& logSta; }; } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/include/log_querier.h b/services/hilogd/include/log_querier.h index d14360d..f78e22b 100644 --- a/services/hilogd/include/log_querier.h +++ b/services/hilogd/include/log_querier.h @@ -23,7 +23,7 @@ namespace OHOS { namespace HiviewDFX { class LogQuerier : public LogReader { public: - LogQuerier(std::unique_ptr handler, HilogBuffer& buffer); + LogQuerier(std::unique_ptr handler, HilogBuffer& buffer, LogStatistic& logStatistic); static void LogQuerierThreadFunc(std::shared_ptr logReader); int WriteData(LogQueryResponse& rsp, OptRef pData); int WriteData(OptRef pData); diff --git a/services/hilogd/include/log_reader.h b/services/hilogd/include/log_reader.h index 6235656..8ea244b 100644 --- a/services/hilogd/include/log_reader.h +++ b/services/hilogd/include/log_reader.h @@ -25,6 +25,7 @@ #include "log_msg_wrapper.h" #include "hilogtool_msg.h" #include "socket.h" +#include "log_statistic.h" namespace OHOS { namespace HiviewDFX { @@ -76,6 +77,7 @@ protected: unsigned int sendId = 1; uint8_t cmd = 0; static HilogBuffer* hilogBuffer; + static LogStatistic* logSta; private: bool isReload = true; diff --git a/services/hilogd/include/statistics.h b/services/hilogd/include/log_statistic.h similarity index 46% rename from services/hilogd/include/statistics.h rename to services/hilogd/include/log_statistic.h index cd14292..8b1211c 100644 --- a/services/hilogd/include/statistics.h +++ b/services/hilogd/include/log_statistic.h @@ -12,31 +12,49 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef STATISTICS_H -#define STATISTICS_H +#ifndef LOG_STATISTIC_H +#define LOG_STATISTIC_H #include +#include "hilog_common.h" namespace OHOS { namespace HiviewDFX { -using StatisticInfo = struct { +using Statistic = struct { size_t dropped; - size_t printLen; - size_t cacheLen; + size_t printed; + size_t cached; }; +using StaInfo = struct { + uint16_t logType; + uint32_t domain; + Statistic statistic; +}; -int32_t GetStatisticInfoByLog(uint16_t logType, StatisticInfo& result); -int32_t GetStatisticInfoByDomain(uint32_t domain, StatisticInfo& result); -int32_t ClearStatisticInfoByLog(uint16_t logType); -int32_t ClearStatisticInfoByDomain(uint32_t domain); -int32_t GetStatisticInfo(std::unordered_map>& result); -int32_t ClearStatisticInfo(); -int32_t UpdateStatisticInfo(uint16_t logType, uint32_t domain, StatisticInfo& info); +class LogStatistic { +public: + LogStatistic(); + ~LogStatistic(); + int Printed(const HilogMsg& msg); + int Cached(const HilogMsg& msg); + int CacheDropped(const HilogMsg& msg); + int Dropped(const HilogMsg& msg); + int GetByType(uint16_t logType, Statistic& statistic); + int GetByDomain(uint32_t domain, Statistic& statistic); + int GetStatistic(std::vector& statistic); + int ResetByType(uint16_t logType); + int ResetByDomain(uint32_t domain); + int ResetStatistic(); +private: + std::vector staInfo; + StaInfo* IsExisted(const HilogMsg& msg) ; +}; + } } #endif diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 9a5e6fc..8d11749 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -18,7 +18,6 @@ #include #include "hilog_common.h" #include "flow_control_init.h" -#include "statistics.h" #include "log_time_stamp.h" namespace OHOS { @@ -33,7 +32,7 @@ const int DOMAIN_FUZZY_MASK = 0xdffff; const int DOMAIN_MODULE_BITS = 8; const int MAX_TIME_DIFF = 5; -HilogBuffer::HilogBuffer() +HilogBuffer::HilogBuffer(LogStatistic& logStatistic) : logSta(logStatistic) { size = 0; for (int i = 0; i < LOG_TYPE_MAX; i++) { @@ -77,11 +76,8 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) size_t cLen = it->len - it->tag_len; size -= cLen; sizeByType[(*it).type] -= cLen; - StatisticInfo info; - info.dropped = 0; - info.printLen = 0; - info.cacheLen = 0 - cLen; - UpdateStatisticInfo((*it).type, (*it).domain, info); + HilogMsg* tmp = (HilogMsg*)(&(*it)); + logSta.CacheDropped(*tmp); it = msgList.erase(it); } // Re-confirm if enough elements has been removed @@ -112,11 +108,7 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) // Update current size of HilogBuffer size += eleSize; sizeByType[msg.type] += eleSize; - StatisticInfo info; - info.dropped = 0; - info.printLen = 0; - info.cacheLen = eleSize; - UpdateStatisticInfo(msg.type, msg.domain, info); + logSta.Cached(msg); return eleSize; } @@ -188,11 +180,8 @@ size_t HilogBuffer::Delete(uint16_t logType) sum += cLen; sizeByType[(*it).type] -= cLen; size -= cLen; - StatisticInfo info; - info.dropped = 0; - info.printLen = 0; - info.cacheLen = 0 - cLen; - UpdateStatisticInfo((*it).type, (*it).domain, info); + HilogMsg* tmp = (HilogMsg*)(&(*it)); + logSta.CacheDropped(*tmp); it = msgList.erase(it); } diff --git a/services/hilogd/log_collector.cpp b/services/hilogd/log_collector.cpp index ee69bcb..b96dbff 100644 --- a/services/hilogd/log_collector.cpp +++ b/services/hilogd/log_collector.cpp @@ -76,6 +76,7 @@ void LogCollector::onDataRecv(const ucred& cred, std::vector& data) #ifdef __RECV_MSG_WITH_UCRED_ msg->pid = cred.pid; #endif + logSta.Printed(*msg); // Domain flow control int ret = FlowCtrlDomain(msg); if (ret < 0) { @@ -84,6 +85,7 @@ void LogCollector::onDataRecv(const ucred& cred, std::vector& data) } else if (ret > 0) { /* if >0 !Need print how many lines was dopped */ // store info how many was dropped InsertDropInfo(*msg, ret); + logSta.Dropped(*msg); } InsertLogToBuffer(*msg); } diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 233844e..0d76e36 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -36,7 +36,6 @@ #include "log_buffer.h" #include "log_persister.h" #include "log_reader.h" -#include "statistics.h" namespace OHOS { namespace HiviewDFX { @@ -367,7 +366,8 @@ void HandleBufferSizeRequest(char* reqMsg, std::shared_ptr logReader, logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); } -void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, HilogBuffer* buffer) +void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, HilogBuffer* buffer, + LogStatistic* logSta) { char msgToSend[MAX_DATA_LEN]; int32_t rst = 0; @@ -379,39 +379,35 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, if (pStatisticInfoQueryReq->logType != 0xffff) { pRst->logType = pStatisticInfoQueryReq->logType; pRst->domain = pStatisticInfoQueryReq->domain; - StatisticInfo info; - rst = GetStatisticInfoByLog(pStatisticInfoQueryReq->logType, info); + Statistic info; + rst = logSta->GetByType(pStatisticInfoQueryReq->logType, info); pRst->dropped = info.dropped; - pRst->printLen = info.printLen; - pRst->cacheLen = info.cacheLen; + pRst->printLen = info.printed; + pRst->cacheLen = info.cached; pRst->result = (rst < 0) ? rst : RET_SUCCESS; rstNum ++; } else if (pStatisticInfoQueryReq->domain != 0xffffffff) { pRst->logType = pStatisticInfoQueryReq->logType; pRst->domain = pStatisticInfoQueryReq->domain; - StatisticInfo info; - rst = GetStatisticInfoByDomain(pStatisticInfoQueryReq->domain, info); + Statistic info; + rst = logSta->GetByDomain(pStatisticInfoQueryReq->domain, info); pRst->dropped = info.dropped; - pRst->printLen = info.printLen; - pRst->cacheLen = info.cacheLen; + pRst->printLen = info.printed; + pRst->cacheLen = info.cached; pRst->result = (rst < 0) ? rst : RET_SUCCESS; rstNum ++; } else { - std::unordered_map> infoMap; - rst = GetStatisticInfo(infoMap); - std::unordered_map>::iterator it; - for (it = infoMap.begin(); it != infoMap.end(); it++) { - std::unordered_map::iterator iter; - for (iter = it->second.begin(); iter != it->second.end(); iter++) { - pRst->result = rst; - pRst->logType = it->first; - pRst->domain = iter->first; - pRst->dropped = iter->second->dropped; - pRst->printLen = iter->second->printLen; - pRst->cacheLen = iter->second->cacheLen; - rstNum ++; - pRst ++; - } + std::vector info; + rst = logSta->GetStatistic(info); + for (StaInfo& tmp : info) { + pRst->result = rst; + pRst->logType = tmp.logType; + pRst->domain = tmp.domain; + pRst->dropped = tmp.statistic.dropped; + pRst->printLen = tmp.statistic.printed; + pRst->cacheLen = tmp.statistic.cached; + rstNum ++; + pRst ++; } } uint16_t sendMsgLen = sizeof(StatisticInfoResult) * rstNum; @@ -419,7 +415,7 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); } -void HandleInfoClearRequest(char* reqMsg, std::shared_ptr logReader, HilogBuffer* buffer) +void HandleInfoClearRequest(char* reqMsg, std::shared_ptr logReader, HilogBuffer* buffer, LogStatistic* logSta) { char msgToSend[MAX_DATA_LEN]; int32_t rst = 0; @@ -429,11 +425,11 @@ void HandleInfoClearRequest(char* reqMsg, std::shared_ptr logReader, pStatisticInfoClearRsp->logType = pStatisticInfoClearReq->logType; pStatisticInfoClearRsp->domain = pStatisticInfoClearReq->domain; if (pStatisticInfoClearReq->logType != 0xffff) { - rst = ClearStatisticInfoByLog(pStatisticInfoClearReq->logType); + rst = logSta->ResetByType(pStatisticInfoClearReq->logType); } else if (pStatisticInfoClearReq->domain != 0xffffffff) { - rst = ClearStatisticInfoByDomain(pStatisticInfoClearReq->domain); + rst = logSta->ResetByDomain(pStatisticInfoClearReq->domain); } else { - rst = ClearStatisticInfo(); + rst = logSta->ResetStatistic(); } pStatisticInfoClearRsp->result = (rst < 0) ? rst : RET_SUCCESS; SetMsgHead(pStatisticInfoClearRsp->msgHeader, MC_RSP_STATISTIC_INFO_CLEAR, sizeof(StatisticInfoClearResponse) - @@ -546,10 +542,10 @@ void LogQuerier::LogQuerierThreadFunc(std::shared_ptr logReader) HandleBufferSizeRequest(g_tempBuffer, logReader, hilogBuffer); break; case MC_REQ_STATISTIC_INFO_QUERY: - HandleInfoQueryRequest(g_tempBuffer, logReader, hilogBuffer); + HandleInfoQueryRequest(g_tempBuffer, logReader, hilogBuffer, logSta); break; case MC_REQ_STATISTIC_INFO_CLEAR: - HandleInfoClearRequest(g_tempBuffer, logReader, hilogBuffer); + HandleInfoClearRequest(g_tempBuffer, logReader, hilogBuffer, logSta); break; case MC_REQ_LOG_CLEAR: HandleBufferClearRequest(g_tempBuffer, logReader, hilogBuffer); @@ -561,10 +557,11 @@ void LogQuerier::LogQuerierThreadFunc(std::shared_ptr logReader) hilogBuffer->RemoveLogReader(logReader); } -LogQuerier::LogQuerier(std::unique_ptr handler, HilogBuffer& buffer) +LogQuerier::LogQuerier(std::unique_ptr handler, HilogBuffer& buffer, LogStatistic& logStatistic) { hilogtoolConnectSocket = std::move(handler); hilogBuffer = &buffer; + logSta = &logStatistic; } int LogQuerier::WriteData(LogQueryResponse& rsp, OptRef pData) diff --git a/services/hilogd/log_reader.cpp b/services/hilogd/log_reader.cpp index b3c7b17..7f1b428 100644 --- a/services/hilogd/log_reader.cpp +++ b/services/hilogd/log_reader.cpp @@ -27,6 +27,8 @@ namespace HiviewDFX { using namespace std; HilogBuffer* LogReader::hilogBuffer = nullptr; +LogStatistic* LogReader::logSta = nullptr; + LogReader::LogReader() { isNotified = false; diff --git a/services/hilogd/log_statistic.cpp b/services/hilogd/log_statistic.cpp new file mode 100644 index 0000000..b1c66ce --- /dev/null +++ b/services/hilogd/log_statistic.cpp @@ -0,0 +1,169 @@ +/* + * 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. + */ + +#include +#include +#include +#include +#include +#include "log_statistic.h" + +namespace OHOS { +namespace HiviewDFX { + + +LogStatistic::LogStatistic() +{} + +LogStatistic::~LogStatistic() +{} + +StaInfo* LogStatistic::IsExisted(const HilogMsg& msg) +{ + for (StaInfo& tmp : staInfo) { + if (tmp.logType == msg.type && tmp.domain == msg.domain) { + return &tmp; + } + } + return nullptr; +} + +int LogStatistic::Printed(const HilogMsg& msg) +{ + StaInfo* info = IsExisted(msg); + if (info) { + info->statistic.printed += CONTENT_LEN((&msg)); + } else { + StaInfo tmp; + tmp.logType = msg.type; + tmp.domain = msg.domain; + tmp.statistic.printed = CONTENT_LEN((&msg)); + tmp.statistic.cached = 0; + tmp.statistic.dropped = 0; + staInfo.emplace_back(tmp); + } + + return 0; +} + +int LogStatistic::Cached(const HilogMsg& msg) +{ + StaInfo* info = IsExisted(msg); + if (info) { + info->statistic.cached += CONTENT_LEN((&msg)); + } else { + StaInfo tmp; + tmp.logType = msg.type; + tmp.domain = msg.domain; + tmp.statistic.printed = 0; + tmp.statistic.cached = CONTENT_LEN((&msg)); + tmp.statistic.dropped = 0; + staInfo.emplace_back(tmp); + } + return 0; +} + +int LogStatistic::CacheDropped(const HilogMsg& msg) +{ + StaInfo* info = IsExisted(msg); + if (info) { + //printf("type %d, domain %d, len %d", msg.type, msg.domain, CONTENT_LEN((&msg))); + info->statistic.cached -= (msg.len - msg.tag_len); + } + return 0; +} + +int LogStatistic::Dropped(const HilogMsg& msg) +{ + StaInfo* info = IsExisted(msg); + if (info) { + info->statistic.printed += 1; + } else { + StaInfo tmp; + tmp.logType = msg.type; + tmp.domain = msg.domain; + tmp.statistic.printed = 0; + tmp.statistic.cached = 0; + tmp.statistic.dropped = 1; + staInfo.emplace_back(tmp); + } + return 0; +} + +int LogStatistic::GetByType(uint16_t logType, Statistic& statistic) +{ + statistic = {0}; + for (StaInfo& info : staInfo) { + if (info.logType == logType) { + statistic.printed += info.statistic.printed; + statistic.cached += info.statistic.cached; + statistic.dropped += info.statistic.dropped; + } + } + return 0; +} + +int LogStatistic::GetByDomain(uint32_t domain, Statistic& statistic) +{ + statistic = {0}; + for (StaInfo& info : staInfo) { + if (info.domain == domain) { + statistic.printed += info.statistic.printed; + statistic.cached += info.statistic.cached; + statistic.dropped += info.statistic.dropped; + } + } + return 0; +} + +int LogStatistic::GetStatistic(std::vector& statistic) +{ + statistic = staInfo; + return 0; +} + +int LogStatistic::ResetByType(uint16_t logType) +{ + for (StaInfo& info : staInfo) { + if (info.logType == logType) { + info.statistic.printed = 0; + info.statistic.dropped = 0; + } + } + return 0; +} + +int LogStatistic::ResetByDomain(uint32_t domain) +{ + for (StaInfo& info : staInfo) { + if (info.domain == domain) { + info.statistic.printed = 0; + info.statistic.dropped = 0; + } + } + return 0; +} + +int LogStatistic::ResetStatistic() +{ + for (StaInfo& info : staInfo) { + info.statistic.printed = 0; + info.statistic.dropped = 0; + } + return 0; +} + +} +} diff --git a/services/hilogd/main.cpp b/services/hilogd/main.cpp index 7ba78af..8e4d884 100644 --- a/services/hilogd/main.cpp +++ b/services/hilogd/main.cpp @@ -58,7 +58,8 @@ static void SigHandler(int sig) int HilogdEntry() { - HilogBuffer hilogBuffer; + LogStatistic logStatistic; + HilogBuffer hilogBuffer(logStatistic); umask(HILOG_FILE_MASK); #ifdef DEBUG int fd = open(HILOG_FILE_DIR"hilogd.txt", O_WRONLY | O_APPEND); @@ -74,13 +75,13 @@ int HilogdEntry() // Start log_collector #ifndef __RECV_MSG_WITH_UCRED_ - auto onDataReceive = [&hilogBuffer](std::vector& data) { - static LogCollector logCollector(hilogBuffer); + auto onDataReceive = [&hilogBuffer, &logStatistic](std::vector& data) { + static LogCollector logCollector(hilogBuffer, logStatistic); logCollector.onDataRecv(data); }; #else - auto onDataReceive = [&hilogBuffer](const ucred& cred, std::vector& data) { - static LogCollector logCollector(hilogBuffer); + auto onDataReceive = [&hilogBuffer, &logStatistic](const ucred& cred, std::vector& data) { + static LogCollector logCollector(hilogBuffer, logStatistic); logCollector.onDataRecv(cred, data); }; #endif @@ -100,17 +101,17 @@ int HilogdEntry() incomingLogsServer.RunServingThread(); } - auto startupCheckTask = std::async(std::launch::async, [&hilogBuffer]() { + auto startupCheckTask = std::async(std::launch::async, [&hilogBuffer, &logStatistic]() { prctl(PR_SET_NAME, "hilogd.pst_res"); - std::shared_ptr logQuerier = std::make_shared(nullptr, hilogBuffer); + std::shared_ptr logQuerier = std::make_shared(nullptr, hilogBuffer, logStatistic); logQuerier->RestorePersistJobs(hilogBuffer); }); - auto kmsgTask = std::async(std::launch::async, [&hilogBuffer]() { + auto kmsgTask = std::async(std::launch::async, [&hilogBuffer, &logStatistic]() { LogKmsg logKmsg(hilogBuffer); logKmsg.ReadAllKmsg(); }); - CmdExecutor cmdExecutor(hilogBuffer); + CmdExecutor cmdExecutor(hilogBuffer, logStatistic); cmdExecutor.MainLoop(); return 0; } diff --git a/services/hilogd/statistics.cpp b/services/hilogd/statistics.cpp deleted file mode 100644 index 857dd11..0000000 --- a/services/hilogd/statistics.cpp +++ /dev/null @@ -1,156 +0,0 @@ -/* - * 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. - */ - -#include -#include -#include -#include -#include -#include "statistics.h" - -namespace OHOS { -namespace HiviewDFX { - - -static std::unordered_map> statisticInfo; - - -int32_t GetStatisticInfoByLog(uint16_t logType, StatisticInfo& result) -{ - result.dropped = 0; - result.printLen = 0; - result.cacheLen = 0; - std::unordered_map>::iterator it; - for (it = statisticInfo.begin(); it != statisticInfo.end(); it++) { - if (it->first == logType) { - std::unordered_map::iterator iter; - for (iter = it->second.begin(); iter != it->second.end(); iter++) { - result.dropped += iter->second->dropped; - result.printLen += iter->second->printLen; - result.cacheLen += iter->second->cacheLen; - } - - } - } - return 0; -} - -int32_t GetStatisticInfoByDomain(uint32_t domain, StatisticInfo& result) -{ - result.dropped = 0; - result.printLen = 0; - result.cacheLen = 0; - std::unordered_map>::iterator it; - for (it = statisticInfo.begin(); it != statisticInfo.end(); it++) { - std::unordered_map::iterator iter; - for (iter = it->second.begin(); iter != it->second.end(); iter++) { - if (iter->first == domain) { - result.dropped += iter->second->dropped; - result.printLen += iter->second->printLen; - result.cacheLen += iter->second->cacheLen; - } - } - - } - return 0; -} - -int32_t ClearStatisticInfoByLog(uint16_t logType) -{ - std::unordered_map>::iterator it; - for (it = statisticInfo.begin(); it != statisticInfo.end(); it++) { - if (it->first == logType) { - std::unordered_map::iterator iter; - for (iter = it->second.begin(); iter != it->second.end(); iter++) { - iter->second->dropped = 0; - iter->second->printLen = 0; - } - } - } - return 0; -} - -int32_t ClearStatisticInfoByDomain(uint32_t domain) -{ - std::unordered_map>::iterator it; - for (it = statisticInfo.begin(); it != statisticInfo.end(); it++) { - std::unordered_map::iterator iter; - if (iter->first == domain) { - iter->second->dropped = 0; - iter->second->printLen = 0; - } - } - return 0; -} - -int32_t GetStatisticInfo(std::unordered_map>& result) -{ - result = statisticInfo; - return 0; -} - -int32_t ClearStatisticInfo() -{ - std::unordered_map>::iterator it; - for (it = statisticInfo.begin(); it != statisticInfo.end(); it++) { - std::unordered_map::iterator iter; - for (iter = it->second.begin(); iter != it->second.end(); iter++) { - iter->second->dropped = 0; - iter->second->printLen = 0; - } - } - - return 0; -} - - -int32_t UpdateStatisticInfo(uint16_t logType, uint32_t domain, StatisticInfo& info) -{ - if (logType >= LOG_TYPE_MAX) { - return -1; - } - std::unordered_map>::iterator it = statisticInfo.find(logType); - if (it != statisticInfo.end()) { - std::unordered_map::iterator iter = it->second.find(domain); - if (iter != it->second.end()) { - iter->second->dropped += info.dropped; - iter->second->printLen += info.printLen; - iter->second->cacheLen += info.cacheLen; - } else { - StatisticInfo* newInfo = new StatisticInfo; - if (!newInfo) { - return -1; - } - newInfo->dropped = info.dropped; - newInfo->printLen = info.printLen; - newInfo->cacheLen = info.cacheLen; - it->second.insert({domain, newInfo}); - } - } else { - StatisticInfo* newInfo = new StatisticInfo; - if (!newInfo) { - return -1; - } - newInfo->dropped = info.dropped; - newInfo->printLen = info.printLen; - newInfo->cacheLen = info.cacheLen; - statisticInfo[logType].insert({domain, newInfo}); - } - return 0; -} - - -} -} diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 125e5dc..f897e92 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -319,7 +319,11 @@ int32_t StatisticInfoOp(SeqPacketSocketClient& controller, uint8_t msgCmd, if (domainStr == "") { domain = 0xffffffff; } else { - std::istringstream(domainStr) >> domain; + if (domainStr.rfind("0x", 0) == 0 || domainStr.rfind("0X", 0) == 0) { + std::istringstream(domainStr) >> hex >> domain; + } else { + std::istringstream(domainStr) >> domain; + } if (domain == 0 || domain > DOMAIN_MAX_SCOPE) { cout << ParseErrorCode(ERR_DOMAIN_INVALID) << endl; return RET_FAIL; diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 6d41b84..7c9b16f 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -264,16 +264,18 @@ int32_t ControlCmdResult(const char* message) case MC_RSP_STATISTIC_INFO_QUERY: { StatisticInfoQueryResponse* staInfoQueryRsp = (StatisticInfoQueryResponse*)message; StatisticInfoResult* infoRst = (StatisticInfoResult*)staInfoQueryRsp->statisticInfoRst; - string logOrDomain; + std::ostringstream oss; if (!staInfoQueryRsp) { return RET_FAIL; } if (infoRst->logType == 0xffff) { - logOrDomain = to_string(infoRst->domain); + oss << std::left << std::setw(20) << "DOMAINID" << std::setw(20) << "DOMAINNAME" + << std::setw(20) << "PRINTED" << std::setw(20) << "CACHED" << std::setw(20) << "DROPPED" + << std::endl << "0x" << std::setw(18) << hex << infoRst->domain << std::setw(20) << ""; } else if (infoRst->domain == 0xffffffff){ - logOrDomain = GetLogTypeStr(infoRst->logType); + oss << std::left << std::setw(20) << "TYPE" << std::setw(20) << "PRINTED" << std::setw(20) << "CACHED" << std::setw(20) << "DROPPED" + << std::endl << std::setw(20) << GetLogTypeStr(infoRst->logType); } else { - std::ostringstream oss; oss << std::left << std::setw(20) << "TYPE" << std::setw(20) << "DOMAINID" << std::setw(20) << "DOMAINNAME" << std::setw(20) << "PRINTED" << std::setw(20) << "CACHED" << std::setw(20) << "DROPPED" << std::endl; @@ -288,43 +290,21 @@ int32_t ControlCmdResult(const char* message) break; } if (infoRst->result == RET_SUCCESS) { - outputStr += logOrDomain; - outputStr += " print log length is "; - outputStr += GetByteLenStr(infoRst->printLen); - outputStr += "\n"; - outputStr += logOrDomain; - outputStr += " cache log length is "; - outputStr += GetByteLenStr(infoRst->cacheLen); - outputStr += "\n"; - outputStr += logOrDomain; - outputStr += " dropped log lines is "; - outputStr += GetByteLenStr(infoRst->dropped); + oss << setw(20) << GetByteLenPrecise(infoRst->printLen) << setw(20) << + GetByteLenPrecise(infoRst->cacheLen) << setw(20) << to_string(infoRst->dropped) + "lines" << std::endl; + std::cout << oss.str(); } else if (infoRst->result < 0) { - outputStr += logOrDomain; - outputStr += " statistic info query fail\n"; + outputStr += "Statistic Info Query Failed\n"; outputStr += ParseErrorCode((ErrorCode)infoRst->result); } break; } case MC_RSP_STATISTIC_INFO_CLEAR: { StatisticInfoClearResponse* staInfoClearRsp = (StatisticInfoClearResponse*)message; - string logOrDomain; - if (!staInfoClearRsp) { - return RET_FAIL; - } - if (staInfoClearRsp->domain != 0xffffffff) { - logOrDomain = to_string(staInfoClearRsp->domain); - } else if (staInfoClearRsp->logType != 0xffff) { - logOrDomain = GetLogTypeStr(staInfoClearRsp->logType); - } else { - logOrDomain = "all"; - } if (staInfoClearRsp->result == RET_SUCCESS) { - outputStr += logOrDomain; - outputStr += " statistic info clear success "; + outputStr += "Statistic Info Clear Success "; } else if (staInfoClearRsp->result < 0) { - outputStr += logOrDomain; - outputStr += " statistic info clear fail\n"; + outputStr += "Statistic Info Clear Failed\n"; outputStr += ParseErrorCode((ErrorCode)staInfoClearRsp->result); } break; -- Gitee From 7f16ad2da54ce82b34909e9d080df8dabeb06001 Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Tue, 28 Dec 2021 04:58:18 -0500 Subject: [PATCH 12/28] rename Signed-off-by: youyouyuai --- services/hilogd/include/log_buffer.h | 4 ++-- services/hilogd/include/log_collector.h | 4 ++-- services/hilogd/include/log_querier.h | 2 +- services/hilogd/include/log_reader.h | 2 +- services/hilogd/log_buffer.cpp | 8 ++++---- services/hilogd/log_collector.cpp | 4 ++-- services/hilogd/log_querier.cpp | 25 +++++++++++++------------ services/hilogd/log_reader.cpp | 2 +- 8 files changed, 26 insertions(+), 25 deletions(-) diff --git a/services/hilogd/include/log_buffer.h b/services/hilogd/include/log_buffer.h index 5fa5fc6..b1104d9 100644 --- a/services/hilogd/include/log_buffer.h +++ b/services/hilogd/include/log_buffer.h @@ -31,7 +31,7 @@ namespace OHOS { namespace HiviewDFX { class HilogBuffer { public: - HilogBuffer(LogStatistic& logStatistic); + HilogBuffer(LogStatistic& statistic); ~HilogBuffer(); std::vector> logReaderList; @@ -55,7 +55,7 @@ private: std::shared_mutex hilogBufferMutex; bool ConditionMatch(std::shared_ptr reader); void ReturnNoLog(std::shared_ptr reader); - LogStatistic& logSta; + LogStatistic& m_logStatistic; }; } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/include/log_collector.h b/services/hilogd/include/log_collector.h index a2e4faf..9aa08df 100644 --- a/services/hilogd/include/log_collector.h +++ b/services/hilogd/include/log_collector.h @@ -24,7 +24,7 @@ namespace OHOS { namespace HiviewDFX { class LogCollector { public: - LogCollector(HilogBuffer& buffer, LogStatistic& logStatistic) : m_hilogBuffer(buffer), logSta(logStatistic) {} + LogCollector(HilogBuffer& buffer, LogStatistic& statistic) : m_hilogBuffer(buffer), m_logStatistic(statistic) {} void InsertDropInfo(const HilogMsg &msg, int droppedCount); size_t InsertLogToBuffer(const HilogMsg& msg); #ifndef __RECV_MSG_WITH_UCRED_ @@ -35,7 +35,7 @@ public: ~LogCollector() = default; private: HilogBuffer& m_hilogBuffer; - LogStatistic& logSta; + LogStatistic& m_logStatistic; }; } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/include/log_querier.h b/services/hilogd/include/log_querier.h index f78e22b..aa845ef 100644 --- a/services/hilogd/include/log_querier.h +++ b/services/hilogd/include/log_querier.h @@ -23,7 +23,7 @@ namespace OHOS { namespace HiviewDFX { class LogQuerier : public LogReader { public: - LogQuerier(std::unique_ptr handler, HilogBuffer& buffer, LogStatistic& logStatistic); + LogQuerier(std::unique_ptr handler, HilogBuffer& buffer, LogStatistic& statistic); static void LogQuerierThreadFunc(std::shared_ptr logReader); int WriteData(LogQueryResponse& rsp, OptRef pData); int WriteData(OptRef pData); diff --git a/services/hilogd/include/log_reader.h b/services/hilogd/include/log_reader.h index 8ea244b..32da9a0 100644 --- a/services/hilogd/include/log_reader.h +++ b/services/hilogd/include/log_reader.h @@ -77,7 +77,7 @@ protected: unsigned int sendId = 1; uint8_t cmd = 0; static HilogBuffer* hilogBuffer; - static LogStatistic* logSta; + static LogStatistic* logStatistic; private: bool isReload = true; diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index cc43ed9..a174013 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -32,7 +32,7 @@ const int DOMAIN_FUZZY_MASK = 0xdffff; const int DOMAIN_MODULE_BITS = 8; const int MAX_TIME_DIFF = 5; -HilogBuffer::HilogBuffer(LogStatistic& logStatistic) : logSta(logStatistic) +HilogBuffer::HilogBuffer(LogStatistic& statistic) : m_logStatistic(statistic) { size = 0; for (int i = 0; i < LOG_TYPE_MAX; i++) { @@ -81,7 +81,7 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) size -= cLen; sizeByType[(*it).type] -= cLen; HilogMsg* tmp = (HilogMsg*)(&(*it)); - logSta.CacheDropped(*tmp); + m_logStatistic.CacheDropped(*tmp); it = msgList.erase(it); } // Re-confirm if enough elements has been removed @@ -112,7 +112,7 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) // Update current size of HilogBuffer size += eleSize; sizeByType[msg.type] += eleSize; - logSta.Cached(msg); + m_logStatistic.Cached(msg); return eleSize; } @@ -188,7 +188,7 @@ size_t HilogBuffer::Delete(uint16_t logType) sizeByType[(*it).type] -= cLen; size -= cLen; HilogMsg* tmp = (HilogMsg*)(&(*it)); - logSta.CacheDropped(*tmp); + m_logStatistic.CacheDropped(*tmp); it = msgList.erase(it); } diff --git a/services/hilogd/log_collector.cpp b/services/hilogd/log_collector.cpp index 7caedc9..0a5c176 100644 --- a/services/hilogd/log_collector.cpp +++ b/services/hilogd/log_collector.cpp @@ -76,7 +76,7 @@ void LogCollector::onDataRecv(const ucred& cred, std::vector& data) #ifdef __RECV_MSG_WITH_UCRED_ msg->pid = cred.pid; #endif - logSta.Printed(*msg); + m_logStatistic.Printed(*msg); // Domain flow control int ret = FlowCtrlDomain(msg); if (ret < 0) { @@ -85,7 +85,7 @@ void LogCollector::onDataRecv(const ucred& cred, std::vector& data) } else if (ret > 0) { /* if >0 !Need print how many lines was dopped */ // store info how many was dropped InsertDropInfo(*msg, ret); - logSta.Dropped(*msg); + m_logStatistic.Dropped(*msg); } InsertLogToBuffer(*msg); } diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 0d76e36..8446593 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -367,7 +367,7 @@ void HandleBufferSizeRequest(char* reqMsg, std::shared_ptr logReader, } void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, HilogBuffer* buffer, - LogStatistic* logSta) + LogStatistic* statistic) { char msgToSend[MAX_DATA_LEN]; int32_t rst = 0; @@ -380,7 +380,7 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, pRst->logType = pStatisticInfoQueryReq->logType; pRst->domain = pStatisticInfoQueryReq->domain; Statistic info; - rst = logSta->GetByType(pStatisticInfoQueryReq->logType, info); + rst = statistic->GetByType(pStatisticInfoQueryReq->logType, info); pRst->dropped = info.dropped; pRst->printLen = info.printed; pRst->cacheLen = info.cached; @@ -390,7 +390,7 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, pRst->logType = pStatisticInfoQueryReq->logType; pRst->domain = pStatisticInfoQueryReq->domain; Statistic info; - rst = logSta->GetByDomain(pStatisticInfoQueryReq->domain, info); + rst = statistic->GetByDomain(pStatisticInfoQueryReq->domain, info); pRst->dropped = info.dropped; pRst->printLen = info.printed; pRst->cacheLen = info.cached; @@ -398,7 +398,7 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, rstNum ++; } else { std::vector info; - rst = logSta->GetStatistic(info); + rst = statistic->GetStatistic(info); for (StaInfo& tmp : info) { pRst->result = rst; pRst->logType = tmp.logType; @@ -415,7 +415,8 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); } -void HandleInfoClearRequest(char* reqMsg, std::shared_ptr logReader, HilogBuffer* buffer, LogStatistic* logSta) +void HandleInfoClearRequest(char* reqMsg, std::shared_ptr logReader, HilogBuffer* buffer, + LogStatistic* statistic) { char msgToSend[MAX_DATA_LEN]; int32_t rst = 0; @@ -425,11 +426,11 @@ void HandleInfoClearRequest(char* reqMsg, std::shared_ptr logReader, pStatisticInfoClearRsp->logType = pStatisticInfoClearReq->logType; pStatisticInfoClearRsp->domain = pStatisticInfoClearReq->domain; if (pStatisticInfoClearReq->logType != 0xffff) { - rst = logSta->ResetByType(pStatisticInfoClearReq->logType); + rst = statistic->ResetByType(pStatisticInfoClearReq->logType); } else if (pStatisticInfoClearReq->domain != 0xffffffff) { - rst = logSta->ResetByDomain(pStatisticInfoClearReq->domain); + rst = statistic->ResetByDomain(pStatisticInfoClearReq->domain); } else { - rst = logSta->ResetStatistic(); + rst = statistic->ResetStatistic(); } pStatisticInfoClearRsp->result = (rst < 0) ? rst : RET_SUCCESS; SetMsgHead(pStatisticInfoClearRsp->msgHeader, MC_RSP_STATISTIC_INFO_CLEAR, sizeof(StatisticInfoClearResponse) - @@ -542,10 +543,10 @@ void LogQuerier::LogQuerierThreadFunc(std::shared_ptr logReader) HandleBufferSizeRequest(g_tempBuffer, logReader, hilogBuffer); break; case MC_REQ_STATISTIC_INFO_QUERY: - HandleInfoQueryRequest(g_tempBuffer, logReader, hilogBuffer, logSta); + HandleInfoQueryRequest(g_tempBuffer, logReader, hilogBuffer, logStatistic); break; case MC_REQ_STATISTIC_INFO_CLEAR: - HandleInfoClearRequest(g_tempBuffer, logReader, hilogBuffer, logSta); + HandleInfoClearRequest(g_tempBuffer, logReader, hilogBuffer, logStatistic); break; case MC_REQ_LOG_CLEAR: HandleBufferClearRequest(g_tempBuffer, logReader, hilogBuffer); @@ -557,11 +558,11 @@ void LogQuerier::LogQuerierThreadFunc(std::shared_ptr logReader) hilogBuffer->RemoveLogReader(logReader); } -LogQuerier::LogQuerier(std::unique_ptr handler, HilogBuffer& buffer, LogStatistic& logStatistic) +LogQuerier::LogQuerier(std::unique_ptr handler, HilogBuffer& buffer, LogStatistic& statistic) { hilogtoolConnectSocket = std::move(handler); hilogBuffer = &buffer; - logSta = &logStatistic; + logStatistic = &statistic; } int LogQuerier::WriteData(LogQueryResponse& rsp, OptRef pData) diff --git a/services/hilogd/log_reader.cpp b/services/hilogd/log_reader.cpp index 7f1b428..56e557d 100644 --- a/services/hilogd/log_reader.cpp +++ b/services/hilogd/log_reader.cpp @@ -27,7 +27,7 @@ namespace HiviewDFX { using namespace std; HilogBuffer* LogReader::hilogBuffer = nullptr; -LogStatistic* LogReader::logSta = nullptr; +LogStatistic* LogReader::logStatistic = nullptr; LogReader::LogReader() { -- Gitee From da127f31014b47bec339867f2041ae3567d4afef Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Wed, 29 Dec 2021 04:00:53 -0500 Subject: [PATCH 13/28] statistic optimize Signed-off-by: youyouyuai --- frameworks/native/include/hilog_common.h | 2 + services/hilogd/flow_control_init.cpp | 5 +- services/hilogd/include/flow_control_init.h | 9 +- services/hilogd/include/log_statistic.h | 19 ++- services/hilogd/log_buffer.cpp | 6 +- services/hilogd/log_querier.cpp | 39 +++--- services/hilogd/log_statistic.cpp | 133 ++++++++++---------- services/hilogtool/log_controller.cpp | 22 ++-- services/hilogtool/log_display.cpp | 4 +- 9 files changed, 118 insertions(+), 121 deletions(-) diff --git a/frameworks/native/include/hilog_common.h b/frameworks/native/include/hilog_common.h index c556374..55ce8c7 100644 --- a/frameworks/native/include/hilog_common.h +++ b/frameworks/native/include/hilog_common.h @@ -46,6 +46,8 @@ #define ONE_MB (1UL<<20) #define ONE_GB (1UL<<30) #define ONE_TB (1ULL<<40) +#define U16_MAX 0xffff +#define U32_MAX 0xffffffff const long long NSEC = 1000000000LL; const long long US = 1000000LL; const long long NS2US = 1000LL; diff --git a/services/hilogd/flow_control_init.cpp b/services/hilogd/flow_control_init.cpp index 10a502e..0db9615 100644 --- a/services/hilogd/flow_control_init.cpp +++ b/services/hilogd/flow_control_init.cpp @@ -113,14 +113,13 @@ int32_t InitDomainFlowCtrl() int FlowCtrlDomain(HilogMsg* hilogMsg) { - uint32_t domain = hilogMsg->domain; - uint32_t domainId = (domain & DOMAIN_FILTER) >> DOMAIN_FILTER_SUBSYSTEM; - if (hilogMsg->type == LOG_APP || !IsDomainSwitchOn() || IsDebugOn()) { return 0; } LogTimeStamp tsNow(0, 0); std::unordered_map::iterator it; + uint32_t domain = hilogMsg->domain; + uint32_t domainId = (domain & DOMAIN_FILTER) >> DOMAIN_FILTER_SUBSYSTEM; int logLen = hilogMsg->len - sizeof(HilogMsg) - 1 - 1; /* quota length exclude '\0' of tag and log content */ int ret = 0; it = g_domainMap.find(domainId); diff --git a/services/hilogd/include/flow_control_init.h b/services/hilogd/include/flow_control_init.h index b8f591f..0c6f798 100644 --- a/services/hilogd/include/flow_control_init.h +++ b/services/hilogd/include/flow_control_init.h @@ -22,14 +22,7 @@ namespace OHOS { namespace HiviewDFX { int32_t InitDomainFlowCtrl(); int FlowCtrlDomain(HilogMsg* hilogMsg); -int32_t GetDroppedByType(uint16_t logType); -int32_t GetDroppedByDomain(uint32_t domainId); -uint32_t GetPrintByType(uint16_t logType); -uint32_t GetPrintByDomain(uint32_t domainId); -void ClearDroppedByType(uint16_t logType); -void ClearDroppedByDomain(uint32_t domainId); -void ClearPrintByType(uint16_t logType); -void ClearPrintByDomain(uint32_t domainId); + } } #endif diff --git a/services/hilogd/include/log_statistic.h b/services/hilogd/include/log_statistic.h index 8b1211c..46d10ad 100644 --- a/services/hilogd/include/log_statistic.h +++ b/services/hilogd/include/log_statistic.h @@ -17,7 +17,7 @@ #include #include "hilog_common.h" - +#include "log_data.h" namespace OHOS { namespace HiviewDFX { @@ -28,11 +28,6 @@ using Statistic = struct { size_t cached; }; -using StaInfo = struct { - uint16_t logType; - uint32_t domain; - Statistic statistic; -}; class LogStatistic { public: @@ -41,18 +36,18 @@ public: int Printed(const HilogMsg& msg); int Cached(const HilogMsg& msg); - int CacheDropped(const HilogMsg& msg); + int CacheDropped(const HilogData& data); int Dropped(const HilogMsg& msg); - int GetByType(uint16_t logType, Statistic& statistic); - int GetByDomain(uint32_t domain, Statistic& statistic); - int GetStatistic(std::vector& statistic); + int GetByType(uint16_t logType, Statistic& result); + int GetByDomain(uint32_t domain, Statistic& result); + int GetStatistic(std::unordered_map result[]); int ResetByType(uint16_t logType); int ResetByDomain(uint32_t domain); int ResetStatistic(); private: - std::vector staInfo; - StaInfo* IsExisted(const HilogMsg& msg) ; + std::unordered_map statistics[LOG_TYPE_MAX]; + Statistic* GetStatsInfo(uint16_t logType, uint32_t domain); }; } diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index a174013..aea7e26 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -80,8 +80,7 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) size_t cLen = it->len - it->tag_len; size -= cLen; sizeByType[(*it).type] -= cLen; - HilogMsg* tmp = (HilogMsg*)(&(*it)); - m_logStatistic.CacheDropped(*tmp); + m_logStatistic.CacheDropped(*it); it = msgList.erase(it); } // Re-confirm if enough elements has been removed @@ -187,8 +186,7 @@ size_t HilogBuffer::Delete(uint16_t logType) sum += cLen; sizeByType[(*it).type] -= cLen; size -= cLen; - HilogMsg* tmp = (HilogMsg*)(&(*it)); - m_logStatistic.CacheDropped(*tmp); + m_logStatistic.CacheDropped(*it); it = msgList.erase(it); } diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 8446593..91b36cd 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -376,7 +376,7 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, StatisticInfoQueryRequest* pStatisticInfoQueryReq = reinterpret_cast(reqMsg); StatisticInfoQueryResponse* pStatisticInfoQueryRsp = reinterpret_cast(msgToSend); StatisticInfoResult* pRst = reinterpret_cast(pStatisticInfoQueryRsp->statisticInfoRst); - if (pStatisticInfoQueryReq->logType != 0xffff) { + if (pStatisticInfoQueryReq->logType != U16_MAX) { pRst->logType = pStatisticInfoQueryReq->logType; pRst->domain = pStatisticInfoQueryReq->domain; Statistic info; @@ -386,7 +386,7 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, pRst->cacheLen = info.cached; pRst->result = (rst < 0) ? rst : RET_SUCCESS; rstNum ++; - } else if (pStatisticInfoQueryReq->domain != 0xffffffff) { + } else if (pStatisticInfoQueryReq->domain != U32_MAX) { pRst->logType = pStatisticInfoQueryReq->logType; pRst->domain = pStatisticInfoQueryReq->domain; Statistic info; @@ -397,17 +397,28 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, pRst->result = (rst < 0) ? rst : RET_SUCCESS; rstNum ++; } else { - std::vector info; + std::unordered_map info[LOG_TYPE_MAX]; + std::unordered_map::iterator it; rst = statistic->GetStatistic(info); - for (StaInfo& tmp : info) { - pRst->result = rst; - pRst->logType = tmp.logType; - pRst->domain = tmp.domain; - pRst->dropped = tmp.statistic.dropped; - pRst->printLen = tmp.statistic.printed; - pRst->cacheLen = tmp.statistic.cached; - rstNum ++; - pRst ++; + for (int i = 0; i < LOG_TYPE_MAX; i++) { + if (info[i].empty()) { + continue; + } + for (it = info[i].begin(); it != info[i].end(); it++) { + rstNum ++; + if (sizeof(StatisticInfoResult) * rstNum + sizeof(MessageHeader) > MAX_DATA_LEN) { + std::cout << "Statistic info response length more than max value" << std::endl; + rstNum --; + break; + } + pRst->result = rst; + pRst->logType = i; + pRst->domain = it->first; + pRst->dropped = it->second.dropped; + pRst->printLen = it->second.printed; + pRst->cacheLen = it->second.cached; + pRst ++; + } } } uint16_t sendMsgLen = sizeof(StatisticInfoResult) * rstNum; @@ -425,9 +436,9 @@ void HandleInfoClearRequest(char* reqMsg, std::shared_ptr logReader, StatisticInfoClearResponse* pStatisticInfoClearRsp = reinterpret_cast(msgToSend); pStatisticInfoClearRsp->logType = pStatisticInfoClearReq->logType; pStatisticInfoClearRsp->domain = pStatisticInfoClearReq->domain; - if (pStatisticInfoClearReq->logType != 0xffff) { + if (pStatisticInfoClearReq->logType != U16_MAX) { rst = statistic->ResetByType(pStatisticInfoClearReq->logType); - } else if (pStatisticInfoClearReq->domain != 0xffffffff) { + } else if (pStatisticInfoClearReq->domain != U32_MAX) { rst = statistic->ResetByDomain(pStatisticInfoClearReq->domain); } else { rst = statistic->ResetStatistic(); diff --git a/services/hilogd/log_statistic.cpp b/services/hilogd/log_statistic.cpp index b1c66ce..575966e 100644 --- a/services/hilogd/log_statistic.cpp +++ b/services/hilogd/log_statistic.cpp @@ -30,29 +30,26 @@ LogStatistic::LogStatistic() LogStatistic::~LogStatistic() {} -StaInfo* LogStatistic::IsExisted(const HilogMsg& msg) +Statistic* LogStatistic::GetStatsInfo(uint16_t logType, uint32_t domain) { - for (StaInfo& tmp : staInfo) { - if (tmp.logType == msg.type && tmp.domain == msg.domain) { - return &tmp; - } + std::unordered_map::iterator it = statistics[logType].find(domain); + if (it != statistics[logType].end()) { + return &(it->second); } return nullptr; } int LogStatistic::Printed(const HilogMsg& msg) { - StaInfo* info = IsExisted(msg); + Statistic* info = GetStatsInfo(msg.type, msg.domain); if (info) { - info->statistic.printed += CONTENT_LEN((&msg)); + info->printed += CONTENT_LEN((&msg)); } else { - StaInfo tmp; - tmp.logType = msg.type; - tmp.domain = msg.domain; - tmp.statistic.printed = CONTENT_LEN((&msg)); - tmp.statistic.cached = 0; - tmp.statistic.dropped = 0; - staInfo.emplace_back(tmp); + Statistic tmp; + tmp.printed = CONTENT_LEN((&msg)); + tmp.cached = 0; + tmp.dropped = 0; + statistics[msg.type].insert({msg.domain, tmp}); } return 0; @@ -60,97 +57,96 @@ int LogStatistic::Printed(const HilogMsg& msg) int LogStatistic::Cached(const HilogMsg& msg) { - StaInfo* info = IsExisted(msg); + Statistic* info = GetStatsInfo(msg.type, msg.domain); if (info) { - info->statistic.cached += CONTENT_LEN((&msg)); + info->cached += CONTENT_LEN((&msg)); } else { - StaInfo tmp; - tmp.logType = msg.type; - tmp.domain = msg.domain; - tmp.statistic.printed = 0; - tmp.statistic.cached = CONTENT_LEN((&msg)); - tmp.statistic.dropped = 0; - staInfo.emplace_back(tmp); + Statistic tmp; + tmp.printed = 0; + tmp.cached = CONTENT_LEN((&msg)); + tmp.dropped = 0; + statistics[msg.type].insert({msg.domain, tmp}); } return 0; } -int LogStatistic::CacheDropped(const HilogMsg& msg) +int LogStatistic::CacheDropped(const HilogData& data) { - StaInfo* info = IsExisted(msg); + Statistic* info = GetStatsInfo(data.type, data.domain); if (info) { - //printf("type %d, domain %d, len %d", msg.type, msg.domain, CONTENT_LEN((&msg))); - info->statistic.cached -= (msg.len - msg.tag_len); - } + info->cached -= (data.len - data.tag_len); + } return 0; } int LogStatistic::Dropped(const HilogMsg& msg) { - StaInfo* info = IsExisted(msg); + Statistic* info = GetStatsInfo(msg.type, msg.domain); if (info) { - info->statistic.printed += 1; + info->printed ++; } else { - StaInfo tmp; - tmp.logType = msg.type; - tmp.domain = msg.domain; - tmp.statistic.printed = 0; - tmp.statistic.cached = 0; - tmp.statistic.dropped = 1; - staInfo.emplace_back(tmp); + Statistic tmp; + tmp.printed = 0; + tmp.cached = 0; + tmp.dropped = 1; + statistics[msg.type].insert({msg.domain, tmp}); } return 0; } -int LogStatistic::GetByType(uint16_t logType, Statistic& statistic) +int LogStatistic::GetByType(uint16_t logType, Statistic& result) { - statistic = {0}; - for (StaInfo& info : staInfo) { - if (info.logType == logType) { - statistic.printed += info.statistic.printed; - statistic.cached += info.statistic.cached; - statistic.dropped += info.statistic.dropped; - } + result = {0}; + std::unordered_map::iterator it; + for (it = statistics[logType].begin(); it != statistics[logType].end(); it++) { + result.printed += it->second.printed; + result.cached += it->second.cached; + result.dropped += it->second.dropped; } return 0; } -int LogStatistic::GetByDomain(uint32_t domain, Statistic& statistic) +int LogStatistic::GetByDomain(uint32_t domain, Statistic& result) { - statistic = {0}; - for (StaInfo& info : staInfo) { - if (info.domain == domain) { - statistic.printed += info.statistic.printed; - statistic.cached += info.statistic.cached; - statistic.dropped += info.statistic.dropped; + result = {0}; + std::unordered_map::iterator it; + for (int i = 0; i < LOG_TYPE_MAX; i++) { + std::unordered_map::iterator it = statistics[i].find(domain); + if (it != statistics[i].end()) { + result.printed = it->second.printed; + result.cached = it->second.cached; + result.dropped = it->second.dropped; + break; } } return 0; } -int LogStatistic::GetStatistic(std::vector& statistic) +int LogStatistic::GetStatistic(std::unordered_map result[]) { - statistic = staInfo; + result = statistics; return 0; } int LogStatistic::ResetByType(uint16_t logType) { - for (StaInfo& info : staInfo) { - if (info.logType == logType) { - info.statistic.printed = 0; - info.statistic.dropped = 0; - } + std::unordered_map::iterator it; + for (it = statistics[logType].begin(); it != statistics[logType].end(); it++) { + it->second.printed = 0; + it->second.dropped = 0; } return 0; } int LogStatistic::ResetByDomain(uint32_t domain) { - for (StaInfo& info : staInfo) { - if (info.domain == domain) { - info.statistic.printed = 0; - info.statistic.dropped = 0; + std::unordered_map::iterator it; + for (int i = 0; i < LOG_TYPE_MAX; i++) { + std::unordered_map::iterator it = statistics[i].find(domain); + if (it != statistics[i].end()) { + it->second.printed = 0; + it->second.dropped = 0; + break; } } return 0; @@ -158,12 +154,15 @@ int LogStatistic::ResetByDomain(uint32_t domain) int LogStatistic::ResetStatistic() { - for (StaInfo& info : staInfo) { - info.statistic.printed = 0; - info.statistic.dropped = 0; + std::unordered_map::iterator it; + for (int i = 0; i < LOG_TYPE_MAX; i++) { + for (it = statistics[i].begin(); it != statistics[i].end(); it++) { + it->second.printed = 0; + it->second.dropped = 0; + } } return 0; } - + } } diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index f897e92..f7fc796 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -85,7 +85,7 @@ uint16_t GetLogType(const string& logTypeStr) } else if (logTypeStr == "kmsg") { logType = LOG_KMSG; } else { - return 0xffff; + return U16_MAX; } return logType; } @@ -145,7 +145,7 @@ uint16_t GetLogLevel(const std::string& logLevelStr, std::string& logLevel) logLevel = "F"; return LOG_FATAL; } - return 0xffff; + return U16_MAX; } string SetDefaultLogType(const std::string& logTypeStr) @@ -271,7 +271,7 @@ int32_t BufferSizeOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const st } for (iter = 0; iter < logTypeNum; iter++) { pBuffSizeMsg->logType = GetLogType(vecLogType[iter]); - if (pBuffSizeMsg->logType == 0xffff) { + if (pBuffSizeMsg->logType == U16_MAX) { cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; return RET_FAIL; } @@ -290,7 +290,7 @@ int32_t BufferSizeOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const st } for (iter = 0; iter < logTypeNum; iter++) { pBuffResizeMsg->logType = GetLogType(vecLogType[iter]); - if (pBuffResizeMsg->logType == 0xffff) { + if (pBuffResizeMsg->logType == U16_MAX) { cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; return RET_FAIL; } @@ -317,7 +317,7 @@ int32_t StatisticInfoOp(SeqPacketSocketClient& controller, uint8_t msgCmd, uint16_t logType = GetLogType(logTypeStr); uint32_t domain; if (domainStr == "") { - domain = 0xffffffff; + domain = U32_MAX; } else { if (domainStr.rfind("0x", 0) == 0 || domainStr.rfind("0X", 0) == 0) { std::istringstream(domainStr) >> hex >> domain; @@ -373,7 +373,7 @@ int32_t LogClearOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const std: } for (iter = 0; iter < logTypeNum; iter++) { pLogClearMsg->logType = GetLogType(vecLogType[iter]); - if (pLogClearMsg->logType == 0xffff) { + if (pLogClearMsg->logType == U16_MAX) { cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; return RET_FAIL; } @@ -411,7 +411,7 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi } for (iter = 0; iter < logTypeNum; iter++) { uint16_t tmpType = GetLogType(vecLogType[iter]); - if (tmpType == 0xffff) { + if (tmpType == U16_MAX) { cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; return RET_FAIL; } @@ -478,7 +478,7 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi for (iter = 0; iter < logTypeNum; iter++) { uint16_t tmpType = GetLogType(vecLogType[iter]); - if (tmpType == 0xffff) { + if (tmpType == U16_MAX) { cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; return RET_FAIL; } @@ -545,7 +545,7 @@ int32_t SetPropertiesOp(SeqPacketSocketClient& controller, uint8_t operationType std::string keyPre = GetPropertyName(PROP_DOMAIN_LOG_LEVEL); for (iter = 0; iter < domainNum; iter++) { key = keyPre + vecDomain[iter]; - if (GetLogLevel(propertyParm->logLevelStr, value) == 0xffff) { + if (GetLogLevel(propertyParm->logLevelStr, value) == U16_MAX) { continue; } PropertySet(key.c_str(), value.c_str()); @@ -555,7 +555,7 @@ int32_t SetPropertiesOp(SeqPacketSocketClient& controller, uint8_t operationType std::string keyPre = GetPropertyName(PROP_TAG_LOG_LEVEL); for (iter = 0; iter < tagNum; iter++) { key = keyPre + vecTag[iter]; - if (GetLogLevel(propertyParm->logLevelStr, value) == 0xffff) { + if (GetLogLevel(propertyParm->logLevelStr, value) == U16_MAX) { continue; } PropertySet(key.c_str(), value.c_str()); @@ -563,7 +563,7 @@ int32_t SetPropertiesOp(SeqPacketSocketClient& controller, uint8_t operationType } } else { key = GetPropertyName(PROP_GLOBAL_LOG_LEVEL); - if (GetLogLevel(propertyParm->logLevelStr, value) == 0xffff) { + if (GetLogLevel(propertyParm->logLevelStr, value) == U16_MAX) { return RET_FAIL; } PropertySet(key.c_str(), value.c_str()); diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 7c9b16f..dad1816 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -268,11 +268,11 @@ int32_t ControlCmdResult(const char* message) if (!staInfoQueryRsp) { return RET_FAIL; } - if (infoRst->logType == 0xffff) { + if (infoRst->logType == U16_MAX) { oss << std::left << std::setw(20) << "DOMAINID" << std::setw(20) << "DOMAINNAME" << std::setw(20) << "PRINTED" << std::setw(20) << "CACHED" << std::setw(20) << "DROPPED" << std::endl << "0x" << std::setw(18) << hex << infoRst->domain << std::setw(20) << ""; - } else if (infoRst->domain == 0xffffffff){ + } else if (infoRst->domain == U32_MAX){ oss << std::left << std::setw(20) << "TYPE" << std::setw(20) << "PRINTED" << std::setw(20) << "CACHED" << std::setw(20) << "DROPPED" << std::endl << std::setw(20) << GetLogTypeStr(infoRst->logType); } else { -- Gitee From cc94dee99713f28635fff40aa207c6b9ef31342f Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Wed, 29 Dec 2021 20:51:04 -0500 Subject: [PATCH 14/28] rm unused Signed-off-by: youyouyuai --- adapter/BUILD_bak.gn | 42 --- adapter/properties_bak.cpp | 580 ------------------------------------- 2 files changed, 622 deletions(-) delete mode 100644 adapter/BUILD_bak.gn delete mode 100644 adapter/properties_bak.cpp diff --git a/adapter/BUILD_bak.gn b/adapter/BUILD_bak.gn deleted file mode 100644 index 85cc4b1..0000000 --- a/adapter/BUILD_bak.gn +++ /dev/null @@ -1,42 +0,0 @@ -# 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. - -import("//build/ohos.gni") - -ohos_shared_library("libhilog_os_adapter") { - sources = [ - "properties.cpp", - "socket_server_adapter.cpp", - ] - - deps = [ - "//utils/native/base:utilsecurec_shared", - #"//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", - ] - - defines = [ "USING_EXISTING_SOCKET" ] - - include_dirs = [ - "//base/hiviewdfx/hilog/interfaces/native/innerkits/include", - "//base/hiviewdfx/hilog/adapter", - "//base/startup/syspara_lite/interfaces/innerkits/native/syspara/include", - ] - - install_enable = true - part_name = "hilog_native" - subsystem_name = "hiviewdfx" - install_images = [ - "system", - "updater", - ] -} diff --git a/adapter/properties_bak.cpp b/adapter/properties_bak.cpp deleted file mode 100644 index 9825109..0000000 --- a/adapter/properties_bak.cpp +++ /dev/null @@ -1,580 +0,0 @@ -/* - * 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. - */ - -#include "properties.h" -#include "hilog/log.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace std; - -using ReadLock = shared_lock; -using InsertLock = unique_lock; - - -static pthread_mutex_t g_globalLevelLock = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t g_tagLevelLock = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t g_domainLevelLock = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t g_debugLock = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t g_persistDebugLock = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t g_privateLock = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t g_processFlowLock = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t g_domainFlowLock = PTHREAD_MUTEX_INITIALIZER; - -using PropertyCache = struct { - const void* pinfo; - uint32_t serial; - char propertyValue[HILOG_PROP_VALUE_MAX]; -}; - -/* -using SwitchCache = struct { - PropertyCache cache; - bool isOn; -}; - -using LogLevelCache = struct { - PropertyCache cache; - uint16_t logLevel; -}; - -using ProcessInfo = struct { - PropertyCache cache; - string propertyKey; - string process; - string processHashPre; - uint32_t processQuota; -}; -*/ - -using ContextInfo = struct { - uint32_t propType; - bool switchOn; - uint16_t logLevel; - std::string key; - char value[HILOG_PROP_VALUE_MAX]; -}; - -void PropertyGet(const string &key, char *value, int len) -{ - if (len < HILOG_PROP_VALUE_MAX) { - return; - } - /* use OHOS interface */ - GetParameter(key.c_str(), nullptr, value, HILOG_PROP_VALUE_MAX); - -} - -void PropertySet(const string &key, const char* value) -{ - /* use OHOS interface */ - SetParameter(key.c_str(), value); -} - -string GetProgName() -{ - return program_invocation_short_name; /* use HOS interface */ -} - -string GetPropertyName(uint32_t propType) -{ - string key; - switch (propType) { - case PROP_PRIVATE: - key = "hilog.private.on"; - break; - case PROP_PROCESS_FLOWCTRL: - key = "hilog.flowctrl.pid.on"; - break; - case PROP_DOMAIN_FLOWCTRL: - key = "hilog.flowctrl.domain.on"; - break; - case PROP_GLOBAL_LOG_LEVEL: - key = "hilog.loggable.global"; - break; - case PROP_DOMAIN_LOG_LEVEL: - key = "hilog.loggable.domain."; - break; - case PROP_TAG_LOG_LEVEL: - key = "hilog.loggable.tag."; - break; - case PROP_SINGLE_DEBUG: - key = "hilog.debug.on"; - break; - case PROP_PERSIST_DEBUG: - key = "persist.sys.hilog.debug.on"; - break; - default: - break; - } - return key; -} - -static int LockByProp(uint32_t propType) -{ - switch (propType) { - case PROP_PRIVATE: - return pthread_mutex_trylock(&g_privateLock); - case PROP_PROCESS_FLOWCTRL: - return pthread_mutex_trylock(&g_processFlowLock); - case PROP_DOMAIN_FLOWCTRL: - return pthread_mutex_trylock(&g_domainFlowLock); - case PROP_GLOBAL_LOG_LEVEL: - return pthread_mutex_trylock(&g_globalLevelLock); - case PROP_DOMAIN_LOG_LEVEL: - return pthread_mutex_trylock(&g_domainLevelLock); - case PROP_TAG_LOG_LEVEL: - return pthread_mutex_trylock(&g_tagLevelLock); - case PROP_SINGLE_DEBUG: - return pthread_mutex_trylock(&g_debugLock); - case PROP_PERSIST_DEBUG: - return pthread_mutex_trylock(&g_persistDebugLock); - default: - return -1; - } -} - -static void UnlockByProp(uint32_t propType) -{ - switch (propType) { - case PROP_PRIVATE: - pthread_mutex_unlock(&g_privateLock); - break; - case PROP_PROCESS_FLOWCTRL: - pthread_mutex_unlock(&g_processFlowLock); - break; - case PROP_DOMAIN_FLOWCTRL: - pthread_mutex_unlock(&g_domainFlowLock); - break; - case PROP_GLOBAL_LOG_LEVEL: - pthread_mutex_unlock(&g_globalLevelLock); - break; - case PROP_DOMAIN_LOG_LEVEL: - pthread_mutex_unlock(&g_domainLevelLock); - break; - case PROP_TAG_LOG_LEVEL: - pthread_mutex_unlock(&g_tagLevelLock); - break; - case PROP_SINGLE_DEBUG: - pthread_mutex_unlock(&g_debugLock); - break; - case PROP_PERSIST_DEBUG: - pthread_mutex_unlock(&g_persistDebugLock); - break; - default: - break; - } -} -#if 0 -static void RefreshCacheBuf(PropertyCache *cache, const char *key) -{ - /* use OHOS interface */ -} - -static bool CheckCache(const PropertyCache *cache) -{ - /* use OHOS interface */ - return true; -} - -/* - check & refresh -*/ -#endif - -/* -static bool GetSwitchCache(bool isFirst, SwitchCache& switchCache, uint32_t propType, bool defaultValue) -{ - int notLocked; - string key = GetPropertyName(propType); - - if (isFirst || CheckCache(&switchCache.cache)) { - notLocked = LockByProp(propType); - if (!notLocked) { - RefreshCacheBuf(&switchCache.cache, key.c_str()); - if (strcmp(switchCache.cache.propertyValue, "true") == 0) { - switchCache.isOn = true; - } else if (strcmp(switchCache.cache.propertyValue, "false") == 0) { - switchCache.isOn = false; - } else { - switchCache.isOn = defaultValue; - } - UnlockByProp(propType); - return switchCache.isOn; - } else { - SwitchCache tmpCache = {{nullptr, 0xffffffff, ""}, defaultValue}; - RefreshCacheBuf(&tmpCache.cache, key.c_str()); - if (strcmp(tmpCache.cache.propertyValue, "true") == 0) { - tmpCache.isOn = true; - } else if (strcmp(tmpCache.cache.propertyValue, "false") == 0) { - tmpCache.isOn = false; - } else { - tmpCache.isOn = defaultValue; - } - return tmpCache.isOn; - } - } else { - return switchCache.isOn; - } -} -*/ -static bool GetSwitchCache(const char* propValue, bool defaultSwitch) -{ - if (strcmp(propValue, "true") == 0) { - return true; - } else if (strcmp(propValue, "false") == 0) { - return false; - } else { - return defaultSwitch; - } -} - - -static uint16_t GetCacheLevel(char propertyChar, uint16_t defaultLevel) -{ - uint16_t cacheLevel = defaultLevel; - switch (propertyChar) { - case 'D': - case 'd': - cacheLevel = LOG_DEBUG; - break; - case 'I': - case 'i': - cacheLevel = LOG_INFO; - break; - case 'W': - case 'w': - cacheLevel = LOG_WARN; - break; - case 'E': - case 'e': - cacheLevel = LOG_ERROR; - break; - case 'F': - case 'f': - cacheLevel = LOG_FATAL; - break; - default: - break; - } - return cacheLevel; -} - -static void ChangeCallback(const char *key, const char *value, void *context) -{ - int notLocked; - ContextInfo* contextInfo = reinterpret_cast(context); - notLocked = LockByProp(contextInfo->propType); - if (!notLocked && strcmp(key, contextInfo->key.c_str()) == 0) { - if (strncpy_s(contextInfo->value, HILOG_PROP_VALUE_MAX, value, strlen(value))) { - UnlockByProp(contextInfo->propType); - return; - } - switch (contextInfo->propType) { - case PROP_PRIVATE: - contextInfo->switchOn = GetSwitchCache(contextInfo->value, true); - break; - case PROP_PROCESS_FLOWCTRL: - contextInfo->switchOn = GetSwitchCache(contextInfo->value, false); - break; - case PROP_DOMAIN_FLOWCTRL: - contextInfo->switchOn = GetSwitchCache(contextInfo->value, false); - break; - case PROP_GLOBAL_LOG_LEVEL: - contextInfo->logLevel = GetCacheLevel(contextInfo->value[0], LOG_LEVEL_MIN); - break; - case PROP_DOMAIN_LOG_LEVEL: - contextInfo->logLevel = GetCacheLevel(contextInfo->value[0], LOG_LEVEL_MIN); - break; - case PROP_TAG_LOG_LEVEL: - contextInfo->logLevel = GetCacheLevel(contextInfo->value[0], LOG_LEVEL_MIN); - break; - case PROP_SINGLE_DEBUG: - contextInfo->logLevel = GetSwitchCache(contextInfo->value, false); - break; - case PROP_PERSIST_DEBUG: - contextInfo->logLevel = GetSwitchCache(contextInfo->value, false); - break; - } - UnlockByProp(contextInfo->propType); - } -} - -static void WatchProp(const std::string& keyprefix, ContextInfo& contextInfo) -{ - WatchParameter(keyprefix.c_str(), ChangeCallback, reinterpret_cast(&contextInfo)); -} - -bool IsDebugOn() -{ - return IsSingleDebugOn() || IsPersistDebugOn(); -} -/* -bool IsSingleDebugOn() -{ - static SwitchCache *switchCache = new SwitchCache {{nullptr, 0xffffffff, ""}, false}; - static atomic_flag isFirstFlag = ATOMIC_FLAG_INIT; - bool isFirst = !isFirstFlag.test_and_set(); - return GetSwitchCache(isFirst, *switchCache, PROP_SINGLE_DEBUG, false); -} - -bool IsPersistDebugOn() -{ - static SwitchCache *switchCache = new SwitchCache {{nullptr, 0xffffffff, ""}, false}; - static atomic_flag isFirstFlag = ATOMIC_FLAG_INIT; - bool isFirst = !isFirstFlag.test_and_set(); - return GetSwitchCache(isFirst, *switchCache, PROP_PERSIST_DEBUG, false); -} - -bool IsPrivateSwitchOn() -{ - static SwitchCache *switchCache = new SwitchCache {{nullptr, 0xffffffff, ""}, true}; - static atomic_flag isFirstFlag = ATOMIC_FLAG_INIT; - bool isFirst = !isFirstFlag.test_and_set(); - return GetSwitchCache(isFirst, *switchCache, PROP_PRIVATE, true); -} - -bool IsProcessSwitchOn() -{ - static SwitchCache *switchCache = new SwitchCache {{nullptr, 0xffffffff, ""}, false}; - static atomic_flag isFirstFlag = ATOMIC_FLAG_INIT; - bool isFirst = !isFirstFlag.test_and_set(); - return GetSwitchCache(isFirst, *switchCache, PROP_PROCESS_FLOWCTRL, false); -} - -bool IsDomainSwitchOn() -{ - static SwitchCache *switchCache = new SwitchCache {{nullptr, 0xffffffff, ""}, false}; - static atomic_flag isFirstFlag = ATOMIC_FLAG_INIT; - bool isFirst = !isFirstFlag.test_and_set(); - return GetSwitchCache(isFirst, *switchCache, PROP_DOMAIN_FLOWCTRL, false); -} -*/ -bool IsSingleDebugOn() -{ - std::string key = GetPropertyName(PROP_SINGLE_DEBUG); - static ContextInfo *contextInfo = new ContextInfo{PROP_SINGLE_DEBUG, false, LOG_LEVEL_MIN, key, ""}; - WatchProp(key, *contextInfo); - return contextInfo->switchOn; -} - -bool IsPersistDebugOn() -{ - std::string key = GetPropertyName(PROP_PERSIST_DEBUG); - static ContextInfo *contextInfo = new ContextInfo{PROP_PERSIST_DEBUG, false, LOG_LEVEL_MIN, key, ""}; - WatchProp(key, *contextInfo); - return contextInfo->switchOn; -} - -bool IsPrivateSwitchOn() -{ - std::string key = GetPropertyName(PROP_PRIVATE); - static ContextInfo *contextInfo = new ContextInfo{PROP_PRIVATE, true, LOG_LEVEL_MIN, key, ""}; - WatchProp(key, *contextInfo); - return contextInfo->switchOn; -} - -bool IsProcessSwitchOn() -{ - std::string key = GetPropertyName(PROP_PROCESS_FLOWCTRL); - static ContextInfo *contextInfo = new ContextInfo{PROP_PROCESS_FLOWCTRL, false, LOG_LEVEL_MIN, key, ""}; - WatchProp(key, *contextInfo); - return contextInfo->switchOn; -} - -bool IsDomainSwitchOn() -{ - std::string key = GetPropertyName(PROP_DOMAIN_FLOWCTRL); - static ContextInfo *contextInfo = new ContextInfo{PROP_DOMAIN_FLOWCTRL, false, LOG_LEVEL_MIN, key, ""}; - WatchProp(key, *contextInfo); - return contextInfo->switchOn; -} - -/* -uint16_t GetGlobalLevel() -{ - string key = GetPropertyName(PROP_GLOBAL_LOG_LEVEL); - static LogLevelCache *levelCache = new LogLevelCache{{nullptr, 0xffffffff, ""}, LOG_LEVEL_MIN}; - static atomic_flag isFirstFlag = ATOMIC_FLAG_INIT; - int notLocked; - - if (!isFirstFlag.test_and_set() || CheckCache(&levelCache->cache)) { - notLocked = LockByProp(PROP_GLOBAL_LOG_LEVEL); - if (!notLocked) { - RefreshCacheBuf(&levelCache->cache, key.c_str()); - levelCache->logLevel = GetCacheLevel(levelCache->cache.propertyValue[0]); - UnlockByProp(PROP_GLOBAL_LOG_LEVEL); - return levelCache->logLevel; - } else { - LogLevelCache tmpCache = {{nullptr, 0xffffffff, ""}, LOG_LEVEL_MIN}; - RefreshCacheBuf(&tmpCache.cache, key.c_str()); - tmpCache.logLevel = GetCacheLevel(tmpCache.cache.propertyValue[0]); - return tmpCache.logLevel; - } - } else { - return levelCache->logLevel; - } -} - -uint16_t GetDomainLevel(uint32_t domain) -{ - static unordered_map *domainMap = new unordered_map(); - unordered_map::iterator it; - string key = GetPropertyName(PROP_DOMAIN_LOG_LEVEL) + to_string(domain); - - static shared_timed_mutex* mtx = new shared_timed_mutex; - { - ReadLock lock(*mtx); - it = domainMap->find(domain); - } - if (it == domainMap->end()) { // new domain - InsertLock lock(*mtx); - LogLevelCache* levelCache = new LogLevelCache{{nullptr, 0xffffffff, ""}, LOG_LEVEL_MIN}; - RefreshCacheBuf(&levelCache->cache, key.c_str()); - levelCache->logLevel = GetCacheLevel(levelCache->cache.propertyValue[0]); - uint16_t lvl = levelCache->logLevel; - pair::iterator, bool> ret = domainMap->insert({ domain, levelCache }); - if (!ret.second) { - delete levelCache; - levelCache = nullptr; - } - return lvl; - } else { // existed domain - if (CheckCache(&it->second->cache)) { // changed - InsertLock lock(*mtx); - RefreshCacheBuf(&it->second->cache, key.c_str()); - it->second->logLevel = GetCacheLevel(it->second->cache.propertyValue[0]); - return it->second->logLevel; - } else { // not changed - return it->second->logLevel; - } - } -} - -uint16_t GetTagLevel(const string& tag) -{ - static unordered_map *tagMap = new unordered_map(); - unordered_map::iterator it; - string key = GetPropertyName(PROP_TAG_LOG_LEVEL) + tag; - - static shared_timed_mutex* mtx = new shared_timed_mutex; - { - ReadLock lock(*mtx); - it = tagMap->find(tag); - } - if (it == tagMap->end()) { // new tag - InsertLock lock(*mtx); - LogLevelCache* levelCache = new LogLevelCache{{nullptr, 0xffffffff, ""}, LOG_LEVEL_MIN}; - RefreshCacheBuf(&levelCache->cache, key.c_str()); - levelCache->logLevel = GetCacheLevel(levelCache->cache.propertyValue[0]); - uint16_t lvl = levelCache->logLevel; - pair::iterator, bool> ret = tagMap->insert({ tag, levelCache }); - if (!ret.second) { - delete(levelCache); - levelCache = nullptr; - } - return lvl; - } else { // existed tag - if (CheckCache(&it->second->cache)) { // changed - InsertLock lock(*mtx); - RefreshCacheBuf(&it->second->cache, key.c_str()); - it->second->logLevel = GetCacheLevel(it->second->cache.propertyValue[0]); - return it->second->logLevel; - } else { // not changed - return it->second->logLevel; - } - } -} -*/ -uint16_t GetGlobalLevel() -{ - string key = GetPropertyName(PROP_GLOBAL_LOG_LEVEL); - static ContextInfo *contextInfo = new ContextInfo{PROP_GLOBAL_LOG_LEVEL, false, LOG_LEVEL_MIN, key, ""}; - WatchProp(key, *contextInfo); - return contextInfo->logLevel; -} - -uint16_t GetDomainLevel(uint32_t domain) -{ - static unordered_map *domainMap = new unordered_map(); - unordered_map::iterator it; - string key = GetPropertyName(PROP_DOMAIN_LOG_LEVEL) + to_string(domain); - - static shared_timed_mutex* mtx = new shared_timed_mutex; - { - ReadLock lock(*mtx); - it = domainMap->find(domain); - } - if (it == domainMap->end()) { // new domain - ContextInfo* contextInfo = new ContextInfo{PROP_DOMAIN_LOG_LEVEL, false, LOG_LEVEL_MIN, key, ""}; - WatchProp(key, *contextInfo); - uint16_t lvl = contextInfo->logLevel; - InsertLock lock(*mtx); - pair::iterator, bool> ret = domainMap->insert({ domain, contextInfo }); - if (!ret.second) { - delete contextInfo; - contextInfo = nullptr; - } - return lvl; - } else { // existed domain - WatchProp(key, *(it->second)); - return it->second->logLevel; - } -} - -uint16_t GetTagLevel(const string& tag) -{ - static unordered_map *tagMap = new unordered_map(); - unordered_map::iterator it; - string key = GetPropertyName(PROP_TAG_LOG_LEVEL) + tag; - - static shared_timed_mutex* mtx = new shared_timed_mutex; - { - ReadLock lock(*mtx); - it = tagMap->find(tag); - } - if (it == tagMap->end()) { // new tag - ContextInfo* contextInfo = new ContextInfo{PROP_TAG_LOG_LEVEL, false, LOG_LEVEL_MIN, key, ""}; - WatchProp(key, *contextInfo); - uint16_t lvl = contextInfo->logLevel; - InsertLock lock(*mtx); - pair::iterator, bool> ret = tagMap->insert({ tag, contextInfo }); - if (!ret.second) { - delete(contextInfo); - contextInfo = nullptr; - } - return lvl; - } else { // existed tag - WatchProp(key, *(it->second)); - return it->second->logLevel; - } -} \ No newline at end of file -- Gitee From 910a6f46efcdad23733133d4d0e060d4c37b771a Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Wed, 29 Dec 2021 20:59:17 -0500 Subject: [PATCH 15/28] fix Signed-off-by: youyouyuai --- services/hilogd/include/cmd_executor.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/hilogd/include/cmd_executor.h b/services/hilogd/include/cmd_executor.h index 471b159..047deb8 100644 --- a/services/hilogd/include/cmd_executor.h +++ b/services/hilogd/include/cmd_executor.h @@ -34,7 +34,7 @@ struct ClientThread { class CmdExecutor { public: - CmdExecutor(HilogBuffer& buffer, LogStatistic& logStatistic) : m_hilogBuffer(buffer), logSta(logStatistic) {} + CmdExecutor(HilogBuffer& buffer, LogStatistic& logStatistic) : m_hilogBuffer(buffer), m_logStatistic(logStatistic) {} ~CmdExecutor(); void MainLoop(); private: @@ -47,7 +47,7 @@ private: std::mutex m_clientAccess; std::vector m_finishedClients; std::mutex m_finishedClientAccess; - LogStatistic& logSta; + LogStatistic& m_logStatistic; }; } // namespace HiviewDFX } // namespace OHOS -- Gitee From 7c37ff46f2889eb9eb43d0c684beda38b810c51a Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Wed, 29 Dec 2021 21:03:02 -0500 Subject: [PATCH 16/28] fix Signed-off-by: youyouyuai --- services/hilogd/cmd_executor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/hilogd/cmd_executor.cpp b/services/hilogd/cmd_executor.cpp index a3891d2..1d8aca8 100644 --- a/services/hilogd/cmd_executor.cpp +++ b/services/hilogd/cmd_executor.cpp @@ -120,7 +120,7 @@ void CmdExecutor::ClientEventLoop(std::unique_ptr handler) assert(clientInfoIt != m_clients.end()); prctl(PR_SET_NAME, "hilogd.query"); - auto logQuerier = std::make_shared(std::move(handler), m_hilogBuffer, logSta); + auto logQuerier = std::make_shared(std::move(handler), m_hilogBuffer, m_logStatistic); logQuerier->LogQuerierThreadFunc(logQuerier); std::lock_guard ul(m_finishedClientAccess); -- Gitee From 372f19d397f7276e4aa2b353fef2229940e47e58 Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Wed, 29 Dec 2021 21:07:56 -0500 Subject: [PATCH 17/28] fix Signed-off-by: youyouyuai --- services/hilogd/log_buffer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index aea7e26..347d6b9 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -131,7 +131,7 @@ bool HilogBuffer::Query(std::shared_ptr reader) if (reader->readPos == msgList.end()) { reader->readPos = std::next(reader->lastPos); } - } + } while (reader->readPos != msgList.end()) { reader->lastPos = reader->readPos; if (ConditionMatch(reader)) { -- Gitee From bcf8b7d43b4121e7345c8e7cd4aeffa12ee7c6b1 Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Thu, 30 Dec 2021 04:46:35 -0500 Subject: [PATCH 18/28] statistic time Signed-off-by: youyouyuai --- frameworks/native/include/hilogtool_msg.h | 6 +- services/hilogd/include/log_statistic.h | 32 +++++++-- services/hilogd/log_querier.cpp | 67 ++++++++--------- services/hilogd/log_statistic.cpp | 88 ++++++++++++----------- services/hilogtool/log_controller.cpp | 4 +- services/hilogtool/log_display.cpp | 4 ++ 6 files changed, 107 insertions(+), 94 deletions(-) diff --git a/frameworks/native/include/hilogtool_msg.h b/frameworks/native/include/hilogtool_msg.h index 946d73b..e2132fa 100644 --- a/frameworks/native/include/hilogtool_msg.h +++ b/frameworks/native/include/hilogtool_msg.h @@ -199,6 +199,8 @@ typedef struct { typedef struct { int32_t result; + uint32_t tv_nsec; + uint32_t tv_sec; uint16_t logType; uint32_t domain; size_t printLen; @@ -213,15 +215,11 @@ typedef struct { typedef struct { MessageHeader msgHeader; - uint16_t logType; - uint32_t domain; } StatisticInfoClearRequest; typedef struct { MessageHeader msgHeader; int32_t result; - uint16_t logType; - uint32_t domain; } StatisticInfoClearResponse; typedef struct { diff --git a/services/hilogd/include/log_statistic.h b/services/hilogd/include/log_statistic.h index 46d10ad..c5c73a4 100644 --- a/services/hilogd/include/log_statistic.h +++ b/services/hilogd/include/log_statistic.h @@ -16,9 +16,11 @@ #define LOG_STATISTIC_H #include +#include #include "hilog_common.h" #include "log_data.h" + namespace OHOS { namespace HiviewDFX { @@ -28,6 +30,23 @@ using Statistic = struct { size_t cached; }; +using StatisticInfo = struct { + uint16_t logType; + uint32_t domain; + Statistic statistic; +}; + +using QueryResult = struct { + uint32_t tv_nsec; + uint32_t tv_sec; + Statistic statistic; +}; + +using QueryResults = struct { + uint32_t tv_nsec; + uint32_t tv_sec; + std::vector infoVec; +}; class LogStatistic { public: @@ -38,16 +57,17 @@ public: int Cached(const HilogMsg& msg); int CacheDropped(const HilogData& data); int Dropped(const HilogMsg& msg); - int GetByType(uint16_t logType, Statistic& result); - int GetByDomain(uint32_t domain, Statistic& result); - int GetStatistic(std::unordered_map result[]); - int ResetByType(uint16_t logType); - int ResetByDomain(uint32_t domain); + int GetByType(uint16_t logType, QueryResult& result); + int GetByDomain(uint32_t domain, QueryResult& result); + int GetStatistic(QueryResults & results); int ResetStatistic(); private: - std::unordered_map statistics[LOG_TYPE_MAX]; + std::unordered_map statisticMap[LOG_TYPE_MAX]; Statistic* GetStatsInfo(uint16_t logType, uint32_t domain); + void ResetTime(); + uint32_t time_nsec; + uint32_t time_sec; }; } diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 91b36cd..ecf86d1 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -379,46 +379,46 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, if (pStatisticInfoQueryReq->logType != U16_MAX) { pRst->logType = pStatisticInfoQueryReq->logType; pRst->domain = pStatisticInfoQueryReq->domain; - Statistic info; + QueryResult info; rst = statistic->GetByType(pStatisticInfoQueryReq->logType, info); - pRst->dropped = info.dropped; - pRst->printLen = info.printed; - pRst->cacheLen = info.cached; + pRst->tv_nsec = info.tv_nsec; + pRst->tv_sec = info.tv_sec; + pRst->dropped = info.statistic.dropped; + pRst->printLen = info.statistic.printed; + pRst->cacheLen = info.statistic.cached; pRst->result = (rst < 0) ? rst : RET_SUCCESS; rstNum ++; } else if (pStatisticInfoQueryReq->domain != U32_MAX) { pRst->logType = pStatisticInfoQueryReq->logType; pRst->domain = pStatisticInfoQueryReq->domain; - Statistic info; + QueryResult info; rst = statistic->GetByDomain(pStatisticInfoQueryReq->domain, info); - pRst->dropped = info.dropped; - pRst->printLen = info.printed; - pRst->cacheLen = info.cached; + pRst->tv_nsec = info.tv_nsec; + pRst->tv_sec = info.tv_sec; + pRst->dropped = info.statistic.dropped; + pRst->printLen = info.statistic.printed; + pRst->cacheLen = info.statistic.cached; pRst->result = (rst < 0) ? rst : RET_SUCCESS; rstNum ++; } else { - std::unordered_map info[LOG_TYPE_MAX]; - std::unordered_map::iterator it; + QueryResults info; rst = statistic->GetStatistic(info); - for (int i = 0; i < LOG_TYPE_MAX; i++) { - if (info[i].empty()) { - continue; - } - for (it = info[i].begin(); it != info[i].end(); it++) { - rstNum ++; - if (sizeof(StatisticInfoResult) * rstNum + sizeof(MessageHeader) > MAX_DATA_LEN) { - std::cout << "Statistic info response length more than max value" << std::endl; - rstNum --; - break; - } - pRst->result = rst; - pRst->logType = i; - pRst->domain = it->first; - pRst->dropped = it->second.dropped; - pRst->printLen = it->second.printed; - pRst->cacheLen = it->second.cached; - pRst ++; + pRst->tv_nsec = info.tv_nsec; + pRst->tv_sec = info.tv_sec; + for (uint32_t i = 0; i < info.infoVec.size(); i++) { + rstNum ++; + if (sizeof(StatisticInfoResult) * rstNum + sizeof(MessageHeader) > MAX_DATA_LEN) { + std::cout << "Statistic info response length more than max value" << std::endl; + rstNum --; + break; } + pRst->result = rst; + pRst->logType = info.infoVec[i].logType; + pRst->domain = info.infoVec[i].domain; + pRst->dropped = info.infoVec[i].statistic.dropped; + pRst->printLen = info.infoVec[i].statistic.printed; + pRst->cacheLen = info.infoVec[i].statistic.cached; + pRst ++; } } uint16_t sendMsgLen = sizeof(StatisticInfoResult) * rstNum; @@ -432,17 +432,8 @@ void HandleInfoClearRequest(char* reqMsg, std::shared_ptr logReader, char msgToSend[MAX_DATA_LEN]; int32_t rst = 0; memset_s(msgToSend, MAX_DATA_LEN, 0, MAX_DATA_LEN); - StatisticInfoClearRequest* pStatisticInfoClearReq = reinterpret_cast(reqMsg); StatisticInfoClearResponse* pStatisticInfoClearRsp = reinterpret_cast(msgToSend); - pStatisticInfoClearRsp->logType = pStatisticInfoClearReq->logType; - pStatisticInfoClearRsp->domain = pStatisticInfoClearReq->domain; - if (pStatisticInfoClearReq->logType != U16_MAX) { - rst = statistic->ResetByType(pStatisticInfoClearReq->logType); - } else if (pStatisticInfoClearReq->domain != U32_MAX) { - rst = statistic->ResetByDomain(pStatisticInfoClearReq->domain); - } else { - rst = statistic->ResetStatistic(); - } + rst = statistic->ResetStatistic(); pStatisticInfoClearRsp->result = (rst < 0) ? rst : RET_SUCCESS; SetMsgHead(pStatisticInfoClearRsp->msgHeader, MC_RSP_STATISTIC_INFO_CLEAR, sizeof(StatisticInfoClearResponse) - sizeof(MessageHeader)); diff --git a/services/hilogd/log_statistic.cpp b/services/hilogd/log_statistic.cpp index 575966e..28d8615 100644 --- a/services/hilogd/log_statistic.cpp +++ b/services/hilogd/log_statistic.cpp @@ -25,15 +25,25 @@ namespace HiviewDFX { LogStatistic::LogStatistic() -{} +{ + ResetTime(); +} LogStatistic::~LogStatistic() {} +void LogStatistic::ResetTime() +{ + timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + time_nsec = ts.tv_nsec; + time_sec = ts.tv_sec; +} + Statistic* LogStatistic::GetStatsInfo(uint16_t logType, uint32_t domain) { - std::unordered_map::iterator it = statistics[logType].find(domain); - if (it != statistics[logType].end()) { + std::unordered_map::iterator it = statisticMap[logType].find(domain); + if (it != statisticMap[logType].end()) { return &(it->second); } return nullptr; @@ -49,7 +59,7 @@ int LogStatistic::Printed(const HilogMsg& msg) tmp.printed = CONTENT_LEN((&msg)); tmp.cached = 0; tmp.dropped = 0; - statistics[msg.type].insert({msg.domain, tmp}); + statisticMap[msg.type].insert({msg.domain, tmp}); } return 0; @@ -65,7 +75,7 @@ int LogStatistic::Cached(const HilogMsg& msg) tmp.printed = 0; tmp.cached = CONTENT_LEN((&msg)); tmp.dropped = 0; - statistics[msg.type].insert({msg.domain, tmp}); + statisticMap[msg.type].insert({msg.domain, tmp}); } return 0; } @@ -89,64 +99,55 @@ int LogStatistic::Dropped(const HilogMsg& msg) tmp.printed = 0; tmp.cached = 0; tmp.dropped = 1; - statistics[msg.type].insert({msg.domain, tmp}); + statisticMap[msg.type].insert({msg.domain, tmp}); } return 0; } -int LogStatistic::GetByType(uint16_t logType, Statistic& result) +int LogStatistic::GetByType(uint16_t logType, QueryResult& result) { - result = {0}; + result.tv_nsec = time_nsec; + result.tv_sec = time_sec; std::unordered_map::iterator it; - for (it = statistics[logType].begin(); it != statistics[logType].end(); it++) { - result.printed += it->second.printed; - result.cached += it->second.cached; - result.dropped += it->second.dropped; + for (it = statisticMap[logType].begin(); it != statisticMap[logType].end(); it++) { + result.statistic.printed += it->second.printed; + result.statistic.cached += it->second.cached; + result.statistic.dropped += it->second.dropped; } return 0; } -int LogStatistic::GetByDomain(uint32_t domain, Statistic& result) +int LogStatistic::GetByDomain(uint32_t domain, QueryResult& result) { - result = {0}; + result.tv_nsec = time_nsec; + result.tv_sec = time_sec; std::unordered_map::iterator it; for (int i = 0; i < LOG_TYPE_MAX; i++) { - std::unordered_map::iterator it = statistics[i].find(domain); - if (it != statistics[i].end()) { - result.printed = it->second.printed; - result.cached = it->second.cached; - result.dropped = it->second.dropped; + std::unordered_map::iterator it = statisticMap[i].find(domain); + if (it != statisticMap[i].end()) { + result.statistic.printed = it->second.printed; + result.statistic.cached = it->second.cached; + result.statistic.dropped = it->second.dropped; break; } } return 0; } -int LogStatistic::GetStatistic(std::unordered_map result[]) -{ - result = statistics; - return 0; -} - -int LogStatistic::ResetByType(uint16_t logType) -{ - std::unordered_map::iterator it; - for (it = statistics[logType].begin(); it != statistics[logType].end(); it++) { - it->second.printed = 0; - it->second.dropped = 0; - } - return 0; -} - -int LogStatistic::ResetByDomain(uint32_t domain) +int LogStatistic::GetStatistic(QueryResults & results) { - std::unordered_map::iterator it; + results.tv_nsec = time_nsec; + results.tv_sec = time_sec; for (int i = 0; i < LOG_TYPE_MAX; i++) { - std::unordered_map::iterator it = statistics[i].find(domain); - if (it != statistics[i].end()) { - it->second.printed = 0; - it->second.dropped = 0; - break; + std::unordered_map::iterator it; + for (it = statisticMap[i].begin(); it != statisticMap[i].end(); it++) { + StatisticInfo info; + info.logType = i; + info.domain = it->first; + info.statistic.printed = it->second.printed; + info.statistic.cached = it->second.cached; + info.statistic.dropped = it->second.dropped; + results.infoVec.emplace_back(info); } } return 0; @@ -154,9 +155,10 @@ int LogStatistic::ResetByDomain(uint32_t domain) int LogStatistic::ResetStatistic() { + ResetTime(); std::unordered_map::iterator it; for (int i = 0; i < LOG_TYPE_MAX; i++) { - for (it = statistics[i].begin(); it != statistics[i].end(); it++) { + for (it = statisticMap[i].begin(); it != statisticMap[i].end(); it++) { it->second.printed = 0; it->second.dropped = 0; } diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index f7fc796..757642a 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -311,7 +311,7 @@ int32_t BufferSizeOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const st int32_t StatisticInfoOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const std::string& logTypeStr, const std::string& domainStr) { - if ((logTypeStr != "" && domainStr != "")/* || (logTypeStr == "" && domainStr == "")*/) { + if ((logTypeStr != "" && domainStr != "")) { return RET_FAIL; } uint16_t logType = GetLogType(logTypeStr); @@ -340,8 +340,6 @@ int32_t StatisticInfoOp(SeqPacketSocketClient& controller, uint8_t msgCmd, } case MC_REQ_STATISTIC_INFO_CLEAR: { StatisticInfoClearRequest staInfoClearReq = {{0}}; - staInfoClearReq.logType = logType; - staInfoClearReq.domain = domain; SetMsgHead(&staInfoClearReq.msgHeader, msgCmd, sizeof(StatisticInfoClearRequest) - sizeof(MessageHeader)); controller.WriteAll((char*)&staInfoClearReq, sizeof(StatisticInfoClearRequest)); break; diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index dad1816..b208333 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -265,6 +265,10 @@ int32_t ControlCmdResult(const char* message) StatisticInfoQueryResponse* staInfoQueryRsp = (StatisticInfoQueryResponse*)message; StatisticInfoResult* infoRst = (StatisticInfoResult*)staInfoQueryRsp->statisticInfoRst; std::ostringstream oss; + char tmp[100] = {}; + time_t ts = infoRst->tv_sec; + strftime(tmp, 100, "%Y-%m-%d %H:%M:%S", localtime(&ts)); + std::cout << "Statistic begin at: " << tmp << std::endl; if (!staInfoQueryRsp) { return RET_FAIL; } -- Gitee From f54585f863b4f54f29865b0af39cad1a11382182 Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Thu, 30 Dec 2021 20:50:38 -0500 Subject: [PATCH 19/28] fix Signed-off-by: youyouyuai --- services/hilogd/include/cmd_executor.h | 4 ++-- services/hilogd/include/log_statistic.h | 1 - services/hilogd/log_buffer.cpp | 1 - services/hilogd/log_querier.cpp | 10 +++++----- services/hilogd/log_statistic.cpp | 3 +-- 5 files changed, 8 insertions(+), 11 deletions(-) diff --git a/services/hilogd/include/cmd_executor.h b/services/hilogd/include/cmd_executor.h index 047deb8..d4fc313 100644 --- a/services/hilogd/include/cmd_executor.h +++ b/services/hilogd/include/cmd_executor.h @@ -24,7 +24,6 @@ #include "log_buffer.h" #include "log_statistic.h" - namespace OHOS { namespace HiviewDFX { struct ClientThread { @@ -34,7 +33,8 @@ struct ClientThread { class CmdExecutor { public: - CmdExecutor(HilogBuffer& buffer, LogStatistic& logStatistic) : m_hilogBuffer(buffer), m_logStatistic(logStatistic) {} + CmdExecutor(HilogBuffer& buffer, LogStatistic& logStatistic) : m_hilogBuffer(buffer), + m_logStatistic(logStatistic) {} ~CmdExecutor(); void MainLoop(); private: diff --git a/services/hilogd/include/log_statistic.h b/services/hilogd/include/log_statistic.h index c5c73a4..f228b44 100644 --- a/services/hilogd/include/log_statistic.h +++ b/services/hilogd/include/log_statistic.h @@ -20,7 +20,6 @@ #include "hilog_common.h" #include "log_data.h" - namespace OHOS { namespace HiviewDFX { diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 347d6b9..59f0d4b 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -115,7 +115,6 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) return eleSize; } - bool HilogBuffer::Query(std::shared_ptr reader) { uint16_t qTypes = reader->queryCondition.types; diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index ecf86d1..0e9c3ec 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -387,7 +387,7 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, pRst->printLen = info.statistic.printed; pRst->cacheLen = info.statistic.cached; pRst->result = (rst < 0) ? rst : RET_SUCCESS; - rstNum ++; + rstNum++; } else if (pStatisticInfoQueryReq->domain != U32_MAX) { pRst->logType = pStatisticInfoQueryReq->logType; pRst->domain = pStatisticInfoQueryReq->domain; @@ -399,17 +399,17 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, pRst->printLen = info.statistic.printed; pRst->cacheLen = info.statistic.cached; pRst->result = (rst < 0) ? rst : RET_SUCCESS; - rstNum ++; + rstNum++; } else { QueryResults info; rst = statistic->GetStatistic(info); pRst->tv_nsec = info.tv_nsec; pRst->tv_sec = info.tv_sec; for (uint32_t i = 0; i < info.infoVec.size(); i++) { - rstNum ++; + rstNum++; if (sizeof(StatisticInfoResult) * rstNum + sizeof(MessageHeader) > MAX_DATA_LEN) { std::cout << "Statistic info response length more than max value" << std::endl; - rstNum --; + rstNum--; break; } pRst->result = rst; @@ -418,7 +418,7 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, pRst->dropped = info.infoVec[i].statistic.dropped; pRst->printLen = info.infoVec[i].statistic.printed; pRst->cacheLen = info.infoVec[i].statistic.cached; - pRst ++; + pRst++; } } uint16_t sendMsgLen = sizeof(StatisticInfoResult) * rstNum; diff --git a/services/hilogd/log_statistic.cpp b/services/hilogd/log_statistic.cpp index 28d8615..2248212 100644 --- a/services/hilogd/log_statistic.cpp +++ b/services/hilogd/log_statistic.cpp @@ -23,7 +23,6 @@ namespace OHOS { namespace HiviewDFX { - LogStatistic::LogStatistic() { ResetTime(); @@ -93,7 +92,7 @@ int LogStatistic::Dropped(const HilogMsg& msg) { Statistic* info = GetStatsInfo(msg.type, msg.domain); if (info) { - info->printed ++; + info->printed++; } else { Statistic tmp; tmp.printed = 0; -- Gitee From 25876f84927e9d748452b579e7edae9c25395833 Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Thu, 30 Dec 2021 21:14:47 -0500 Subject: [PATCH 20/28] domain sort Signed-off-by: youyouyuai --- services/hilogd/log_statistic.cpp | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/services/hilogd/log_statistic.cpp b/services/hilogd/log_statistic.cpp index 2248212..178539f 100644 --- a/services/hilogd/log_statistic.cpp +++ b/services/hilogd/log_statistic.cpp @@ -137,17 +137,31 @@ int LogStatistic::GetStatistic(QueryResults & results) { results.tv_nsec = time_nsec; results.tv_sec = time_sec; + std::vector domainVec; for (int i = 0; i < LOG_TYPE_MAX; i++) { std::unordered_map::iterator it; for (it = statisticMap[i].begin(); it != statisticMap[i].end(); it++) { - StatisticInfo info; - info.logType = i; - info.domain = it->first; - info.statistic.printed = it->second.printed; - info.statistic.cached = it->second.cached; - info.statistic.dropped = it->second.dropped; - results.infoVec.emplace_back(info); + domainVec.emplace_back(it->first); } + } + sort(domainVec.begin(), domainVec.end()); + std::unordered_map::iterator it; + for (uint32_t domain : domainVec) { + for (int i = 0; i < LOG_TYPE_MAX; i++) { + it = statisticMap[i].find(domain); + if (it != statisticMap[i].end()) { + StatisticInfo info; + info.logType = i; + info.domain = it->first; + info.statistic.printed = it->second.printed; + info.statistic.cached = it->second.cached; + info.statistic.dropped = it->second.dropped; + results.infoVec.emplace_back(info); + break; + } + } + + } return 0; } -- Gitee From a652c83050d0839da163ee38535f41d4f2fdceef Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Thu, 6 Jan 2022 01:44:24 -0500 Subject: [PATCH 21/28] fix Signed-off-by: youyouyuai --- frameworks/native/include/hilog_common.h | 5 +++-- services/hilogd/BUILD.gn | 2 +- services/hilogd/include/log_buffer.h | 1 - services/hilogd/include/log_statistic.h | 3 --- services/hilogd/log_querier.cpp | 7 ++----- services/hilogd/log_statistic.cpp | 10 +++------- services/hilogtool/log_controller.cpp | 22 +++++++++++----------- services/hilogtool/log_display.cpp | 4 ++-- 8 files changed, 22 insertions(+), 32 deletions(-) diff --git a/frameworks/native/include/hilog_common.h b/frameworks/native/include/hilog_common.h index 55ce8c7..8e3fbc4 100644 --- a/frameworks/native/include/hilog_common.h +++ b/frameworks/native/include/hilog_common.h @@ -46,8 +46,9 @@ #define ONE_MB (1UL<<20) #define ONE_GB (1UL<<30) #define ONE_TB (1ULL<<40) -#define U16_MAX 0xffff -#define U32_MAX 0xffffffff +#define TYPE_ALL 0xffff +#define DOMAIN_ALL 0xffffffff +#define LEVEL_ALL 0xffff const long long NSEC = 1000000000LL; const long long US = 1000000LL; const long long NS2US = 1000LL; diff --git a/services/hilogd/BUILD.gn b/services/hilogd/BUILD.gn index c972ea5..ec2a3ba 100644 --- a/services/hilogd/BUILD.gn +++ b/services/hilogd/BUILD.gn @@ -32,8 +32,8 @@ ohos_executable("hilogd") { "log_persister_rotator.cpp", "log_querier.cpp", "log_reader.cpp", - "main.cpp", "log_statistic.cpp", + "main.cpp", ] configs = [ ":hilogd_config" ] defines = [ "__RECV_MSG_WITH_UCRED_" ] diff --git a/services/hilogd/include/log_buffer.h b/services/hilogd/include/log_buffer.h index b1104d9..22554b6 100644 --- a/services/hilogd/include/log_buffer.h +++ b/services/hilogd/include/log_buffer.h @@ -46,7 +46,6 @@ public: size_t SetBuffLen(uint16_t logType, uint64_t buffSize); void GetBufferLock(); void ReleaseBufferLock(); - private: size_t size; size_t sizeByType[LOG_TYPE_MAX]; diff --git a/services/hilogd/include/log_statistic.h b/services/hilogd/include/log_statistic.h index f228b44..6d5b054 100644 --- a/services/hilogd/include/log_statistic.h +++ b/services/hilogd/include/log_statistic.h @@ -36,13 +36,11 @@ using StatisticInfo = struct { }; using QueryResult = struct { - uint32_t tv_nsec; uint32_t tv_sec; Statistic statistic; }; using QueryResults = struct { - uint32_t tv_nsec; uint32_t tv_sec; std::vector infoVec; }; @@ -65,7 +63,6 @@ private: std::unordered_map statisticMap[LOG_TYPE_MAX]; Statistic* GetStatsInfo(uint16_t logType, uint32_t domain); void ResetTime(); - uint32_t time_nsec; uint32_t time_sec; }; diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 0e9c3ec..6ddc85a 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -376,24 +376,22 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, StatisticInfoQueryRequest* pStatisticInfoQueryReq = reinterpret_cast(reqMsg); StatisticInfoQueryResponse* pStatisticInfoQueryRsp = reinterpret_cast(msgToSend); StatisticInfoResult* pRst = reinterpret_cast(pStatisticInfoQueryRsp->statisticInfoRst); - if (pStatisticInfoQueryReq->logType != U16_MAX) { + if (pStatisticInfoQueryReq->logType != TYPE_ALL) { pRst->logType = pStatisticInfoQueryReq->logType; pRst->domain = pStatisticInfoQueryReq->domain; QueryResult info; rst = statistic->GetByType(pStatisticInfoQueryReq->logType, info); - pRst->tv_nsec = info.tv_nsec; pRst->tv_sec = info.tv_sec; pRst->dropped = info.statistic.dropped; pRst->printLen = info.statistic.printed; pRst->cacheLen = info.statistic.cached; pRst->result = (rst < 0) ? rst : RET_SUCCESS; rstNum++; - } else if (pStatisticInfoQueryReq->domain != U32_MAX) { + } else if (pStatisticInfoQueryReq->domain != DOMAIN_ALL) { pRst->logType = pStatisticInfoQueryReq->logType; pRst->domain = pStatisticInfoQueryReq->domain; QueryResult info; rst = statistic->GetByDomain(pStatisticInfoQueryReq->domain, info); - pRst->tv_nsec = info.tv_nsec; pRst->tv_sec = info.tv_sec; pRst->dropped = info.statistic.dropped; pRst->printLen = info.statistic.printed; @@ -403,7 +401,6 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, } else { QueryResults info; rst = statistic->GetStatistic(info); - pRst->tv_nsec = info.tv_nsec; pRst->tv_sec = info.tv_sec; for (uint32_t i = 0; i < info.infoVec.size(); i++) { rstNum++; diff --git a/services/hilogd/log_statistic.cpp b/services/hilogd/log_statistic.cpp index 178539f..a1829ba 100644 --- a/services/hilogd/log_statistic.cpp +++ b/services/hilogd/log_statistic.cpp @@ -35,7 +35,6 @@ void LogStatistic::ResetTime() { timespec ts; clock_gettime(CLOCK_REALTIME, &ts); - time_nsec = ts.tv_nsec; time_sec = ts.tv_sec; } @@ -105,10 +104,9 @@ int LogStatistic::Dropped(const HilogMsg& msg) int LogStatistic::GetByType(uint16_t logType, QueryResult& result) { - result.tv_nsec = time_nsec; result.tv_sec = time_sec; std::unordered_map::iterator it; - for (it = statisticMap[logType].begin(); it != statisticMap[logType].end(); it++) { + for (it = statisticMap[logType].begin(); it != statisticMap[logType].end(); ++it) { result.statistic.printed += it->second.printed; result.statistic.cached += it->second.cached; result.statistic.dropped += it->second.dropped; @@ -118,7 +116,6 @@ int LogStatistic::GetByType(uint16_t logType, QueryResult& result) int LogStatistic::GetByDomain(uint32_t domain, QueryResult& result) { - result.tv_nsec = time_nsec; result.tv_sec = time_sec; std::unordered_map::iterator it; for (int i = 0; i < LOG_TYPE_MAX; i++) { @@ -135,12 +132,11 @@ int LogStatistic::GetByDomain(uint32_t domain, QueryResult& result) int LogStatistic::GetStatistic(QueryResults & results) { - results.tv_nsec = time_nsec; results.tv_sec = time_sec; std::vector domainVec; for (int i = 0; i < LOG_TYPE_MAX; i++) { std::unordered_map::iterator it; - for (it = statisticMap[i].begin(); it != statisticMap[i].end(); it++) { + for (it = statisticMap[i].begin(); it != statisticMap[i].end(); ++it) { domainVec.emplace_back(it->first); } } @@ -171,7 +167,7 @@ int LogStatistic::ResetStatistic() ResetTime(); std::unordered_map::iterator it; for (int i = 0; i < LOG_TYPE_MAX; i++) { - for (it = statisticMap[i].begin(); it != statisticMap[i].end(); it++) { + for (it = statisticMap[i].begin(); it != statisticMap[i].end(); ++it) { it->second.printed = 0; it->second.dropped = 0; } diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 757642a..27e4b64 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -85,7 +85,7 @@ uint16_t GetLogType(const string& logTypeStr) } else if (logTypeStr == "kmsg") { logType = LOG_KMSG; } else { - return U16_MAX; + return TYPE_ALL; } return logType; } @@ -145,7 +145,7 @@ uint16_t GetLogLevel(const std::string& logLevelStr, std::string& logLevel) logLevel = "F"; return LOG_FATAL; } - return U16_MAX; + return LEVEL_ALL; } string SetDefaultLogType(const std::string& logTypeStr) @@ -271,7 +271,7 @@ int32_t BufferSizeOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const st } for (iter = 0; iter < logTypeNum; iter++) { pBuffSizeMsg->logType = GetLogType(vecLogType[iter]); - if (pBuffSizeMsg->logType == U16_MAX) { + if (pBuffSizeMsg->logType == TYPE_ALL) { cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; return RET_FAIL; } @@ -290,7 +290,7 @@ int32_t BufferSizeOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const st } for (iter = 0; iter < logTypeNum; iter++) { pBuffResizeMsg->logType = GetLogType(vecLogType[iter]); - if (pBuffResizeMsg->logType == U16_MAX) { + if (pBuffResizeMsg->logType == TYPE_ALL) { cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; return RET_FAIL; } @@ -317,7 +317,7 @@ int32_t StatisticInfoOp(SeqPacketSocketClient& controller, uint8_t msgCmd, uint16_t logType = GetLogType(logTypeStr); uint32_t domain; if (domainStr == "") { - domain = U32_MAX; + domain = DOMAIN_ALL; } else { if (domainStr.rfind("0x", 0) == 0 || domainStr.rfind("0X", 0) == 0) { std::istringstream(domainStr) >> hex >> domain; @@ -371,7 +371,7 @@ int32_t LogClearOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const std: } for (iter = 0; iter < logTypeNum; iter++) { pLogClearMsg->logType = GetLogType(vecLogType[iter]); - if (pLogClearMsg->logType == U16_MAX) { + if (pLogClearMsg->logType == TYPE_ALL) { cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; return RET_FAIL; } @@ -409,7 +409,7 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi } for (iter = 0; iter < logTypeNum; iter++) { uint16_t tmpType = GetLogType(vecLogType[iter]); - if (tmpType == U16_MAX) { + if (tmpType == TYPE_ALL) { cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; return RET_FAIL; } @@ -476,7 +476,7 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi for (iter = 0; iter < logTypeNum; iter++) { uint16_t tmpType = GetLogType(vecLogType[iter]); - if (tmpType == U16_MAX) { + if (tmpType == TYPE_ALL) { cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; return RET_FAIL; } @@ -543,7 +543,7 @@ int32_t SetPropertiesOp(SeqPacketSocketClient& controller, uint8_t operationType std::string keyPre = GetPropertyName(PROP_DOMAIN_LOG_LEVEL); for (iter = 0; iter < domainNum; iter++) { key = keyPre + vecDomain[iter]; - if (GetLogLevel(propertyParm->logLevelStr, value) == U16_MAX) { + if (GetLogLevel(propertyParm->logLevelStr, value) == LEVEL_ALL) { continue; } PropertySet(key.c_str(), value.c_str()); @@ -553,7 +553,7 @@ int32_t SetPropertiesOp(SeqPacketSocketClient& controller, uint8_t operationType std::string keyPre = GetPropertyName(PROP_TAG_LOG_LEVEL); for (iter = 0; iter < tagNum; iter++) { key = keyPre + vecTag[iter]; - if (GetLogLevel(propertyParm->logLevelStr, value) == U16_MAX) { + if (GetLogLevel(propertyParm->logLevelStr, value) == LEVEL_ALL) { continue; } PropertySet(key.c_str(), value.c_str()); @@ -561,7 +561,7 @@ int32_t SetPropertiesOp(SeqPacketSocketClient& controller, uint8_t operationType } } else { key = GetPropertyName(PROP_GLOBAL_LOG_LEVEL); - if (GetLogLevel(propertyParm->logLevelStr, value) == U16_MAX) { + if (GetLogLevel(propertyParm->logLevelStr, value) == LEVEL_ALL) { return RET_FAIL; } PropertySet(key.c_str(), value.c_str()); diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index b208333..b2bfd4b 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -272,11 +272,11 @@ int32_t ControlCmdResult(const char* message) if (!staInfoQueryRsp) { return RET_FAIL; } - if (infoRst->logType == U16_MAX) { + if (infoRst->logType == TYPE_ALL) { oss << std::left << std::setw(20) << "DOMAINID" << std::setw(20) << "DOMAINNAME" << std::setw(20) << "PRINTED" << std::setw(20) << "CACHED" << std::setw(20) << "DROPPED" << std::endl << "0x" << std::setw(18) << hex << infoRst->domain << std::setw(20) << ""; - } else if (infoRst->domain == U32_MAX){ + } else if (infoRst->domain == DOMAIN_ALL){ oss << std::left << std::setw(20) << "TYPE" << std::setw(20) << "PRINTED" << std::setw(20) << "CACHED" << std::setw(20) << "DROPPED" << std::endl << std::setw(20) << GetLogTypeStr(infoRst->logType); } else { -- Gitee From fcca61795810f180310726b33d7cd9882c02ce33 Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Thu, 6 Jan 2022 01:55:51 -0500 Subject: [PATCH 22/28] fix Signed-off-by: youyouyuai --- frameworks/native/include/hilogtool_msg.h | 1 - 1 file changed, 1 deletion(-) diff --git a/frameworks/native/include/hilogtool_msg.h b/frameworks/native/include/hilogtool_msg.h index e2132fa..512566c 100644 --- a/frameworks/native/include/hilogtool_msg.h +++ b/frameworks/native/include/hilogtool_msg.h @@ -199,7 +199,6 @@ typedef struct { typedef struct { int32_t result; - uint32_t tv_nsec; uint32_t tv_sec; uint16_t logType; uint32_t domain; -- Gitee From 14a66274ed51f0cac8f3287d607bbb819b0b9d8a Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Thu, 6 Jan 2022 02:32:30 -0500 Subject: [PATCH 23/28] fix Signed-off-by: youyouyuai --- services/hilogd/include/log_statistic.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/hilogd/include/log_statistic.h b/services/hilogd/include/log_statistic.h index 6d5b054..2547178 100644 --- a/services/hilogd/include/log_statistic.h +++ b/services/hilogd/include/log_statistic.h @@ -23,24 +23,24 @@ namespace OHOS { namespace HiviewDFX { -using Statistic = struct { +struct Statistic{ size_t dropped; size_t printed; size_t cached; }; -using StatisticInfo = struct { +struct StatisticInfo{ uint16_t logType; uint32_t domain; Statistic statistic; }; -using QueryResult = struct { +struct QueryResult{ uint32_t tv_sec; Statistic statistic; }; -using QueryResults = struct { +struct QueryResults{ uint32_t tv_sec; std::vector infoVec; }; -- Gitee From c23a06736515a92225a2d19dd89652f9240804b7 Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Tue, 11 Jan 2022 02:24:41 -0500 Subject: [PATCH 24/28] add statistic info wait Signed-off-by: youyouyuai --- frameworks/native/include/hilogtool_msg.h | 4 +- services/hilogd/log_querier.cpp | 46 +++++++++++++- services/hilogtool/include/log_controller.h | 1 + services/hilogtool/log_controller.cpp | 25 ++++++++ services/hilogtool/log_display.cpp | 67 ++++++++++++++++----- services/hilogtool/main.cpp | 4 +- 6 files changed, 128 insertions(+), 19 deletions(-) diff --git a/frameworks/native/include/hilogtool_msg.h b/frameworks/native/include/hilogtool_msg.h index 512566c..e5212a5 100644 --- a/frameworks/native/include/hilogtool_msg.h +++ b/frameworks/native/include/hilogtool_msg.h @@ -47,7 +47,9 @@ typedef enum { MC_REQ_FLOW_CONTROL, // set flow control request MC_RSP_FLOW_CONTROL, // set flow control response MC_REQ_LOG_CLEAR, // clear log request - MC_RSP_LOG_CLEAR // clear log response + MC_RSP_LOG_CLEAR, // clear log response + MC_REQ_STATISTIC_INFO_WAIT, + MC_RSP_STATISTIC_INFO_WAIT } OperationCmd; /* diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 6ddc85a..466f53a 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -47,6 +47,8 @@ constexpr int DEFAULT_LOG_TYPE = 1< logReader, for (uint32_t i = 0; i < info.infoVec.size(); i++) { rstNum++; if (sizeof(StatisticInfoResult) * rstNum + sizeof(MessageHeader) > MAX_DATA_LEN) { - std::cout << "Statistic info response length more than max value" << std::endl; rstNum--; - break; + uint16_t sendMsgLen = sizeof(StatisticInfoResult) * rstNum; + SetMsgHead(pStatisticInfoQueryRsp->msgHeader, MC_RSP_STATISTIC_INFO_WAIT, sendMsgLen); + logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); + g_info.tv_sec = info.tv_sec; + g_info.infoVec = info.infoVec; + g_infoIndex = i; + return; } pRst->result = rst; pRst->logType = info.infoVec[i].logType; @@ -423,6 +430,38 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); } +void HandleInfoWaitRequest(std::shared_ptr logReader) +{ + char msgToSend[MAX_DATA_LEN]; + int32_t rst = 0; + uint32_t rstNum = 0; + memset_s(msgToSend, MAX_DATA_LEN, 0, MAX_DATA_LEN); + StatisticInfoQueryResponse* pStatisticInfoQueryRsp = reinterpret_cast(msgToSend); + StatisticInfoResult* pRst = reinterpret_cast(pStatisticInfoQueryRsp->statisticInfoRst); + pRst->tv_sec = g_info.tv_sec; + for (uint32_t i = g_infoIndex; i < g_info.infoVec.size(); i++) { + rstNum++; + if (sizeof(StatisticInfoResult) * rstNum + sizeof(MessageHeader) > MAX_DATA_LEN) { + rstNum--; + uint16_t sendMsgLen = sizeof(StatisticInfoResult) * rstNum; + SetMsgHead(pStatisticInfoQueryRsp->msgHeader, MC_RSP_STATISTIC_INFO_WAIT, sendMsgLen); + logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); + g_infoIndex = i; + return; + } + pRst->result = rst; + pRst->logType = g_info.infoVec[i].logType; + pRst->domain = g_info.infoVec[i].domain; + pRst->dropped = g_info.infoVec[i].statistic.dropped; + pRst->printLen = g_info.infoVec[i].statistic.printed; + pRst->cacheLen = g_info.infoVec[i].statistic.cached; + pRst++; + } + uint16_t sendMsgLen = sizeof(StatisticInfoResult) * rstNum; + SetMsgHead(pStatisticInfoQueryRsp->msgHeader, MC_RSP_STATISTIC_INFO_QUERY, sendMsgLen); + logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); +} + void HandleInfoClearRequest(char* reqMsg, std::shared_ptr logReader, HilogBuffer* buffer, LogStatistic* statistic) { @@ -544,6 +583,9 @@ void LogQuerier::LogQuerierThreadFunc(std::shared_ptr logReader) case MC_REQ_STATISTIC_INFO_QUERY: HandleInfoQueryRequest(g_tempBuffer, logReader, hilogBuffer, logStatistic); break; + case MC_REQ_STATISTIC_INFO_WAIT: + HandleInfoWaitRequest(logReader); + break; case MC_REQ_STATISTIC_INFO_CLEAR: HandleInfoClearRequest(g_tempBuffer, logReader, hilogBuffer, logStatistic); break; diff --git a/services/hilogtool/include/log_controller.h b/services/hilogtool/include/log_controller.h index c9369fc..7b9a69a 100644 --- a/services/hilogtool/include/log_controller.h +++ b/services/hilogtool/include/log_controller.h @@ -41,6 +41,7 @@ int32_t StatisticInfoOp(SeqPacketSocketClient& controller, uint8_t msgCmd, int32_t LogClearOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const std::string& logTypeStr); int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersistParam* logPersistParam); int32_t SetPropertiesOp(SeqPacketSocketClient& controller, uint8_t operationType, SetPropertyParam* propertyParm); +int32_t StatisticWaitOp(SeqPacketSocketClient& controller, char* recvBuff, int recvLen); } // namespace HiviewDFX } // namespace OHOS #endif diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 27e4b64..d9764c1 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -38,6 +38,7 @@ const int LOG_PERSIST_FILE_SIZE = 4 * ONE_MB; const int LOG_PERSIST_FILE_NUM = 10; const uint32_t DEFAULT_JOBID = 1; const uint32_t DEFAULT_KMSG_JOBID = 2; + void SetMsgHead(MessageHeader* msgHeader, const uint8_t msgCmd, const uint16_t msgLen) { if (!msgHeader) { @@ -350,6 +351,30 @@ int32_t StatisticInfoOp(SeqPacketSocketClient& controller, uint8_t msgCmd, return RET_SUCCESS; } +int32_t StatisticWaitOp(SeqPacketSocketClient& controller, char* recvBuff, int recvLen) +{ + while (1) { + MessageHeader* msgHeader = (MessageHeader*)recvBuff; + uint8_t msgCmd = msgHeader->msgType; + ControlCmdResult(recvBuff); + if (msgCmd == MC_RSP_STATISTIC_INFO_WAIT) { + StatisticInfoQueryRequest staInfoQueryReq = {{0}}; + staInfoQueryReq.logType = TYPE_ALL; + staInfoQueryReq.domain = DOMAIN_ALL; + SetMsgHead(&staInfoQueryReq.msgHeader, MC_REQ_STATISTIC_INFO_WAIT, sizeof(StatisticInfoQueryRequest) - sizeof(MessageHeader)); + controller.WriteAll((char*)&staInfoQueryReq, sizeof(StatisticInfoQueryRequest)); + if (controller.RecvMsg(recvBuff, recvLen) == 0) { + fprintf(stderr, "Unexpected EOF %s\n", strerror(errno)); + exit(1); + return 0; + } + } else { + break; + } + } + return 0; +} + int32_t LogClearOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const std::string& logTypeStr) { char msgToSend[MSG_MAX_LEN] = {0}; diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index b2bfd4b..41f403b 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -70,7 +70,8 @@ unordered_map errorMsg {ERR_COMMAND_INVALID, "Invalid command, only one control command can be executed each time"}, {ERR_KMSG_SWITCH_VALUE_INVALID, "Invalid kmsg switch value, valid:on/off"} }; - +static bool g_infoWait = false; +static bool g_lastPck = false; string ParseErrorCode(ErrorCode errorCode) { if (errorMsg.count(errorCode) == 0) { @@ -261,32 +262,69 @@ int32_t ControlCmdResult(const char* message) } break; } - case MC_RSP_STATISTIC_INFO_QUERY: { + case MC_RSP_STATISTIC_INFO_WAIT: { StatisticInfoQueryResponse* staInfoQueryRsp = (StatisticInfoQueryResponse*)message; StatisticInfoResult* infoRst = (StatisticInfoResult*)staInfoQueryRsp->statisticInfoRst; + if (!staInfoQueryRsp) { + return RET_FAIL; + } std::ostringstream oss; char tmp[100] = {}; time_t ts = infoRst->tv_sec; strftime(tmp, 100, "%Y-%m-%d %H:%M:%S", localtime(&ts)); - std::cout << "Statistic begin at: " << tmp << std::endl; + if (!g_infoWait) { + g_infoWait = true; + std::cout << "Statistic begin at: " << tmp << std::endl; + oss << std::left << std::setw(20) << "TYPE" << std::setw(20) << "DOMAINID" + << std::setw(20) << "DOMAINNAME" << std::setw(20) << "PRINTED" << std::setw(20) << "CACHED" + << std::setw(20) << "DROPPED" << std::endl; + } + while (infoRst && resultLen < msgLen) { + oss << std::left << std::setw(20) << GetLogTypeStr(infoRst->logType) << "0x" << std::setw(18) + << hex << infoRst->domain << std::setw(20) << "" << std::setw(20) + << GetByteLenPrecise(infoRst->printLen) << std::setw(20) << GetByteLenPrecise(infoRst->cacheLen) + << std::setw(20) << to_string(infoRst->dropped) + "lines" << std::endl; + infoRst++; + resultLen += sizeof(StatisticInfoResult); + } + std::cout << oss.str(); + break; + } + case MC_RSP_STATISTIC_INFO_QUERY: { + StatisticInfoQueryResponse* staInfoQueryRsp = (StatisticInfoQueryResponse*)message; + StatisticInfoResult* infoRst = (StatisticInfoResult*)staInfoQueryRsp->statisticInfoRst; if (!staInfoQueryRsp) { return RET_FAIL; } + std::ostringstream oss; + char tmp[100] = {}; + time_t ts = infoRst->tv_sec; + strftime(tmp, 100, "%Y-%m-%d %H:%M:%S", localtime(&ts)); + if (g_infoWait) { + g_lastPck = true; + } + if (!g_lastPck) { + std::cout << "Statistic begin at: " << tmp << std::endl; + } if (infoRst->logType == TYPE_ALL) { oss << std::left << std::setw(20) << "DOMAINID" << std::setw(20) << "DOMAINNAME" << std::setw(20) << "PRINTED" << std::setw(20) << "CACHED" << std::setw(20) << "DROPPED" << std::endl << "0x" << std::setw(18) << hex << infoRst->domain << std::setw(20) << ""; } else if (infoRst->domain == DOMAIN_ALL){ - oss << std::left << std::setw(20) << "TYPE" << std::setw(20) << "PRINTED" << std::setw(20) << "CACHED" << std::setw(20) << "DROPPED" - << std::endl << std::setw(20) << GetLogTypeStr(infoRst->logType); + oss << std::left << std::setw(20) << "TYPE" << std::setw(20) << "PRINTED" + << std::setw(20) << "CACHED" << std::setw(20) << "DROPPED" << std::endl + << std::setw(20) << GetLogTypeStr(infoRst->logType); } else { - oss << std::left << std::setw(20) << "TYPE" << std::setw(20) << "DOMAINID" << std::setw(20) << - "DOMAINNAME" << std::setw(20) << "PRINTED" << std::setw(20) << "CACHED" << std::setw(20) << "DROPPED" - << std::endl; + if (!g_lastPck) { + oss << std::left << std::setw(20) << "TYPE" << std::setw(20) << "DOMAINID" + << std::setw(20) << "DOMAINNAME" << std::setw(20) << "PRINTED" << std::setw(20) << "CACHED" + << std::setw(20) << "DROPPED" << std::endl; + } while (infoRst && resultLen < msgLen) { - oss << std::setw(20) << GetLogTypeStr(infoRst->logType) << "0x" << std::setw(18) << hex << infoRst->domain - << std::setw(20) << "" << std::setw(20) << GetByteLenPrecise(infoRst->printLen) << std::setw(20) << - GetByteLenPrecise(infoRst->cacheLen) << std::setw(20) << to_string(infoRst->dropped) + "lines" << std::endl; + oss << std::left << std::setw(20) << GetLogTypeStr(infoRst->logType) << "0x" << std::setw(18) + << hex << infoRst->domain << std::setw(20) << "" << std::setw(20) + << GetByteLenPrecise(infoRst->printLen) << std::setw(20) << GetByteLenPrecise(infoRst->cacheLen) + << std::setw(20) << to_string(infoRst->dropped) + "lines" << std::endl;; infoRst++; resultLen += sizeof(StatisticInfoResult); } @@ -306,7 +344,7 @@ int32_t ControlCmdResult(const char* message) case MC_RSP_STATISTIC_INFO_CLEAR: { StatisticInfoClearResponse* staInfoClearRsp = (StatisticInfoClearResponse*)message; if (staInfoClearRsp->result == RET_SUCCESS) { - outputStr += "Statistic Info Clear Success "; + outputStr += "Statistic Info Clear Success\n"; } else if (staInfoClearRsp->result < 0) { outputStr += "Statistic Info Clear Failed\n"; outputStr += ParseErrorCode((ErrorCode)staInfoClearRsp->result); @@ -327,8 +365,7 @@ int32_t ControlCmdResult(const char* message) outputStr += "\n"; } else { outputStr += GetLogTypeStr(pLogClearRst->logType); - outputStr += " log clear success "; - outputStr += "\n"; + outputStr += " log clear success\n"; } pLogClearRst++; resultLen += sizeof(LogClearResult); @@ -415,7 +452,7 @@ int32_t ControlCmdResult(const char* message) default: break; } - cout << outputStr << endl; + cout << outputStr; return 0; } diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index d35e920..474ee06 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -614,7 +614,9 @@ int HilogEntry(int argc, char* argv[]) ControlCmdResult(recvBuffer); break; } - + case MC_RSP_STATISTIC_INFO_WAIT: + StatisticWaitOp(controller, recvBuffer, RECV_BUF_LEN); + break; case LOG_QUERY_RESPONSE: { LogQueryResponseOp(controller, recvBuffer, RECV_BUF_LEN, &context, showFormat); -- Gitee From f0184c205192deeddce05dce3cecd0f8d324f33d Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Thu, 13 Jan 2022 01:11:40 -0500 Subject: [PATCH 25/28] coding rules Signed-off-by: youyouyuai --- services/hilogd/include/flow_control_init.h | 1 - services/hilogd/include/log_statistic.h | 10 +-- services/hilogd/log_buffer.cpp | 2 +- services/hilogd/log_querier.cpp | 4 +- services/hilogd/log_statistic.cpp | 8 +- services/hilogtool/log_controller.cpp | 3 +- services/hilogtool/log_display.cpp | 99 +++++++++------------ 7 files changed, 54 insertions(+), 73 deletions(-) diff --git a/services/hilogd/include/flow_control_init.h b/services/hilogd/include/flow_control_init.h index 0c6f798..988c894 100644 --- a/services/hilogd/include/flow_control_init.h +++ b/services/hilogd/include/flow_control_init.h @@ -22,7 +22,6 @@ namespace OHOS { namespace HiviewDFX { int32_t InitDomainFlowCtrl(); int FlowCtrlDomain(HilogMsg* hilogMsg); - } } #endif diff --git a/services/hilogd/include/log_statistic.h b/services/hilogd/include/log_statistic.h index 2547178..7e0186c 100644 --- a/services/hilogd/include/log_statistic.h +++ b/services/hilogd/include/log_statistic.h @@ -23,24 +23,24 @@ namespace OHOS { namespace HiviewDFX { -struct Statistic{ +struct Statistic { size_t dropped; size_t printed; size_t cached; }; -struct StatisticInfo{ +struct StatisticInfo { uint16_t logType; uint32_t domain; Statistic statistic; }; -struct QueryResult{ +struct QueryResult { uint32_t tv_sec; Statistic statistic; }; -struct QueryResults{ +struct QueryResults { uint32_t tv_sec; std::vector infoVec; }; @@ -58,14 +58,12 @@ public: int GetByDomain(uint32_t domain, QueryResult& result); int GetStatistic(QueryResults & results); int ResetStatistic(); - private: std::unordered_map statisticMap[LOG_TYPE_MAX]; Statistic* GetStatsInfo(uint16_t logType, uint32_t domain); void ResetTime(); uint32_t time_sec; }; - } } #endif diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 59f0d4b..4984c2c 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -82,7 +82,7 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) sizeByType[(*it).type] -= cLen; m_logStatistic.CacheDropped(*it); it = msgList.erase(it); - } + } // Re-confirm if enough elements has been removed if (sizeByType[msg.type] >= (size_t)g_maxBufferSizeByType[msg.type]) { std::cout << "Failed to clean old logs." << std::endl; diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 466f53a..a3aee71 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -368,7 +368,7 @@ void HandleBufferSizeRequest(char* reqMsg, std::shared_ptr logReader, logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); } -void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, HilogBuffer* buffer, +void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, HilogBuffer* buffer, LogStatistic* statistic) { char msgToSend[MAX_DATA_LEN]; @@ -462,7 +462,7 @@ void HandleInfoWaitRequest(std::shared_ptr logReader) logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); } -void HandleInfoClearRequest(char* reqMsg, std::shared_ptr logReader, HilogBuffer* buffer, +void HandleInfoClearRequest(char* reqMsg, std::shared_ptr logReader, HilogBuffer* buffer, LogStatistic* statistic) { char msgToSend[MAX_DATA_LEN]; diff --git a/services/hilogd/log_statistic.cpp b/services/hilogd/log_statistic.cpp index a1829ba..31a696a 100644 --- a/services/hilogd/log_statistic.cpp +++ b/services/hilogd/log_statistic.cpp @@ -38,7 +38,7 @@ void LogStatistic::ResetTime() time_sec = ts.tv_sec; } -Statistic* LogStatistic::GetStatsInfo(uint16_t logType, uint32_t domain) +Statistic* LogStatistic::GetStatsInfo(uint16_t logType, uint32_t domain) { std::unordered_map::iterator it = statisticMap[logType].find(domain); if (it != statisticMap[logType].end()) { @@ -57,7 +57,7 @@ int LogStatistic::Printed(const HilogMsg& msg) tmp.printed = CONTENT_LEN((&msg)); tmp.cached = 0; tmp.dropped = 0; - statisticMap[msg.type].insert({msg.domain, tmp}); + statisticMap[msg.type].insert( {msg.domain, tmp} ); } return 0; @@ -73,7 +73,7 @@ int LogStatistic::Cached(const HilogMsg& msg) tmp.printed = 0; tmp.cached = CONTENT_LEN((&msg)); tmp.dropped = 0; - statisticMap[msg.type].insert({msg.domain, tmp}); + statisticMap[msg.type].insert( {msg.domain, tmp} ); } return 0; } @@ -97,7 +97,7 @@ int LogStatistic::Dropped(const HilogMsg& msg) tmp.printed = 0; tmp.cached = 0; tmp.dropped = 1; - statisticMap[msg.type].insert({msg.domain, tmp}); + statisticMap[msg.type].insert( {msg.domain, tmp} ); } return 0; } diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index d9764c1..d0295b0 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -361,7 +361,8 @@ int32_t StatisticWaitOp(SeqPacketSocketClient& controller, char* recvBuff, int r StatisticInfoQueryRequest staInfoQueryReq = {{0}}; staInfoQueryReq.logType = TYPE_ALL; staInfoQueryReq.domain = DOMAIN_ALL; - SetMsgHead(&staInfoQueryReq.msgHeader, MC_REQ_STATISTIC_INFO_WAIT, sizeof(StatisticInfoQueryRequest) - sizeof(MessageHeader)); + SetMsgHead(&staInfoQueryReq.msgHeader, MC_REQ_STATISTIC_INFO_WAIT, + sizeof(StatisticInfoQueryRequest) - sizeof(MessageHeader)); controller.WriteAll((char*)&staInfoQueryReq, sizeof(StatisticInfoQueryRequest)); if (controller.RecvMsg(recvBuff, recvLen) == 0) { fprintf(stderr, "Unexpected EOF %s\n", strerror(errno)); diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 41f403b..f5de171 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -72,6 +72,10 @@ unordered_map errorMsg }; static bool g_infoWait = false; static bool g_lastPck = false; +const int LENGTH_DEFAULT = 20; +const int LENGTH_HEX = 18; +const int LENGTH_TEMP = 100; +const int LENGHT_PRECISE = 2; string ParseErrorCode(ErrorCode errorCode) { if (errorMsg.count(errorCode) == 0) { @@ -149,33 +153,7 @@ string GetPressAlgStr(uint16_t pressAlg) return pressAlgStr; } -string GetByteLenStr(uint64_t buffSize) -{ - string buffSizeStr; - if (buffSize < ONE_KB) { - buffSizeStr += to_string(buffSize); - buffSizeStr += "B"; - } else if (buffSize < ONE_MB) { - buffSize = buffSize / ONE_KB; - buffSizeStr += to_string(buffSize); - buffSizeStr += "K"; - } else if (buffSize < ONE_GB) { - buffSize = buffSize / ONE_MB; - buffSizeStr += to_string(buffSize); - buffSizeStr += "M"; - } else if (buffSize < ONE_TB) { - buffSize = buffSize / ONE_GB; - buffSizeStr += to_string(buffSize); - buffSizeStr += "G"; - } else { - buffSize = buffSize / ONE_TB; - buffSizeStr += to_string(buffSize); - buffSizeStr += "T"; - } - return buffSizeStr; -} - -string GetByteLenPrecise(uint64_t buffSize) +string GetByteLenPrecise(uint64_t buffSize, uint32_t precise) { string buffSizeStr; if (buffSize < ONE_KB) { @@ -183,22 +161,22 @@ string GetByteLenPrecise(uint64_t buffSize) buffSizeStr += "B"; } else if (buffSize < ONE_MB) { std::ostringstream oss; - oss << setiosflags(ios::fixed) << setprecision(2) << std::fixed << (double)buffSize / ONE_KB; + oss << setiosflags(ios::fixed) << setprecision(precise) << std::fixed << (double)buffSize / ONE_KB; buffSizeStr += oss.str(); buffSizeStr += "K"; } else if (buffSize < ONE_GB) { std::ostringstream oss; - oss << setiosflags(ios::fixed) << setprecision(2) << std::fixed << (double)buffSize / ONE_MB; + oss << setiosflags(ios::fixed) << setprecision(precise) << std::fixed << (double)buffSize / ONE_MB; buffSizeStr += oss.str(); buffSizeStr += "M"; } else if (buffSize < ONE_TB) { std::ostringstream oss; - oss << setiosflags(ios::fixed) << setprecision(2) << std::fixed << (double)buffSize / ONE_GB; + oss << setiosflags(ios::fixed) << setprecision(precise) << std::fixed << (double)buffSize / ONE_GB; buffSizeStr += oss.str(); buffSizeStr += "G"; } else { std::ostringstream oss; - oss << setiosflags(ios::fixed) << setprecision(2) << std::fixed << (double)buffSize / ONE_TB; + oss << setiosflags(ios::fixed) << setprecision(precise) << std::fixed << (double)buffSize / ONE_TB; buffSizeStr += oss.str(); buffSizeStr += "T"; } @@ -231,7 +209,7 @@ int32_t ControlCmdResult(const char* message) } else { outputStr += GetLogTypeStr(pBuffSizeRst->logType); outputStr += " buffer size is "; - outputStr += GetByteLenStr(pBuffSizeRst->buffSize); + outputStr += GetByteLenPrecise(pBuffSizeRst->buffSize, 0); outputStr += "\n"; } pBuffSizeRst++; @@ -254,7 +232,7 @@ int32_t ControlCmdResult(const char* message) } else { outputStr += GetLogTypeStr(pBuffResizeRst->logType); outputStr += " buffer size is "; - outputStr += GetByteLenStr(pBuffResizeRst->buffSize); + outputStr += GetByteLenPrecise(pBuffResizeRst->buffSize, 0); outputStr += "\n"; } pBuffResizeRst++; @@ -269,21 +247,22 @@ int32_t ControlCmdResult(const char* message) return RET_FAIL; } std::ostringstream oss; - char tmp[100] = {}; + char tmp[LENGTH_TEMP] = {}; time_t ts = infoRst->tv_sec; - strftime(tmp, 100, "%Y-%m-%d %H:%M:%S", localtime(&ts)); + strftime(tmp, LENGTH_TEMP, "%Y-%m-%d %H:%M:%S", localtime(&ts)); if (!g_infoWait) { g_infoWait = true; std::cout << "Statistic begin at: " << tmp << std::endl; - oss << std::left << std::setw(20) << "TYPE" << std::setw(20) << "DOMAINID" - << std::setw(20) << "DOMAINNAME" << std::setw(20) << "PRINTED" << std::setw(20) << "CACHED" - << std::setw(20) << "DROPPED" << std::endl; + oss << std::left << std::setw(LENGTH_DEFAULT) << "TYPE" << std::setw(LENGTH_DEFAULT) << "DOMAINID" + << std::setw(LENGTH_DEFAULT) << "DOMAINNAME" << std::setw(LENGTH_DEFAULT) << "PRINTED" + << std::setw(LENGTH_DEFAULT) << "CACHED" << std::setw(LENGTH_DEFAULT) << "DROPPED" << std::endl; } while (infoRst && resultLen < msgLen) { - oss << std::left << std::setw(20) << GetLogTypeStr(infoRst->logType) << "0x" << std::setw(18) - << hex << infoRst->domain << std::setw(20) << "" << std::setw(20) - << GetByteLenPrecise(infoRst->printLen) << std::setw(20) << GetByteLenPrecise(infoRst->cacheLen) - << std::setw(20) << to_string(infoRst->dropped) + "lines" << std::endl; + oss << std::left << std::setw(LENGTH_DEFAULT) << GetLogTypeStr(infoRst->logType) + << "0x" << std::setw(18) << hex << infoRst->domain << std::setw(LENGTH_DEFAULT) << "" + << std::setw(LENGTH_DEFAULT) << GetByteLenPrecise(infoRst->printLen, LENGHT_PRECISE) + << std::setw(LENGTH_DEFAULT) << GetByteLenPrecise(infoRst->cacheLen, LENGHT_PRECISE) + << std::setw(LENGTH_DEFAULT) << to_string(infoRst->dropped) + "lines" << std::endl; infoRst++; resultLen += sizeof(StatisticInfoResult); } @@ -307,24 +286,27 @@ int32_t ControlCmdResult(const char* message) std::cout << "Statistic begin at: " << tmp << std::endl; } if (infoRst->logType == TYPE_ALL) { - oss << std::left << std::setw(20) << "DOMAINID" << std::setw(20) << "DOMAINNAME" - << std::setw(20) << "PRINTED" << std::setw(20) << "CACHED" << std::setw(20) << "DROPPED" - << std::endl << "0x" << std::setw(18) << hex << infoRst->domain << std::setw(20) << ""; - } else if (infoRst->domain == DOMAIN_ALL){ - oss << std::left << std::setw(20) << "TYPE" << std::setw(20) << "PRINTED" - << std::setw(20) << "CACHED" << std::setw(20) << "DROPPED" << std::endl - << std::setw(20) << GetLogTypeStr(infoRst->logType); + oss << std::left << std::setw(LENGTH_DEFAULT) << "DOMAINID" << std::setw(LENGTH_DEFAULT) + << "DOMAINNAME" << std::setw(LENGTH_DEFAULT) << "PRINTED" << std::setw(LENGTH_DEFAULT) + << "CACHED" << std::setw(LENGTH_DEFAULT) << "DROPPED" << std::endl << "0x" << std::setw(18) + << hex << infoRst->domain << std::setw(LENGTH_DEFAULT) << ""; + } else if (infoRst->domain == DOMAIN_ALL) { + oss << std::left << std::setw(LENGTH_DEFAULT) << "TYPE" << std::setw(LENGTH_DEFAULT) << "PRINTED" + << std::setw(LENGTH_DEFAULT) << "CACHED" << std::setw(LENGTH_DEFAULT) << "DROPPED" << std::endl + << std::setw(LENGTH_DEFAULT) << GetLogTypeStr(infoRst->logType); } else { if (!g_lastPck) { - oss << std::left << std::setw(20) << "TYPE" << std::setw(20) << "DOMAINID" - << std::setw(20) << "DOMAINNAME" << std::setw(20) << "PRINTED" << std::setw(20) << "CACHED" - << std::setw(20) << "DROPPED" << std::endl; + oss << std::left << std::setw(LENGTH_DEFAULT) << "TYPE" << std::setw(LENGTH_DEFAULT) << "DOMAINID" + << std::setw(LENGTH_DEFAULT) << "DOMAINNAME" << std::setw(LENGTH_DEFAULT) << "PRINTED" + << std::setw(LENGTH_DEFAULT) << "CACHED" << std::setw(LENGTH_DEFAULT) + << "DROPPED" << std::endl; } while (infoRst && resultLen < msgLen) { - oss << std::left << std::setw(20) << GetLogTypeStr(infoRst->logType) << "0x" << std::setw(18) - << hex << infoRst->domain << std::setw(20) << "" << std::setw(20) - << GetByteLenPrecise(infoRst->printLen) << std::setw(20) << GetByteLenPrecise(infoRst->cacheLen) - << std::setw(20) << to_string(infoRst->dropped) + "lines" << std::endl;; + oss << std::left << std::setw(LENGTH_DEFAULT) << GetLogTypeStr(infoRst->logType) << "0x" + << std::setw(LENGTH_HEX) << hex << infoRst->domain << std::setw(LENGTH_DEFAULT) + << "" << std::setw(LENGTH_DEFAULT) << GetByteLenPrecise(infoRst->printLen, LENGHT_PRECISE) + << std::setw(LENGTH_DEFAULT) << GetByteLenPrecise(infoRst->cacheLen, LENGHT_PRECISE) + << std::setw(LENGTH_DEFAULT) << to_string(infoRst->dropped) + "lines" << std::endl;; infoRst++; resultLen += sizeof(StatisticInfoResult); } @@ -332,8 +314,9 @@ int32_t ControlCmdResult(const char* message) break; } if (infoRst->result == RET_SUCCESS) { - oss << setw(20) << GetByteLenPrecise(infoRst->printLen) << setw(20) << - GetByteLenPrecise(infoRst->cacheLen) << setw(20) << to_string(infoRst->dropped) + "lines" << std::endl; + oss << setw(LENGTH_DEFAULT) << GetByteLenPrecise(infoRst->printLen, LENGHT_PRECISE) + << setw(LENGTH_DEFAULT) << GetByteLenPrecise(infoRst->cacheLen, LENGHT_PRECISE) + << setw(LENGTH_DEFAULT) << to_string(infoRst->dropped) + "lines" << std::endl; std::cout << oss.str(); } else if (infoRst->result < 0) { outputStr += "Statistic Info Query Failed\n"; -- Gitee From 38ce443ac6c08ed85ecad6fa67ee07630c5b2573 Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Thu, 13 Jan 2022 01:57:50 -0500 Subject: [PATCH 26/28] coding rules Signed-off-by: youyouyuai --- services/hilogd/include/log_statistic.h | 2 -- services/hilogd/log_statistic.cpp | 5 ----- services/hilogtool/log_display.cpp | 8 ++++---- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/services/hilogd/include/log_statistic.h b/services/hilogd/include/log_statistic.h index 7e0186c..99a277e 100644 --- a/services/hilogd/include/log_statistic.h +++ b/services/hilogd/include/log_statistic.h @@ -22,7 +22,6 @@ namespace OHOS { namespace HiviewDFX { - struct Statistic { size_t dropped; size_t printed; @@ -49,7 +48,6 @@ class LogStatistic { public: LogStatistic(); ~LogStatistic(); - int Printed(const HilogMsg& msg); int Cached(const HilogMsg& msg); int CacheDropped(const HilogData& data); diff --git a/services/hilogd/log_statistic.cpp b/services/hilogd/log_statistic.cpp index 31a696a..8adc02d 100644 --- a/services/hilogd/log_statistic.cpp +++ b/services/hilogd/log_statistic.cpp @@ -19,10 +19,8 @@ #include #include #include "log_statistic.h" - namespace OHOS { namespace HiviewDFX { - LogStatistic::LogStatistic() { ResetTime(); @@ -156,8 +154,6 @@ int LogStatistic::GetStatistic(QueryResults & results) break; } } - - } return 0; } @@ -174,6 +170,5 @@ int LogStatistic::ResetStatistic() } return 0; } - } } diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index f5de171..9948197 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -259,7 +259,7 @@ int32_t ControlCmdResult(const char* message) } while (infoRst && resultLen < msgLen) { oss << std::left << std::setw(LENGTH_DEFAULT) << GetLogTypeStr(infoRst->logType) - << "0x" << std::setw(18) << hex << infoRst->domain << std::setw(LENGTH_DEFAULT) << "" + << "0x" << std::setw(LENGTH_HEX) << hex << infoRst->domain << std::setw(LENGTH_DEFAULT) << "" << std::setw(LENGTH_DEFAULT) << GetByteLenPrecise(infoRst->printLen, LENGHT_PRECISE) << std::setw(LENGTH_DEFAULT) << GetByteLenPrecise(infoRst->cacheLen, LENGHT_PRECISE) << std::setw(LENGTH_DEFAULT) << to_string(infoRst->dropped) + "lines" << std::endl; @@ -276,9 +276,9 @@ int32_t ControlCmdResult(const char* message) return RET_FAIL; } std::ostringstream oss; - char tmp[100] = {}; + char tmp[LENGTH_TEMP] = {}; time_t ts = infoRst->tv_sec; - strftime(tmp, 100, "%Y-%m-%d %H:%M:%S", localtime(&ts)); + strftime(tmp, LENGTH_TEMP, "%Y-%m-%d %H:%M:%S", localtime(&ts)); if (g_infoWait) { g_lastPck = true; } @@ -288,7 +288,7 @@ int32_t ControlCmdResult(const char* message) if (infoRst->logType == TYPE_ALL) { oss << std::left << std::setw(LENGTH_DEFAULT) << "DOMAINID" << std::setw(LENGTH_DEFAULT) << "DOMAINNAME" << std::setw(LENGTH_DEFAULT) << "PRINTED" << std::setw(LENGTH_DEFAULT) - << "CACHED" << std::setw(LENGTH_DEFAULT) << "DROPPED" << std::endl << "0x" << std::setw(18) + << "CACHED" << std::setw(LENGTH_DEFAULT) << "DROPPED" << std::endl << "0x" << std::setw(LENGTH_HEX) << hex << infoRst->domain << std::setw(LENGTH_DEFAULT) << ""; } else if (infoRst->domain == DOMAIN_ALL) { oss << std::left << std::setw(LENGTH_DEFAULT) << "TYPE" << std::setw(LENGTH_DEFAULT) << "PRINTED" -- Gitee From 809dc496a36de2c22d28ab3513829cfb5f941a58 Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Fri, 14 Jan 2022 04:55:27 -0500 Subject: [PATCH 27/28] fix statistic Signed-off-by: youyouyuai --- services/hilogd/include/log_reader.h | 3 +- services/hilogd/log_querier.cpp | 51 +++++++++++++--------------- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/services/hilogd/include/log_reader.h b/services/hilogd/include/log_reader.h index 32da9a0..0cd1180 100644 --- a/services/hilogd/include/log_reader.h +++ b/services/hilogd/include/log_reader.h @@ -61,7 +61,8 @@ public: QueryCondition queryCondition; std::unique_ptr hilogtoolConnectSocket; bool isNotified; - + int infoIndex; + QueryResults queryInfo; LogReader(); virtual ~LogReader(); bool GetReload() const; diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index a3aee71..446d1b5 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -45,10 +45,7 @@ string g_logPersisterDir = HILOG_FILE_DIR; constexpr int DEFAULT_LOG_LEVEL = 1< logReader, uint16_t sendMsgLen = sizeof(StatisticInfoResult) * rstNum; SetMsgHead(pStatisticInfoQueryRsp->msgHeader, MC_RSP_STATISTIC_INFO_WAIT, sendMsgLen); logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); - g_info.tv_sec = info.tv_sec; - g_info.infoVec = info.infoVec; - g_infoIndex = i; + logReader->queryInfo.tv_sec = info.tv_sec; + logReader->queryInfo.infoVec = info.infoVec; + logReader->infoIndex = i; return; } pRst->result = rst; @@ -438,23 +435,23 @@ void HandleInfoWaitRequest(std::shared_ptr logReader) memset_s(msgToSend, MAX_DATA_LEN, 0, MAX_DATA_LEN); StatisticInfoQueryResponse* pStatisticInfoQueryRsp = reinterpret_cast(msgToSend); StatisticInfoResult* pRst = reinterpret_cast(pStatisticInfoQueryRsp->statisticInfoRst); - pRst->tv_sec = g_info.tv_sec; - for (uint32_t i = g_infoIndex; i < g_info.infoVec.size(); i++) { + pRst->tv_sec = logReader->queryInfo.tv_sec; + for (uint32_t i = logReader->infoIndex; i < logReader->queryInfo.infoVec.size(); i++) { rstNum++; if (sizeof(StatisticInfoResult) * rstNum + sizeof(MessageHeader) > MAX_DATA_LEN) { rstNum--; uint16_t sendMsgLen = sizeof(StatisticInfoResult) * rstNum; SetMsgHead(pStatisticInfoQueryRsp->msgHeader, MC_RSP_STATISTIC_INFO_WAIT, sendMsgLen); logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); - g_infoIndex = i; + logReader->infoIndex = i; return; } pRst->result = rst; - pRst->logType = g_info.infoVec[i].logType; - pRst->domain = g_info.infoVec[i].domain; - pRst->dropped = g_info.infoVec[i].statistic.dropped; - pRst->printLen = g_info.infoVec[i].statistic.printed; - pRst->cacheLen = g_info.infoVec[i].statistic.cached; + pRst->logType = logReader->queryInfo.infoVec[i].logType; + pRst->domain = logReader->queryInfo.infoVec[i].domain; + pRst->dropped = logReader->queryInfo.infoVec[i].statistic.dropped; + pRst->printLen = logReader->queryInfo.infoVec[i].statistic.printed; + pRst->cacheLen = logReader->queryInfo.infoVec[i].statistic.cached; pRst++; } uint16_t sendMsgLen = sizeof(StatisticInfoResult) * rstNum; @@ -547,12 +544,12 @@ void LogQuerier::LogQuerierThreadFunc(std::shared_ptr logReader) int readRes = 0; LogQueryRequest* qRstMsg = nullptr; NextRequest* nRstMsg = nullptr; - - while ((readRes = logReader->hilogtoolConnectSocket->Read(g_tempBuffer, MAX_DATA_LEN - 1)) > 0) { - MessageHeader *header = (MessageHeader *)g_tempBuffer; + char tempBuffer[MAX_DATA_LEN] = {0}; + while ((readRes = logReader->hilogtoolConnectSocket->Read(tempBuffer, MAX_DATA_LEN - 1)) > 0) { + MessageHeader *header = (MessageHeader *)tempBuffer; switch (header->msgType) { case LOG_QUERY_REQUEST: - qRstMsg = (LogQueryRequest*) g_tempBuffer; + qRstMsg = (LogQueryRequest*) tempBuffer; SetCondition(logReader, *qRstMsg); if (!LogTypeForbidden(logReader->queryCondition.types)) { return; @@ -560,37 +557,37 @@ void LogQuerier::LogQuerierThreadFunc(std::shared_ptr logReader) HandleLogQueryRequest(logReader, *hilogBuffer); break; case NEXT_REQUEST: - nRstMsg = (NextRequest*) g_tempBuffer; + nRstMsg = (NextRequest*) tempBuffer; if (nRstMsg->sendId == SENDIDA) { HandleNextRequest(logReader, *hilogBuffer); } break; case MC_REQ_LOG_PERSIST_START: - HandlePersistStartRequest(g_tempBuffer, logReader, *hilogBuffer); + HandlePersistStartRequest(tempBuffer, logReader, *hilogBuffer); break; case MC_REQ_LOG_PERSIST_STOP: - HandlePersistDeleteRequest(g_tempBuffer, logReader); + HandlePersistDeleteRequest(tempBuffer, logReader); break; case MC_REQ_LOG_PERSIST_QUERY: - HandlePersistQueryRequest(g_tempBuffer, logReader); + HandlePersistQueryRequest(tempBuffer, logReader); break; case MC_REQ_BUFFER_RESIZE: - HandleBufferResizeRequest(g_tempBuffer, logReader, hilogBuffer); + HandleBufferResizeRequest(tempBuffer, logReader, hilogBuffer); break; case MC_REQ_BUFFER_SIZE: - HandleBufferSizeRequest(g_tempBuffer, logReader, hilogBuffer); + HandleBufferSizeRequest(tempBuffer, logReader, hilogBuffer); break; case MC_REQ_STATISTIC_INFO_QUERY: - HandleInfoQueryRequest(g_tempBuffer, logReader, hilogBuffer, logStatistic); + HandleInfoQueryRequest(tempBuffer, logReader, hilogBuffer, logStatistic); break; case MC_REQ_STATISTIC_INFO_WAIT: HandleInfoWaitRequest(logReader); break; case MC_REQ_STATISTIC_INFO_CLEAR: - HandleInfoClearRequest(g_tempBuffer, logReader, hilogBuffer, logStatistic); + HandleInfoClearRequest(tempBuffer, logReader, hilogBuffer, logStatistic); break; case MC_REQ_LOG_CLEAR: - HandleBufferClearRequest(g_tempBuffer, logReader, hilogBuffer); + HandleBufferClearRequest(tempBuffer, logReader, hilogBuffer); break; default: break; -- Gitee From e7e47b156eb46cb348fef6df989dd1e3738a4ef3 Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Sun, 16 Jan 2022 21:57:13 -0500 Subject: [PATCH 28/28] rules Signed-off-by: youyouyuai --- services/hilogd/log_querier.cpp | 59 ++++++++++++------------------ services/hilogd/log_statistic.cpp | 6 +-- services/hilogtool/log_display.cpp | 6 ++- 3 files changed, 31 insertions(+), 40 deletions(-) diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 446d1b5..38e3983 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -368,39 +368,14 @@ void HandleBufferSizeRequest(char* reqMsg, std::shared_ptr logReader, void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, HilogBuffer* buffer, LogStatistic* statistic) { - char msgToSend[MAX_DATA_LEN]; - int32_t rst = 0; - uint32_t rstNum = 0; - memset_s(msgToSend, MAX_DATA_LEN, 0, MAX_DATA_LEN); + char msgToSend[MAX_DATA_LEN] = {0}; + int rst = 0, rstNum = 0; StatisticInfoQueryRequest* pStatisticInfoQueryReq = reinterpret_cast(reqMsg); StatisticInfoQueryResponse* pStatisticInfoQueryRsp = reinterpret_cast(msgToSend); StatisticInfoResult* pRst = reinterpret_cast(pStatisticInfoQueryRsp->statisticInfoRst); - if (pStatisticInfoQueryReq->logType != TYPE_ALL) { - pRst->logType = pStatisticInfoQueryReq->logType; - pRst->domain = pStatisticInfoQueryReq->domain; - QueryResult info; - rst = statistic->GetByType(pStatisticInfoQueryReq->logType, info); - pRst->tv_sec = info.tv_sec; - pRst->dropped = info.statistic.dropped; - pRst->printLen = info.statistic.printed; - pRst->cacheLen = info.statistic.cached; - pRst->result = (rst < 0) ? rst : RET_SUCCESS; - rstNum++; - } else if (pStatisticInfoQueryReq->domain != DOMAIN_ALL) { - pRst->logType = pStatisticInfoQueryReq->logType; - pRst->domain = pStatisticInfoQueryReq->domain; - QueryResult info; - rst = statistic->GetByDomain(pStatisticInfoQueryReq->domain, info); - pRst->tv_sec = info.tv_sec; - pRst->dropped = info.statistic.dropped; - pRst->printLen = info.statistic.printed; - pRst->cacheLen = info.statistic.cached; - pRst->result = (rst < 0) ? rst : RET_SUCCESS; - rstNum++; - } else { + if (pStatisticInfoQueryReq->logType == TYPE_ALL && pStatisticInfoQueryReq->domain == DOMAIN_ALL) { QueryResults info; rst = statistic->GetStatistic(info); - pRst->tv_sec = info.tv_sec; for (uint32_t i = 0; i < info.infoVec.size(); i++) { rstNum++; if (sizeof(StatisticInfoResult) * rstNum + sizeof(MessageHeader) > MAX_DATA_LEN) { @@ -413,7 +388,8 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, logReader->infoIndex = i; return; } - pRst->result = rst; + pRst->tv_sec = info.tv_sec; + pRst->result = (rst < 0) ? rst : RET_SUCCESS; pRst->logType = info.infoVec[i].logType; pRst->domain = info.infoVec[i].domain; pRst->dropped = info.infoVec[i].statistic.dropped; @@ -421,6 +397,21 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, pRst->cacheLen = info.infoVec[i].statistic.cached; pRst++; } + } else { + QueryResult info; + pRst->logType = pStatisticInfoQueryReq->logType; + pRst->domain = pStatisticInfoQueryReq->domain; + if (pStatisticInfoQueryReq->logType != TYPE_ALL) { + rst = statistic->GetByType(pStatisticInfoQueryReq->logType, info); + } else if (pStatisticInfoQueryReq->domain != DOMAIN_ALL) { + rst = statistic->GetByDomain(pStatisticInfoQueryReq->domain, info); + } + pRst->tv_sec = info.tv_sec; + pRst->dropped = info.statistic.dropped; + pRst->printLen = info.statistic.printed; + pRst->cacheLen = info.statistic.cached; + pRst->result = (rst < 0) ? rst : RET_SUCCESS; + rstNum++; } uint16_t sendMsgLen = sizeof(StatisticInfoResult) * rstNum; SetMsgHead(pStatisticInfoQueryRsp->msgHeader, MC_RSP_STATISTIC_INFO_QUERY, sendMsgLen); @@ -429,13 +420,10 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, void HandleInfoWaitRequest(std::shared_ptr logReader) { - char msgToSend[MAX_DATA_LEN]; - int32_t rst = 0; - uint32_t rstNum = 0; - memset_s(msgToSend, MAX_DATA_LEN, 0, MAX_DATA_LEN); + char msgToSend[MAX_DATA_LEN] = {0}; + int rstNum = 0; StatisticInfoQueryResponse* pStatisticInfoQueryRsp = reinterpret_cast(msgToSend); StatisticInfoResult* pRst = reinterpret_cast(pStatisticInfoQueryRsp->statisticInfoRst); - pRst->tv_sec = logReader->queryInfo.tv_sec; for (uint32_t i = logReader->infoIndex; i < logReader->queryInfo.infoVec.size(); i++) { rstNum++; if (sizeof(StatisticInfoResult) * rstNum + sizeof(MessageHeader) > MAX_DATA_LEN) { @@ -446,7 +434,8 @@ void HandleInfoWaitRequest(std::shared_ptr logReader) logReader->infoIndex = i; return; } - pRst->result = rst; + pRst->tv_sec = logReader->queryInfo.tv_sec; + pRst->result = RET_SUCCESS; pRst->logType = logReader->queryInfo.infoVec[i].logType; pRst->domain = logReader->queryInfo.infoVec[i].domain; pRst->dropped = logReader->queryInfo.infoVec[i].statistic.dropped; diff --git a/services/hilogd/log_statistic.cpp b/services/hilogd/log_statistic.cpp index 8adc02d..da6a0f0 100644 --- a/services/hilogd/log_statistic.cpp +++ b/services/hilogd/log_statistic.cpp @@ -55,7 +55,7 @@ int LogStatistic::Printed(const HilogMsg& msg) tmp.printed = CONTENT_LEN((&msg)); tmp.cached = 0; tmp.dropped = 0; - statisticMap[msg.type].insert( {msg.domain, tmp} ); + statisticMap[msg.type].insert({msg.domain, tmp}); } return 0; @@ -71,7 +71,7 @@ int LogStatistic::Cached(const HilogMsg& msg) tmp.printed = 0; tmp.cached = CONTENT_LEN((&msg)); tmp.dropped = 0; - statisticMap[msg.type].insert( {msg.domain, tmp} ); + statisticMap[msg.type].insert({msg.domain, tmp}); } return 0; } @@ -95,7 +95,7 @@ int LogStatistic::Dropped(const HilogMsg& msg) tmp.printed = 0; tmp.cached = 0; tmp.dropped = 1; - statisticMap[msg.type].insert( {msg.domain, tmp} ); + statisticMap[msg.type].insert({msg.domain, tmp}); } return 0; } diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 9948197..39a27e2 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -249,7 +249,8 @@ int32_t ControlCmdResult(const char* message) std::ostringstream oss; char tmp[LENGTH_TEMP] = {}; time_t ts = infoRst->tv_sec; - strftime(tmp, LENGTH_TEMP, "%Y-%m-%d %H:%M:%S", localtime(&ts)); + struct tm* tm = localtime(&ts); + strftime(tmp, LENGTH_TEMP, "%Y-%m-%d %H:%M:%S", tm); if (!g_infoWait) { g_infoWait = true; std::cout << "Statistic begin at: " << tmp << std::endl; @@ -278,7 +279,8 @@ int32_t ControlCmdResult(const char* message) std::ostringstream oss; char tmp[LENGTH_TEMP] = {}; time_t ts = infoRst->tv_sec; - strftime(tmp, LENGTH_TEMP, "%Y-%m-%d %H:%M:%S", localtime(&ts)); + struct tm* tm = localtime(&ts); + strftime(tmp, LENGTH_TEMP, "%Y-%m-%d %H:%M:%S", tm); if (g_infoWait) { g_lastPck = true; } -- Gitee