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