diff options
| author | Tobias Markus <tobias@miglix.eu> | 2017-01-30 10:48:00 +0100 |
|---|---|---|
| committer | Mark Lobodzinski <mark@lunarg.com> | 2017-01-31 10:27:31 -0700 |
| commit | 33778517e01b0f718fe0e60b1aa853e5e3b8d0e4 (patch) | |
| tree | 964e83a96ca35c3e924cdb29edde6bfe01ec665f | |
| parent | 4cfb4e2ad89d0a1c0d6e69de3b5460e35c1196b1 (diff) | |
| download | usermoji-33778517e01b0f718fe0e60b1aa853e5e3b8d0e4.tar.xz | |
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
| -rw-r--r-- | demos/vulkaninfo.c | 38 |
1 files changed, 37 insertions, 1 deletions
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 <mark@lunarg.com> * Author: Rene Lindsay <rene@lunarg.com> */ + +#ifdef __GNUC__ +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200809L +#endif +#else +#define strndup(p, n) strdup(p) +#endif + #include <assert.h> #include <inttypes.h> #include <stdbool.h> @@ -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"); |
