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 stats->push_back(std::make_pair(val, err));
57 }
58
DumpInstrStatResult(std::vector<std::pair<uint32_t,uint32_t>> & result)59 void DfxInstrStatistic::DumpInstrStatResult(std::vector<std::pair<uint32_t, uint32_t>> &result)
60 {
61 auto iter = statisticInfo_.begin();
62 LOGU("++++++Dump Instr Statistic for elf file: %s", soName_.c_str());
63 while (iter != statisticInfo_.end()) {
64 InstrStatisticType type = static_cast<InstrStatisticType>(iter->first);
65 std::shared_ptr<std::vector<std::pair<uint64_t, uint64_t>>> stats = iter->second;
66 switch (type) {
67 case InstructionEntriesArmExidx:
68 case InstructionEntriesEhFrame:
69 case InstructionEntriesDebugFrame:
70 LOGU("\t Type: %u, Count: %" PRIu64 "", type, (uint64_t) stats->size());
71 for (size_t i = 0; i < stats->size(); ++i) {
72 LOGU("\t Value: %" PRIx64 "", (uint64_t) stats->at(i).first);
73 result.push_back(std::make_pair(type, static_cast<uint32_t>(stats->at(i).first)));
74 }
75 break;
76 default:
77 LOGU("\t Type: %u, Count: %" PRIu64 "", type, (uint64_t) stats->size());
78 result.push_back(std::make_pair(type, static_cast<uint32_t>(stats->size())));
79 break;
80 }
81 iter++;
82 }
83 LOGU("------Dump Instr Statistic End.\n");
84 }
85 } // namespace HiviewDFX
86 } // namespace OHOS
87