From 0e4e323297c16ae36ba3a95d2de9c023f2f0935c Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Mon, 16 Aug 2021 10:19:38 +0800 Subject: [PATCH 1/9] Fix persister restart fail Signed-off-by: Wu Shangwei <2826256824@qq.com> Change-Id: I6e79f664722d1ffd49e5668653bd3998138b2354 --- services/hilogd/log_persister_rotator.cpp | 3 ++- services/hilogd/log_querier.cpp | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index 61db2e2..f0d6a82 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -49,6 +49,7 @@ int LogPersisterRotator::Init() } } fdinfo = fopen((mmapPath + ".info").c_str(), "r+"); + if (fdinfo == nullptr) fdinfo = fopen((mmapPath + ".info").c_str(), "w+"); if (fdinfo == nullptr) return RET_FAIL; return RET_SUCCESS; } @@ -127,4 +128,4 @@ void LogPersisterRotator::SetId(uint32_t pId) id = pId; } } // namespace HiviewDFX -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 94c4041..f66e19c 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -144,8 +144,8 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade persister->queryCondition.levels = DEFAULT_LOG_LEVEL; int saveInfoRes = persister->SaveInfo(*pLogPersistStartMsg); pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; - pLogPersistStartRst->result = persister->Init(); int rotatorRes = rotator->Init(); + pLogPersistStartRst->result = persister->Init(); if (pLogPersistStartRst->result == RET_FAIL || saveInfoRes == RET_FAIL || rotatorRes == RET_FAIL) { cout << "LogPersister failed to initialize!" << endl; persister.reset(); @@ -601,8 +601,8 @@ int LogQuerier::RestorePersistJobs(HilogBuffer& _buffer) info.msg.compressAlg, SLEEP_TIME, *rotator, _buffer); persister->SetRestore(true); - int persisterRes = persister->Init(); int rotatorRes = rotator->Init(); + int persisterRes = persister->Init(); persister->queryCondition.types = info.types; persister->queryCondition.levels = info.levels; if (persisterRes == RET_FAIL || rotatorRes == RET_FAIL) { -- Gitee From 9b6c5f0085c8f623be67ffaac8dda50254f9da5e Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Mon, 16 Aug 2021 10:48:24 +0800 Subject: [PATCH 2/9] Move info file operations into rotator Signed-off-by: Wu Shangwei <2826256824@qq.com> Change-Id: Ifadf0628d5afd0405d3a6e471360281f3fac1208 Signed-off-by: Wu Shangwei <2826256824@qq.com> --- services/hilogd/include/log_persister.h | 10 ---- .../hilogd/include/log_persister_rotator.h | 15 +++++ services/hilogd/log_persister.cpp | 32 +--------- services/hilogd/log_persister_rotator.cpp | 59 +++++++++++++++---- services/hilogd/log_querier.cpp | 3 +- 5 files changed, 66 insertions(+), 53 deletions(-) diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index 8af781c..076048b 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -31,13 +31,6 @@ namespace OHOS { namespace HiviewDFX { using namespace std; -typedef struct { - uint8_t index; - uint16_t types; - uint8_t levels; - LogPersistStartMsg msg; -} PersistRecoveryInfo; - class LogPersister : public LogReader { public: LogPersister(uint32_t id, std::string path, uint32_t fileSize, uint16_t compressAlg, int sleepTime, @@ -59,7 +52,6 @@ public: bool writeUnCompressedBuffer(HilogData *data); uint8_t GetType() const; std::string getPath(); - int SaveInfo(LogPersistStartMsg& pMsg); void SetRestore(bool flag); LogPersisterBuffer *buffer; LogPersisterBuffer *compressBuffer; @@ -79,12 +71,10 @@ private: bool hasExited; inline void WriteFile(); bool isExited(); - FILE *fdinfo = nullptr; int fd = -1; LogCompress *compressor; list persistList; uint32_t plainLogSize; - PersistRecoveryInfo info; bool restore = false; }; diff --git a/services/hilogd/include/log_persister_rotator.h b/services/hilogd/include/log_persister_rotator.h index a6ee48a..cd345f3 100644 --- a/services/hilogd/include/log_persister_rotator.h +++ b/services/hilogd/include/log_persister_rotator.h @@ -17,8 +17,17 @@ #include #include #include "hilog_common.h" +#include "hilogtool_msg.h" +#include "log_buffer.h" namespace OHOS { namespace HiviewDFX { +typedef struct { + uint8_t index; + uint16_t types; + uint8_t levels; + LogPersistStartMsg msg; +} PersistRecoveryInfo; + const std::string ANXILLARY_FILE_NAME = "persisterInfo_"; class LogPersisterRotator { public: @@ -30,6 +39,10 @@ public: void FinishInput(); void SetIndex(int pIndex); void SetId(uint32_t pId); + void OpenInfoFile(); + void UpdateRotateNumber(); + int SaveInfo(LogPersistStartMsg& pMsg, QueryCondition queryCondition); + void WriteRecoveryInfo(); protected: void InternalRotate(); uint32_t fileNum; @@ -43,6 +56,8 @@ private: bool needRotate = false; FILE* fdinfo; uint32_t id = 0; + std::string infoPath; + PersistRecoveryInfo info; }; } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index d656e06..3b26814 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -58,7 +58,6 @@ LogPersister::LogPersister(uint32_t id, string path, uint32_t fileSize, uint16_t hasExited = false; hilogBuffer = &_buffer; compressor = nullptr; - fdinfo = nullptr; buffer = nullptr; plainLogSize = 0; } @@ -137,14 +136,6 @@ int LogPersister::Init() #endif return RET_FAIL; } - fdinfo = fopen((mmapPath + ".info").c_str(), "a+"); - if (fdinfo == nullptr) { -#ifdef DEBUG - cout << "open loginfo file failed: " << strerror(errno) << endl; -#endif - close(fd); - return RET_FAIL; - } buffer = (LogPersisterBuffer *)mmap(nullptr, sizeof(LogPersisterBuffer), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); close(fd); @@ -152,7 +143,6 @@ int LogPersister::Init() #ifdef DEBUG cout << "mmap file failed: " << strerror(errno) << endl; #endif - fclose(fdinfo); return RET_FAIL; } if (restore == true) { @@ -274,13 +264,6 @@ int LogPersister::WriteData(HilogData *data) void LogPersister::Start() { - if (!restore) { - std::cout << "Save Info file!" << std::endl; - fseek(fdinfo, 0, SEEK_SET); - fwrite(&info, sizeof(PersistRecoveryInfo), 1, fdinfo); - fsync(fileno(fdinfo)); - } - fclose(fdinfo); auto newThread = thread(&LogPersister::ThreadFunc, static_pointer_cast(shared_from_this())); newThread.detach(); @@ -384,6 +367,7 @@ bool LogPersister::isExited() void LogPersister::Exit() { + std::cout << "LogPersister Exit!" << std::endl; toExit = true; condVariable.notify_all(); unique_lock lk(mutexForhasExited); @@ -395,7 +379,6 @@ void LogPersister::Exit() munmap(buffer, MAX_PERSISTER_BUFFER_SIZE); cout << "removed mmap file" << endl; remove(mmapPath.c_str()); - remove((mmapPath + ".info").c_str()); return; } bool LogPersister::Identify(uint32_t id) @@ -413,18 +396,7 @@ uint8_t LogPersister::GetType() const return TYPE_PERSISTER; } -int LogPersister::SaveInfo(LogPersistStartMsg& pMsg) -{ - info.msg = pMsg; - info.types = queryCondition.types; - info.levels = queryCondition.levels; - if (strcpy_s(info.msg.filePath, FILE_PATH_MAX_LEN, pMsg.filePath) != 0) { - cout << "Failed to save persister file path" << endl; - return RET_FAIL; - } - cout << "Saved Path=" << info.msg.filePath << endl; - return RET_SUCCESS; -} + void LogPersister::SetRestore(bool flag) { diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index f0d6a82..e80c5fb 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -20,6 +20,7 @@ #include #include #include +#include namespace OHOS { namespace HiviewDFX { @@ -37,19 +38,12 @@ LogPersisterRotator::~LogPersisterRotator() if (fdinfo != nullptr) { fclose(fdinfo); } + remove(infoPath.c_str()); } int LogPersisterRotator::Init() { - int nPos = fileName.find_last_of('/'); - std::string mmapPath = fileName.substr(0, nPos) + "/." + ANXILLARY_FILE_NAME + to_string(id); - if (access(fileName.substr(0, nPos).c_str(), F_OK) != 0) { - if (errno == ENOENT) { - mkdir(fileName.substr(0, nPos).c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO); - } - } - fdinfo = fopen((mmapPath + ".info").c_str(), "r+"); - if (fdinfo == nullptr) fdinfo = fopen((mmapPath + ".info").c_str(), "w+"); + OpenInfoFile(); if (fdinfo == nullptr) return RET_FAIL; return RET_SUCCESS; } @@ -102,9 +96,7 @@ void LogPersisterRotator::Rotate() cout << "THE FILE NAME !!!!!!! " << ss.str() << endl; output.open(ss.str(), ios::out | ios::trunc); } - fseek(fdinfo, 0, SEEK_SET); - fwrite(&index, sizeof(uint8_t), 1, fdinfo); - fsync(fileno(fdinfo)); + UpdateRotateNumber(); } void LogPersisterRotator::FillInfo(uint32_t *size, uint32_t *num) @@ -127,5 +119,48 @@ void LogPersisterRotator::SetId(uint32_t pId) { id = pId; } + +void LogPersisterRotator::OpenInfoFile() +{ + int nPos = fileName.find_last_of('/'); + std::string mmapPath = fileName.substr(0, nPos) + "/." + ANXILLARY_FILE_NAME + to_string(id); + if (access(fileName.substr(0, nPos).c_str(), F_OK) != 0) { + if (errno == ENOENT) { + mkdir(fileName.substr(0, nPos).c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO); + } + } + infoPath = mmapPath + ".info"; + fdinfo = fopen(infoPath.c_str(), "a+"); +} + +void LogPersisterRotator::UpdateRotateNumber() +{ + fseek(fdinfo, 0, SEEK_SET); + fwrite(&index, sizeof(uint8_t), 1, fdinfo); + fflush(fdinfo); + fsync(fileno(fdinfo)); +} + +int LogPersisterRotator::SaveInfo(LogPersistStartMsg& pMsg, QueryCondition queryCondition) +{ + info.msg = pMsg; + info.types = queryCondition.types; + info.levels = queryCondition.levels; + if (strcpy_s(info.msg.filePath, FILE_PATH_MAX_LEN, pMsg.filePath) != 0) { + cout << "Failed to save persister file path" << endl; + return RET_FAIL; + } + cout << "Saved Path=" << info.msg.filePath << endl; + return RET_SUCCESS; +} + +void LogPersisterRotator::WriteRecoveryInfo() +{ + std::cout << "Save Info file!" << std::endl; + fseek(fdinfo, 0, SEEK_SET); + fwrite(&info, sizeof(PersistRecoveryInfo), 1, fdinfo); + fflush(fdinfo); + fsync(fileno(fdinfo)); +} } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index f66e19c..1f90679 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -142,14 +142,15 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade SLEEP_TIME, *rotator, buffer); persister->queryCondition.types = pLogPersistStartMsg->logType; persister->queryCondition.levels = DEFAULT_LOG_LEVEL; - int saveInfoRes = persister->SaveInfo(*pLogPersistStartMsg); pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; int rotatorRes = rotator->Init(); + int saveInfoRes = rotator->SaveInfo(*pLogPersistStartMsg, persister->queryCondition); pLogPersistStartRst->result = persister->Init(); if (pLogPersistStartRst->result == RET_FAIL || saveInfoRes == RET_FAIL || rotatorRes == RET_FAIL) { cout << "LogPersister failed to initialize!" << endl; persister.reset(); } else { + rotator->WriteRecoveryInfo(); persister->Start(); buffer.AddLogReader(weak_ptr(persister)); } -- Gitee From b6653d7eddb17583d5e39f46715cc800af74e93a Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Mon, 16 Aug 2021 13:52:14 +0800 Subject: [PATCH 3/9] LogPersister & LogPersisterRotator: Use fopen(); Deteremine restore from LogQuerier Signed-off-by: Wu Shangwei <2826256824@qq.com> Change-Id: Idb4a2f848ed834a5a6d2183daf2578b4e7b8d881 Signed-off-by: Wu Shangwei <2826256824@qq.com> --- services/hilogd/include/log_persister.h | 2 +- .../hilogd/include/log_persister_rotator.h | 4 ++- services/hilogd/log_persister.cpp | 26 +++++++------------ services/hilogd/log_persister_rotator.cpp | 11 +++++++- services/hilogd/log_querier.cpp | 3 ++- 5 files changed, 26 insertions(+), 20 deletions(-) diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index 076048b..dac83b4 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -71,7 +71,7 @@ private: bool hasExited; inline void WriteFile(); bool isExited(); - int fd = -1; + FILE* fd = nullptr; LogCompress *compressor; list persistList; uint32_t plainLogSize; diff --git a/services/hilogd/include/log_persister_rotator.h b/services/hilogd/include/log_persister_rotator.h index cd345f3..994cd65 100644 --- a/services/hilogd/include/log_persister_rotator.h +++ b/services/hilogd/include/log_persister_rotator.h @@ -43,6 +43,7 @@ public: void UpdateRotateNumber(); int SaveInfo(LogPersistStartMsg& pMsg, QueryCondition queryCondition); void WriteRecoveryInfo(); + void SetRestore(bool flag); protected: void InternalRotate(); uint32_t fileNum; @@ -54,10 +55,11 @@ protected: private: void Rotate(); bool needRotate = false; - FILE* fdinfo; + FILE* fdinfo = nullptr; uint32_t id = 0; std::string infoPath; PersistRecoveryInfo info; + bool restore = false; }; } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 3b26814..9231e6b 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -117,28 +117,24 @@ int LogPersister::Init() if (InitCompress() == RET_FAIL) { return RET_FAIL; } - fd = open(mmapPath.c_str(), O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); - if (fd <= 0) { - if (errno == EEXIST) { - cout << "File already exists!" << endl; - fd = open(mmapPath.c_str(), O_RDWR, 0); - } + if (restore) { + fd = fopen(mmapPath.c_str(), "r+"); } else { -#ifdef DEBUG - cout << "New log file: " << mmapPath << endl; -#endif - lseek(fd, sizeof(LogPersisterBuffer) - 1, SEEK_SET); - write(fd, "", 1); + fd = fopen(mmapPath.c_str(), "w+"); + ftruncate(fileno(fd), sizeof(LogPersisterBuffer)); + fflush(fd); + fsync(fileno(fd)); } - if (fd < 0) { + + if (fd == nullptr) { #ifdef DEBUG cout << "open log file(" << mmapPath << ") failed: " << strerror(errno) << endl; #endif return RET_FAIL; } buffer = (LogPersisterBuffer *)mmap(nullptr, sizeof(LogPersisterBuffer), PROT_READ | PROT_WRITE, - MAP_SHARED, fd, 0); - close(fd); + MAP_SHARED, fileno(fd), 0); + fclose(fd); if (buffer == MAP_FAILED) { #ifdef DEBUG cout << "mmap file failed: " << strerror(errno) << endl; @@ -396,8 +392,6 @@ uint8_t LogPersister::GetType() const return TYPE_PERSISTER; } - - void LogPersister::SetRestore(bool flag) { restore = flag; diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index e80c5fb..b39d935 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -130,7 +130,11 @@ void LogPersisterRotator::OpenInfoFile() } } infoPath = mmapPath + ".info"; - fdinfo = fopen(infoPath.c_str(), "a+"); + if (restore) { + fdinfo = fopen(infoPath.c_str(), "r+"); + } else { + fdinfo = fopen(infoPath.c_str(), "w+"); + } } void LogPersisterRotator::UpdateRotateNumber() @@ -162,5 +166,10 @@ void LogPersisterRotator::WriteRecoveryInfo() fflush(fdinfo); fsync(fileno(fdinfo)); } + +void LogPersisterRotator::SetRestore(bool flag) +{ + restore = flag; +} } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 1f90679..f61b4fd 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -590,7 +590,7 @@ int LogQuerier::RestorePersistJobs(HilogBuffer& _buffer) PersistRecoveryInfo info; fread(&info, sizeof(PersistRecoveryInfo), 1, infile); fclose(infile); - LogPersisterRotator* rotator = rotator = MakeRotator(info.msg); + LogPersisterRotator* rotator = MakeRotator(info.msg); rotator->SetIndex(info.index + 1); rotator->SetId(info.msg.jobId); printf("Recovery Info:\njobId=%u\nfilePath=%s\n", @@ -602,6 +602,7 @@ int LogQuerier::RestorePersistJobs(HilogBuffer& _buffer) info.msg.compressAlg, SLEEP_TIME, *rotator, _buffer); persister->SetRestore(true); + rotator->SetRestore(true); int rotatorRes = rotator->Init(); int persisterRes = persister->Init(); persister->queryCondition.types = info.types; -- Gitee From 849d57d0418867d1641cd21cb4bbcb7171d69d3e Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Thu, 19 Aug 2021 18:20:46 +0800 Subject: [PATCH 4/9] Persister and rotator use the same restore flag Signed-off-by: Wu Shangwei <2826256824@qq.com> Change-Id: Id59b43a41cd83586cc969dda2ceda6ae68c13827 --- services/hilogd/include/log_persister.h | 2 -- services/hilogd/include/log_persister_rotator.h | 1 + services/hilogd/log_persister.cpp | 6 +----- services/hilogd/log_persister_rotator.cpp | 10 +++++++--- services/hilogd/log_querier.cpp | 5 ++--- 5 files changed, 11 insertions(+), 13 deletions(-) diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index dac83b4..b8fcd42 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -52,7 +52,6 @@ public: bool writeUnCompressedBuffer(HilogData *data); uint8_t GetType() const; std::string getPath(); - void SetRestore(bool flag); LogPersisterBuffer *buffer; LogPersisterBuffer *compressBuffer; private: @@ -75,7 +74,6 @@ private: LogCompress *compressor; list persistList; uint32_t plainLogSize; - bool restore = false; }; int GenPersistLogHeader(HilogData *data, list& persistList); diff --git a/services/hilogd/include/log_persister_rotator.h b/services/hilogd/include/log_persister_rotator.h index 994cd65..fadc924 100644 --- a/services/hilogd/include/log_persister_rotator.h +++ b/services/hilogd/include/log_persister_rotator.h @@ -44,6 +44,7 @@ public: int SaveInfo(LogPersistStartMsg& pMsg, QueryCondition queryCondition); void WriteRecoveryInfo(); void SetRestore(bool flag); + bool GetRestore(); protected: void InternalRotate(); uint32_t fileNum; diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 9231e6b..83e3056 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -93,6 +93,7 @@ int LogPersister::InitCompress() int LogPersister::Init() { + bool restore = rotator.GetRestore(); int nPos = path.find_last_of('/'); if (nPos == RET_FAIL) { return RET_FAIL; @@ -391,10 +392,5 @@ uint8_t LogPersister::GetType() const { return TYPE_PERSISTER; } - -void LogPersister::SetRestore(bool flag) -{ - restore = flag; -} } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index b39d935..ec0d740 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -13,14 +13,13 @@ * limitations under the License. */ #include "log_persister_rotator.h" - +#include +#include #include #include #include -#include #include #include -#include namespace OHOS { namespace HiviewDFX { @@ -171,5 +170,10 @@ void LogPersisterRotator::SetRestore(bool flag) { restore = flag; } + +void LogPersisterRotator::GetRestore() +{ + return restore; +} } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index f61b4fd..055e9d6 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -601,12 +601,11 @@ int LogQuerier::RestorePersistJobs(HilogBuffer& _buffer) info.msg.fileSize, info.msg.compressAlg, SLEEP_TIME, *rotator, _buffer); - persister->SetRestore(true); rotator->SetRestore(true); - int rotatorRes = rotator->Init(); - int persisterRes = persister->Init(); persister->queryCondition.types = info.types; persister->queryCondition.levels = info.levels; + int rotatorRes = rotator->Init(); + int persisterRes = persister->Init(); if (persisterRes == RET_FAIL || rotatorRes == RET_FAIL) { cout << "LogPersister failed to initialize!" << endl; persister.reset(); -- Gitee From f13b35c06dbc2e8c6a5607952ae7fb66a1e3e25d Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Thu, 19 Aug 2021 19:16:54 +0800 Subject: [PATCH 5/9] Add PersistJobLauncher Signed-off-by: Wu Shangwei <2826256824@qq.com> Change-Id: I4eb01a28d3a4131d015ece4c510856343d2e2afc --- services/hilogd/log_persister.cpp | 2 +- services/hilogd/log_persister_rotator.cpp | 2 +- services/hilogd/log_querier.cpp | 73 +++++++++-------------- 3 files changed, 31 insertions(+), 46 deletions(-) diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 83e3056..259b552 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -93,7 +93,7 @@ int LogPersister::InitCompress() int LogPersister::Init() { - bool restore = rotator.GetRestore(); + bool restore = rotator->GetRestore(); int nPos = path.find_last_of('/'); if (nPos == RET_FAIL) { return RET_FAIL; diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index ec0d740..c6d395d 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -171,7 +171,7 @@ void LogPersisterRotator::SetRestore(bool flag) restore = flag; } -void LogPersisterRotator::GetRestore() +bool LogPersisterRotator::GetRestore() { return restore; } diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 055e9d6..8dfe088 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -82,6 +82,33 @@ LogPersisterRotator* MakeRotator(LogPersistStartMsg& pLogPersistStartMsg) fileSuffix); } +void PersistJobLauncher(LogPersistStartMsg& pLogPersistStartMsg, HilogBuffer& buffer, bool restore = false, int index = -1) +{ + LogPersisterRotator* rotator = MakeRotator(pLogPersistStartMsg); + rotator->SetId(pLogPersistStartMsg.jobId); + rotator->SetIndex(index); + std::shared_ptr persister = make_shared( + pLogPersistStartMsg.jobId, + pLogPersistStartMsg.filePath, + pLogPersistStartMsg.fileSize, + pLogPersistStartMsg.compressAlg, + SLEEP_TIME, *rotator, buffer); + persister->queryCondition.types = pLogPersistStartMsg.logType; + persister->queryCondition.levels = DEFAULT_LOG_LEVEL; + rotator->SetRestore(restore); + int rotatorRes = rotator->Init(); + int saveInfoRes = rotator->SaveInfo(pLogPersistStartMsg, persister->queryCondition); + int persistRes = persister->Init(); + if (persistRes == RET_FAIL || saveInfoRes == RET_FAIL || rotatorRes == RET_FAIL) { + cout << "LogPersister failed to initialize!" << endl; + persister.reset(); + } else { + if (!restore) rotator->WriteRecoveryInfo(); + persister->Start(); + buffer.AddLogReader(weak_ptr(persister)); + } +} + void HandleLogQueryRequest(std::shared_ptr logReader, HilogBuffer& buffer) { logReader->SetCmd(LOG_QUERY_RESPONSE); @@ -99,7 +126,6 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade { char msgToSend[MAX_DATA_LEN]; const uint16_t sendMsgLen = sizeof(LogPersistStartResult); - LogPersisterRotator *rotator = nullptr; LogPersistStartRequest* pLogPersistStartReq = reinterpret_cast(reqMsg); LogPersistStartMsg* pLogPersistStartMsg @@ -132,28 +158,7 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade return; } strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersisterPath.c_str()); - rotator = MakeRotator(*pLogPersistStartMsg); - rotator->SetId(pLogPersistStartMsg->jobId); - std::shared_ptr persister = make_shared( - pLogPersistStartMsg->jobId, - pLogPersistStartMsg->filePath, - pLogPersistStartMsg->fileSize, - pLogPersistStartMsg->compressAlg, - SLEEP_TIME, *rotator, buffer); - persister->queryCondition.types = pLogPersistStartMsg->logType; - persister->queryCondition.levels = DEFAULT_LOG_LEVEL; - pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; - int rotatorRes = rotator->Init(); - int saveInfoRes = rotator->SaveInfo(*pLogPersistStartMsg, persister->queryCondition); - pLogPersistStartRst->result = persister->Init(); - if (pLogPersistStartRst->result == RET_FAIL || saveInfoRes == RET_FAIL || rotatorRes == RET_FAIL) { - cout << "LogPersister failed to initialize!" << endl; - persister.reset(); - } else { - rotator->WriteRecoveryInfo(); - persister->Start(); - buffer.AddLogReader(weak_ptr(persister)); - } + PersistJobLauncher(*pLogPersistStartMsg, buffer); SetMsgHead(&pLogPersistStartRsp->msgHeader, MC_RSP_LOG_PERSIST_START, sendMsgLen); logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); } @@ -590,29 +595,9 @@ int LogQuerier::RestorePersistJobs(HilogBuffer& _buffer) PersistRecoveryInfo info; fread(&info, sizeof(PersistRecoveryInfo), 1, infile); fclose(infile); - LogPersisterRotator* rotator = MakeRotator(info.msg); - rotator->SetIndex(info.index + 1); - rotator->SetId(info.msg.jobId); + PersistJobLauncher(info.msg, _buffer, true, info.index + 1); printf("Recovery Info:\njobId=%u\nfilePath=%s\n", info.msg.jobId, info.msg.filePath); - std::shared_ptr persister = make_shared( - info.msg.jobId, - info.msg.filePath, - info.msg.fileSize, - info.msg.compressAlg, - SLEEP_TIME, *rotator, _buffer); - rotator->SetRestore(true); - persister->queryCondition.types = info.types; - persister->queryCondition.levels = info.levels; - int rotatorRes = rotator->Init(); - int persisterRes = persister->Init(); - if (persisterRes == RET_FAIL || rotatorRes == RET_FAIL) { - cout << "LogPersister failed to initialize!" << endl; - persister.reset(); - } else { - persister->Start(); - _buffer.AddLogReader(weak_ptr(persister)); - } } } closedir (dir); -- Gitee From e7242688f5a03790bf8c0f2c3701705f36766f83 Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Sun, 22 Aug 2021 23:31:49 +0800 Subject: [PATCH 6/9] Check CRC32 of recovery info Signed-off-by: Wu Shangwei <2826256824@qq.com> --- services/hilogd/log_persister_rotator.cpp | 10 ++++++---- services/hilogd/log_querier.cpp | 8 ++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index c6d395d..e4c7a04 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -20,6 +20,7 @@ #include #include #include +#include namespace OHOS { namespace HiviewDFX { @@ -138,10 +139,8 @@ void LogPersisterRotator::OpenInfoFile() void LogPersisterRotator::UpdateRotateNumber() { - fseek(fdinfo, 0, SEEK_SET); - fwrite(&index, sizeof(uint8_t), 1, fdinfo); - fflush(fdinfo); - fsync(fileno(fdinfo)); + info.index = index; + WriteRecoveryInfo(); } int LogPersisterRotator::SaveInfo(LogPersistStartMsg& pMsg, QueryCondition queryCondition) @@ -160,8 +159,11 @@ int LogPersisterRotator::SaveInfo(LogPersistStartMsg& pMsg, QueryCondition query void LogPersisterRotator::WriteRecoveryInfo() { std::cout << "Save Info file!" << std::endl; + uLong crc = crc32(0L, Z_NULL, 0); + crc = crc32(crc, (Bytef*)(&info), sizeof(PersistRecoveryInfo)); fseek(fdinfo, 0, SEEK_SET); fwrite(&info, sizeof(PersistRecoveryInfo), 1, fdinfo); + fwrite(&crc, sizeof(uLong), 1, fdinfo); fflush(fdinfo); fsync(fileno(fdinfo)); } diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 8dfe088..6ebc099 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -594,7 +594,15 @@ int LogQuerier::RestorePersistJobs(HilogBuffer& _buffer) } PersistRecoveryInfo info; fread(&info, sizeof(PersistRecoveryInfo), 1, infile); + uLong crcSum = 0L; + fread(&crcSum, sizeof(uLong), 1, infile); fclose(infile); + uLong crc = crc32(0L, Z_NULL, 0); + crc = crc32(crc, (Bytef*)(&info), sizeof(PersistRecoveryInfo)); + if (crc != crcSum) { + printf("Info file CRC Checksum Failed! %lu vs %lu\n", crc, crcSum); + continue; + } PersistJobLauncher(info.msg, _buffer, true, info.index + 1); printf("Recovery Info:\njobId=%u\nfilePath=%s\n", info.msg.jobId, info.msg.filePath); -- Gitee From 2d50e30abae872d2675c50e6d0f59e0b7a9f6b66 Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Mon, 23 Aug 2021 17:02:51 +0800 Subject: [PATCH 7/9] GetInfoCRC32 function Signed-off-by: Wu Shangwei <2826256824@qq.com> --- services/hilogd/include/log_persister_rotator.h | 2 ++ services/hilogd/log_persister_rotator.cpp | 11 ++++++++--- services/hilogd/log_querier.cpp | 10 +++++----- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/services/hilogd/include/log_persister_rotator.h b/services/hilogd/include/log_persister_rotator.h index fadc924..6fcb9bb 100644 --- a/services/hilogd/include/log_persister_rotator.h +++ b/services/hilogd/include/log_persister_rotator.h @@ -16,6 +16,7 @@ #define _HILOG_PERSISTER_ROTATOR_H #include #include +#include #include "hilog_common.h" #include "hilogtool_msg.h" #include "log_buffer.h" @@ -29,6 +30,7 @@ typedef struct { } PersistRecoveryInfo; const std::string ANXILLARY_FILE_NAME = "persisterInfo_"; +uLong GetInfoCRC32(PersistRecoveryInfo &info); class LogPersisterRotator { public: LogPersisterRotator(std::string path, uint32_t fileSize, uint32_t fileNum, std::string suffix = ""); diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index 7a6f410..2f84962 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -20,12 +20,18 @@ #include #include #include -#include namespace OHOS { namespace HiviewDFX { using namespace std; +uLong GetInfoCRC32(PersistRecoveryInfo &info) +{ + uLong crc = crc32(0L, Z_NULL, 0); + crc = crc32(crc, (Bytef*)(&info), sizeof(PersistRecoveryInfo)); + return crc; +} + LogPersisterRotator::LogPersisterRotator(string path, uint32_t fileSize, uint32_t fileNum, string suffix) : fileNum(fileNum), fileSize(fileSize), fileName(path), fileSuffix(suffix) { @@ -159,8 +165,7 @@ int LogPersisterRotator::SaveInfo(LogPersistStartMsg& pMsg, QueryCondition query void LogPersisterRotator::WriteRecoveryInfo() { std::cout << "Save Info file!" << std::endl; - uLong crc = crc32(0L, Z_NULL, 0); - crc = crc32(crc, (Bytef*)(&info), sizeof(PersistRecoveryInfo)); + uLong crc = GetInfoCRC32(info); fseek(fdinfo, 0, SEEK_SET); fwrite(&info, sizeof(PersistRecoveryInfo), 1, fdinfo); fwrite(&crc, sizeof(uLong), 1, fdinfo); diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index a7096f9..5b13842 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -603,15 +603,15 @@ int LogQuerier::RestorePersistJobs(HilogBuffer& _buffer) uLong crcSum = 0L; fread(&crcSum, sizeof(uLong), 1, infile); fclose(infile); - uLong crc = crc32(0L, Z_NULL, 0); - crc = crc32(crc, (Bytef*)(&info), sizeof(PersistRecoveryInfo)); + uLong crc = GetInfoCRC32(info); if (crc != crcSum) { - printf("Info file CRC Checksum Failed! %lu vs %lu\n", crc, crcSum); + std::cout << "Info file CRC Checksum Failed!" << std::endl; continue; } PersistJobLauncher(info.msg, _buffer, true, info.index + 1); - printf("Recovery Info:\njobId=%u\nfilePath=%s\n", - info.msg.jobId, info.msg.filePath); + std::cout << "Recovery Info:" << std::endl << + "jobId=" << (unsigned)(info.msg.jobId) << std::endl << + "filePath=" << (unsigned)(info.msg.filePath) << std::endl; } } closedir (dir); -- Gitee From 918c58cb3b8bc2d5bd4c397e1bc303c63b6bc1af Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Tue, 24 Aug 2021 14:26:13 +0800 Subject: [PATCH 8/9] Use const parameter Signed-off-by: Wu Shangwei <2826256824@qq.com> --- .../hilogd/include/log_persister_rotator.h | 4 +-- services/hilogd/log_persister.cpp | 1 + services/hilogd/log_persister_rotator.cpp | 14 +++++----- services/hilogd/log_querier.cpp | 27 +++++++++---------- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/services/hilogd/include/log_persister_rotator.h b/services/hilogd/include/log_persister_rotator.h index 6fcb9bb..eda4ec9 100644 --- a/services/hilogd/include/log_persister_rotator.h +++ b/services/hilogd/include/log_persister_rotator.h @@ -30,7 +30,7 @@ typedef struct { } PersistRecoveryInfo; const std::string ANXILLARY_FILE_NAME = "persisterInfo_"; -uLong GetInfoCRC32(PersistRecoveryInfo &info); +uLong GetInfoCRC32(const PersistRecoveryInfo &info); class LogPersisterRotator { public: LogPersisterRotator(std::string path, uint32_t fileSize, uint32_t fileNum, std::string suffix = ""); @@ -43,7 +43,7 @@ public: void SetId(uint32_t pId); void OpenInfoFile(); void UpdateRotateNumber(); - int SaveInfo(LogPersistStartMsg& pMsg, QueryCondition queryCondition); + int SaveInfo(const LogPersistStartMsg& pMsg, const QueryCondition queryCondition); void WriteRecoveryInfo(); void SetRestore(bool flag); bool GetRestore(); diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 2335230..9a571dc 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -261,6 +261,7 @@ int LogPersister::WriteData(HilogData *data) void LogPersister::Start() { + hilogBuffer->AddLogReader(weak_from_this()); auto newThread = thread(&LogPersister::ThreadFunc, static_pointer_cast(shared_from_this())); newThread.detach(); diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index 2f84962..407172f 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -13,19 +13,19 @@ * limitations under the License. */ #include "log_persister_rotator.h" -#include +#include +#include +#include #include -#include #include -#include -#include -#include +#include +#include namespace OHOS { namespace HiviewDFX { using namespace std; -uLong GetInfoCRC32(PersistRecoveryInfo &info) +uLong GetInfoCRC32(const PersistRecoveryInfo &info) { uLong crc = crc32(0L, Z_NULL, 0); crc = crc32(crc, (Bytef*)(&info), sizeof(PersistRecoveryInfo)); @@ -149,7 +149,7 @@ void LogPersisterRotator::UpdateRotateNumber() WriteRecoveryInfo(); } -int LogPersisterRotator::SaveInfo(LogPersistStartMsg& pMsg, QueryCondition queryCondition) +int LogPersisterRotator::SaveInfo(const LogPersistStartMsg& pMsg, const QueryCondition queryCondition) { info.msg = pMsg; info.types = queryCondition.types; diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 5b13842..ee7c398 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -62,7 +62,7 @@ inline bool IsValidFileName(const std::string& strFileName) bool bValid = !std::regex_search(strFileName, regExpress); return bValid; } -LogPersisterRotator* MakeRotator(LogPersistStartMsg& pLogPersistStartMsg) +LogPersisterRotator* MakeRotator(const LogPersistStartMsg& pLogPersistStartMsg) { string fileSuffix = ""; switch (pLogPersistStartMsg.compressAlg) { @@ -82,22 +82,22 @@ LogPersisterRotator* MakeRotator(LogPersistStartMsg& pLogPersistStartMsg) fileSuffix); } -void PersistJobLauncher(LogPersistStartMsg& pLogPersistStartMsg, HilogBuffer& buffer, bool restore = false, int index = -1) +void JobLauncher(const LogPersistStartMsg& pMsg, const HilogBuffer& buffer, bool restore = false, int index = -1) { - LogPersisterRotator* rotator = MakeRotator(pLogPersistStartMsg); - rotator->SetId(pLogPersistStartMsg.jobId); + LogPersisterRotator* rotator = MakeRotator(pMsg); + rotator->SetId(pMsg.jobId); rotator->SetIndex(index); std::shared_ptr persister = make_shared( - pLogPersistStartMsg.jobId, - pLogPersistStartMsg.filePath, - pLogPersistStartMsg.fileSize, - pLogPersistStartMsg.compressAlg, - SLEEP_TIME, *rotator, buffer); - persister->queryCondition.types = pLogPersistStartMsg.logType; + pMsg.jobId, + pMsg.filePath, + pMsg.fileSize, + pMsg.compressAlg, + SLEEP_TIME, *rotator, const_cast(buffer)); + persister->queryCondition.types = pMsg.logType; persister->queryCondition.levels = DEFAULT_LOG_LEVEL; rotator->SetRestore(restore); int rotatorRes = rotator->Init(); - int saveInfoRes = rotator->SaveInfo(pLogPersistStartMsg, persister->queryCondition); + int saveInfoRes = rotator->SaveInfo(pMsg, persister->queryCondition); int persistRes = persister->Init(); if (persistRes == RET_FAIL || saveInfoRes == RET_FAIL || rotatorRes == RET_FAIL) { cout << "LogPersister failed to initialize!" << endl; @@ -105,7 +105,6 @@ void PersistJobLauncher(LogPersistStartMsg& pLogPersistStartMsg, HilogBuffer& bu } else { if (!restore) rotator->WriteRecoveryInfo(); persister->Start(); - buffer.AddLogReader(weak_ptr(persister)); } } @@ -164,7 +163,7 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade return; } strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersisterPath.c_str()); - PersistJobLauncher(*pLogPersistStartMsg, buffer); + JobLauncher(*pLogPersistStartMsg, buffer); SetMsgHead(&pLogPersistStartRsp->msgHeader, MC_RSP_LOG_PERSIST_START, sendMsgLen); logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); } @@ -608,7 +607,7 @@ int LogQuerier::RestorePersistJobs(HilogBuffer& _buffer) std::cout << "Info file CRC Checksum Failed!" << std::endl; continue; } - PersistJobLauncher(info.msg, _buffer, true, info.index + 1); + JobLauncher(info.msg, _buffer, true, info.index + 1); std::cout << "Recovery Info:" << std::endl << "jobId=" << (unsigned)(info.msg.jobId) << std::endl << "filePath=" << (unsigned)(info.msg.filePath) << std::endl; -- Gitee From 113198d2781a970f36c108f7f2df8cb499913baf Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Tue, 24 Aug 2021 16:04:56 +0800 Subject: [PATCH 9/9] Bugfix: persist start result is not returned to hilogtool correctly Signed-off-by: Wu Shangwei <2826256824@qq.com> --- services/hilogd/log_querier.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index ee7c398..cdd88d9 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -82,7 +82,7 @@ LogPersisterRotator* MakeRotator(const LogPersistStartMsg& pLogPersistStartMsg) fileSuffix); } -void JobLauncher(const LogPersistStartMsg& pMsg, const HilogBuffer& buffer, bool restore = false, int index = -1) +int JobLauncher(const LogPersistStartMsg& pMsg, const HilogBuffer& buffer, bool restore = false, int index = -1) { LogPersisterRotator* rotator = MakeRotator(pMsg); rotator->SetId(pMsg.jobId); @@ -102,9 +102,11 @@ void JobLauncher(const LogPersistStartMsg& pMsg, const HilogBuffer& buffer, bool if (persistRes == RET_FAIL || saveInfoRes == RET_FAIL || rotatorRes == RET_FAIL) { cout << "LogPersister failed to initialize!" << endl; persister.reset(); + return RET_FAIL; } else { if (!restore) rotator->WriteRecoveryInfo(); persister->Start(); + return RET_SUCCESS; } } @@ -163,7 +165,8 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade return; } strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersisterPath.c_str()); - JobLauncher(*pLogPersistStartMsg, buffer); + pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; + pLogPersistStartRst->result = JobLauncher(*pLogPersistStartMsg, buffer); SetMsgHead(&pLogPersistStartRsp->msgHeader, MC_RSP_LOG_PERSIST_START, sendMsgLen); logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); } -- Gitee