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 #include "executor/memory/parse/parse_vmallocinfo.h"
16 #include <cstdlib>
17 #include <fstream>
18 #include <sstream>
19 #include "executor/memory/memory_util.h"
20 #include "hilog_wrapper.h"
21
22 using namespace std;
23 namespace OHOS {
24 namespace HiviewDFX {
ParseVmallocinfo()25 ParseVmallocinfo::ParseVmallocinfo()
26 {
27 }
28
~ParseVmallocinfo()29 ParseVmallocinfo::~ParseVmallocinfo()
30 {
31 }
32
CaclVmalloclValue(const string & str,uint64_t & totalValue)33 void ParseVmallocinfo::CaclVmalloclValue(const string &str, uint64_t &totalValue)
34 {
35 uint64_t value = 0;
36 istringstream ss(str);
37 string word;
38 vector<string> words;
39 while (ss >> word) {
40 words.push_back(word);
41 }
42 const int base = 10;
43 value = strtoull(words.at(1).c_str(), nullptr, base);
44 totalValue += value;
45 }
46
GetVmallocinfo(uint64_t & value)47 bool ParseVmallocinfo::GetVmallocinfo(uint64_t &value)
48 {
49 value = 0;
50
51 string filename = "/proc/vmallocinfo";
52 ifstream in(filename);
53 if (!in) {
54 DUMPER_HILOGE(MODULE_SERVICE, "File %s not found.\n", filename.c_str());
55 return false;
56 }
57
58 string str;
59 while (getline(in, str)) {
60 if (str.find("pages=") == string::npos) {
61 continue;
62 }
63 CaclVmalloclValue(str, value);
64 }
65
66 in.close();
67
68 return true;
69 }
70 } // namespace HiviewDFX
71 } // namespace OHOS