• 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 <string>
21 #include <vector>
22 #include "executor/memory/dma_info.h"
23 #include "executor/memory/parse/meminfo_data.h"
24 #include "common.h"
25 #include "time.h"
26 namespace OHOS {
27 namespace HiviewDFX {
28 namespace {
29 static const std::string MEMINFO_PSS = "Pss";
30 static const std::string MEMINFO_SHARED_CLEAN = "Shared_Clean";
31 static const std::string MEMINFO_SHARED_DIRTY = "Shared_Dirty";
32 static const std::string MEMINFO_PRIVATE_CLEAN = "Private_Clean";
33 static const std::string MEMINFO_PRIVATE_DIRTY = "Private_Dirty";
34 static const std::string MEMINFO_SWAP = "Swap";
35 static const std::string MEMINFO_SWAP_PSS = "SwapPss";
36 static const std::string MEMINFO_HEAP_SIZE = "Heap_Size";
37 static const std::string MEMINFO_HEAP_ALLOC = "Heap_Alloc";
38 static const std::string MEMINFO_HEAP_FREE = "Heap_Free";
39 static const std::string MEMINFO_DMA = "Dma";
40 }
41 class MemoryInfo {
42 public:
43     MemoryInfo();
44     ~MemoryInfo();
45 
46     using StringMatrix = std::shared_ptr<std::vector<std::vector<std::string>>>;
47     using ValueMap = std::map<std::string, uint64_t>;
48     using GroupMap = std::map<std::string, ValueMap>;
49     using MemFun = std::function<void(MemInfoData::MemInfo&, uint64_t)>;
50 
51     bool GetMemoryInfoByPid(const int32_t &pid, StringMatrix result);
52     DumpStatus GetMemoryInfoNoPid(StringMatrix result);
53     DumpStatus DealResult(StringMatrix result);
54 
55 private:
56     enum Status {
57         SUCCESS_MORE_DATA = 1,
58         FAIL_MORE_DATA = 2,
59         SUCCESS_NO_MORE_DATA = 3,
60         FAIL_NO_MORE_DATA = 4,
61     };
62 
63     const int LINE_WIDTH_ = 14;
64     const int RAM_WIDTH_ = 16;
65     const size_t TYPE_SIZE = 2;
66     const char SEPARATOR_ = '-';
67     const char BLANK_ = ' ';
68     const static int NAME_SIZE_ = 2;
69     const int PID_WIDTH_ = 5;
70     const int NAME_WIDTH_ = 20;
71     const int PSS_WIDTH_ = 30;
72     const int KB_WIDTH_ = 12;
73     const int NAME_AND_PID_WIDTH = 30;
74     const static int VSS_BIT = 4;
75     const static int BYTE_PER_KB = 1024;
76     bool isReady_ = false;
77     bool dumpSmapsOnStart_ = false;
78     uint64_t totalGL_ = 0;
79     uint64_t totalGraph_ = 0;
80     std::future<GroupMap> fut_;
81     std::vector<int32_t> pids_;
82     std::vector<MemInfoData::MemUsage> memUsages_;
83     std::vector<std::pair<std::string, MemFun>> methodVec_;
84     std::map<std::string, std::vector<MemInfoData::MemUsage>> adjMemResult_ = {
85         {"System", {}}, {"Foreground", {}}, {"Suspend-delay", {}},
86         {"Perceived", {}}, {"Background", {}}, {"Undefined", {}},
87     };
88     std::vector<std::string> NATIVE_HEAP_TAG_ = {"heap", "brk heap", "mmap heap", "jemalloc heap"};
89     DmaInfo dmaInfo_;
90     void insertMemoryTitle(StringMatrix result);
91     void BuildResult(const GroupMap &infos, StringMatrix result);
92 
93     std::string AddKbUnit(const uint64_t &value) const;
94     bool GetMemByProcessPid(const int32_t &pid, const DmaInfo &dmaInfo, MemInfoData::MemUsage &usage);
95     static bool GetSmapsInfoNoPid(const int32_t &pid, GroupMap &result);
96     bool GetMeminfo(ValueMap &result);
97     bool GetHardWareUsage(StringMatrix result);
98     bool GetCMAUsage(StringMatrix result);
99     bool GetKernelUsage(const ValueMap &infos, StringMatrix result);
100     void GetProcesses(const GroupMap &infos, StringMatrix result);
101     bool GetPids();
102     void GetPssTotal(const GroupMap &infos, StringMatrix result);
103     void GetRamUsage(const GroupMap &smapsinfos, const ValueMap &meminfo, StringMatrix result);
104     void GetPurgTotal(const ValueMap &meminfo, StringMatrix result);
105     void GetPurgByPid(const int32_t &pid, StringMatrix result);
106     void GetDmaByPid(const int32_t &pid, StringMatrix result);
107     void GetNativeHeap(const GroupMap& nativeGroupMap, StringMatrix result);
108     void GetNativeValue(const std::string& tag, const GroupMap& nativeGroupMap, StringMatrix result);
109     void GetRamCategory(const GroupMap &smapsinfos, const ValueMap &meminfos, StringMatrix result);
110     void AddBlankLine(StringMatrix result);
111     void MemUsageToMatrix(const MemInfoData::MemUsage &memUsage, StringMatrix result);
112     void PairToStringMatrix(const std::string &titleStr, std::vector<std::pair<std::string, uint64_t>> &vec,
113                             StringMatrix result);
114     void AddMemByProcessTitle(StringMatrix result, std::string sortType);
115     static uint64_t GetVss(const int32_t &pid);
116     static std::string GetProcName(const int32_t &pid);
117     static std::string GetProcStatusName(const int32_t &pid);
118     static uint64_t GetProcValue(const int32_t &pid, const std::string& key);
119 #ifdef HIDUMPER_MEMMGR_ENABLE
120     static std::string GetProcessAdjLabel(const int32_t pid);
121 #endif
122     static void InitMemInfo(MemInfoData::MemInfo &memInfo);
123     static void InitMemUsage(MemInfoData::MemUsage &usage);
124     void CalcGroup(const GroupMap &infos, StringMatrix result);
125     void GetSortedMemoryInfoNoPid(StringMatrix result);
126 #ifdef HIDUMPER_GRAPHIC_ENABLE
127     static void GetMemGraphics();
128 #endif
129     static bool GetGraphicsMemory(int32_t pid, MemInfoData::GraphicsMemory &graphicsMemory);
130     static bool GetRenderServiceGraphics(int32_t pid, MemInfoData::GraphicsMemory &graphicsMemory);
131     static bool IsRenderService(int32_t pid);
132     static bool IsOHService(const int32_t &pid);
133     static void GetNSPidByPid(const int32_t &pid, std::string &nsPid);
134 #ifdef HIDUMPER_MEMMGR_ENABLE
135     void GetMemoryByAdj(StringMatrix result);
136 #endif
137     void SetPss(MemInfoData::MemInfo &meminfo, uint64_t value);
138     void SetSharedClean(MemInfoData::MemInfo &meminfo, uint64_t value);
139     void SetSharedDirty(MemInfoData::MemInfo &meminfo, uint64_t value);
140     void SetPrivateClean(MemInfoData::MemInfo &meminfo, uint64_t value);
141     void SetPrivateDirty(MemInfoData::MemInfo &meminfo, uint64_t value);
142     void SetSwap(MemInfoData::MemInfo &meminfo, uint64_t value);
143     void SetSwapPss(MemInfoData::MemInfo &meminfo, uint64_t value);
144     void SetHeapSize(MemInfoData::MemInfo &meminfo, uint64_t value);
145     void SetHeapAlloc(MemInfoData::MemInfo &meminfo, uint64_t value);
146     void SetHeapFree(MemInfoData::MemInfo &meminfo, uint64_t value);
147 };
148 } // namespace HiviewDFX
149 } // namespace OHOS
150 #endif
151