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 
16 #ifndef MEMORY_FILTER_H
17 #define MEMORY_FILTER_H
18 #include <map>
19 #include <string>
20 #include <vector>
21 #include "singleton.h"
22 
23 namespace OHOS {
24 namespace HiviewDFX {
25 class MemoryFilter : public Singleton<MemoryFilter> {
26 public:
27     MemoryFilter();
28     ~MemoryFilter();
29     MemoryFilter(MemoryFilter const &) = delete;
30     void operator=(MemoryFilter const &) = delete;
31 
32     using MatchFunc = std::function<bool(std::string, std::string)>;
33 
34     enum MemoryType {
35         APPOINT_PID,
36         NOT_SPECIFIED_PID,
37     };
38 
39     int SMAPS_THREAD_NUM_ = 5;
40     size_t HARDWARE_USAGE_THREAD_NUM_ = 5;
41     const std::string FILE_PAGE_TAG = "File-backed Page";
42     const std::string ANON_PAGE_TAG = "Anonymous Page";
43     const std::string GPU_TAG = "GPU";
44     const std::string DMA_TAG = "DMA";
45     const std::string PURGEABLE_TAG = "PURGEABLE";
46     const std::string GL_OUT_LABEL = "GL";
47     const std::string GRAPH_OUT_LABEL = "Graph";
48     const std::string DMA_OUT_LABEL = "Dma";
49     const std::string PURGSUM_OUT_LABEL = "PurgSum";
50     const std::string PURGPIN_OUT_LABEL = "PurgPin";
51     const std::string NATIVE_HEAP_LABEL = "native heap";
52 
53     const std::vector<std::string> VALUE_WITH_PID = {"Pss", "Shared_Clean", "Shared_Dirty", "Private_Clean",
54                                                      "Private_Dirty", "Swap", "SwapPss", "Heap_Size", "Heap_Alloc",
55                                                      "Heap_Free"};
56 
57     const std::vector<std::string> PURG_SUM = {"Active(purg)", "Inactive(purg)"};
58     const std::vector<std::string> PURG_PIN = {"Pined(purg)"};
59 
60     const std::vector<std::string> VALUE_SMAPS_V_WITH_PID_ = {"Size", "Rss", "Pss", "Shared_Clean", "Shared_Dirty",
61         "Private_Clean", "Private_Dirty", "Swap",  "SwapPss", "Perm", "Start", "End", "Name"};
62 
63     const std::vector<std::string> TITLE_HAS_PID_ = {"Pss_Total",     "Shared_Clean", "Shared_Dirty", "Private_Clean",
64                                                      "Private_Dirty", "Swap_Total",   "SwapPss_Total", "Heap_Size",
65                                                      "Heap_Alloc", "Heap_Free"};
66 
67     const std::vector<std::string> TITLE_SMAPS_HAS_PID_ = {"Size", "Rss", "Pss", "Shared_Clean", "Shared_Dirty",
68         "Private_Clean", "Private_Dirty", "Swap", "SwapPss", "Counts", "Name"};
69 
70     const std::vector<std::string> TITLE_V_SMAPS_HAS_PID_ = {"Size", "Rss", "Pss", "Shared_Clean", "Shared_Dirty",
71         "Private_Clean", "Private_Dirty", "Swap",  "SwapPss", "Perm", "Start", "End", "Name"};
72 
73     const std::vector<std::string> TITLE_NO_PID_ = {"Pss", "SwapPss"};
74 
75     const std::vector<std::string> MEMINFO_TAG_ = {
76         "MemTotal", "MemFree",       "Cached",       "SwapTotal", "KernelStack", "SUnreclaim", "PageTables",
77         "Shmem",    "IonTotalCache", "IonTotalUsed", "Buffers",   "Mapped",      "Slab",       "VmallocUsed",
78         "Active(purg)", "Inactive(purg)",   "Pined(purg)", "KReclaimable", "SReclaimable",
79     };
80 
81     // The fields used to calculate kernel data
82     const std::vector<std::string> CALC_KERNEL_TOTAL_ = {"KernelStack", "SUnreclaim", "PageTables", "Shmem"};
83 
84     const std::vector<std::string> CALC_PSS_TOTAL_ = {"Pss", "SwapPss"};
85     const std::vector<std::string> CALC_PROCESS_TOTAL_ = {"Pss", "SwapPss"};
86     const std::vector<std::string> CALC_TOTAL_PSS_ = {"Pss"};
87     const std::vector<std::string> CALC_TOTAL_SWAP_PSS_ = {"SwapPss"};
88     const std::vector<std::string> CALC_KERNEL_USED_ = {"Shmem", "Slab", "VmallocUsed", "PageTables", "KernelStack"};
89     const std::vector<std::string> CALC_FREE_ = {"MemFree"};
90     const std::vector<std::string> CALC_CACHED_ = {"Buffers", "Cached", "KReclaimable", "SReclaimable"};
91     const std::vector<std::string> CALC_KRECL_ = {"KReclaimable"};
92     const std::vector<std::string> CALC_SRECL_ = {"SReclaimable"};
93     const std::vector<std::string> CALC_MAPPED_ = {"Mapped"};
94     const std::vector<std::string> CALC_TOTAL_ = {"MemTotal"};
95     const std::vector<std::string> CALC_ZARM_TOTAL_;
96     const std::vector<std::string> HAS_PID_ORDER_ = {"Pss", "Shared_Clean", "Shared_Dirty", "Private_Clean",
97                                                      "Private_Dirty", "Swap", "SwapPss"};
98     const std::vector<std::string> NO_PID_ORDER_ = {"Pss"};
99 
100     void ParseMemoryGroup(const std::string &name, std::string &group, uint64_t iNode);
101     void ParseNativeHeapMemoryGroup(const std::string &name, std::string &group, uint64_t iNode);
102 
103 private:
104     const std::map<std::string, std::string> beginMap_ = {
105         {"[heap]", "native heap"}, {"[stack]", "stack"}, {"[anon:stack", "stack"},
106         {"[anon:native_heap:", "native heap"}, {"[anon:ArkTS Heap", "ark ts heap"},
107         {"[anon:guard", "guard"}, {"/dev", "dev"}, {"[anon:signal_stack", "stack"},
108         {"/dmabuf", "dmabuf"}, {"/data/storage", ".hap"}, {"[anon:libc_malloc", "native heap"},
109     };
110     const std::map<std::string, std::string> heapBeginMap_ = {
111         {"[heap]", "heap"}, {"[anon:native_heap:jemalloc meta", "jemalloc meta"},
112         {"[anon:native_heap:jemalloc]", "jemalloc heap"}, {"[anon:native_heap:brk", "brk heap"},
113         {"[anon:native_heap:meta", "musl heap"}, {"[anon:native_heap:mmap", "mmap heap"},
114     };
115     const std::map<std::string, std::string> endMap_ = {
116         {".so", ".so"}, {".so.1", ".so"}, {".ttf", ".ttf"},
117         {".db", ".db"}, {".db-shm", ".db"},
118     };
119 
120     bool GetGroupFromMap(const std::string &name, std::string &group,
121                          const std::map<std::string, std::string> &map, MatchFunc func);
122 };
123 } // namespace HiviewDFX
124 } // namespace OHOS
125 #endif
126