• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 using namespace std;
22 
23 namespace OHOS {
24 namespace HiviewDFX {
ParseSmapsRollupInfo()25 ParseSmapsRollupInfo::ParseSmapsRollupInfo()
26 {
27 }
~ParseSmapsRollupInfo()28 ParseSmapsRollupInfo::~ParseSmapsRollupInfo()
29 {
30 }
31 
32 
GetValue(const string & str,MemInfoData::MemInfo & memInfo)33 void ParseSmapsRollupInfo::GetValue(const string &str, MemInfoData::MemInfo &memInfo)
34 {
35     if (StringUtils::GetInstance().IsBegin(str, "R")) {
36         string type;
37         uint64_t value = 0;
38         bool success = MemoryUtil::GetInstance().GetTypeAndValue(str, type, value);
39         if (!success) {
40             return;
41         }
42         if (type == "Rss") {
43             memInfo.rss = value;
44         }
45     } else if (StringUtils::GetInstance().IsBegin(str, "P")) {
46         string type;
47         uint64_t value = 0;
48         bool success = MemoryUtil::GetInstance().GetTypeAndValue(str, type, value);
49         if (!success) {
50             return;
51         }
52         if (type == "Pss") {
53             memInfo.pss = value;
54         } else if (type == "Private_Clean") {
55             memInfo.privateDirty = value;
56         } else if (type == "Private_Dirty") {
57             memInfo.privateDirty = value;
58         }
59     } else if (StringUtils::GetInstance().IsBegin(str, "S")) {
60         string type;
61         uint64_t value = 0;
62         bool success = MemoryUtil::GetInstance().GetTypeAndValue(str, type, value);
63         if (!success) {
64             return;
65         }
66         if (type == "Shared_Clean") {
67             memInfo.sharedClean = value;
68         } else if (type == "Shared_Dirty") {
69             memInfo.sharedDirty = value;
70         } else if (type == "Swap") {
71             memInfo.swap = value;
72         } else if (type == "SwapPss") {
73             memInfo.swapPss = value;
74         }
75     }
76 }
77 
GetMemInfo(const int & pid,MemInfoData::MemInfo & memInfo)78 bool ParseSmapsRollupInfo::GetMemInfo(const int &pid, MemInfoData::MemInfo &memInfo)
79 {
80     string filename = "/proc/" + to_string(pid) + "/smaps_rollup";
81     ifstream in(filename);
82     if (!in) {
83         DUMPER_HILOGE(MODULE_SERVICE, "File %s not found.\n", filename.c_str());
84         return false;
85     }
86 
87     MemoryUtil::GetInstance().InitMemInfo(memInfo);
88 
89     string lineContent;
90     while (getline(in, lineContent)) {
91         GetValue(lineContent, memInfo);
92     }
93 
94     in.close();
95 
96     return true;
97 }
98 } // namespace HiviewDFX
99 } // namespace OHOS
100