• 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> RECLAIM_PRIORITY = {"System", "Foreground", "Suspend-delay", "Perceived",
54                                                        "Background", "Undefined"};
55 
56     const std::vector<std::string> VALUE_WITH_PID = {"Pss", "Shared_Clean", "Shared_Dirty", "Private_Clean",
57                                                      "Private_Dirty", "Swap", "SwapPss"};
58 
59     const std::vector<std::string> TITLE_HAS_PID_ = {"Pss_Total",     "Shared_Clean", "Shared_Dirty", "Private_Clean",
60                                                      "Private_Dirty", "Swap_Total",   "SwapPss_Total"};
61 
62     const std::vector<std::string> TITLE_NO_PID_ = {"Pss", "SwapPss"};
63 
64     std::vector<std::string> MEMINFO_TAG_ = {
65         "MemTotal", "MemFree",       "Cached",       "SwapTotal", "KernelStack", "SUnreclaim", "PageTables",
66         "Shmem",    "IonTotalCache", "IonTotalUsed", "Buffers",   "Mapped",      "Slab",       "VmallocUsed",
67     };
68 
69     // The fields used to calculate kernel data
70     std::vector<std::string> CALC_KERNEL_TOTAL_ = {"KernelStack", "SUnreclaim", "PageTables", "Shmem"};
71 
72     std::vector<std::string> CALC_PSS_TOTAL_ = {"Pss", "SwapPss"};
73     std::vector<std::string> CALC_PROCESS_TOTAL_ = {"Pss", "SwapPss"};
74     std::vector<std::string> CALC_TOTAL_PSS_ = {"Pss"};
75     std::vector<std::string> CALC_TOTAL_SWAP_PSS_ = {"SwapPss"};
76     std::vector<std::string> CALC_KERNEL_USED_ = {"Shmem", "Slab", "VmallocUsed", "PageTables", "KernelStack"};
77     std::vector<std::string> CALC_FREE_ = {"MemFree"};
78     std::vector<std::string> CALC_CACHED_ = {"Buffers", "Cached", "Mapped"};
79     std::vector<std::string> CALC_TOTAL_ = {"MemTotal"};
80     std::vector<std::string> CALC_ZARM_TOTAL_;
81     std::vector<std::string> HAS_PID_ORDER_ = {"Pss",           "Shared_Clean", "Shared_Dirty", "Private_Clean",
82                                                "Private_Dirty", "Swap",         "SwapPss"};
83     std::vector<std::string> NO_PID_ORDER_ = {"Pss"};
84     void ParseMemoryGroup(const std::string &name, std::string &group, uint64_t iNode);
85 
86 private:
87     const std::map<std::string, std::string> beginMap_ = {
88         {"[heap]", "native heap"}, {"[stack]", "stack"}, {"[anon:stack", "stack"},
89         {"[anon:native_heap:", "native heap"}, {"[anon:ArkJS Heap]", "ark js heap"},
90         {"[anon:guard", "guard"}, {"/dev", "dev"}, {"[anon:signal_stack", "stack"},
91         {"/dmabuf", "dmabuf"}, {"/data/storage", ".hap"}, {"[anon:libc_malloc", "native heap"},
92     };
93     const std::map<std::string, std::string> endMap_ = {
94         {".so", ".so"}, {".so.1", ".so"}, {".ttf", ".ttf"},
95         {".db", ".db"}, {".db-shm", ".db"},
96     };
97 
98     bool GetGroupFromMap(const std::string &name, std::string &group,
99                          const std::map<std::string, std::string> &map, MatchFunc func);
100 };
101 } // namespace HiviewDFX
102 } // namespace OHOS
103 #endif
104