From 9396905fb6dac38eeb7ec1319d1e8444b240914b Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 6 Jul 2021 07:03:03 +0000 Subject: [PATCH 01/24] add compress buffer Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/include/log_persister.h | 4 ++-- services/hilogd/log_buffer.cpp | 2 -- services/hilogd/log_persister.cpp | 32 ++++++++++++++++--------- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index 74dd580..06a7d50 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -35,7 +35,7 @@ typedef struct { } LogPersisterBuffer; const uint16_t MAX_PERSISTER_BUFFER_SIZE = 4096; - +const uint32_t COMPRESS_BUFFER_SIZE = 4 * 1024; class LogPersister : public LogReader { public: LogPersister(uint32_t id, std::string path, uint16_t compressType, @@ -59,7 +59,6 @@ public: uint8_t getType() const; std::string getPath(); LogPersisterBuffer *buffer; - private: uint32_t id; std::string path; @@ -79,6 +78,7 @@ private: FILE *fdinfo; int fd = -1; LogCompress *LogCompress; + LogPersisterBuffer *compressBuffer; list persistList; }; diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 7b0180a..3e04ab8 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -335,11 +335,9 @@ bool HilogBuffer::ConditionMatch(std::shared_ptr reader) * strict mode: 0xdxxxxxx (full) * fuzzy mode: 0xdxxxx (using last 2 digits of full domain as mask) */ - if (((static_cast((0b01 << (reader->readPos->type)) & (reader->queryCondition.types)) == 0) || (static_cast((0b01 << (reader->readPos->level)) & (reader->queryCondition.levels)) == 0))) return false; - int ret = 0; if (reader->queryCondition.nDomain > 0) { for (int i = 0; i < reader->queryCondition.nDomain; i++) { diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index be8a760..bed9601 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -68,6 +68,7 @@ LogPersister::~LogPersister() { SAFE_DELETE(rotator); SAFE_DELETE(LogCompress); + SAFE_DELETE(compressBuffer); } int LogPersister::Init() @@ -149,6 +150,8 @@ int LogPersister::Init() SetBufferOffset(0); } logPersisters.push_back(std::static_pointer_cast(shared_from_this())); + compressBuffer = new LogPersisterBuffer[COMPRESS_BUFFER_SIZE]; + return 0; } @@ -183,7 +186,6 @@ int GenPersistLogHeader(HilogData *data, list& persistList) showBuffer.domain = data->domain; showBuffer.tv_sec = data->tv_sec; showBuffer.tv_nsec = data->tv_nsec; - int offset = data->tag_len; char *dataCopy= (char*)calloc(data->len, sizeof(char)); memcpy_s(dataCopy, offset, data->tag, offset); @@ -222,7 +224,7 @@ bool LogPersister::writeUnCompressedBuffer(HilogData *data) int listSize = persistList.size(); if (persistList.empty()) { - listSize = GenPersistLogHeader(data, persistList); + listSize = GenPersistLogHeader(data, persistList); } while (listSize--) { string header = persistList.front(); @@ -232,7 +234,6 @@ bool LogPersister::writeUnCompressedBuffer(HilogData *data) int r = 0; if (buffer->offset + size > MAX_PERSISTER_BUFFER_SIZE) return false; - r = memcpy_s(buffer->content + buffer->offset, MAX_PERSISTER_BUFFER_SIZE - buffer->offset, header.c_str(), headerLen); if (r != 0) { @@ -259,21 +260,30 @@ int LogPersister::WriteData(HilogData *data) break; case COMPRESS_TYPE_ZLIB: { LogCompress = new ZlibCompress(); -#ifdef DEBUG - cout << buffer->content << endl; -#endif LogCompress->Compress((Bytef *)buffer->content, buffer->offset); - rotator->Input((char *)LogCompress->zdata, LogCompress->zdlen); - rotator->FinishInput(); - SetBufferOffset(0); + memcpy_s(compressBuffer->content + compressBuffer->offset, COMPRESS_BUFFER_SIZE, + LogCompress->zdata, LogCompress->zdlen); + compressBuffer->offset += LogCompress->zdlen; + if (compressBuffer->offset >= COMPRESS_BUFFER_SIZE * 2/3) { + rotator->Input((char *)compressBuffer->content, compressBuffer->offset); + rotator->FinishInput(); + compressBuffer->offset = 0; } + SetBufferOffset(0); + } break; #ifdef USING_ZSTD_COMPRESS case COMPRESS_TYPE_ZSTD: { LogCompress = new ZstdCompress(); LogCompress->Compress((Bytef *)buffer->content, buffer->offset); - rotator->Input((char *)LogCompress->zdata, LogCompress->zdlen); - rotator->FinishInput(); + memcpy_s(compressBuffer->content + compressBuffer->offset, COMPRESS_BUFFER_SIZE, + LogCompress->zdata, LogCompress->zdlen); + compressBuffer->offset += LogCompress->zdlen; + if (compressBuffer->offset >= COMPRESS_BUFFER_SIZE * 2/3) { + rotator->Input((char *)compressBuffer->content, compressBuffer->offset); + rotator->FinishInput(); + compressBuffer->offset = 0; + } SetBufferOffset(0); } break; -- Gitee From 79f014efe06d5bda3420accb21de3bdd9dd47aa3 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Wed, 7 Jul 2021 04:32:50 +0000 Subject: [PATCH 02/24] compress Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_compress.cpp | 17 ++++++++++++++--- services/hilogd/log_persister.cpp | 19 +++++++++++++------ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/services/hilogd/log_compress.cpp b/services/hilogd/log_compress.cpp index c06eb59..259f768 100644 --- a/services/hilogd/log_compress.cpp +++ b/services/hilogd/log_compress.cpp @@ -35,6 +35,10 @@ int ZlibCompress::Compress(Bytef *data, uLong dlen) { zdlen = compressBound(dlen); zdata = new unsigned char [zdlen]; + if (zdata == NULL) { + cout << "no enough memory!" << endl; + return -1; + } int err = 0; z_stream c_stream; @@ -78,7 +82,10 @@ int ZstdCompress::Compress(Bytef *data, uLong dlen) #ifdef USING_ZSTD_COMPRESS zdlen = ZSTD_CStreamOutSize(); zdata = new unsigned char [zdlen]; - + if (zdata == NULL) { + cout << "no enough memory!" << endl; + return -1; + } size_t const buffInSize = ZSTD_CStreamInSize(); void* const buffIn = malloc(buffInSize); size_t const buffOutSize = ZSTD_CStreamOutSize(); @@ -86,6 +93,10 @@ int ZstdCompress::Compress(Bytef *data, uLong dlen) int compressionlevel = 1; ZSTD_CCtx* const cctx = ZSTD_createCCtx(); + if (cctx == NULL) { + cout << "ZSTD_createCCtx() failed!" << endl; + return -1; + } ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, compressionlevel); size_t const toRead = buffInSize; @@ -94,8 +105,8 @@ int ZstdCompress::Compress(Bytef *data, uLong dlen) size_t read = dlen; for (;;) { if (read - src_pos < toRead) { - memcpy_s(buffIn, buffInSize - src_pos, data + src_pos, read); - src_pos += read; + memcpy_s(buffIn, buffInSize - src_pos, data + src_pos, read - src_pos); + src_pos += read - src_pos; } else { memcpy_s(buffIn, buffInSize - src_pos, data + src_pos, toRead); src_pos += toRead; diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index bed9601..4cce7d2 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -43,7 +43,7 @@ using namespace std; static std::list> logPersisters; static std::mutex g_listMutex; - +#define ANXILLARY_FILE_NAME "persisterInfo_" #define SAFE_DELETE(x) \ do { \ delete (x); \ @@ -77,7 +77,7 @@ int LogPersister::Init() if (nPos == RET_FAIL) { return RET_FAIL; } - mmapPath = path.substr(0, nPos) + "/." + to_string(id); + mmapPath = path.substr(0, nPos) + "/." + ANXILLARY_FILE_NAME + to_string(id); if (access(path.substr(0, nPos).c_str(), F_OK) != 0) { if (errno == ENOENT) { MkDirPath(path.substr(0, nPos).c_str()); @@ -94,6 +94,7 @@ int LogPersister::Init() if (hit) { return RET_FAIL; } + fd = open(mmapPath.c_str(), O_RDWR | O_CREAT | O_EXCL, 0); bool restore = false; if (fd <= 0) { @@ -260,11 +261,14 @@ int LogPersister::WriteData(HilogData *data) break; case COMPRESS_TYPE_ZLIB: { LogCompress = new ZlibCompress(); - LogCompress->Compress((Bytef *)buffer->content, buffer->offset); + if (LogCompress->Compress((Bytef *)buffer->content, buffer->offset) != 0) { + cout << "COMPRESS_TYPE_ZLIB Error" << endl; + return -1; + }; memcpy_s(compressBuffer->content + compressBuffer->offset, COMPRESS_BUFFER_SIZE, LogCompress->zdata, LogCompress->zdlen); compressBuffer->offset += LogCompress->zdlen; - if (compressBuffer->offset >= COMPRESS_BUFFER_SIZE * 2/3) { + if (compressBuffer->offset >= COMPRESS_BUFFER_SIZE * 2 / 3) { rotator->Input((char *)compressBuffer->content, compressBuffer->offset); rotator->FinishInput(); compressBuffer->offset = 0; @@ -275,11 +279,14 @@ int LogPersister::WriteData(HilogData *data) #ifdef USING_ZSTD_COMPRESS case COMPRESS_TYPE_ZSTD: { LogCompress = new ZstdCompress(); - LogCompress->Compress((Bytef *)buffer->content, buffer->offset); + if (LogCompress->Compress((Bytef *)buffer->content, buffer->offset) != 0) { + cout << "COMPRESS_TYPE_ZSTD Error" << endl; + return -1; + }; memcpy_s(compressBuffer->content + compressBuffer->offset, COMPRESS_BUFFER_SIZE, LogCompress->zdata, LogCompress->zdlen); compressBuffer->offset += LogCompress->zdlen; - if (compressBuffer->offset >= COMPRESS_BUFFER_SIZE * 2/3) { + if (compressBuffer->offset >= COMPRESS_BUFFER_SIZE * 2 / 3) { rotator->Input((char *)compressBuffer->content, compressBuffer->offset); rotator->FinishInput(); compressBuffer->offset = 0; -- Gitee From 3e2a799622a78aad83f2d6f25c7eecd1e3af14bc Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Wed, 7 Jul 2021 09:48:11 +0000 Subject: [PATCH 03/24] delete compress rotator Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/compressing_rotator.cpp | 90 ------------------- services/hilogd/include/compressing_rotator.h | 47 ---------- services/hilogd/log_persister_rotator.cpp | 6 +- services/hilogd/log_querier.cpp | 12 --- 4 files changed, 3 insertions(+), 152 deletions(-) delete mode 100644 services/hilogd/compressing_rotator.cpp delete mode 100644 services/hilogd/include/compressing_rotator.h diff --git a/services/hilogd/compressing_rotator.cpp b/services/hilogd/compressing_rotator.cpp deleted file mode 100644 index 3ec2fd0..0000000 --- a/services/hilogd/compressing_rotator.cpp +++ /dev/null @@ -1,90 +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 "compressing_rotator.h" - -#include -#include - -#include -#include -namespace OHOS { -namespace HiviewDFX { -using namespace std; - -void CompressingRotator::InternalRotate() -{ - cout << "compressing internal rotate" << endl; - if ((uint32_t)fileIndex == resFileNum) { - stringstream ss; - ss << resPath << "."; - int pos = ss.tellp(); - ss << 1; - remove(ss.str().c_str()); - - for (uint32_t i = 2; i <= fileNum; ++i) { - ss.seekp(pos); - ss << (i - 1); - string newName = ss.str(); - ss.seekp(pos); - ss << i; - string oldName = ss.str(); -#ifdef DEBUG - cout << "OLD NAME " << oldName << " NEW NAME " << newName << endl; -#endif - rename(oldName.c_str(), newName.c_str()); - } - fileIndex -= 1; - } - - fileIndex += 1; - gzFile fd; - stringstream resName; - resName << resPath << "." << fileIndex << ".gz"; - fd = gzopen(resName.str().c_str(), "wb"); - if (!fd) { -#ifdef DEBUG - cout << "gzopen failed" << endl; -#endif - } - - uint32_t i = 1; - for (; i <= fileNum; i++) { - stringstream ss; - ss << fileName << "." << i; -#ifdef DEBUG - cout << "ss name " << ss.str() << endl; -#endif - ifstream file(ss.str(), ios::in); - file.seekg(0, file.end); - int size = file.tellg(); -#ifdef DEBUG - cout << "size is " << size << endl; -#endif - unique_ptr memblock = make_unique(size); - file.seekg(0, ios::beg); - file.read(memblock.get(), size); - gzwrite(fd, memblock.get(), size); - - file.close(); - remove(ss.str().c_str()); - } - gzclose(fd); - - leftSize = 0; - index = 0; -} -} // namespace HiviewDFX -} // namespace OHOS diff --git a/services/hilogd/include/compressing_rotator.h b/services/hilogd/include/compressing_rotator.h deleted file mode 100644 index eb2462e..0000000 --- a/services/hilogd/include/compressing_rotator.h +++ /dev/null @@ -1,47 +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. - */ -#ifndef _COMPRESSING_ROTATOR_H -#define _COMPRESSING_ROTATOR_H - -#include - -#include "log_persister_rotator.h" -namespace OHOS { -namespace HiviewDFX { -class CompressingRotator : public LogPersisterRotator { -public: - CompressingRotator(std::string resPath, uint32_t resFileSize, - uint32_t resFileNum, uint32_t tmpFileSize, - uint32_t tmpFileNum, std::string tmpPath = "") - : LogPersisterRotator(tmpPath, tmpFileSize, tmpFileNum) - { - this->resPath = resPath; - this->resFileSize = resFileSize; - this->resFileNum = resFileNum; - this->fileIndex = 0; - } - ~CompressingRotator() = default; -protected: - void InternalRotate() override; - -private: - std::string resPath; - uint32_t resFileSize; - uint32_t resFileNum; - int fileIndex; -}; -} // namespace HiviewDFX -} // namespace OHOS -#endif diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index 8426d2c..1034e51 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -23,7 +23,7 @@ namespace HiviewDFX { using namespace std; LogPersisterRotator::LogPersisterRotator(string path, uint32_t fileSize, uint32_t fileNum, string suffix) - : fileNum(fileNum), fileSize(fileSize), fileName(path), fileSuffix(suffix) + : fileNum(fileNum - 1), fileSize(fileSize), fileName(path), fileSuffix(suffix) { index = 0; leftSize = 0; @@ -72,10 +72,10 @@ void LogPersisterRotator::InternalRotate() stringstream ss; ss << fileName << "."; int pos = ss.tellp(); - ss << 1 << fileSuffix; + ss << 0 << fileSuffix; remove(ss.str().c_str()); - for (uint32_t i = 2; i <= fileNum; ++i) { + for (uint32_t i = 1; i <= fileNum; ++i) { ss.seekp(pos); ss << (i - 1) << fileSuffix; string newName = ss.str(); diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index e7126c3..39aa38d 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -29,7 +29,6 @@ #include #include #include -#include "compressing_rotator.h" #include "hilog/log.h" #include "hilog_common.h" #include "log_data.h" @@ -66,17 +65,6 @@ LogPersisterRotator* MakeRotator(LogPersistStartMsg& pLogPersistStartMsg) string fileSuffix = ""; switch (pLogPersistStartMsg.compressType) { - case CompressStrategy::DIVIDED:{ - stringstream tmpFileSuffix; - tmpFileSuffix << pLogPersistStartMsg.filePath << "tmpLog" << pLogPersistStartMsg.jobId; - int tmpFileSize = 4096; - int tmpFileNum = 2; - return new CompressingRotator( - pLogPersistStartMsg.filePath, - pLogPersistStartMsg.fileSize, - pLogPersistStartMsg.fileNum, - tmpFileSize, tmpFileNum, tmpFileSuffix.str()); - } case CompressStrategy::STREAM: switch (pLogPersistStartMsg.compressAlg) { case CompressAlg::COMPRESS_TYPE_ZSTD: -- Gitee From 9c00452f6bac0a1af024224b74b6d5e343d7b642 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Wed, 7 Jul 2021 14:44:03 +0000 Subject: [PATCH 04/24] zlib stream Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/include/log_compress.h | 2 +- services/hilogd/log_compress.cpp | 75 ++++++++++++++------------ 2 files changed, 43 insertions(+), 34 deletions(-) diff --git a/services/hilogd/include/log_compress.h b/services/hilogd/include/log_compress.h index e57aa23..63b705c 100644 --- a/services/hilogd/include/log_compress.h +++ b/services/hilogd/include/log_compress.h @@ -21,7 +21,7 @@ #include "zstd.h" #endif #include - +#define CHUNK 16384 namespace OHOS { namespace HiviewDFX { class LogCompress { diff --git a/services/hilogd/log_compress.cpp b/services/hilogd/log_compress.cpp index 259f768..43ba0df 100644 --- a/services/hilogd/log_compress.cpp +++ b/services/hilogd/log_compress.cpp @@ -39,42 +39,51 @@ int ZlibCompress::Compress(Bytef *data, uLong dlen) cout << "no enough memory!" << endl; return -1; } - int err = 0; + void* const buffIn = malloc(CHUNK); + void* const buffOut = malloc(CHUNK); + size_t const toRead = CHUNK; + auto src_pos = 0; + auto dst_pos = 0; + size_t read = dlen; + int ret = 0; + unsigned have = 0; + int flush = 0; z_stream c_stream; - - if (data && dlen > 0) { - c_stream.zalloc = NULL; - c_stream.zfree = NULL; - c_stream.opaque = NULL; - if (deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK) { - return -1; - } - c_stream.next_in = data; - c_stream.avail_in = dlen; - c_stream.next_out = zdata; - c_stream.avail_out = zdlen; - while (c_stream.avail_in != 0 && c_stream.total_out < zdlen) { - if (deflate(&c_stream, Z_NO_FLUSH) != Z_OK) { - return -1; - } - } - if (c_stream.avail_in != 0) { - return c_stream.avail_in; + c_stream.zalloc = Z_NULL; + c_stream.zfree = Z_NULL; + c_stream.opaque = Z_NULL; + if (deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK) { + return -1; + } + do { + if (read - src_pos < toRead) { + memcpy_s(buffIn, CHUNK - src_pos , data + src_pos, read - src_pos); + src_pos += read - src_pos; + } else { + memcpy_s(buffIn, CHUNK - src_pos, data + src_pos, toRead); + src_pos += toRead; } - for (;;) { - if ((err = deflate(&c_stream, Z_FINISH)) == Z_STREAM_END) - break; - if (err != Z_OK) { + int const lastChunk = (read < toRead); + flush = lastChunk ? Z_FINISH : Z_NO_FLUSH; + c_stream.avail_in = read; + c_stream.next_in = (Bytef *)buffIn; + /* run deflate() on input until output buffer not full, finish + compression if all of source has been read in */ + do { + c_stream.avail_out = CHUNK; + c_stream.next_out = (Bytef *)buffOut; + if (deflate(&c_stream, flush) == Z_STREAM_ERROR) { return -1; - } - } - if (deflateEnd(&c_stream) != Z_OK) { - return -1; - } - zdlen = c_stream.total_out; - return 0; - } - return -1; + } /* no bad return value */ + have = CHUNK - c_stream.avail_out; + memcpy_s(zdata + dst_pos, zdlen - dst_pos, buffOut, have); + dst_pos += have; + zdlen = dst_pos; + } while (c_stream.avail_out == 0); + } while (flush != Z_FINISH); + /* clean up and return */ + (void)deflateEnd(&c_stream); + return 0; } int ZstdCompress::Compress(Bytef *data, uLong dlen) -- Gitee From 1ddceea953a901d5e4dd0e4b5a686552e6607cdf Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Fri, 9 Jul 2021 13:00:37 +0000 Subject: [PATCH 05/24] persisit bug solved Signed-off-by: aajwy <13051180828@163.com> --- frameworks/native/include/hilogtool_msg.h | 8 --- services/hilogd/include/log_persister.h | 6 +-- services/hilogd/log_compress.cpp | 61 ++++++++++++----------- services/hilogd/log_persister.cpp | 8 +-- services/hilogd/log_persister_rotator.cpp | 4 -- services/hilogd/log_querier.cpp | 38 +++++--------- services/hilogtool/include/hilogtool.h | 1 - services/hilogtool/log_controller.cpp | 30 ++++++++--- services/hilogtool/log_display.cpp | 16 ------ services/hilogtool/main.cpp | 22 ++++---- 10 files changed, 79 insertions(+), 115 deletions(-) diff --git a/frameworks/native/include/hilogtool_msg.h b/frameworks/native/include/hilogtool_msg.h index 86a1f26..2aaed3c 100644 --- a/frameworks/native/include/hilogtool_msg.h +++ b/frameworks/native/include/hilogtool_msg.h @@ -59,11 +59,6 @@ typedef enum { OT_FLOW_SWITCH, } OperateType; -typedef enum { - NONE = 0, - STREAM = 1, - DIVIDED = 2, -} CompressStrategy; typedef enum { CREATE_SUCCESS = 1, @@ -241,7 +236,6 @@ typedef struct { typedef struct { std::string logTypeStr; - std::string compressTypeStr; std::string compressAlgStr; std::string fileSizeStr; std::string fileNumStr; @@ -250,7 +244,6 @@ typedef struct { } LogPersistParam; typedef struct { uint16_t logType; // union logType - uint16_t compressType; uint16_t compressAlg; char filePath[FILE_PATH_MAX_LEN]; uint32_t fileSize; @@ -301,7 +294,6 @@ typedef struct { int32_t result; uint32_t jobId; uint16_t logType; - uint16_t compressType; uint16_t compressAlg; char filePath[FILE_PATH_MAX_LEN]; uint32_t fileSize; diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index 06a7d50..39909cc 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -38,9 +38,8 @@ const uint16_t MAX_PERSISTER_BUFFER_SIZE = 4096; const uint32_t COMPRESS_BUFFER_SIZE = 4 * 1024; class LogPersister : public LogReader { public: - LogPersister(uint32_t id, std::string path, uint16_t compressType, - uint16_t compressAlg, int sleepTime, LogPersisterRotator *rotator, - HilogBuffer *buffer); + LogPersister(uint32_t id, std::string path, uint16_t compressAlg, int sleepTime, + LogPersisterRotator *rotator, HilogBuffer *buffer); ~LogPersister(); void SetBufferOffset(int off); void NotifyForNewData(); @@ -63,7 +62,6 @@ private: uint32_t id; std::string path; std::string mmapPath; - uint16_t compressType; uint16_t compressAlg; int sleepTime; std::mutex cvMutex; diff --git a/services/hilogd/log_compress.cpp b/services/hilogd/log_compress.cpp index 43ba0df..db8dffb 100644 --- a/services/hilogd/log_compress.cpp +++ b/services/hilogd/log_compress.cpp @@ -35,8 +35,9 @@ int ZlibCompress::Compress(Bytef *data, uLong dlen) { zdlen = compressBound(dlen); zdata = new unsigned char [zdlen]; + if (zdata == NULL) { - cout << "no enough memory!" << endl; + cout << "no enough memory!" << endl; return -1; } void* const buffIn = malloc(CHUNK); @@ -45,8 +46,6 @@ int ZlibCompress::Compress(Bytef *data, uLong dlen) auto src_pos = 0; auto dst_pos = 0; size_t read = dlen; - int ret = 0; - unsigned have = 0; int flush = 0; z_stream c_stream; c_stream.zalloc = Z_NULL; @@ -55,17 +54,20 @@ int ZlibCompress::Compress(Bytef *data, uLong dlen) if (deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK) { return -1; } - do { - if (read - src_pos < toRead) { - memcpy_s(buffIn, CHUNK - src_pos , data + src_pos, read - src_pos); + do { + bool flag = read - src_pos < toRead; + if (flag) { + memset_s(buffIn, CHUNK, 0, CHUNK); + memmove_s(buffIn, CHUNK, data + src_pos, read - src_pos); + c_stream.avail_in = read - src_pos; src_pos += read - src_pos; } else { - memcpy_s(buffIn, CHUNK - src_pos, data + src_pos, toRead); + memset_s(buffIn, CHUNK, 0, CHUNK); + memmove_s(buffIn, CHUNK, data + src_pos, toRead); src_pos += toRead; + c_stream.avail_in = toRead; } - int const lastChunk = (read < toRead); - flush = lastChunk ? Z_FINISH : Z_NO_FLUSH; - c_stream.avail_in = read; + flush = flag ? Z_FINISH : Z_NO_FLUSH; c_stream.next_in = (Bytef *)buffIn; /* run deflate() on input until output buffer not full, finish compression if all of source has been read in */ @@ -75,14 +77,16 @@ int ZlibCompress::Compress(Bytef *data, uLong dlen) if (deflate(&c_stream, flush) == Z_STREAM_ERROR) { return -1; } /* no bad return value */ - have = CHUNK - c_stream.avail_out; - memcpy_s(zdata + dst_pos, zdlen - dst_pos, buffOut, have); + unsigned have = CHUNK - c_stream.avail_out; + memmove_s(zdata + dst_pos, CHUNK, buffOut, have); dst_pos += have; zdlen = dst_pos; } while (c_stream.avail_out == 0); } while (flush != Z_FINISH); /* clean up and return */ (void)deflateEnd(&c_stream); + free(buffIn); + free(buffOut); return 0; } @@ -99,7 +103,7 @@ int ZstdCompress::Compress(Bytef *data, uLong dlen) void* const buffIn = malloc(buffInSize); size_t const buffOutSize = ZSTD_CStreamOutSize(); void* const buffOut = malloc(buffOutSize); - + ZSTD_EndDirective mode; int compressionlevel = 1; ZSTD_CCtx* const cctx = ZSTD_createCCtx(); if (cctx == NULL) { @@ -107,38 +111,35 @@ int ZstdCompress::Compress(Bytef *data, uLong dlen) return -1; } ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, compressionlevel); - size_t const toRead = buffInSize; auto src_pos = 0; auto dst_pos = 0; size_t read = dlen; - for (;;) { - if (read - src_pos < toRead) { - memcpy_s(buffIn, buffInSize - src_pos, data + src_pos, read - src_pos); + ZSTD_inBuffer input; + do { + bool flag = read - src_pos < toRead; + if (flag) { + memset_s(buffIn, buffInSize, 0, buffInSize); + memmove(buffIn, data + src_pos, read - src_pos); + input = {buffIn, read - src_pos, 0}; src_pos += read - src_pos; } else { - memcpy_s(buffIn, buffInSize - src_pos, data + src_pos, toRead); + memset_s(buffIn, buffInSize, 0, buffInSize); + memmove(buffIn, data + src_pos, toRead); + input = {buffIn, toRead, 0}; src_pos += toRead; } - int const lastChunk = (read < toRead); - ZSTD_EndDirective const mode = lastChunk ? ZSTD_e_end : ZSTD_e_continue; - - ZSTD_inBuffer input = {buffIn, read, 0}; + mode = flag ? ZSTD_e_end : ZSTD_e_continue; int finished; - do { ZSTD_outBuffer output = {buffOut, buffOutSize, 0}; size_t const remaining = ZSTD_compressStream2(cctx, &output, &input, mode); - memcpy_s(zdata + dst_pos, zdlen - dst_pos, buffOut, output.pos); + memmove_s(zdata + dst_pos, zdlen, (Bytef *)buffOut, output.pos); dst_pos += output.pos; zdlen = dst_pos; - finished = lastChunk ? (remaining == 0) : (input.pos == input.size); + finished = flag ? (remaining == 0) : (input.pos == input.size); } while (!finished); - - if (lastChunk) { - break; - } - } + } while (mode != ZSTD_e_end); free(buffIn); free(buffOut); ZSTD_freeCCtx(cctx); diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 4cce7d2..0f23ec0 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -40,7 +40,6 @@ namespace HiviewDFX { using namespace std::literals::chrono_literals; using namespace std; - static std::list> logPersisters; static std::mutex g_listMutex; #define ANXILLARY_FILE_NAME "persisterInfo_" @@ -50,10 +49,9 @@ static std::mutex g_listMutex; (x) = nullptr; \ } while (0) -LogPersister::LogPersister(uint32_t id, string path, uint16_t compressType, - uint16_t compressAlg, int sleepTime, +LogPersister::LogPersister(uint32_t id, string path, uint16_t compressAlg, int sleepTime, LogPersisterRotator *rotator, HilogBuffer *_buffer) - : id(id), path(path), compressType(compressType), compressAlg(compressAlg), + : id(id), path(path), compressAlg(compressAlg), sleepTime(sleepTime), rotator(rotator) { toExit = false; @@ -152,7 +150,6 @@ int LogPersister::Init() } logPersisters.push_back(std::static_pointer_cast(shared_from_this())); compressBuffer = new LogPersisterBuffer[COMPRESS_BUFFER_SIZE]; - return 0; } @@ -370,7 +367,6 @@ void LogPersister::FillInfo(LogPersistQueryResult *response) if (strcpy_s(response->filePath, FILE_PATH_MAX_LEN, path.c_str())) { return; } - response->compressType = compressType; response->compressAlg = compressAlg; rotator->FillInfo(&response->fileSize, &response->fileNum); return; diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index 1034e51..85845d8 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -39,7 +39,6 @@ int LogPersisterRotator::Input(const char *buf, int length) Rotate(); needRotate = false; } - if (length <= 0 || buf == nullptr) return -1; unsigned int offset = 0; @@ -59,11 +58,9 @@ int LogPersisterRotator::Input(const char *buf, int length) } Rotate(); } - output.write(buf + offset, length); leftSize -= length; output.flush(); - return 0; } @@ -85,7 +82,6 @@ void LogPersisterRotator::InternalRotate() cout << "OLD NAME " << oldName << " NEW NAME " << newName << endl; rename(oldName.c_str(), newName.c_str()); } - output.open(ss.str(), ios::out); leftSize = fileSize; } diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 39aa38d..6f2f499 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -63,30 +63,21 @@ inline bool IsValidFileName(const std::string& strFileName) LogPersisterRotator* MakeRotator(LogPersistStartMsg& pLogPersistStartMsg) { string fileSuffix = ""; - - switch (pLogPersistStartMsg.compressType) { - case CompressStrategy::STREAM: - switch (pLogPersistStartMsg.compressAlg) { - case CompressAlg::COMPRESS_TYPE_ZSTD: - fileSuffix = ".zst"; - break; - case CompressAlg::COMPRESS_TYPE_ZLIB: - fileSuffix = ".gz"; - break; - default: - break; - }; - return new LogPersisterRotator( - pLogPersistStartMsg.filePath, - pLogPersistStartMsg.fileSize, - pLogPersistStartMsg.fileNum, - fileSuffix); + switch (pLogPersistStartMsg.compressAlg) { + case CompressAlg::COMPRESS_TYPE_ZSTD: + fileSuffix = ".zst"; + break; + case CompressAlg::COMPRESS_TYPE_ZLIB: + fileSuffix = ".gz"; + break; default: - return new LogPersisterRotator( - pLogPersistStartMsg.filePath, - pLogPersistStartMsg.fileSize, - pLogPersistStartMsg.fileNum); + break; }; + return new LogPersisterRotator( + pLogPersistStartMsg.filePath, + pLogPersistStartMsg.fileSize, + pLogPersistStartMsg.fileNum, + fileSuffix); } void HandleLogQueryRequest(std::shared_ptr logReader, HilogBuffer& buffer) @@ -135,7 +126,6 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade std::shared_ptr persister = make_shared( pLogPersistStartMsg->jobId, pLogPersistStartMsg->filePath, - pLogPersistStartMsg->compressType, pLogPersistStartMsg->compressAlg, SLEEP_TIME, rotator, buffer); @@ -149,7 +139,6 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade persister->Start(); buffer->AddLogReader(weak_ptr(persister)); } - SetMsgHead(&pLogPersistStartRsp->msgHeader, MC_RSP_LOG_PERSIST_START, sendMsgLen); logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); } @@ -221,7 +210,6 @@ void HandlePersistQueryRequest(char* reqMsg, std::shared_ptr logReade pLogPersistQueryRst->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; pLogPersistQueryRst->jobId = (*it).jobId; pLogPersistQueryRst->logType = (*it).logType; - pLogPersistQueryRst->compressType = (*it).compressType; pLogPersistQueryRst->compressAlg = (*it).compressAlg; if (strcpy_s(pLogPersistQueryRst->filePath, FILE_PATH_MAX_LEN, (*it).filePath)) { return; diff --git a/services/hilogtool/include/hilogtool.h b/services/hilogtool/include/hilogtool.h index a13cfdb..0e10f3e 100644 --- a/services/hilogtool/include/hilogtool.h +++ b/services/hilogtool/include/hilogtool.h @@ -38,7 +38,6 @@ typedef struct { std::string regexArgs; std::string buffSizeArgs; std::string logFileCtrlArgs; - std::string compressArgs; std::string fileSizeArgs; std::string fileNumArgs; std::string filePathArgs; diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 1fb61fd..f641134 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -107,6 +107,19 @@ uint64_t GetBuffSize(const string& buffSizeStr) } return buffSize; } + +uint16_t GetCompressAlg(const std::string& pressAlg) +{ + if (pressAlg == "none") { + return COMPRESS_TYPE_OFF; + } else if (pressAlg == "zlib") { + return COMPRESS_TYPE_ZLIB; + } else if (pressAlg == "zstd") { + return COMPRESS_TYPE_ZSTD; + } + return COMPRESS_TYPE_ZLIB; +} + uint16_t GetLogLevel(const string& logLevelStr) { if (logLevelStr == "debug" || logLevelStr == "DEBUG") { @@ -362,9 +375,6 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi Split(logPersistParam->jobIdStr, " ", vecJobId); logTypeNum = vecLogType.size(); jobIdNum = vecJobId.size(); - if (msgCmd == MC_REQ_LOG_PERSIST_STOP && logPersistParam->jobIdStr == "") { // support stop several jobs each time - return RET_FAIL; - } switch (msgCmd) { case MC_REQ_LOG_PERSIST_START: { LogPersistStartRequest* pLogPersistStartReq = reinterpret_cast(msgToSend); @@ -382,11 +392,9 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi } pLogPersistStartMsg->jobId = (logPersistParam->jobIdStr == "") ? DEFAULT_JOBID : stoi(logPersistParam->jobIdStr); - pLogPersistStartMsg->compressType = (logPersistParam->compressTypeStr == "") ? STREAM : stoi(logPersistParam - ->compressTypeStr); - pLogPersistStartMsg->compressAlg = (logPersistParam->compressAlgStr == "") ? COMPRESS_TYPE_ZLIB : stoi( - logPersistParam->compressAlgStr); - pLogPersistStartMsg->fileSize = (logPersistParam->fileSizeStr == "") ? fileSizeDefault : stoi( + pLogPersistStartMsg->compressAlg = (logPersistParam->compressAlgStr == "") ? COMPRESS_TYPE_ZLIB : + GetCompressAlg(logPersistParam->compressAlgStr); + pLogPersistStartMsg->fileSize = (logPersistParam->fileSizeStr == "") ? fileSizeDefault : GetBuffSize( logPersistParam->fileSizeStr); pLogPersistStartMsg->fileNum = (logPersistParam->fileNumStr == "") ? fileNumDefault : stoi(logPersistParam->fileNumStr); @@ -406,6 +414,12 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi reinterpret_cast(msgToSend); LogPersistStopMsg* pLogPersistStopMsg = reinterpret_cast(&pLogPersistStopReq->logPersistStopMsg); + if (logPersistParam->jobIdStr == "") { + pLogPersistStopMsg->jobId = 0xffffffff; + SetMsgHead(&pLogPersistStopReq->msgHeader, msgCmd, sizeof(LogPersistStopMsg)); + controller.WriteAll(msgToSend, sizeof(LogPersistStopMsg) + sizeof(MessageHeader)); + break; + } if (jobIdNum * sizeof(LogPersistStopMsg) + sizeof(MessageHeader) > MSG_MAX_LEN) { return RET_FAIL; } diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 6bf63aa..28956e4 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -84,20 +84,6 @@ string GetOrigType(uint16_t shiftType) return logType; } -string GetPressTypeStr(uint16_t pressType) -{ - string pressTypeStr; - if (pressType == NONE) { - pressTypeStr = "none"; - } - if (pressType == STREAM) { - pressTypeStr = "stream"; - } - if (pressType == DIVIDED) { - pressTypeStr = "divided"; - } - return pressTypeStr; -} string GetPressAlgStr(uint16_t pressAlg) { @@ -322,8 +308,6 @@ int32_t ControlCmdResult(const char* message) outputStr += " "; outputStr += GetOrigType(pLogPersistQueryRst->logType); outputStr += " "; - outputStr += GetPressTypeStr(pLogPersistQueryRst->compressType); - outputStr += " "; outputStr += GetPressAlgStr(pLogPersistQueryRst->compressAlg); outputStr += " "; outputStr += pLogPersistQueryRst->filePath; diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index 0b2edf1..37e1e6c 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -68,9 +68,6 @@ static void Helper() " specify the domain.\n" " -T , --Tag=\n" " specify the tag.\n" - " -c , --compress=\n" - " specify compress type of log files.\n" - " 0 plain text, 1 stream compression 2 compress per file.\n" " -a , --head= show n lines log on head.\n" " -z , --tail= show n lines log on tail.\n" " -G , --buffer-size=\n" @@ -79,7 +76,7 @@ static void Helper() " -e , --regex=\n" " show the logs which match the regular expression,\n" " is a regular expression.\n" - " -F , --filename=\n" + " -f , --filename=\n" " set log file name.\n" " -l , --length=\n" " set single log file size.\n" @@ -91,6 +88,10 @@ static void Helper() " query log file writing task query.\n" " start start a log file writing task, see -F -l -n -c for to set more configs,\n" " stop stop a log file writing task.\n" + " -m ,--stream=\n" + " none log file without compressing\n" + " zlib compress log file by the zlib algorithm\n" + " zstd compress log file by the zstd algorithm\n" " -v , --format= options:\n" " time display local time.\n" " color display colorful logs by log level.i.e. \x1B[38;5;231mVERBOSE\n" @@ -193,10 +194,9 @@ int HilogEntry(int argc, char* argv[]) { "buffer-size", required_argument, nullptr, 'G' }, { "jobid", required_argument, nullptr, 'j' }, { "flowctrl", required_argument, nullptr, 'Q' }, - { "compress", required_argument, nullptr, 'c' }, { "stream", required_argument, nullptr, 'm' }, { "number", required_argument, nullptr, 'n' }, - { "filename", required_argument, nullptr, 'F' }, + { "filename", required_argument, nullptr, 'f' }, { "private", required_argument, nullptr, 'p' }, { "domain", required_argument, nullptr, 'D' }, { "tag", required_argument, nullptr, 'T' }, @@ -207,7 +207,7 @@ int HilogEntry(int argc, char* argv[]) {nullptr, 0, nullptr, 0} }; - int choice = getopt_long(argc, argv, "hxz:grsSa:v:e:t:L:G:F:l:n:j:w:c:p:k:M:D:T:b:Q:m:P:", + int choice = getopt_long(argc, argv, "hxz:grsSa:v:e:t:L:G:f:l:n:j:w:p:k:M:D:T:b:Q:m:P:", longOptions, &optIndex); if (choice == -1) { break; @@ -308,16 +308,13 @@ int HilogEntry(int argc, char* argv[]) context.logFileCtrlArgs = optarg; noLogOption = true; break; - case 'c': - context.compressArgs = optarg; - break; case 'l': context.fileSizeArgs = optarg; break; case 'n': context.fileNumArgs = optarg; break; - case 'F': + case 'f': context.fileNameArgs = optarg; break; case 'j': @@ -448,7 +445,6 @@ int HilogEntry(int argc, char* argv[]) } else if (context.logFileCtrlArgs != "") { LogPersistParam logPersistParam; logPersistParam.logTypeStr = context.logTypeArgs; - logPersistParam.compressTypeStr = context.compressArgs; logPersistParam.compressAlgStr = context.algorithmArgs; logPersistParam.fileSizeStr = context.fileSizeArgs; logPersistParam.fileNumStr = context.fileNumArgs; @@ -552,7 +548,7 @@ int HilogEntry(int argc, char* argv[]) } default: - cout << "Invalid response from hilogd!" << endl; + cout << "Invalid response from hilogd! response: " << msgHeader->msgType << endl; break; } return 0; -- Gitee From ed7c0670de2da10ab6024e44158591d49fa8a54f Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Mon, 12 Jul 2021 10:36:58 +0000 Subject: [PATCH 06/24] persist simply Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_compress.cpp | 7 +++-- services/hilogd/log_querier.cpp | 37 +++++++++++++++++--------- services/hilogtool/include/hilogtool.h | 2 +- services/hilogtool/log_controller.cpp | 4 +-- services/hilogtool/log_display.cpp | 22 +++++++-------- 5 files changed, 42 insertions(+), 30 deletions(-) diff --git a/services/hilogd/log_compress.cpp b/services/hilogd/log_compress.cpp index db8dffb..a66148f 100644 --- a/services/hilogd/log_compress.cpp +++ b/services/hilogd/log_compress.cpp @@ -35,9 +35,8 @@ int ZlibCompress::Compress(Bytef *data, uLong dlen) { zdlen = compressBound(dlen); zdata = new unsigned char [zdlen]; - if (zdata == NULL) { - cout << "no enough memory!" << endl; + cout << "no enough memory!" << endl; return -1; } void* const buffIn = malloc(CHUNK); @@ -54,7 +53,7 @@ int ZlibCompress::Compress(Bytef *data, uLong dlen) if (deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK) { return -1; } - do { + do { bool flag = read - src_pos < toRead; if (flag) { memset_s(buffIn, CHUNK, 0, CHUNK); @@ -116,7 +115,7 @@ int ZstdCompress::Compress(Bytef *data, uLong dlen) auto dst_pos = 0; size_t read = dlen; ZSTD_inBuffer input; - do { + do { bool flag = read - src_pos < toRead; if (flag) { memset_s(buffIn, buffInSize, 0, buffInSize); diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 6f2f499..8785dc7 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -45,7 +45,7 @@ constexpr int MAX_DATA_LEN = 2048; string g_logPersisterDir = "/data/misc/logd/"; constexpr int DEFAULT_LOG_LEVEL = 1< persisterJobIds; static char g_tempBuffer[MAX_DATA_LEN] = {0}; inline void SetMsgHead(MessageHeader* msgHeader, uint8_t msgCmd, uint16_t msgLen) { @@ -128,7 +128,6 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade pLogPersistStartMsg->filePath, pLogPersistStartMsg->compressAlg, SLEEP_TIME, rotator, buffer); - pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; pLogPersistStartRst->result = persister->Init(); persister->queryCondition.types = pLogPersistStartMsg->logType; @@ -136,6 +135,7 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade if (pLogPersistStartRst->result == RET_FAIL) { persister.reset(); } else { + persisterJobIds.push_back(pLogPersistStartRst->jobId); persister->Start(); buffer->AddLogReader(weak_ptr(persister)); } @@ -163,17 +163,30 @@ void HandlePersistDeleteRequest(char* reqMsg, std::shared_ptr logRead if (msgLen > sizeof(LogPersistStopMsg) * LOG_TYPE_MAX) { return; } - - while (pLogPersistStopMsg && recvMsgLen < msgLen) { - rst = LogPersister::Kill(pLogPersistStopMsg->jobId); - if (pLogPersistStopRst) { - pLogPersistStopRst->jobId = pLogPersistStopMsg->jobId; - pLogPersistStopRst->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; - pLogPersistStopRst++; + if(pLogPersistStopMsg && pLogPersistStopMsg->jobId == 0xffffffff) { + for (list::iterator iter = persisterJobIds.begin();iter != persisterJobIds.end(); ++iter) { + rst = LogPersister::Kill(*iter); + persisterJobIds.pop_front(); + cout << "*iter" << hex << *iter << endl; + if (pLogPersistStopRst) { + pLogPersistStopRst->jobId = *iter; + pLogPersistStopRst->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; + pLogPersistStopRst++; + msgNum++; + } + } + } else { + while (pLogPersistStopMsg && recvMsgLen < msgLen) { + rst = LogPersister::Kill(pLogPersistStopMsg->jobId); + if (pLogPersistStopRst) { + pLogPersistStopRst->jobId = pLogPersistStopMsg->jobId; + pLogPersistStopRst->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; + pLogPersistStopRst++; + } + pLogPersistStopMsg++; + recvMsgLen += sizeof(LogPersistStopMsg); + msgNum++; } - pLogPersistStopMsg++; - recvMsgLen += sizeof(LogPersistStopMsg); - msgNum++; } sendMsgLen = msgNum * sizeof(LogPersistStopResult); SetMsgHead(&pLogPersistStopRsp->msgHeader, MC_RSP_LOG_PERSIST_STOP, sendMsgLen); diff --git a/services/hilogtool/include/hilogtool.h b/services/hilogtool/include/hilogtool.h index 0e10f3e..98d2c42 100644 --- a/services/hilogtool/include/hilogtool.h +++ b/services/hilogtool/include/hilogtool.h @@ -57,4 +57,4 @@ typedef struct { } HilogArgs; } // namespace HiviewDFX } // namespace OHOS -#endif +#endif diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index f641134..46e2f25 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -227,7 +227,7 @@ void LogQueryResponseOp(SeqPacketSocketClient& controller, char* recvBuffer, uin HilogShowLog(format, data, context, tailBuffer); NextRequestOp(controller, SENDIDA); break; - default: + default: NextRequestOp(controller, SENDIDA); break; } @@ -392,7 +392,7 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi } pLogPersistStartMsg->jobId = (logPersistParam->jobIdStr == "") ? DEFAULT_JOBID : stoi(logPersistParam->jobIdStr); - pLogPersistStartMsg->compressAlg = (logPersistParam->compressAlgStr == "") ? COMPRESS_TYPE_ZLIB : + pLogPersistStartMsg->compressAlg = (logPersistParam->compressAlgStr == "") ? COMPRESS_TYPE_ZLIB : GetCompressAlg(logPersistParam->compressAlgStr); pLogPersistStartMsg->fileSize = (logPersistParam->fileSizeStr == "") ? fileSizeDefault : GetBuffSize( logPersistParam->fileSizeStr); diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 28956e4..358d5a1 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -257,13 +257,13 @@ int32_t ControlCmdResult(const char* message) (LogPersistStartResult*)&pLogPersistStartRsp->logPersistStartRst; while (pLogPersistStartRst && resultLen < msgLen) { if (pLogPersistStartRst->result == RET_FAIL) { + outputStr += "Persist task [jobid:"; outputStr += to_string(pLogPersistStartRst->jobId); - outputStr += " log file task start fail"; - outputStr += "\n"; + outputStr += "] start failed\n"; } else { + outputStr += "Persist task [jobid:"; outputStr += to_string(pLogPersistStartRst->jobId); - outputStr += " log file task start success"; - outputStr += "\n"; + outputStr += "] started successfully\n"; } pLogPersistStartRst++; resultLen += sizeof(LogPersistStartResult); @@ -278,13 +278,13 @@ int32_t ControlCmdResult(const char* message) LogPersistStopResult* pLogPersistStopRst = (LogPersistStopResult*)&pLogPersistStopRsp->logPersistStopRst; while (pLogPersistStopRst && resultLen < msgLen) { if (pLogPersistStopRst->result == RET_FAIL) { + outputStr += "Persist task [jobid:"; outputStr += to_string(pLogPersistStopRst->jobId); - outputStr += " log file task stop fail"; - outputStr += "\n"; + outputStr += "] stop failed\n"; } else { + outputStr += "Persist task [jobid:"; outputStr += to_string(pLogPersistStopRst->jobId); - outputStr += " log file task stop success"; - outputStr += "\n"; + outputStr += "] stopped successfully\n"; } pLogPersistStopRst++; resultLen += sizeof(LogPersistStopResult); @@ -299,10 +299,10 @@ int32_t ControlCmdResult(const char* message) LogPersistQueryResult* pLogPersistQueryRst = (LogPersistQueryResult*)&pLogPersistQueryRsp->logPersistQueryRst; while (pLogPersistQueryRst && resultLen < msgLen) { - if (pLogPersistQueryRst->result == RET_FAIL) { + if (pLogPersistQueryRst->result == RET_FAIL) {\ + outputStr = "Persist task [logtype:"; outputStr += GetLogTypeStr(pLogPersistQueryRst->logType); - outputStr = " log file task query fail"; - outputStr += "\n"; + outputStr += "] query failed\n"; } else { outputStr += to_string(pLogPersistQueryRst->jobId); outputStr += " "; -- Gitee From 20eef5a2fce96b8a8242e20cc6c405d146a697e3 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Mon, 12 Jul 2021 11:07:24 +0000 Subject: [PATCH 07/24] code format Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/BUILD.gn | 4 ---- services/hilogd/include/log_persister.h | 2 +- services/hilogd/log_compress.cpp | 30 +++++++++++++++++++------ services/hilogd/log_querier.cpp | 16 +++++++------ services/hilogtool/log_controller.cpp | 2 +- services/hilogtool/log_display.cpp | 5 ++--- services/hilogtool/main.cpp | 2 +- 7 files changed, 37 insertions(+), 24 deletions(-) diff --git a/services/hilogd/BUILD.gn b/services/hilogd/BUILD.gn index a0e4359..151ba18 100644 --- a/services/hilogd/BUILD.gn +++ b/services/hilogd/BUILD.gn @@ -22,7 +22,6 @@ config("hilogd_config") { ohos_executable("hilogd") { sources = [ "cmd_executor.cpp", - "compressing_rotator.cpp", "flow_control_init.cpp", "log_buffer.cpp", "log_collector.cpp", @@ -33,7 +32,6 @@ ohos_executable("hilogd") { "log_reader.cpp", "main.cpp", ] - configs = [ ":hilogd_config" ] deps = [ @@ -54,7 +52,6 @@ ohos_executable("hilogd") { ohos_executable("hilogd_native") { sources = [ "cmd_executor.cpp", - "compressing_rotator.cpp", "flow_control_init.cpp", "log_buffer.cpp", "log_collector.cpp", @@ -65,7 +62,6 @@ ohos_executable("hilogd_native") { "log_reader.cpp", "main.cpp", ] - configs = [ ":hilogd_config" ] deps = [ diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index 513c439..82c0259 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -35,7 +35,7 @@ typedef struct { } LogPersisterBuffer; const uint16_t MAX_PERSISTER_BUFFER_SIZE = 4096; -const uint32_t COMPRESS_BUFFER_SIZE = 4 * 1024; +const uint32_t COMPRESS_BUFFER_SIZE = 64 * 1024; class LogPersister : public LogReader { public: LogPersister(uint32_t id, std::string path, uint16_t compressAlg, int sleepTime, diff --git a/services/hilogd/log_compress.cpp b/services/hilogd/log_compress.cpp index a66148f..90b0436 100644 --- a/services/hilogd/log_compress.cpp +++ b/services/hilogd/log_compress.cpp @@ -51,18 +51,24 @@ int ZlibCompress::Compress(Bytef *data, uLong dlen) c_stream.zfree = Z_NULL; c_stream.opaque = Z_NULL; if (deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK) { + free(buffIn); + free(buffOut); return -1; } do { bool flag = read - src_pos < toRead; if (flag) { memset_s(buffIn, CHUNK, 0, CHUNK); - memmove_s(buffIn, CHUNK, data + src_pos, read - src_pos); + if (memmove_s(buffIn, CHUNK, data + src_pos, read - src_pos) != 0) { + return -1; + } c_stream.avail_in = read - src_pos; src_pos += read - src_pos; } else { memset_s(buffIn, CHUNK, 0, CHUNK); - memmove_s(buffIn, CHUNK, data + src_pos, toRead); + if (memmove_s(buffIn, CHUNK, data + src_pos, toRead) != 0) { + return -1; + }; src_pos += toRead; c_stream.avail_in = toRead; } @@ -75,9 +81,11 @@ int ZlibCompress::Compress(Bytef *data, uLong dlen) c_stream.next_out = (Bytef *)buffOut; if (deflate(&c_stream, flush) == Z_STREAM_ERROR) { return -1; - } /* no bad return value */ + } unsigned have = CHUNK - c_stream.avail_out; - memmove_s(zdata + dst_pos, CHUNK, buffOut, have); + if (memmove_s(zdata + dst_pos, CHUNK, buffOut, have) != 0) { + return -1; + } dst_pos += have; zdlen = dst_pos; } while (c_stream.avail_out == 0); @@ -107,6 +115,8 @@ int ZstdCompress::Compress(Bytef *data, uLong dlen) ZSTD_CCtx* const cctx = ZSTD_createCCtx(); if (cctx == NULL) { cout << "ZSTD_createCCtx() failed!" << endl; + free(buffIn); + free(buffOut); return -1; } ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, compressionlevel); @@ -119,12 +129,16 @@ int ZstdCompress::Compress(Bytef *data, uLong dlen) bool flag = read - src_pos < toRead; if (flag) { memset_s(buffIn, buffInSize, 0, buffInSize); - memmove(buffIn, data + src_pos, read - src_pos); + if (memmove_s(buffIn, buffInSize, data + src_pos, read - src_pos) != 0) { + return -1; + } input = {buffIn, read - src_pos, 0}; src_pos += read - src_pos; } else { memset_s(buffIn, buffInSize, 0, buffInSize); - memmove(buffIn, data + src_pos, toRead); + if (memmove_s(buffIn, buffInSize, data + src_pos, toRead) != 0) { + return -1; + } input = {buffIn, toRead, 0}; src_pos += toRead; } @@ -133,7 +147,9 @@ int ZstdCompress::Compress(Bytef *data, uLong dlen) do { ZSTD_outBuffer output = {buffOut, buffOutSize, 0}; size_t const remaining = ZSTD_compressStream2(cctx, &output, &input, mode); - memmove_s(zdata + dst_pos, zdlen, (Bytef *)buffOut, output.pos); + if (memmove_s(zdata + dst_pos, zdlen, (Bytef *)buffOut, output.pos) != 0) { + return -1; + } dst_pos += output.pos; zdlen = dst_pos; finished = flag ? (remaining == 0) : (input.pos == input.size); diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 99f2ac9..633f271 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -45,7 +45,7 @@ constexpr int MAX_DATA_LEN = 2048; string g_logPersisterDir = "/data/misc/logd/"; constexpr int DEFAULT_LOG_LEVEL = 1< persisterJobIds; +list g_persisterJobId; static char g_tempBuffer[MAX_DATA_LEN] = {0}; inline void SetMsgHead(MessageHeader* msgHeader, uint8_t msgCmd, uint16_t msgLen) { @@ -135,7 +135,7 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade if (pLogPersistStartRst->result == RET_FAIL) { persister.reset(); } else { - persisterJobIds.push_back(pLogPersistStartRst->jobId); + g_persisterJobId.push_back(pLogPersistStartRst->jobId); persister->Start(); buffer->AddLogReader(weak_ptr(persister)); } @@ -154,7 +154,6 @@ void HandlePersistDeleteRequest(char* reqMsg, std::shared_ptr logRead = reinterpret_cast(msgToSend); LogPersistStopResult* pLogPersistStopRst = reinterpret_cast(&pLogPersistStopRsp->logPersistStopRst); - uint32_t recvMsgLen = 0; uint32_t msgNum = 0; uint16_t msgLen = pLogPersistStopReq->msgHeader.msgLen; uint16_t sendMsgLen = 0; @@ -163,11 +162,10 @@ void HandlePersistDeleteRequest(char* reqMsg, std::shared_ptr logRead if (msgLen > sizeof(LogPersistStopMsg) * LOG_TYPE_MAX) { return; } - if(pLogPersistStopMsg && pLogPersistStopMsg->jobId == 0xffffffff) { - for (list::iterator iter = persisterJobIds.begin();iter != persisterJobIds.end(); ++iter) { + if (pLogPersistStopMsg && pLogPersistStopMsg->jobId == 0xffffffff) { + for (list::iterator iter = g_persisterJobId.begin(); iter != g_persisterJobId.end(); ++iter) { rst = LogPersister::Kill(*iter); - persisterJobIds.pop_front(); - cout << "*iter" << hex << *iter << endl; + g_persisterJobId.pop_front(); if (pLogPersistStopRst) { pLogPersistStopRst->jobId = *iter; pLogPersistStopRst->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; @@ -176,11 +174,15 @@ void HandlePersistDeleteRequest(char* reqMsg, std::shared_ptr logRead } } } else { + uint32_t recvMsgLen = 0; while (pLogPersistStopMsg && recvMsgLen < msgLen) { rst = LogPersister::Kill(pLogPersistStopMsg->jobId); if (pLogPersistStopRst) { pLogPersistStopRst->jobId = pLogPersistStopMsg->jobId; pLogPersistStopRst->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; + if (pLogPersistStopRst->result == RET_SUCCESS) { + g_persisterJobId.remove(pLogPersistStopMsg->jobId); + } pLogPersistStopRst++; } pLogPersistStopMsg++; diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 01c8936..7625ab0 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -120,7 +120,7 @@ uint16_t GetCompressAlg(const std::string& pressAlg) return COMPRESS_TYPE_ZLIB; } -uint16_t GetLogLevel(const string& logLevelStr) +uint16_t GetLogLevel(const std::string& logLevelStr, std::string& logLevel) { if (logLevelStr == "debug" || logLevelStr == "DEBUG" || logLevelStr == "d" || logLevelStr == "D") { logLevel = "D"; diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 358d5a1..e79ba70 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -299,7 +299,7 @@ int32_t ControlCmdResult(const char* message) LogPersistQueryResult* pLogPersistQueryRst = (LogPersistQueryResult*)&pLogPersistQueryRsp->logPersistQueryRst; while (pLogPersistQueryRst && resultLen < msgLen) { - if (pLogPersistQueryRst->result == RET_FAIL) {\ + if (pLogPersistQueryRst->result == RET_FAIL) { outputStr = "Persist task [logtype:"; outputStr += GetLogTypeStr(pLogPersistQueryRst->logType); outputStr += "] query failed\n"; @@ -382,7 +382,7 @@ bool HilogMatchByRegex(string context, string regExpArg) } } -void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* data, HilogArgs* context, +void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* data, HilogArgs* context, vector& tailBuffer) { if (data->sendId == SENDIDN) { @@ -420,7 +420,6 @@ void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* data, HilogArgs* showBuffer.tv_nsec = data->tv_nsec; int offset = data->tag_len; - char *dataBegin = data->data + offset; char *dataPos = data->data + offset; while (*dataPos != 0) { diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index 1d291ad..d7d378a 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -67,7 +67,7 @@ static void Helper() " -D , --domain=\n" " specify the domain, no more than %d.\n" " -T , --Tag=\n" - " specify the tag.\n" + " specify the tag, no more than %d.\n" " -a , --head= show n lines log on head.\n" " -z , --tail= show n lines log on tail.\n" " -G , --buffer-size=\n" -- Gitee From 78d17aaf092254ea91fc1a84892ca402ed6f0066 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Thu, 15 Jul 2021 15:00:54 +0000 Subject: [PATCH 08/24] reconfiguration persistor Signed-off-by: aajwy <13051180828@163.com> --- frameworks/native/include/hilogtool_msg.h | 4 +- services/hilogd/include/log_compress.h | 41 +++++-- services/hilogd/include/log_persister.h | 12 +- .../hilogd/include/log_persister_rotator.h | 2 +- services/hilogd/log_compress.cpp | 65 +++++------ services/hilogd/log_persister.cpp | 109 +++++++++--------- services/hilogd/log_persister_rotator.cpp | 51 +++----- services/hilogd/log_querier.cpp | 51 ++++---- services/hilogtool/log_controller.cpp | 11 +- 9 files changed, 169 insertions(+), 177 deletions(-) diff --git a/frameworks/native/include/hilogtool_msg.h b/frameworks/native/include/hilogtool_msg.h index bda517d..bf3b9f9 100644 --- a/frameworks/native/include/hilogtool_msg.h +++ b/frameworks/native/include/hilogtool_msg.h @@ -23,7 +23,7 @@ #include "hilog_common.h" #define FILE_PATH_MAX_LEN 100 - +#define JOB_ID_ALL 0xffffffff typedef enum { LOG_QUERY_REQUEST = 0x01, LOG_QUERY_RESPONSE, @@ -71,7 +71,7 @@ typedef enum { } PersisterResponse; typedef enum { - COMPRESS_TYPE_OFF = 0, + COMPRESS_TYPE_NONE = 0, COMPRESS_TYPE_ZSTD, COMPRESS_TYPE_ZLIB, } CompressAlg; diff --git a/services/hilogd/include/log_compress.h b/services/hilogd/include/log_compress.h index 63b705c..21491a1 100644 --- a/services/hilogd/include/log_compress.h +++ b/services/hilogd/include/log_compress.h @@ -14,34 +14,53 @@ */ #ifndef HILOG_COMPRESS_H #define HILOG_COMPRESS_H - +#include #ifdef USING_ZSTD_COMPRESS #define ZSTD_STATIC_LINKING_ONLY #include "include/common.h" #include "zstd.h" #endif #include -#define CHUNK 16384 namespace OHOS { namespace HiviewDFX { -class LogCompress { + +const uint32_t COMPRESS_BUFFER_SIZE = 64 * 1024; +const uint16_t CHUNK = 16384; +class LogCompress +{ public: LogCompress(); - virtual ~LogCompress(); - virtual int Compress(Bytef *data, uLong dlen) = 0; + virtual ~LogCompress() = default; + virtual int Compress(char (&src)[], uint32_t &inLen,char (&dst)[], uint32_t &outLen) = 0; uLong zdlen = 0; - unsigned char *zdata = nullptr; + unsigned char *zdata; + char buffIn[CHUNK]; + char buffOut[CHUNK]; +}; + +class NoneCompress: public LogCompress { +public: + int Compress(char (&src)[], uint32_t& inLen,char (&dst)[], uint32_t &outLen); }; -class ZlibCompress : public LogCompress { +class ZlibCompress: public LogCompress { + public: - int Compress(Bytef *data, uLong dlen); + int Compress(char (&src)[], uint32_t &inLen,char (&dst)[], uint32_t &outLen); +private: + z_stream c_stream; }; -class ZstdCompress : public LogCompress { +class ZstdCompress: public LogCompress { + public: - int Compress(Bytef *data, uLong dlen); + int Compress(char (&src)[], uint32_t &inLen,char (&dst)[], uint32_t &outLen); +private: +#ifdef USING_ZSTD_COMPRESS + ZSTD_CCtx* cctx; +#endif }; } } -#endif /* HILOG_COMPRESS_H */ +#endif + /* HILOG_COMPRESS_H */ diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index 82c0259..fd42f94 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -30,15 +30,14 @@ namespace OHOS { namespace HiviewDFX { using namespace std; typedef struct { - uint16_t offset; + uint32_t offset; char content[]; } LogPersisterBuffer; -const uint16_t MAX_PERSISTER_BUFFER_SIZE = 4096; -const uint32_t COMPRESS_BUFFER_SIZE = 64 * 1024; +const uint32_t MAX_PERSISTER_BUFFER_SIZE = 64 * 1024; class LogPersister : public LogReader { public: - LogPersister(uint32_t id, std::string path, uint16_t compressAlg, int sleepTime, + LogPersister(uint32_t id, std::string path, uint32_t fileSize, uint16_t compressAlg, int sleepTime, LogPersisterRotator *rotator, HilogBuffer *buffer); ~LogPersister(); void SetBufferOffset(int off); @@ -50,6 +49,7 @@ public: static int Query(uint16_t logType, std::list &results); int Init(); + int InitCompress(); void Start(); bool Identify(uint32_t id); void FillInfo(LogPersistQueryResult *response); @@ -61,6 +61,7 @@ public: private: uint32_t id; std::string path; + uint32_t fileSize; std::string mmapPath; uint16_t compressAlg; int sleepTime; @@ -75,9 +76,10 @@ private: bool isExited(); FILE *fdinfo; int fd = -1; - LogCompress *LogCompress; + LogCompress *compressor; LogPersisterBuffer *compressBuffer; list persistList; + uint32_t beforeCompressLogSize; }; 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 4d203f2..46ff6d9 100644 --- a/services/hilogd/include/log_persister_rotator.h +++ b/services/hilogd/include/log_persister_rotator.h @@ -22,7 +22,7 @@ class LogPersisterRotator { public: LogPersisterRotator(std::string path, uint32_t fileSize, uint32_t fileNum, std::string suffix = ""); virtual ~LogPersisterRotator(){}; - int Input(const char *buf, int length); + int Input(const char *buf, uint32_t); void FillInfo(uint32_t *size, uint32_t *num); void FinishInput(); diff --git a/services/hilogd/log_compress.cpp b/services/hilogd/log_compress.cpp index 90b0436..95269e2 100644 --- a/services/hilogd/log_compress.cpp +++ b/services/hilogd/log_compress.cpp @@ -26,47 +26,46 @@ LogCompress::LogCompress() { } -LogCompress::~LogCompress() +int NoneCompress::Compress(char (&src)[], uint32_t& inLen,char (&dst)[], uint32_t &outLen) { - delete zdata; + outLen = inLen; + if( memcpy_s(dst, outLen, src, inLen) != 0) { + return -1; + } + return 0; } -int ZlibCompress::Compress(Bytef *data, uLong dlen) +int ZlibCompress::Compress(char (&src)[], uint32_t &inLen,char (&dst)[], uint32_t &outLen) { - zdlen = compressBound(dlen); + zdlen = compressBound(inLen); zdata = new unsigned char [zdlen]; if (zdata == NULL) { cout << "no enough memory!" << endl; return -1; } - void* const buffIn = malloc(CHUNK); - void* const buffOut = malloc(CHUNK); size_t const toRead = CHUNK; auto src_pos = 0; auto dst_pos = 0; - size_t read = dlen; + size_t read = inLen; int flush = 0; - z_stream c_stream; c_stream.zalloc = Z_NULL; c_stream.zfree = Z_NULL; c_stream.opaque = Z_NULL; if (deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK) { - free(buffIn); - free(buffOut); return -1; } do { bool flag = read - src_pos < toRead; if (flag) { memset_s(buffIn, CHUNK, 0, CHUNK); - if (memmove_s(buffIn, CHUNK, data + src_pos, read - src_pos) != 0) { + if (memmove_s(buffIn, CHUNK, src + src_pos, read - src_pos) != 0) { return -1; } c_stream.avail_in = read - src_pos; src_pos += read - src_pos; } else { memset_s(buffIn, CHUNK, 0, CHUNK); - if (memmove_s(buffIn, CHUNK, data + src_pos, toRead) != 0) { + if (memmove_s(buffIn, CHUNK, src + src_pos, toRead) != 0) { return -1; }; src_pos += toRead; @@ -92,62 +91,53 @@ int ZlibCompress::Compress(Bytef *data, uLong dlen) } while (flush != Z_FINISH); /* clean up and return */ (void)deflateEnd(&c_stream); - free(buffIn); - free(buffOut); + memcpy_s(dst + outLen, COMPRESS_BUFFER_SIZE - outLen, zdata, zdlen); + outLen += zdlen; + delete zdata; return 0; } -int ZstdCompress::Compress(Bytef *data, uLong dlen) +int ZstdCompress::Compress(char (&src)[], uint32_t &inLen,char (&dst)[], uint32_t &outLen) { #ifdef USING_ZSTD_COMPRESS - zdlen = ZSTD_CStreamOutSize(); + zdlen = ZSTD_CStreamOutSize(); zdata = new unsigned char [zdlen]; if (zdata == NULL) { cout << "no enough memory!" << endl; return -1; } - size_t const buffInSize = ZSTD_CStreamInSize(); - void* const buffIn = malloc(buffInSize); - size_t const buffOutSize = ZSTD_CStreamOutSize(); - void* const buffOut = malloc(buffOutSize); ZSTD_EndDirective mode; int compressionlevel = 1; - ZSTD_CCtx* const cctx = ZSTD_createCCtx(); + cctx = ZSTD_createCCtx(); if (cctx == NULL) { cout << "ZSTD_createCCtx() failed!" << endl; - free(buffIn); - free(buffOut); return -1; } ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, compressionlevel); - size_t const toRead = buffInSize; + size_t const toRead = CHUNK; auto src_pos = 0; auto dst_pos = 0; - size_t read = dlen; + size_t read = inLen; ZSTD_inBuffer input; do { bool flag = read - src_pos < toRead; if (flag) { - memset_s(buffIn, buffInSize, 0, buffInSize); - if (memmove_s(buffIn, buffInSize, data + src_pos, read - src_pos) != 0) { - return -1; - } + memset_s(buffIn, CHUNK, 0, CHUNK); + memmove(buffIn, src + src_pos, read - src_pos); input = {buffIn, read - src_pos, 0}; src_pos += read - src_pos; } else { - memset_s(buffIn, buffInSize, 0, buffInSize); - if (memmove_s(buffIn, buffInSize, data + src_pos, toRead) != 0) { - return -1; - } + memset_s(buffIn, CHUNK, 0, CHUNK); + memmove(buffIn, src + src_pos, toRead); input = {buffIn, toRead, 0}; src_pos += toRead; } mode = flag ? ZSTD_e_end : ZSTD_e_continue; int finished; do { - ZSTD_outBuffer output = {buffOut, buffOutSize, 0}; + ZSTD_outBuffer output = {buffOut, CHUNK, 0}; size_t const remaining = ZSTD_compressStream2(cctx, &output, &input, mode); - if (memmove_s(zdata + dst_pos, zdlen, (Bytef *)buffOut, output.pos) != 0) { + if(memmove_s(zdata + dst_pos, zdlen, (Bytef *)buffOut, output.pos) != 0) { return -1; } dst_pos += output.pos; @@ -155,9 +145,10 @@ int ZstdCompress::Compress(Bytef *data, uLong dlen) finished = flag ? (remaining == 0) : (input.pos == input.size); } while (!finished); } while (mode != ZSTD_e_end); - free(buffIn); - free(buffOut); ZSTD_freeCCtx(cctx); + memcpy_s(dst + outLen, COMPRESS_BUFFER_SIZE - outLen, zdata, zdlen); + outLen += zdlen; + delete zdata; #endif // #ifdef USING_ZSTD_COMPRESS return 0; } diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index a243cdb..2ecc00f 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -49,26 +49,49 @@ static std::mutex g_listMutex; (x) = nullptr; \ } while (0) -LogPersister::LogPersister(uint32_t id, string path, uint16_t compressAlg, int sleepTime, +LogPersister::LogPersister(uint32_t id, string path, uint32_t fileSize, uint16_t compressAlg, int sleepTime, LogPersisterRotator *rotator, HilogBuffer *_buffer) - : id(id), path(path), compressAlg(compressAlg), + : id(id), path(path), fileSize(fileSize), compressAlg(compressAlg), sleepTime(sleepTime), rotator(rotator) { toExit = false; hasExited = false; hilogBuffer = _buffer; - LogCompress = nullptr; + compressor = nullptr; fdinfo = nullptr; buffer = nullptr; + beforeCompressLogSize = 0; } LogPersister::~LogPersister() { SAFE_DELETE(rotator); - SAFE_DELETE(LogCompress); + SAFE_DELETE(compressor); SAFE_DELETE(compressBuffer); } +int LogPersister::InitCompress() { + compressBuffer = new LogPersisterBuffer[MAX_PERSISTER_BUFFER_SIZE]; + if (compressBuffer == NULL) { + return RET_FAIL; + } + switch (compressAlg) + { + case COMPRESS_TYPE_NONE: + compressor = new NoneCompress(); + break; + case COMPRESS_TYPE_ZLIB: + compressor = new ZlibCompress(); + break; + case COMPRESS_TYPE_ZSTD: + compressor = new ZstdCompress(); + break; + default: + break; + } + return RET_SUCCESS; +} + int LogPersister::Init() { int nPos = path.find_last_of('/'); @@ -92,7 +115,9 @@ int LogPersister::Init() if (hit) { return RET_FAIL; } - + if (InitCompress() == RET_FAIL) { + return RET_FAIL; + } fd = open(mmapPath.c_str(), O_RDWR | O_CREAT | O_EXCL, 0); bool restore = false; if (fd <= 0) { @@ -144,7 +169,7 @@ int LogPersister::Init() cout << "Recovered persister, Offset=" << moffset << endl; #endif SetBufferOffset(moffset); - WriteFile(); + //WriteFile(); } else { SetBufferOffset(0); } @@ -228,7 +253,7 @@ bool LogPersister::writeUnCompressedBuffer(HilogData *data) string header = persistList.front(); uint16_t headerLen = header.length(); uint16_t size = headerLen + 1; - uint16_t orig_offset = buffer->offset; + uint32_t orig_offset = buffer->offset; int r = 0; if (buffer->offset + size > MAX_PERSISTER_BUFFER_SIZE) return false; @@ -252,49 +277,12 @@ int LogPersister::WriteData(HilogData *data) return -1; if (writeUnCompressedBuffer(data)) return 0; - switch (compressAlg) { - case COMPRESS_TYPE_OFF: - WriteFile(); - break; - case COMPRESS_TYPE_ZLIB: { - LogCompress = new ZlibCompress(); - if (LogCompress->Compress((Bytef *)buffer->content, buffer->offset) != 0) { - cout << "COMPRESS_TYPE_ZLIB Error" << endl; - return -1; - }; - memcpy_s(compressBuffer->content + compressBuffer->offset, COMPRESS_BUFFER_SIZE, - LogCompress->zdata, LogCompress->zdlen); - compressBuffer->offset += LogCompress->zdlen; - if (compressBuffer->offset >= COMPRESS_BUFFER_SIZE * 2 / 3) { - rotator->Input((char *)compressBuffer->content, compressBuffer->offset); - rotator->FinishInput(); - compressBuffer->offset = 0; - } - SetBufferOffset(0); - } - break; -#ifdef USING_ZSTD_COMPRESS - case COMPRESS_TYPE_ZSTD: { - LogCompress = new ZstdCompress(); - if (LogCompress->Compress((Bytef *)buffer->content, buffer->offset) != 0) { - cout << "COMPRESS_TYPE_ZSTD Error" << endl; - return -1; - }; - memcpy_s(compressBuffer->content + compressBuffer->offset, COMPRESS_BUFFER_SIZE, - LogCompress->zdata, LogCompress->zdlen); - compressBuffer->offset += LogCompress->zdlen; - if (compressBuffer->offset >= COMPRESS_BUFFER_SIZE * 2 / 3) { - rotator->Input((char *)compressBuffer->content, compressBuffer->offset); - rotator->FinishInput(); - compressBuffer->offset = 0; - } - SetBufferOffset(0); - } - break; -#endif // #ifdef USING_ZSTD_COMPRESS - default: - break; - } + if (compressor->Compress(buffer->content, buffer->offset, + compressBuffer->content, compressBuffer->offset) != 0) { + cout << "COMPRESS Error" << endl; + return -1; + }; + WriteFile(); return writeUnCompressedBuffer(data) ? 0 : -1; } @@ -308,11 +296,21 @@ void LogPersister::Start() inline void LogPersister::WriteFile() { - if (buffer->offset == 0) return; - if (compressAlg == 0) { - rotator->Input(buffer->content, buffer->offset); + if (buffer->offset == 0) + return; + rotator->Input((char *)compressBuffer->content, compressBuffer->offset); + beforeCompressLogSize += buffer->offset; +#ifdef DEBUG + cout << buffer->offset << endl; + cout << compressBuffer->offset << endl; + cout << "beforeCompressLogSize" << beforeCompressLogSize < fileSize) { + beforeCompressLogSize = 0; + rotator->FinishInput(); } - SetBufferOffset(0); + compressBuffer->offset = 0; + SetBufferOffset(0); } int LogPersister::ThreadFunc() @@ -333,7 +331,6 @@ int LogPersister::ThreadFunc() WriteFile(); } } - cout << "running! " << compressAlg << endl; } WriteFile(); { @@ -379,7 +376,9 @@ int LogPersister::Kill(const uint32_t id) std::lock_guard guard(g_listMutex); for (auto it = logPersisters.begin(); it != logPersisters.end(); ) { if ((*it)->Identify(id)) { +#ifdef DEBUG cout << "find a persister" << endl; +#endif (*it)->Exit(); it = logPersisters.erase(it); found = true; diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index 85845d8..89aef7f 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -23,44 +23,26 @@ namespace HiviewDFX { using namespace std; LogPersisterRotator::LogPersisterRotator(string path, uint32_t fileSize, uint32_t fileNum, string suffix) - : fileNum(fileNum - 1), fileSize(fileSize), fileName(path), fileSuffix(suffix) + : fileNum(fileNum), fileSize(fileSize), fileName(path), fileSuffix(suffix) { index = 0; leftSize = 0; needRotate = true; } -int LogPersisterRotator::Input(const char *buf, int length) +int LogPersisterRotator::Input(const char *buf, uint32_t length) { cout << __func__ << " " << fileName << " " << index - << " " << length << " need: " << needRotate << endl; - if (needRotate) { - output.close(); - Rotate(); - needRotate = false; - } - if (length <= 0 || buf == nullptr) return -1; + << " " << length << " need: " << needRotate << endl; + if (needRotate) { + output.close(); + Rotate(); + needRotate = false; + } - unsigned int offset = 0; - while (length >= leftSize) { - int pos = leftSize - 1; - while (pos >= 0 && buf[offset + pos] != '\n') - pos--; - int realSize = pos + 1; - if (realSize == 0 && (uint32_t)leftSize == fileSize) { - cout << "ERROR! some log line is too long" << endl; - break; - } else { - output.write(buf + offset, realSize); - offset += realSize; - length -= realSize; - output.close(); - } - Rotate(); - } - output.write(buf + offset, length); - leftSize -= length; - output.flush(); + if (length <= 0 || buf == nullptr) return -1; + uint32_t offset = 0; + output.write(buf + offset, length); return 0; } @@ -72,7 +54,7 @@ void LogPersisterRotator::InternalRotate() ss << 0 << fileSuffix; remove(ss.str().c_str()); - for (uint32_t i = 1; i <= fileNum; ++i) { + for (uint32_t i = 1; i < fileNum; ++i) { ss.seekp(pos); ss << (i - 1) << fileSuffix; string newName = ss.str(); @@ -83,13 +65,12 @@ void LogPersisterRotator::InternalRotate() rename(oldName.c_str(), newName.c_str()); } output.open(ss.str(), ios::out); - leftSize = fileSize; } void LogPersisterRotator::Rotate() { cout << __func__ << endl; - if (index == (int)fileNum) { + if (index == (int)(fileNum - 1)) { InternalRotate(); } else { index += 1; @@ -97,7 +78,6 @@ void LogPersisterRotator::Rotate() ss << fileName << "." << index << fileSuffix; cout << "THE FILE NAME !!!!!!! " << ss.str() << endl; output.open(ss.str(), ios::app); - leftSize = fileSize; } } @@ -107,9 +87,8 @@ void LogPersisterRotator::FillInfo(uint32_t *size, uint32_t *num) *num = fileNum; } -void LogPersisterRotator::FinishInput() -{ - needRotate = true; +void LogPersisterRotator::FinishInput() { + needRotate = true; } } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 633f271..b901449 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -44,8 +44,8 @@ using namespace std; constexpr int MAX_DATA_LEN = 2048; string g_logPersisterDir = "/data/misc/logd/"; constexpr int DEFAULT_LOG_LEVEL = 1< g_persisterJobId; static char g_tempBuffer[MAX_DATA_LEN] = {0}; inline void SetMsgHead(MessageHeader* msgHeader, uint8_t msgCmd, uint16_t msgLen) { @@ -126,6 +126,7 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade std::shared_ptr persister = make_shared( pLogPersistStartMsg->jobId, pLogPersistStartMsg->filePath, + pLogPersistStartMsg->fileSize, pLogPersistStartMsg->compressAlg, SLEEP_TIME, rotator, buffer); pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; @@ -135,7 +136,6 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade if (pLogPersistStartRst->result == RET_FAIL) { persister.reset(); } else { - g_persisterJobId.push_back(pLogPersistStartRst->jobId); persister->Start(); buffer->AddLogReader(weak_ptr(persister)); } @@ -154,6 +154,7 @@ void HandlePersistDeleteRequest(char* reqMsg, std::shared_ptr logRead = reinterpret_cast(msgToSend); LogPersistStopResult* pLogPersistStopRst = reinterpret_cast(&pLogPersistStopRsp->logPersistStopRst); + uint32_t recvMsgLen = 0; uint32_t msgNum = 0; uint16_t msgLen = pLogPersistStopReq->msgHeader.msgLen; uint16_t sendMsgLen = 0; @@ -162,33 +163,29 @@ void HandlePersistDeleteRequest(char* reqMsg, std::shared_ptr logRead if (msgLen > sizeof(LogPersistStopMsg) * LOG_TYPE_MAX) { return; } - if (pLogPersistStopMsg && pLogPersistStopMsg->jobId == 0xffffffff) { - for (list::iterator iter = g_persisterJobId.begin(); iter != g_persisterJobId.end(); ++iter) { - rst = LogPersister::Kill(*iter); - g_persisterJobId.pop_front(); - if (pLogPersistStopRst) { - pLogPersistStopRst->jobId = *iter; - pLogPersistStopRst->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; - pLogPersistStopRst++; - msgNum++; - } - } - } else { - uint32_t recvMsgLen = 0; - while (pLogPersistStopMsg && recvMsgLen < msgLen) { - rst = LogPersister::Kill(pLogPersistStopMsg->jobId); - if (pLogPersistStopRst) { - pLogPersistStopRst->jobId = pLogPersistStopMsg->jobId; - pLogPersistStopRst->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; - if (pLogPersistStopRst->result == RET_SUCCESS) { - g_persisterJobId.remove(pLogPersistStopMsg->jobId); + list resultList; + list::iterator it; + rst = LogPersister::Query(DEFAULT_LOG_TYPE, resultList); + if (pLogPersistStopMsg && recvMsgLen < msgLen) { + for (it = resultList.begin(); it != resultList.end(); ++it) { + if (pLogPersistStopMsg->jobId != JOB_ID_ALL) { + rst = LogPersister::Kill(pLogPersistStopMsg->jobId); + if (pLogPersistStopRst) { + pLogPersistStopRst->jobId = pLogPersistStopMsg->jobId; + pLogPersistStopRst->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; + pLogPersistStopRst++; + msgNum++; + } + break; + } + rst = LogPersister::Kill((*it).jobId); + if (pLogPersistStopRst) { + pLogPersistStopRst->jobId = (*it).jobId; + pLogPersistStopRst->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; + pLogPersistStopRst++; + msgNum++; } - pLogPersistStopRst++; } - pLogPersistStopMsg++; - recvMsgLen += sizeof(LogPersistStopMsg); - msgNum++; - } } sendMsgLen = msgNum * sizeof(LogPersistStopResult); SetMsgHead(&pLogPersistStopRsp->msgHeader, MC_RSP_LOG_PERSIST_STOP, sendMsgLen); diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 7625ab0..3dc18e2 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -38,6 +38,7 @@ const int MSG_MAX_LEN = 2048; const int LOG_PERSIST_FILE_SIZE = 4 * ONE_MB; const int LOG_PERSIST_FILE_NUM = 10; const uint32_t DEFAULT_JOBID = 1; +const int LOG_PERSIST_MIN_FILE_SIZE = 16 * ONE_KB; void SetMsgHead(MessageHeader* msgHeader, const uint8_t msgCmd, const uint16_t msgLen) { if (!msgHeader) { @@ -111,7 +112,7 @@ uint64_t GetBuffSize(const string& buffSizeStr) uint16_t GetCompressAlg(const std::string& pressAlg) { if (pressAlg == "none") { - return COMPRESS_TYPE_OFF; + return COMPRESS_TYPE_NONE; } else if (pressAlg == "zlib") { return COMPRESS_TYPE_ZLIB; } else if (pressAlg == "zstd") { @@ -137,7 +138,7 @@ uint16_t GetLogLevel(const std::string& logLevelStr, std::string& logLevel) } else if (logLevelStr == "fatal" || logLevelStr == "FATAL" || logLevelStr == "f" || logLevelStr == "F") { logLevel = "F"; return LOG_FATAL; - } + } return 0xffff; } @@ -415,6 +416,10 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi GetCompressAlg(logPersistParam->compressAlgStr); pLogPersistStartMsg->fileSize = (logPersistParam->fileSizeStr == "") ? fileSizeDefault : GetBuffSize( logPersistParam->fileSizeStr); + if (pLogPersistStartMsg->fileSize < LOG_PERSIST_MIN_FILE_SIZE) { + std::cout << "Persist log file size less than min size" << std::endl; + return RET_FAIL; + } pLogPersistStartMsg->fileNum = (logPersistParam->fileNumStr == "") ? fileNumDefault : stoi(logPersistParam->fileNumStr); if (logPersistParam->fileNameStr.size() > FILE_PATH_MAX_LEN) { @@ -434,7 +439,7 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi LogPersistStopMsg* pLogPersistStopMsg = reinterpret_cast(&pLogPersistStopReq->logPersistStopMsg); if (logPersistParam->jobIdStr == "") { - pLogPersistStopMsg->jobId = 0xffffffff; + pLogPersistStopMsg->jobId = JOB_ID_ALL; SetMsgHead(&pLogPersistStopReq->msgHeader, msgCmd, sizeof(LogPersistStopMsg)); controller.WriteAll(msgToSend, sizeof(LogPersistStopMsg) + sizeof(MessageHeader)); break; -- Gitee From 653ff636f01d9c5e1fcd302587d8cb2d54ae7396 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Fri, 16 Jul 2021 01:56:54 +0000 Subject: [PATCH 09/24] code format Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/include/log_compress.h | 9 ++--- services/hilogd/log_compress.cpp | 46 ++++++++++++----------- services/hilogd/log_persister.cpp | 38 ++++++++----------- services/hilogd/log_persister_rotator.cpp | 20 +++++----- services/hilogd/log_querier.cpp | 2 +- services/hilogtool/log_controller.cpp | 9 ++--- 6 files changed, 59 insertions(+), 65 deletions(-) diff --git a/services/hilogd/include/log_compress.h b/services/hilogd/include/log_compress.h index 21491a1..2ecfc73 100644 --- a/services/hilogd/include/log_compress.h +++ b/services/hilogd/include/log_compress.h @@ -23,15 +23,13 @@ #include namespace OHOS { namespace HiviewDFX { - const uint32_t COMPRESS_BUFFER_SIZE = 64 * 1024; const uint16_t CHUNK = 16384; -class LogCompress -{ +class LogCompress { public: LogCompress(); virtual ~LogCompress() = default; - virtual int Compress(char (&src)[], uint32_t &inLen,char (&dst)[], uint32_t &outLen) = 0; + virtual int Compress(char (&src)[], uint32_t &inLen, char (&dst)[], uint32_t &outLen) = 0; uLong zdlen = 0; unsigned char *zdata; char buffIn[CHUNK]; @@ -44,11 +42,10 @@ public: }; class ZlibCompress: public LogCompress { - public: int Compress(char (&src)[], uint32_t &inLen,char (&dst)[], uint32_t &outLen); private: - z_stream c_stream; + z_stream cStream; }; class ZstdCompress: public LogCompress { diff --git a/services/hilogd/log_compress.cpp b/services/hilogd/log_compress.cpp index 95269e2..6c4c0c1 100644 --- a/services/hilogd/log_compress.cpp +++ b/services/hilogd/log_compress.cpp @@ -26,16 +26,16 @@ LogCompress::LogCompress() { } -int NoneCompress::Compress(char (&src)[], uint32_t& inLen,char (&dst)[], uint32_t &outLen) +int NoneCompress::Compress(char (&src)[], uint32_t& inLen, char (&dst)[], uint32_t &outLen) { outLen = inLen; - if( memcpy_s(dst, outLen, src, inLen) != 0) { + if (memcpy_s(dst, outLen, src, inLen) != 0) { return -1; } return 0; } -int ZlibCompress::Compress(char (&src)[], uint32_t &inLen,char (&dst)[], uint32_t &outLen) +int ZlibCompress::Compress(char (&src)[], uint32_t &inLen, char (&dst)[], uint32_t &outLen) { zdlen = compressBound(inLen); zdata = new unsigned char [zdlen]; @@ -48,10 +48,10 @@ int ZlibCompress::Compress(char (&src)[], uint32_t &inLen,char (&dst)[], uint32_ auto dst_pos = 0; size_t read = inLen; int flush = 0; - c_stream.zalloc = Z_NULL; - c_stream.zfree = Z_NULL; - c_stream.opaque = Z_NULL; - if (deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK) { + cStream.zalloc = Z_NULL; + cStream.zfree = Z_NULL; + cStream.opaque = Z_NULL; + if (deflateInit2(&cStream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK) { return -1; } do { @@ -61,7 +61,7 @@ int ZlibCompress::Compress(char (&src)[], uint32_t &inLen,char (&dst)[], uint32_ if (memmove_s(buffIn, CHUNK, src + src_pos, read - src_pos) != 0) { return -1; } - c_stream.avail_in = read - src_pos; + cStream.avail_in = read - src_pos; src_pos += read - src_pos; } else { memset_s(buffIn, CHUNK, 0, CHUNK); @@ -69,38 +69,38 @@ int ZlibCompress::Compress(char (&src)[], uint32_t &inLen,char (&dst)[], uint32_ return -1; }; src_pos += toRead; - c_stream.avail_in = toRead; + cStream.avail_in = toRead; } flush = flag ? Z_FINISH : Z_NO_FLUSH; - c_stream.next_in = (Bytef *)buffIn; + cStream.next_in = (Bytef *)buffIn; /* run deflate() on input until output buffer not full, finish compression if all of source has been read in */ do { - c_stream.avail_out = CHUNK; - c_stream.next_out = (Bytef *)buffOut; - if (deflate(&c_stream, flush) == Z_STREAM_ERROR) { + cStream.avail_out = CHUNK; + cStream.next_out = (Bytef *)buffOut; + if (deflate(&cStream, flush) == Z_STREAM_ERROR) { return -1; } - unsigned have = CHUNK - c_stream.avail_out; + unsigned have = CHUNK - cStream.avail_out; if (memmove_s(zdata + dst_pos, CHUNK, buffOut, have) != 0) { return -1; } dst_pos += have; zdlen = dst_pos; - } while (c_stream.avail_out == 0); + } while (cStream.avail_out == 0); } while (flush != Z_FINISH); /* clean up and return */ - (void)deflateEnd(&c_stream); + (void)deflateEnd(&cStream); memcpy_s(dst + outLen, COMPRESS_BUFFER_SIZE - outLen, zdata, zdlen); outLen += zdlen; delete zdata; return 0; } -int ZstdCompress::Compress(char (&src)[], uint32_t &inLen,char (&dst)[], uint32_t &outLen) +int ZstdCompress::Compress(char (&src)[], uint32_t &inLen, char (&dst)[], uint32_t &outLen) { #ifdef USING_ZSTD_COMPRESS - zdlen = ZSTD_CStreamOutSize(); + zdlen = ZSTD_CStreamOutSize(); zdata = new unsigned char [zdlen]; if (zdata == NULL) { cout << "no enough memory!" << endl; @@ -123,12 +123,16 @@ int ZstdCompress::Compress(char (&src)[], uint32_t &inLen,char (&dst)[], uint32_ bool flag = read - src_pos < toRead; if (flag) { memset_s(buffIn, CHUNK, 0, CHUNK); - memmove(buffIn, src + src_pos, read - src_pos); + if (memmove_s(buffIn, CHUNK, src + src_pos, read - src_pos) != 0) { + return -1; + } input = {buffIn, read - src_pos, 0}; src_pos += read - src_pos; } else { memset_s(buffIn, CHUNK, 0, CHUNK); - memmove(buffIn, src + src_pos, toRead); + if (memmove_s(buffIn, CHUNK, src + src_pos, toRead) != 0) { + return -1; + } input = {buffIn, toRead, 0}; src_pos += toRead; } @@ -137,7 +141,7 @@ int ZstdCompress::Compress(char (&src)[], uint32_t &inLen,char (&dst)[], uint32_ do { ZSTD_outBuffer output = {buffOut, CHUNK, 0}; size_t const remaining = ZSTD_compressStream2(cctx, &output, &input, mode); - if(memmove_s(zdata + dst_pos, zdlen, (Bytef *)buffOut, output.pos) != 0) { + if (memmove_s(zdata + dst_pos, zdlen, (Bytef *)buffOut, output.pos) != 0) { return -1; } dst_pos += output.pos; diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 2ecc00f..faab478 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -75,20 +75,19 @@ int LogPersister::InitCompress() { if (compressBuffer == NULL) { return RET_FAIL; } - switch (compressAlg) - { - case COMPRESS_TYPE_NONE: - compressor = new NoneCompress(); - break; - case COMPRESS_TYPE_ZLIB: - compressor = new ZlibCompress(); - break; - case COMPRESS_TYPE_ZSTD: - compressor = new ZstdCompress(); - break; - default: - break; - } + switch (compressAlg) { + case COMPRESS_TYPE_NONE: + compressor = new NoneCompress(); + break; + case COMPRESS_TYPE_ZLIB: + compressor = new ZlibCompress(); + break; + case COMPRESS_TYPE_ZSTD: + compressor = new ZstdCompress(); + break; + default: + break; + } return RET_SUCCESS; } @@ -169,7 +168,7 @@ int LogPersister::Init() cout << "Recovered persister, Offset=" << moffset << endl; #endif SetBufferOffset(moffset); - //WriteFile(); + WriteFile(); } else { SetBufferOffset(0); } @@ -296,21 +295,16 @@ void LogPersister::Start() inline void LogPersister::WriteFile() { - if (buffer->offset == 0) + if (buffer->offset == 0) return; rotator->Input((char *)compressBuffer->content, compressBuffer->offset); beforeCompressLogSize += buffer->offset; -#ifdef DEBUG - cout << buffer->offset << endl; - cout << compressBuffer->offset << endl; - cout << "beforeCompressLogSize" << beforeCompressLogSize < fileSize) { beforeCompressLogSize = 0; rotator->FinishInput(); } compressBuffer->offset = 0; - SetBufferOffset(0); + SetBufferOffset(0); } int LogPersister::ThreadFunc() diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index 89aef7f..39e751c 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -33,16 +33,15 @@ LogPersisterRotator::LogPersisterRotator(string path, uint32_t fileSize, uint32_ int LogPersisterRotator::Input(const char *buf, uint32_t length) { cout << __func__ << " " << fileName << " " << index - << " " << length << " need: " << needRotate << endl; - if (needRotate) { - output.close(); - Rotate(); - needRotate = false; - } - + << " " << length << " need: " << needRotate << endl; + if (needRotate) { + output.close(); + Rotate(); + needRotate = false; + } if (length <= 0 || buf == nullptr) return -1; - uint32_t offset = 0; - output.write(buf + offset, length); + uint32_t offset = 0; + output.write(buf + offset, length); return 0; } @@ -87,7 +86,8 @@ void LogPersisterRotator::FillInfo(uint32_t *size, uint32_t *num) *num = fileNum; } -void LogPersisterRotator::FinishInput() { +void LogPersisterRotator::FinishInput() +{ needRotate = true; } } // namespace HiviewDFX diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index b901449..fcd1b05 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -44,7 +44,7 @@ using namespace std; constexpr int MAX_DATA_LEN = 2048; string g_logPersisterDir = "/data/misc/logd/"; constexpr int DEFAULT_LOG_LEVEL = 1<compressAlgStr); pLogPersistStartMsg->fileSize = (logPersistParam->fileSizeStr == "") ? fileSizeDefault : GetBuffSize( logPersistParam->fileSizeStr); - if (pLogPersistStartMsg->fileSize < LOG_PERSIST_MIN_FILE_SIZE) { + if (pLogPersistStartMsg->fileSize < LOG_PERSIST_MIN_FILE_SIZE) { std::cout << "Persist log file size less than min size" << std::endl; - return RET_FAIL; - } + return RET_FAIL; + } pLogPersistStartMsg->fileNum = (logPersistParam->fileNumStr == "") ? fileNumDefault : stoi(logPersistParam->fileNumStr); if (logPersistParam->fileNameStr.size() > FILE_PATH_MAX_LEN) { @@ -431,8 +431,7 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi SetMsgHead(&pLogPersistStartReq->msgHeader, msgCmd, sizeof(LogPersistStartRequest)); controller.WriteAll(msgToSend, sizeof(LogPersistStartRequest)); break; - } - + } case MC_REQ_LOG_PERSIST_STOP: { LogPersistStopRequest* pLogPersistStopReq = reinterpret_cast(msgToSend); -- Gitee From dee4412787972e1c9bb5f1c68d5d99b0a06e4e21 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Mon, 19 Jul 2021 01:54:45 +0000 Subject: [PATCH 10/24] zcompress Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/include/log_compress.h | 2 +- services/hilogd/log_compress.cpp | 8 ++++++-- services/hilogd/log_persister.cpp | 1 - services/hilogd/log_querier.cpp | 6 ++++++ 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/services/hilogd/include/log_compress.h b/services/hilogd/include/log_compress.h index 6b6e629..ea64ead 100644 --- a/services/hilogd/include/log_compress.h +++ b/services/hilogd/include/log_compress.h @@ -31,7 +31,7 @@ public: virtual ~LogCompress() = default; virtual int Compress(char (&src)[], uint32_t &inLen, char (&dst)[], uint32_t &outLen) = 0; uLong zdlen = 0; - unsigned char *zdata; + unsigned char *zdata = nullptr; char buffIn[CHUNK]; char buffOut[CHUNK]; }; diff --git a/services/hilogd/log_compress.cpp b/services/hilogd/log_compress.cpp index 9f19b4e..d0cc2f3 100644 --- a/services/hilogd/log_compress.cpp +++ b/services/hilogd/log_compress.cpp @@ -91,7 +91,9 @@ int ZlibCompress::Compress(char (&src)[], uint32_t &inLen, char (&dst)[], uint32 } while (flush != Z_FINISH); /* clean up and return */ (void)deflateEnd(&cStream); - memcpy_s(dst + outLen, COMPRESS_BUFFER_SIZE - outLen, zdata, zdlen); + if (memcpy_s(dst + outLen, COMPRESS_BUFFER_SIZE - outLen, zdata, zdlen) != 0) { + return -1; + } outLen += zdlen; delete zdata; return 0; @@ -150,7 +152,9 @@ int ZstdCompress::Compress(char (&src)[], uint32_t &inLen, char (&dst)[], uint32 } while (!finished); } while (mode != ZSTD_e_end); ZSTD_freeCCtx(cctx); - memcpy_s(dst + outLen, COMPRESS_BUFFER_SIZE - outLen, zdata, zdlen); + if (memcpy_s(dst + outLen, COMPRESS_BUFFER_SIZE - outLen, zdata, zdlen) != 0) { + return -1; + } outLen += zdlen; delete zdata; #endif // #ifdef USING_ZSTD_COMPRESS diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index d93ab59..d36eb51 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -174,7 +174,6 @@ int LogPersister::Init() SetBufferOffset(0); } logPersisters.push_back(std::static_pointer_cast(shared_from_this())); - compressBuffer = new LogPersisterBuffer[COMPRESS_BUFFER_SIZE]; return 0; } diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index e71c0de..a353efc 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -112,6 +112,10 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade } if (pLogPersistStartMsg->fileSize < LOG_PERSIST_MIN_FILE_SIZE) { std::cout << "Persist log file size less than min size" << std::endl; + pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; + pLogPersistStartRst->result = RET_FAIL; + SetMsgHead(&pLogPersistStartRsp->msgHeader, MC_RSP_LOG_PERSIST_START, sendMsgLen); + logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); return; } string logPersisterPath; @@ -138,7 +142,9 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade pLogPersistStartRst->result = persister->Init(); persister->queryCondition.types = pLogPersistStartMsg->logType; persister->queryCondition.levels = DEFAULT_LOG_LEVEL; + cout << "pLogPersistStartRst->result" << pLogPersistStartRst->result << endl; if (pLogPersistStartRst->result == RET_FAIL) { + cout << "pLogPersistStartRst->result" << pLogPersistStartRst->result << endl; persister.reset(); } else { persister->Start(); -- Gitee From 065279ce0ccd5fe563b9cab5972d3fb53b95f48f Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Mon, 19 Jul 2021 01:54:45 +0000 Subject: [PATCH 11/24] zcompress Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/include/log_compress.h | 2 +- services/hilogd/log_compress.cpp | 8 ++++++-- services/hilogd/log_persister.cpp | 1 - services/hilogd/log_persister_rotator.cpp | 6 +++--- services/hilogd/log_querier.cpp | 6 ++++++ 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/services/hilogd/include/log_compress.h b/services/hilogd/include/log_compress.h index 6b6e629..ea64ead 100644 --- a/services/hilogd/include/log_compress.h +++ b/services/hilogd/include/log_compress.h @@ -31,7 +31,7 @@ public: virtual ~LogCompress() = default; virtual int Compress(char (&src)[], uint32_t &inLen, char (&dst)[], uint32_t &outLen) = 0; uLong zdlen = 0; - unsigned char *zdata; + unsigned char *zdata = nullptr; char buffIn[CHUNK]; char buffOut[CHUNK]; }; diff --git a/services/hilogd/log_compress.cpp b/services/hilogd/log_compress.cpp index 9f19b4e..d0cc2f3 100644 --- a/services/hilogd/log_compress.cpp +++ b/services/hilogd/log_compress.cpp @@ -91,7 +91,9 @@ int ZlibCompress::Compress(char (&src)[], uint32_t &inLen, char (&dst)[], uint32 } while (flush != Z_FINISH); /* clean up and return */ (void)deflateEnd(&cStream); - memcpy_s(dst + outLen, COMPRESS_BUFFER_SIZE - outLen, zdata, zdlen); + if (memcpy_s(dst + outLen, COMPRESS_BUFFER_SIZE - outLen, zdata, zdlen) != 0) { + return -1; + } outLen += zdlen; delete zdata; return 0; @@ -150,7 +152,9 @@ int ZstdCompress::Compress(char (&src)[], uint32_t &inLen, char (&dst)[], uint32 } while (!finished); } while (mode != ZSTD_e_end); ZSTD_freeCCtx(cctx); - memcpy_s(dst + outLen, COMPRESS_BUFFER_SIZE - outLen, zdata, zdlen); + if (memcpy_s(dst + outLen, COMPRESS_BUFFER_SIZE - outLen, zdata, zdlen) != 0) { + return -1; + } outLen += zdlen; delete zdata; #endif // #ifdef USING_ZSTD_COMPRESS diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index d93ab59..d36eb51 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -174,7 +174,6 @@ int LogPersister::Init() SetBufferOffset(0); } logPersisters.push_back(std::static_pointer_cast(shared_from_this())); - compressBuffer = new LogPersisterBuffer[COMPRESS_BUFFER_SIZE]; return 0; } diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index fb0dd92..59d8f72 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -25,20 +25,20 @@ using namespace std; LogPersisterRotator::LogPersisterRotator(string path, uint32_t fileSize, uint32_t fileNum, string suffix) : fileNum(fileNum), fileSize(fileSize), fileName(path), fileSuffix(suffix) { - index = 0; - needRotate = true; + index = -1; + needRotate = false; } int LogPersisterRotator::Input(const char *buf, uint32_t length) { cout << __func__ << " " << fileName << " " << index << " " << length << " need: " << needRotate << endl; + if (length <= 0 || buf == nullptr) return -1; if (needRotate) { output.close(); Rotate(); needRotate = false; } - if (length <= 0 || buf == nullptr) return -1; output.write(buf, length); output.flush(); return 0; diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index e71c0de..a353efc 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -112,6 +112,10 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade } if (pLogPersistStartMsg->fileSize < LOG_PERSIST_MIN_FILE_SIZE) { std::cout << "Persist log file size less than min size" << std::endl; + pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; + pLogPersistStartRst->result = RET_FAIL; + SetMsgHead(&pLogPersistStartRsp->msgHeader, MC_RSP_LOG_PERSIST_START, sendMsgLen); + logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); return; } string logPersisterPath; @@ -138,7 +142,9 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade pLogPersistStartRst->result = persister->Init(); persister->queryCondition.types = pLogPersistStartMsg->logType; persister->queryCondition.levels = DEFAULT_LOG_LEVEL; + cout << "pLogPersistStartRst->result" << pLogPersistStartRst->result << endl; if (pLogPersistStartRst->result == RET_FAIL) { + cout << "pLogPersistStartRst->result" << pLogPersistStartRst->result << endl; persister.reset(); } else { persister->Start(); -- Gitee From 92b53225ed6eef812376615255e2bb0bc3e1276a Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Sun, 25 Jul 2021 09:05:46 +0000 Subject: [PATCH 12/24] Check for unfinished jobs when CmdExecutor starts Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/cmd_executor.cpp | 8 ++++ services/hilogd/include/log_querier.h | 1 + services/hilogd/log_persister.cpp | 3 ++ services/hilogd/log_querier.cpp | 58 ++++++++++++++++++++++++++- 4 files changed, 69 insertions(+), 1 deletion(-) diff --git a/services/hilogd/cmd_executor.cpp b/services/hilogd/cmd_executor.cpp index ddc72e4..dac1c0e 100644 --- a/services/hilogd/cmd_executor.cpp +++ b/services/hilogd/cmd_executor.cpp @@ -28,6 +28,12 @@ const int MAX_WRITE_LOG_TASK = 100; using namespace std; HilogBuffer* CmdExecutor::hilogBuffer = nullptr; +void StartUpCheck() +{ + std::shared_ptr logQuerier = std::make_shared(nullptr, + CmdExecutor::getHilogBuffer()); + logQuerier->CheckUnfinishedJobs(CmdExecutor::getHilogBuffer()); +} void LogQuerierMonitor(std::unique_ptr handler) { std::shared_ptr logQuerier = std::make_shared(std::move(handler), @@ -37,6 +43,8 @@ void LogQuerierMonitor(std::unique_ptr handler) int CmdExecutorThreadFunc(std::unique_ptr handler) { + std::thread StartUpCheckThread(StartUpCheck); + StartUpCheckThread.detach(); std::thread logQuerierMonitorThread(LogQuerierMonitor, std::move(handler)); logQuerierMonitorThread.detach(); return 0; diff --git a/services/hilogd/include/log_querier.h b/services/hilogd/include/log_querier.h index 5f89d8f..2c63d7d 100644 --- a/services/hilogd/include/log_querier.h +++ b/services/hilogd/include/log_querier.h @@ -28,6 +28,7 @@ public: int WriteData(HilogData* data); void NotifyForNewData(); uint8_t GetType() const; + int CheckUnfinishedJobs(HilogBuffer *_buffer); ~LogQuerier() = default; }; } // namespace HiviewDFX diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index aea2f99..5ca311b 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -287,6 +287,9 @@ int LogPersister::WriteData(HilogData *data) void LogPersister::Start() { + fseek(fdinfo, 0, SEEK_END); + fprintf(fdinfo, "%u\n%s\n%u\n%u\n%u\n%u\n", id, path.c_str(), fileSize, compressAlg, + queryCondition.types, queryCondition.levels); auto newThread = thread(&LogPersister::ThreadFunc, static_pointer_cast(shared_from_this())); newThread.detach(); diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index ca8cee0..da6cd57 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -41,7 +42,7 @@ namespace OHOS { namespace HiviewDFX { using namespace std; - +namespace fs = std::filesystem; constexpr int MAX_DATA_LEN = 2048; string g_logPersisterDir = "/data/misc/logd/"; constexpr int DEFAULT_LOG_LEVEL = 1<d_name); + if (pPath.substr(pPath.length() - 5, pPath.length()) == ".info") { + std::ifstream infile; + infile.open(pPath); + uint8_t levels = 0; + uint32_t jobId = 0, fileSize = 0, offset = 0; + int fileNum = 0; + std::string path; + uint16_t compressAlg = 0, types = 0; + string fileSuffix = ""; + infile >> offset >> jobId >> path >> fileSize >> compressAlg >> types >> levels; + switch (compressAlg) { + case CompressAlg::COMPRESS_TYPE_ZSTD: + fileSuffix = ".zst"; + break; + case CompressAlg::COMPRESS_TYPE_ZLIB: + fileSuffix = ".gz"; + break; + default: + break; + }; + LogPersisterRotator* rotator = new LogPersisterRotator( + path, + fileSize, + fileNum, + fileSuffix); + std::shared_ptr persister = make_shared( + jobId, path, fileSize, compressAlg, SLEEP_TIME, rotator, _buffer); + int result = persister->Init(); + persister->queryCondition.types = types; + persister->queryCondition.levels = levels; + if (result == RET_FAIL) { + cout << "LogPersister Start Failed, result=" << result << endl; + persister.reset(); + } else { + persister->Start(); + _buffer->AddLogReader(weak_ptr(persister)); + } + infile.close(); + } + } + closedir (dir); + } else { + perror ("Failed to open persister directory!"); + return EXIT_FAILURE; + } + return EXIT_SUCCESS; +} } // namespace HiviewDFX } // namespace OHOS -- Gitee From e187294ac3185f7645cc3b9794b6df098e075e7a Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 27 Jul 2021 04:39:41 +0000 Subject: [PATCH 13/24] Reduce persister disk IO; Fix memory leak bugs Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/BUILD.gn | 2 +- services/hilogd/cmd_executor.cpp | 2 +- services/hilogd/include/hilogd.h | 29 ------------------------- services/hilogd/include/log_compress.h | 7 +++++- services/hilogd/include/log_persister.h | 1 + services/hilogd/include/log_querier.h | 2 +- services/hilogd/log_compress.cpp | 12 +++++++++- services/hilogd/log_persister.cpp | 12 ++-------- services/hilogd/log_querier.cpp | 9 ++++---- 9 files changed, 27 insertions(+), 49 deletions(-) delete mode 100644 services/hilogd/include/hilogd.h diff --git a/services/hilogd/BUILD.gn b/services/hilogd/BUILD.gn index 9941856..eb23122 100644 --- a/services/hilogd/BUILD.gn +++ b/services/hilogd/BUILD.gn @@ -33,7 +33,7 @@ ohos_executable("hilogd") { "main.cpp", ] configs = [ ":hilogd_config" ] - + defines = [ "DEBUG" ] deps = [ "//base/hiviewdfx/hilog/adapter:libhilog_os_adapter", "//base/hiviewdfx/hilog/frameworks/native:libhilogutil", diff --git a/services/hilogd/cmd_executor.cpp b/services/hilogd/cmd_executor.cpp index dac1c0e..2fbb0ff 100644 --- a/services/hilogd/cmd_executor.cpp +++ b/services/hilogd/cmd_executor.cpp @@ -32,7 +32,7 @@ void StartUpCheck() { std::shared_ptr logQuerier = std::make_shared(nullptr, CmdExecutor::getHilogBuffer()); - logQuerier->CheckUnfinishedJobs(CmdExecutor::getHilogBuffer()); + logQuerier->RestorePersistJobs(CmdExecutor::getHilogBuffer()); } void LogQuerierMonitor(std::unique_ptr handler) { diff --git a/services/hilogd/include/hilogd.h b/services/hilogd/include/hilogd.h deleted file mode 100644 index 9c34e93..0000000 --- a/services/hilogd/include/hilogd.h +++ /dev/null @@ -1,29 +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. - */ -#ifndef HILOGTOOL_H -#define HILOGTOOL_H -#include - -namespace OHOS { -namespace HiviewDFX { -const uint32_t MAX_PERSISTER_BUFFER_SIZE = 64 * 1024; -typedef struct { - uint32_t offset; - char content[MAX_PERSISTER_BUFFER_SIZE]; -} LogPersisterBuffer; - -} // namespace HiviewDFX -} // namespace OHOS -#endif diff --git a/services/hilogd/include/log_compress.h b/services/hilogd/include/log_compress.h index 2cf865e..4309996 100644 --- a/services/hilogd/include/log_compress.h +++ b/services/hilogd/include/log_compress.h @@ -14,7 +14,6 @@ */ #ifndef HILOG_COMPRESS_H #define HILOG_COMPRESS_H -#include "hilogd.h" #include #ifdef USING_ZSTD_COMPRESS @@ -26,6 +25,12 @@ namespace OHOS { namespace HiviewDFX { +const uint32_t MAX_PERSISTER_BUFFER_SIZE = 64 * 1024; +typedef struct { + uint32_t offset; + char content[MAX_PERSISTER_BUFFER_SIZE]; +} LogPersisterBuffer; + const uint16_t CHUNK = 16384; class LogCompress { public: diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index 88e6924..bb16342 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -26,6 +26,7 @@ #include "log_persister_rotator.h" #include "log_reader.h" #include "log_compress.h" + namespace OHOS { namespace HiviewDFX { using namespace std; diff --git a/services/hilogd/include/log_querier.h b/services/hilogd/include/log_querier.h index 2c63d7d..4f27621 100644 --- a/services/hilogd/include/log_querier.h +++ b/services/hilogd/include/log_querier.h @@ -28,7 +28,7 @@ public: int WriteData(HilogData* data); void NotifyForNewData(); uint8_t GetType() const; - int CheckUnfinishedJobs(HilogBuffer *_buffer); + int RestorePersistJobs(HilogBuffer *_buffer); ~LogQuerier() = default; }; } // namespace HiviewDFX diff --git a/services/hilogd/log_compress.cpp b/services/hilogd/log_compress.cpp index 62879b1..7b35f78 100644 --- a/services/hilogd/log_compress.cpp +++ b/services/hilogd/log_compress.cpp @@ -13,7 +13,6 @@ * limitations under the License. */ #include "log_compress.h" -#include "hilogd.h" #include "malloc.h" #include #include @@ -52,6 +51,7 @@ int ZlibCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com cStream.zfree = Z_NULL; cStream.opaque = Z_NULL; if (deflateInit2(&cStream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK) { + delete zdata; return -1; } do { @@ -59,6 +59,7 @@ int ZlibCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com if (flag) { memset_s(buffIn, CHUNK, 0, CHUNK); if (memmove_s(buffIn, CHUNK, buffer->content + src_pos, read - src_pos) != 0) { + delete zdata; return -1; } cStream.avail_in = read - src_pos; @@ -66,6 +67,7 @@ int ZlibCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com } else { memset_s(buffIn, CHUNK, 0, CHUNK); if (memmove_s(buffIn, CHUNK, buffer->content + src_pos, toRead) != 0) { + delete zdata; return -1; }; src_pos += toRead; @@ -79,10 +81,12 @@ int ZlibCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com cStream.avail_out = CHUNK; cStream.next_out = (Bytef *)buffOut; if (deflate(&cStream, flush) == Z_STREAM_ERROR) { + delete zdata; return -1; } unsigned have = CHUNK - cStream.avail_out; if (memmove_s(zdata + dst_pos, CHUNK, buffOut, have) != 0) { + delete zdata; return -1; } dst_pos += have; @@ -92,6 +96,7 @@ int ZlibCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com (void)deflateEnd(&cStream); if (memcpy_s(compressBuffer->content + compressBuffer->offset, MAX_PERSISTER_BUFFER_SIZE - compressBuffer->offset, zdata, dst_pos) != 0) { + delete zdata; return -1; } compressBuffer->offset += dst_pos; @@ -113,6 +118,7 @@ int ZstdCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com cctx = ZSTD_createCCtx(); if (cctx == nullptr) { cout << "ZSTD_createCCtx() failed!" << endl; + delete zdata; return -1; } ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, compressionlevel); @@ -126,6 +132,7 @@ int ZstdCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com if (flag) { memset_s(buffIn, CHUNK, 0, CHUNK); if (memmove_s(buffIn, CHUNK, buffer->content + src_pos, read - src_pos) != 0) { + delete zdata; return -1; } input = {buffIn, read - src_pos, 0}; @@ -133,6 +140,7 @@ int ZstdCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com } else { memset_s(buffIn, CHUNK, 0, CHUNK); if (memmove_s(buffIn, CHUNK, buffer->content + src_pos, toRead) != 0) { + delete zdata; return -1; } input = {buffIn, toRead, 0}; @@ -144,6 +152,7 @@ int ZstdCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com ZSTD_outBuffer output = {buffOut, CHUNK, 0}; size_t const remaining = ZSTD_compressStream2(cctx, &output, &input, mode); if (memmove_s(zdata + dst_pos, zdlen, (Bytef *)buffOut, output.pos) != 0) { + delete zdata; return -1; } dst_pos += output.pos; @@ -153,6 +162,7 @@ int ZstdCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com ZSTD_freeCCtx(cctx); if (memcpy_s(compressBuffer->content + compressBuffer->offset, MAX_PERSISTER_BUFFER_SIZE - compressBuffer->offset, zdata, dst_pos) != 0) { + delete zdata; return -1; } compressBuffer->offset += dst_pos; diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 5ca311b..467a6e3 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -14,7 +14,6 @@ */ #include "log_persister.h" -#include "hilogd.h" #include #include @@ -162,14 +161,9 @@ int LogPersister::Init() return RET_FAIL; } if (restore == true) { - int moffset; - if (fscanf_s(fdinfo, "%04x", &moffset) == -1) { - return RET_FAIL; - } #ifdef DEBUG - cout << "Recovered persister, Offset=" << moffset << endl; + cout << "Recovered persister, Offset=" << buffer->offset << endl; #endif - SetBufferOffset(moffset); WriteFile(); } else { SetBufferOffset(0); @@ -195,8 +189,6 @@ int LogPersister::MkDirPath(const char *pMkdir) void LogPersister::SetBufferOffset(int off) { buffer->offset = off; - fseek(fdinfo, 0, SEEK_SET); - fprintf(fdinfo, "%04x\n", off); } int GenPersistLogHeader(HilogData *data, list& persistList) @@ -287,7 +279,7 @@ int LogPersister::WriteData(HilogData *data) void LogPersister::Start() { - fseek(fdinfo, 0, SEEK_END); + fseek(fdinfo, 0, SEEK_SET); fprintf(fdinfo, "%u\n%s\n%u\n%u\n%u\n%u\n", id, path.c_str(), fileSize, compressAlg, queryCondition.types, queryCondition.levels); auto newThread = diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index da6cd57..68d5dd9 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -14,7 +14,6 @@ */ #include "log_querier.h" -#include "hilogd.h" #include #include #include @@ -564,7 +563,7 @@ uint8_t LogQuerier::GetType() const } } -int LogQuerier::CheckUnfinishedJobs(HilogBuffer *_buffer) +int LogQuerier::RestorePersistJobs(HilogBuffer *_buffer) { DIR *dir; struct dirent *ent; @@ -575,12 +574,13 @@ int LogQuerier::CheckUnfinishedJobs(HilogBuffer *_buffer) std::ifstream infile; infile.open(pPath); uint8_t levels = 0; - uint32_t jobId = 0, fileSize = 0, offset = 0; + uint32_t jobId = 0, fileSize = 0; int fileNum = 0; std::string path; uint16_t compressAlg = 0, types = 0; string fileSuffix = ""; - infile >> offset >> jobId >> path >> fileSize >> compressAlg >> types >> levels; + infile >> jobId >> path >> fileSize >> compressAlg >> types >> levels; + infile.close(); switch (compressAlg) { case CompressAlg::COMPRESS_TYPE_ZSTD: fileSuffix = ".zst"; @@ -608,7 +608,6 @@ int LogQuerier::CheckUnfinishedJobs(HilogBuffer *_buffer) persister->Start(); _buffer->AddLogReader(weak_ptr(persister)); } - infile.close(); } } closedir (dir); -- Gitee From d7e2072b6af45e9a20855ab695fc7a06b24b01cf Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 27 Jul 2021 05:53:36 +0000 Subject: [PATCH 14/24] LogPersister: read/write struct from/to info file Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/include/log_persister.h | 9 +++++ services/hilogd/log_persister.cpp | 16 +++++++-- services/hilogd/log_querier.cpp | 44 +++++++++---------------- 3 files changed, 38 insertions(+), 31 deletions(-) diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index bb16342..fee2221 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -31,6 +31,13 @@ namespace OHOS { namespace HiviewDFX { using namespace std; +typedef struct { + LogPersistStartMsg msg; +// uint8_t fileId; + uint16_t types; + uint8_t levels; +} PersistRecoveryInfo; + class LogPersister : public LogReader { public: LogPersister(uint32_t id, std::string path, uint32_t fileSize, uint16_t compressAlg, int sleepTime, @@ -52,6 +59,7 @@ public: bool writeUnCompressedBuffer(HilogData *data); uint8_t GetType() const; std::string getPath(); + void saveMsg(LogPersistStartMsg pMsg); LogPersisterBuffer *buffer; LogPersisterBuffer *compressBuffer; private: @@ -75,6 +83,7 @@ private: LogCompress *compressor; list persistList; uint32_t plainLogSize; + LogPersistStartMsg startMsg; }; int GenPersistLogHeader(HilogData *data, list& persistList); diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 467a6e3..7682ce7 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -279,9 +279,14 @@ int LogPersister::WriteData(HilogData *data) void LogPersister::Start() { - fseek(fdinfo, 0, SEEK_SET); - fprintf(fdinfo, "%u\n%s\n%u\n%u\n%u\n%u\n", id, path.c_str(), fileSize, compressAlg, - queryCondition.types, queryCondition.levels); +// fseek(fdinfo, 0, SEEK_SET); +// fprintf(fdinfo, "%u\n%s\n%u\n%u\n%u\n%u\n", id, path.c_str(), fileSize, compressAlg, +// queryCondition.types, queryCondition.levels); + PersistRecoveryInfo info; + info.msg = startMsg; + info.types = queryCondition.types; + info.levels = queryCondition.levels; + fwrite(&info, sizeof(PersistRecoveryInfo), 1, fdinfo); auto newThread = thread(&LogPersister::ThreadFunc, static_pointer_cast(shared_from_this())); newThread.detach(); @@ -414,5 +419,10 @@ uint8_t LogPersister::GetType() const { return TYPE_PERSISTER; } + +void LogPersister::saveMsg(LogPersistStartMsg pMsg) +{ + startMsg = pMsg; +} } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 68d5dd9..1fbc4ed 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -571,36 +571,24 @@ int LogQuerier::RestorePersistJobs(HilogBuffer *_buffer) while ((ent = readdir (dir)) != NULL) { std::string pPath = std::string(ent->d_name); if (pPath.substr(pPath.length() - 5, pPath.length()) == ".info") { - std::ifstream infile; - infile.open(pPath); - uint8_t levels = 0; - uint32_t jobId = 0, fileSize = 0; - int fileNum = 0; - std::string path; - uint16_t compressAlg = 0, types = 0; - string fileSuffix = ""; - infile >> jobId >> path >> fileSize >> compressAlg >> types >> levels; - infile.close(); - switch (compressAlg) { - case CompressAlg::COMPRESS_TYPE_ZSTD: - fileSuffix = ".zst"; - break; - case CompressAlg::COMPRESS_TYPE_ZLIB: - fileSuffix = ".gz"; - break; - default: - break; - }; - LogPersisterRotator* rotator = new LogPersisterRotator( - path, - fileSize, - fileNum, - fileSuffix); + FILE* infile; + infile = fopen(pPath.c_str(), "r"); + if (infile == NULL) { + std::cout << "Error opening recovery info file!" << std::endl; + continue; + } + PersistRecoveryInfo info; + fread(&info, sizeof(PersistRecoveryInfo), 1, infile); + LogPersisterRotator* rotator = rotator = MakeRotator(info.msg); std::shared_ptr persister = make_shared( - jobId, path, fileSize, compressAlg, SLEEP_TIME, rotator, _buffer); + info.msg.jobId, + info.msg.filePath, + info.msg.fileSize, + info.msg.compressAlg, + SLEEP_TIME, rotator, _buffer); int result = persister->Init(); - persister->queryCondition.types = types; - persister->queryCondition.levels = levels; + persister->queryCondition.types = info.types; + persister->queryCondition.levels = info.levels; if (result == RET_FAIL) { cout << "LogPersister Start Failed, result=" << result << endl; persister.reset(); -- Gitee From 44ea67b5c4aa627b699c580580a325c49563ef3a Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 27 Jul 2021 06:30:04 +0000 Subject: [PATCH 15/24] LogPersisterRotator: recovery file index from info Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/include/log_persister.h | 4 ++-- services/hilogd/include/log_persister_rotator.h | 6 ++++-- services/hilogd/log_persister.cpp | 2 +- services/hilogd/log_persister_rotator.cpp | 14 +++++++++++++- services/hilogd/log_querier.cpp | 1 + 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index fee2221..c287aff 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -32,10 +32,10 @@ namespace HiviewDFX { using namespace std; typedef struct { - LogPersistStartMsg msg; -// uint8_t fileId; + uint8_t index; uint16_t types; uint8_t levels; + LogPersistStartMsg msg; } PersistRecoveryInfo; class LogPersister : public LogReader { diff --git a/services/hilogd/include/log_persister_rotator.h b/services/hilogd/include/log_persister_rotator.h index 4ededd2..f7b9d98 100644 --- a/services/hilogd/include/log_persister_rotator.h +++ b/services/hilogd/include/log_persister_rotator.h @@ -21,11 +21,12 @@ namespace HiviewDFX { class LogPersisterRotator { public: LogPersisterRotator(std::string path, uint32_t fileSize, uint32_t fileNum, std::string suffix = ""); - virtual ~LogPersisterRotator(){}; + virtual ~LogPersisterRotator(){fclose(fdinfo);}; + void Init(); int Input(const char *buf, uint32_t length); void FillInfo(uint32_t *size, uint32_t *num); void FinishInput(); - + void setIndex(int pIndex); protected: virtual void InternalRotate(); uint32_t fileNum; @@ -37,6 +38,7 @@ protected: private: void Rotate(); bool needRotate = false; + FILE* fdinfo; }; } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 7682ce7..ac7dbd2 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -287,6 +287,7 @@ void LogPersister::Start() info.types = queryCondition.types; info.levels = queryCondition.levels; fwrite(&info, sizeof(PersistRecoveryInfo), 1, fdinfo); + fclose(fdinfo); auto newThread = thread(&LogPersister::ThreadFunc, static_pointer_cast(shared_from_this())); newThread.detach(); @@ -402,7 +403,6 @@ void LogPersister::Exit() cout << "removed mmap file" << endl; remove(mmapPath.c_str()); remove((mmapPath + ".info").c_str()); - fclose(fdinfo); return; } bool LogPersister::Identify(uint32_t id) diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index 59d8f72..91677ef 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -29,6 +29,11 @@ LogPersisterRotator::LogPersisterRotator(string path, uint32_t fileSize, uint32_ needRotate = false; } +void LogPersisterRotator::Init() +{ + fdinfo = fopen((fileName + ".info").c_str(), "w+"); +} + int LogPersisterRotator::Input(const char *buf, uint32_t length) { cout << __func__ << " " << fileName << " " << index @@ -68,7 +73,7 @@ void LogPersisterRotator::InternalRotate() void LogPersisterRotator::Rotate() { cout << __func__ << endl; - if (index == (int)(fileNum - 1)) { + if (index >= (int)(fileNum - 1)) { InternalRotate(); } else { index += 1; @@ -76,6 +81,8 @@ void LogPersisterRotator::Rotate() ss << fileName << "." << index << fileSuffix; cout << "THE FILE NAME !!!!!!! " << ss.str() << endl; output.open(ss.str(), ios::app); + fseek(fdinfo, 0, SEEK_SET); + fwrite(&index, sizeof(uint8_t), 1, fdinfo); } } @@ -89,5 +96,10 @@ void LogPersisterRotator::FinishInput() { needRotate = true; } + +void LogPersisterRotator::setIndex(int pIndex) +{ + index = pIndex; +} } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 1fbc4ed..3cdebda 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -580,6 +580,7 @@ int LogQuerier::RestorePersistJobs(HilogBuffer *_buffer) PersistRecoveryInfo info; fread(&info, sizeof(PersistRecoveryInfo), 1, infile); LogPersisterRotator* rotator = rotator = MakeRotator(info.msg); + rotator->setIndex(info.index + 1); std::shared_ptr persister = make_shared( info.msg.jobId, info.msg.filePath, -- Gitee From 7f3e67e303d6eb64eadbf8fa570a4a941410ebca Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 27 Jul 2021 06:30:04 +0000 Subject: [PATCH 16/24] LogPersisterRotator: recovery file index from info Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/include/log_persister.h | 4 ++-- services/hilogd/include/log_persister_rotator.h | 8 +++++--- services/hilogd/log_persister.cpp | 2 +- services/hilogd/log_persister_rotator.cpp | 14 +++++++++++++- services/hilogd/log_querier.cpp | 1 + 5 files changed, 22 insertions(+), 7 deletions(-) diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index fee2221..c287aff 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -32,10 +32,10 @@ namespace HiviewDFX { using namespace std; typedef struct { - LogPersistStartMsg msg; -// uint8_t fileId; + uint8_t index; uint16_t types; uint8_t levels; + LogPersistStartMsg msg; } PersistRecoveryInfo; class LogPersister : public LogReader { diff --git a/services/hilogd/include/log_persister_rotator.h b/services/hilogd/include/log_persister_rotator.h index 4ededd2..e452da0 100644 --- a/services/hilogd/include/log_persister_rotator.h +++ b/services/hilogd/include/log_persister_rotator.h @@ -21,13 +21,14 @@ namespace HiviewDFX { class LogPersisterRotator { public: LogPersisterRotator(std::string path, uint32_t fileSize, uint32_t fileNum, std::string suffix = ""); - virtual ~LogPersisterRotator(){}; + ~LogPersisterRotator(){fclose(fdinfo);}; + void Init(); int Input(const char *buf, uint32_t length); void FillInfo(uint32_t *size, uint32_t *num); void FinishInput(); - + void setIndex(int pIndex); protected: - virtual void InternalRotate(); + void InternalRotate(); uint32_t fileNum; uint32_t fileSize; std::string fileName; @@ -37,6 +38,7 @@ protected: private: void Rotate(); bool needRotate = false; + FILE* fdinfo; }; } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 7682ce7..ac7dbd2 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -287,6 +287,7 @@ void LogPersister::Start() info.types = queryCondition.types; info.levels = queryCondition.levels; fwrite(&info, sizeof(PersistRecoveryInfo), 1, fdinfo); + fclose(fdinfo); auto newThread = thread(&LogPersister::ThreadFunc, static_pointer_cast(shared_from_this())); newThread.detach(); @@ -402,7 +403,6 @@ void LogPersister::Exit() cout << "removed mmap file" << endl; remove(mmapPath.c_str()); remove((mmapPath + ".info").c_str()); - fclose(fdinfo); return; } bool LogPersister::Identify(uint32_t id) diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index 59d8f72..91677ef 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -29,6 +29,11 @@ LogPersisterRotator::LogPersisterRotator(string path, uint32_t fileSize, uint32_ needRotate = false; } +void LogPersisterRotator::Init() +{ + fdinfo = fopen((fileName + ".info").c_str(), "w+"); +} + int LogPersisterRotator::Input(const char *buf, uint32_t length) { cout << __func__ << " " << fileName << " " << index @@ -68,7 +73,7 @@ void LogPersisterRotator::InternalRotate() void LogPersisterRotator::Rotate() { cout << __func__ << endl; - if (index == (int)(fileNum - 1)) { + if (index >= (int)(fileNum - 1)) { InternalRotate(); } else { index += 1; @@ -76,6 +81,8 @@ void LogPersisterRotator::Rotate() ss << fileName << "." << index << fileSuffix; cout << "THE FILE NAME !!!!!!! " << ss.str() << endl; output.open(ss.str(), ios::app); + fseek(fdinfo, 0, SEEK_SET); + fwrite(&index, sizeof(uint8_t), 1, fdinfo); } } @@ -89,5 +96,10 @@ void LogPersisterRotator::FinishInput() { needRotate = true; } + +void LogPersisterRotator::setIndex(int pIndex) +{ + index = pIndex; +} } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 1fbc4ed..3cdebda 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -580,6 +580,7 @@ int LogQuerier::RestorePersistJobs(HilogBuffer *_buffer) PersistRecoveryInfo info; fread(&info, sizeof(PersistRecoveryInfo), 1, infile); LogPersisterRotator* rotator = rotator = MakeRotator(info.msg); + rotator->setIndex(info.index + 1); std::shared_ptr persister = make_shared( info.msg.jobId, info.msg.filePath, -- Gitee From 1b2305b82661ad99b025ed95b4139a1cda90e60a Mon Sep 17 00:00:00 2001 From: heartofrainbow <2826256824@qq.com> Date: Wed, 28 Jul 2021 14:40:48 +0800 Subject: [PATCH 17/24] Bugfix about crash and info saving Signed-off-by: heartofrainbow <2826256824@qq.com> --- services/hilogd/cmd_executor.cpp | 5 +++-- services/hilogd/include/log_persister.h | 2 +- services/hilogd/log_persister.cpp | 9 +++++++-- services/hilogd/log_querier.cpp | 14 +++++++++++--- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/services/hilogd/cmd_executor.cpp b/services/hilogd/cmd_executor.cpp index 2fbb0ff..e58b38d 100644 --- a/services/hilogd/cmd_executor.cpp +++ b/services/hilogd/cmd_executor.cpp @@ -43,8 +43,6 @@ void LogQuerierMonitor(std::unique_ptr handler) int CmdExecutorThreadFunc(std::unique_ptr handler) { - std::thread StartUpCheckThread(StartUpCheck); - StartUpCheckThread.detach(); std::thread logQuerierMonitorThread(LogQuerierMonitor, std::move(handler)); logQuerierMonitorThread.detach(); return 0; @@ -65,6 +63,9 @@ void CmdExecutor::StartCmdExecutorThread() cout << "chmod control socket failed !\n"; } cout << "Begin to cmd accept !\n"; + cout << "Restoring !\n"; + std::thread StartUpCheckThread(StartUpCheck); + StartUpCheckThread.detach(); cmdExecutorMainSocket.AcceptConnection(CmdExecutorThreadFunc); } } diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index c287aff..7b8e3cf 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -59,7 +59,7 @@ public: bool writeUnCompressedBuffer(HilogData *data); uint8_t GetType() const; std::string getPath(); - void saveMsg(LogPersistStartMsg pMsg); + void saveMsg(LogPersistStartMsg *pMsg); LogPersisterBuffer *buffer; LogPersisterBuffer *compressBuffer; private: diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index ac7dbd2..5120051 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -98,6 +98,7 @@ int LogPersister::Init() if (nPos == RET_FAIL) { return RET_FAIL; } + std::cout << "Checkpoint 1" << std::endl; mmapPath = path.substr(0, nPos) + "/." + ANXILLARY_FILE_NAME + to_string(id); if (access(path.substr(0, nPos).c_str(), F_OK) != 0) { if (errno == ENOENT) { @@ -115,9 +116,11 @@ int LogPersister::Init() if (hit) { return RET_FAIL; } + std::cout << "Checkpoint 2" << std::endl; if (InitCompress() == RET_FAIL) { return RET_FAIL; } + std::cout << "Checkpoint 3" << std::endl; fd = open(mmapPath.c_str(), O_RDWR | O_CREAT | O_EXCL, 0); bool restore = false; if (fd <= 0) { @@ -420,9 +423,11 @@ uint8_t LogPersister::GetType() const return TYPE_PERSISTER; } -void LogPersister::saveMsg(LogPersistStartMsg pMsg) +void LogPersister::saveMsg(LogPersistStartMsg *pMsg) { - startMsg = pMsg; + startMsg = *pMsg; + strcpy_s(startMsg.filePath, FILE_PATH_MAX_LEN, pMsg->filePath); + printf("Saved Path=%s\n",startMsg.filePath); } } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 3cdebda..c08a060 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -138,6 +138,7 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade pLogPersistStartMsg->fileSize, pLogPersistStartMsg->compressAlg, SLEEP_TIME, rotator, buffer); + persister->saveMsg(pLogPersistStartMsg); pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; pLogPersistStartRst->result = persister->Init(); persister->queryCondition.types = pLogPersistStartMsg->logType; @@ -146,6 +147,7 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade cout << "pLogPersistStartRst->result" << pLogPersistStartRst->result << endl; persister.reset(); } else { + rotator->Init(); persister->Start(); buffer->AddLogReader(weak_ptr(persister)); } @@ -569,18 +571,23 @@ int LogQuerier::RestorePersistJobs(HilogBuffer *_buffer) struct dirent *ent; if ((dir = opendir (g_logPersisterDir.c_str())) != NULL) { while ((ent = readdir (dir)) != NULL) { - std::string pPath = std::string(ent->d_name); - if (pPath.substr(pPath.length() - 5, pPath.length()) == ".info") { + size_t length = strlen(ent->d_name); + std::string pPath(ent->d_name, length); + if (length >= 5 && pPath.substr(length - 5, length) == ".info") { + std::cout << "Found a persist job!" << std::endl; FILE* infile; - infile = fopen(pPath.c_str(), "r"); + infile = fopen((g_logPersisterDir + pPath).c_str(), "r"); if (infile == NULL) { std::cout << "Error opening recovery info file!" << std::endl; continue; } PersistRecoveryInfo info; fread(&info, sizeof(PersistRecoveryInfo), 1, infile); + fclose(infile); LogPersisterRotator* rotator = rotator = MakeRotator(info.msg); rotator->setIndex(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, @@ -604,6 +611,7 @@ int LogQuerier::RestorePersistJobs(HilogBuffer *_buffer) perror ("Failed to open persister directory!"); return EXIT_FAILURE; } + cout << "Finished restoring persist jobs!" << endl; return EXIT_SUCCESS; } } // namespace HiviewDFX -- Gitee From b5a32ed5bee983b8f01f3e6f33d90168207f2f13 Mon Sep 17 00:00:00 2001 From: heartofrainbow <2826256824@qq.com> Date: Wed, 28 Jul 2021 15:22:43 +0800 Subject: [PATCH 18/24] Fix persist info overwrite bug Signed-off-by: heartofrainbow <2826256824@qq.com> --- services/hilogd/include/log_persister.h | 3 ++- services/hilogd/log_persister.cpp | 29 +++++++++++-------------- services/hilogd/log_querier.cpp | 4 +++- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index 7b8e3cf..5c664a2 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -78,12 +78,13 @@ private: bool hasExited; inline void WriteFile(); bool isExited(); - FILE *fdinfo; + FILE *fdinfo = nullptr; int fd = -1; LogCompress *compressor; list persistList; uint32_t plainLogSize; LogPersistStartMsg startMsg; + bool restore = false; }; int GenPersistLogHeader(HilogData *data, list& persistList); diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 5120051..92ea427 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -98,7 +98,6 @@ int LogPersister::Init() if (nPos == RET_FAIL) { return RET_FAIL; } - std::cout << "Checkpoint 1" << std::endl; mmapPath = path.substr(0, nPos) + "/." + ANXILLARY_FILE_NAME + to_string(id); if (access(path.substr(0, nPos).c_str(), F_OK) != 0) { if (errno == ENOENT) { @@ -116,13 +115,10 @@ int LogPersister::Init() if (hit) { return RET_FAIL; } - std::cout << "Checkpoint 2" << std::endl; if (InitCompress() == RET_FAIL) { return RET_FAIL; } - std::cout << "Checkpoint 3" << std::endl; fd = open(mmapPath.c_str(), O_RDWR | O_CREAT | O_EXCL, 0); - bool restore = false; if (fd <= 0) { if (errno == EEXIST) { cout << "File already exists!" << endl; @@ -142,10 +138,10 @@ int LogPersister::Init() #endif return RET_FAIL; } - fdinfo = fopen((mmapPath + ".info").c_str(), "r+"); - if (fdinfo == nullptr) { - fdinfo = fopen((mmapPath + ".info").c_str(), "w+"); - } + fdinfo = fopen((mmapPath + ".info").c_str(), "a+"); +// if (fdinfo == nullptr) { +// fdinfo = fopen((mmapPath + ".info").c_str(), "w+"); +// } if (fdinfo == nullptr) { #ifdef DEBUG cout << "open loginfo file failed: " << strerror(errno) << endl; @@ -282,14 +278,15 @@ int LogPersister::WriteData(HilogData *data) void LogPersister::Start() { -// fseek(fdinfo, 0, SEEK_SET); -// fprintf(fdinfo, "%u\n%s\n%u\n%u\n%u\n%u\n", id, path.c_str(), fileSize, compressAlg, -// queryCondition.types, queryCondition.levels); - PersistRecoveryInfo info; - info.msg = startMsg; - info.types = queryCondition.types; - info.levels = queryCondition.levels; - fwrite(&info, sizeof(PersistRecoveryInfo), 1, fdinfo); + if (!restore) { + std::cout << "Save Info file!" << std::endl; + PersistRecoveryInfo info; + info.msg = startMsg; + info.types = queryCondition.types; + info.levels = queryCondition.levels; + fseek(fdinfo, 0, SEEK_SET); + fwrite(&info, sizeof(PersistRecoveryInfo), 1, fdinfo); + } fclose(fdinfo); auto newThread = thread(&LogPersister::ThreadFunc, static_pointer_cast(shared_from_this())); diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index c08a060..5fb57dd 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -132,6 +132,7 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade } strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersisterPath.c_str()); rotator = MakeRotator(*pLogPersistStartMsg); + rotator->Init(); std::shared_ptr persister = make_shared( pLogPersistStartMsg->jobId, pLogPersistStartMsg->filePath, @@ -147,7 +148,6 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade cout << "pLogPersistStartRst->result" << pLogPersistStartRst->result << endl; persister.reset(); } else { - rotator->Init(); persister->Start(); buffer->AddLogReader(weak_ptr(persister)); } @@ -574,6 +574,7 @@ int LogQuerier::RestorePersistJobs(HilogBuffer *_buffer) size_t length = strlen(ent->d_name); std::string pPath(ent->d_name, length); if (length >= 5 && pPath.substr(length - 5, length) == ".info") { + if (pPath == "hilog.info") continue; std::cout << "Found a persist job!" << std::endl; FILE* infile; infile = fopen((g_logPersisterDir + pPath).c_str(), "r"); @@ -586,6 +587,7 @@ int LogQuerier::RestorePersistJobs(HilogBuffer *_buffer) fclose(infile); LogPersisterRotator* rotator = rotator = MakeRotator(info.msg); rotator->setIndex(info.index + 1); + rotator->Init(); printf("Recovery Info:\njobId=%u\nfilePath=%s\n", info.msg.jobId, info.msg.filePath); std::shared_ptr persister = make_shared( -- Gitee From c6dd6b120a80a08692d315d466553ddb0df400cd Mon Sep 17 00:00:00 2001 From: heartofrainbow <2826256824@qq.com> Date: Wed, 28 Jul 2021 16:47:36 +0800 Subject: [PATCH 19/24] Fix cmdExecutor and LogPersisterRotator recovery bugs Signed-off-by: heartofrainbow <2826256824@qq.com> --- services/hilogd/cmd_executor.cpp | 9 --------- services/hilogd/include/log_persister_rotator.h | 4 +++- services/hilogd/log_persister.cpp | 1 + services/hilogd/log_persister_rotator.cpp | 15 +++++++++++++-- services/hilogd/log_querier.cpp | 4 +++- services/hilogd/main.cpp | 11 +++++++++++ 6 files changed, 31 insertions(+), 13 deletions(-) diff --git a/services/hilogd/cmd_executor.cpp b/services/hilogd/cmd_executor.cpp index e58b38d..ddc72e4 100644 --- a/services/hilogd/cmd_executor.cpp +++ b/services/hilogd/cmd_executor.cpp @@ -28,12 +28,6 @@ const int MAX_WRITE_LOG_TASK = 100; using namespace std; HilogBuffer* CmdExecutor::hilogBuffer = nullptr; -void StartUpCheck() -{ - std::shared_ptr logQuerier = std::make_shared(nullptr, - CmdExecutor::getHilogBuffer()); - logQuerier->RestorePersistJobs(CmdExecutor::getHilogBuffer()); -} void LogQuerierMonitor(std::unique_ptr handler) { std::shared_ptr logQuerier = std::make_shared(std::move(handler), @@ -63,9 +57,6 @@ void CmdExecutor::StartCmdExecutorThread() cout << "chmod control socket failed !\n"; } cout << "Begin to cmd accept !\n"; - cout << "Restoring !\n"; - std::thread StartUpCheckThread(StartUpCheck); - StartUpCheckThread.detach(); cmdExecutorMainSocket.AcceptConnection(CmdExecutorThreadFunc); } } diff --git a/services/hilogd/include/log_persister_rotator.h b/services/hilogd/include/log_persister_rotator.h index f7b9d98..a41e193 100644 --- a/services/hilogd/include/log_persister_rotator.h +++ b/services/hilogd/include/log_persister_rotator.h @@ -26,7 +26,8 @@ public: int Input(const char *buf, uint32_t length); void FillInfo(uint32_t *size, uint32_t *num); void FinishInput(); - void setIndex(int pIndex); + void SetIndex(int pIndex); + void SetId(uint32_t pId); protected: virtual void InternalRotate(); uint32_t fileNum; @@ -39,6 +40,7 @@ private: void Rotate(); bool needRotate = false; FILE* fdinfo; + uint32_t id = 0; }; } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 92ea427..7673a7e 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -286,6 +286,7 @@ void LogPersister::Start() info.levels = queryCondition.levels; fseek(fdinfo, 0, SEEK_SET); fwrite(&info, sizeof(PersistRecoveryInfo), 1, fdinfo); + fsync(fileno(fdinfo)); } fclose(fdinfo); auto newThread = diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index 91677ef..a3f2b5a 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -18,9 +18,12 @@ #include #include #include +#include + namespace OHOS { namespace HiviewDFX { using namespace std; +#define ANXILLARY_FILE_NAME "persisterInfo_" LogPersisterRotator::LogPersisterRotator(string path, uint32_t fileSize, uint32_t fileNum, string suffix) : fileNum(fileNum), fileSize(fileSize), fileName(path), fileSuffix(suffix) @@ -31,7 +34,9 @@ LogPersisterRotator::LogPersisterRotator(string path, uint32_t fileSize, uint32_ void LogPersisterRotator::Init() { - fdinfo = fopen((fileName + ".info").c_str(), "w+"); + int nPos = fileName.find_last_of('/'); + std::string mmapPath = fileName.substr(0, nPos) + "/." + ANXILLARY_FILE_NAME + to_string(id); + fdinfo = fopen((mmapPath + ".info").c_str(), "w+"); } int LogPersisterRotator::Input(const char *buf, uint32_t length) @@ -83,6 +88,7 @@ void LogPersisterRotator::Rotate() output.open(ss.str(), ios::app); fseek(fdinfo, 0, SEEK_SET); fwrite(&index, sizeof(uint8_t), 1, fdinfo); + fsync(fileno(fdinfo)); } } @@ -97,9 +103,14 @@ void LogPersisterRotator::FinishInput() needRotate = true; } -void LogPersisterRotator::setIndex(int pIndex) +void LogPersisterRotator::SetIndex(int pIndex) { index = pIndex; } + +void LogPersisterRotator::SetId(uint32_t pId) +{ + id = pId; +} } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 5fb57dd..af495cf 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -132,6 +132,7 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade } strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersisterPath.c_str()); rotator = MakeRotator(*pLogPersistStartMsg); + rotator->SetId(pLogPersistStartMsg->jobId); rotator->Init(); std::shared_ptr persister = make_shared( pLogPersistStartMsg->jobId, @@ -586,7 +587,8 @@ int LogQuerier::RestorePersistJobs(HilogBuffer *_buffer) fread(&info, sizeof(PersistRecoveryInfo), 1, infile); fclose(infile); LogPersisterRotator* rotator = rotator = MakeRotator(info.msg); - rotator->setIndex(info.index + 1); + rotator->SetIndex(info.index + 1); + rotator->SetId(info.msg.jobId); rotator->Init(); printf("Recovery Info:\njobId=%u\nfilePath=%s\n", info.msg.jobId, info.msg.filePath); diff --git a/services/hilogd/main.cpp b/services/hilogd/main.cpp index 1263292..b4ebbde 100644 --- a/services/hilogd/main.cpp +++ b/services/hilogd/main.cpp @@ -19,6 +19,7 @@ #include #include "cmd_executor.h" +#include "log_querier.h" #include "hilog_input_socket_server.h" #include "log_collector.h" #include "flow_control_init.h" @@ -49,6 +50,13 @@ static void SigHandler(int sig) } } +void StartUpCheck() +{ + std::shared_ptr logQuerier = std::make_shared(nullptr, + CmdExecutor::getHilogBuffer()); + logQuerier->RestorePersistJobs(CmdExecutor::getHilogBuffer()); +} + int HilogdEntry(int argc, char* argv[]) { HilogBuffer hilogBuffer; @@ -81,6 +89,9 @@ int HilogdEntry(int argc, char* argv[]) #endif server.RunServingThread(); } + + std::thread StartupCheckThread(StartUpCheck); + StartupCheckThread.detach(); CmdExecutor cmdExecutor(&hilogBuffer); cmdExecutor.StartCmdExecutorThread(); -- Gitee From 2f0cca8fe5c47f44560f101ba24cd1a5f7f5dd5a Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Wed, 28 Jul 2021 12:42:01 +0000 Subject: [PATCH 20/24] Save info to persister; Fix rotator mkdir bug Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/include/log_persister.h | 4 ++-- services/hilogd/include/log_persister_rotator.h | 5 +++-- services/hilogd/log_persister.cpp | 17 ++++++----------- services/hilogd/log_persister_rotator.cpp | 15 +++++++++++++++ services/hilogd/log_querier.cpp | 8 ++++---- 5 files changed, 30 insertions(+), 19 deletions(-) diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index 5c664a2..3b5c809 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -59,7 +59,7 @@ public: bool writeUnCompressedBuffer(HilogData *data); uint8_t GetType() const; std::string getPath(); - void saveMsg(LogPersistStartMsg *pMsg); + void saveInfo(LogPersistStartMsg *pMsg); LogPersisterBuffer *buffer; LogPersisterBuffer *compressBuffer; private: @@ -83,7 +83,7 @@ private: LogCompress *compressor; list persistList; uint32_t plainLogSize; - LogPersistStartMsg startMsg; + PersistRecoveryInfo info; bool restore = false; }; diff --git a/services/hilogd/include/log_persister_rotator.h b/services/hilogd/include/log_persister_rotator.h index 7b0b021..3ea2616 100644 --- a/services/hilogd/include/log_persister_rotator.h +++ b/services/hilogd/include/log_persister_rotator.h @@ -21,7 +21,7 @@ namespace HiviewDFX { class LogPersisterRotator { public: LogPersisterRotator(std::string path, uint32_t fileSize, uint32_t fileNum, std::string suffix = ""); - ~LogPersisterRotator(){fclose(fdinfo);}; + virtual ~LogPersisterRotator(){fclose(fdinfo);}; void Init(); int Input(const char *buf, uint32_t length); void FillInfo(uint32_t *size, uint32_t *num); @@ -29,7 +29,8 @@ public: void SetIndex(int pIndex); void SetId(uint32_t pId); protected: - void InternalRotate(); + virtual void InternalRotate(); + int MkDirPath(const char *pMkdir); uint32_t fileNum; uint32_t fileSize; std::string fileName; diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 7673a7e..34a814a 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -139,9 +139,6 @@ int LogPersister::Init() return RET_FAIL; } fdinfo = fopen((mmapPath + ".info").c_str(), "a+"); -// if (fdinfo == nullptr) { -// fdinfo = fopen((mmapPath + ".info").c_str(), "w+"); -// } if (fdinfo == nullptr) { #ifdef DEBUG cout << "open loginfo file failed: " << strerror(errno) << endl; @@ -280,10 +277,6 @@ void LogPersister::Start() { if (!restore) { std::cout << "Save Info file!" << std::endl; - PersistRecoveryInfo info; - info.msg = startMsg; - info.types = queryCondition.types; - info.levels = queryCondition.levels; fseek(fdinfo, 0, SEEK_SET); fwrite(&info, sizeof(PersistRecoveryInfo), 1, fdinfo); fsync(fileno(fdinfo)); @@ -421,11 +414,13 @@ uint8_t LogPersister::GetType() const return TYPE_PERSISTER; } -void LogPersister::saveMsg(LogPersistStartMsg *pMsg) +void LogPersister::saveInfo(LogPersistStartMsg *pMsg) { - startMsg = *pMsg; - strcpy_s(startMsg.filePath, FILE_PATH_MAX_LEN, pMsg->filePath); - printf("Saved Path=%s\n",startMsg.filePath); + info.msg = *pMsg; + info.types = queryCondition.types; + info.levels = queryCondition.levels; + strcpy_s(info.msg.filePath, FILE_PATH_MAX_LEN, pMsg->filePath); + printf("Saved Path=%s\n",info.msg.filePath); } } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index a3f2b5a..87ffcd2 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -19,6 +19,7 @@ #include #include #include +#include namespace OHOS { namespace HiviewDFX { @@ -36,6 +37,11 @@ void 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) { + MkDirPath(fileName.substr(0, nPos).c_str()); + } + } fdinfo = fopen((mmapPath + ".info").c_str(), "w+"); } @@ -112,5 +118,14 @@ void LogPersisterRotator::SetId(uint32_t pId) { id = pId; } + +int LogPersisterRotator::MkDirPath(const char *pMkdir) +{ + int isCreate = mkdir(pMkdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO); + if (!isCreate) + cout << "create path:" << pMkdir << endl; + return isCreate; +} + } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index af495cf..f2a4ffd 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -46,7 +46,7 @@ constexpr int MAX_DATA_LEN = 2048; string g_logPersisterDir = "/data/misc/logd/"; constexpr int DEFAULT_LOG_LEVEL = 1< logReade pLogPersistStartMsg->fileSize, pLogPersistStartMsg->compressAlg, SLEEP_TIME, rotator, buffer); - persister->saveMsg(pLogPersistStartMsg); - pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; - pLogPersistStartRst->result = persister->Init(); persister->queryCondition.types = pLogPersistStartMsg->logType; persister->queryCondition.levels = DEFAULT_LOG_LEVEL; + persister->saveInfo(pLogPersistStartMsg); + pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; + pLogPersistStartRst->result = persister->Init(); if (pLogPersistStartRst->result == RET_FAIL) { cout << "pLogPersistStartRst->result" << pLogPersistStartRst->result << endl; persister.reset(); -- Gitee From 0ac4ebfb4b6e9dc177d0a2adf333bcdf97135e13 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Wed, 28 Jul 2021 12:42:01 +0000 Subject: [PATCH 21/24] Save info to persister; Fix rotator mkdir bug Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/BUILD.gn | 1 - services/hilogd/include/log_persister.h | 4 ++-- services/hilogd/include/log_persister_rotator.h | 5 +++-- services/hilogd/log_persister.cpp | 17 ++++++----------- services/hilogd/log_persister_rotator.cpp | 15 +++++++++++++++ services/hilogd/log_querier.cpp | 8 ++++---- 6 files changed, 30 insertions(+), 20 deletions(-) diff --git a/services/hilogd/BUILD.gn b/services/hilogd/BUILD.gn index eb23122..59814b3 100644 --- a/services/hilogd/BUILD.gn +++ b/services/hilogd/BUILD.gn @@ -33,7 +33,6 @@ ohos_executable("hilogd") { "main.cpp", ] configs = [ ":hilogd_config" ] - defines = [ "DEBUG" ] deps = [ "//base/hiviewdfx/hilog/adapter:libhilog_os_adapter", "//base/hiviewdfx/hilog/frameworks/native:libhilogutil", diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index 5c664a2..3b5c809 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -59,7 +59,7 @@ public: bool writeUnCompressedBuffer(HilogData *data); uint8_t GetType() const; std::string getPath(); - void saveMsg(LogPersistStartMsg *pMsg); + void saveInfo(LogPersistStartMsg *pMsg); LogPersisterBuffer *buffer; LogPersisterBuffer *compressBuffer; private: @@ -83,7 +83,7 @@ private: LogCompress *compressor; list persistList; uint32_t plainLogSize; - LogPersistStartMsg startMsg; + PersistRecoveryInfo info; bool restore = false; }; diff --git a/services/hilogd/include/log_persister_rotator.h b/services/hilogd/include/log_persister_rotator.h index 7b0b021..3ea2616 100644 --- a/services/hilogd/include/log_persister_rotator.h +++ b/services/hilogd/include/log_persister_rotator.h @@ -21,7 +21,7 @@ namespace HiviewDFX { class LogPersisterRotator { public: LogPersisterRotator(std::string path, uint32_t fileSize, uint32_t fileNum, std::string suffix = ""); - ~LogPersisterRotator(){fclose(fdinfo);}; + virtual ~LogPersisterRotator(){fclose(fdinfo);}; void Init(); int Input(const char *buf, uint32_t length); void FillInfo(uint32_t *size, uint32_t *num); @@ -29,7 +29,8 @@ public: void SetIndex(int pIndex); void SetId(uint32_t pId); protected: - void InternalRotate(); + virtual void InternalRotate(); + int MkDirPath(const char *pMkdir); uint32_t fileNum; uint32_t fileSize; std::string fileName; diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 7673a7e..34a814a 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -139,9 +139,6 @@ int LogPersister::Init() return RET_FAIL; } fdinfo = fopen((mmapPath + ".info").c_str(), "a+"); -// if (fdinfo == nullptr) { -// fdinfo = fopen((mmapPath + ".info").c_str(), "w+"); -// } if (fdinfo == nullptr) { #ifdef DEBUG cout << "open loginfo file failed: " << strerror(errno) << endl; @@ -280,10 +277,6 @@ void LogPersister::Start() { if (!restore) { std::cout << "Save Info file!" << std::endl; - PersistRecoveryInfo info; - info.msg = startMsg; - info.types = queryCondition.types; - info.levels = queryCondition.levels; fseek(fdinfo, 0, SEEK_SET); fwrite(&info, sizeof(PersistRecoveryInfo), 1, fdinfo); fsync(fileno(fdinfo)); @@ -421,11 +414,13 @@ uint8_t LogPersister::GetType() const return TYPE_PERSISTER; } -void LogPersister::saveMsg(LogPersistStartMsg *pMsg) +void LogPersister::saveInfo(LogPersistStartMsg *pMsg) { - startMsg = *pMsg; - strcpy_s(startMsg.filePath, FILE_PATH_MAX_LEN, pMsg->filePath); - printf("Saved Path=%s\n",startMsg.filePath); + info.msg = *pMsg; + info.types = queryCondition.types; + info.levels = queryCondition.levels; + strcpy_s(info.msg.filePath, FILE_PATH_MAX_LEN, pMsg->filePath); + printf("Saved Path=%s\n",info.msg.filePath); } } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index a3f2b5a..87ffcd2 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -19,6 +19,7 @@ #include #include #include +#include namespace OHOS { namespace HiviewDFX { @@ -36,6 +37,11 @@ void 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) { + MkDirPath(fileName.substr(0, nPos).c_str()); + } + } fdinfo = fopen((mmapPath + ".info").c_str(), "w+"); } @@ -112,5 +118,14 @@ void LogPersisterRotator::SetId(uint32_t pId) { id = pId; } + +int LogPersisterRotator::MkDirPath(const char *pMkdir) +{ + int isCreate = mkdir(pMkdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO); + if (!isCreate) + cout << "create path:" << pMkdir << endl; + return isCreate; +} + } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index af495cf..f2a4ffd 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -46,7 +46,7 @@ constexpr int MAX_DATA_LEN = 2048; string g_logPersisterDir = "/data/misc/logd/"; constexpr int DEFAULT_LOG_LEVEL = 1< logReade pLogPersistStartMsg->fileSize, pLogPersistStartMsg->compressAlg, SLEEP_TIME, rotator, buffer); - persister->saveMsg(pLogPersistStartMsg); - pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; - pLogPersistStartRst->result = persister->Init(); persister->queryCondition.types = pLogPersistStartMsg->logType; persister->queryCondition.levels = DEFAULT_LOG_LEVEL; + persister->saveInfo(pLogPersistStartMsg); + pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; + pLogPersistStartRst->result = persister->Init(); if (pLogPersistStartRst->result == RET_FAIL) { cout << "pLogPersistStartRst->result" << pLogPersistStartRst->result << endl; persister.reset(); -- Gitee From 74f9553b918933f28ff41360c49af8e31bbc9238 Mon Sep 17 00:00:00 2001 From: heartofrainbow <2826256824@qq.com> Date: Thu, 29 Jul 2021 12:12:52 +0800 Subject: [PATCH 22/24] hilogd: pass parameters by reference Signed-off-by: heartofrainbow <2826256824@qq.com> --- services/hilogd/include/log_compress.h | 1 - services/hilogd/include/log_persister.h | 4 +-- .../hilogd/include/log_persister_rotator.h | 6 ++-- services/hilogd/include/log_querier.h | 2 +- services/hilogd/log_persister.cpp | 20 +++++++------ services/hilogd/log_persister_rotator.cpp | 13 ++------- services/hilogd/log_querier.cpp | 28 +++++++++---------- services/hilogd/main.cpp | 14 ++++------ 8 files changed, 40 insertions(+), 48 deletions(-) diff --git a/services/hilogd/include/log_compress.h b/services/hilogd/include/log_compress.h index 4309996..eafcce6 100644 --- a/services/hilogd/include/log_compress.h +++ b/services/hilogd/include/log_compress.h @@ -24,7 +24,6 @@ #include namespace OHOS { namespace HiviewDFX { - const uint32_t MAX_PERSISTER_BUFFER_SIZE = 64 * 1024; typedef struct { uint32_t offset; diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index 3b5c809..bc80893 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -41,7 +41,7 @@ typedef struct { class LogPersister : public LogReader { public: LogPersister(uint32_t id, std::string path, uint32_t fileSize, uint16_t compressAlg, int sleepTime, - LogPersisterRotator *rotator, HilogBuffer *buffer); + LogPersisterRotator& rotator, HilogBuffer &buffer); ~LogPersister(); void SetBufferOffset(int off); void NotifyForNewData(); @@ -59,7 +59,7 @@ public: bool writeUnCompressedBuffer(HilogData *data); uint8_t GetType() const; std::string getPath(); - void saveInfo(LogPersistStartMsg *pMsg); + int SaveInfo(LogPersistStartMsg& pMsg); LogPersisterBuffer *buffer; LogPersisterBuffer *compressBuffer; private: diff --git a/services/hilogd/include/log_persister_rotator.h b/services/hilogd/include/log_persister_rotator.h index 3ea2616..0cd2c47 100644 --- a/services/hilogd/include/log_persister_rotator.h +++ b/services/hilogd/include/log_persister_rotator.h @@ -18,10 +18,13 @@ #include namespace OHOS { namespace HiviewDFX { +const std::string ANXILLARY_FILE_NAME = "persisterInfo_"; class LogPersisterRotator { public: LogPersisterRotator(std::string path, uint32_t fileSize, uint32_t fileNum, std::string suffix = ""); - virtual ~LogPersisterRotator(){fclose(fdinfo);}; + virtual ~LogPersisterRotator() { + fclose(fdinfo); + } void Init(); int Input(const char *buf, uint32_t length); void FillInfo(uint32_t *size, uint32_t *num); @@ -30,7 +33,6 @@ public: void SetId(uint32_t pId); protected: virtual void InternalRotate(); - int MkDirPath(const char *pMkdir); uint32_t fileNum; uint32_t fileSize; std::string fileName; diff --git a/services/hilogd/include/log_querier.h b/services/hilogd/include/log_querier.h index 4f27621..64c1508 100644 --- a/services/hilogd/include/log_querier.h +++ b/services/hilogd/include/log_querier.h @@ -28,7 +28,7 @@ public: int WriteData(HilogData* data); void NotifyForNewData(); uint8_t GetType() const; - int RestorePersistJobs(HilogBuffer *_buffer); + int RestorePersistJobs(HilogBuffer& _buffer); ~LogQuerier() = default; }; } // namespace HiviewDFX diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 34a814a..3937ce0 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -42,7 +42,7 @@ using namespace std; static std::list> logPersisters; static std::mutex g_listMutex; -#define ANXILLARY_FILE_NAME "persisterInfo_" + #define SAFE_DELETE(x) \ do { \ delete (x); \ @@ -50,13 +50,13 @@ static std::mutex g_listMutex; } while (0) LogPersister::LogPersister(uint32_t id, string path, uint32_t fileSize, uint16_t compressAlg, int sleepTime, - LogPersisterRotator *rotator, HilogBuffer *_buffer) + LogPersisterRotator& rotator, HilogBuffer &_buffer) : id(id), path(path), fileSize(fileSize), compressAlg(compressAlg), - sleepTime(sleepTime), rotator(rotator) + sleepTime(sleepTime), rotator(&rotator) { toExit = false; hasExited = false; - hilogBuffer = _buffer; + hilogBuffer = &_buffer; compressor = nullptr; fdinfo = nullptr; buffer = nullptr; @@ -414,13 +414,17 @@ uint8_t LogPersister::GetType() const return TYPE_PERSISTER; } -void LogPersister::saveInfo(LogPersistStartMsg *pMsg) +int LogPersister::SaveInfo(LogPersistStartMsg& pMsg) { - info.msg = *pMsg; + info.msg = pMsg; info.types = queryCondition.types; info.levels = queryCondition.levels; - strcpy_s(info.msg.filePath, FILE_PATH_MAX_LEN, pMsg->filePath); - printf("Saved Path=%s\n",info.msg.filePath); + if (strcpy_s(info.msg.filePath, FILE_PATH_MAX_LEN, pMsg.filePath) != 0) { + printf("Failed to save persister file path\n"); + return RET_FAIL; + } + printf("Saved Path=%s\n", info.msg. filePath); + return RET_SUCCESS; } } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index 87ffcd2..bdf9315 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -24,7 +24,6 @@ namespace OHOS { namespace HiviewDFX { using namespace std; -#define ANXILLARY_FILE_NAME "persisterInfo_" LogPersisterRotator::LogPersisterRotator(string path, uint32_t fileSize, uint32_t fileNum, string suffix) : fileNum(fileNum), fileSize(fileSize), fileName(path), fileSuffix(suffix) @@ -39,7 +38,7 @@ void LogPersisterRotator::Init() 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) { - MkDirPath(fileName.substr(0, nPos).c_str()); + mkdir(fileName.substr(0, nPos).c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO); } } fdinfo = fopen((mmapPath + ".info").c_str(), "w+"); @@ -119,13 +118,5 @@ void LogPersisterRotator::SetId(uint32_t pId) id = pId; } -int LogPersisterRotator::MkDirPath(const char *pMkdir) -{ - int isCreate = mkdir(pMkdir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO); - if (!isCreate) - cout << "create path:" << pMkdir << endl; - return isCreate; -} - } // namespace HiviewDFX -} // namespace OHOS +} // namespace OHOS \ No newline at end of file diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index f2a4ffd..e788b2d 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -48,6 +48,7 @@ constexpr int DEFAULT_LOG_LEVEL = 1<version = 0; @@ -94,7 +95,7 @@ void HandleNextRequest(std::shared_ptr logReader, HilogBuffer& buffer buffer.Query(logReader); } -void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReader, HilogBuffer* buffer) +void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReader, HilogBuffer& buffer) { char msgToSend[MAX_DATA_LEN]; const uint16_t sendMsgLen = sizeof(LogPersistStartResult); @@ -139,18 +140,18 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade pLogPersistStartMsg->filePath, pLogPersistStartMsg->fileSize, pLogPersistStartMsg->compressAlg, - SLEEP_TIME, rotator, buffer); + SLEEP_TIME, *rotator, buffer); persister->queryCondition.types = pLogPersistStartMsg->logType; persister->queryCondition.levels = DEFAULT_LOG_LEVEL; - persister->saveInfo(pLogPersistStartMsg); + int saveInfoRes = persister->SaveInfo(*pLogPersistStartMsg); pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; pLogPersistStartRst->result = persister->Init(); - if (pLogPersistStartRst->result == RET_FAIL) { - cout << "pLogPersistStartRst->result" << pLogPersistStartRst->result << endl; + if (pLogPersistStartRst->result == RET_FAIL || saveInfoRes == RET_FAIL) { + cout << "Error initializing log persister!" << endl; persister.reset(); } else { persister->Start(); - buffer->AddLogReader(weak_ptr(persister)); + buffer.AddLogReader(weak_ptr(persister)); } SetMsgHead(&pLogPersistStartRsp->msgHeader, MC_RSP_LOG_PERSIST_START, sendMsgLen); logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); @@ -462,7 +463,7 @@ void LogQuerier::LogQuerierThreadFunc(std::shared_ptr logReader) } break; case MC_REQ_LOG_PERSIST_START: - HandlePersistStartRequest(g_tempBuffer, logReader, hilogBuffer); + HandlePersistStartRequest(g_tempBuffer, logReader, *hilogBuffer); break; case MC_REQ_LOG_PERSIST_STOP: HandlePersistDeleteRequest(g_tempBuffer, logReader); @@ -566,19 +567,18 @@ uint8_t LogQuerier::GetType() const } } -int LogQuerier::RestorePersistJobs(HilogBuffer *_buffer) +int LogQuerier::RestorePersistJobs(HilogBuffer& _buffer) { DIR *dir; - struct dirent *ent; + struct dirent *ent = nullptr; if ((dir = opendir (g_logPersisterDir.c_str())) != NULL) { while ((ent = readdir (dir)) != NULL) { size_t length = strlen(ent->d_name); std::string pPath(ent->d_name, length); - if (length >= 5 && pPath.substr(length - 5, length) == ".info") { + if (length >= INFO_SUFFIX && pPath.substr(length - INFO_SUFFIX, length) == ".info") { if (pPath == "hilog.info") continue; std::cout << "Found a persist job!" << std::endl; - FILE* infile; - infile = fopen((g_logPersisterDir + pPath).c_str(), "r"); + FILE* infile = fopen((g_logPersisterDir + pPath).c_str(), "r"); if (infile == NULL) { std::cout << "Error opening recovery info file!" << std::endl; continue; @@ -597,7 +597,7 @@ int LogQuerier::RestorePersistJobs(HilogBuffer *_buffer) info.msg.filePath, info.msg.fileSize, info.msg.compressAlg, - SLEEP_TIME, rotator, _buffer); + SLEEP_TIME, *rotator, _buffer); int result = persister->Init(); persister->queryCondition.types = info.types; persister->queryCondition.levels = info.levels; @@ -606,7 +606,7 @@ int LogQuerier::RestorePersistJobs(HilogBuffer *_buffer) persister.reset(); } else { persister->Start(); - _buffer->AddLogReader(weak_ptr(persister)); + _buffer.AddLogReader(weak_ptr(persister)); } } } diff --git a/services/hilogd/main.cpp b/services/hilogd/main.cpp index b4ebbde..2b54691 100644 --- a/services/hilogd/main.cpp +++ b/services/hilogd/main.cpp @@ -50,13 +50,6 @@ static void SigHandler(int sig) } } -void StartUpCheck() -{ - std::shared_ptr logQuerier = std::make_shared(nullptr, - CmdExecutor::getHilogBuffer()); - logQuerier->RestorePersistJobs(CmdExecutor::getHilogBuffer()); -} - int HilogdEntry(int argc, char* argv[]) { HilogBuffer hilogBuffer; @@ -90,8 +83,11 @@ int HilogdEntry(int argc, char* argv[]) server.RunServingThread(); } - std::thread StartupCheckThread(StartUpCheck); - StartupCheckThread.detach(); + std::thread startupCheckThread([&hilogBuffer]() { + std::shared_ptr logQuerier = std::make_shared(nullptr, &hilogBuffer); + logQuerier->RestorePersistJobs(hilogBuffer); + }); + startupCheckThread.detach(); CmdExecutor cmdExecutor(&hilogBuffer); cmdExecutor.StartCmdExecutorThread(); -- Gitee From a9da8455f2e7b673e9c91fc6dffbacfd0096ea85 Mon Sep 17 00:00:00 2001 From: heartofrainbow <2826256824@qq.com> Date: Thu, 29 Jul 2021 12:49:47 +0800 Subject: [PATCH 23/24] hilogd.rc: create persister directory Signed-off-by: heartofrainbow <2826256824@qq.com> --- services/hilogd/etc/hilogd.rc | 1 + services/hilogd/include/log_persister_rotator.h | 2 +- services/hilogd/log_persister.cpp | 2 +- services/hilogd/log_persister_rotator.cpp | 1 - 4 files changed, 3 insertions(+), 3 deletions(-) diff --git a/services/hilogd/etc/hilogd.rc b/services/hilogd/etc/hilogd.rc index ceace43..627c786 100644 --- a/services/hilogd/etc/hilogd.rc +++ b/services/hilogd/etc/hilogd.rc @@ -15,6 +15,7 @@ on early-init write /proc/sys/net/unix/max_dgram_qlen 600 on post-fs-data + mkdir /data/misc/logd/ 0770 logd logd start hilogd service hilogd /system/bin/hilogd diff --git a/services/hilogd/include/log_persister_rotator.h b/services/hilogd/include/log_persister_rotator.h index 0cd2c47..4d3dad0 100644 --- a/services/hilogd/include/log_persister_rotator.h +++ b/services/hilogd/include/log_persister_rotator.h @@ -22,7 +22,7 @@ const std::string ANXILLARY_FILE_NAME = "persisterInfo_"; class LogPersisterRotator { public: LogPersisterRotator(std::string path, uint32_t fileSize, uint32_t fileNum, std::string suffix = ""); - virtual ~LogPersisterRotator() { + virtual ~LogPersisterRotator(){ fclose(fdinfo); } void Init(); diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 3937ce0..4883fa3 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -423,7 +423,7 @@ int LogPersister::SaveInfo(LogPersistStartMsg& pMsg) printf("Failed to save persister file path\n"); return RET_FAIL; } - printf("Saved Path=%s\n", info.msg. filePath); + printf("Saved Path=%s\n", info.msg.filePath); return RET_SUCCESS; } } // namespace HiviewDFX diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index bdf9315..67e0982 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -117,6 +117,5 @@ void LogPersisterRotator::SetId(uint32_t pId) { id = pId; } - } // namespace HiviewDFX } // namespace OHOS \ No newline at end of file -- Gitee From 2e1f189baeed766f383a6e5913e38064556f3dfd Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Thu, 29 Jul 2021 07:50:13 +0000 Subject: [PATCH 24/24] code format Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/include/log_persister_rotator.h | 5 +++-- services/hilogd/log_persister.cpp | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/services/hilogd/include/log_persister_rotator.h b/services/hilogd/include/log_persister_rotator.h index 4d3dad0..fd2a147 100644 --- a/services/hilogd/include/log_persister_rotator.h +++ b/services/hilogd/include/log_persister_rotator.h @@ -22,7 +22,8 @@ const std::string ANXILLARY_FILE_NAME = "persisterInfo_"; class LogPersisterRotator { public: LogPersisterRotator(std::string path, uint32_t fileSize, uint32_t fileNum, std::string suffix = ""); - virtual ~LogPersisterRotator(){ + ~LogPersisterRotator() + { fclose(fdinfo); } void Init(); @@ -32,7 +33,7 @@ public: void SetIndex(int pIndex); void SetId(uint32_t pId); protected: - virtual void InternalRotate(); + void InternalRotate(); uint32_t fileNum; uint32_t fileSize; std::string fileName; diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 4883fa3..1ac6fe4 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -420,10 +420,10 @@ int LogPersister::SaveInfo(LogPersistStartMsg& pMsg) info.types = queryCondition.types; info.levels = queryCondition.levels; if (strcpy_s(info.msg.filePath, FILE_PATH_MAX_LEN, pMsg.filePath) != 0) { - printf("Failed to save persister file path\n"); + cout << "Failed to save persister file path" << endl; return RET_FAIL; } - printf("Saved Path=%s\n", info.msg.filePath); + cout << "Saved Path=" << info.msg.filePath << endl; return RET_SUCCESS; } } // namespace HiviewDFX -- Gitee