1 /*
2  * Copyright (C) 2021 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 #pragma once
18 
19 #include <unordered_map>
20 #include <vector>
21 
22 namespace android {
23 namespace dmabufinfo {
24 
25 /*
26  * struct DmabufInfo: Represents information about a DMA-BUF.
27  *
28  * @inode: The unique inode number for the buffer.
29  * @exp_name: Name of the exporter of the buffer.
30  * @size: Size of the buffer.
31  */
32 struct DmabufInfo {
33     unsigned int inode;
34     std::string exp_name;
35     unsigned int size;
36 };
37 
38 struct DmabufTotal {
39     uint64_t size;
40     unsigned int buffer_count;
41 };
42 
43 class DmabufSysfsStats {
44   public:
buffer_stats()45     inline const std::vector<DmabufInfo>& buffer_stats() const { return buffer_stats_; }
exporter_info()46     inline const std::unordered_map<std::string, struct DmabufTotal>& exporter_info() const {
47         return exporter_info_;
48     }
total_size()49     inline uint64_t total_size() const { return total_.size; }
total_count()50     inline unsigned int total_count() const { return total_.buffer_count; }
51 
52     friend bool GetDmabufSysfsStats(DmabufSysfsStats* stats, const std::string& path);
53 
54   private:
55     std::vector<DmabufInfo> buffer_stats_;
56     std::unordered_map<std::string, struct DmabufTotal> exporter_info_;
57     struct DmabufTotal total_;
58 };
59 
60 /*
61  * Reads and parses DMA-BUF statistics from sysfs to create per-buffer
62  * and per-exporter stats.
63  *
64  * @stats: output argument that will be populated with information from DMA-BUF sysfs stats.
65  * @path: Not for use by clients, to be used only for unit testing.
66  *
67  * Returns true on success.
68  */
69 bool GetDmabufSysfsStats(DmabufSysfsStats* stats,
70                          const std::string& path = "/sys/kernel/dmabuf/buffers");
71 
72 /*
73  * Calculates the total size of all DMA-BUFs exported. It does not read or parse
74  * the rest of the DMA-BUF sysfs statistics inorder to be performant.
75  *
76  * @path: Not for use by clients, to be used only for unit testing.
77  *
78  * Returns total size of all DMA-BUFs exported in units of KB.
79  */
80 bool GetDmabufTotalExportedKb(uint64_t* total_exported,
81                               const std::string& path = "/sys/kernel/dmabuf/buffers");
82 
83 /* Reads the exporter name of the DMA buffer with @inode */
84 bool ReadBufferExporter(unsigned int inode, std::string* exporter,
85                         const std::string& dmabuf_sysfs_path = "/sys/kernel/dmabuf/buffers");
86 
87 }  // namespace dmabufinfo
88 }  // namespace android
89