xref: /aosp_15_r20/external/angle/doc/GPUMemoryAnalysis.md (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker# GPU Memory Reporting and Analysis
2*8975f5c5SAndroid Build Coastguard Worker
3*8975f5c5SAndroid Build Coastguard Worker[MemRptExt]: https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VK_EXT_device_memory_report.html
4*8975f5c5SAndroid Build Coastguard Worker[enabling-general-logging]: DebuggingTips.md#enabling-general-logging
5*8975f5c5SAndroid Build Coastguard Worker
6*8975f5c5SAndroid Build Coastguard WorkerGPU memory usage data can be reported when using the Vulkan back-end with drivers that support the
7*8975f5c5SAndroid Build Coastguard Worker[VK_EXT_device_memory_report][MemRptExt] extension.  When enabled, ANGLE will produce log messages
8*8975f5c5SAndroid Build Coastguard Workerbased on every allocation, free, import, unimport, and failed allocation of GPU memory.  This
9*8975f5c5SAndroid Build Coastguard Workerfunctionality requires [enabling general logging](#enabling-general-logging) as well as enabling
10*8975f5c5SAndroid Build Coastguard Workerone or two feature flags.
11*8975f5c5SAndroid Build Coastguard Worker
12*8975f5c5SAndroid Build Coastguard Worker## GPU Memory Reporting
13*8975f5c5SAndroid Build Coastguard Worker
14*8975f5c5SAndroid Build Coastguard WorkerANGLE registers a callback function with the Vulkan driver for the
15*8975f5c5SAndroid Build Coastguard Worker[VK_EXT_device_memory_report][MemRptExt] extension.  The Vulkan driver calls this callback for
16*8975f5c5SAndroid Build Coastguard Workereach of the following GPU memory events:
17*8975f5c5SAndroid Build Coastguard Worker
18*8975f5c5SAndroid Build Coastguard Worker- Allocation of GPU memory by ANGLE
19*8975f5c5SAndroid Build Coastguard Worker- Free of GPU memory by ANGLE
20*8975f5c5SAndroid Build Coastguard Worker- Import of GPU memory provided by another process (e.g. Android SurfaceFlinger)
21*8975f5c5SAndroid Build Coastguard Worker- Unimport of GPU memory provided by another process
22*8975f5c5SAndroid Build Coastguard Worker- Failed allocation
23*8975f5c5SAndroid Build Coastguard Worker
24*8975f5c5SAndroid Build Coastguard WorkerThe callback provides additional information about each event such as the size, the VkObjectType,
25*8975f5c5SAndroid Build Coastguard Workerand the address (see the extension documentation for more details).  ANGLE caches this information,
26*8975f5c5SAndroid Build Coastguard Workerand logs messages based on this information.  ANGLE keeps track of how much of each type of memory
27*8975f5c5SAndroid Build Coastguard Workeris allocated and imported.  For example, if a GLES command causes ANGLE five 4 KB descriptor set
28*8975f5c5SAndroid Build Coastguard Worker(VK_OBJECT_TYPE_DESCRIPTOR_SET) allocations, ANGLE will add 20 KB to the total of allocated
29*8975f5c5SAndroid Build Coastguard Workerdescriptor set memory.
30*8975f5c5SAndroid Build Coastguard Worker
31*8975f5c5SAndroid Build Coastguard WorkerANGLE supports two types of memory reporting, both of which are enabled
32*8975f5c5SAndroid Build Coastguard Workervia feature flags:
33*8975f5c5SAndroid Build Coastguard Worker
34*8975f5c5SAndroid Build Coastguard Worker* `logMemoryReportStats` provides summary statistics at each eglSwapBuffers() command
35*8975f5c5SAndroid Build Coastguard Worker* `logMemoryReportCallbacks` provides per-callback information at the time of the callback
36*8975f5c5SAndroid Build Coastguard Worker
37*8975f5c5SAndroid Build Coastguard WorkerBoth feature flags can be enabled at the same time.  A simple way to enable either or both of these
38*8975f5c5SAndroid Build Coastguard Workerfeature flags on Android is with with the following command:
39*8975f5c5SAndroid Build Coastguard Worker```
40*8975f5c5SAndroid Build Coastguard Workeradb shell setprop debug.angle.feature_overrides_enabled <feature>[:<feature>]
41*8975f5c5SAndroid Build Coastguard Worker```
42*8975f5c5SAndroid Build Coastguard Workerwhere `<feature>` is either `logMemoryReportStats` or `logMemoryReportCallbacks`.  Both can be
43*8975f5c5SAndroid Build Coastguard Workerenabled by putting a colon between them, such as the following:
44*8975f5c5SAndroid Build Coastguard Worker```
45*8975f5c5SAndroid Build Coastguard Workeradb shell setprop debug.angle.feature_overrides_enabled logMemoryReportCallbacks:logMemoryReportStats
46*8975f5c5SAndroid Build Coastguard Worker```
47*8975f5c5SAndroid Build Coastguard Worker
48*8975f5c5SAndroid Build Coastguard WorkerAnother way to enable either or both of these feature flags is by editing the `vk_renderer.cpp` file,
49*8975f5c5SAndroid Build Coastguard Workerand changing `false` in the following lines to `true`:
50*8975f5c5SAndroid Build Coastguard Worker```
51*8975f5c5SAndroid Build Coastguard Worker    ANGLE_FEATURE_CONDITION(&mFeatures, logMemoryReportCallbacks, false);
52*8975f5c5SAndroid Build Coastguard Worker    ANGLE_FEATURE_CONDITION(&mFeatures, logMemoryReportStats, false);
53*8975f5c5SAndroid Build Coastguard Worker```
54*8975f5c5SAndroid Build Coastguard Worker
55*8975f5c5SAndroid Build Coastguard WorkerNote: At this time, GPU memory reporting has only been tested and used on Android, where the logged
56*8975f5c5SAndroid Build Coastguard Workerinformation can be viewed with the `adb logcat` command.
57*8975f5c5SAndroid Build Coastguard Worker
58*8975f5c5SAndroid Build Coastguard Worker## GPU Memory Analysis
59*8975f5c5SAndroid Build Coastguard Worker
60*8975f5c5SAndroid Build Coastguard WorkerGPU memory reporting can be combined with other forms of debugging in order to do analysis.  For
61*8975f5c5SAndroid Build Coastguard Workerexample, for a GLES application/test that properly shuts down, the total size of each type of
62*8975f5c5SAndroid Build Coastguard Workerallocated and imported memory should be zero bytes at the end of the application/test.  If not, a
63*8975f5c5SAndroid Build Coastguard Workermemory leak exists, and the log can be used to determine where the leak occurs.
64*8975f5c5SAndroid Build Coastguard Worker
65*8975f5c5SAndroid Build Coastguard WorkerIf an application seems to be using too much GPU memory, enabling memory reporting can reveal which
66*8975f5c5SAndroid Build Coastguard Workertype of memory is being excessively used.
67*8975f5c5SAndroid Build Coastguard Worker
68*8975f5c5SAndroid Build Coastguard WorkerComplex forms of analysis can be done by enabling logging of every GLES and EGL API command.  This
69*8975f5c5SAndroid Build Coastguard Workercan be enabled at compilation time by [enabling general logging](#enabling-general-logging) as well
70*8975f5c5SAndroid Build Coastguard Workeras setting the following GN arg:
71*8975f5c5SAndroid Build Coastguard Worker```
72*8975f5c5SAndroid Build Coastguard Workerangle_enable_trace_android_logcat = true
73*8975f5c5SAndroid Build Coastguard Worker```
74*8975f5c5SAndroid Build Coastguard Worker
75*8975f5c5SAndroid Build Coastguard WorkerCombining that with enabling the `logMemoryReportCallbacks` feature flag will allow each memory
76*8975f5c5SAndroid Build Coastguard Workerallocation and import to be correlated with the GLES/EGL commands that caused it.  If more context
77*8975f5c5SAndroid Build Coastguard Workeris needed for the type of drawing and/or setup that is being done in a sea of GLES commands, this
78*8975f5c5SAndroid Build Coastguard Workercan also be combined with the use of a graphics debugger such as Android GPU Inspector (AGI) or
79*8975f5c5SAndroid Build Coastguard WorkerRenderDoc.  The debugger can help you understand what the application is doing at the time of the
80*8975f5c5SAndroid Build Coastguard Workerparticular GPU memory event is occuring.  For example, you might determine that the application is
81*8975f5c5SAndroid Build Coastguard Workerdoing something to cause a memory leak; or you may get insight into what the game is doing that
82*8975f5c5SAndroid Build Coastguard Workercontributes to ANGLE using excessive amounts of GPU memory.
83