• 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 DMA_TAG = "DMA";
46     const std::string GL_OUT_LABEL = "GL";
47     const std::string GRAPH_OUT_LABEL = "Graph";
48     const std::vector<std::pair<MemoryTrackerType, std::string>> MEMORY_TRACKER_TYPES = {
49         {MEMORY_TRACKER_TYPE_GL, "GL"}, {MEMORY_TRACKER_TYPE_GRAPH, "Graph"},
50         {MEMORY_TRACKER_TYPE_OTHER, "Other"}
51     };
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> VALUE_SMAPS_V_WITH_PID_ = {"Size", "Rss", "Pss", "Shared_Clean", "Shared_Dirty",
58         "Private_Clean", "Private_Dirty", "Swap",  "SwapPss", "Start", "End", "Name"};
59 
60     const std::vector<std::string> TITLE_HAS_PID_ = {"Pss_Total",     "Shared_Clean", "Shared_Dirty", "Private_Clean",
61                                                      "Private_Dirty", "Swap_Total",   "SwapPss_Total", "Heap_Size",
62                                                      "Heap_Alloc", "Heap_Free"};
63 
64     const std::vector<std::string> TITLE_SMAPS_HAS_PID_ = {"Size", "Rss", "Pss", "Shared_Clean", "Shared_Dirty",
65         "Private_Clean", "Private_Dirty", "Swap", "Swap_Pss", "Counts", "Name"};
66 
67     const std::vector<std::string> TITLE_V_SMAPS_HAS_PID_ = {"Size", "Rss", "Pss", "Shared_Clean", "Shared_Dirty",
68         "Private_Clean", "Private_Dirty", "Swap",  "Swap_Pss", "Start", "End", "Name"};
69 
70     const std::vector<std::string> TITLE_NO_PID_ = {"Pss", "SwapPss"};
71 
72     std::vector<std::string> MEMINFO_TAG_ = {
73         "MemTotal", "MemFree",       "Cached",       "SwapTotal", "KernelStack", "SUnreclaim", "PageTables",
74         "Shmem",    "IonTotalCache", "IonTotalUsed", "Buffers",   "Mapped",      "Slab",       "VmallocUsed",
75     };
76 
77     // The fields used to calculate kernel data
78     std::vector<std::string> CALC_KERNEL_TOTAL_ = {"KernelStack", "SUnreclaim", "PageTables", "Shmem"};
79 
80     std::vector<std::string> CALC_PSS_TOTAL_ = {"Pss", "SwapPss"};
81     std::vector<std::string> CALC_PROCESS_TOTAL_ = {"Pss", "SwapPss"};
82     std::vector<std::string> CALC_TOTAL_PSS_ = {"Pss"};
83     std::vector<std::string> CALC_TOTAL_SWAP_PSS_ = {"SwapPss"};
84     std::vector<std::string> CALC_KERNEL_USED_ = {"Shmem", "Slab", "VmallocUsed", "PageTables", "KernelStack"};
85     std::vector<std::string> CALC_FREE_ = {"MemFree"};
86     std::vector<std::string> CALC_CACHED_ = {"Buffers", "Cached", "Mapped"};
87     std::vector<std::string> CALC_TOTAL_ = {"MemTotal"};
88     std::vector<std::string> CALC_ZARM_TOTAL_;
89     std::vector<std::string> HAS_PID_ORDER_ = {"Pss",           "Shared_Clean", "Shared_Dirty", "Private_Clean",
90                                                "Private_Dirty", "Swap",         "SwapPss"};
91     std::vector<std::string> NO_PID_ORDER_ = {"Pss"};
92     void ParseMemoryGroup(const std::string &name, std::string &group, uint64_t iNode);
93 
94 private:
95     const std::map<std::string, std::string> beginMap_ = {
96         {"[heap]", "native heap"}, {"[stack]", "stack"}, {"[anon:stack", "stack"},
97         {"[anon:native_heap:", "native heap"}, {"[anon:ArkTS Heap]", "ark ts heap"},
98         {"[anon:guard", "guard"}, {"/dev", "dev"}, {"[anon:signal_stack", "stack"},
99         {"/dmabuf", "dmabuf"}, {"/data/storage", ".hap"}, {"[anon:libc_malloc", "native heap"},
100     };
101     const std::map<std::string, std::string> endMap_ = {
102         {".so", ".so"}, {".so.1", ".so"}, {".ttf", ".ttf"},
103         {".db", ".db"}, {".db-shm", ".db"},
104     };
105 
106     bool GetGroupFromMap(const std::string &name, std::string &group,
107                          const std::map<std::string, std::string> &map, MatchFunc func);
108 };
109 } // namespace HiviewDFX
110 } // namespace OHOS
111 #endif
112