From 33778517e01b0f718fe0e60b1aa853e5e3b8d0e4 Mon Sep 17 00:00:00 2001 From: Tobias Markus Date: Mon, 30 Jan 2017 10:48:00 +0100 Subject: vulkaninfo: Print human-readable heap sizes Since the size of a memory heap can be rather substantial, it is helpful to have the size additionally printed out in a human-readable format, i.e. using prefixes. Change-Id: I47aad4fce06471804ce9e853ba300626b6ad34c8 --- demos/vulkaninfo.c | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/demos/vulkaninfo.c b/demos/vulkaninfo.c index be81c6fd..3e7e133e 100644 --- a/demos/vulkaninfo.c +++ b/demos/vulkaninfo.c @@ -20,6 +20,15 @@ * Author: Mark Lobodzinski * Author: Rene Lindsay */ + +#ifdef __GNUC__ +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200809L +#endif +#else +#define strndup(p, n) strdup(p) +#endif + #include #include #include @@ -47,6 +56,7 @@ #ifdef _WIN32 #define snprintf _snprintf +#define strdup _strdup // Returns nonzero if the console is used only for this process. Will return // zero if another process (such as cmd.exe) is also attached. @@ -1312,6 +1322,29 @@ static void AppGpuDumpQueueProps(const struct AppGpu *gpu, uint32_t id) { fflush(stdout); } +// This prints a number of bytes in a human-readable format according to prefixes of the International System of Quantities (ISQ), +// defined in ISO/IEC 80000. The prefixes used here are not SI prefixes, but rather the binary prefixes based on powers of 1024 +// (kibi-, mebi-, gibi- etc.). +#define kBufferSize 32 + +static char *HumanReadable(const size_t sz) { + const char prefixes[] = "KMGTPEZY"; + char buf[kBufferSize]; + int which = -1; + double result = (double)sz; + while (result > 1024 && which < 7) { + result /= 1024; + ++which; + } + + char unit[] = "\0i"; + if (which >= 0) { + unit[0] = prefixes[which]; + } + snprintf(buf, kBufferSize, "%.2f %sB", result, unit); + return strndup(buf, kBufferSize); +} + static void AppGpuDumpMemoryProps(const struct AppGpu *gpu) { const VkPhysicalDeviceMemoryProperties *props = &gpu->memory_props; @@ -1339,7 +1372,10 @@ static void AppGpuDumpMemoryProps(const struct AppGpu *gpu) { for (uint32_t i = 0; i < props->memoryHeapCount; i++) { printf("\tmemoryHeaps[%u] : \n", i); const VkDeviceSize memSize = props->memoryHeaps[i].size; - printf("\t\tsize = " PRINTF_SIZE_T_SPECIFIER " (0x%" PRIxLEAST64 ")\n", (size_t)memSize, memSize); + char *mem_size_human_readable = HumanReadable((const size_t)memSize); + printf("\t\tsize = " PRINTF_SIZE_T_SPECIFIER " (0x%" PRIxLEAST64 ") (%s)\n", (size_t)memSize, memSize, + mem_size_human_readable); + free(mem_size_human_readable); VkMemoryHeapFlags heap_flags = props->memoryHeaps[i].flags; printf("\t\tflags: \n\t\t\t"); -- cgit v1.2.3