Compare commits

...

5 Commits

Author SHA1 Message Date
55f5fbefe4 tb Add & syntax to run commands without blocking/waiting 2025-10-16 15:55:39 +02:00
e7421f73bb pctl List process uptime 2025-10-16 15:46:27 +02:00
43ab1674e8 ulib More time conversion utils 2025-10-16 15:46:06 +02:00
1a2962de80 Track process uptime 2025-10-16 15:45:36 +02:00
5158305fa6 Fix parenthesis in time_isleap() 2025-10-16 15:23:50 +02:00
9 changed files with 154 additions and 10 deletions

View File

@ -15,6 +15,7 @@
#include "ipc/pipe/pipe.h" #include "ipc/pipe/pipe.h"
#include "sysdefs/proc.h" #include "sysdefs/proc.h"
#include "sysdefs/fs.h" #include "sysdefs/fs.h"
#include "time/time.h"
#define PROC_REAPER_FREQ 30 #define PROC_REAPER_FREQ 30
@ -147,6 +148,8 @@ Proc *proc_spawnuser(char *mountpoint, char *path) {
proc->pipes[1] = dlmalloc(sizeof(IpcPipe)); proc->pipes[1] = dlmalloc(sizeof(IpcPipe));
ipc_pipeinit(proc->pipes[1], proc->pid); ipc_pipeinit(proc->pipes[1], proc->pid);
time_get(&proc->time);
return proc; return proc;
} }

View File

@ -8,6 +8,7 @@
#include "vfs/vfs.h" #include "vfs/vfs.h"
#include "ipc/pipe/pipe.h" #include "ipc/pipe/pipe.h"
#include "sysdefs/proc.h" #include "sysdefs/proc.h"
#include "sysdefs/time.h"
#include "dev/dev.h" #include "dev/dev.h"
#define PROC_NAME_MAX 0x100 #define PROC_NAME_MAX 0x100
@ -68,6 +69,8 @@ typedef struct Proc {
} procargs; } procargs;
uint64_t mman_map_base; uint64_t mman_map_base;
Time time;
} Proc; } Proc;
typedef struct { typedef struct {

View File

@ -226,6 +226,8 @@ int32_t SYSCALL2(sys_proc_stat, pidx, pstat1) {
LL_FOREACH_SAFE(p->vas, vas, vastmp) { LL_FOREACH_SAFE(p->vas, vas, vastmp) {
stat->usemem += vas->size; stat->usemem += vas->size;
} }
hal_memcpy(&stat->time, &p->time, sizeof(p->time));
break; break;
} }
} }

View File

