• 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 "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)
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 || serial == INVALID_UINT32) {
34         fileIdToRow_.insert(std::make_pair(fileId, size));
35     }
36     return size;
37 }
38 
AppendPerfCallChain(uint32_t callChainId,uint32_t depth,uint64_t ip,uint64_t vaddrInFile,uint64_t fileId,uint64_t symbolId)39 void PerfDataFilter::AppendPerfCallChain(uint32_t callChainId,
40                                          uint32_t depth,
41                                          uint64_t ip,
42                                          uint64_t vaddrInFile,
43                                          uint64_t fileId,
44                                          uint64_t symbolId)
45 {
46     traceDataCache_->GetPerfCallChainData()->AppendNewPerfCallChain(callChainId, depth, ip, vaddrInFile, fileId,
47                                                                     symbolId);
48 }
BeforeReload()49 void PerfDataFilter::BeforeReload()
50 {
51     traceDataCache_->GetPerfCallChainData()->Clear();
52     traceDataCache_->GetPerfFilesData()->Clear();
53     fileIdToRowInFileTable_.Clear();
54     fileIds_.clear();
55     fileIdToRow_.clear();
56 }
Finish()57 void PerfDataFilter::Finish()
58 {
59     auto fileIds = traceDataCache_->GetPerfCallChainData()->FileIds();
60     auto ips = traceDataCache_->GetPerfCallChainData()->Ips();
61     auto symbolsIds = traceDataCache_->GetPerfCallChainData()->SymbolIds();
62     auto vaddrs = traceDataCache_->GetPerfCallChainData()->VaddrInFiles();
63     auto size = traceDataCache_->GetPerfCallChainData()->Size();
64     auto filePath = traceDataCache_->GetPerfFilesData()->FilePaths();
65     auto sambols = traceDataCache_->GetPerfFilesData()->Symbols();
66     uint64_t flag = 1;
67     flag = ~(flag << FLAG_SHIFT_LEFT);
68     for (auto i = 0; i < size; i++) {
69         if (fileIds[i] == INVALID_UINT64) {
70             auto nameIndex = traceDataCache_->GetDataIndex("@0x" + base::number(ips[i], base::INTEGER_RADIX_TYPE_HEX));
71             traceDataCache_->GetPerfCallChainData()->SetName(i, nameIndex);
72             continue;
73         }
74         if (vaddrs[i] == 0 || symbolsIds[i] == -1) {
75             auto pathIndex = filePath[fileIdToRow_.at(fileIds[i])];
76             auto fullPath = traceDataCache_->GetDataFromDict(pathIndex);
77             auto iPos = fullPath.find_last_of('/');
78             fullPath = fullPath.substr(iPos + 1, -1);
79             auto nameIndex = traceDataCache_->GetDataIndex(fullPath + "@0x" +
80                                                            base::number(ips[i] & flag, base::INTEGER_RADIX_TYPE_HEX));
81             traceDataCache_->GetPerfCallChainData()->SetName(i, nameIndex);
82             continue;
83         }
84         // if there has the file Id to which the function belongs,and the symboleid is not -1 and vaddrinfile is not -1.
85         // Set the function name as the virtual address of this function
86         auto value = fileIdToRowInFileTable_.Find(fileIds[i], symbolsIds[i]);
87         if (value == INVALID_UINT64) {
88             auto nameIndex = traceDataCache_->GetDataIndex(
89                 "+0x" + base::number(traceDataCache_->GetPerfCallChainData()->VaddrInFiles()[i] & flag));
90             traceDataCache_->GetPerfCallChainData()->SetName(i, nameIndex);
91             continue;
92         }
93         // The function name is not empty
94         traceDataCache_->GetPerfCallChainData()->SetName(i, sambols[value]);
95     }
96     fileIdToRowInFileTable_.Clear();
97     fileIds_.clear();
98     fileIdToRow_.clear();
99 }
100 } // namespace TraceStreamer
101 } // namespace SysTuning
102