Add size pretty-printing

This commit is contained in:
Mark
2019-09-30 10:39:38 +03:00
parent 6f35ab11a5
commit a71a024348
5 changed files with 42 additions and 9 deletions
+2
View File
@@ -23,6 +23,8 @@
#define kfatal(f, ...) debugf(DEBUG_FATAL, "[%s] " f, __func__, ##__VA_ARGS__)
#define kprint(l, f, ...) debugf(l, "[%s] " f, __func__, ##__VA_ARGS__)
void fmtsiz(char *buf, size_t sz);
void debugc(int level, char c);
void debugs(int level, const char *s);
+1 -1
View File
@@ -118,6 +118,6 @@ void amd64_heap_dump(const heap_t *heap) {
block; block = block->next) {
assert((block->magic & HEAP_MAGIC) == HEAP_MAGIC, "Corrupted heap block magic\n");
kdebug("%p: %u %s%s\n", block, block->size, (block->magic & 1 ? "USED" : "FREE"), (block->next ? " -> " : ""));
kdebug("%p: %S %s%s\n", block, block->size, (block->magic & 1 ? "USED" : "FREE"), (block->next ? " -> " : ""));
}
}
+3 -3
View File
@@ -41,9 +41,9 @@ void amd64_mm_init(void) {
// Allocate some pages for kernel heap (base size: 16MiB)
uintptr_t heap_base_phys = amd64_phys_alloc_contiguous(KERNEL_HEAP >> 12);
// TODO: pretty-print sizes
assert(heap_base_phys != MM_NADDR, "Could not allocate %uKiB of memory for kernel heap\n", KERNEL_HEAP >> 10);
kdebug("Setting up kernel heap of %uKiB @ %p\n", KERNEL_HEAP >> 10, heap_base_phys);
assert(heap_base_phys != MM_NADDR, "Could not allocate %S of memory for kernel heap\n", KERNEL_HEAP);
kdebug("Setting up kernel heap of %S @ %p\n", KERNEL_HEAP, heap_base_phys);
amd64_heap_init(heap_global, heap_base_phys, KERNEL_HEAP);
amd64_heap_dump(heap_global);
}
+3 -5
View File
@@ -65,7 +65,7 @@ void amd64_phys_free(uintptr_t page) {
// XXX: very slow impl.
uintptr_t amd64_phys_alloc_contiguous(size_t count) {
uintptr_t addr = 0;
kdebug("Requested %luKiB\n", count << 2);
kdebug("Requested %S\n", count << 12);
if (count >= (PHYS_MAX_INDEX << 6)) {
// The requested range is too large
@@ -115,8 +115,7 @@ void amd64_phys_memory_map(const multiboot_memory_map_t *mmap, size_t length) {
uintptr_t page_aligned_end = (entry->addr + entry->len) & ~0xFFF;
if (entry->type == 1 && page_aligned_end > page_aligned_begin) {
// TODO: size pretty-printing
kdebug("+++ %uKiB @ %p\n", (page_aligned_end - page_aligned_begin) / 1024, page_aligned_begin);
kdebug("+++ %S @ %p\n", page_aligned_end - page_aligned_begin, page_aligned_begin);
for (uintptr_t addr = page_aligned_begin - PHYS_ALLOWED_BEGIN;
addr < (page_aligned_end - PHYS_ALLOWED_BEGIN); addr += 0x1000) {
@@ -133,6 +132,5 @@ void amd64_phys_memory_map(const multiboot_memory_map_t *mmap, size_t length) {
curr_item += entry->size + sizeof(uint32_t);
}
// TODO: size pretty-printing
kdebug("%uKiB available\n", total_phys << 2);
kdebug("%S available\n", total_phys << 12);
}
+33
View File
@@ -10,6 +10,34 @@
static const char *s_debug_xs_set0 = "0123456789abcdef";
static const char *s_debug_xs_set1 = "0123456789ABCDEF";
void fmtsiz(char *out, size_t sz) {
static const char sizs[] = "KMGTPE???";
size_t f = sz, r = 0;
int pwr = 0;
size_t l = 0;
while (f >= 1536) {
r = ((f % 1024) * 10) / 1024;
f /= 1024;
++pwr;
}
debug_ds(f, out, 0, 0);
l = strlen(out);
if (pwr) {
out[l++] = '.';
out[l++] = '0' + r;
out[l++] = sizs[pwr - 1];
out[l++] = 'i';
}
out[l++] = 'B';
out[l++] = 0;
}
// TODO: make debugc a __weak function of a character
void debugc(int level, char c) {
#if defined(ARCH_AMD64)
@@ -233,6 +261,11 @@ void debugfv(int level, const char *fmt, va_list args) {
debug_xs(value.v_ptr, buf, s_debug_xs_set0);
debugspl(level, buf, '0', sizeof(uintptr_t) * 2);
break;
case 'S':
value.v_ptr = va_arg(args, uintptr_t);
fmtsiz(buf, value.v_ptr);
debugsp(level, buf, padc, padn);
break;
case 's':
value.v_string = va_arg(args, const char *);
debugsp(level, value.v_string ? value.v_string : "(null)", padc, padn);