• 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 <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 static const std::string MEMINFO_HEAP_SIZE = "Heap_Size";
36 static const std::string MEMINFO_HEAP_ALLOC = "Heap_Alloc";
37 static const std::string MEMINFO_HEAP_FREE = "Heap_Free";
38 }
39 class MemoryInfo {
40 public:
41     MemoryInfo();
42     ~MemoryInfo();
43 
44     using StringMatrix = std::shared_ptr<std::vector<std::vector<std::string>>>;
45     using ValueMap = std::map<std::string, uint64_t>;
46     using GroupMap = std::map<std::string, ValueMap>;
47     using MemFun = std::function<void(MemInfoData::MemInfo&, uint64_t)>;
48 
49     bool GetMemoryInfoByPid(const int &pid, StringMatrix result);
50     DumpStatus GetMemoryInfoNoPid(StringMatrix result);
51     DumpStatus DealResult(StringMatrix result);
52 
53 private:
54     enum Status {
55         SUCCESS_MORE_DATA = 1,
56         FAIL_MORE_DATA = 2,
57         SUCCESS_NO_MORE_DATA = 3,
58         FAIL_NO_MORE_DATA = 4,
59     };
60 
61     const int LINE_WIDTH_ = 14;
62     const int RAM_WIDTH_ = 16;
63     const size_t TYPE_SIZE = 2;
64     const char SEPARATOR_ = '-';
65     const char BLANK_ = ' ';
66     const static int NAME_SIZE_ = 2;
67     const int PID_WIDTH_ = 5;
68     const int NAME_WIDTH_ = 20;
69     const int PSS_WIDTH_ = 25;
70     const int KB_WIDTH_ = 12;
71     const int NAME_AND_PID_WIDTH = 30;
72     const static int VSS_BIT = 4;
73     const static int BYTE_PER_KB = 1024;
74     bool isReady_ = false;
75     bool dumpSmapsOnStart_ = false;
76     uint64_t totalGL_ = 0;
77     uint64_t totalGraph_ = 0;
78     std::future<GroupMap> fut_;
79     std::vector<int> pids_;
80     std::vector<MemInfoData::MemUsage> memUsages_;
81     std::vector<std::pair<std::string, MemFun>> methodVec_;
82     std::map<std::string, std::vector<MemInfoData::MemUsage>> adjMemResult_ = {
83         {"System", {}}, {"Foreground", {}}, {"Suspend-delay", {}},
84         {"Perceived", {}}, {"Background", {}}, {"Undefined", {}},
85     };
86     void insertMemoryTitle(StringMatrix result);
87     void BuildResult(const GroupMap &infos, StringMatrix result);
88 
89     std::string AddKbUnit(const uint64_t &value) const;
90     static bool GetMemByProcessPid(const int &pid, MemInfoData::MemUsage &usage);
91     static bool GetSmapsInfoNoPid(const int &pid, GroupMap &result);
92     bool GetMeminfo(ValueMap &result);
93     bool GetHardWareUsage(StringMatrix result);
94     bool GetCMAUsage(StringMatrix result);
95     bool GetKernelUsage(const ValueMap &infos, StringMatrix result);
96     void GetProcesses(const GroupMap &infos, StringMatrix result);
97     bool GetPids();
98     void GetPssTotal(const GroupMap &infos, StringMatrix result);
99     void GetRamUsage(const GroupMap &smapsinfos, const ValueMap &meminfo, StringMatrix result);
100     void GetRamCategory(const GroupMap &smapsinfos, const ValueMap &meminfos, StringMatrix result);
101     void AddBlankLine(StringMatrix result);
102     void MemUsageToMatrix(const MemInfoData::MemUsage &memUsage, StringMatrix result);
103     void PairToStringMatrix(const std::string &titleStr, std::vector<std::pair<std::string, uint64_t>> &vec,
104                             StringMatrix result);
105     void AddMemByProcessTitle(StringMatrix result, std::string sortType);
106     static uint64_t GetVss(const int &pid);
107     static std::string GetProcName(const int &pid);
108 #ifdef HIDUMPER_MEMMGR_ENABLE
109     static std::string GetProcessAdjLabel(const int pid);
110 #endif
111     static void InitMemInfo(MemInfoData::MemInfo &memInfo);
112     static void InitMemUsage(MemInfoData::MemUsage &usage);
113     void CalcGroup(const GroupMap &infos, StringMatrix result);
114     void GetSortedMemoryInfoNoPid(StringMatrix result);
115 #ifdef HIDUMPER_GRAPHIC_ENABLE
116     static void GetMemGraphics();
117 #endif
118     static bool GetGraphicsMemory(int32_t pid, MemInfoData::GraphicsMemory &graphicsMemory);
119     static bool GetRenderServiceGraphics(int32_t pid, MemInfoData::GraphicsMemory &graphicsMemory);
120     static bool IsRenderService(int32_t pid);
121 #ifdef HIDUMPER_MEMMGR_ENABLE
122     void GetMemoryByAdj(StringMatrix result);
123 #endif
124     void SetPss(MemInfoData::MemInfo &meminfo, uint64_t value);
125     void SetSharedClean(MemInfoData::MemInfo &meminfo, uint64_t value);
126     void SetSharedDirty(MemInfoData::MemInfo &meminfo, uint64_t value);
127     void SetPrivateClean(MemInfoData::MemInfo &meminfo, uint64_t value);
128     void SetPrivateDirty(MemInfoData::MemInfo &meminfo, uint64_t value);
129     void SetSwap(MemInfoData::MemInfo &meminfo, uint64_t value);
130     void SetSwapPss(MemInfoData::MemInfo &meminfo, uint64_t value);
131     void SetHeapSize(MemInfoData::MemInfo &meminfo, uint64_t value);
132     void SetHeapAlloc(MemInfoData::MemInfo &meminfo, uint64_t value);
133     void SetHeapFree(MemInfoData::MemInfo &meminfo, uint64_t value);
134 };
135 } // namespace HiviewDFX
136 } // namespace OHOS
137 #endif
138