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 "paged_memory_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 {
PagedMemoryDataParser(TraceDataCache * dataCache,const TraceStreamerFilters * ctx)22 PagedMemoryDataParser::PagedMemoryDataParser(TraceDataCache* dataCache, const TraceStreamerFilters* ctx)
23 : EventParserBase(dataCache, ctx),
24 EbpfBase(dataCache, ctx),
25 timeParser_(std::make_unique<HtracePluginTimeParser>(dataCache, ctx))
26 {
27 }
~PagedMemoryDataParser()28 PagedMemoryDataParser::~PagedMemoryDataParser()
29 {
30 TS_LOGI("EBPF paged memory data ts MIN:%llu, MAX:%llu",
31 static_cast<unsigned long long>(timeParser_->GetPluginStartTime()),
32 static_cast<unsigned long long>(timeParser_->GetPluginEndTime()));
33 }
ParsePagedMemoryEvent()34 void PagedMemoryDataParser::ParsePagedMemoryEvent()
35 {
36 if (!reader_->GetPagedMemoryMap().size()) {
37 return;
38 }
39 for (auto mapItor = reader_->GetPagedMemoryMap().begin();
40 mapItor != reader_->GetPagedMemoryMap().end(); mapItor++) {
41 streamFilters_->statFilter_->IncreaseStat(TRACE_EVENT_EBPF_PAGED_MEMORY, STAT_EVENT_RECEIVED);
42 auto pagedMemoryFixedHeadrAddr = mapItor->second;
43 bool callIdExistFlag = false;
44
45 auto userIpsAddr = reinterpret_cast<const uint64_t*>(pagedMemoryFixedHeadrAddr + 1);
46 if (pagedMemoryFixedHeadrAddr->nips) {
47 std::string ipsToStr(reinterpret_cast<const char*>(userIpsAddr),
48 pagedMemoryFixedHeadrAddr->nips * SINGLE_IP_SIZE);
49 auto ipsHashValue = hashFun_(ipsToStr);
50 auto value = pidAndipsToCallId_.Find(pagedMemoryFixedHeadrAddr->pid, ipsHashValue);
51 if (value != INVALID_UINT64) {
52 callIdExistFlag = true;
53 currentCallId_ = value;
54 } else {
55 pidAndipsToCallId_.Insert(pagedMemoryFixedHeadrAddr->pid, ipsHashValue, callChainId_);
56 currentCallId_ = callChainId_++;
57 }
58 } else {
59 currentCallId_ = INVALID_UINT64;
60 }
61
62 auto type = pagedMemoryFixedHeadrAddr->type;
63 // Init process name data
64 const char* processName = reinterpret_cast<const char*>(pagedMemoryFixedHeadrAddr->comm);
65 uint32_t ipid =
66 streamFilters_->processFilter_->UpdateOrCreateProcessWithName(pagedMemoryFixedHeadrAddr->pid, processName);
67 uint32_t itid = streamFilters_->processFilter_->GetOrCreateThreadWithPid(pagedMemoryFixedHeadrAddr->tid,
68 pagedMemoryFixedHeadrAddr->pid);
69 uint64_t startTs = pagedMemoryFixedHeadrAddr->startTime;
70 uint64_t endTs = pagedMemoryFixedHeadrAddr->endTime;
71 auto newStartTs = streamFilters_->clockFilter_->ToPrimaryTraceTime(clockId_, startTs);
72 timeParser_->UpdatePluginTimeRange(clockId_, startTs, newStartTs);
73 auto newEndTs = streamFilters_->clockFilter_->ToPrimaryTraceTime(clockId_, endTs);
74 timeParser_->UpdatePluginTimeRange(clockId_, endTs, newEndTs);
75 if (newStartTs > newEndTs) {
76 TS_LOGE("paged memory startTs = %lu, endTs = %lu, newStartTs = %lu, newEndTs = %lu", startTs, endTs,
77 newStartTs, newEndTs);
78 streamFilters_->statFilter_->IncreaseStat(TRACE_EVENT_EBPF_PAGED_MEMORY, STAT_EVENT_DATA_INVALID);
79 return;
80 }
81 uint64_t duration = newEndTs - newStartTs;
82
83 auto addr = ConvertToHexTextIndex(pagedMemoryFixedHeadrAddr->addr);
84 auto size = pagedMemoryFixedHeadrAddr->size;
85
86 traceDataCache_->GetPagedMemorySampleData()->AppendNewData(currentCallId_, type, ipid, newStartTs, newEndTs,
87 duration, size, addr, itid);
88 if (!callIdExistFlag) {
89 ParseCallStackData(userIpsAddr, pagedMemoryFixedHeadrAddr->nips,
90 pagedMemoryFixedHeadrAddr->pid, currentCallId_);
91 }
92 }
93 }
94 } // namespace TraceStreamer
95 } // namespace SysTuning
96