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 #include "executor/memory/memory_filter.h"
17 #include "util/string_utils.h"
18 
19 using namespace std;
20 namespace OHOS {
21 namespace HiviewDFX {
MemoryFilter()22 MemoryFilter::MemoryFilter()
23 {
24 }
~MemoryFilter()25 MemoryFilter::~MemoryFilter()
26 {
27 }
28 
ParseMemoryGroup(const string & name,string & group,uint64_t iNode)29 void MemoryFilter::ParseMemoryGroup(const string &name, string &group, uint64_t iNode)
30 {
31     group = iNode > 0 ? FILE_PAGE_TAG : ANON_PAGE_TAG;
32     group += "#";
33     if (GetGroupFromMap(name, group, endMap_,
34                         bind(&StringUtils::IsEnd, &StringUtils::GetInstance(), placeholders::_1, placeholders::_2)) ||
35         GetGroupFromMap(name, group, beginMap_,
36                         bind(&StringUtils::IsBegin, &StringUtils::GetInstance(), placeholders::_1, placeholders::_2))) {
37         return;
38     }
39     group += "other";
40 }
41 
ParseNativeHeapMemoryGroup(const string & name,string & group,uint64_t iNode)42 void MemoryFilter::ParseNativeHeapMemoryGroup(const string &name, string &group, uint64_t iNode)
43 {
44     group = "";
45     if (GetGroupFromMap(name, group, heapBeginMap_,
46                         bind(&StringUtils::IsBegin, &StringUtils::GetInstance(), placeholders::_1, placeholders::_2))) {
47         return;
48     }
49 }
50 
GetGroupFromMap(const string & name,string & group,const std::map<std::string,std::string> & map,MatchFunc func)51 bool MemoryFilter::GetGroupFromMap(const string &name, string &group,
52                                    const std::map<std::string, std::string> &map, MatchFunc func)
53 {
54     for (const auto &p : map) {
55         if (func(name, p.first)) {
56             group += p.second;
57             return true;
58         }
59     }
60     return false;
61 }
62 } // namespace HiviewDFX
63 } // namespace OHOS
64