• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_TOTAL = "Total";
47 }
48 class MemoryInfo {
49 public:
50     MemoryInfo();
51     ~MemoryInfo();
52 
53     using StringMatrix = std::shared_ptr<std::vector<std::vector<std::string>>>;
54     using ValueMap = std::map<std::string, uint64_t>;
55     using GroupMap = std::map<std::string, ValueMap>;
56     using MemFun = std::function<void(MemInfoData::MemInfo&, uint64_t)>;
57     using MemoryItemMap = std::map<std::string, MemoryItem>;
58     using PageType = std::vector<std::pair<std::string, uint64_t>>;
59     using PageTypePair = std::pair<PageType, PageType>;
60 
61     bool GetMemoryInfoByPid(const int32_t &pid, StringMatrix result);
62     DumpStatus GetMemoryInfoNoPid(int fd, StringMatrix result);
63     DumpStatus DealResult(StringMatrix result);
64 
65 private:
66     enum Status {
67         SUCCESS_MORE_DATA = 1,
68         FAIL_MORE_DATA = 2,
69         SUCCESS_NO_MORE_DATA = 3,
70         FAIL_NO_MORE_DATA = 4,
71     };
72     int rawParamFd_ = 0;
73     const int LINE_WIDTH_ = 14;
74     const int RAM_WIDTH_ = 16;
75     const size_t TYPE_SIZE = 2;
76     const char SEPARATOR_ = '-';
77     const char BLANK_ = ' ';
78     const static int NAME_SIZE_ = 2;
79     const int PID_WIDTH_ = 5;
80     const int NAME_WIDTH_ = 20;
81     const int PSS_WIDTH_ = 30;
82     const int KB_WIDTH_ = 12;
83     const int NAME_AND_PID_WIDTH = 30;
84     const int MALLOC_HEAP_TYPES = 3;
85     const static int VSS_BIT = 4;
86     const static int BYTE_PER_KB = 1024;
87     const std::string ZERO = "0";
88     const std::vector<std::string> MEMORY_CLASS_VEC = {
89         "graph", "ark ts heap", ".db", "dev", "dmabuf", "guard", ".hap",
90         "native heap", ".so", "stack", ".ttf", "other"
91     };
92     const std::vector<std::string> MEMORY_PRINT_ORDER_VEC = {
93         "ark ts heap", "guard", "native heap", ".hap", "AnonPage other",
94         "stack", ".db", ".so", "dev", "dmabuf", ".ttf"
95     };
96     const std::vector<std::string> NATIVE_HEAP_TAG = {"heap", "jemalloc meta", "jemalloc heap",
97         "brk heap", "musl heap", "mmap heap"};
98     const std::map<MemoryItemType, std::string> NATIVE_HEAP_MAP = {
99         {MemoryItemType::MEMORY_ITEM_TYPE_HEAP, "heap"},
100         {MemoryItemType::MEMORY_ITEM_TYPE_ANON_NATIVE_HEAP_JEMALLOC_META, "jemalloc meta"},
101         {MemoryItemType::MEMORY_ITEM_TYPE_ANON_NATIVE_HEAP_JEMALLOC, "jemalloc heap"},
102         {MemoryItemType::MEMORY_ITEM_TYPE_ANON_NATIVE_HEAP_BRK, "brk heap"},
103         {MemoryItemType::MEMORY_ITEM_TYPE_ANON_NATIVE_HEAP_META, "musl heap"},
104         {MemoryItemType::MEMORY_ITEM_TYPE_ANON_NATIVE_HEAP_MMAP, "mmap heap"},
105     };
106     const std::map<MemoryItemType, std::string> GRAPHIC_MAP = {
107         {MemoryItemType::MEMORY_ITEM_TYPE_GRAPH_GL, "GL"},
108         {MemoryItemType::MEMORY_ITEM_TYPE_GRAPH_GRAPHICS, "Graph"},
109     };
110 
111     bool isReady_ = false;
112     bool dumpSmapsOnStart_ = false;
113     uint64_t totalGL_ = 0;
114     uint64_t totalGraph_ = 0;
115     uint64_t totalDma_ = 0;
116     std::mutex mutex_;
117     std::future<GroupMap> fut_;
118     std::vector<int32_t> pids_;
119     std::vector<MemInfoData::MemUsage> memUsages_;
120     std::vector<std::pair<std::string, MemFun>> methodVec_;
121     std::map<std::string, std::vector<MemInfoData::MemUsage>> adjMemResult_ = {
122         {"System", {}}, {"Foreground", {}}, {"Suspend-delay", {}},
123         {"Perceived", {}}, {"Background", {}}, {"Undefined", {}},
124     };
125     MemoryItemMap memoryItemMap_;
126     MemInfoData::GraphicsMemory graphicsMemory_ = {0};
127 
128     void InsertMemoryTitle(StringMatrix result);
129     void GetResult(const int32_t& pid, StringMatrix result);
130     std::string AddKbUnit(const uint64_t &value) const;
131     bool GetMemByProcessPid(const int32_t &pid, MemInfoData::MemUsage &usage);
132     static bool GetSmapsInfoNoPid(const int32_t &pid, GroupMap &result);
133     bool GetMeminfo(ValueMap &result);
134     bool GetHardWareUsage(StringMatrix result);
135     bool GetCMAUsage(StringMatrix result);
136     bool GetKernelUsage(const ValueMap &infos, StringMatrix result);
137     void GetProcesses(const GroupMap &infos, StringMatrix result);
138     bool GetPids();
139     void GetPssTotal(const GroupMap &infos, StringMatrix result);
140     void GetRamUsage(const GroupMap &smapsinfos, const ValueMap &meminfo, StringMatrix result);
141     void GetPurgTotal(const ValueMap &meminfo, StringMatrix result);
142     void GetPurgByPid(const int32_t &pid, StringMatrix result);
143     void GetDma(const uint64_t& dma, StringMatrix result);
144     void GetHiaiServerIon(const int32_t &pid, StringMatrix result);
145     void GetNativeHeap(const std::unique_ptr<MemoryDetail>& detail, StringMatrix result);
146     void GetNativeValue(const std::string& tag, const GroupMap& nativeGroupMap, StringMatrix result);
147     void GetRamCategory(const GroupMap &smapsinfos, const ValueMap &meminfos, StringMatrix result);
148     void UpdateGraphicsMemoryRet(const std::string& title, const uint64_t& value, StringMatrix result);
149     void AddBlankLine(StringMatrix result);
150     void MemUsageToMatrix(const MemInfoData::MemUsage &memUsage, StringMatrix result);
151     void PairToStringMatrix(const std::string &titleStr, std::vector<std::pair<std::string, uint64_t>> &vec,
152                             StringMatrix result);
153     void AddMemByProcessTitle(StringMatrix result, std::string sortType);
154 
155     static uint64_t GetVss(const int32_t &pid);
156     static std::string GetProcName(const int32_t &pid);
157     static uint64_t GetProcValue(const int32_t &pid, const std::string& key);
158 #ifdef HIDUMPER_MEMMGR_ENABLE
159     static std::string GetProcessAdjLabel(const int32_t pid);
160 #endif
161     static void InitMemInfo(MemInfoData::MemInfo &memInfo);
162     static void InitMemUsage(MemInfoData::MemUsage &usage);
163     void GetSortedMemoryInfoNoPid(StringMatrix result);
164     bool GetGraphicsMemory(int32_t pid, MemInfoData::GraphicsMemory &graphicsMemory, GraphicType graphicType);
165 #ifdef HIDUMPER_MEMMGR_ENABLE
166     void GetMemoryByAdj(StringMatrix result);
167 #endif
168     void SetPss(MemInfoData::MemInfo &meminfo, uint64_t value);
169     void SetSharedClean(MemInfoData::MemInfo &meminfo, uint64_t value);
170     void SetSharedDirty(MemInfoData::MemInfo &meminfo, uint64_t value);
171     void SetPrivateClean(MemInfoData::MemInfo &meminfo, uint64_t value);
172     void SetPrivateDirty(MemInfoData::MemInfo &meminfo, uint64_t value);
173     void SetSwap(MemInfoData::MemInfo &meminfo, uint64_t value);
174     void SetSwapPss(MemInfoData::MemInfo &meminfo, uint64_t value);
175     void SetHeapSize(MemInfoData::MemInfo &meminfo, uint64_t value);
176     void SetHeapAlloc(MemInfoData::MemInfo &meminfo, uint64_t value);
177     void SetHeapFree(MemInfoData::MemInfo &meminfo, uint64_t value);
178 
179     // hidumper --mem pid
180     void UpdateResult(const int32_t& pid, const std::unique_ptr<ProcessMemoryDetail>& processMemoryDetail,
181         std::unique_ptr<MemoryDetail>& nativeHeapDetail, StringMatrix result);
182     void GetGraphicsMemoryByDetail(const std::unique_ptr<MemoryDetail>& detail, StringMatrix result);
183     void UpdatePageDetail(std::unique_ptr<MemoryDetail>& anonPageDetail,
184         std::unique_ptr<MemoryDetail>& filePageDetail, const std::unique_ptr<MemoryDetail>& tempDetail);
185     void UpdateTotalDetail(const std::unique_ptr<ProcessMemoryDetail>& detail,
186         const std::unique_ptr<MallHeapInfo>& heapInfo, StringMatrix result);
187     void SetDetailRet(const std::string& memoryClassStr, const std::unique_ptr<MemoryDetail>& detail,
188         const std::unique_ptr<MallHeapInfo>& heapInfo, StringMatrix result);
189     void SetValueForRet(const std::string& value, std::vector<std::string>& tempResult);
190     void SetNativeDetailRet(const std::string& nativeClassStr, const std::unique_ptr<MemoryItem>& item,
191         StringMatrix result);
192 };
193 } // namespace HiviewDFX
194 } // namespace OHOS
195 #endif
196