!47 send sysfreeze/appfreeze hisysevent with HIVIEW_LOG_FILE_PATHS

Merge pull request !47 from yantongguang/master
This commit is contained in:
openharmony_ci 2022-01-06 02:27:56 +00:00 committed by Gitee
commit 588cb5dec1
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
12 changed files with 119 additions and 57 deletions

View File

@ -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 = [

View File

@ -69,10 +69,11 @@
"threads": {
"singledthread": {
"SysEventSource": "sysevent_source",
"SysEventService": "sysevent_service"
"SysEventService": "sysevent_service",
"FreezeDetectorPlugin": "freeze_detector"
}
}
}
}
]
}
}

View File

@ -18,6 +18,7 @@
#include <memory>
#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> sysEventDbMgr_;
std::unique_ptr<SysEventStat> sysEventStat_;
std::atomic<bool> hasLoaded_;
std::unique_ptr<EventJsonParser> sysEventParser_;
}; // SysEventService
using NotifySysEvent = std::function<void (std::shared_ptr<Event>)>;
} // namespace HiviewDFX
} // namespace OHOS
#endif // HIVIEW_PLUGINS_EVENT_SERVICE_INCLUDE_SYS_EVENT_SERVICE_H
#endif // HIVIEW_PLUGINS_EVENT_SERVICE_INCLUDE_SYS_EVENT_SERVICE_H

View File

@ -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<EventJsonParser>(dbFile);
}
void SysEventService::SendEvent(std::shared_ptr<Event>& event)
@ -88,7 +94,16 @@ bool SysEventService::OnEvent(std::shared_ptr<Event>& 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<std::string>& cmds)
}
}
} // namespace HiviewDFX
} // namespace OHOS
} // namespace OHOS

View File

@ -29,7 +29,7 @@ namespace HiviewDFX {
DEFINE_LOG_TAG("FreezeDetector");
void DBHelper::SelectEventFromDB(
bool all, unsigned long start, unsigned long end, std::list<WatchPoint>& list)
bool all, unsigned long long start, unsigned long long end, std::list<WatchPoint>& list)
{
if (start > end) {
return;

View File

@ -25,7 +25,7 @@ namespace OHOS {
namespace HiviewDFX {
class DBHelper {
public:
static void SelectEventFromDB(bool all, unsigned long start, unsigned long end, std::list<WatchPoint>& list);
static void SelectEventFromDB(bool all, unsigned long long start, unsigned long long end, std::list<WatchPoint>& list);
static void UpdateEventIntoDB(const WatchPoint& watchPoint, int id);
};
} // namespace HiviewDFX

View File

@ -34,6 +34,7 @@ std::string FreezeDetectorPlugin::GetListenerName()
bool FreezeDetectorPlugin::ReadyToLoad()
{
Vendor::GetInstance().Init();
return FreezeResolver::GetInstance().Init();
}

View File

@ -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 {

View File

@ -39,12 +39,12 @@ const std::vector<std::pair<std::string, std::string>> 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<std::string> Vendor::GetFreezeStringIds() const
{
std::set<std::string> 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<WatchPoint>& 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<WatchPoint>& 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<std::string> 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<PipelineEvent> Vendor::MakeEvent(
return nullptr;
}
bool Vendor::Init()
{
logStore_ = std::make_unique<LogStoreEx>(FREEZE_DETECTOR_PATH, true);
logStore_->SetMaxSize(MAX_FOLDER_SIZE);
logStore_->SetMinKeepingFileNumber(MAX_FILE_NUM);
logStore_->Init();
return true;
}
Vendor::Vendor()
{
}

View File

@ -21,6 +21,7 @@
#include <vector>
#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<std::string> 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<WatchPoint>& 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<std::pair<std::string, std::string>> applicationPairs_;
static const std::vector<std::pair<std::string, std::string>> systemPairs_;
std::unique_ptr<LogStoreEx> logStore_ = nullptr;
};
} // namespace HiviewDFX
} // namespace OHOS

View File

@ -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_;
}

View File

@ -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_;