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 #include "util/file_utils.h"
22 
23 using namespace std;
24 namespace OHOS {
25 namespace HiviewDFX {
ParseVmallocinfo()26 ParseVmallocinfo::ParseVmallocinfo()
27 {
28 }
29 
~ParseVmallocinfo()30 ParseVmallocinfo::~ParseVmallocinfo()
31 {
32 }
33 
CaclVmalloclValue(const string & str,uint64_t & totalValue)34 void ParseVmallocinfo::CaclVmalloclValue(const string &str, uint64_t &totalValue)
35 {
36     uint64_t value = 0;
37     istringstream ss(str);
38     string word;
39     vector<string> words;
40     while (ss >> word) {
41         words.push_back(word);
42     }
43     const int base = 10;
44     value = strtoull(words.at(1).c_str(), nullptr, base);
45     totalValue += value;
46 }
47 
GetVmallocinfo(uint64_t & value)48 bool ParseVmallocinfo::GetVmallocinfo(uint64_t &value)
49 {
50     value = 0;
51     string path = "/proc/vmallocinfo";
52     bool ret = FileUtils::GetInstance().LoadStringFromProcCb(path, false, true, [&](const string& line) -> void {
53         if (line.find("pages=") != string::npos) {
54             CaclVmalloclValue(line, value);
55         }
56     });
57     return ret;
58 }
59 } // namespace HiviewDFX
60 } // namespace OHOS