1 /* 2 * Copyright (c) 2024 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 #ifndef UNIQUE_STACK_TABLE_H 16 #define UNIQUE_STACK_TABLE_H 17 18 #include <cinttypes> 19 #include <mutex> 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 #include <sys/mman.h> 25 26 namespace OHOS { 27 namespace HiviewDFX { 28 #define ADDR_BIT_LENGTH 40 29 #define IDX_BIT_LENGTH 23 30 #define KERNEL_FLAG_BIT_LENGTH 1 31 #define DECONFLICT_INCREASE_STEP 3 32 #define RESIZE_MULTIPLE 2 33 #define NR_BIT_LENGTH 41 34 constexpr uint32_t INITIAL_TABLE_SIZE = 1 * 1024 * 1024; 35 constexpr uint32_t MAX_NODES_CNT = 1 << IDX_BIT_LENGTH ; 36 constexpr uint64_t PC_IN_KERNEL = 1ull << 63; 37 constexpr uint64_t HEAD_NODE_INDEX = 0; 38 // FFFFFF0000000000 39 constexpr uint64_t KERNEL_PREFIX = 0xFFFFFFull << 40; 40 constexpr uint8_t INIT_DECONFLICT_ALLOWED = 22; 41 42 // align 43 #pragma pack(push, 4) 44 45 union Node { 46 uint64_t value; 47 struct { 48 uint64_t pc : ADDR_BIT_LENGTH; 49 uint64_t prevIdx : IDX_BIT_LENGTH; 50 uint64_t inKernel : KERNEL_FLAG_BIT_LENGTH; 51 } section; 52 }; 53 54 struct UniStackNode { 55 uint32_t index; 56 Node node; 57 }; 58 59 struct UniStackTableInfo { 60 uint32_t pid; 61 uint32_t tableSize; 62 uint32_t numNodes; 63 std::vector<UniStackNode> nodes; 64 }; 65 66 union StackId { 67 uint64_t value; 68 struct { 69 uint64_t id : IDX_BIT_LENGTH; 70 uint64_t nr : NR_BIT_LENGTH; 71 } section; 72 }; 73 74 #pragma pack(pop) 75 static_assert(sizeof(Node) == 8, "Node size must be 8 byte"); 76 77 class UniqueStackTable { 78 public: 79 bool Init(); 80 static UniqueStackTable* Instance(); 81 82 UniqueStackTable() = default; 83 UniqueStackTable(pid_t pid)84 explicit UniqueStackTable(pid_t pid) : pid_(pid) 85 { 86 } 87 UniqueStackTable(pid_t pid,uint32_t size)88 UniqueStackTable(pid_t pid, uint32_t size) : pid_(pid), tableSize_(size) 89 { 90 } 91 92 UniqueStackTable(void* buf, uint32_t size, bool releaseBuffer = true) tableBufMMap_(buf)93 : tableBufMMap_(buf), tableSize_(size), releaseBuffer_(releaseBuffer) 94 { 95 totalNodes_ = ((tableSize_ / sizeof(Node)) >> 1) << 1; 96 } 97 ~UniqueStackTable()98 ~UniqueStackTable() 99 { 100 if (tableBufMMap_ != nullptr && releaseBuffer_) { 101 munmap(tableBufMMap_, tableSize_); 102 tableBufMMap_ = nullptr; 103 } 104 } 105 106 uint64_t PutPcsInTable(StackId *stackId, const uintptr_t *pcs, size_t nr); 107 bool GetPcsByStackId(const StackId stackId, std::vector<uintptr_t>& pcs); 108 bool ImportNode(uint32_t index, const Node& node); 109 size_t GetWriteSize(); 110 111 bool Resize(); 112 GetPid()113 uint32_t GetPid() 114 { 115 return pid_; 116 } 117 GetTabelSize()118 uint32_t GetTabelSize() 119 { 120 return tableSize_; 121 } 122 GetUsedIndexes()123 std::vector<uint32_t>& GetUsedIndexes() 124 { 125 return usedSlots_; 126 } 127 GetHeadNode()128 Node* GetHeadNode() 129 { 130 return reinterpret_cast<Node *>(tableBufMMap_); 131 } 132 133 private: 134 Node* GetFrame(uint64_t stackId); 135 uint64_t PutPcInSlot(uint64_t thisPc, uint64_t prevIdx); 136 int32_t pid_ = 0; 137 void* tableBufMMap_ = nullptr; 138 uint32_t tableSize_ = INITIAL_TABLE_SIZE; 139 std::vector<uint32_t> usedSlots_; 140 uint32_t totalNodes_ = 0; 141 // current available node count, include index 0 142 uint32_t availableNodes_ = 0; 143 uint32_t hashModulus_ = 0; 144 // 0 for reserved, start from 1 145 uint32_t availableIndex_ = 1; 146 // for de-conflict 147 uint64_t hashStep_ = 0; 148 uint8_t deconflictTimes_ = INIT_DECONFLICT_ALLOWED; 149 std::mutex stackTableMutex_; 150 bool releaseBuffer_ = true; 151 }; 152 } 153 } 154 #endif // HIEPRF_UNIQUE_STACK_TABLE_H