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/parse/parse_meminfo.h"
17 #include <fstream>
18 #include "executor/memory/memory_filter.h"
19 #include "executor/memory/memory_util.h"
20 #include "hilog_wrapper.h"
21 #include "util/string_utils.h"
22
23 using namespace std;
24 namespace OHOS {
25 namespace HiviewDFX {
ParseMeminfo()26 ParseMeminfo::ParseMeminfo()
27 {
28 }
29
~ParseMeminfo()30 ParseMeminfo::~ParseMeminfo()
31 {
32 }
33
34 /**
35 * @description: SetData
36 * @param {string} &str-String to be inserted into result
37 * @param {ValueMap} &result-Returned results
38 * @return void
39 */
SetData(const string & str,ValueMap & result)40 void ParseMeminfo::SetData(const string &str, ValueMap &result)
41 {
42 string type = "";
43 uint64_t value = 0;
44 if (MemoryUtil::GetInstance().GetTypeValue(str, MemoryFilter::GetInstance().MEMINFO_TAG_, type, value)) {
45 result.insert(pair<string, uint64_t>(type, value));
46 }
47 }
48
49 /**
50 * @description: Get the data from meminfo
51 * @param {ValueMap} &meminfo - the meminfo result
52 * @return bool-true:success,false-fail
53 */
GetMeminfo(ValueMap & result)54 bool ParseMeminfo::GetMeminfo(ValueMap &result)
55 {
56 string filename = "/proc/meminfo";
57 ifstream in(filename);
58 if (!in) {
59 DUMPER_HILOGE(MODULE_SERVICE, "File %s not found.\n", filename.c_str());
60 return false;
61 }
62
63 string str;
64 while (getline(in, str)) {
65 SetData(str, result);
66 }
67
68 in.close();
69
70 return true;
71 }
72 } // namespace HiviewDFX
73 } // namespace OHOS
74