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