xref: /aosp_15_r20/hardware/libhardware/include_all/hardware/memtrack.h (revision e01b6f769022e40d0923dee176e8dc7cd1d52984)
1*e01b6f76SAndroid Build Coastguard Worker /*
2*e01b6f76SAndroid Build Coastguard Worker  * Copyright (C) 2013 The Android Open Source Project
3*e01b6f76SAndroid Build Coastguard Worker  *
4*e01b6f76SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*e01b6f76SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*e01b6f76SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*e01b6f76SAndroid Build Coastguard Worker  *
8*e01b6f76SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*e01b6f76SAndroid Build Coastguard Worker  *
10*e01b6f76SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*e01b6f76SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*e01b6f76SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*e01b6f76SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*e01b6f76SAndroid Build Coastguard Worker  * limitations under the License.
15*e01b6f76SAndroid Build Coastguard Worker  */
16*e01b6f76SAndroid Build Coastguard Worker 
17*e01b6f76SAndroid Build Coastguard Worker #ifndef ANDROID_INCLUDE_HARDWARE_MEMTRACK_H
18*e01b6f76SAndroid Build Coastguard Worker #define ANDROID_INCLUDE_HARDWARE_MEMTRACK_H
19*e01b6f76SAndroid Build Coastguard Worker 
20*e01b6f76SAndroid Build Coastguard Worker #include <stdint.h>
21*e01b6f76SAndroid Build Coastguard Worker #include <sys/cdefs.h>
22*e01b6f76SAndroid Build Coastguard Worker #include <sys/types.h>
23*e01b6f76SAndroid Build Coastguard Worker 
24*e01b6f76SAndroid Build Coastguard Worker #include <hardware/hardware.h>
25*e01b6f76SAndroid Build Coastguard Worker 
26*e01b6f76SAndroid Build Coastguard Worker __BEGIN_DECLS
27*e01b6f76SAndroid Build Coastguard Worker 
28*e01b6f76SAndroid Build Coastguard Worker #define MEMTRACK_MODULE_API_VERSION_0_1  HARDWARE_MODULE_API_VERSION(0, 1)
29*e01b6f76SAndroid Build Coastguard Worker 
30*e01b6f76SAndroid Build Coastguard Worker /**
31*e01b6f76SAndroid Build Coastguard Worker  * The id of this module
32*e01b6f76SAndroid Build Coastguard Worker  */
33*e01b6f76SAndroid Build Coastguard Worker #define MEMTRACK_HARDWARE_MODULE_ID "memtrack"
34*e01b6f76SAndroid Build Coastguard Worker 
35*e01b6f76SAndroid Build Coastguard Worker /*
36*e01b6f76SAndroid Build Coastguard Worker  * The Memory Tracker HAL is designed to return information about device-specific
37*e01b6f76SAndroid Build Coastguard Worker  * memory usage.  The primary goal is to be able to track memory that is not
38*e01b6f76SAndroid Build Coastguard Worker  * trackable in any other way, for example texture memory that is allocated by
39*e01b6f76SAndroid Build Coastguard Worker  * a process, but not mapped in to that process' address space.
40*e01b6f76SAndroid Build Coastguard Worker  * A secondary goal is to be able to categorize memory used by a process into
41*e01b6f76SAndroid Build Coastguard Worker  * GL, graphics, etc.  All memory sizes should be in real memory usage,
42*e01b6f76SAndroid Build Coastguard Worker  * accounting for stride, bit depth, rounding up to page size, etc.
43*e01b6f76SAndroid Build Coastguard Worker  *
44*e01b6f76SAndroid Build Coastguard Worker  * A process collecting memory statistics will call getMemory for each
45*e01b6f76SAndroid Build Coastguard Worker  * combination of pid and memory type.  For each memory type that it recognizes
46*e01b6f76SAndroid Build Coastguard Worker  * the HAL should fill out an array of memtrack_record structures breaking
47*e01b6f76SAndroid Build Coastguard Worker  * down the statistics of that memory type as much as possible.  For example,
48*e01b6f76SAndroid Build Coastguard Worker  * getMemory(<pid>, MEMTRACK_TYPE_GL) might return:
49*e01b6f76SAndroid Build Coastguard Worker  * { { 4096,  ACCOUNTED | PRIVATE | SYSTEM },
50*e01b6f76SAndroid Build Coastguard Worker  *   { 40960, UNACCOUNTED | PRIVATE | SYSTEM },
51*e01b6f76SAndroid Build Coastguard Worker  *   { 8192,  ACCOUNTED | PRIVATE | DEDICATED },
52*e01b6f76SAndroid Build Coastguard Worker  *   { 8192,  UNACCOUNTED | PRIVATE | DEDICATED } }
53*e01b6f76SAndroid Build Coastguard Worker  * If the HAL could not differentiate between SYSTEM and DEDICATED memory, it
54*e01b6f76SAndroid Build Coastguard Worker  * could return:
55*e01b6f76SAndroid Build Coastguard Worker  * { { 12288,  ACCOUNTED | PRIVATE },
56*e01b6f76SAndroid Build Coastguard Worker  *   { 49152,  UNACCOUNTED | PRIVATE } }
57*e01b6f76SAndroid Build Coastguard Worker  *
58*e01b6f76SAndroid Build Coastguard Worker  * Memory should not overlap between types.  For example, a graphics buffer
59*e01b6f76SAndroid Build Coastguard Worker  * that has been mapped into the GPU as a surface should show up when
60*e01b6f76SAndroid Build Coastguard Worker  * MEMTRACK_TYPE_GRAPHICS is requested, and not when MEMTRACK_TYPE_GL
61*e01b6f76SAndroid Build Coastguard Worker  * is requested.
62*e01b6f76SAndroid Build Coastguard Worker  */
63*e01b6f76SAndroid Build Coastguard Worker 
64*e01b6f76SAndroid Build Coastguard Worker enum memtrack_type {
65*e01b6f76SAndroid Build Coastguard Worker     MEMTRACK_TYPE_OTHER = 0,
66*e01b6f76SAndroid Build Coastguard Worker     MEMTRACK_TYPE_GL = 1,
67*e01b6f76SAndroid Build Coastguard Worker     MEMTRACK_TYPE_GRAPHICS = 2,
68*e01b6f76SAndroid Build Coastguard Worker     MEMTRACK_TYPE_MULTIMEDIA = 3,
69*e01b6f76SAndroid Build Coastguard Worker     MEMTRACK_TYPE_CAMERA = 4,
70*e01b6f76SAndroid Build Coastguard Worker     MEMTRACK_NUM_TYPES,
71*e01b6f76SAndroid Build Coastguard Worker };
72*e01b6f76SAndroid Build Coastguard Worker 
73*e01b6f76SAndroid Build Coastguard Worker struct memtrack_record {
74*e01b6f76SAndroid Build Coastguard Worker     size_t size_in_bytes;
75*e01b6f76SAndroid Build Coastguard Worker     unsigned int flags;
76*e01b6f76SAndroid Build Coastguard Worker };
77*e01b6f76SAndroid Build Coastguard Worker 
78*e01b6f76SAndroid Build Coastguard Worker /**
79*e01b6f76SAndroid Build Coastguard Worker  * Flags to differentiate memory that can already be accounted for in
80*e01b6f76SAndroid Build Coastguard Worker  * /proc/<pid>/smaps,
81*e01b6f76SAndroid Build Coastguard Worker  * (Shared_Clean + Shared_Dirty + Private_Clean + Private_Dirty = Size).
82*e01b6f76SAndroid Build Coastguard Worker  * In general, memory mapped in to a userspace process is accounted unless
83*e01b6f76SAndroid Build Coastguard Worker  * it was mapped with remap_pfn_range.
84*e01b6f76SAndroid Build Coastguard Worker  * Exactly one of these should be set.
85*e01b6f76SAndroid Build Coastguard Worker  */
86*e01b6f76SAndroid Build Coastguard Worker #define MEMTRACK_FLAG_SMAPS_ACCOUNTED   (1 << 1)
87*e01b6f76SAndroid Build Coastguard Worker #define MEMTRACK_FLAG_SMAPS_UNACCOUNTED (1 << 2)
88*e01b6f76SAndroid Build Coastguard Worker 
89*e01b6f76SAndroid Build Coastguard Worker /**
90*e01b6f76SAndroid Build Coastguard Worker  * Flags to differentiate memory shared across multiple processes vs. memory
91*e01b6f76SAndroid Build Coastguard Worker  * used by a single process.  Only zero or one of these may be set in a record.
92*e01b6f76SAndroid Build Coastguard Worker  * If none are set, record is assumed to count shared + private memory.
93*e01b6f76SAndroid Build Coastguard Worker  */
94*e01b6f76SAndroid Build Coastguard Worker #define MEMTRACK_FLAG_SHARED      (1 << 3)
95*e01b6f76SAndroid Build Coastguard Worker #define MEMTRACK_FLAG_SHARED_PSS  (1 << 4) /* shared / num_procesess */
96*e01b6f76SAndroid Build Coastguard Worker #define MEMTRACK_FLAG_PRIVATE     (1 << 5)
97*e01b6f76SAndroid Build Coastguard Worker 
98*e01b6f76SAndroid Build Coastguard Worker /**
99*e01b6f76SAndroid Build Coastguard Worker  * Flags to differentiate memory taken from the kernel's allocation pool vs.
100*e01b6f76SAndroid Build Coastguard Worker  * memory that is dedicated to non-kernel allocations, for example a carveout
101*e01b6f76SAndroid Build Coastguard Worker  * or separate video memory.  Only zero or one of these may be set in a record.
102*e01b6f76SAndroid Build Coastguard Worker  * If none are set, record is assumed to count system + dedicated memory.
103*e01b6f76SAndroid Build Coastguard Worker  */
104*e01b6f76SAndroid Build Coastguard Worker #define MEMTRACK_FLAG_SYSTEM     (1 << 6)
105*e01b6f76SAndroid Build Coastguard Worker #define MEMTRACK_FLAG_DEDICATED  (1 << 7)
106*e01b6f76SAndroid Build Coastguard Worker 
107*e01b6f76SAndroid Build Coastguard Worker /**
108*e01b6f76SAndroid Build Coastguard Worker  * Flags to differentiate memory accessible by the CPU in non-secure mode vs.
109*e01b6f76SAndroid Build Coastguard Worker  * memory that is protected.  Only zero or one of these may be set in a record.
110*e01b6f76SAndroid Build Coastguard Worker  * If none are set, record is assumed to count secure + nonsecure memory.
111*e01b6f76SAndroid Build Coastguard Worker  */
112*e01b6f76SAndroid Build Coastguard Worker #define MEMTRACK_FLAG_NONSECURE  (1 << 8)
113*e01b6f76SAndroid Build Coastguard Worker #define MEMTRACK_FLAG_SECURE     (1 << 9)
114*e01b6f76SAndroid Build Coastguard Worker 
115*e01b6f76SAndroid Build Coastguard Worker /**
116*e01b6f76SAndroid Build Coastguard Worker  * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
117*e01b6f76SAndroid Build Coastguard Worker  * and the fields of this data structure must begin with hw_module_t
118*e01b6f76SAndroid Build Coastguard Worker  * followed by module specific information.
119*e01b6f76SAndroid Build Coastguard Worker  */
120*e01b6f76SAndroid Build Coastguard Worker typedef struct memtrack_module {
121*e01b6f76SAndroid Build Coastguard Worker     struct hw_module_t common;
122*e01b6f76SAndroid Build Coastguard Worker 
123*e01b6f76SAndroid Build Coastguard Worker     /**
124*e01b6f76SAndroid Build Coastguard Worker      * (*init)() performs memtrack management setup actions and is called
125*e01b6f76SAndroid Build Coastguard Worker      * once before any calls to getMemory().
126*e01b6f76SAndroid Build Coastguard Worker      * Returns 0 on success, -errno on error.
127*e01b6f76SAndroid Build Coastguard Worker      */
128*e01b6f76SAndroid Build Coastguard Worker     int (*init)(const struct memtrack_module *module);
129*e01b6f76SAndroid Build Coastguard Worker 
130*e01b6f76SAndroid Build Coastguard Worker     /**
131*e01b6f76SAndroid Build Coastguard Worker      * (*getMemory)() expects an array of record objects and populates up to
132*e01b6f76SAndroid Build Coastguard Worker      * *num_record structures with the sizes of memory plus associated flags for
133*e01b6f76SAndroid Build Coastguard Worker      * that memory.  It also updates *num_records with the total number of
134*e01b6f76SAndroid Build Coastguard Worker      * records it could return if *num_records was large enough when passed in.
135*e01b6f76SAndroid Build Coastguard Worker      * Returning records with size 0 is expected, the number of records should
136*e01b6f76SAndroid Build Coastguard Worker      * not vary between calls to getMemory for the same memory type, even
137*e01b6f76SAndroid Build Coastguard Worker      * for different pids.
138*e01b6f76SAndroid Build Coastguard Worker      *
139*e01b6f76SAndroid Build Coastguard Worker      * The caller will often call getMemory for a type and pid with
140*e01b6f76SAndroid Build Coastguard Worker      * *num_records == 0 to determine how many records to allocate room for,
141*e01b6f76SAndroid Build Coastguard Worker      * this case should be a fast-path in the HAL, returning a constant and
142*e01b6f76SAndroid Build Coastguard Worker      * not querying any kernel files.  If *num_records passed in is 0,
143*e01b6f76SAndroid Build Coastguard Worker      * then records may be NULL.
144*e01b6f76SAndroid Build Coastguard Worker      *
145*e01b6f76SAndroid Build Coastguard Worker      * This function must be thread-safe, it may get called from multiple
146*e01b6f76SAndroid Build Coastguard Worker      * threads at the same time.
147*e01b6f76SAndroid Build Coastguard Worker      *
148*e01b6f76SAndroid Build Coastguard Worker      * Returns 0 on success, -ENODEV if the type is not supported, -errno
149*e01b6f76SAndroid Build Coastguard Worker      * on other errors.
150*e01b6f76SAndroid Build Coastguard Worker      */
151*e01b6f76SAndroid Build Coastguard Worker     int (*getMemory)(const struct memtrack_module *module,
152*e01b6f76SAndroid Build Coastguard Worker                      pid_t pid,
153*e01b6f76SAndroid Build Coastguard Worker                      int type,
154*e01b6f76SAndroid Build Coastguard Worker                      struct memtrack_record *records,
155*e01b6f76SAndroid Build Coastguard Worker                      size_t *num_records);
156*e01b6f76SAndroid Build Coastguard Worker } memtrack_module_t;
157*e01b6f76SAndroid Build Coastguard Worker 
158*e01b6f76SAndroid Build Coastguard Worker __END_DECLS
159*e01b6f76SAndroid Build Coastguard Worker 
160*e01b6f76SAndroid Build Coastguard Worker #endif  // ANDROID_INCLUDE_HARDWARE_MEMTRACK_H
161