1 /*
2 * Copyright (c) 2024 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 #include "native_leak_config.h"
16
17 #include <fstream>
18 #include <regex>
19 #include <string>
20 #include <unordered_map>
21
22 #include "hiview_logger.h"
23
24 namespace OHOS {
25 namespace HiviewDFX {
26 DEFINE_LOG_TAG("NativeLeakConfig");
27
28 using std::string;
29 using std::regex;
30 using std::smatch;
31 using std::regex_match;
32 using std::unordered_map;
33 using std::ifstream;
34
35 namespace {
36 const string NATIVE_THRESHOLD_PATH = "/system/etc/hiview/memory_leak_threshold";
37
38 enum RecordItem {
39 ITEM_NAME = 1,
40 ITEM_VALUE
41 };
42 }
43
GetThresholdList(unordered_map<string,uint64_t> & list)44 bool NativeLeakConfig::GetThresholdList(unordered_map<string, uint64_t> &list)
45 {
46 ifstream fin;
47 const string &path = NATIVE_THRESHOLD_PATH;
48 fin.open(path.c_str());
49 if (!fin.is_open()) {
50 HIVIEW_LOGI("open file failed. path:%{public}s", path.c_str());
51 return false;
52 }
53 string line;
54 while (getline(fin, line)) {
55 HIVIEW_LOGI("start match line:%{public}s", line.c_str());
56 if (line.empty() || line[0] == '#') {
57 HIVIEW_LOGI("This line is a comment");
58 continue;
59 }
60 regex pattern("^\\s*([\\w._]+)\\s+(\\d+)\\s*$");
61 smatch matches;
62 if (!regex_match(line, matches, pattern)) {
63 HIVIEW_LOGW("regex_match failed, this line:%{public}s", line.c_str());
64 continue;
65 }
66 HIVIEW_LOGI("regex_match success, process: %{public}s", matches[ITEM_NAME].str().c_str());
67 list.insert(make_pair(matches[ITEM_NAME].str(), stoull(matches[ITEM_VALUE])));
68 }
69 return true;
70 }
71 } // namespace HiviewDFX
72 } // namespace OHOS
73