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