1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
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 "htrace_hilog_parser.h"
16 #include "clock_filter_ex.h"
17 #include "event_parser_base.h"
18 #include "hilog_plugin_result.pbreader.h"
19 #include "htrace_event_parser.h"
20 #include "process_filter.h"
21 #include "stat_filter.h"
22 namespace SysTuning {
23 namespace TraceStreamer {
HtraceHiLogParser(TraceDataCache * dataCache,const TraceStreamerFilters * ctx)24 HtraceHiLogParser::HtraceHiLogParser(TraceDataCache* dataCache, const TraceStreamerFilters* ctx)
25 : EventParserBase(dataCache, ctx)
26 {
27 }
28
~HtraceHiLogParser()29 HtraceHiLogParser::~HtraceHiLogParser()
30 {
31 TS_LOGI("hilog ts MIN:%llu, MAX:%llu", static_cast<unsigned long long>(GetPluginStartTime()),
32 static_cast<unsigned long long>(GetPluginEndTime()));
33 }
Parse(ProtoReader::BytesView tracePacket,bool & haveSplitSeg)34 void HtraceHiLogParser::Parse(ProtoReader::BytesView tracePacket, bool& haveSplitSeg)
35 {
36 ProtoReader::HilogInfo_Reader hilogInfo(tracePacket.data_, tracePacket.size_);
37 if (!hilogInfo.has_info()) {
38 return;
39 }
40 for (auto i = hilogInfo.info(); i; ++i) {
41 ProtoReader::HilogLine_Reader hilogLine(i->ToBytes());
42 uint64_t curLineSeq = hilogLine.id();
43 streamFilters_->statFilter_->IncreaseStat(TRACE_HILOG, STAT_EVENT_RECEIVED);
44 if (curLineSeq < lastLineSeq_ + 1) {
45 streamFilters_->statFilter_->IncreaseStat(TRACE_HILOG, STAT_EVENT_NOTMATCH);
46 } else if (curLineSeq > lastLineSeq_ + 1) {
47 streamFilters_->statFilter_->IncreaseStat(TRACE_HILOG, STAT_EVENT_DATA_LOST);
48 }
49 lastLineSeq_ = curLineSeq;
50 auto logData = traceDataCache_->GetDataIndex(hilogLine.context().ToStdString());
51 ProtoReader::HilogDetails_Reader logDetails(hilogLine.detail());
52 streamFilters_->processFilter_->GetOrCreateThreadWithPid(logDetails.tid(), logDetails.pid());
53 auto iter = logLevelString_.find(logDetails.level());
54 if (iter == logLevelString_.end()) {
55 streamFilters_->statFilter_->IncreaseStat(TRACE_HILOG, STAT_EVENT_DATA_INVALID);
56 TS_LOGD("log level do not exit!!!");
57 continue;
58 }
59 auto timeStamp = logDetails.tv_nsec() + logDetails.tv_sec() * SEC_TO_NS;
60 auto newTimeStamp = streamFilters_->clockFilter_->ToPrimaryTraceTime(TS_CLOCK_REALTIME, timeStamp);
61 UpdatePluginTimeRange(TS_CLOCK_REALTIME, timeStamp, newTimeStamp);
62 if (traceDataCache_->isSplitFile_) {
63 if (newTimeStamp >= traceDataCache_->SplitFileMinTime() &&
64 newTimeStamp <= traceDataCache_->SplitFileMaxTime()) {
65 haveSplitSeg = true;
66 return;
67 }
68 continue;
69 }
70 DataIndex levelData = traceDataCache_->dataDict_.GetStringIndex(iter->second.c_str());
71 DataIndex logTag = traceDataCache_->dataDict_.GetStringIndex(logDetails.tag().ToStdString());
72 traceDataCache_->GetHilogData()->AppendNewLogInfo(curLineSeq, newTimeStamp, logDetails.pid(), logDetails.tid(),
73 levelData, logTag, logData, timeStamp);
74 }
75 }
Finish()76 void HtraceHiLogParser::Finish()
77 {
78 traceDataCache_->MixTraceTime(GetPluginStartTime(), GetPluginEndTime());
79 }
80 } // namespace TraceStreamer
81 } // namespace SysTuning
82