1 /*
2  * Copyright (c) 2023 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 
16 #include "dfx_instr_statistic.h"
17 #include "dfx_define.h"
18 #include "dfx_log.h"
19 
20 namespace OHOS {
21 namespace HiviewDFX {
22 namespace {
23 #undef LOG_DOMAIN
24 #undef LOG_TAG
25 #define LOG_DOMAIN 0xD002D11
26 #define LOG_TAG "DfxInstrStatistic"
27 }
28 
GetInstance()29 DfxInstrStatistic &DfxInstrStatistic::GetInstance()
30 {
31     static DfxInstrStatistic instance;
32     return instance;
33 }
34 
SetCurrentStatLib(const std::string soName)35 void DfxInstrStatistic::SetCurrentStatLib(const std::string soName)
36 {
37     soName_ = soName;
38     statisticInfo_.clear();
39 }
40 
AddInstrStatistic(InstrStatisticType type,uint64_t val,uint64_t err)41 void DfxInstrStatistic::AddInstrStatistic(InstrStatisticType type, uint64_t val, uint64_t err)
42 {
43     if (err != 0) {
44         LOGE("type: %u, val: %" PRIx64 ", err: %" PRIx64 "", type, val, err);
45     } else {
46         LOGU("type: %u, val: %" PRIx64 "", type, val);
47     }
48     std::shared_ptr<std::vector<std::pair<uint64_t, uint64_t>>> stats;
49     auto iter = statisticInfo_.find(static_cast<uint32_t>(type));
50     if (iter != statisticInfo_.end()) {
51         stats = iter->second;
52     } else {
53         stats = std::make_shared<std::vector<std::pair<uint64_t, uint64_t>>>();
54         statisticInfo_[type] = stats;
55     }
56     if (stats != nullptr) {
57         stats->push_back(std::make_pair(val, err));
58     }
59 }
60 
DumpInstrStatResult(std::vector<std::pair<uint32_t,uint32_t>> & result)61 void DfxInstrStatistic::DumpInstrStatResult(std::vector<std::pair<uint32_t, uint32_t>> &result)
62 {
63     auto iter = statisticInfo_.begin();
64     LOGU("++++++Dump Instr Statistic for elf file: %s", soName_.c_str());
65     while (iter != statisticInfo_.end()) {
66         InstrStatisticType type = static_cast<InstrStatisticType>(iter->first);
67         std::shared_ptr<std::vector<std::pair<uint64_t, uint64_t>>> stats = iter->second;
68         if (stats == nullptr) {
69             iter++;
70             continue;
71         }
72         switch (type) {
73             case InstructionEntriesArmExidx:
74             case InstructionEntriesEhFrame:
75             case InstructionEntriesDebugFrame:
76                 LOGU("\t Type: %u, Count: %" PRIu64 "", type, (uint64_t) stats->size());
77                 for (size_t i = 0; i < stats->size(); ++i) {
78                     LOGU("\t Value: %" PRIx64 "", (uint64_t) stats->at(i).first);
79                     result.push_back(std::make_pair(type, static_cast<uint32_t>(stats->at(i).first)));
80                 }
81                 break;
82             default:
83                 LOGU("\t Type: %u, Count: %" PRIu64 "", type, (uint64_t) stats->size());
84                 result.push_back(std::make_pair(type, static_cast<uint32_t>(stats->size())));
85                 break;
86         }
87         iter++;
88     }
89     LOGU("%s", "------Dump Instr Statistic End.");
90 }
91 } // namespace HiviewDFX
92 } // namespace OHOS
93