Skip to content

Commit 55110a7

Browse files
committed
Add more memory debugging for addr_placed and external mem emulation
1 parent c8ab9d2 commit 55110a7

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

src/vulkan/wrapper/wrapper_debug.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,7 @@ bool use_wrapper_trace() {
366366

367367
return value = false;
368368
}
369+
370+
bool should_log_memory_debug() {
371+
return CHECK_FLAG("DEBUG_MEMORY");
372+
}

src/vulkan/wrapper/wrapper_debug.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ bool use_image_view_mode(void);
2828

2929
bool use_compute_shader_mode(void);
3030

31-
bool use_wrapper_trace(void);
31+
bool use_wrapper_trace(void);
32+
33+
bool should_log_memory_debug(void);

src/vulkan/wrapper/wrapper_device_memory.c

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,21 @@ WRAPPER_AllocateMemory(VkDevice _device,
303303
struct wrapper_device_memory *mem;
304304
VkResult result;
305305

306+
bool debug = should_log_memory_debug();
307+
308+
if (debug) {
309+
WLOGD("WRAPPER_AllocateMemory, pAllocateInfo:");
310+
LOG_STRUCT(VkMemoryAllocateInfo, pAllocateInfo);
311+
}
312+
306313
VkMemoryPropertyFlags property_flags =
307314
device->physical->memory_properties.memoryTypes[
308315
pAllocateInfo->memoryTypeIndex].propertyFlags;
309316

310-
if (!(property_flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT))
317+
if (!(property_flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)) {
318+
if (debug) WLOGD("Memory type %d does not support VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT", pAllocateInfo->memoryTypeIndex);
311319
goto fallback;
320+
}
312321

313322
if (!device->vk.enabled_features.memoryMapPlaced ||
314323
!device->vk.enabled_extensions.EXT_map_memory_placed)
@@ -323,6 +332,7 @@ WRAPPER_AllocateMemory(VkDevice _device,
323332
if (vk_find_struct_const(pAllocateInfo, EXPORT_MEMORY_ALLOCATE_INFO))
324333
goto fallback;
325334

335+
if (debug) WLOGD("Emulating AllocateMemory");
326336
simple_mtx_lock(&device->resource_mutex);
327337

328338
result = wrapper_device_memory_create(device, pAllocator, &mem);
@@ -331,17 +341,22 @@ WRAPPER_AllocateMemory(VkDevice _device,
331341
goto out;
332342
}
333343

344+
if (debug) WLOGD("Trying dmabuf");
334345
result = wrapper_allocate_memory_dmabuf(device, pAllocateInfo,
335346
pAllocator, &mem->dispatch_handle, &mem->dmabuf_fd);
336347

337348
if (result != VK_SUCCESS) {
338349
wrapper_device_memory_reset(mem);
350+
351+
if (debug) WLOGD("Trying dmaheap");
339352
result = wrapper_allocate_memory_dmaheap(device,
340353
pAllocateInfo, pAllocator, &mem->dispatch_handle, &mem->dmabuf_fd);
341354
}
342355

343356
if (result != VK_SUCCESS) {
344357
wrapper_device_memory_reset(mem);
358+
359+
if (debug) WLOGD("Trying ahb");
345360
result = wrapper_allocate_memory_ahardware_buffer(device,
346361
pAllocateInfo, pAllocator, &mem->dispatch_handle, &mem->ahardware_buffer);
347362
}
@@ -351,14 +366,18 @@ WRAPPER_AllocateMemory(VkDevice _device,
351366
vk_error(device, result);
352367
} else {
353368
*pMemory = mem->dispatch_handle;
369+
if (debug) WLOGD("AllocateMemory out VkDeviceMemory: %p", *pMemory);
354370
}
355371

356372
out:
357373
simple_mtx_unlock(&mem->device->resource_mutex);
374+
if (debug) WLOGD("vkAllocateMemory returned %d", result);
358375
return result;
359376

360377
fallback:
378+
if (debug) WLOGD("Dispatching to vkAllocateMemory (not emulating AllocateMemory)");
361379
result = CHECK(AllocateMemory(_device, pAllocateInfo, pAllocator, pMemory));
380+
if (debug) WLOGD("vkAllocateMemory returned %d", result);
362381
return result;
363382
}
364383

@@ -384,31 +403,44 @@ WRAPPER_MapMemory2KHR(VkDevice _device,
384403
void** ppData)
385404
{
386405
VK_FROM_HANDLE(wrapper_device, device, _device);
406+
bool debug = should_log_memory_debug();
387407
const VkMemoryMapPlacedInfoEXT *placed_info = NULL;
388408
struct wrapper_device_memory *mem;
389409
int fd;
390410

391-
if (pMemoryMapInfo->flags & VK_MEMORY_MAP_PLACED_BIT_EXT)
411+
if (pMemoryMapInfo->flags & VK_MEMORY_MAP_PLACED_BIT_EXT) {
392412
placed_info = vk_find_struct_const(pMemoryMapInfo->pNext,
393413
MEMORY_MAP_PLACED_INFO_EXT);
414+
if (debug) {
415+
WLOGD("Using VK_MEMORY_MAP_PLACED_BIT_EXT:")
416+
LOG_STRUCT(VkMemoryMapPlacedInfoEXT, placed_info);
417+
}
418+
}
394419

395420
mem = wrapper_device_memory_from_handle(device, pMemoryMapInfo->memory);
396-
if (!placed_info || !mem)
421+
if (!placed_info || !mem) {
422+
if (debug) WLOGD("Not emulating MapMemory2KHR");
397423
return CHECK(MapMemory(_device,
398424
pMemoryMapInfo->memory, pMemoryMapInfo->offset, pMemoryMapInfo->size,
399425
0, ppData));
426+
}
400427

401428
if (mem->map_address) {
429+
if (debug) WLOGD("mem %p has already been mapped to %p", pMemoryMapInfo->memory, mem->map_address);
402430
if (placed_info->pPlacedAddress != mem->map_address) {
431+
WLOGE("mem %p has already been mapped to %p, but is requested to be remapped to %p, an invalid operation", mem, mem->map_address, placed_info->pPlacedAddress);
403432
return VK_ERROR_MEMORY_MAP_FAILED;
404433
} else {
405434
*ppData = (char *)mem->map_address
406435
+ pMemoryMapInfo->offset;
436+
if (debug) WLOGD("mem %p successfully mapped to %p", pMemoryMapInfo->memory, *ppData);
407437
return VK_SUCCESS;
408438
}
409439
}
410440
assert(mem->dmabuf_fd >= 0 || mem->ahardware_buffer != NULL);
411441

442+
if (debug) WLOGD("Creating a memory map for mem %p (ahb=%p)", pMemoryMapInfo->memory, mem->ahardware_buffer);
443+
412444
if (mem->ahardware_buffer) {
413445
const native_handle_t *handle;
414446
const int *handle_fds;
@@ -429,25 +461,30 @@ WRAPPER_MapMemory2KHR(VkDevice _device,
429461
fd = mem->dmabuf_fd;
430462
}
431463

464+
if (debug) WLOGD("mem %p associated with fd %d", pMemoryMapInfo->memory, fd);
465+
432466
if (pMemoryMapInfo->size == VK_WHOLE_SIZE)
433467
mem->map_size = mem->alloc_size > 0 ?
434468
mem->alloc_size : lseek(fd, 0, SEEK_END);
435469
else
436470
mem->map_size = pMemoryMapInfo->size;
437471

472+
if (debug) WLOGD("Mmapping mem %p with fd %d to %p", pMemoryMapInfo->memory, fd, placed_info->pPlacedAddress);
438473
mem->map_address = mmap(placed_info->pPlacedAddress,
439474
mem->map_size, PROT_READ | PROT_WRITE,
440475
MAP_SHARED | MAP_FIXED, fd, 0);
441476

442477
if (mem->map_address == MAP_FAILED) {
443478
mem->map_address = NULL;
444479
mem->map_size = 0;
445-
fprintf(stderr, "%s: mmap failed\n", __func__);
480+
WLOGE("mmap failed emulating MapMemory2KHR");
446481
return vk_error(device, VK_ERROR_MEMORY_MAP_FAILED);
447482
}
448483

449484
*ppData = (char *)mem->map_address + pMemoryMapInfo->offset;
450485

486+
if (debug) WLOGD("mem %p successfully mapped to %p", pMemoryMapInfo->memory, *ppData);
487+
451488
return VK_SUCCESS;
452489
}
453490

@@ -460,19 +497,21 @@ WRAPPER_UnmapMemory2KHR(VkDevice _device,
460497
{
461498
VK_FROM_HANDLE(wrapper_device, device, _device);
462499
struct wrapper_device_memory *mem;
500+
bool debug = should_log_memory_debug();
463501

464502
mem = wrapper_device_memory_from_handle(device, pMemoryUnmapInfo->memory);
465503
if (!mem) {
466-
device->dispatch_table.UnmapMemory(device->dispatch_handle,
467-
pMemoryUnmapInfo->memory);
504+
if (debug) WLOGD("Unmapping mem %p without emulation", pMemoryUnmapInfo->memory);
505+
CHECKV(UnmapMemory(_device, pMemoryUnmapInfo->memory));
468506
return VK_SUCCESS;
469507
}
470508

509+
if (debug) WLOGD("Unmapping mem %p (mapped at %p)", pMemoryUnmapInfo->memory, mem->map_address);
471510
if (pMemoryUnmapInfo->flags & VK_MEMORY_UNMAP_RESERVE_BIT_EXT) {
472511
mem->map_address = mmap(mem->map_address, mem->map_size,
473512
PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
474513
if (mem->map_address == MAP_FAILED) {
475-
fprintf(stderr, "Failed to replace mapping with reserved memory");
514+
WLOGE("Failed to replace mapping with reserved memory");
476515
return vk_error(device, VK_ERROR_MEMORY_MAP_FAILED);
477516
}
478517
} else {

0 commit comments

Comments
 (0)