blob: 41ecc5195ad83f581d6063555f8686a63ba30a13 [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
122 DmaBuffer& buf =
123 dmabufs->emplace_back(sb.st_ino, sb.st_blocks * 512, count, exporter, name);
124 buf.AddFdRef(pid);
125 }
126
127 return true;
128}
129
130static bool ReadDmaBufMapRefs(pid_t pid, std::vector<DmaBuffer>* dmabufs) {
131 std::string mapspath = ::android::base::StringPrintf("/proc/%d/maps", pid);
132 auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(mapspath.c_str(), "re"), fclose};
133 if (fp == nullptr) {
134 LOG(ERROR) << "Failed to open maps for pid: " << pid;
135 return false;
136 }
137
138 char* line = nullptr;
139 size_t len = 0;
140
141 // Process the map if it is dmabuf. Add map reference to existing object in 'dmabufs'
142 // if it was already found. If it wasn't create a new one and append it to 'dmabufs'
143 auto account_dmabuf = [&](uint64_t start, uint64_t end, uint16_t /* flags */,
144 uint64_t /* pgoff */, const char* name) {
145 // no need to look into this mapping if it is not dmabuf
146 if (!FileIsDmaBuf(std::string(name))) {
147 return;
148 }
149
150 // TODO (b/123532375) : Add inode number to the callback of ReadMapFileContent.
151 //
152 // Workaround: we know 'name' points to the name at the end of 'line'.
153 // We use that to backtrack and pick up the inode number from the line as well.
154 // start end flag pgoff mj:mn inode name
155 // 00400000-00409000 r-xp 00000000 00:00 426998 /dmabuf (deleted)
156 const char* p = name;
157 p--;
158 // skip spaces
159 while (p != line && *p == ' ') {
160 p--;
161 }
162 // walk backwards to the beginning of inode number
163 while (p != line && isdigit(*p)) {
164 p--;
165 }
166 uint64_t inode = strtoull(p, nullptr, 10);
167 auto buf = std::find_if(dmabufs->begin(), dmabufs->end(),
168 [&inode](const DmaBuffer& dbuf) { return dbuf.inode() == inode; });
169 if (buf != dmabufs->end()) {
170 buf->AddMapRef(pid);
171 return;
172 }
173
174 // We have a new buffer, but unknown count and name
175 DmaBuffer& dbuf = dmabufs->emplace_back(inode, end - start, 0, "<unknown>", "<unknown>");
176 dbuf.AddMapRef(pid);
177 };
178
179 while (getline(&line, &len, fp.get()) > 0) {
180 if (!::android::procinfo::ReadMapFileContent(line, account_dmabuf)) {
181 LOG(ERROR) << "Failed t parse maps for pid: " << pid;
182 return false;
183 }
184 }
185
186 free(line);
187 return true;
188}
189
190// Public methods
191bool ReadDmaBufInfo(std::vector<DmaBuffer>* dmabufs, const std::string& path) {
192 auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose};
193 if (fp == nullptr) {
194 LOG(ERROR) << "Failed to open dmabuf info from debugfs";
195 return false;
196 }
197
198 char* line = nullptr;
199 size_t len = 0;
200 dmabufs->clear();
201 while (getline(&line, &len, fp.get()) > 0) {
202 // The new dmabuf bufinfo format adds inode number and a name at the end
203 // We are looking for lines as follows:
204 // size flags mode count exp_name ino name
205 // 01048576 00000002 00000007 00000001 ion 00018758 CAMERA
206 // 01048576 00000002 00000007 00000001 ion 00018758
207 uint64_t size, count;
208 char* exporter_name = nullptr;
209 ino_t inode;
210 char* name = nullptr;
211 int matched = sscanf(line, "%" SCNu64 "%*x %*x %" SCNu64 " %ms %lu %ms", &size, &count,
212 &exporter_name, &inode, &name);
213 if (matched < 4) {
214 continue;
215 }
216 dmabufs->emplace_back(inode, size, count, exporter_name, matched > 4 ? name : "");
217 free(exporter_name);
218 free(name);
219 }
220
221 free(line);
222
223 return true;
224}
225
226bool ReadDmaBufInfo(pid_t pid, std::vector<DmaBuffer>* dmabufs) {
227 dmabufs->clear();
228 if (!ReadDmaBufFdRefs(pid, dmabufs)) {
229 LOG(ERROR) << "Failed to read dmabuf fd references";
230 return false;
231 }
232
233 if (!ReadDmaBufMapRefs(pid, dmabufs)) {
234 LOG(ERROR) << "Failed to read dmabuf map references";
235 return false;
236 }
237 return true;
238}
239
240} // namespace dmabufinfo
241} // namespace android