blob: ff10e96ea5f8a94d0600eb6d2197f45dba779169 [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()) {
Sandeep Patilf31c7092019-01-30 17:43:22 -0800126 if (buf->name() == "" || buf->name() == "<unknown>") buf->SetName(name);
127 if (buf->exporter() == "" || buf->exporter() == "<unknown>") buf->SetExporter(exporter);
128 if (buf->count() == 0) buf->SetCount(count);
Erick Reyes302a2592019-01-28 19:53:30 -0800129 buf->AddFdRef(pid);
130 return true;
131 }
132
Sandeep Patilf31c7092019-01-30 17:43:22 -0800133 DmaBuffer& db = dmabufs->emplace_back(sb.st_ino, sb.st_blocks * 512, count, exporter, name);
Erick Reyes302a2592019-01-28 19:53:30 -0800134 db.AddFdRef(pid);
Erick Reyesd2918fe2019-01-24 15:26:23 -0800135 }
136
137 return true;
138}
139
140static bool ReadDmaBufMapRefs(pid_t pid, std::vector<DmaBuffer>* dmabufs) {
141 std::string mapspath = ::android::base::StringPrintf("/proc/%d/maps", pid);
142 auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(mapspath.c_str(), "re"), fclose};
143 if (fp == nullptr) {
144 LOG(ERROR) << "Failed to open maps for pid: " << pid;
145 return false;
146 }
147
148 char* line = nullptr;
149 size_t len = 0;
150
151 // Process the map if it is dmabuf. Add map reference to existing object in 'dmabufs'
152 // if it was already found. If it wasn't create a new one and append it to 'dmabufs'
153 auto account_dmabuf = [&](uint64_t start, uint64_t end, uint16_t /* flags */,
Sandeep Patilf31c7092019-01-30 17:43:22 -0800154 uint64_t /* pgoff */, ino_t inode, const char* name) {
Erick Reyesd2918fe2019-01-24 15:26:23 -0800155 // no need to look into this mapping if it is not dmabuf
156 if (!FileIsDmaBuf(std::string(name))) {
157 return;
158 }
159
Erick Reyesd2918fe2019-01-24 15:26:23 -0800160 auto buf = std::find_if(dmabufs->begin(), dmabufs->end(),
161 [&inode](const DmaBuffer& dbuf) { return dbuf.inode() == inode; });
162 if (buf != dmabufs->end()) {
163 buf->AddMapRef(pid);
164 return;
165 }
166
167 // We have a new buffer, but unknown count and name
168 DmaBuffer& dbuf = dmabufs->emplace_back(inode, end - start, 0, "<unknown>", "<unknown>");
169 dbuf.AddMapRef(pid);
170 };
171
172 while (getline(&line, &len, fp.get()) > 0) {
173 if (!::android::procinfo::ReadMapFileContent(line, account_dmabuf)) {
174 LOG(ERROR) << "Failed t parse maps for pid: " << pid;
175 return false;
176 }
177 }
178
179 free(line);
180 return true;
181}
182
183// Public methods
184bool ReadDmaBufInfo(std::vector<DmaBuffer>* dmabufs, const std::string& path) {
185 auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose};
186 if (fp == nullptr) {
187 LOG(ERROR) << "Failed to open dmabuf info from debugfs";
188 return false;
189 }
190
191 char* line = nullptr;
192 size_t len = 0;
193 dmabufs->clear();
194 while (getline(&line, &len, fp.get()) > 0) {
195 // The new dmabuf bufinfo format adds inode number and a name at the end
196 // We are looking for lines as follows:
197 // size flags mode count exp_name ino name
198 // 01048576 00000002 00000007 00000001 ion 00018758 CAMERA
199 // 01048576 00000002 00000007 00000001 ion 00018758
200 uint64_t size, count;
201 char* exporter_name = nullptr;
202 ino_t inode;
203 char* name = nullptr;
204 int matched = sscanf(line, "%" SCNu64 "%*x %*x %" SCNu64 " %ms %lu %ms", &size, &count,
205 &exporter_name, &inode, &name);
206 if (matched < 4) {
207 continue;
208 }
209 dmabufs->emplace_back(inode, size, count, exporter_name, matched > 4 ? name : "");
210 free(exporter_name);
211 free(name);
212 }
213
214 free(line);
215
216 return true;
217}
218
219bool ReadDmaBufInfo(pid_t pid, std::vector<DmaBuffer>* dmabufs) {
220 dmabufs->clear();
Erick Reyes302a2592019-01-28 19:53:30 -0800221 return AppendDmaBufInfo(pid, dmabufs);
222}
223
224bool AppendDmaBufInfo(pid_t pid, std::vector<DmaBuffer>* dmabufs) {
Erick Reyesd2918fe2019-01-24 15:26:23 -0800225 if (!ReadDmaBufFdRefs(pid, dmabufs)) {
226 LOG(ERROR) << "Failed to read dmabuf fd references";
227 return false;
228 }
229
230 if (!ReadDmaBufMapRefs(pid, dmabufs)) {
231 LOG(ERROR) << "Failed to read dmabuf map references";
232 return false;
233 }
234 return true;
235}
236
237} // namespace dmabufinfo
238} // namespace android