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/memory_util.h"
16 #include <cstdlib>
17 #include <fstream>
18 #include <thread>
19 #include <vector>
20 #include "securec.h"
21 #include "util/string_utils.h"
22 #include "hilog_wrapper.h"
23 using namespace std;
24 namespace OHOS {
25 namespace HiviewDFX {
MemoryUtil()26 MemoryUtil::MemoryUtil()
27 {
28 }
29
~MemoryUtil()30 MemoryUtil::~MemoryUtil()
31 {
32 }
33
IsNameLine(const string & str,string & name,uint64_t & iNode)34 bool MemoryUtil::IsNameLine(const string &str, string &name, uint64_t &iNode)
35 {
36 uint32_t len = 0;
37 if (sscanf_s(str.c_str(), "%*llx-%*llx %*s %*llx %*s %llu%n", &iNode, &len) != 1) {
38 return false;
39 }
40
41 while (len < str.size() && str[len] == ' ') {
42 len++;
43 }
44 if (len < str.size()) {
45 name = str.substr(len, str.size());
46 }
47 if (name.empty()) {
48 name = "[anon]";
49 }
50 return true;
51 }
52
GetTypeValue(const string & str,const vector<string> & tags,string & type,uint64_t & value)53 bool MemoryUtil::GetTypeValue(const string &str, const vector<string> &tags, string &type, uint64_t &value)
54 {
55 type = "";
56 string tempType = "";
57 bool getSuccess = GetTypeAndValue(str, tempType, value);
58 if (!getSuccess) {
59 return false;
60 }
61
62 bool hasTag = false;
63 auto iter = find(tags.begin(), tags.end(), tempType);
64 if (iter != tags.end()) {
65 hasTag = true;
66 }
67 if (!hasTag) {
68 value = 0;
69 }
70 type = tempType;
71 return true;
72 }
73
CalcGroup(const string & group,const string & type,const uint64_t & value,GroupMap & infos)74 void MemoryUtil::CalcGroup(const string &group, const string &type, const uint64_t &value, GroupMap &infos)
75 {
76 if (infos.find(group) == infos.end()) {
77 map<string, uint64_t> valueMap;
78 valueMap.insert(pair<string, uint64_t>(type, value));
79 infos.insert(pair<string, map<string, uint64_t>>(group, valueMap));
80 } else {
81 if (infos[group].find(type) == infos[group].end()) {
82 infos[group].insert(pair<string, uint64_t>(type, value));
83 } else {
84 infos[group][type] += value;
85 }
86 }
87 }
88
RunCMD(const string & cmd,vector<string> & result)89 bool MemoryUtil::RunCMD(const string &cmd, vector<string> &result)
90 {
91 FILE* fp = popen(("/system/bin/" + cmd).c_str(), "r");
92 if (fp == nullptr) {
93 return false;
94 }
95 char* buffer = nullptr;
96 size_t len = 0;
97 while (getline(&buffer, &len, fp) != -1) {
98 std::string line = buffer;
99 StringUtils::GetInstance().ReplaceAll(line, "\n", "");
100 result.push_back(line);
101 }
102 pclose(fp);
103 return true;
104 }
105
GetMaxThreadNum(const size_t & threadNum)106 size_t MemoryUtil::GetMaxThreadNum(const size_t &threadNum)
107 {
108 size_t maxThreadNum = 0;
109 size_t const hardwareThreads = std::thread::hardware_concurrency();
110 if (hardwareThreads == 0) {
111 maxThreadNum = threadNum;
112 } else if (hardwareThreads < threadNum) {
113 maxThreadNum = hardwareThreads;
114 } else {
115 maxThreadNum = threadNum;
116 }
117 if (maxThreadNum == 0) {
118 maxThreadNum = 1;
119 }
120 return maxThreadNum;
121 }
122
InitMemInfo(MemInfoData::MemInfo & memInfo)123 void MemoryUtil::InitMemInfo(MemInfoData::MemInfo &memInfo)
124 {
125 memInfo.rss = 0;
126 memInfo.pss = 0;
127 memInfo.sharedClean = 0;
128 memInfo.sharedDirty = 0;
129 memInfo.privateClean = 0;
130 memInfo.privateDirty = 0;
131 memInfo.swap = 0;
132 memInfo.swapPss = 0;
133 memInfo.heapSize = 0;
134 memInfo.heapAlloc = 0;
135 memInfo.heapFree = 0;
136 }
137
138
InitMemSmapsInfo(MemInfoData::MemSmapsInfo & memInfo)139 void MemoryUtil::InitMemSmapsInfo(MemInfoData::MemSmapsInfo &memInfo)
140 {
141 memInfo.rss = 0;
142 memInfo.pss = 0;
143 memInfo.sharedClean = 0;
144 memInfo.sharedDirty = 0;
145 memInfo.privateClean = 0;
146 memInfo.privateDirty = 0;
147 memInfo.swap = 0;
148 memInfo.swapPss = 0;
149 memInfo.name = "";
150 memInfo.size = 0;
151 memInfo.counts = 0;
152 memInfo.start = "";
153 memInfo.end = "";
154 memInfo.perm = "";
155 }
156
157
InitMemUsage(MemInfoData::MemUsage & usage)158 void MemoryUtil::InitMemUsage(MemInfoData::MemUsage &usage)
159 {
160 usage.vss = 0;
161 usage.rss = 0;
162 usage.uss = 0;
163 usage.pss = 0;
164 usage.gl = 0;
165 usage.graph = 0;
166 usage.purgSum = 0;
167 usage.purgPin = 0;
168 usage.pid = 0;
169 }
170
InitGraphicsMemory(MemInfoData::GraphicsMemory & graphicsMemory)171 void MemoryUtil::InitGraphicsMemory(MemInfoData::GraphicsMemory &graphicsMemory)
172 {
173 graphicsMemory.gl = 0;
174 graphicsMemory.graph = 0;
175 }
176
GetTypeAndValue(const string & str,string & type,uint64_t & value)177 bool MemoryUtil::GetTypeAndValue(const string &str, string &type, uint64_t &value)
178 {
179 string::size_type typePos = str.find(":");
180 if (typePos != str.npos) {
181 type = str.substr(0, typePos);
182 string valueStr = str.substr(typePos + 1);
183 const int base = 10;
184 value = strtoull(valueStr.c_str(), nullptr, base);
185 return true;
186 }
187 return false;
188 }
189
SetMemTotalValue(const string & value,vector<string> & lines,vector<string> & values,bool flag)190 void MemoryUtil::SetMemTotalValue(const string &value, vector<string> &lines, vector<string> &values, bool flag)
191 {
192 if (!flag) {
193 string separator = "-";
194 string space = " ";
195 StringUtils::GetInstance().SetWidth(LINE_WIDTH_, BLANK_, false, space);
196 StringUtils::GetInstance().SetWidth(LINE_WIDTH_, SEPARATOR_, false, separator);
197 lines.push_back(separator);
198 }
199 string tempValue = value;
200 if (flag) {
201 if (StringUtils::GetInstance().IsSameStr(value, "Summary")) {
202 constexpr int SMPAPS_INFO_WIDTH = 25;
203 StringUtils::GetInstance().SetWidth(SMPAPS_INFO_WIDTH, BLANK_, false, tempValue);
204 } else {
205 StringUtils::GetInstance().SetWidth(SMAPS_LINE_WIDTH_, BLANK_, true, tempValue);
206 }
207 } else {
208 StringUtils::GetInstance().SetWidth(LINE_WIDTH_, BLANK_, false, tempValue);
209 }
210 values.push_back(tempValue);
211 }
212
PermToString(const uint64_t iPerm)213 string MemoryUtil::PermToString(const uint64_t iPerm)
214 {
215 string perm = "";
216 const string permValue = "rwxp";
217 for (size_t i = 0; i < permValue.size(); i++) {
218 if ((iPerm & (1 << i)) == (1 << i)) {
219 perm += permValue[i];
220 } else if (i == permValue.size() - 1) {
221 perm += "s";
222 } else {
223 perm += "-";
224 }
225 }
226 return perm;
227 }
228
PermToInt(const string & perm)229 uint64_t MemoryUtil::PermToInt(const string& perm)
230 {
231 uint64_t iPerm = 0;
232 for (size_t i = 0; i < perm.size(); i++) {
233 if (perm[i] != '-' && perm[i] != 's') {
234 iPerm |= (1 << i);
235 }
236 }
237 return iPerm;
238 }
239 } // namespace HiviewDFX
240 } // namespace OHOS
241