1 /* 2 * Copyright (C) 2021-2022 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 MEMORY_INFO_H 16 #define MEMORY_INFO_H 17 #include <future> 18 #include <map> 19 #include <memory> 20 #include <mutex> 21 #include <string> 22 #include <vector> 23 #include "executor/memory/get_heap_info.h" 24 #include "executor/memory/parse/meminfo_data.h" 25 #include "common.h" 26 #include "time.h" 27 #include "graphic_memory_collector.h" 28 #include "memory_collector.h" 29 namespace OHOS { 30 namespace HiviewDFX { 31 namespace { 32 static const std::string MEMINFO_PSS = "Pss"; 33 static const std::string MEMINFO_SHARED_CLEAN = "Shared_Clean"; 34 static const std::string MEMINFO_SHARED_DIRTY = "Shared_Dirty"; 35 static const std::string MEMINFO_PRIVATE_CLEAN = "Private_Clean"; 36 static const std::string MEMINFO_PRIVATE_DIRTY = "Private_Dirty"; 37 static const std::string MEMINFO_SWAP = "Swap"; 38 static const std::string MEMINFO_SWAP_PSS = "SwapPss"; 39 static const std::string MEMINFO_HEAP_SIZE = "Heap_Size"; 40 static const std::string MEMINFO_HEAP_ALLOC = "Heap_Alloc"; 41 static const std::string MEMINFO_HEAP_FREE = "Heap_Free"; 42 static const std::string MEMINFO_DMA = "Dma"; 43 static const std::string MEMINFO_OTHER = "other"; 44 static const std::string MEMINFO_ANONPAGE_OTHER = "AnonPage other"; 45 static const std::string MEMINFO_FILEPAGE_OTHER = "FilePage other"; 46 static const std::string MEMINFO_ASHMEM = "Ashmem"; 47 static const std::string MEMINFO_TOTAL = "Total"; 48 } 49 class MemoryInfo { 50 public: 51 MemoryInfo(); 52 ~MemoryInfo(); 53 54 using StringMatrix = std::shared_ptr<std::vector<std::vector<std::string>>>; 55 using ValueMap = std::map<std::string, uint64_t>; 56 using GroupMap = std::map<std::string, ValueMap>; 57 using MemFun = std::function<void(MemInfoData::MemInfo&, uint64_t)>; 58 using MemoryItemMap = std::map<std::string, MemoryItem>; 59 using PageType = std::vector<std::pair<std::string, uint64_t>>; 60 using PageTypePair = std::pair<PageType, PageType>; 61 62 bool GetMemoryInfoByPid(const int32_t &pid, StringMatrix result, bool showAshmem, bool showDmaBuf); 63 void GetMemoryInfoByTimeInterval(int fd, const int32_t &pid, const int32_t &timeInterval); 64 void SetReceivedSigInt(bool isReceivedSigInt); 65 DumpStatus GetMemoryInfoNoPid(int fd, StringMatrix result); 66 DumpStatus GetMemoryInfoPrune(int fd, StringMatrix result); 67 DumpStatus DealResult(StringMatrix result); 68 69 private: 70 enum Status { 71 SUCCESS_MORE_DATA = 1, 72 FAIL_MORE_DATA = 2, 73 SUCCESS_NO_MORE_DATA = 3, 74 FAIL_NO_MORE_DATA = 4, 75 }; 76 int rawParamFd_ = 0; 77 const int LINE_WIDTH_ = 14; 78 const int RAM_WIDTH_ = 16; 79 const size_t TYPE_SIZE = 2; 80 const char SEPARATOR_ = '-'; 81 const char BLANK_ = ' '; 82 const static int NAME_SIZE_ = 2; 83 const int PID_WIDTH_ = 5; 84 const int NAME_WIDTH_ = 20; 85 const int PSS_WIDTH_ = 30; 86 const int KB_WIDTH_ = 12; 87 const int NAME_AND_PID_WIDTH = 30; 88 const int MALLOC_HEAP_TYPES = 3; 89 const static int VSS_BIT = 4; 90 const static int BYTE_PER_KB = 1024; 91 const std::string ZERO = "0"; 92 const std::vector<std::string> MEMORY_CLASS_VEC = { 93 "graph", "ark ts heap", ".db", "dev", "dmabuf", "guard", ".hap", 94 "native heap", ".so", "stack", ".ttf", "other" 95 }; 96 const std::vector<std::string> MEMORY_PRINT_ORDER_VEC = { 97 "ark ts heap", "guard", "native heap", ".hap", "AnonPage other", 98 "stack", ".db", ".so", "dev", "dmabuf", ".ttf" 99 }; 100 const std::vector<std::string> NATIVE_HEAP_TAG = {"heap", "jemalloc meta", "jemalloc heap", 101 "brk heap", "musl heap", "mmap heap"}; 102 const std::map<MemoryItemType, std::string> NATIVE_HEAP_MAP = { 103 {MemoryItemType::MEMORY_ITEM_TYPE_HEAP, "heap"}, 104 {MemoryItemType::MEMORY_ITEM_TYPE_ANON_NATIVE_HEAP_JEMALLOC_META, "jemalloc meta"}, 105 {MemoryItemType::MEMORY_ITEM_TYPE_ANON_NATIVE_HEAP_JEMALLOC, "jemalloc heap"}, 106 {MemoryItemType::MEMORY_ITEM_TYPE_ANON_NATIVE_HEAP_BRK, "brk heap"}, 107 {MemoryItemType::MEMORY_ITEM_TYPE_ANON_NATIVE_HEAP_META, "musl heap"}, 108 {MemoryItemType::MEMORY_ITEM_TYPE_ANON_NATIVE_HEAP_MMAP, "mmap heap"}, 109 }; 110 const std::map<MemoryItemType, std::string> GRAPHIC_MAP = { 111 {MemoryItemType::MEMORY_ITEM_TYPE_GRAPH_GL, "GL"}, 112 {MemoryItemType::MEMORY_ITEM_TYPE_GRAPH_GRAPHICS, "Graph"}, 113 }; 114 115 bool isReady_ = false; 116 bool dumpPrune_ = false; 117 bool dumpSmapsOnStart_ = false; 118 uint64_t totalGL_ = 0; 119 uint64_t totalGraph_ = 0; 120 uint64_t totalDma_ = 0; 121 uint64_t currentPss_ = 0; 122 std::string startTime_; 123 std::mutex mutex_; 124 std::mutex timeIntervalMutex_; 125 std::future<GroupMap> fut_; 126 std::vector<int32_t> pids_; 127 std::vector<MemInfoData::MemUsage> memUsages_; 128 std::vector<std::pair<std::string, MemFun>> methodVec_; 129 std::map<std::string, std::vector<MemInfoData::MemUsage>> adjMemResult_ = { 130 {"System", {}}, {"Foreground", {}}, {"Suspend-delay", {}}, 131 {"Perceived", {}}, {"Background", {}}, {"Undefined", {}}, 132 }; 133 MemoryItemMap memoryItemMap_; 134 MemInfoData::GraphicsMemory graphicsMemory_ = {0}; 135 136 void InsertMemoryTitle(StringMatrix result); 137 void GetResult(const int32_t& pid, StringMatrix result); 138 std::string AddKbUnit(const uint64_t &value) const; 139 bool GetMemByProcessPid(const int32_t &pid, MemInfoData::MemUsage &usage); 140 static bool GetSmapsInfoNoPid(const int32_t &pid, GroupMap &result); 141 bool GetMeminfo(ValueMap &result); 142 bool GetHardWareUsage(StringMatrix result); 143 bool GetCMAUsage(StringMatrix result); 144 bool GetKernelUsage(const ValueMap &infos, StringMatrix result); 145 void GetProcesses(const GroupMap &infos, StringMatrix result); 146 bool GetPids(); 147 void GetPssTotal(const GroupMap &infos, StringMatrix result); 148 void GetRamUsage(const GroupMap &smapsinfos, const ValueMap &meminfo, StringMatrix result); 149 void GetPurgTotal(const ValueMap &meminfo, StringMatrix result); 150 void GetPurgByPid(const int32_t &pid, StringMatrix result); 151 void GetDma(const uint64_t& dma, StringMatrix result); 152 void GetHiaiServerIon(const int32_t &pid, StringMatrix result); 153 void GetNativeHeap(const std::unique_ptr<MemoryDetail>& detail, StringMatrix result); 154 void GetNativeValue(const std::string& tag, const GroupMap& nativeGroupMap, StringMatrix result); 155 void GetRamCategory(const GroupMap &smapsinfos, const ValueMap &meminfos, StringMatrix result); 156 void UpdateGraphicsMemoryRet(const std::string& title, const uint64_t& value, StringMatrix result); 157 void AddBlankLine(StringMatrix result); 158 void MemUsageToMatrix(const MemInfoData::MemUsage &memUsage, StringMatrix result); 159 void PairToStringMatrix(const std::string &titleStr, std::vector<std::pair<std::string, uint64_t>> &vec, 160 StringMatrix result); 161 void AddMemByProcessTitle(StringMatrix result, std::string sortType); 162 bool GetMemoryInfoInit(StringMatrix result); 163 void GetMemoryUsageInfo(StringMatrix result); 164 165 static uint64_t GetVss(const int32_t &pid); 166 static std::string GetProcName(const int32_t &pid); 167 static int32_t GetProcUid(const int32_t &pid); 168 static uint64_t GetProcValue(const int32_t &pid, const std::string& key); 169 #ifdef HIDUMPER_MEMMGR_ENABLE 170 static std::string GetProcessAdjLabel(const int32_t pid); 171 #endif 172 static int GetScoreAdj(const int32_t pid); 173 static void InitMemInfo(MemInfoData::MemInfo &memInfo); 174 static void InitMemUsage(MemInfoData::MemUsage &usage); 175 void GetSortedMemoryInfoNoPid(StringMatrix result); 176 bool GetGraphicsMemory(int32_t pid, MemInfoData::GraphicsMemory &graphicsMemory, GraphicType graphicType); 177 #ifdef HIDUMPER_MEMMGR_ENABLE 178 void GetMemoryByAdj(StringMatrix result); 179 #endif 180 void SetPss(MemInfoData::MemInfo &meminfo, uint64_t value); 181 void SetSharedClean(MemInfoData::MemInfo &meminfo, uint64_t value); 182 void SetSharedDirty(MemInfoData::MemInfo &meminfo, uint64_t value); 183 void SetPrivateClean(MemInfoData::MemInfo &meminfo, uint64_t value); 184 void SetPrivateDirty(MemInfoData::MemInfo &meminfo, uint64_t value); 185 void SetSwap(MemInfoData::MemInfo &meminfo, uint64_t value); 186 void SetSwapPss(MemInfoData::MemInfo &meminfo, uint64_t value); 187 void SetHeapSize(MemInfoData::MemInfo &meminfo, uint64_t value); 188 void SetHeapAlloc(MemInfoData::MemInfo &meminfo, uint64_t value); 189 void SetHeapFree(MemInfoData::MemInfo &meminfo, uint64_t value); 190 191 // hidumper --mem pid 192 void UpdateResult(const int32_t& pid, const std::unique_ptr<ProcessMemoryDetail>& processMemoryDetail, 193 std::unique_ptr<MemoryDetail>& nativeHeapDetail, StringMatrix result); 194 void GetGraphicsMemoryByDetail(const std::unique_ptr<MemoryDetail>& detail, StringMatrix result); 195 void UpdatePageDetail(std::unique_ptr<MemoryDetail>& anonPageDetail, 196 std::unique_ptr<MemoryDetail>& filePageDetail, const std::unique_ptr<MemoryDetail>& tempDetail); 197 void UpdateTotalDetail(const std::unique_ptr<ProcessMemoryDetail>& detail, 198 const std::unique_ptr<MallHeapInfo>& heapInfo, StringMatrix result); 199 void SetDetailRet(const std::string& memoryClassStr, const std::unique_ptr<MemoryDetail>& detail, 200 const std::unique_ptr<MallHeapInfo>& heapInfo, StringMatrix result); 201 void SetValueForRet(const std::string& value, std::vector<std::string>& tempResult); 202 void SetNativeDetailRet(const std::string& nativeClassStr, const std::unique_ptr<MemoryItem>& item, 203 StringMatrix result); 204 void GetAshmem(const int32_t &pid, StringMatrix result, bool showAshmem); 205 void GetDmaBuf(const int32_t &pid, StringMatrix result, bool showDmaBuf); 206 int CalculateStars(const std::vector<int>& pssValues, int currentPSS); 207 void WriteStdout(const std::string& s); 208 void ClearPreviousLines(int lineCount); 209 std::string GenerateTimestamps(const std::vector<int>& pssValues); 210 std::string GenerateLine(const std::vector<int>& pssValues, int index); 211 void CalculateMaxIdex(const std::vector<int>& pssValues, int *maxIndex); 212 void PrintMemoryInfo(const std::vector<int>& pssValues, int* prevLineCount); 213 void RedirectMemoryInfo(int timeIndex, StringMatrix result); 214 }; 215 } // namespace HiviewDFX 216 } // namespace OHOS 217 #endif 218