• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "include/RAM.h"
16 #include <sstream>
17 #include <fstream>
18 #include <climits>
19 #include <cstdio>
20 #include <algorithm>
21 #include <iostream>
22 #include <thread>
23 #include <string>
24 #include <regex>
25 #include "include/sp_utils.h"
26 #include "memory_collector.h"
27 #include "collect_result.h"
28 #include "include/startup_delay.h"
29 
30 using namespace OHOS::HiviewDFX;
31 using namespace OHOS::HiviewDFX::UCollectUtil;
32 using namespace OHOS::HiviewDFX::UCollect;
33 
34 namespace OHOS {
35 namespace SmartPerf {
ItemData()36 std::map<std::string, std::string> RAM::ItemData()
37 {
38     std::map<std::string, std::string> result;
39     std::map<std::string, std::string> sysRamInfo = RAM::GetSysRamInfo();
40     for (auto it = sysRamInfo.begin(); it != sysRamInfo.end(); ++it) {
41         result.insert(*it);
42     }
43     std::map<std::string, std::string> procRamInfo = RAM::GetRamInfo();
44     for (auto it = procRamInfo.begin(); it != procRamInfo.end(); ++it) {
45         result.insert(*it);
46     }
47     return result;
48 }
49 
SetPackageName(std::string pName)50 void RAM::SetPackageName(std::string pName)
51 {
52     packageName = pName;
53 }
54 
GetRamInfo() const55 std::map<std::string, std::string> RAM::GetRamInfo() const
56 {
57     std::string processId = "";
58     OHOS::SmartPerf::StartUpDelay sp;
59     processId = sp.GetPidByPkg(packageName);
60     std::map<std::string, std::string> procRamInfo;
61     std::string pssValue = "";
62     if (processId.size() == 0) {
63         return procRamInfo;
64     }
65     std::string cmd = "hidumper --mem "+ processId;
66     FILE *fd = popen(cmd.c_str(), "r");
67     if (fd == nullptr) {
68         return procRamInfo;
69     }
70     const int paramEleven = 11;
71     char buf[1024] = {'\0'};
72     while ((fgets(buf, sizeof(buf), fd)) != nullptr) {
73         std::string line = buf;
74         if (line[0] == '-') {
75             continue;
76         }
77         std::vector<std::string> params;
78         SPUtils::StrSplit(line, " ", params);
79         if (params.size() == paramEleven && params[0].find("Total") != std::string::npos) {
80             pssValue = params[1];
81         }
82         if (pssValue.size() > 0) {
83             break;
84         }
85     }
86     pclose(fd);
87     if (pssValue.size() > 0) {
88         procRamInfo["pss"] = pssValue;
89     }
90     return procRamInfo;
91 }
92 
GetSysRamInfo() const93 std::map<std::string, std::string> RAM::GetSysRamInfo() const
94 {
95     std::map<std::string, std::string> sysRamInfo;
96     std::shared_ptr<MemoryCollector> collector = MemoryCollector::Create();
97     CollectResult<SysMemory> result = collector->CollectSysMemory();
98     sysRamInfo["memTotal"] = std::to_string(result.data.memTotal);
99     sysRamInfo["memFree"] = std::to_string(result.data.memFree);
100     sysRamInfo["memAvailable"] = std::to_string(result.data.memAvailable);
101     return sysRamInfo;
102 }
103 }
104 }
105