• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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 
61     traceDataCache_->GetPagedMemorySampleData()->AppendNewData(currentCallId_, type, ipid, newStartTs, newEndTs,
62                                                                duration, size, addr, itid);
63     return 1;
64 }
65 
ParsePagedMemoryEvent()66 void PagedMemoryDataParser::ParsePagedMemoryEvent()
67 {
68     if (!reader_->GetPagedMemoryMap().size()) {
69         return;
70     }
71     for (auto mapItor = reader_->GetPagedMemoryMap().begin(); mapItor != reader_->GetPagedMemoryMap().end();
72          mapItor++) {
73         streamFilters_->statFilter_->IncreaseStat(TRACE_EVENT_EBPF_PAGED_MEMORY, STAT_EVENT_RECEIVED);
74         auto pagedMemoryFixedHeadrAddr = mapItor->second;
75         bool callIdExistFlag = false;
76 
77         auto userIpsAddr = reinterpret_cast<const uint64_t*>(pagedMemoryFixedHeadrAddr + 1);
78         if (pagedMemoryFixedHeadrAddr->nips) {
79             std::string ipsToStr(reinterpret_cast<const char*>(userIpsAddr),
80                                  pagedMemoryFixedHeadrAddr->nips * SINGLE_IP_SIZE);
81             auto ipsHashValue = hashFun_(ipsToStr);
82             auto value = pidAndipsToCallId_.Find(pagedMemoryFixedHeadrAddr->pid, ipsHashValue);
83             if (value != INVALID_UINT64) {
84                 callIdExistFlag = true;
85                 currentCallId_ = value;
86             } else {
87                 pidAndipsToCallId_.Insert(pagedMemoryFixedHeadrAddr->pid, ipsHashValue, callChainId_);
88                 currentCallId_ = callChainId_++;
89             }
90         } else {
91             currentCallId_ = INVALID_UINT32;
92         }
93 
94         int32_t ret = PagingData(pagedMemoryFixedHeadrAddr);
95         if (1 == ret && !callIdExistFlag) {
96             ParseCallStackData(userIpsAddr, pagedMemoryFixedHeadrAddr->nips, pagedMemoryFixedHeadrAddr->pid,
97                                currentCallId_);
98         }
99     }
100 }
101 } // namespace TraceStreamer
102 } // namespace SysTuning
103