100 lines
2.7 KiB
C
100 lines
2.7 KiB
C
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <ulib.h>
|
|
#include "ls.h"
|
|
#include "macros.h"
|
|
|
|
struct {
|
|
char *specificproc;
|
|
int32_t pid;
|
|
} PCTL_LS_CONFIG = {
|
|
.specificproc = NULL,
|
|
.pid = -1,
|
|
};
|
|
|
|
static Arg ARGS[] = {
|
|
ARG("-proc", ARG_STRING, &PCTL_LS_CONFIG.specificproc),
|
|
ARG("-pid", ARG_INT, &PCTL_LS_CONFIG.pid),
|
|
ARG_END(),
|
|
};
|
|
|
|
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) {
|
|
int32_t ret;
|
|
if ((ret = parse_args(SUBCMD_ARGS(), SUBCMD_ARGSLEN(), ARGS)) < 0) {
|
|
uprintf("pctl ls: Could not parse args: %d\n", ret);
|
|
return;
|
|
}
|
|
|
|
static const char *states[] = {"embryo", "ready", "zombie", "waiting", "died"};
|
|
|
|
uint64_t procslen = processctl(-1, PCTL_PLS_SZ, 0, 0, 0);
|
|
|
|
char *namebuf = umalloc(34);
|
|
char *membuf = umalloc(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) {
|
|
if (PCTL_LS_CONFIG.specificproc != NULL
|
|
&& string_strcmp(stat.name, PCTL_LS_CONFIG.specificproc) != 0) {
|
|
continue;
|
|
}
|
|
|
|
if (PCTL_LS_CONFIG.pid != -1 && stat.pid != PCTL_LS_CONFIG.pid) {
|
|
continue;
|
|
}
|
|
|
|
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");
|
|
}
|
|
}
|
|
}
|