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_smaps_rollup_info.h"
16 #include <fstream>
17 #include "executor/memory/memory_util.h"
18 #include "hilog_wrapper.h"
19 #include "util/string_utils.h"
20 #include "util/file_utils.h"
21
22 using namespace std;
23
24 namespace OHOS {
25 namespace HiviewDFX {
ParseSmapsRollupInfo()26 ParseSmapsRollupInfo::ParseSmapsRollupInfo()
27 {
28 }
~ParseSmapsRollupInfo()29 ParseSmapsRollupInfo::~ParseSmapsRollupInfo()
30 {
31 }
32
33
GetValue(const string & str,MemInfoData::MemInfo & memInfo)34 void ParseSmapsRollupInfo::GetValue(const string &str, MemInfoData::MemInfo &memInfo)
35 {
36 if (StringUtils::GetInstance().IsBegin(str, "R")) {
37 string type;
38 uint64_t value = 0;
39 bool success = MemoryUtil::GetInstance().GetTypeAndValue(str, type, value);
40 if (!success) {
41 return;
42 }
43 if (type == "Rss") {
44 memInfo.rss = value;
45 }
46 } else if (StringUtils::GetInstance().IsBegin(str, "P")) {
47 string type;
48 uint64_t value = 0;
49 bool success = MemoryUtil::GetInstance().GetTypeAndValue(str, type, value);
50 if (!success) {
51 return;
52 }
53 if (type == "Pss") {
54 memInfo.pss = value;
55 } else if (type == "Private_Clean") {
56 memInfo.privateClean = value;
57 } else if (type == "Private_Dirty") {
58 memInfo.privateDirty = value;
59 }
60 } else if (StringUtils::GetInstance().IsBegin(str, "S")) {
61 string type;
62 uint64_t value = 0;
63 bool success = MemoryUtil::GetInstance().GetTypeAndValue(str, type, value);
64 if (!success) {
65 return;
66 }
67 if (type == "Shared_Clean") {
68 memInfo.sharedClean = value;
69 } else if (type == "Shared_Dirty") {
70 memInfo.sharedDirty = value;
71 } else if (type == "Swap") {
72 memInfo.swap = value;
73 } else if (type == "SwapPss") {
74 memInfo.swapPss = value;
75 }
76 }
77 }
78
GetMemInfo(const int & pid,MemInfoData::MemInfo & memInfo)79 bool ParseSmapsRollupInfo::GetMemInfo(const int &pid, MemInfoData::MemInfo &memInfo)
80 {
81 string path = "/proc/" + to_string(pid) + "/smaps_rollup";
82 bool ret = FileUtils::GetInstance().LoadStringFromProcCb(path, false, true, [&](const string& line) -> void {
83 GetValue(line, memInfo);
84 });
85 return ret;
86 }
87 } // namespace HiviewDFX
88 } // namespace OHOS
89