1 /*
2 * Copyright (c) 2023 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
16 #include "meminfo.h"
17
18 #include <fstream>
19 #include <sstream>
20 #include <v1_0/imemory_tracker_interface.h>
21
22 #include "file_ex.h" // LoadStringFromFile
23 #include "hilog/log.h"
24
25 namespace OHOS {
26 namespace MemInfo {
27 using namespace OHOS::HDI::Memorytracker::V1_0;
28 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0xD001799, "MemInfo" };
29 constexpr int PAGE_TO_KB = 4;
30 constexpr int BYTE_PER_KB = 1024;
31
32 // get Rss from statm
GetRssByPid(const int pid)33 uint64_t GetRssByPid(const int pid)
34 {
35 uint64_t size = 0;
36 std::string statm;
37 std::string vss;
38 std::string rss;
39
40 std::string statmPath = "/proc/" + std::to_string(pid) + "/statm";
41 // format like:
42 // 640 472 369 38 0 115 0
43 if (!OHOS::LoadStringFromFile(statmPath, statm)) {
44 HiviewDFX::HiLog::Error(LABEL, "statm file error!");
45 return size;
46 }
47 std::istringstream isStatm(statm);
48 isStatm >> vss >> rss; // pages
49
50 size = static_cast<uint64_t>(atoi(rss.c_str()) * PAGE_TO_KB);
51 return size;
52 }
53
54 // get Pss from smaps_rollup
GetPssByPid(const int pid)55 uint64_t GetPssByPid(const int pid)
56 {
57 uint64_t size = 0;
58 std::string filename = "/proc/" + std::to_string(pid) + "/smaps_rollup";
59 std::ifstream in(filename);
60 if (!in) {
61 HiviewDFX::HiLog::Error(LABEL, "File %{public}s not found.\n", filename.c_str());
62 return size;
63 }
64
65 std::string content;
66 while (in.good() && getline(in, content)) {
67 std::string::size_type typePos = content.find(":");
68 if (typePos != content.npos) {
69 std::string type = content.substr(0, typePos);
70 if (type == "Pss") {
71 std::string valueStr = content.substr(typePos + 1);
72 const int base = 10;
73 size = strtoull(valueStr.c_str(), nullptr, base);
74 break;
75 }
76 }
77 }
78 in.close();
79 return size;
80 }
81
82 // get SwapPss from smaps_rollup
GetSwapPssByPid(const int pid)83 uint64_t GetSwapPssByPid(const int pid)
84 {
85 uint64_t size = 0;
86 std::string filename = "/proc/" + std::to_string(pid) + "/smaps_rollup";
87 std::ifstream in(filename);
88 if (!in) {
89 HiviewDFX::HiLog::Error(LABEL, "File %{public}s not found.\n", filename.c_str());
90 return size;
91 }
92
93 std::string content;
94 while (in.good() && getline(in, content)) {
95 std::string::size_type typePos = content.find(":");
96 if (typePos != content.npos) {
97 std::string type = content.substr(0, typePos);
98 if (type == "SwapPss") {
99 std::string valueStr = content.substr(typePos + 1);
100 const int base = 10;
101 size = strtoull(valueStr.c_str(), nullptr, base);
102 break;
103 }
104 }
105 }
106 in.close();
107 return size;
108 }
109
110 // get graphics memory from hdi
GetGraphicsMemory(const int pid,uint64_t & gl,uint64_t & graph)111 bool GetGraphicsMemory(const int pid, uint64_t &gl, uint64_t &graph)
112 {
113 bool ret = false;
114 sptr<IMemoryTrackerInterface> memtrack = IMemoryTrackerInterface::Get(true);
115 if (memtrack == nullptr) {
116 HiviewDFX::HiLog::Error(LABEL, "memtrack service is null");
117 return ret;
118 }
119 const std::vector<std::pair<MemoryTrackerType, std::string>> MEMORY_TRACKER_TYPES = {
120 {MEMORY_TRACKER_TYPE_GL, "GL"}, {MEMORY_TRACKER_TYPE_GRAPH, "Graph"},
121 {MEMORY_TRACKER_TYPE_OTHER, "Other"}
122 };
123
124 for (const auto &memTrackerType : MEMORY_TRACKER_TYPES) {
125 std::vector<MemoryRecord> records;
126 if (memtrack->GetDevMem(pid, memTrackerType.first, records) != HDF_SUCCESS) {
127 continue;
128 }
129 uint64_t value = 0;
130 for (const auto &record : records) {
131 if ((static_cast<uint32_t>(record.flags) & FLAG_UNMAPPED) == FLAG_UNMAPPED) {
132 value = static_cast<uint64_t>(record.size / BYTE_PER_KB);
133 break;
134 }
135 }
136 if (memTrackerType.first == MEMORY_TRACKER_TYPE_GL) {
137 gl = value;
138 ret = true;
139 } else if (memTrackerType.first == MEMORY_TRACKER_TYPE_GRAPH) {
140 graph = value;
141 ret = true;
142 }
143 }
144 return ret;
145 }
146 } /* namespace MemInfo */
147 } /* namespace OHOS */
148