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 <map> 18 #include <memory> 19 #include <future> 20 #include <string> 21 #include <vector> 22 #include "executor/memory/parse/meminfo_data.h" 23 #include "common.h" 24 #include "time.h" 25 namespace OHOS { 26 namespace HiviewDFX { 27 namespace { 28 static const std::string MEMINFO_PSS = "Pss"; 29 static const std::string MEMINFO_SHARED_CLEAN = "Shared_Clean"; 30 static const std::string MEMINFO_SHARED_DIRTY = "Shared_Dirty"; 31 static const std::string MEMINFO_PRIVATE_CLEAN = "Private_Clean"; 32 static const std::string MEMINFO_PRIVATE_DIRTY = "Private_Dirty"; 33 static const std::string MEMINFO_SWAP = "Swap"; 34 static const std::string MEMINFO_SWAP_PSS = "SwapPss"; 35 } 36 class MemoryInfo { 37 public: 38 MemoryInfo(); 39 ~MemoryInfo(); 40 41 using StringMatrix = std::shared_ptr<std::vector<std::vector<std::string>>>; 42 using ValueMap = std::map<std::string, uint64_t>; 43 using GroupMap = std::map<std::string, ValueMap>; 44 45 using MemFun = std::function<void(MemInfoData::MemInfo&, uint64_t)>; 46 47 bool GetMemoryInfoByPid(const int &pid, StringMatrix result); 48 DumpStatus GetMemoryInfoNoPid(StringMatrix result); 49 50 private: 51 enum Status { 52 SUCCESS_MORE_DATA = 1, 53 FAIL_MORE_DATA = 2, 54 SUCCESS_NO_MORE_DATA = 3, 55 FAIL_NO_MORE_DATA = 4, 56 }; 57 58 const int LINE_WIDTH_ = 14; 59 const int RAM_WIDTH_ = 16; 60 const size_t TYPE_SIZE = 2; 61 const char SEPARATOR_ = '-'; 62 const char BLANK_ = ' '; 63 const static int NAME_SIZE_ = 2; 64 const int PID_WIDTH_ = 5; 65 const int NAME_WIDTH_ = 20; 66 const int KB_WIDTH_ = 12; 67 const int NAME_AND_PID_WIDTH = 30; 68 const static int VSS_BIT = 4; 69 const static int BYTE_PER_KB = 1024; 70 bool isReady_ = false; 71 bool dumpSmapsOnStart_ = false; 72 uint64_t totalGL_ = 0; 73 uint64_t totalGraph_ = 0; 74 std::future<GroupMap> fut_; 75 std::vector<int> pids_; 76 std::vector<MemInfoData::MemUsage> memUsages_; 77 std::vector<std::pair<std::string, MemFun>> methodVec_; 78 std::map<std::string, std::vector<MemInfoData::MemUsage>> adjMemResult_ = { 79 {"System", {}}, {"Foreground", {}}, {"Suspend-delay", {}}, 80 {"Perceived", {}}, {"Background", {}}, {"Undefined", {}}, 81 }; 82 void insertMemoryTitle(StringMatrix result); 83 void BuildResult(const GroupMap &infos, StringMatrix result); 84 85 std::string AddKbUnit(const uint64_t &value) const; 86 static bool GetMemByProcessPid(const int &pid, MemInfoData::MemUsage &usage); 87 static bool GetSmapsInfoNoPid(const int &pid, GroupMap &result); 88 bool GetMeminfo(ValueMap &result); 89 static bool GetGraphicsMemory(int32_t pid, MemInfoData::GraphicsMemory &graphicsMemory); 90 bool GetHardWareUsage(StringMatrix result); 91 bool GetCMAUsage(StringMatrix result); 92 bool GetKernelUsage(const ValueMap &infos, StringMatrix result); 93 void GetProcesses(const GroupMap &infos, StringMatrix result); 94 bool GetPids(); 95 void GetPssTotal(const GroupMap &infos, StringMatrix result); 96 void GetRamUsage(const GroupMap &smapsinfos, const ValueMap &meminfo, StringMatrix result); 97 void GetRamCategory(const GroupMap &smapsinfos, const ValueMap &meminfos, StringMatrix result); 98 void AddBlankLine(StringMatrix result); 99 void MemUsageToMatrix(const MemInfoData::MemUsage &memUsage, StringMatrix result); 100 void PairToStringMatrix(const std::string &titleStr, std::vector<std::pair<std::string, uint64_t>> &vec, 101 StringMatrix result); 102 void AddMemByProcessTitle(StringMatrix result, std::string sortType); 103 static uint64_t GetVss(const int &pid); 104 static std::string GetProcName(const int &pid); 105 static std::string GetProcessAdjLabel(const int pid); 106 static void InitMemInfo(MemInfoData::MemInfo &memInfo); 107 static void InitMemUsage(MemInfoData::MemUsage &usage); 108 void CalcGroup(const GroupMap &infos, StringMatrix result); 109 void SetValue(const std::string &value, std::vector<std::string> &lines, std::vector<std::string> &values); 110 void GetSortedMemoryInfoNoPid(StringMatrix result); 111 void GetMemoryByAdj(StringMatrix result); 112 void SetPss(MemInfoData::MemInfo &meminfo, uint64_t value); 113 void SetSharedClean(MemInfoData::MemInfo &meminfo, uint64_t value); 114 void SetSharedDirty(MemInfoData::MemInfo &meminfo, uint64_t value); 115 void SetPrivateClean(MemInfoData::MemInfo &meminfo, uint64_t value); 116 void SetPrivateDirty(MemInfoData::MemInfo &meminfo, uint64_t value); 117 void SetSwap(MemInfoData::MemInfo &meminfo, uint64_t value); 118 void SetSwapPss(MemInfoData::MemInfo &meminfo, uint64_t value); 119 }; 120 } // namespace HiviewDFX 121 } // namespace OHOS 122 #endif 123