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 "perf_data_filter.h"
16 #include "measure_filter.h"
17 #include "process_filter.h"
18 #include "slice_filter.h"
19 #include "string_to_numerical.h"
20 namespace SysTuning {
21 namespace TraceStreamer {
PerfDataFilter(TraceDataCache * dataCache,const TraceStreamerFilters * filter)22 PerfDataFilter::PerfDataFilter(TraceDataCache* dataCache, const TraceStreamerFilters* filter)
23 : FilterBase(dataCache, filter), fileIdToRowInFileTable_(INVALID_UINT64), fileIdToRowInChainTable_(INVALID_UINT64)
24 {
25 }
26 PerfDataFilter::~PerfDataFilter() = default;
27
AppendPerfFiles(uint64_t fileId,uint32_t serial,DataIndex symbols,DataIndex filePath)28 size_t PerfDataFilter::AppendPerfFiles(uint64_t fileId, uint32_t serial, DataIndex symbols, DataIndex filePath)
29 {
30 fileIds_.emplace(fileId);
31 auto size = traceDataCache_->GetPerfFilesData()->AppendNewPerfFiles(fileId, serial, symbols, filePath);
32 fileIdToRowInFileTable_.Insert(fileId, serial, size);
33 if (!serial) {
34 fileIdToRow_.insert(std::make_pair(fileId, size));
35 }
36 return size;
37 }
38
AppendPerfCallChain(uint64_t sampleId,uint64_t callChainId,uint64_t vaddrInFile,uint64_t fileId,uint64_t symbolId)39 size_t PerfDataFilter::AppendPerfCallChain(uint64_t sampleId,
40 uint64_t callChainId,
41 uint64_t vaddrInFile,
42 uint64_t fileId,
43 uint64_t symbolId)
44 {
45 auto size = traceDataCache_->GetPerfCallChainData()->AppendNewPerfCallChain(sampleId, callChainId, vaddrInFile,
46 fileId, symbolId);
47 fileIdToRowInChainTable_.Insert(fileId, symbolId, size);
48 return size;
49 }
Finish()50 void PerfDataFilter::Finish()
51 {
52 auto fileIds = traceDataCache_->GetPerfCallChainData()->FileIds();
53 auto symbolsIds = traceDataCache_->GetPerfCallChainData()->SymbolIds();
54 auto size = traceDataCache_->GetPerfCallChainData()->Size();
55 auto filePath = traceDataCache_->GetPerfFilesData()->FilePaths();
56 auto sambols = traceDataCache_->GetPerfFilesData()->Symbols();
57 uint64_t flag = 1;
58 flag = ~(flag << 63);
59 for (auto i = 0; i < size; i++) {
60 if (fileIds_.find(fileIds[i]) == fileIds_.end()) {
61 // When the function name is empty and there is no file information to which the function belongs,
62 // set the function name to the virtual address of the function in the file
63 traceDataCache_->GetPerfCallChainData()->SetName(
64 i, "+0x" + base::number(traceDataCache_->GetPerfCallChainData()->VaddrInFiles()[i] & flag));
65 continue;
66 }
67 if (symbolsIds[i] == -1) {
68 // When the function name is empty, if there has the file Id to which the function belongs,but the symboleid
69 // is -1. Set the function name as "the file name of the function at the top of the callstack + the virtual
70 // address of this function"
71 auto pathIndex = filePath[fileIdToRow_.at(fileIds[i])];
72 auto fullPath = traceDataCache_->GetDataFromDict(pathIndex);
73 auto iPos = fullPath.find_last_of('/');
74 fullPath = fullPath.substr(iPos + 1, -1);
75 traceDataCache_->GetPerfCallChainData()->SetName(
76 i, fullPath + "+0x" + base::number(traceDataCache_->GetPerfCallChainData()->VaddrInFiles()[i] & flag));
77 continue;
78 }
79 // When the function name is empty, if there has the file Id to which the function belongs,and the symboleid
80 // is not -1. Set the function name as the virtual address of this function
81 auto value = fileIdToRowInFileTable_.Find(fileIds[i], symbolsIds[i]);
82 if (value == INVALID_UINT64) {
83 traceDataCache_->GetPerfCallChainData()->SetName(
84 i, "+0x" + base::number(traceDataCache_->GetPerfCallChainData()->VaddrInFiles()[i] & flag));
85 continue;
86 }
87 // The function name is not empty
88 traceDataCache_->GetPerfCallChainData()->SetName(i, traceDataCache_->GetDataFromDict(sambols[value]));
89 }
90 fileIdToRowInFileTable_.Clear();
91 fileIdToRowInChainTable_.Clear();
92 fileIds_.clear();
93 fileIdToRow_.clear();
94 }
95 } // namespace TraceStreamer
96 } // namespace SysTuning
97