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