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