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 #ifndef MAPLE_UTIL_INCLUDE_PROFILE_H 17 #define MAPLE_UTIL_INCLUDE_PROFILE_H 18 #include <string> 19 #include <unordered_set> 20 #include <unordered_map> 21 #include <vector> 22 #include "profile_type.h" 23 #include "mpl_logging.h" 24 #include "types_def.h" 25 #include "option.h" 26 27 namespace maple { 28 struct IRProfileDesc { 29 uint32 counterStart = 0; 30 uint32 counterEnd = 0; 31 uint64 funcHash = 0; 32 IRProfileDesc() = default; IRProfileDescIRProfileDesc33 IRProfileDesc(uint64 hash, uint32 start, uint32 end) : counterStart(start), counterEnd(end), funcHash(hash) {} 34 ~IRProfileDesc() = default; 35 }; 36 37 class Profile { 38 public: 39 struct FuncItem { 40 uint32 callTimes; 41 uint8 type; 42 }; 43 // function ir profile info 44 struct BBInfo { 45 uint64 funcHash = 0; 46 uint32 totalCounter = 0; 47 std::vector<uint32> counter; 48 BBInfo() = default; BBInfoBBInfo49 BBInfo(uint64 hash, uint32 num, std::vector<uint32> &&counter) 50 : funcHash(hash), totalCounter(num), counter(counter) 51 { 52 } BBInfoBBInfo53 BBInfo(uint64 hash, uint32 num, const std::initializer_list<uint32> &iList) 54 : funcHash(hash), totalCounter(num), counter(iList) 55 { 56 } 57 ~BBInfo() = default; 58 }; 59 60 static const uint8 stringEnd; 61 bool CheckFuncHot(const std::string &className) const; 62 bool CheckMethodHot(const std::string &className) const; 63 bool CheckMethodSigHot(const std::string &methodSigStr) const; 64 bool CheckFieldHot(const std::string &className) const; 65 bool CheckClassHot(const std::string &className) const; 66 bool CheckLiteralHot(const std::string &literal) const; 67 bool CheckReflectionStrHot(const std::string &str, uint8 &layoutType) const; 68 // default get all kind profile 69 bool DeCompress(const std::string &fileName, const std::string &dexName, ProfileType type = kAll); 70 const std::unordered_map<std::string, FuncItem> &GetFunctionProf() const; 71 bool GetFunctionBBProf(const std::string &funcName, BBInfo &result); 72 size_t GetLiteralProfileSize() const; 73 bool CheckProfValid() const; 74 bool CheckDexValid(uint32 idx) const; 75 void SetProfileMode(); 76 void Dump() const; 77 void DumpFuncIRProfUseInfo() const; 78 void SetFuncStatus(const std::string &funcName, bool succ); 79 Profile(); 80 ~Profile() = default; 81 IsValid()82 bool IsValid() const 83 { 84 return valid; 85 } 86 87 private: 88 bool valid = false; 89 bool profileMode = false; 90 bool isCoreSo = false; 91 bool isAppProfile = false; 92 static bool debug; 93 static uint32 hotFuncCountThreshold; 94 static bool initialized; 95 std::vector<std::string> strMap; 96 std::string dexName; 97 std::string appPackageName; 98 std::unordered_set<std::string> classMeta; 99 std::unordered_set<std::string> methodMeta; 100 std::unordered_set<std::string> fieldMeta; 101 std::unordered_set<std::string> methodSigMeta; 102 std::unordered_set<std::string> literal; 103 std::unordered_map<std::string, uint8> reflectionStrData; 104 std::unordered_map<std::string, Profile::FuncItem> funcProfData; 105 std::unordered_set<std::string> &GetMeta(uint8 type); 106 std::unordered_map<std::string, BBInfo> funcBBProfData; 107 std::unordered_map<std::string, bool> funcBBProfUseInfo; 108 std::unordered_map<std::string, IRProfileDesc> funcDesc; 109 std::vector<uint32> counterTab; 110 static const std::string preClassHot[]; 111 bool CheckProfileHeader(const Header &header) const; 112 std::string GetProfileNameByType(uint8 type) const; 113 std::string GetFunctionName(uint32 classIdx, uint32 methodIdx, uint32 sigIdx) const; 114 std::string GetMethodSigStr(uint32 methodIdx, uint32 sigIdx) const; 115 void ParseMethodSignature(const char *data, int fileNum, std::unordered_set<std::string> &metaData) const; 116 void ParseMeta(const char *data, int fileNum, std::unordered_set<std::string> &metaData) const; 117 void ParseReflectionStr(const char *data, int fileNum); 118 void ParseFunc(const char *data, int fileNum); 119 void ParseLiteral(const char *data, const char *end); 120 void ParseIRFuncDesc(const char *data, int fileNum); 121 void ParseCounterTab(const char *data, int fileNum); 122 }; 123 } // namespace maple 124 #endif // MAPLE_UTIL_INCLUDE_PROFILE_H 125