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.h"
17 #include "htrace_event_parser.h"
18 #include "process_filter.h"
19 #include "stat_filter.h"
20 namespace SysTuning {
21 namespace TraceStreamer {
HtraceHiLogParser(TraceDataCache * dataCache,const TraceStreamerFilters * ctx)22 HtraceHiLogParser::HtraceHiLogParser(TraceDataCache* dataCache, const TraceStreamerFilters* ctx)
23 : streamFilters_(ctx), traceDataCache_(dataCache)
24 {
25 if (!traceDataCache_) {
26 TS_LOGE("traceDataCache_ should not be null");
27 return;
28 }
29 if (!streamFilters_) {
30 TS_LOGE("streamFilters_ should not be null");
31 return;
32 }
33 }
34
~HtraceHiLogParser()35 HtraceHiLogParser::~HtraceHiLogParser()
36 {
37 TS_LOGI("hilog ts MIN:%llu, MAX:%llu",
38 static_cast<unsigned long long>(traceStartTime_), static_cast<unsigned long long>(traceEndTime_));
39 }
Parse(HilogInfo & tracePacket)40 void HtraceHiLogParser::Parse(HilogInfo& tracePacket)
41 {
42 if (!tracePacket.info_size()) {
43 return;
44 }
45 if (!traceDataCache_) {
46 TS_LOGE("traceDataCache_ should not be null");
47 return;
48 }
49 if (!streamFilters_) {
50 TS_LOGE("streamFilters_ should not be null");
51 return;
52 }
53 for (int i = 0; i < tracePacket.info_size(); i++) {
54 auto hilogLine = tracePacket.mutable_info(i);
55 uint64_t curLineSeq = hilogLine->id();
56 streamFilters_->statFilter_->IncreaseStat(TRACE_HILOG, STAT_EVENT_RECEIVED);
57 if (curLineSeq < lastLineSeq_ + 1) {
58 streamFilters_->statFilter_->IncreaseStat(TRACE_HILOG, STAT_EVENT_NOTMATCH);
59 } else if (curLineSeq > lastLineSeq_ + 1) {
60 streamFilters_->statFilter_->IncreaseStat(TRACE_HILOG, STAT_EVENT_DATA_LOST);
61 }
62 lastLineSeq_ = curLineSeq;
63 auto logData = traceDataCache_->GetDataIndex(hilogLine->context().c_str());
64 auto logDetails = hilogLine->detail();
65
66 auto timeStamp = logDetails.tv_nsec() + logDetails.tv_sec() * SEC_TO_NS;
67 auto newTimeStamp = streamFilters_->clockFilter_->ToPrimaryTraceTime(TS_CLOCK_REALTIME, timeStamp);
68 if (newTimeStamp != timeStamp) { // record the time only when the time is valid
69 traceStartTime_ = std::min(traceStartTime_, newTimeStamp);
70 traceEndTime_ = std::max(traceEndTime_, newTimeStamp);
71 }
72
73 streamFilters_->processFilter_->GetOrCreateThreadWithPid(logDetails.tid(), logDetails.pid());
74 auto iter = logLevelString_.find(logDetails.level());
75 if (iter == logLevelString_.end()) {
76 streamFilters_->statFilter_->IncreaseStat(TRACE_HILOG, STAT_EVENT_DATA_INVALID);
77 TS_LOGD("log level do not exit!!!");
78 continue;
79 }
80 DataIndex levelData = traceDataCache_->dataDict_.GetStringIndex(iter->second.c_str());
81 DataIndex logTag = traceDataCache_->dataDict_.GetStringIndex(logDetails.tag().c_str());
82 traceDataCache_->GetHilogData()->AppendNewLogInfo(curLineSeq, newTimeStamp, logDetails.pid(), logDetails.tid(),
83 levelData, logTag, logData, timeStamp);
84 }
85 }
Finish()86 void HtraceHiLogParser::Finish()
87 {
88 traceDataCache_->MixTraceTime(traceStartTime_, traceEndTime_);
89 }
90 } // namespace TraceStreamer
91 } // namespace SysTuning
92