1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci#ifndef DFX_INSTR_STATISTIC_H
16800b99b8Sopenharmony_ci#define DFX_INSTR_STATISTIC_H
17800b99b8Sopenharmony_ci
18800b99b8Sopenharmony_ci#include <cstdint>
19800b99b8Sopenharmony_ci#include <string>
20800b99b8Sopenharmony_ci#include <unordered_map>
21800b99b8Sopenharmony_ci#include <vector>
22800b99b8Sopenharmony_ci
23800b99b8Sopenharmony_ci#ifdef INSTR_STATISTIC_ENABLE
24800b99b8Sopenharmony_ci#define INSTR_STATISTIC_ELF(elf) \
25800b99b8Sopenharmony_ci    OHOS::HiviewDFX::DfxInstrStatistic::GetInstance().SetCurrentStatLib(elf)
26800b99b8Sopenharmony_ci#define INSTR_STATISTIC(Type, Arg1, Arg2) \
27800b99b8Sopenharmony_ci    OHOS::HiviewDFX::DfxInstrStatistic::GetInstance().AddInstrStatistic(Type, (uint64_t)Arg1, (uint64_t)Arg2)
28800b99b8Sopenharmony_ci#else
29800b99b8Sopenharmony_ci#define INSTR_STATISTIC_ELF(elf)
30800b99b8Sopenharmony_ci#define INSTR_STATISTIC(Type, Arg1, Arg2)
31800b99b8Sopenharmony_ci#endif
32800b99b8Sopenharmony_ci
33800b99b8Sopenharmony_cinamespace OHOS {
34800b99b8Sopenharmony_cinamespace HiviewDFX {
35800b99b8Sopenharmony_cienum InstrStatisticType : uint32_t {
36800b99b8Sopenharmony_ci    InstructionEntriesArmExidx = 0,
37800b99b8Sopenharmony_ci    InstructionEntriesEhFrame,
38800b99b8Sopenharmony_ci    InstructionEntriesDebugFrame,
39800b99b8Sopenharmony_ci
40800b99b8Sopenharmony_ci    UnsupportedArmExidx = 10,
41800b99b8Sopenharmony_ci    UnsupportedDwarfOp_Reg,
42800b99b8Sopenharmony_ci    UnsupportedDwarfOp_Regx,
43800b99b8Sopenharmony_ci    UnsupportedDwarfOp_Breg,
44800b99b8Sopenharmony_ci    UnsupportedDwarfOp_Bregx,
45800b99b8Sopenharmony_ci    UnsupportedDwCfaOffset,
46800b99b8Sopenharmony_ci    UnsupportedDwCfaValOffset,
47800b99b8Sopenharmony_ci    UnsupportedDwCfaRegister,
48800b99b8Sopenharmony_ci    UnsupportedDwCfaExpr,
49800b99b8Sopenharmony_ci    UnsupportedDwCfaValExpr,
50800b99b8Sopenharmony_ci    UnsupportedDwCfaRestore,
51800b99b8Sopenharmony_ci    UnsupportedDwCfaUndefined,
52800b99b8Sopenharmony_ci    UnsupportedDwCfaSame,
53800b99b8Sopenharmony_ci    UnsupportedDefCfa,
54800b99b8Sopenharmony_ci};
55800b99b8Sopenharmony_ci
56800b99b8Sopenharmony_ciclass DfxInstrStatistic {
57800b99b8Sopenharmony_cipublic:
58800b99b8Sopenharmony_ci    typedef std::unordered_map<uint32_t, std::shared_ptr<std::vector<std::pair<uint64_t, uint64_t>>>> STAT_INFO_MAP;
59800b99b8Sopenharmony_ci    static DfxInstrStatistic &GetInstance();
60800b99b8Sopenharmony_ci    virtual ~DfxInstrStatistic() { statisticInfo_.clear(); }
61800b99b8Sopenharmony_ci
62800b99b8Sopenharmony_ci    void SetCurrentStatLib(const std::string soName);
63800b99b8Sopenharmony_ci    void AddInstrStatistic(InstrStatisticType type, uint64_t val, uint64_t err);
64800b99b8Sopenharmony_ci    void DumpInstrStatResult(std::vector<std::pair<uint32_t, uint32_t>> &result);
65800b99b8Sopenharmony_ciprivate:
66800b99b8Sopenharmony_ci    DfxInstrStatistic() = default;
67800b99b8Sopenharmony_ci    std::string soName_;
68800b99b8Sopenharmony_ci    STAT_INFO_MAP statisticInfo_;
69800b99b8Sopenharmony_ci};
70800b99b8Sopenharmony_ci} // namespace HiviewDFX
71800b99b8Sopenharmony_ci} // namespace OHOS
72800b99b8Sopenharmony_ci#endif