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_GCOV_PROFILE_H 17 #define MAPLE_UTIL_INCLUDE_GCOV_PROFILE_H 18 19 #include "mempool_allocator.h" 20 #include <string> 21 #include <unordered_map> 22 23 namespace maple { 24 typedef unsigned gcov_unsigned_t; 25 typedef int64_t gcov_type; 26 typedef uint64_t gcov_type_unsigned; 27 typedef unsigned location_t; 28 #define HOTCALLSITEFREQ 100 29 30 enum UpdateFreqOp { 31 kKeepOrigFreq = 0, 32 kUpdateOrigFreq = 0x1, 33 kUpdateFreqbyScale = 0x2, 34 kUpdateUnrolledFreq = 0x4, 35 kUpdateUnrollRemainderFreq = 0x8, 36 }; 37 38 class GcovFuncInfo { 39 public: GcovFuncInfo(MapleAllocator * alloc,unsigned funcIdent,unsigned lineno_cs,unsigned cfg_cs)40 GcovFuncInfo(MapleAllocator *alloc, unsigned funcIdent, unsigned lineno_cs, unsigned cfg_cs) 41 : ident(funcIdent), lineno_checksum(lineno_cs), cfg_checksum(cfg_cs), counts(alloc->Adapter()) {}; 42 ~GcovFuncInfo() = default; 43 GetFuncFrequency()44 int64_t GetFuncFrequency() const 45 { 46 return entry_freq; 47 } SetFuncFrequency(int64_t freq)48 void SetFuncFrequency(int64_t freq) 49 { 50 entry_freq = freq; 51 } 52 GetFuncRealFrequency()53 int64_t GetFuncRealFrequency() const 54 { 55 return real_entryfreq; 56 } SetFuncRealFrequency(int64_t freq)57 void SetFuncRealFrequency(int64_t freq) 58 { 59 real_entryfreq = freq; 60 } 61 GetStmtFreqs()62 std::unordered_map<uint32_t, uint64_t> &GetStmtFreqs() 63 { 64 return stmtFreqs; 65 } GetStmtFreq(uint32_t stmtID)66 int64_t GetStmtFreq(uint32_t stmtID) 67 { 68 if (stmtFreqs.count(stmtID) > 0) { 69 return stmtFreqs[stmtID]; 70 } 71 return -1; // unstored 72 } SetStmtFreq(uint32_t stmtID,int64_t freq)73 void SetStmtFreq(uint32_t stmtID, int64_t freq) 74 { 75 stmtFreqs[stmtID] = static_cast<uint64_t>(freq); 76 } EraseStmtFreq(uint32_t stmtID)77 void EraseStmtFreq(uint32_t stmtID) 78 { 79 stmtFreqs.erase(stmtID); 80 } 81 void CopyStmtFreq(uint32_t newStmtID, uint32_t origStmtId, bool deleteOld = false) 82 { 83 DEBUG_ASSERT(GetStmtFreq(origStmtId) >= 0, "origStmtId no freq record"); 84 SetStmtFreq(newStmtID, GetStmtFreq(origStmtId)); 85 if (deleteOld) { 86 EraseStmtFreq(origStmtId); 87 } 88 } IsHotCallSite(uint32_t stmtID)89 bool IsHotCallSite(uint32_t stmtID) 90 { 91 if (stmtFreqs.count(stmtID) > 0) { 92 uint64 freq = stmtFreqs[stmtID]; 93 return (freq >= HOTCALLSITEFREQ); 94 } 95 DEBUG_ASSERT(0, "should not be here"); 96 return false; 97 } 98 unsigned ident; 99 unsigned lineno_checksum; 100 unsigned cfg_checksum; 101 102 // Raw arc coverage counts. 103 unsigned num_counts; 104 MapleVector<gcov_type> counts; 105 int64_t entry_freq; // record entry bb frequence 106 std::unordered_map<uint32_t, uint64_t> stmtFreqs; // stmt_id is key, counter value 107 int64_t real_entryfreq; // function prof data may be modified after clone/inline 108 }; 109 110 class GcovProfileData { 111 public: GcovProfileData(MapleAllocator * alloc)112 GcovProfileData(MapleAllocator *alloc) : funcsCounter(alloc->Adapter()) {} 113 GetFuncProfile(unsigned puidx)114 GcovFuncInfo *GetFuncProfile(unsigned puidx) 115 { 116 if (funcsCounter.count(puidx) > 0) { 117 return funcsCounter[puidx]; 118 } 119 return nullptr; 120 } AddFuncProfile(unsigned puidx,GcovFuncInfo * funcData)121 void AddFuncProfile(unsigned puidx, GcovFuncInfo *funcData) 122 { 123 DEBUG_ASSERT(funcsCounter.count(puidx) == 0, "sanity check"); 124 funcsCounter[puidx] = funcData; 125 } 126 MapleUnorderedMap<unsigned, GcovFuncInfo *> funcsCounter; // use puidx as key 127 }; 128 129 } // namespace maple 130 #endif // MAPLE_UTIL_INCLUDE_GCOV_PROFILE_H 131