• 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/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 }
155 
156 
InitMemUsage(MemInfoData::MemUsage & usage)157 void MemoryUtil::InitMemUsage(MemInfoData::MemUsage &usage)
158 {
159     usage.vss = 0;
160     usage.rss = 0;
161     usage.uss = 0;
162     usage.pss = 0;
163     usage.gl = 0;
164     usage.graph = 0;
165     usage.pid = 0;
166 }
167 
InitGraphicsMemory(MemInfoData::GraphicsMemory & graphicsMemory)168 void MemoryUtil::InitGraphicsMemory(MemInfoData::GraphicsMemory &graphicsMemory)
169 {
170     graphicsMemory.gl = 0;
171     graphicsMemory.graph = 0;
172 }
173 
GetTypeAndValue(const string & str,string & type,uint64_t & value)174 bool MemoryUtil::GetTypeAndValue(const string &str, string &type, uint64_t &value)
175 {
176     string::size_type typePos = str.find(":");
177     if (typePos != str.npos) {
178         type = str.substr(0, typePos);
179         string valueStr = str.substr(typePos + 1);
180         const int base = 10;
181         value = strtoull(valueStr.c_str(), nullptr, base);
182         return true;
183     }
184     return false;
185 }
186 
SetMemTotalValue(const string & value,vector<string> & lines,vector<string> & values,bool flag)187 void MemoryUtil::SetMemTotalValue(const string &value, vector<string> &lines, vector<string> &values, bool flag)
188 {
189     if (!flag) {
190         string separator = "-";
191         string space = " ";
192         StringUtils::GetInstance().SetWidth(LINE_WIDTH_, BLANK_, false, space);
193         StringUtils::GetInstance().SetWidth(LINE_WIDTH_, SEPARATOR_, false, separator);
194         lines.push_back(separator);
195     }
196     string tempValue = value;
197     if (flag) {
198         if (StringUtils::GetInstance().IsSameStr(value, "Summary")) {
199             constexpr int SMPAPS_INFO_WIDTH = 25;
200             StringUtils::GetInstance().SetWidth(SMPAPS_INFO_WIDTH, BLANK_, false, tempValue);
201         } else {
202             StringUtils::GetInstance().SetWidth(SMAPS_LINE_WIDTH_, BLANK_, true, tempValue);
203         }
204     } else {
205         StringUtils::GetInstance().SetWidth(LINE_WIDTH_, BLANK_, false, tempValue);
206     }
207     values.push_back(tempValue);
208 }
209 } // namespace HiviewDFX
210 } // namespace OHOS
211