blob: b4ad6678a44c411abebcb79fa5b07df3e7e901cf [file] [log] [blame]
Erick Reyesd2918fe2019-01-24 15:26:23 -08001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <dmabufinfo/dmabufinfo.h>
18
19#include <inttypes.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24#include <unistd.h>
25
26#include <filesystem>
27#include <memory>
28#include <string>
29#include <vector>
30
31#include <android-base/file.h>
32#include <android-base/logging.h>
33#include <android-base/parseint.h>
34#include <android-base/stringprintf.h>
35#include <android-base/strings.h>
36#include <procinfo/process_map.h>
37
38namespace android {
39namespace dmabufinfo {
40
41static bool FileIsDmaBuf(const std::string& path) {
42 return ::android::base::StartsWith(path, "/dmabuf");
43}
44
45static bool ReadDmaBufFdInfo(pid_t pid, int fd, std::string* name, std::string* exporter,
46 uint64_t* count) {
47 std::string fdinfo = ::android::base::StringPrintf("/proc/%d/fdinfo/%d", pid, fd);
48 auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(fdinfo.c_str(), "re"), fclose};
49 if (fp == nullptr) {
50 LOG(ERROR) << "Failed to open dmabuf info from debugfs";
51 return false;
52 }
53
54 char* line = nullptr;
55 size_t len = 0;
56 while (getline(&line, &len, fp.get()) > 0) {
57 switch (line[0]) {
58 case 'c':
59 if (strncmp(line, "count:", 6) == 0) {
60 char* c = line + 6;
61 *count = strtoull(c, nullptr, 10);
62 }
63 break;
64 case 'e':
65 if (strncmp(line, "exp_name:", 9) == 0) {
66 char* c = line + 9;
67 *exporter = ::android::base::Trim(c);
68 }
69 break;
70 case 'n':
71 if (strncmp(line, "name:", 5) == 0) {
72 char* c = line + 5;
73 *name = ::android::base::Trim(std::string(c));
74 }
75 break;
76 }
77 }
78
79 free(line);
80 return true;
81}
82
83static bool ReadDmaBufFdRefs(pid_t pid, std::vector<DmaBuffer>* dmabufs) {
84 std::string fdpath = ::android::base::StringPrintf("/proc/%d/fd", pid);
85 for (auto& de : std::filesystem::directory_iterator(fdpath)) {
86 if (!std::filesystem::is_symlink(de.path())) {
87 continue;
88 }
89
90 std::string target;
91 if (!::android::base::Readlink(de.path().string(), &target)) {
92 LOG(ERROR) << "Failed to find target for symlink: " << de.path().string();
93 return false;
94 }
95
96 if (!FileIsDmaBuf(target)) {
97 continue;
98 }
99
100 int fd;
101 if (!::android::base::ParseInt(de.path().filename().string(), &fd)) {
102 LOG(ERROR) << "Dmabuf fd: " << de.path().string() << " is invalid";
103 return false;
104 }
105
106 // Set defaults in case the kernel doesn't give us the information
107 // we need in fdinfo
108 std::string name = "<unknown>";
109 std::string exporter = "<unknown>";
110 uint64_t count = 0;
111 if (!ReadDmaBufFdInfo(pid, fd, &name, &exporter, &count)) {
112 LOG(ERROR) << "Failed to read fdinfo for: " << de.path().string();
113 return false;
114 }
115
116 struct stat sb;
117 if (stat(de.path().c_str(), &sb) < 0) {
118 PLOG(ERROR) << "Failed to stat: " << de.path().string();
119 return false;
120 }
121
Erick Reyes302a2592019-01-28 19:53:30 -0800122 uint64_t inode = sb.st_ino;
123 auto buf = std::find_if(dmabufs->begin(), dmabufs->end(),
124 [&inode](const DmaBuffer& dbuf) { return dbuf.inode() == inode; });
125 if (buf != dmabufs->end()) {
126 if (buf->name() == "" || buf->name() == "<unknown>")
127 buf->SetName(name);
128 if (buf->exporter() == "" || buf->exporter() == "<unknown>")
129 buf->SetExporter(exporter);
130 if (buf->count() == 0)
131 buf->SetCount(count);
132 buf->AddFdRef(pid);
133 return true;
134 }
135
136 DmaBuffer& db =
Erick Reyesd2918fe2019-01-24 15:26:23 -0800137 dmabufs->emplace_back(sb.st_ino, sb.st_blocks * 512, count, exporter, name);
Erick Reyes302a2592019-01-28 19:53:30 -0800138 db.AddFdRef(pid);
Erick Reyesd2918fe2019-01-24 15:26:23 -0800139 }
140
141 return true;
142}
143
144static bool ReadDmaBufMapRefs(pid_t pid, std::vector<DmaBuffer>* dmabufs) {
145 std::string mapspath = ::android::base::StringPrintf("/proc/%d/maps", pid);
146 auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(mapspath.c_str(), "re"), fclose};
147 if (fp == nullptr) {
148 LOG(ERROR) << "Failed to open maps for pid: " << pid;
149 return false;
150 }
151
152 char* line = nullptr;
153 size_t len = 0;
154
155 // Process the map if it is dmabuf. Add map reference to existing object in 'dmabufs'
156 // if it was already found. If it wasn't create a new one and append it to 'dmabufs'
157 auto account_dmabuf = [&](uint64_t start, uint64_t end, uint16_t /* flags */,
158 uint64_t /* pgoff */, const char* name) {
159 // no need to look into this mapping if it is not dmabuf
160 if (!FileIsDmaBuf(std::string(name))) {
161 return;
162 }
163
164 // TODO (b/123532375) : Add inode number to the callback of ReadMapFileContent.
165 //
166 // Workaround: we know 'name' points to the name at the end of 'line'.
167 // We use that to backtrack and pick up the inode number from the line as well.
168 // start end flag pgoff mj:mn inode name
169 // 00400000-00409000 r-xp 00000000 00:00 426998 /dmabuf (deleted)
170 const char* p = name;
171 p--;
172 // skip spaces
173 while (p != line && *p == ' ') {
174 p--;
175 }
176 // walk backwards to the beginning of inode number
177 while (p != line && isdigit(*p)) {
178 p--;
179 }
180 uint64_t inode = strtoull(p, nullptr, 10);
181 auto buf = std::find_if(dmabufs->begin(), dmabufs->end(),
182 [&inode](const DmaBuffer& dbuf) { return dbuf.inode() == inode; });
183 if (buf != dmabufs->end()) {
184 buf->AddMapRef(pid);
185 return;
186 }
187
188 // We have a new buffer, but unknown count and name
189 DmaBuffer& dbuf = dmabufs->emplace_back(inode, end - start, 0, "<unknown>", "<unknown>");
190 dbuf.AddMapRef(pid);
191 };
192
193 while (getline(&line, &len, fp.get()) > 0) {
194 if (!::android::procinfo::ReadMapFileContent(line, account_dmabuf)) {
195 LOG(ERROR) << "Failed t parse maps for pid: " << pid;
196 return false;
197 }
198 }
199
200 free(line);
201 return true;
202}
203
204// Public methods
205bool ReadDmaBufInfo(std::vector<DmaBuffer>* dmabufs, const std::string& path) {
206 auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose};
207 if (fp == nullptr) {
208 LOG(ERROR) << "Failed to open dmabuf info from debugfs";
209 return false;
210 }
211
212 char* line = nullptr;
213 size_t len = 0;
214 dmabufs->clear();
215 while (getline(&line, &len, fp.get()) > 0) {
216 // The new dmabuf bufinfo format adds inode number and a name at the end
217 // We are looking for lines as follows:
218 // size flags mode count exp_name ino name
219 // 01048576 00000002 00000007 00000001 ion 00018758 CAMERA
220 // 01048576 00000002 00000007 00000001 ion 00018758
221 uint64_t size, count;
222 char* exporter_name = nullptr;
223 ino_t inode;
224 char* name = nullptr;
225 int matched = sscanf(line, "%" SCNu64 "%*x %*x %" SCNu64 " %ms %lu %ms", &size, &count,
226 &exporter_name, &inode, &name);
227 if (matched < 4) {
228 continue;
229 }
230 dmabufs->emplace_back(inode, size, count, exporter_name, matched > 4 ? name : "");
231 free(exporter_name);
232 free(name);
233 }
234
235 free(line);
236
237 return true;
238}
239
240bool ReadDmaBufInfo(pid_t pid, std::vector<DmaBuffer>* dmabufs) {
241 dmabufs->clear();
Erick Reyes302a2592019-01-28 19:53:30 -0800242 return AppendDmaBufInfo(pid, dmabufs);
243}
244
245bool AppendDmaBufInfo(pid_t pid, std::vector<DmaBuffer>* dmabufs) {
Erick Reyesd2918fe2019-01-24 15:26:23 -0800246 if (!ReadDmaBufFdRefs(pid, dmabufs)) {
247 LOG(ERROR) << "Failed to read dmabuf fd references";
248 return false;
249 }
250
251 if (!ReadDmaBufMapRefs(pid, dmabufs)) {
252 LOG(ERROR) << "Failed to read dmabuf map references";
253 return false;
254 }
255 return true;
256}
257
258} // namespace dmabufinfo
259} // namespace android