1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "pbreader_hilog_parser.h"
16 #include "clock_filter_ex.h"
17 #include "event_parser_base.h"
18 #include "hilog_plugin_result.pbreader.h"
19 #include "process_filter.h"
20 #include "stat_filter.h"
21 namespace SysTuning {
22 namespace TraceStreamer {
PbreaderHiLogParser(TraceDataCache * dataCache,const TraceStreamerFilters * ctx)23 PbreaderHiLogParser::PbreaderHiLogParser(TraceDataCache *dataCache, const TraceStreamerFilters *ctx)
24 : EventParserBase(dataCache, ctx)
25 {
26 }
27
~PbreaderHiLogParser()28 PbreaderHiLogParser::~PbreaderHiLogParser()
29 {
30 TS_LOGI("hilog ts MIN:%llu, MAX:%llu", static_cast<unsigned long long>(GetPluginStartTime()),
31 static_cast<unsigned long long>(GetPluginEndTime()));
32 }
Parse(ProtoReader::BytesView tracePacket,bool & haveSplitSeg)33 void PbreaderHiLogParser::Parse(ProtoReader::BytesView tracePacket, bool &haveSplitSeg)
34 {
35 ProtoReader::HilogInfo_Reader hilogInfo(tracePacket.data_, tracePacket.size_);
36 if (!hilogInfo.has_info()) {
37 return;
38 }
39 for (auto i = hilogInfo.info(); i; ++i) {
40 ProtoReader::HilogLine_Reader hilogLine(i->ToBytes());
41 uint64_t curLineSeq = hilogLine.id();
42 streamFilters_->statFilter_->IncreaseStat(TRACE_HILOG, STAT_EVENT_RECEIVED);
43 if (curLineSeq < lastLineSeq_ + 1) {
44 streamFilters_->statFilter_->IncreaseStat(TRACE_HILOG, STAT_EVENT_NOTMATCH);
45 } else if (curLineSeq > lastLineSeq_ + 1) {
46 streamFilters_->statFilter_->IncreaseStat(TRACE_HILOG, STAT_EVENT_DATA_LOST);
47 }
48 lastLineSeq_ = curLineSeq;
49 auto logData = traceDataCache_->GetDataIndex(hilogLine.context().ToStdString());
50 ProtoReader::HilogDetails_Reader logDetails(hilogLine.detail());
51 auto iter = logLevelString_.find(logDetails.level());
52 if (iter == logLevelString_.end()) {
53 streamFilters_->statFilter_->IncreaseStat(TRACE_HILOG, STAT_EVENT_DATA_INVALID);
54 TS_LOGD("log level do not exit!!!");
55 continue;
56 }
57 auto timeStamp = logDetails.tv_nsec() + logDetails.tv_sec() * SEC_TO_NS;
58 auto newTimeStamp = streamFilters_->clockFilter_->ToPrimaryTraceTime(TS_CLOCK_REALTIME, timeStamp);
59 UpdatePluginTimeRange(TS_CLOCK_REALTIME, timeStamp, newTimeStamp);
60 if (traceDataCache_->isSplitFile_) {
61 if (newTimeStamp >= traceDataCache_->SplitFileMinTime() &&
62 newTimeStamp <= traceDataCache_->SplitFileMaxTime()) {
63 haveSplitSeg = true;
64 return;
65 }
66 continue;
67 }
68 DataIndex levelData = traceDataCache_->dataDict_.GetStringIndex(iter->second.c_str());
69 DataIndex logTag = traceDataCache_->dataDict_.GetStringIndex(logDetails.tag().ToStdString());
70 LogInfoRow logInfoRow = {curLineSeq, newTimeStamp, logDetails.pid(), logDetails.tid(),
71 levelData, logTag, logData, timeStamp};
72 traceDataCache_->GetHilogData()->AppendNewLogInfo(logInfoRow);
73 }
74 }
Finish()75 void PbreaderHiLogParser::Finish()
76 {
77 traceDataCache_->MixTraceTime(GetPluginStartTime(), GetPluginEndTime());
78 }
79 } // namespace TraceStreamer
80 } // namespace SysTuning
81