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 UniqueStackTable()82 UniqueStackTable() 83 { 84 } 85 UniqueStackTable(pid_t pid)86 explicit UniqueStackTable(pid_t pid) : pid_(pid) 87 { 88 } 89 UniqueStackTable(pid_t pid,uint32_t size)90 UniqueStackTable(pid_t pid, uint32_t size) : pid_(pid), tableSize_(size) 91 { 92 } 93 94 UniqueStackTable(void* buf, uint32_t size, bool releaseBuffer = true) 95 :tableBufMMap_(buf), tableSize_(size), releaseBuffer_(releaseBuffer) 96 { 97 totalNodes_ = ((tableSize_ / sizeof(Node)) >> 1) << 1; 98 } 99 ~UniqueStackTable()100 ~UniqueStackTable() 101 { 102 if (tableBufMMap_ != nullptr && releaseBuffer_) { 103 munmap(tableBufMMap_, tableSize_); 104 tableBufMMap_ = nullptr; 105 } 106 } 107 108 uint64_t PutPcsInTable(StackId *stackId, uintptr_t *pcs, size_t nr); 109 bool GetPcsByStackId(const StackId stackId, std::vector<uintptr_t>& pcs); 110 bool ImportNode(uint32_t index, const Node& node); 111 size_t GetWriteSize(); 112 113 bool Resize(); 114 GetPid()115 uint32_t GetPid() 116 { 117 return pid_; 118 } 119 GetTabelSize()120 uint32_t GetTabelSize() 121 { 122 return tableSize_; 123 } 124 GetUsedIndexes()125 std::vector<uint32_t>& GetUsedIndexes() 126 { 127 return usedSlots_; 128 } 129 GetHeadNode()130 Node* GetHeadNode() 131 { 132 return reinterpret_cast<Node *>(tableBufMMap_); 133 } 134 135 private: 136 Node* GetFrame(uint64_t stackId); 137 uint64_t PutPcInSlot(uint64_t thisPc, uint64_t prevIdx); 138 int32_t pid_ = 0; 139 void* tableBufMMap_ = nullptr; 140 uint32_t tableSize_ = INITIAL_TABLE_SIZE; 141 std::vector<uint32_t> usedSlots_; 142 uint32_t totalNodes_ = 0; 143 // current available node count, include index 0 144 uint32_t availableNodes_ = 0; 145 uint32_t hashModulus_ = 0; 146 // 0 for reserved, start from 1 147 uint32_t availableIndex_ = 1; 148 // for de-conflict 149 uint64_t hashStep_ = 0; 150 uint8_t deconflictTimes_ = INIT_DECONFLICT_ALLOWED; 151 std::mutex stackTableMutex_; 152 bool releaseBuffer_ = true; 153 }; 154 } 155 } 156 #endif // HIEPRF_UNIQUE_STACK_TABLE_H