display mem formatted

This commit is contained in:
Freya Murphy 2024-02-01 15:53:11 -05:00
parent 341c95f535
commit ef384ad9a7
Signed by: freya
GPG key ID: 744AB800E383AE52
4 changed files with 90 additions and 4 deletions

View file

@ -59,6 +59,6 @@ run: all
-cdrom $(BUILD_DIR)/$(ISO_NAME) \
-serial stdio \
-display gtk,show-menubar=off,zoom-to-fit=on \
-m 4G \
-m 1G \
-enable-kvm \
-name corn

View file

@ -156,6 +156,14 @@ char *ultoa(unsigned long int n, char *buffer, int radix);
*/
char *ulltoa(unsigned long long int n, char *buffer, int radix);
/**
* Converts a byte count to a human readable file size
* @param bytes - the bytes to convert
* @param buf - the buffer to store it in
* @preturns - bus
*/
char *btoa(size_t bytes, char *buf);
/**
* Converts the string in str to an int value based on the given base.
* The endptr is updated to where the string was no longer valid.

View file

@ -128,6 +128,83 @@ int ctoi(char c) {
}
}
static char fs_endings[] = {'K', 'M', 'G', 'T', 'P'};
static size_t nth_pow(size_t i, size_t e) {
size_t a = i;
while(--e)
a *= i;
return a;
}
static char nth_digit(size_t bytes, size_t place) {
while (1) {
if (bytes < nth_pow(10, place))
break;
bytes /= 10;
}
return bytes %= 10;
}
char *btoa(const size_t bytes, char *buf) {
char *start = buf;
int idx = -1;
size_t b = bytes;
size_t s = b;
while (1) {
if (b < 1000)
break;
s = b;
b /= 1024;
idx++;
}
if (idx == -1) {
ultoa(bytes, buf, 10);
return start;
}
s = s * 1000 / 1024;
if (s < 1024) {
b = 1024;
idx--;
}
char fd = nth_digit(s, 1);
char sd = nth_digit(s, 2);
char td = nth_digit(s, 3);
*buf = fd + '0';
buf++;
if (b < 10) {
*buf = '.';
buf++;
}
*buf = sd + '0';
buf++;
if (b < 10)
goto end;
if (b > 99) {
*buf = td + '0';
buf++;
}
end:
*buf = fs_endings[idx];
buf++;
*buf = '\0';
return start;
}
#define UXTOA(type, name) \
char *name(unsigned type n, char *buffer, int radix) { \
if (n == 0) { \

View file

@ -218,10 +218,11 @@ void memory_init(struct memory_map *map) {
page_count -= bitmap_pages;
free_memory = page_count * PAGE_SIZE;
char buf[20];
kprintf("\nMEMORY USAGE\n");
kprintf("mem total: %ld\n", memory_total());
kprintf("mem free: %ld\n", memory_free());
kprintf("mem used: %ld\n\n", memory_used());
kprintf("mem total: %s\n", btoa(memory_total(), buf));
kprintf("mem free: %s\n", btoa(memory_free(), buf));
kprintf("mem used: %s\n\n", btoa(memory_used(), buf));
memory_unlock();