1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved. 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 NATIVE_MEMORY_STDTYPE_H 17 #define NATIVE_MEMORY_STDTYPE_H 18 #include "base_stdtype.h" 19 #include <unordered_map> 20 #include <unordered_set> 21 22 namespace SysTuning { 23 namespace TraceStdtype { 24 class NativeHookSampleBase : public CacheBase { 25 public: 26 void AppendNativeHookSampleBase(uint32_t callChainId, uint32_t ipid, uint32_t itid, uint64_t timeStamp); 27 void AppendNativeHookSampleBase(uint32_t callChainId, uint32_t ipid, uint64_t timeStamp); 28 const std::deque<uint32_t> &CallChainIds() const; 29 const std::deque<uint32_t> &Ipids() const; 30 const std::deque<uint64_t> &LastCallerPathIndexs() const; 31 const std::deque<uint64_t> &LastSymbolIndexs() const; 32 void UpdateLastCallerPathAndSymbolIndexs( 33 std::unordered_map<uint32_t, std::tuple<DataIndex, DataIndex>> &callIdToLasLibId); Clear()34 void Clear() override 35 { 36 CacheBase::Clear(); 37 callChainIds_.clear(); 38 ipids_.clear(); 39 lastCallerPathIndexs_.clear(); 40 lastSymbolIndexs_.clear(); 41 } 42 43 public: 44 std::deque<uint32_t> callChainIds_ = {}; 45 std::deque<uint32_t> ipids_ = {}; 46 std::deque<DataIndex> lastCallerPathIndexs_ = {}; 47 std::deque<DataIndex> lastSymbolIndexs_ = {}; 48 }; 49 50 struct NativeHookRow { 51 uint32_t callChainId = INVALID_UINT32; 52 uint32_t ipid = INVALID_UINT32; 53 uint32_t itid = INVALID_UINT32; 54 std::string eventType; 55 DataIndex subType = INVALID_DATAINDEX; 56 uint64_t timeStamp = INVALID_UINT64; 57 uint64_t endTimeStamp = INVALID_UINT64; 58 uint64_t duration = INVALID_UINT64; 59 uint64_t addr = INVALID_UINT64; 60 int64_t memSize = INVALID_UINT64; 61 }; 62 class NativeHook : public NativeHookSampleBase { 63 public: 64 size_t AppendNewNativeHookData(const NativeHookRow &context); 65 void UpdateCallChainId(size_t row, uint32_t callChainId); 66 void UpdateEndTimeStampAndDuration(size_t row, uint64_t endTimeStamp); 67 void UpdateCurrentSizeDur(size_t row, uint64_t timeStamp); 68 void UpdateMemMapSubType(uint64_t row, uint64_t tagId); 69 const std::deque<std::string> &EventTypes() const; 70 const std::deque<DataIndex> &SubTypes() const; 71 const std::deque<uint64_t> &EndTimeStamps() const; 72 const std::deque<uint64_t> &Durations() const; 73 const std::deque<uint64_t> &Addrs() const; 74 const std::deque<int64_t> &MemSizes() const; 75 const std::deque<int64_t> &AllMemSizes() const; 76 const std::deque<uint64_t> &CurrentSizeDurs() const; Clear()77 void Clear() override 78 { 79 NativeHookSampleBase::Clear(); 80 eventTypes_.clear(); 81 subTypes_.clear(); 82 endTimeStamps_.clear(); 83 durations_.clear(); 84 addrs_.clear(); 85 memSizes_.clear(); 86 allMemSizes_.clear(); 87 currentSizeDurs_.clear(); 88 } GetAddrToAllocEventRow()89 std::unordered_map<uint64_t, uint64_t> *GetAddrToAllocEventRow() 90 { 91 return &addrToAllocEventRow_; 92 } GetAddrToMmapEventRow()93 std::unordered_map<uint64_t, uint64_t> *GetAddrToMmapEventRow() 94 { 95 return &addrToMmapEventRow_; 96 } GetLastMallocEventRaw()97 uint64_t &GetLastMallocEventRaw() 98 { 99 return lastMallocEventRaw_; 100 } GetLastMmapEventRaw()101 uint64_t &GetLastMmapEventRaw() 102 { 103 return lastMmapEventRaw_; 104 } 105 106 private: 107 std::deque<std::string> eventTypes_ = {}; 108 std::deque<DataIndex> subTypes_ = {}; 109 std::deque<uint64_t> endTimeStamps_ = {}; 110 std::deque<uint64_t> durations_ = {}; 111 std::deque<uint64_t> addrs_ = {}; 112 std::deque<int64_t> memSizes_ = {}; 113 std::deque<int64_t> allMemSizes_ = {}; 114 std::deque<uint64_t> currentSizeDurs_ = {}; 115 int64_t countHeapSizes_ = 0; 116 int64_t countMmapSizes_ = 0; 117 const std::string ALLOC_EVET = "AllocEvent"; 118 const std::string FREE_EVENT = "FreeEvent"; 119 const std::string MMAP_EVENT = "MmapEvent"; 120 const std::string MUNMAP_EVENT = "MunmapEvent"; 121 std::unordered_map<uint64_t, uint64_t> addrToAllocEventRow_ = {}; 122 std::unordered_map<uint64_t, uint64_t> addrToMmapEventRow_ = {}; 123 uint64_t lastMallocEventRaw_ = INVALID_UINT64; 124 uint64_t lastMmapEventRaw_ = INVALID_UINT64; 125 }; 126 127 struct NativeHookFrameRow { 128 /* data */ 129 uint32_t callChainId = INVALID_UINT32; 130 uint16_t depth = INVALID_UINT16; 131 uint64_t ip = INVALID_UINT64; 132 DataIndex symbolName = INVALID_DATAINDEX; 133 DataIndex filePath = INVALID_DATAINDEX; 134 uint64_t offset = INVALID_UINT64; 135 uint64_t symbolOffset = INVALID_UINT64; 136 }; 137 138 struct NativeHookFrameVaddrRow { 139 /* data */ 140 uint32_t callChainId = INVALID_UINT32; 141 uint16_t depth = INVALID_UINT16; 142 uint64_t ip = INVALID_UINT64; 143 DataIndex symbolName = INVALID_DATAINDEX; 144 DataIndex filePath = INVALID_DATAINDEX; 145 uint64_t offset = INVALID_UINT64; 146 uint64_t symbolOffset = INVALID_UINT64; 147 const std::string &vaddr; 148 }; 149 150 class NativeHookFrame { 151 public: 152 size_t AppendNewNativeHookFrame(const NativeHookFrameRow &context); 153 size_t AppendNewNativeHookFrame(const NativeHookFrameVaddrRow &context); 154 void UpdateFrameInfo(size_t row, 155 DataIndex symbolIndex, 156 DataIndex filePathIndex, 157 uint64_t offset, 158 uint64_t symbolOffset); 159 void UpdateSymbolIdToNameMap(uint64_t originSymbolId, uint64_t symbolId); 160 void UpdateSymbolId(); 161 void UpdateSymbolId(size_t index, DataIndex symbolId); 162 void UpdateFileId(std::map<uint32_t, uint64_t> &filePathIdToFilePathName); 163 void UpdateVaddrs(std::deque<std::string> &vaddrs); 164 void ClearUselessCallChainIds(const std::unordered_set<uint32_t> &callChainIdsSet); 165 const std::deque<uint32_t> &CallChainIds() const; 166 const std::deque<uint16_t> &Depths() const; 167 const std::deque<uint64_t> &Ips() const; 168 const std::deque<DataIndex> &SymbolNames() const; 169 const std::deque<DataIndex> &FilePaths() const; 170 const std::deque<uint64_t> &Offsets() const; 171 const std::deque<uint64_t> &SymbolOffsets() const; 172 const std::deque<std::string> &Vaddrs() const; 173 const std::deque<uint32_t> &realStack() const; Size()174 size_t Size() const 175 { 176 return callChainIds_.size(); 177 } Clear()178 void Clear() 179 { 180 callChainIds_.clear(); 181 depths_.clear(); 182 ips_.clear(); 183 symbolNames_.clear(); 184 filePaths_.clear(); 185 offsets_.clear(); 186 symbolOffsets_.clear(); 187 vaddrs_.clear(); 188 } 189 190 private: 191 std::deque<uint32_t> callChainIds_ = {}; 192 std::deque<uint16_t> depths_ = {}; 193 std::deque<uint64_t> ips_ = {}; 194 std::deque<DataIndex> symbolNames_ = {}; 195 std::deque<DataIndex> filePaths_ = {}; 196 std::deque<uint64_t> offsets_ = {}; 197 std::deque<uint64_t> symbolOffsets_ = {}; 198 std::deque<std::string> vaddrs_ = {}; 199 std::map<uint32_t, uint64_t> symbolIdToSymbolName_ = {}; 200 }; 201 struct NativeHookStatisticRow { 202 uint32_t ipid = INVALID_UINT32; 203 uint64_t timeStamp = INVALID_UINT64; 204 uint32_t callChainId = INVALID_UINT32; 205 uint32_t memoryType = INVALID_UINT32; 206 DataIndex subMemType = INVALID_UINT64; 207 uint64_t applyCount = INVALID_UINT64; 208 uint64_t releaseCount = INVALID_UINT64; 209 uint64_t applySize = INVALID_UINT64; 210 uint64_t releaseSize = INVALID_UINT64; 211 }; 212 class NativeHookStatistic : public NativeHookSampleBase { 213 public: 214 size_t AppendNewNativeHookStatistic(const NativeHookStatisticRow &nativeHookStatisticRow); 215 const std::deque<uint32_t> &MemoryTypes() const; 216 const std::deque<DataIndex> &MemorySubTypes() const; 217 const std::deque<uint64_t> &ApplyCounts() const; 218 const std::deque<uint64_t> &ReleaseCounts() const; 219 const std::deque<uint64_t> &ApplySizes() const; 220 const std::deque<uint64_t> &ReleaseSizes() const; Clear()221 void Clear() override 222 { 223 NativeHookSampleBase::Clear(); 224 memoryTypes_.clear(); 225 applyCounts_.clear(); 226 releaseCounts_.clear(); 227 applySizes_.clear(); 228 releaseSizes_.clear(); 229 } 230 231 private: 232 std::deque<uint32_t> memoryTypes_ = {}; 233 std::deque<DataIndex> memSubTypes_ = {}; 234 std::deque<uint64_t> applyCounts_ = {}; 235 std::deque<uint64_t> releaseCounts_ = {}; 236 std::deque<uint64_t> applySizes_ = {}; 237 std::deque<uint64_t> releaseSizes_ = {}; 238 }; 239 } // namespace TraceStdtype 240 } // namespace SysTuning 241 242 #endif // NATIVE_MEMORY_STDTYPE_H 243