From 33edd308cd420af54238aeacb8bc776a43b71db1 Mon Sep 17 00:00:00 2001 From: yantongguang Date: Wed, 5 Jan 2022 15:34:29 +0800 Subject: [PATCH] send sysfreeze/appfreeze hisysevent with HIVIEW_LOG_FILE_PATHS Signed-off-by: yantongguang --- .../eventservice/service/test/BUILD.gn | 5 + build/plugin_build.json | 5 +- .../eventservice/include/sys_event_service.h | 4 +- plugins/eventservice/sys_event_service.cpp | 19 ++- plugins/freeze_detector/db_helper.cpp | 2 +- plugins/freeze_detector/db_helper.h | 2 +- plugins/freeze_detector/plugin.cpp | 1 + plugins/freeze_detector/resolver.cpp | 4 +- plugins/freeze_detector/vendor.cpp | 114 +++++++++++------- plugins/freeze_detector/vendor.h | 8 +- plugins/freeze_detector/watch_point.cpp | 4 +- plugins/freeze_detector/watch_point.h | 8 +- 12 files changed, 119 insertions(+), 57 deletions(-) diff --git a/adapter/plugins/eventservice/service/test/BUILD.gn b/adapter/plugins/eventservice/service/test/BUILD.gn index 8d219981..a4577463 100644 --- a/adapter/plugins/eventservice/service/test/BUILD.gn +++ b/adapter/plugins/eventservice/service/test/BUILD.gn @@ -24,6 +24,8 @@ group("unittest") { config("unittest_config") { include_dirs = [ "//utils/native/base/include", + "//third_party/cJSON", + "//third_party/jsoncpp/include", "unittest/common", "$hiview_plugin/eventservice/include", "$hiview_base/include", @@ -37,6 +39,7 @@ ohos_unittest("SysEventServiceOhosUnitTest") { configs = [ ":unittest_config" ] sources = [ + "$hiview_plugin/eventservice/event_json_parser.cpp", "$hiview_plugin/eventservice/service/sys_event_stat.cpp", "$hiview_plugin/eventservice/store/sys_event_db_backup.cpp", "$hiview_plugin/eventservice/store/sys_event_db_clean.cpp", @@ -53,7 +56,9 @@ ohos_unittest("SysEventServiceOhosUnitTest") { "$hiview_adapter/plugins/eventservice/service/idl:sys_event_service_ohos", "$hiview_base:hiview_base", "$hiview_core:hiview_core", + "//third_party/cJSON:cjson_static", "//third_party/googletest:gtest_main", + "//third_party/jsoncpp:jsoncpp", ] external_deps = [ diff --git a/build/plugin_build.json b/build/plugin_build.json index 47bf60e3..f003d75c 100755 --- a/build/plugin_build.json +++ b/build/plugin_build.json @@ -69,10 +69,11 @@ "threads": { "singledthread": { "SysEventSource": "sysevent_source", - "SysEventService": "sysevent_service" + "SysEventService": "sysevent_service", + "FreezeDetectorPlugin": "freeze_detector" } } } } ] -} \ No newline at end of file +} diff --git a/plugins/eventservice/include/sys_event_service.h b/plugins/eventservice/include/sys_event_service.h index 86082d90..53bc3769 100644 --- a/plugins/eventservice/include/sys_event_service.h +++ b/plugins/eventservice/include/sys_event_service.h @@ -18,6 +18,7 @@ #include #include "event.h" +#include "event_json_parser.h" #include "plugin.h" #include "sys_event_db_mgr.h" #include "sys_event_stat.h" @@ -40,8 +41,9 @@ private: std::unique_ptr sysEventDbMgr_; std::unique_ptr sysEventStat_; std::atomic hasLoaded_; + std::unique_ptr sysEventParser_; }; // SysEventService using NotifySysEvent = std::function)>; } // namespace HiviewDFX } // namespace OHOS -#endif // HIVIEW_PLUGINS_EVENT_SERVICE_INCLUDE_SYS_EVENT_SERVICE_H \ No newline at end of file +#endif // HIVIEW_PLUGINS_EVENT_SERVICE_INCLUDE_SYS_EVENT_SERVICE_H diff --git a/plugins/eventservice/sys_event_service.cpp b/plugins/eventservice/sys_event_service.cpp index fd1f7e3c..45f74894 100644 --- a/plugins/eventservice/sys_event_service.cpp +++ b/plugins/eventservice/sys_event_service.cpp @@ -47,6 +47,12 @@ void SysEventService::OnLoad() SysEventServiceAdapter::StartService(this, notifyFunc); sysEventDbMgr_->StartCheckStoreTask(this->workLoop_); hasLoaded_ = true; + + std::string dbFile = + HiviewGlobal::GetInstance()->GetHiViewDirectory(HiviewContext::DirectoryType::CONFIG_DIRECTORY); + dbFile = (dbFile[dbFile.size() - 1] != '/') ? (dbFile + "/hisysevent.def") : (dbFile + "hisysevent.def"); + HIVIEW_LOGE("dbFile is %{public}s", dbFile.c_str()); + sysEventParser_ = std::make_unique(dbFile); } void SysEventService::SendEvent(std::shared_ptr& event) @@ -88,7 +94,16 @@ bool SysEventService::OnEvent(std::shared_ptr& event) sysEventStat_->AccumulateEvent(false); return false; } - sysEventStat_->AccumulateEvent(sysEvent->domain_, sysEvent->eventName_); + + if (!sysEventParser_->HandleEventJson(sysEvent)) { + HIVIEW_LOGE("HandleEventJson fail"); + sysEventStat_->AccumulateEvent(sysEvent->domain_, sysEvent->eventName_, false); + } + else { + sysEventStat_->AccumulateEvent(sysEvent->domain_, sysEvent->eventName_); + } + HIVIEW_LOGI("SysEvent Json String is %{}s.", sysEvent->jsonExtraInfo_.c_str()); + SysEventServiceAdapter::OnSysEvent(sysEvent); sysEventDbMgr_->SaveToStore(sysEvent); return true; @@ -124,4 +139,4 @@ void SysEventService::Dump(int fd, const std::vector& cmds) } } } // namespace HiviewDFX -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/plugins/freeze_detector/db_helper.cpp b/plugins/freeze_detector/db_helper.cpp index d223b8cf..e7858351 100644 --- a/plugins/freeze_detector/db_helper.cpp +++ b/plugins/freeze_detector/db_helper.cpp @@ -29,7 +29,7 @@ namespace HiviewDFX { DEFINE_LOG_TAG("FreezeDetector"); void DBHelper::SelectEventFromDB( - bool all, unsigned long start, unsigned long end, std::list& list) + bool all, unsigned long long start, unsigned long long end, std::list& list) { if (start > end) { return; diff --git a/plugins/freeze_detector/db_helper.h b/plugins/freeze_detector/db_helper.h index 0ebef38e..7468b676 100644 --- a/plugins/freeze_detector/db_helper.h +++ b/plugins/freeze_detector/db_helper.h @@ -25,7 +25,7 @@ namespace OHOS { namespace HiviewDFX { class DBHelper { public: - static void SelectEventFromDB(bool all, unsigned long start, unsigned long end, std::list& list); + static void SelectEventFromDB(bool all, unsigned long long start, unsigned long long end, std::list& list); static void UpdateEventIntoDB(const WatchPoint& watchPoint, int id); }; } // namespace HiviewDFX diff --git a/plugins/freeze_detector/plugin.cpp b/plugins/freeze_detector/plugin.cpp index 13075439..6af5b536 100644 --- a/plugins/freeze_detector/plugin.cpp +++ b/plugins/freeze_detector/plugin.cpp @@ -34,6 +34,7 @@ std::string FreezeDetectorPlugin::GetListenerName() bool FreezeDetectorPlugin::ReadyToLoad() { + Vendor::GetInstance().Init(); return FreezeResolver::GetInstance().Init(); } diff --git a/plugins/freeze_detector/resolver.cpp b/plugins/freeze_detector/resolver.cpp index e79688d4..93ab96bf 100644 --- a/plugins/freeze_detector/resolver.cpp +++ b/plugins/freeze_detector/resolver.cpp @@ -63,8 +63,8 @@ bool FreezeResolver::ResolveEvent(WatchPoint& watchPoint, WatchPoint& matchedWat } } - unsigned long start = watchPoint.GetTimestamp() - (window * MILLISECOND); - unsigned long end = watchPoint.GetTimestamp(); + unsigned long long start = watchPoint.GetTimestamp() - (window * MILLISECOND); + unsigned long long end = watchPoint.GetTimestamp(); if (window == 0) { list.push_back(watchPoint); } else { diff --git a/plugins/freeze_detector/vendor.cpp b/plugins/freeze_detector/vendor.cpp index 715cb37c..4c2abc8d 100644 --- a/plugins/freeze_detector/vendor.cpp +++ b/plugins/freeze_detector/vendor.cpp @@ -39,12 +39,12 @@ const std::vector> Vendor::systemPairs_ = { bool Vendor::IsFreezeEvent(const std::string& domain, const std::string& stringId) const { - for (auto const pair : applicationPairs_) { + for (auto const &pair : applicationPairs_) { if (domain == pair.first && stringId == pair.second) { return true; } } - for (auto const pair : systemPairs_) { + for (auto const &pair : systemPairs_) { if (domain == pair.first && stringId == pair.second) { return true; } @@ -54,7 +54,7 @@ bool Vendor::IsFreezeEvent(const std::string& domain, const std::string& stringI bool Vendor::IsApplicationEvent(const std::string& domain, const std::string& stringId) const { - for (auto const pair : applicationPairs_) { + for (auto const &pair : applicationPairs_) { if (domain == pair.first && stringId == pair.second) { return true; } @@ -64,7 +64,7 @@ bool Vendor::IsApplicationEvent(const std::string& domain, const std::string& st bool Vendor::IsSystemEvent(const std::string& domain, const std::string& stringId) const { - for (auto const pair : systemPairs_) { + for (auto const &pair : systemPairs_) { if (domain == pair.first && stringId == pair.second) { return true; } @@ -91,10 +91,10 @@ std::set Vendor::GetFreezeStringIds() const { std::set set; - for (auto const pair : applicationPairs_) { + for (auto const &pair : applicationPairs_) { set.insert(pair.second); } - for (auto const pair : systemPairs_) { + for (auto const &pair : systemPairs_) { set.insert(pair.second); } @@ -150,7 +150,7 @@ bool Vendor::ReduceRelevanceEvents(std::list& list, const FreezeResu return list.size() != 0; } -std::string Vendor::GetTimeString(unsigned long timestamp) const +std::string Vendor::GetTimeString(unsigned long long timestamp) const { struct tm tm; time_t ts; @@ -162,6 +162,20 @@ std::string Vendor::GetTimeString(unsigned long timestamp) const return std::string(buf, strlen(buf)); } +void Vendor::DumpEventInfo(std::ostringstream& oss, const std::string& header, const WatchPoint& watchPoint) const +{ + oss << header << std::endl; + oss << FreezeDetectorPlugin::EVENT_DOMAIN << FreezeDetectorPlugin::COLON << watchPoint.GetDomain() << std::endl; + oss << FreezeDetectorPlugin::EVENT_STRINGID << FreezeDetectorPlugin::COLON << watchPoint.GetStringId() << std::endl; + oss << FreezeDetectorPlugin::EVENT_TIMESTAMP << FreezeDetectorPlugin::COLON << + watchPoint.GetTimestamp() << std::endl; + oss << FreezeDetectorPlugin::EVENT_PID << FreezeDetectorPlugin::COLON << watchPoint.GetPid() << std::endl; + oss << FreezeDetectorPlugin::EVENT_UID << FreezeDetectorPlugin::COLON << watchPoint.GetUid() << std::endl; + oss << FreezeDetectorPlugin::EVENT_PACKAGE_NAME << FreezeDetectorPlugin::COLON << watchPoint.GetPackageName() << std::endl; + oss << FreezeDetectorPlugin::EVENT_PROCESS_NAME << FreezeDetectorPlugin::COLON << watchPoint.GetProcessName() << std::endl; + oss << FreezeDetectorPlugin::EVENT_MSG << FreezeDetectorPlugin::COLON << watchPoint.GetMsg() << std::endl; +} + std::string Vendor::MergeEventLog( const WatchPoint &watchPoint, const std::list& list, const FreezeResult& result, std::string& digest) const @@ -181,26 +195,22 @@ std::string Vendor::MergeEventLog( packageName = stringId; } - std::string retPath = FAULT_LOGGER_PATH + APPFREEZE + HYPHEN + packageName + HYPHEN + std::to_string(uid) + HYPHEN + timestamp; - std::string logPath = FAULT_LOGGER_PATH + APPFREEZE + HYPHEN + packageName + HYPHEN + std::to_string(uid) + HYPHEN + timestamp + POSTFIX; - - std::ofstream output(logPath, std::ios::out); - if (!output.is_open()) { - HIVIEW_LOGE("cannot open log file for writing:%{public}s.\n", logPath.c_str()); - return ""; + std::string retPath; + std::string logPath; + std::string logName; + if (IsApplicationResult(result)) { + retPath = FAULT_LOGGER_PATH + APPFREEZE + HYPHEN + packageName + HYPHEN + std::to_string(uid) + HYPHEN + timestamp; + logPath = FREEZE_DETECTOR_PATH + APPFREEZE + HYPHEN + packageName + HYPHEN + std::to_string(uid) + HYPHEN + timestamp + POSTFIX; + logName = APPFREEZE + HYPHEN + packageName + HYPHEN + std::to_string(uid) + HYPHEN + timestamp + POSTFIX; } - output << HEADER << std::endl; - output << FreezeDetectorPlugin::EVENT_DOMAIN << FreezeDetectorPlugin::COLON << domain << std::endl; - output << FreezeDetectorPlugin::EVENT_STRINGID << FreezeDetectorPlugin::COLON << stringId << std::endl; - output << FreezeDetectorPlugin::EVENT_TIMESTAMP << FreezeDetectorPlugin::COLON << - watchPoint.GetTimestamp() << std::endl; - output << FreezeDetectorPlugin::EVENT_PID << FreezeDetectorPlugin::COLON << pid << std::endl; - output << FreezeDetectorPlugin::EVENT_UID << FreezeDetectorPlugin::COLON << uid << std::endl; - output << FreezeDetectorPlugin::EVENT_PACKAGE_NAME << FreezeDetectorPlugin::COLON << packageName << std::endl; - output << FreezeDetectorPlugin::EVENT_PROCESS_NAME << FreezeDetectorPlugin::COLON << processName << std::endl; - output << FreezeDetectorPlugin::EVENT_MSG << FreezeDetectorPlugin::COLON << msg << std::endl; - output.flush(); - output.close(); + else { + retPath = FAULT_LOGGER_PATH + SYSFREEZE + HYPHEN + packageName + HYPHEN + std::to_string(uid) + HYPHEN + timestamp; + logPath = FREEZE_DETECTOR_PATH + SYSFREEZE + HYPHEN + packageName + HYPHEN + std::to_string(uid) + HYPHEN + timestamp + POSTFIX; + logName = SYSFREEZE + HYPHEN + packageName + HYPHEN + std::to_string(uid) + HYPHEN + timestamp + POSTFIX; + } + + std::ostringstream header; + DumpEventInfo(header, TRIGGER_HEADER, watchPoint); HIVIEW_LOGI("merging list size %{public}zu", list.size()); std::ostringstream body; @@ -208,31 +218,35 @@ std::string Vendor::MergeEventLog( std::string filePath = node.GetLogPath(); HIVIEW_LOGI("merging file:%{public}s.\n", filePath.c_str()); if (filePath == "" || filePath == "nolog" || FileUtil::FileExists(filePath) == false) { + HIVIEW_LOGI("only header, no content:[%{public}s, %{public}s]", + node.GetDomain().c_str(), node.GetStringId().c_str()); + DumpEventInfo(body, HEADER, node); continue; } std::ifstream ifs(filePath, std::ios::in); if (!ifs.is_open()) { HIVIEW_LOGE("cannot open log file for reading:%{public}s.\n", filePath.c_str()); + DumpEventInfo(body, HEADER, node); continue; } - std::ofstream ofs(logPath, std::ios::out | std::ios::app); - if (!ofs.is_open()) { - ifs.close(); - HIVIEW_LOGE("cannot open log file for writing:%{public}s.\n", logPath.c_str()); - continue; - } + body << HEADER << std::endl; + body << ifs.rdbuf(); - ofs << HEADER << std::endl; - ofs << ifs.rdbuf(); - - ofs.flush(); - ofs.close(); ifs.close(); } - std::string type = IsSystemResult(result) ? SP_SYSTEMHUNGFAULT : SP_APPFREEZE; + int fd = logStore_->CreateLogFile(logName); + if (fd < 0) { + HIVIEW_LOGE("failed to create log file %{public}s.\n", logPath.c_str()); + return ""; + } + FileUtil::SaveStringToFd(fd, header.str()); + FileUtil::SaveStringToFd(fd, body.str()); + close(fd); + + std::string type = IsApplicationResult(result) ? SP_APPFREEZE : SP_SYSTEMHUNGFAULT; auto eventInfos = SmartParser::Analysis(logPath, SMART_PARSER_PATH, type); digest = eventInfos[SP_ENDSTACK]; std::string summary = eventInfos[SP_ENDSTACK]; @@ -249,14 +263,23 @@ std::string Vendor::MergeEventLog( info.logPath = logPath; AddFaultLog(info); - HiSysEvent::Write("RELIABILITY", IsSystemResult(result) ? "SYSTEM_FREEZE" : "APP_FREEZE", HiSysEvent::FAULT, + std::vector paths = {retPath}; + HiSysEvent::Write("RELIABILITY", IsApplicationResult(result) ? "APP_FREEZE" : "SYSTEM_FREEZE", HiSysEvent::FAULT, "SUB_EVENT_TYPE", stringId, "EVENT_TIME", timestamp, "MODULE", packageName, "PNAME", packageName, "REASON", stringId, - "DIAG_INFO", summary, - "STACK", summary); + "DIAG_INFO", digest, + "STACK", summary, + "HIVIEW_LOG_FILE_PATHS", paths, + "DOMAIN", domain, + "STRING_ID", stringId, + "PID", pid, + "UID", uid, + "PACKAGE_NAME", packageName, + "PROCESS_NAME", processName, + "MSG", msg); return retPath; } @@ -273,6 +296,15 @@ std::shared_ptr Vendor::MakeEvent( return nullptr; } +bool Vendor::Init() +{ + logStore_ = std::make_unique(FREEZE_DETECTOR_PATH, true); + logStore_->SetMaxSize(MAX_FOLDER_SIZE); + logStore_->SetMinKeepingFileNumber(MAX_FILE_NUM); + logStore_->Init(); + return true; +} + Vendor::Vendor() { } diff --git a/plugins/freeze_detector/vendor.h b/plugins/freeze_detector/vendor.h index e4fffea4..9ba9fd2d 100644 --- a/plugins/freeze_detector/vendor.h +++ b/plugins/freeze_detector/vendor.h @@ -21,6 +21,7 @@ #include #include "faultlog_info.h" +#include "log_store_ex.h" #include "pipeline.h" #include "resolver.h" #include "smart_parser.h" @@ -38,7 +39,8 @@ public: bool IsApplicationEvent(const std::string& domain, const std::string& stringId) const; bool IsBetaVersion() const; std::set GetFreezeStringIds() const; - std::string GetTimeString(unsigned long timestamp) const; + std::string GetTimeString(unsigned long long timestamp) const; + void DumpEventInfo(std::ostringstream& oss, const std::string& header, const WatchPoint& watchPoint) const; std::string MergeEventLog( const WatchPoint &watchPoint, const std::list& list, const FreezeResult& result, std::string& digest) const; @@ -55,15 +57,18 @@ private: static const int TIME_STRING_LEN = 16; static const int MAX_FILE_NUM = 500; static const int MAX_FOLDER_SIZE = 50 * 1024 * 1024; + static const inline std::string TRIGGER_HEADER = ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"; static const inline std::string HEADER = "*******************************************"; static const inline std::string HYPHEN = "-"; static const inline std::string NEW_LINE = "\n"; static const inline std::string EVENT_SUMMARY = "SUMMARY"; static const inline std::string POSTFIX = ".tmp"; static const inline std::string APPFREEZE = "appfreeze"; + static const inline std::string SYSFREEZE = "sysfreeze"; static const inline std::string SP_SYSTEMHUNGFAULT = "SystemHungFault"; static const inline std::string SP_APPFREEZE = "AppFreeze"; static const inline std::string SP_ENDSTACK = "END_STACK"; + static const inline std::string FREEZE_DETECTOR_PATH = "/data/log/faultlog/"; static const inline std::string FAULT_LOGGER_PATH = "/data/log/faultlog/faultlogger/"; static const inline std::string SMART_PARSER_PATH = "/system/etc/hiview/"; @@ -74,6 +79,7 @@ private: static const std::vector> applicationPairs_; static const std::vector> systemPairs_; + std::unique_ptr logStore_ = nullptr; }; } // namespace HiviewDFX } // namespace OHOS diff --git a/plugins/freeze_detector/watch_point.cpp b/plugins/freeze_detector/watch_point.cpp index a23fce11..e5baecff 100644 --- a/plugins/freeze_detector/watch_point.cpp +++ b/plugins/freeze_detector/watch_point.cpp @@ -52,7 +52,7 @@ WatchPoint::Builder& WatchPoint::Builder::InitSeq(long seq) return *this; } -WatchPoint::Builder& WatchPoint::Builder::InitTimestamp(unsigned long timestamp) +WatchPoint::Builder& WatchPoint::Builder::InitTimestamp(unsigned long long timestamp) { timestamp_ = timestamp; return *this; @@ -123,7 +123,7 @@ long WatchPoint::GetSeq() const return seq_; } -unsigned long WatchPoint::GetTimestamp() const +unsigned long long WatchPoint::GetTimestamp() const { return timestamp_; } diff --git a/plugins/freeze_detector/watch_point.h b/plugins/freeze_detector/watch_point.h index 0be4a8dd..1790a761 100644 --- a/plugins/freeze_detector/watch_point.h +++ b/plugins/freeze_detector/watch_point.h @@ -28,7 +28,7 @@ public: Builder(); ~Builder(); Builder& InitSeq(long seq); - Builder& InitTimestamp(unsigned long timestamp); + Builder& InitTimestamp(unsigned long long timestamp); Builder& InitPid(long pid); Builder& InitTid(long tid); Builder& InitUid(long uid); @@ -42,7 +42,7 @@ public: private: long seq_; - unsigned long timestamp_; + unsigned long long timestamp_; long pid_; long uid_; long tid_; @@ -60,7 +60,7 @@ public: ~WatchPoint() {}; long GetSeq() const; - unsigned long GetTimestamp() const; + unsigned long long GetTimestamp() const; long GetPid() const; long GetTid() const; long GetUid() const; @@ -77,7 +77,7 @@ public: private: long seq_; - unsigned long timestamp_; + unsigned long long timestamp_; long pid_; long uid_; long tid_;