1 /*
2  * Copyright (C) 2013 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 #ifndef _LIBMEMTRACK_MEMTRACK_H_
18 #define _LIBMEMTRACK_MEMTRACK_H_
19 
20 #include <sys/types.h>
21 #include <stddef.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /**
28  * struct memtrack_proc
29  *
30  * an opaque handle to the memory stats on a process.
31  * Created with memtrack_proc_new, destroyed by
32  * memtrack_proc_destroy.  Can be reused multiple times with
33  * memtrack_proc_get.
34  */
35 struct memtrack_proc;
36 
37 /**
38  * memtrack_proc_new
39  *
40  * Return a new handle to hold process memory stats.
41  *
42  * Returns NULL on error.
43  */
44 struct memtrack_proc *memtrack_proc_new(void);
45 
46 /**
47  * memtrack_proc_destroy
48  *
49  * Free all memory associated with a process memory stats handle.
50  */
51 void memtrack_proc_destroy(struct memtrack_proc *p);
52 
53 /**
54  * memtrack_proc_get
55  *
56  * Fill a process memory stats handle with data about the given pid.  Can be
57  * called on a handle that was just allocated with memtrack_proc_new,
58  * or on a handle that has been previously passed to memtrack_proc_get
59  * to replace the data with new data on the same or another process.  It is
60  * expected that the second call on the same handle should not require
61  * allocating any new memory.
62  *
63  * Returns 0 on success, -errno on error.
64  */
65 int memtrack_proc_get(struct memtrack_proc *p, pid_t pid);
66 
67 /**
68  * memtrack_proc_graphics_total
69  *
70  * Return total amount of memory that has been allocated for use as window
71  * buffers.  Does not differentiate between memory that has already been
72  * accounted for by reading /proc/pid/smaps and memory that has not been
73  * accounted for.
74  *
75  * Returns non-negative size in bytes on success, -errno on error.
76  */
77 ssize_t memtrack_proc_graphics_total(struct memtrack_proc *p);
78 
79 /**
80  * memtrack_proc_graphics_pss
81  *
82  * Return total amount of memory that has been allocated for use as window
83  * buffers, but has not already been accounted for by reading /proc/pid/smaps.
84  * Memory that is shared across processes may already be divided by the
85  * number of processes that share it (preferred), or may be charged in full to
86  * every process that shares it, depending on the capabilities of the driver.
87  *
88  * Returns non-negative size in bytes on success, -errno on error.
89  */
90 ssize_t memtrack_proc_graphics_pss(struct memtrack_proc *p);
91 
92 /**
93  * memtrack_proc_gl_total
94  *
95  * Same as memtrack_proc_graphics_total, but counts GL memory (which
96  * should not overlap with graphics memory) instead of graphics memory.
97  *
98  * Returns non-negative size in bytes on success, -errno on error.
99  */
100 ssize_t memtrack_proc_gl_total(struct memtrack_proc *p);
101 
102 /**
103  * memtrack_proc_gl_pss
104  *
105  * Same as memtrack_proc_graphics_total, but counts GL memory (which
106  * should not overlap with graphics memory) instead of graphics memory.
107  *
108  * Returns non-negative size in bytes on success, -errno on error.
109  */
110 ssize_t memtrack_proc_gl_pss(struct memtrack_proc *p);
111 
112 /**
113  * memtrack_proc_other_total
114  *
115  * Same as memtrack_proc_graphics_total, but counts miscellaneous memory
116  * not tracked by gl or graphics calls above.
117  *
118  * Returns non-negative size in bytes on success, -errno on error.
119  */
120 ssize_t memtrack_proc_other_total(struct memtrack_proc *p);
121 
122 /**
123  * memtrack_proc_other_pss
124  *
125  * Same as memtrack_proc_graphics_total, but counts miscellaneous memory
126  * not tracked by gl or graphics calls above.
127  *
128  * Returns non-negative size in bytes on success, -errno on error.
129  */
130 ssize_t memtrack_proc_other_pss(struct memtrack_proc *p);
131 
132 /**
133  * class DeviceInfo
134  *
135  * Contains the device id and name.
136  */
137 namespace aidl {
138 namespace android {
139 namespace hardware {
140 namespace memtrack {
141 
142 class DeviceInfo;
143 
144 }  // namespace memtrack
145 }  // namespace hardware
146 }  // namespace android
147 }  // namespace aidl
148 
149 /**
150  * memtrack_gpu_device_info
151  *
152  * Populates the @device_info vector with the  DeviceInfo for all GPU devices.
153  *
154  * Returns true on success and false otherwise.
155  */
156 bool memtrack_gpu_device_info(
157         std::vector<aidl::android::hardware::memtrack::DeviceInfo>* device_info);
158 
159 #ifdef __cplusplus
160 }
161 #endif
162 
163 #endif
164