@ -69,7 +69,7 @@ void time_get(Time *time) {
} }
bool time_isleap(uint32_t y) { bool time_isleap(uint32_t y) {
return ((y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0)); return (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0));
} }
const uint16_t days_before_month[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; const uint16_t days_before_month[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
@ -94,3 +94,45 @@ timeunix_t time_tounix(Time *time) {
secs += time->second; secs += time->second;
return secs; return secs;
} }
void timeunix_totime(timeunix_t unix, Time *time) {
uint64_t days = unix / 86400;
uint32_t rem = days % 86400;
time->hour = rem / 3600;
time->minute = (rem % 3600) / 60;
time->second = rem % 60;
uint32_t year = 1970;
for (;;) {
uint32_t ydays = time_isleap(year) ? 366 : 365;
if (days >= ydays) {
days -= ydays;
year++;
} else {
break;
}
}
time->year = year;
bool leap = time_isleap(year);
uint32_t month = 0;
while (month < 11) {
uint32_t next = days_before_month[month + 1];
if (leap && month + 1 > 1) {
next++;
}
if (days < next) {
break;
}
month++;
}
uint32_t month_start = days_before_month[month];
if (leap && month > 1) {
month_start++;
}
time->month = month + 1;
time->day = (days - month_start) + 1;
}

View File

@ -1,6 +1,8 @@
#ifndef SHARE_SYSDEFS_PROC_H_ #ifndef SHARE_SYSDEFS_PROC_H_
#define SHARE_SYSDEFS_PROC_H_ #define SHARE_SYSDEFS_PROC_H_
#include "time.h"
#define PROC_ARG_MAX 128 #define PROC_ARG_MAX 128
typedef struct { typedef struct {
@ -8,6 +10,7 @@ typedef struct {
char name[0x100]; char name[0x100];
uint8_t state; uint8_t state;
size_t usemem; size_t usemem;
Time time;
} ProcStat; } ProcStat;
typedef uint64_t PID_t; typedef uint64_t PID_t;

View File

@ -29,3 +29,62 @@ timeunix_t time_tounix(Time *time) {
secs += time->second; secs += time->second;
return secs; return secs;
} }
void timeunix_totime(timeunix_t unix, Time *time) {
uint64_t days = unix / 86400;
uint32_t rem = days % 86400;
time->hour = rem / 3600;
time->minute = (rem % 3600) / 60;
time->second = rem % 60;
uint32_t year = 1970;
for (;;) {
uint32_t ydays = time_isleap(year) ? 366 : 365;
if (days >= ydays) {
days -= ydays;
year++;
} else {
break;
}
}
time->year = year;
bool leap = time_isleap(year);
uint32_t month = 0;
while (month < 11) {
uint32_t next = days_before_month[month + 1];
if (leap && month + 1 > 1) {
next++;
}
if (days < next) {
break;
}
month++;
}
uint32_t month_start = days_before_month[month];
if (leap && month > 1) {
month_start++;
}
time->month = month + 1;
time->day = (days - month_start) + 1;
}
uint32_t time_totalhours(Time *time) {
uint32_t days = 0;
for (uint32_t y = 1970; y < time->year; y++) {
days += time_isleap(y) ? 366 : 365;
}
days += days_before_month[time->month - 1];
if (time->month > 2 && time_isleap(time->year)) {
days += 1;
}
days += (time->day - 1);
uint32_t total = days * 24 + time->hour;
return total;
}

View File

@ -4,5 +4,7 @@
#include <sysdefs/time.h> #include <sysdefs/time.h>
timeunix_t time_tounix(Time *time); timeunix_t time_tounix(Time *time);
void timeunix_totime(timeunix_t unix, Time *time);
uint32_t time_totalhours(Time *time);
#endif // ULIB_TIME_TIME_H_ #endif // ULIB_TIME_TIME_H_

View File

@ -31,7 +31,7 @@ void pctl_ls(void) {
char *namebuf = umalloc(34); char *namebuf = umalloc(34);
char *membuf = umalloc(20); char *membuf = umalloc(20);
uprintf("%-30s %s %-15s %-8s\n", "NAME", "PID", "MEMORY", "STATE"); uprintf("%-30s %s %-15s %-8s %-25s\n", "NAME", "PID", "MEMORY", "STATE", "UPTIME");
for (size_t i = 0; i < (size_t)procslen; i++) { for (size_t i = 0; i < (size_t)procslen; i++) {
ProcStat stat; ZERO(&stat); ProcStat stat; ZERO(&stat);
@ -58,7 +58,24 @@ void pctl_ls(void) {
stat.pid, stat.pid,
human_size(stat.usemem, membuf, 20) human_size(stat.usemem, membuf, 20)
); );
uprintf("%-8s", states[stat.state]); uprintf("%-8s ", states[stat.state]);
Time time1;
time(&time1);
timeunix_t now = time_tounix(&time1);
timeunix_t procspawn = time_tounix(&stat.time);
timeunix_t diff = now - procspawn;
char uptimebuf[100];
uint32_t hrs = diff / 3600;
uint32_t mins = (diff % 3600) / 60;
uint32_t secs = diff % 60;
usprintf(uptimebuf, "%us %umin %uh", secs, mins, hrs);
uprintf("%-25s ", uptimebuf);
uprintf("\n"); uprintf("\n");
} }
} }

View File

@ -188,6 +188,17 @@ bool interp_runstring(char *string, InterpResult **res, bool logcmds, bool inter
tz_classify(&tz); tz_classify(&tz);
tz_expandspecial(&tz); tz_expandspecial(&tz);
bool wait = true;
if (tz.tokens->str[0] == '&') {
Token *tk = tz.tokens;
LL_REMOVE(tz.tokens, tk);
ufree(tk->str);
ufree(tk);
wait = false;
}
Token *cmdtk = tz.tokens; Token *cmdtk = tz.tokens;
if (cmdtk->type == TOK_CMD) { if (cmdtk->type == TOK_CMD) {
ok = cmdtk->cmd(cmdtk->next); ok = cmdtk->cmd(cmdtk->next);
@ -217,15 +228,17 @@ bool interp_runstring(char *string, InterpResult **res, bool logcmds, bool inter
proc_run(app); proc_run(app);
while(proc_pollstate(app) != 4) { if (wait) {
if (interactive) { while(proc_pollstate(app) != 4) {
int32_t key = dev_cmd(&ps2kbdev, DEV_PS2KBDEV_READCH, (void *)PID, 0, NULL); if (interactive) {
if (key > 0 && (uint8_t)key == C('S')) { int32_t key = dev_cmd(&ps2kbdev, DEV_PS2KBDEV_READCH, (void *)PID, 0, NULL);
proc_kill(app); if (key > 0 && (uint8_t)key == C('S')) {
goto cleanup; proc_kill(app);
goto cleanup;
}
} }
schedrelease();
} }
schedrelease();
} }
cleanup: { cleanup: {