mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-21 10:09:59 -04:00
Log the memory by owner every 3600 frames, and how often released textures come back.
Every VMA allocation is counted under the file that made it until it is freed, so the new "mem: by owner" line splits the device-local memory the driver reports into textures (live, GPU-drawn, idle, released, released too lately to drop), pools and VRAM mirrors, upscalers, replay buffers, uniform snapshots, the device address table, the ASTC decoder, present and post-processing, headroom spares, the free space inside VMA's blocks and what VMA never allocated. "mem: host" gives the host-visible allocations, the process's commit and working set and the game's heap. "vk: released textures" counts take-backs by how long the texture was released and textures made again after a drop by a bound or a pressure, with the drop's reason. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// Video memory by the heaps' live budgets, for every allocation through VMA (vk.h maps vmaCreateImage, vmaCreateBuffer
|
||||
// and vmaAllocateMemory here).
|
||||
// and vmaAllocateMemory here, and the frees that go with them), and the allocations' sums by owner ("owners", below).
|
||||
//
|
||||
// An RX 580 (8 GiB, AMD 23.10) has a 6 GiB device-local heap and a 2 GiB device-local, host-visible one. The heap limits
|
||||
// of vk_device.c (vram_heap_limits) are fixed when the device is made: each heap's budget then, less a tenth for the
|
||||
@@ -19,6 +19,7 @@
|
||||
// headroom's anchors, vk_headroom.c) are made as asked.
|
||||
#define VK_VMA_BUDGET_IMPL
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "vk.h"
|
||||
#include "cfg.h"
|
||||
#include "host.h"
|
||||
@@ -83,42 +84,121 @@ static uint32_t all_bits(VmaAllocator vma, uint32_t bits) {
|
||||
return mp->memoryTypeCount >= 32 ? ~0u : (1u << mp->memoryTypeCount) - 1;
|
||||
}
|
||||
|
||||
// ---- owners
|
||||
// Every allocation made through the wrappers below is counted under the owner of the file that made it, the device-local
|
||||
// heaps apart from the others, and leaves the count when vk.h's vmaDestroyImage, vmaDestroyBuffer or
|
||||
// vmaFreeMemory frees it. The allocation's user data (unused otherwise) holds 1 + owner * 2 + local, so the free knows
|
||||
// where it was counted; one made before its owner was known, or by a file that does not include vk.h, has none and is
|
||||
// never counted. The memory report (vk_memory.c owner_log) reads the sums: a category that only climbs over a session
|
||||
// is where a leak shows. A count costs one VMA info query and two atomic adds per allocation and per free.
|
||||
static volatile int64_t own_bytes[VK_OWN_N][2];
|
||||
|
||||
static int owner_of(const char *file) {
|
||||
static const struct { const char *name; int owner; } map[] = {
|
||||
{"vk_texture.c", VK_OWN_TEXTURES}, {"vk_memory.c", VK_OWN_POOLS}, // mirrors, physical pools, staging
|
||||
{"vk_dlaa.c", VK_OWN_UPSCALER}, {"vk_fsr.c", VK_OWN_UPSCALER}, {"vk_dlssg.c", VK_OWN_UPSCALER},
|
||||
{"vk_exec_draw.c", VK_OWN_REPLAY}, {"vk_exec.c", VK_OWN_REPLAY}, {"vk_exec_xfer.c", VK_OWN_REPLAY},
|
||||
{"vk_mesh_fb.c", VK_OWN_REPLAY}, {"vk_usnap.c", VK_OWN_USNAP}, {"vk_gpa.c", VK_OWN_GPA},
|
||||
{"vk_astc.c", VK_OWN_ASTC}, {"gfx_present.c", VK_OWN_PRESENT}, {"vk_hdr.c", VK_OWN_PRESENT},
|
||||
{"vk_tonemap.c", VK_OWN_PRESENT}, {"vk_aa.c", VK_OWN_PRESENT}, {"vk_color.c", VK_OWN_PRESENT},
|
||||
{"vk_headroom.c", VK_OWN_HEADROOM},
|
||||
};
|
||||
if (!file) return VK_OWN_OTHER;
|
||||
const char *b = file;
|
||||
for (const char *p = file; *p; p++)
|
||||
if (*p == '/' || *p == '\\') b = p + 1;
|
||||
for (unsigned i = 0; i < sizeof map / sizeof *map; i++)
|
||||
if (!strcmp(b, map[i].name)) return map[i].owner;
|
||||
return VK_OWN_OTHER;
|
||||
}
|
||||
|
||||
static void own_add(VmaAllocator vma, VmaAllocation a, const char *file) {
|
||||
if (!a) return;
|
||||
VmaAllocationInfo i;
|
||||
vmaGetAllocationInfo(vma, a, &i);
|
||||
const VkPhysicalDeviceMemoryProperties *mp;
|
||||
vmaGetMemoryProperties(vma, &mp);
|
||||
int local = i.memoryType < mp->memoryTypeCount &&
|
||||
(mp->memoryHeaps[mp->memoryTypes[i.memoryType].heapIndex].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT);
|
||||
int owner = owner_of(file);
|
||||
vmaSetAllocationUserData(vma, a, (void *)(uintptr_t)(1 + owner * 2 + local));
|
||||
__atomic_add_fetch(&own_bytes[owner][local], (int64_t)i.size, __ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
static void own_sub(VmaAllocator vma, VmaAllocation a) {
|
||||
if (!a) return;
|
||||
VmaAllocationInfo i;
|
||||
vmaGetAllocationInfo(vma, a, &i);
|
||||
uintptr_t tag = (uintptr_t)i.pUserData;
|
||||
if (!tag || tag > VK_OWN_N * 2) return;
|
||||
__atomic_sub_fetch(&own_bytes[(tag - 1) / 2][(tag - 1) & 1], (int64_t)i.size, __ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
uint64_t vk_vma_owner_bytes(int owner, int local) {
|
||||
if ((unsigned)owner >= VK_OWN_N) return 0;
|
||||
int64_t v = __atomic_load_n(&own_bytes[owner][local != 0], __ATOMIC_RELAXED);
|
||||
return v > 0 ? (uint64_t)v : 0;
|
||||
}
|
||||
|
||||
void vk_vma_destroy_image(VmaAllocator vma, VkImage img, VmaAllocation a) {
|
||||
own_sub(vma, a);
|
||||
vmaDestroyImage(vma, img, a);
|
||||
}
|
||||
|
||||
void vk_vma_destroy_buffer(VmaAllocator vma, VkBuffer buf, VmaAllocation a) {
|
||||
own_sub(vma, a);
|
||||
vmaDestroyBuffer(vma, buf, a);
|
||||
}
|
||||
|
||||
void vk_vma_free_memory(VmaAllocator vma, VmaAllocation a) {
|
||||
own_sub(vma, a);
|
||||
vmaFreeMemory(vma, a);
|
||||
}
|
||||
|
||||
static VkResult made(VkResult r, VmaAllocator vma, VmaAllocation *a, const char *file) {
|
||||
if (r == VK_SUCCESS) own_add(vma, *a, file);
|
||||
return r;
|
||||
}
|
||||
|
||||
VkResult vk_vma_create_image(VmaAllocator vma, const VkImageCreateInfo *ici, const VmaAllocationCreateInfo *ai, VkImage *img,
|
||||
VmaAllocation *a, VmaAllocationInfo *info) {
|
||||
VmaAllocation *a, VmaAllocationInfo *info, const char *file) {
|
||||
if (!ai->pool) {
|
||||
uint32_t bits = all_bits(vma, ai->memoryTypeBits), nb = budget_bits(vma, bits);
|
||||
if (nb != bits) {
|
||||
VmaAllocationCreateInfo b = *ai;
|
||||
b.memoryTypeBits = nb;
|
||||
if (vmaCreateImage(vma, ici, &b, img, a, info) == VK_SUCCESS) return note_moved(), VK_SUCCESS;
|
||||
if (vmaCreateImage(vma, ici, &b, img, a, info) == VK_SUCCESS)
|
||||
return note_moved(), made(VK_SUCCESS, vma, a, file);
|
||||
}
|
||||
}
|
||||
return vmaCreateImage(vma, ici, ai, img, a, info);
|
||||
return made(vmaCreateImage(vma, ici, ai, img, a, info), vma, a, file);
|
||||
}
|
||||
|
||||
VkResult vk_vma_create_buffer(VmaAllocator vma, const VkBufferCreateInfo *bci, const VmaAllocationCreateInfo *ai,
|
||||
VkBuffer *buf, VmaAllocation *a, VmaAllocationInfo *info) {
|
||||
VkBuffer *buf, VmaAllocation *a, VmaAllocationInfo *info, const char *file) {
|
||||
if (!ai->pool) {
|
||||
uint32_t bits = all_bits(vma, ai->memoryTypeBits), nb = budget_bits(vma, bits);
|
||||
if (nb != bits) {
|
||||
VmaAllocationCreateInfo b = *ai;
|
||||
b.memoryTypeBits = nb;
|
||||
if (vmaCreateBuffer(vma, bci, &b, buf, a, info) == VK_SUCCESS) return note_moved(), VK_SUCCESS;
|
||||
if (vmaCreateBuffer(vma, bci, &b, buf, a, info) == VK_SUCCESS)
|
||||
return note_moved(), made(VK_SUCCESS, vma, a, file);
|
||||
}
|
||||
}
|
||||
return vmaCreateBuffer(vma, bci, ai, buf, a, info);
|
||||
return made(vmaCreateBuffer(vma, bci, ai, buf, a, info), vma, a, file);
|
||||
}
|
||||
|
||||
VkResult vk_vma_allocate_memory(VmaAllocator vma, const VkMemoryRequirements *req, const VmaAllocationCreateInfo *ai,
|
||||
VmaAllocation *a, VmaAllocationInfo *info) {
|
||||
VmaAllocation *a, VmaAllocationInfo *info, const char *file) {
|
||||
uint32_t bits = req->memoryTypeBits & all_bits(vma, ai->memoryTypeBits);
|
||||
if (!ai->pool && (bits & (bits - 1))) { // more than one type: one alone is asked for on purpose
|
||||
uint32_t nb = budget_bits(vma, bits);
|
||||
if (nb != bits) {
|
||||
VkMemoryRequirements r = *req;
|
||||
r.memoryTypeBits = nb;
|
||||
if (vmaAllocateMemory(vma, &r, ai, a, info) == VK_SUCCESS) return note_moved(), VK_SUCCESS;
|
||||
if (vmaAllocateMemory(vma, &r, ai, a, info) == VK_SUCCESS)
|
||||
return note_moved(), made(VK_SUCCESS, vma, a, file);
|
||||
}
|
||||
}
|
||||
return vmaAllocateMemory(vma, req, ai, a, info);
|
||||
return made(vmaAllocateMemory(vma, req, ai, a, info), vma, a, file);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user