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