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 "bio_latency_data_parser.h"
16 #include "process_filter.h"
17 #include "stat_filter.h"
18 #include "string_to_numerical.h"
19
20 namespace SysTuning {
21 namespace TraceStreamer {
BioLatencyDataParser(TraceDataCache * dataCache,const TraceStreamerFilters * ctx)22 BioLatencyDataParser::BioLatencyDataParser(TraceDataCache* dataCache, const TraceStreamerFilters* ctx)
23 : EventParserBase(dataCache, ctx),
24 EbpfBase(dataCache, ctx),
25 timeParser_(std::make_unique<HtracePluginTimeParser>(dataCache, ctx))
26 {
27 }
~BioLatencyDataParser()28 BioLatencyDataParser::~BioLatencyDataParser()
29 {
30 TS_LOGI("EBPF Bio data ts MIN:%llu, MAX:%llu", static_cast<unsigned long long>(timeParser_->GetPluginStartTime()),
31 static_cast<unsigned long long>(timeParser_->GetPluginEndTime()));
32 }
ParseBioLatencyEvent()33 void BioLatencyDataParser::ParseBioLatencyEvent()
34 {
35 if (!reader_->GetBIOSampleMap().size()) {
36 return;
37 }
38 for (auto mapItor = reader_->GetBIOSampleMap().begin(); mapItor != reader_->GetBIOSampleMap().end(); mapItor++) {
39 streamFilters_->statFilter_->IncreaseStat(TRACE_EVENT_EBPF_BIO_LATENCY, STAT_EVENT_RECEIVED);
40 auto bioFixedHeadrAddr = mapItor->second;
41 bool callIdExistFlag = false;
42
43 auto userIpsAddr = reinterpret_cast<const uint64_t*>(bioFixedHeadrAddr + 1);
44 if (bioFixedHeadrAddr->nips) {
45 std::string ipsToStr(reinterpret_cast<const char*>(userIpsAddr), bioFixedHeadrAddr->nips * SINGLE_IP_SIZE);
46 auto ipsHashValue = hashFun_(ipsToStr);
47 auto value = pidAndipsToCallId_.Find(bioFixedHeadrAddr->pid, ipsHashValue);
48 if (value != INVALID_UINT64) {
49 callIdExistFlag = true;
50 currentCallId_ = value;
51 } else {
52 pidAndipsToCallId_.Insert(bioFixedHeadrAddr->pid, ipsHashValue, callChainId_);
53 currentCallId_ = callChainId_++;
54 }
55 } else {
56 currentCallId_ = INVALID_UINT64;
57 }
58
59 uint32_t type = bioFixedHeadrAddr->type;
60 // Init process name data
61 const char* processName = reinterpret_cast<const char*>(bioFixedHeadrAddr->processName);
62 uint32_t ipid =
63 streamFilters_->processFilter_->UpdateOrCreateProcessWithName(bioFixedHeadrAddr->pid, processName);
64 uint32_t itid =
65 streamFilters_->processFilter_->GetOrCreateThreadWithPid(bioFixedHeadrAddr->tid, bioFixedHeadrAddr->pid);
66 uint64_t startTs = bioFixedHeadrAddr->startTime;
67 uint64_t endTs = bioFixedHeadrAddr->endTime;
68 auto newStartTs = streamFilters_->clockFilter_->ToPrimaryTraceTime(clockId_, startTs);
69 timeParser_->UpdatePluginTimeRange(clockId_, startTs, newStartTs);
70 auto newEndTs = streamFilters_->clockFilter_->ToPrimaryTraceTime(clockId_, endTs);
71 timeParser_->UpdatePluginTimeRange(clockId_, endTs, newEndTs);
72 if (newStartTs > newEndTs) {
73 TS_LOGE("File system event origin startTs = %lu, endTs = %lu, newStartTs = %lu, newEndTs = %lu", startTs,
74 endTs, newStartTs, newEndTs);
75 streamFilters_->statFilter_->IncreaseStat(TRACE_EVENT_EBPF_PAGED_MEMORY, STAT_EVENT_DATA_INVALID);
76 return;
77 }
78 uint64_t duration = newEndTs - newStartTs;
79 auto prio = bioFixedHeadrAddr->prio;
80 auto size = bioFixedHeadrAddr->size;
81 const int FOUR_KB = 1024 * 4;
82 uint32_t durPer4K = INVALID_UINT32;
83 if (size > 0) {
84 durPer4K = duration / (size / FOUR_KB);
85 }
86
87 auto blkCount = ConvertToHexTextIndex(bioFixedHeadrAddr->blkcnt);
88
89 auto tracerEventToStrIndexMap = reader_->GetTracerEventToStrIndexMap();
90 auto pathId = tracerEventToStrIndexMap.Find(ITEM_EVENT_BIO, type, itid, startTs);
91 if (pathId != INVALID_UINT64) {
92 tracerEventToStrIndexMap.Erase(ITEM_EVENT_FS, type, itid, startTs);
93 }
94 traceDataCache_->GetBioLatencySampleData()->AppendNewData(currentCallId_, type, ipid, itid, newStartTs,
95 newEndTs, duration, prio, size, blkCount, pathId, durPer4K);
96 if (!callIdExistFlag) {
97 ParseCallStackData(userIpsAddr, bioFixedHeadrAddr->nips, bioFixedHeadrAddr->pid, currentCallId_);
98 }
99 }
100 }
101 } // namespace TraceStreamer
102 } // namespace SysTuning
103