• 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/dma_info.h"
24 #include "executor/memory/parse/meminfo_data.h"
25 #include "common.h"
26 #include "time.h"
27 #ifdef HIDUMPER_GRAPHIC_ENABLE
28 #include "transaction/rs_interfaces.h"
29 #endif
30 namespace OHOS {
31 namespace HiviewDFX {
32 namespace {
33 static const std::string MEMINFO_PSS = "Pss";
34 static const std::string MEMINFO_SHARED_CLEAN = "Shared_Clean";
35 static const std::string MEMINFO_SHARED_DIRTY = "Shared_Dirty";
36 static const std::string MEMINFO_PRIVATE_CLEAN = "Private_Clean";
37 static const std::string MEMINFO_PRIVATE_DIRTY = "Private_Dirty";
38 static const std::string MEMINFO_SWAP = "Swap";
39 static const std::string MEMINFO_SWAP_PSS = "SwapPss";
40 static const std::string MEMINFO_HEAP_SIZE = "Heap_Size";
41 static const std::string MEMINFO_HEAP_ALLOC = "Heap_Alloc";
42 static const std::string MEMINFO_HEAP_FREE = "Heap_Free";
43 static const std::string MEMINFO_DMA = "Dma";
44 
45 // system app
46 constexpr int RECLAIM_PRIORITY_SYSTEM = -1000;
47 //ondemand system app
48 constexpr int RECLAIM_ONDEMAND_SYSTEM = -900;
49 // persist(killable) system app
50 constexpr int RECLAIM_PRIORITY_KILLABLE_SYSTEM = -800;
51 // foreground process priority
52 constexpr int RECLAIM_PRIORITY_FOREGROUND = 0;
53 // visible process priority
54 constexpr int RECLAIM_PRIORITY_VISIBLE = 50;
55 // perceived suspend delay case
56 constexpr int RECLAIM_PRIORITY_BG_SUSPEND_DELAY = 100;
57 // perceived background process priority
58 constexpr int RECLAIM_PRIORITY_BG_PERCEIVED = 200;
59 // background and connected by distribute device
60 constexpr int RECLAIM_PRIORITY_BG_DIST_DEVICE = 260;
61 // background priority
62 constexpr int RECLAIM_PRIORITY_BACKGROUND = 400;
63 // unknown process priority
64 constexpr int RECLAIM_PRIORITY_UNKNOWN = 1000;
65 
66 const std::map<int, std::string> ReclaimPriorityMapping = {
67     { RECLAIM_PRIORITY_SYSTEM, "System" },
68     { RECLAIM_ONDEMAND_SYSTEM, "OnDemand_system" },
69     { RECLAIM_PRIORITY_KILLABLE_SYSTEM, "Persistent" },
70     { RECLAIM_PRIORITY_FOREGROUND, "Foreground" },
71     { RECLAIM_PRIORITY_VISIBLE, "visible" },
72     { RECLAIM_PRIORITY_BG_SUSPEND_DELAY, "Suspend-delay" },
73     { RECLAIM_PRIORITY_BG_PERCEIVED, "Perceived" },
74     { RECLAIM_PRIORITY_BG_DIST_DEVICE, "Dist-device" },
75     { RECLAIM_PRIORITY_BACKGROUND, "Background" },
76 };
77 
78 const std::string RECLAIM_PRIORITY_UNKNOWN_DESC = "Undefined";
79 }
80 class MemoryInfo {
81 public:
82     MemoryInfo();
83     ~MemoryInfo();
84 
85     using StringMatrix = std::shared_ptr<std::vector<std::vector<std::string>>>;
86     using ValueMap = std::map<std::string, uint64_t>;
87     using GroupMap = std::map<std::string, ValueMap>;
88     using MemFun = std::function<void(MemInfoData::MemInfo&, uint64_t)>;
89 
90     bool GetMemoryInfoByPid(const int32_t &pid, StringMatrix result);
91     DumpStatus GetMemoryInfoNoPid(int fd, StringMatrix result);
92     DumpStatus DealResult(StringMatrix result);
93 
94 private:
95     enum Status {
96         SUCCESS_MORE_DATA = 1,
97         FAIL_MORE_DATA = 2,
98         SUCCESS_NO_MORE_DATA = 3,
99         FAIL_NO_MORE_DATA = 4,
100     };
101     int rawParamFd_ = 0;
102     const int LINE_WIDTH_ = 14;
103     const int RAM_WIDTH_ = 16;
104     const size_t TYPE_SIZE = 2;
105     const char SEPARATOR_ = '-';
106     const char BLANK_ = ' ';
107     const static int NAME_SIZE_ = 2;
108     const int PID_WIDTH_ = 5;
109     const int NAME_WIDTH_ = 20;
110     const int PSS_WIDTH_ = 30;
111     const int KB_WIDTH_ = 12;
112     const int NAME_AND_PID_WIDTH = 30;
113     const static int VSS_BIT = 4;
114     const static int BYTE_PER_KB = 1024;
115     bool isReady_ = false;
116     bool dumpSmapsOnStart_ = false;
117     uint64_t totalGL_ = 0;
118     uint64_t totalGraph_ = 0;
119     std::mutex mutex_;
120     std::future<GroupMap> fut_;
121     std::vector<int32_t> pids_;
122     std::vector<MemInfoData::MemUsage> memUsages_;
123     std::vector<std::pair<std::string, MemFun>> methodVec_;
124 #ifdef HIDUMPER_GRAPHIC_ENABLE
125     std::vector<OHOS::Rosen::MemoryGraphic> memGraphicVec_;
126 #endif
127     std::map<std::string, std::vector<MemInfoData::MemUsage>> adjMemResult_ = {
128         {"System", {}}, {"Foreground", {}}, {"Suspend-delay", {}},
129         {"Perceived", {}}, {"Background", {}}, {"Undefined", {}},
130     };
131     std::vector<std::string> NATIVE_HEAP_TAG_ = {"heap", "jemalloc meta", "jemalloc heap",
132                                                  "brk heap", "musl heap", "mmap heap"};
133     DmaInfo dmaInfo_;
134     void insertMemoryTitle(StringMatrix result);
135     void BuildResult(const GroupMap &infos, StringMatrix result);
136 
137     std::string AddKbUnit(const uint64_t &value) const;
138     bool GetMemByProcessPid(const int32_t &pid, const DmaInfo &dmaInfo, MemInfoData::MemUsage &usage);
139     static bool GetSmapsInfoNoPid(const int32_t &pid, GroupMap &result);
140     bool GetMeminfo(ValueMap &result);
141     bool GetHardWareUsage(StringMatrix result);
142     bool GetCMAUsage(StringMatrix result);
143     bool GetKernelUsage(const ValueMap &infos, StringMatrix result);
144     void GetProcesses(const GroupMap &infos, StringMatrix result);
145     bool GetPids();
146     void GetPssTotal(const GroupMap &infos, StringMatrix result);
147     void GetRamUsage(const GroupMap &smapsinfos, const ValueMap &meminfo, StringMatrix result);
148     void GetPurgTotal(const ValueMap &meminfo, StringMatrix result);
149     void GetPurgByPid(const int32_t &pid, StringMatrix result);
150     void GetDmaByPid(const int32_t &pid, StringMatrix result);
151     void GetHiaiServerIon(const int32_t &pid, StringMatrix result);
152     void GetNativeHeap(const GroupMap& nativeGroupMap, StringMatrix result);
153     void GetNativeValue(const std::string& tag, const GroupMap& nativeGroupMap, StringMatrix result);
154     void GetRamCategory(const GroupMap &smapsinfos, const ValueMap &meminfos, StringMatrix result);
155     void SetGraphGroupMap(GroupMap& groupMap, MemInfoData::GraphicsMemory &graphicsMemory);
156     void AddBlankLine(StringMatrix result);
157     void MemUsageToMatrix(const MemInfoData::MemUsage &memUsage, StringMatrix result);
158     void PairToStringMatrix(const std::string &titleStr, std::vector<std::pair<std::string, uint64_t>> &vec,
159                             StringMatrix result);
160     void AddMemByProcessTitle(StringMatrix result, std::string sortType);
161 
162     static uint64_t GetVss(const int32_t &pid);
163     static std::string GetProcName(const int32_t &pid);
164     static uint64_t GetProcValue(const int32_t &pid, const std::string& key);
165     static std::string GetProcessAdjLabel(const int32_t pid);
166     static std::string GetReclaimPriorityString(int32_t priority);
167     static void InitMemInfo(MemInfoData::MemInfo &memInfo);
168     static void InitMemUsage(MemInfoData::MemUsage &usage);
169     void CalcGroup(const GroupMap &infos, StringMatrix result);
170     void GetSortedMemoryInfoNoPid(StringMatrix result);
171 #ifdef HIDUMPER_GRAPHIC_ENABLE
172     void GetMemGraphics();
173 #endif
174     bool GetGraphicsMemory(int32_t pid, MemInfoData::GraphicsMemory &graphicsMemory);
175     bool GetRenderServiceGraphics(int32_t pid, MemInfoData::GraphicsMemory &graphicsMemory);
176     bool IsRenderService(int32_t pid);
177     void GetMemoryByAdj(StringMatrix result);
178     void SetPss(MemInfoData::MemInfo &meminfo, uint64_t value);
179     void SetSharedClean(MemInfoData::MemInfo &meminfo, uint64_t value);
180     void SetSharedDirty(MemInfoData::MemInfo &meminfo, uint64_t value);
181     void SetPrivateClean(MemInfoData::MemInfo &meminfo, uint64_t value);
182     void SetPrivateDirty(MemInfoData::MemInfo &meminfo, uint64_t value);
183     void SetSwap(MemInfoData::MemInfo &meminfo, uint64_t value);
184     void SetSwapPss(MemInfoData::MemInfo &meminfo, uint64_t value);
185     void SetHeapSize(MemInfoData::MemInfo &meminfo, uint64_t value);
186     void SetHeapAlloc(MemInfoData::MemInfo &meminfo, uint64_t value);
187     void SetHeapFree(MemInfoData::MemInfo &meminfo, uint64_t value);
188 };
189 } // namespace HiviewDFX
190 } // namespace OHOS
191 #endif
192