• 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 using namespace std;
23 namespace OHOS {
24 namespace HiviewDFX {
MemoryUtil()25 MemoryUtil::MemoryUtil()
26 {
27 }
28 
~MemoryUtil()29 MemoryUtil::~MemoryUtil()
30 {
31 }
32 
IsNameLine(const string & str,string & name,uint64_t & iNode)33 bool MemoryUtil::IsNameLine(const string &str, string &name, uint64_t &iNode)
34 {
35     uint32_t len = 0;
36     if (sscanf_s(str.c_str(), "%*llx-%*llx %*s %*llx %*s %llu%n", &iNode, &len) != 1) {
37         return false;
38     }
39 
40     while (len < str.size() && str[len] == ' ') {
41         len++;
42     }
43     if (len < str.size()) {
44         name = str.substr(len, str.size());
45     }
46     return true;
47 }
48 
GetTypeValue(const string & str,const vector<string> & tags,string & type,uint64_t & value)49 bool MemoryUtil::GetTypeValue(const string &str, const vector<string> &tags, string &type, uint64_t &value)
50 {
51     type = "";
52     string tempType = "";
53     bool getSuccess = GetTypeAndValue(str, tempType, value);
54     if (!getSuccess) {
55         return false;
56     }
57 
58     bool hasTag = false;
59     auto iter = find(tags.begin(), tags.end(), tempType);
60     if (iter != tags.end()) {
61         hasTag = true;
62     }
63     if (!hasTag) {
64         value = 0;
65     }
66     type = tempType;
67     return true;
68 }
69 
CalcGroup(const string & group,const string & type,const uint64_t & value,GroupMap & infos)70 void MemoryUtil::CalcGroup(const string &group, const string &type, const uint64_t &value, GroupMap &infos)
71 {
72     if (infos.find(group) == infos.end()) {
73         map<string, uint64_t> valueMap;
74         valueMap.insert(pair<string, uint64_t>(type, value));
75         infos.insert(pair<string, map<string, uint64_t>>(group, valueMap));
76     } else {
77         if (infos[group].find(type) == infos[group].end()) {
78             infos[group].insert(pair<string, uint64_t>(type, value));
79         } else {
80             infos[group][type] += value;
81         }
82     }
83 }
84 
RunCMD(const string & cmd,vector<string> & result)85 bool MemoryUtil::RunCMD(const string &cmd, vector<string> &result)
86 {
87     FILE* fp = popen(("/system/bin/" + cmd).c_str(), "r");
88     if (fp == nullptr) {
89         return false;
90     }
91     char* buffer = nullptr;
92     size_t len = 0;
93     while (getline(&buffer, &len, fp) != -1) {
94         std::string line = buffer;
95         StringUtils::GetInstance().ReplaceAll(line, "\n", "");
96         result.push_back(line);
97     }
98     pclose(fp);
99     return true;
100 }
101 
GetMaxThreadNum(const size_t & threadNum)102 size_t MemoryUtil::GetMaxThreadNum(const size_t &threadNum)
103 {
104     size_t maxThreadNum = 0;
105     size_t const hardwareThreads = std::thread::hardware_concurrency();
106     if (hardwareThreads == 0) {
107         maxThreadNum = threadNum;
108     } else if (hardwareThreads < threadNum) {
109         maxThreadNum = hardwareThreads;
110     } else {
111         maxThreadNum = threadNum;
112     }
113     if (maxThreadNum == 0) {
114         maxThreadNum = 1;
115     }
116     return maxThreadNum;
117 }
118 
InitMemInfo(MemInfoData::MemInfo & memInfo)119 void MemoryUtil::InitMemInfo(MemInfoData::MemInfo &memInfo)
120 {
121     memInfo.rss = 0;
122     memInfo.pss = 0;
123     memInfo.sharedClean = 0;
124     memInfo.sharedDirty = 0;
125     memInfo.privateClean = 0;
126     memInfo.privateDirty = 0;
127     memInfo.swap = 0;
128     memInfo.swapPss = 0;
129 }
130 
InitMemUsage(MemInfoData::MemUsage & usage)131 void MemoryUtil::InitMemUsage(MemInfoData::MemUsage &usage)
132 {
133     usage.vss = 0;
134     usage.rss = 0;
135     usage.uss = 0;
136     usage.pss = 0;
137     usage.gl = 0;
138     usage.graph = 0;
139     usage.pid = 0;
140 }
141 
InitGraphicsMemory(MemInfoData::GraphicsMemory & graphicsMemory)142 void MemoryUtil::InitGraphicsMemory(MemInfoData::GraphicsMemory &graphicsMemory)
143 {
144     graphicsMemory.gl = 0;
145     graphicsMemory.graph = 0;
146 }
147 
GetTypeAndValue(const string & str,string & type,uint64_t & value)148 bool MemoryUtil::GetTypeAndValue(const string &str, string &type, uint64_t &value)
149 {
150     string::size_type typePos = str.find(":");
151     if (typePos != str.npos) {
152         type = str.substr(0, typePos);
153         string valueStr = str.substr(typePos + 1);
154         const int base = 10;
155         value = strtoull(valueStr.c_str(), nullptr, base);
156         return true;
157     }
158     return false;
159 }
160 } // namespace HiviewDFX
161 } // namespace OHOS
162