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