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