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     std::lock_guard<std::mutex> lock(mutex_);
92     FILE* fp = popen(("/system/bin/" + cmd).c_str(), "r");
93     if (fp == nullptr) {
94         return false;
95     }
96     char* buffer = nullptr;
97     size_t len = 0;
98     while (getline(&buffer, &len, fp) != -1) {
99         std::string line = buffer;
100         StringUtils::GetInstance().ReplaceAll(line, "\n", "");
101         result.push_back(line);
102     }
103     // free memory
104     if (buffer != nullptr) {
105         delete[] buffer;
106         buffer = nullptr;
107     }
108     pclose(fp);
109     return true;
110 }
111 
GetMaxThreadNum(const size_t & threadNum)112 size_t MemoryUtil::GetMaxThreadNum(const size_t &threadNum)
113 {
114     size_t maxThreadNum = 0;
115     size_t const hardwareThreads = std::thread::hardware_concurrency();
116     if (hardwareThreads == 0) {
117         maxThreadNum = threadNum;
118     } else if (hardwareThreads < threadNum) {
119         maxThreadNum = hardwareThreads;
120     } else {
121         maxThreadNum = threadNum;
122     }
123     if (maxThreadNum == 0) {
124         maxThreadNum = 1;
125     }
126     return maxThreadNum;
127 }
128 
InitMemInfo(MemInfoData::MemInfo & memInfo)129 void MemoryUtil::InitMemInfo(MemInfoData::MemInfo &memInfo)
130 {
131     memInfo.rss = 0;
132     memInfo.pss = 0;
133     memInfo.sharedClean = 0;
134     memInfo.sharedDirty = 0;
135     memInfo.privateClean = 0;
136     memInfo.privateDirty = 0;
137     memInfo.swap = 0;
138     memInfo.swapPss = 0;
139     memInfo.heapSize = 0;
140     memInfo.heapAlloc = 0;
141     memInfo.heapFree = 0;
142 }
143 
144 
InitMemSmapsInfo(MemInfoData::MemSmapsInfo & memInfo)145 void MemoryUtil::InitMemSmapsInfo(MemInfoData::MemSmapsInfo &memInfo)
146 {
147     memInfo.rss = 0;
148     memInfo.pss = 0;
149     memInfo.sharedClean = 0;
150     memInfo.sharedDirty = 0;
151     memInfo.privateClean = 0;
152     memInfo.privateDirty = 0;
153     memInfo.swap = 0;
154     memInfo.swapPss = 0;
155     memInfo.name = "";
156     memInfo.size = 0;
157     memInfo.counts = 0;
158     memInfo.start = "";
159     memInfo.end = "";
160     memInfo.perm = "";
161 }
162 
163 
InitMemUsage(MemInfoData::MemUsage & usage)164 void MemoryUtil::InitMemUsage(MemInfoData::MemUsage &usage)
165 {
166     usage.vss = 0;
167     usage.rss = 0;
168     usage.uss = 0;
169     usage.pss = 0;
170     usage.gl = 0;
171     usage.graph = 0;
172     usage.purgSum = 0;
173     usage.purgPin = 0;
174     usage.pid = 0;
175 }
176 
InitGraphicsMemory(MemInfoData::GraphicsMemory & graphicsMemory)177 void MemoryUtil::InitGraphicsMemory(MemInfoData::GraphicsMemory &graphicsMemory)
178 {
179     graphicsMemory.gl = 0;
180     graphicsMemory.graph = 0;
181 }
182 
GetTypeAndValue(const string & str,string & type,uint64_t & value)183 bool MemoryUtil::GetTypeAndValue(const string &str, string &type, uint64_t &value)
184 {
185     string::size_type typePos = str.find(":");
186     if (typePos != str.npos) {
187         type = str.substr(0, typePos);
188         string valueStr = str.substr(typePos + 1);
189         const int base = 10;
190         value = strtoull(valueStr.c_str(), nullptr, base);
191         return true;
192     }
193     return false;
194 }
195 
SetMemTotalValue(const string & value,vector<string> & lines,vector<string> & values,bool flag)196 void MemoryUtil::SetMemTotalValue(const string &value, vector<string> &lines, vector<string> &values, bool flag)
197 {
198     if (!flag) {
199         string separator = "-";
200         string space = " ";
201         StringUtils::GetInstance().SetWidth(LINE_WIDTH_, BLANK_, false, space);
202         StringUtils::GetInstance().SetWidth(LINE_WIDTH_, SEPARATOR_, false, separator);
203         lines.push_back(separator + SEPARATOR_);
204     }
205     string tempValue = value;
206     if (flag) {
207         if (StringUtils::GetInstance().IsSameStr(value, "Summary")) {
208             constexpr int SMPAPS_INFO_WIDTH = 25;
209             StringUtils::GetInstance().SetWidth(SMPAPS_INFO_WIDTH, BLANK_, false, tempValue);
210         } else {
211             StringUtils::GetInstance().SetWidth(SMAPS_LINE_WIDTH_, BLANK_, true, tempValue);
212         }
213     } else {
214         StringUtils::GetInstance().SetWidth(LINE_WIDTH_, BLANK_, false, tempValue);
215         tempValue += BLANK_;
216     }
217     values.push_back(tempValue);
218 }
219 
PermToInt(const string & perm)220 uint64_t MemoryUtil::PermToInt(const string& perm)
221 {
222     uint64_t iPerm = 0;
223     for (size_t i = 0; i < perm.size(); i++) {
224         if (perm[i] != '-' && perm[i] != 's') {
225             iPerm |= (1 << i);
226         }
227     }
228     return iPerm;
229 }
230 } // namespace HiviewDFX
231 } // namespace OHOS
232