mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-06 11:18:30 +00:00
Put more thought into i/o polyfills
wait4() is now solid enough to run `make -j100` on Windows. You can now use MSG_DONTWAIT on Windows. There was a handle leak in accept() that's been fixed. Our WIN32 overlapped i/o code has been simplified. Priority class now inherits into subprocesses, so the verynice command will work and the signal mask will now be inherited by execve() and posix_spawn()
This commit is contained in:
parent
736fdb757a
commit
e961385e55
52 changed files with 679 additions and 487 deletions
204
libc/proc/proc.c
204
libc/proc/proc.c
|
@ -16,7 +16,6 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/calls/sig.internal.h"
|
||||
|
@ -37,6 +36,7 @@
|
|||
#include "libc/nt/enum/processcreationflags.h"
|
||||
#include "libc/nt/enum/status.h"
|
||||
#include "libc/nt/enum/wait.h"
|
||||
#include "libc/nt/events.h"
|
||||
#include "libc/nt/process.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/struct/filetime.h"
|
||||
|
@ -59,95 +59,167 @@
|
|||
|
||||
struct Procs __proc;
|
||||
|
||||
static textwindows void GetProcessStats(int64_t h, struct rusage *ru) {
|
||||
static textwindows void __proc_stats(int64_t h, struct rusage *ru) {
|
||||
bzero(ru, sizeof(*ru));
|
||||
struct NtProcessMemoryCountersEx memcount = {sizeof(memcount)};
|
||||
unassert(GetProcessMemoryInfo(h, &memcount, sizeof(memcount)));
|
||||
GetProcessMemoryInfo(h, &memcount, sizeof(memcount));
|
||||
ru->ru_maxrss = memcount.PeakWorkingSetSize / 1024;
|
||||
ru->ru_majflt = memcount.PageFaultCount;
|
||||
struct NtFileTime createtime, exittime;
|
||||
struct NtFileTime kerneltime, usertime;
|
||||
unassert(GetProcessTimes(h, &createtime, &exittime, &kerneltime, &usertime));
|
||||
GetProcessTimes(h, &createtime, &exittime, &kerneltime, &usertime);
|
||||
ru->ru_utime = WindowsDurationToTimeVal(ReadFileTime(usertime));
|
||||
ru->ru_stime = WindowsDurationToTimeVal(ReadFileTime(kerneltime));
|
||||
struct NtIoCounters iocount;
|
||||
unassert(GetProcessIoCounters(h, &iocount));
|
||||
GetProcessIoCounters(h, &iocount);
|
||||
ru->ru_inblock = iocount.ReadOperationCount;
|
||||
ru->ru_oublock = iocount.WriteOperationCount;
|
||||
}
|
||||
|
||||
// performs accounting on exited process
|
||||
// multiple threads can wait on a process
|
||||
// it's important that only one calls this
|
||||
textwindows int __proc_harvest(struct Proc *pr, bool iswait4) {
|
||||
int sic = 0;
|
||||
uint32_t status;
|
||||
struct rusage ru;
|
||||
GetExitCodeProcess(pr->handle, &status);
|
||||
if (status == kNtStillActive) return 0;
|
||||
__proc_stats(pr->handle, &ru);
|
||||
rusage_add(&pr->ru, &ru);
|
||||
rusage_add(&__proc.ruchlds, &ru);
|
||||
if ((status & 0xFF000000u) == 0x23000000u) {
|
||||
// handle child execve()
|
||||
CloseHandle(pr->handle);
|
||||
pr->handle = status & 0x00FFFFFF;
|
||||
} else {
|
||||
// handle child _Exit()
|
||||
if (status == 0xc9af3d51u) {
|
||||
status = kNtStillActive;
|
||||
}
|
||||
pr->wstatus = status;
|
||||
if (!iswait4 && !pr->waiters && !__proc.waiters &&
|
||||
(__sighandrvas[SIGCHLD] == (uintptr_t)SIG_IGN ||
|
||||
(__sighandflags[SIGCHLD] & SA_NOCLDWAIT))) {
|
||||
// perform automatic zombie reaping
|
||||
dll_remove(&__proc.list, &pr->elem);
|
||||
dll_make_first(&__proc.free, &pr->elem);
|
||||
CloseHandle(pr->handle);
|
||||
} else {
|
||||
// transitions process to zombie state
|
||||
// wait4 is responsible for reaping it
|
||||
pr->status = PROC_ZOMBIE;
|
||||
dll_remove(&__proc.list, &pr->elem);
|
||||
dll_make_first(&__proc.zombies, &pr->elem);
|
||||
SetEvent(__proc.haszombies);
|
||||
if (!pr->waiters && !__proc.waiters) {
|
||||
if (WIFSIGNALED(status)) {
|
||||
sic = CLD_KILLED;
|
||||
} else {
|
||||
sic = CLD_EXITED;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return sic;
|
||||
}
|
||||
|
||||
static textwindows dontinstrument uint32_t __proc_worker(void *arg) {
|
||||
__bootstrap_tls(&__proc.tls, __builtin_frame_address(0));
|
||||
struct CosmoTib tls;
|
||||
__bootstrap_tls(&tls, __builtin_frame_address(0));
|
||||
for (;;) {
|
||||
|
||||
// assemble a group of processes to wait on. if more than 64
|
||||
// children exist, then we'll use a small timeout and select
|
||||
// processes with a shifting window via a double linked list
|
||||
struct rusage ru;
|
||||
// if fewer than 64 processes exist then we'll also wait for
|
||||
// process birth notifications, and wait on them immediately
|
||||
int64_t handles[64];
|
||||
int sic, dosignal = 0;
|
||||
struct Proc *pr, *objects[64];
|
||||
struct Proc *objects[64];
|
||||
uint32_t millis, i, n = 0;
|
||||
struct Dll *e, *e2, *samples = 0;
|
||||
uint32_t millis, status, i, n = 1;
|
||||
__proc_lock();
|
||||
handles[0] = __proc.onstart;
|
||||
for (e = dll_first(__proc.list); e && n < 64; ++n, e = e2) {
|
||||
pr = PROC_CONTAINER(e);
|
||||
for (e = dll_first(__proc.list); e && n < 64; e = e2) {
|
||||
struct Proc *pr = PROC_CONTAINER(e);
|
||||
e2 = dll_next(__proc.list, e);
|
||||
// cycle process to end of list
|
||||
dll_remove(&__proc.list, e);
|
||||
dll_make_last(&samples, e);
|
||||
handles[n] = pr->handle;
|
||||
objects[n] = pr;
|
||||
// don't bother waiting if it's already awaited
|
||||
if (!pr->waiters) {
|
||||
handles[n] = pr->handle;
|
||||
objects[n] = pr;
|
||||
++pr->waiters;
|
||||
++n;
|
||||
}
|
||||
}
|
||||
dll_make_last(&__proc.list, samples);
|
||||
__proc_unlock();
|
||||
|
||||
// wait for win32 to report any status change
|
||||
millis = n == 64 ? 20 : -1u;
|
||||
unassert((i = WaitForMultipleObjects(n, handles, false, millis)) != -1u);
|
||||
i &= ~kNtWaitAbandoned;
|
||||
if (!i || i == kNtWaitTimeout) continue;
|
||||
GetExitCodeProcess(handles[i], &status);
|
||||
if (status == kNtStillActive) continue;
|
||||
GetProcessStats(handles[i], &ru);
|
||||
|
||||
// update data structures and notify folks
|
||||
__proc_lock();
|
||||
pr = objects[i];
|
||||
rusage_add(&pr->ru, &ru);
|
||||
rusage_add(&__proc.ruchlds, &ru);
|
||||
if ((status & 0xFF000000u) == 0x23000000u) {
|
||||
// handle child execve()
|
||||
CloseHandle(pr->handle);
|
||||
pr->handle = status & 0x00FFFFFF;
|
||||
// wait for something to happen
|
||||
if (n == 64) {
|
||||
millis = 5;
|
||||
} else {
|
||||
// handle child _exit()
|
||||
CloseHandle(pr->handle);
|
||||
if (status == 0xc9af3d51u) {
|
||||
status = kNtStillActive;
|
||||
}
|
||||
pr->wstatus = status;
|
||||
if ((__sighandrvas[SIGCHLD] == (uintptr_t)SIG_IGN ||
|
||||
(__sighandflags[SIGCHLD] & SA_NOCLDWAIT)) &&
|
||||
(!pr->waiters && !__proc.waiters)) {
|
||||
dll_remove(&__proc.list, &pr->elem);
|
||||
dll_make_first(&__proc.free, &pr->elem);
|
||||
} else {
|
||||
pr->iszombie = 1;
|
||||
dll_remove(&__proc.list, &pr->elem);
|
||||
dll_make_first(&__proc.zombies, &pr->elem);
|
||||
if (pr->waiters) {
|
||||
nsync_cv_broadcast(&pr->onexit);
|
||||
} else if (__proc.waiters) {
|
||||
nsync_cv_signal(&__proc.onexit);
|
||||
} else {
|
||||
dosignal = 1;
|
||||
sic = WIFSIGNALED(status) ? CLD_KILLED : CLD_EXITED;
|
||||
}
|
||||
millis = -1u;
|
||||
handles[n++] = __proc.onbirth;
|
||||
}
|
||||
i = WaitForMultipleObjects(n, handles, false, millis);
|
||||
if (i == -1u) {
|
||||
STRACE("proc wait panic %d", GetLastError());
|
||||
_Exit(157);
|
||||
}
|
||||
if (i & kNtWaitAbandoned) {
|
||||
i &= ~kNtWaitAbandoned;
|
||||
STRACE("proc %u handle %ld abandoned", i, handles[i]);
|
||||
}
|
||||
__proc_lock();
|
||||
|
||||
// release our waiter status
|
||||
for (int j = 0; j < n; ++j) {
|
||||
if (handles[j] == __proc.onbirth) continue;
|
||||
if (j == i) continue;
|
||||
if (!--objects[j]->waiters && objects[j]->status == PROC_UNDEAD) {
|
||||
__proc_free(objects[j]);
|
||||
}
|
||||
}
|
||||
|
||||
// check if we need to churn due to >64 processes
|
||||
if (i == kNtWaitTimeout) {
|
||||
__proc_unlock();
|
||||
continue;
|
||||
}
|
||||
|
||||
// churn on new process birth
|
||||
if (handles[i] == __proc.onbirth) {
|
||||
__proc_unlock();
|
||||
continue;
|
||||
}
|
||||
|
||||
// handle process status change
|
||||
int sic = 0;
|
||||
--objects[i]->waiters;
|
||||
switch (objects[i]->status) {
|
||||
case PROC_ALIVE:
|
||||
sic = __proc_harvest(objects[i], false);
|
||||
break;
|
||||
case PROC_ZOMBIE:
|
||||
break;
|
||||
case PROC_UNDEAD:
|
||||
if (!objects[i]->waiters) {
|
||||
__proc_free(objects[i]);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
__proc_unlock();
|
||||
if (dosignal) {
|
||||
|
||||
// don't raise SIGCHLD if
|
||||
// 1. wait4() is being used
|
||||
// 2. SIGCHLD has SIG_IGN handler
|
||||
// 3. SIGCHLD has SA_NOCLDWAIT flag
|
||||
if (sic) {
|
||||
__sig_generate(SIGCHLD, sic);
|
||||
}
|
||||
}
|
||||
|
@ -158,7 +230,8 @@ static textwindows dontinstrument uint32_t __proc_worker(void *arg) {
|
|||
* Lazy initializes process tracker data structures and worker.
|
||||
*/
|
||||
static textwindows void __proc_setup(void) {
|
||||
__proc.onstart = CreateSemaphore(0, 0, 1, 0);
|
||||
__proc.onbirth = CreateEvent(0, 0, 0, 0); // auto reset
|
||||
__proc.haszombies = CreateEvent(0, 1, 0, 0); // manual reset
|
||||
__proc.thread = CreateThread(0, 65536, __proc_worker, 0,
|
||||
kNtStackSizeParamIsAReservation, 0);
|
||||
}
|
||||
|
@ -191,7 +264,7 @@ textwindows void __proc_wipe(void) {
|
|||
* The returned memory is not tracked by any list. It must be filled in
|
||||
* with system process information and then added back to the system by
|
||||
* calling __proc_add(). If process creation fails, then it needs to be
|
||||
* released using __proc_free().
|
||||
* added back to the __proc.free list by caller.
|
||||
*/
|
||||
textwindows struct Proc *__proc_new(void) {
|
||||
struct Dll *e;
|
||||
|
@ -230,16 +303,13 @@ IGNORE_LEAKS(__proc_new)
|
|||
*/
|
||||
textwindows void __proc_add(struct Proc *proc) {
|
||||
dll_make_first(&__proc.list, &proc->elem);
|
||||
ReleaseSemaphore(__proc.onstart, 1, 0);
|
||||
SetEvent(__proc.onbirth);
|
||||
}
|
||||
|
||||
/**
|
||||
* Frees process allocation.
|
||||
*
|
||||
* Process must not be currently tracked in the active or zombies list.
|
||||
*/
|
||||
textwindows void __proc_free(struct Proc *proc) {
|
||||
dll_make_first(&__proc.free, &proc->elem);
|
||||
textwindows void __proc_free(struct Proc *pr) {
|
||||
dll_remove(&__proc.undead, &pr->elem);
|
||||
dll_make_first(&__proc.free, &pr->elem);
|
||||
CloseHandle(pr->handle);
|
||||
}
|
||||
|
||||
// returns owned handle of direct child process
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue