Files
my-os-project2/user/pctl/ls.c

75 lines
2.2 KiB
C

#include <stddef.h>
#include <stdint.h>
#include <system/system.h>
#include <sysdefs/processctl.h>
#include <util/util.h>
#include <errors.h>
#include <uprintf.h>
#include <string/string.h>
#include <dlmalloc/malloc.h>
const char *human_size(uint64_t bytes, char *buf, size_t bufsize) {
static const char *units[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB" };
int unit = 0;
// Scale down until value fits nicely
uint64_t rem = 0;
while (bytes >= 1024 && unit < (int)(sizeof(units)/sizeof(units[0])) - 1) {
rem = bytes % 1024;
bytes /= 1024;
unit++;
}
if (unit == 0) {
// Just bytes
usnprintf(buf, bufsize, "%llu %s", (unsigned long long)bytes, units[unit]);
} else {
// Show one decimal place without using floats
// Multiply remainder by 10 to get first decimal digit
uint64_t frac = (rem * 10 + 512) / 1024; // rounded
if (frac == 10) { // handle carry, e.g. 1023.9 -> 1024.0
bytes++;
frac = 0;
}
if (frac > 0) usnprintf(buf, bufsize, "%llu.%llu %s", (unsigned long long)bytes, (unsigned long long)frac, units[unit]);
else usnprintf(buf, bufsize, "%llu %s", (unsigned long long)bytes, units[unit]);
}
return buf;
}
void pctl_ls(void) {
static const char *states[] = {"embryo", "ready", "zombie", "waiting", "died"};
uint64_t procslen = processctl(-1, PCTL_PLS_SZ, 0, 0, 0);
char *namebuf = dlmalloc(34);
char *membuf = dlmalloc(20);
uprintf("%-30s %s %-6s %-15s %-8s\n", "NAME", "PID", "TYPE", "MEMORY", "STATE");
for (size_t i = 0; i < procslen; i++) {
ProcStat stat = ZERO(&stat);
string_memset(namebuf, 0, 34);
string_memset(membuf, 0, 20);
int32_t r = processctl(-1, PCTL_PLS_STAT, i, (uint64_t)&stat, 0);
if (r == E_OK) {
string_memcpy(namebuf, stat.name, 30);
namebuf[31] = namebuf[32] = namebuf[33] = '.';
// Too big format string causes a stack overflow. This is an issue with printf ;(
uprintf("%-30s %-3lu %-6s %-15s ",
namebuf,
stat.pid,
stat.kern ? "KERNEL" : "USER",
human_size(stat.usemem, membuf, 20)
);
uprintf("%-8s", states[stat.state]);
uprintf("\n");
}
}
}