mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-07 03:38:31 +00:00
Make improvements
- We now serialize the file descriptor table when spawning / executing processes on Windows. This means you can now inherit more stuff than just standard i/o. It's needed by bash, which duplicates the console to file descriptor #255. We also now do a better job serializing the environment variables, so you're less likely to encounter E2BIG when using your bash shell. We also no longer coerce environ to uppercase - execve() on Windows now remotely controls its parent process to make them spawn a replacement for itself. Then it'll be able to terminate immediately once the spawn succeeds, without having to linger around for the lifetime as a shell process for proxying the exit code. When process worker thread running in the parent sees the child die, it's given a handle to the new child, to replace it in the process table. - execve() and posix_spawn() on Windows will now provide CreateProcess an explicit handle list. This allows us to remove handle locks which enables better fork/spawn concurrency, with seriously correct thread safety. Other codebases like Go use the same technique. On the other hand fork() still favors the conventional WIN32 inheritence approach which can be a little bit messy, but is *controlled* by guaranteeing perfectly clean slates at both the spawning and execution boundaries - sigset_t is now 64 bits. Having it be 128 bits was a mistake because there's no reason to use that and it's only supported by FreeBSD. By using the system word size, signal mask manipulation on Windows goes very fast. Furthermore @asyncsignalsafe funcs have been rewritten on Windows to take advantage of signal masking, now that it's much more pleasant to use. - All the overlapped i/o code on Windows has been rewritten for pretty good signal and cancelation safety. We're now able to ensure overlap data structures are cleaned up so long as you don't longjmp() out of out of a signal handler that interrupted an i/o operation. Latencies are also improved thanks to the removal of lots of "busy wait" code. Waits should be optimal for everything except poll(), which shall be the last and final demon we slay in the win32 i/o horror show. - getrusage() on Windows is now able to report RUSAGE_CHILDREN as well as RUSAGE_SELF, thanks to aggregation in the process manager thread.
This commit is contained in:
parent
af7cb3c82f
commit
791f79fcb3
382 changed files with 4008 additions and 4511 deletions
107
libc/proc/proc.c
107
libc/proc/proc.c
|
@ -16,21 +16,32 @@
|
|||
│ 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"
|
||||
#include "libc/calls/state.internal.h"
|
||||
#include "libc/calls/struct/rusage.h"
|
||||
#include "libc/calls/struct/siginfo.h"
|
||||
#include "libc/calls/struct/sigset.internal.h"
|
||||
#include "libc/cosmo.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/wintime.internal.h"
|
||||
#include "libc/intrin/dll.h"
|
||||
#include "libc/intrin/leaky.internal.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/intrin/weaken.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/nt/accounting.h"
|
||||
#include "libc/nt/enum/processaccess.h"
|
||||
#include "libc/nt/enum/processcreationflags.h"
|
||||
#include "libc/nt/enum/status.h"
|
||||
#include "libc/nt/enum/wait.h"
|
||||
#include "libc/nt/process.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/struct/filetime.h"
|
||||
#include "libc/nt/struct/iocounters.h"
|
||||
#include "libc/nt/struct/processmemorycounters.h"
|
||||
#include "libc/nt/synchronization.h"
|
||||
#include "libc/nt/thread.h"
|
||||
#include "libc/proc/proc.internal.h"
|
||||
|
@ -42,11 +53,37 @@
|
|||
#include "libc/thread/tls.h"
|
||||
#ifdef __x86_64__
|
||||
|
||||
/**
|
||||
* @fileoverview Windows Subprocess Management.
|
||||
*/
|
||||
|
||||
struct Procs __proc;
|
||||
|
||||
static textwindows void GetProcessStats(int64_t h, struct rusage *ru) {
|
||||
bzero(ru, sizeof(*ru));
|
||||
struct NtProcessMemoryCountersEx memcount = {sizeof(memcount)};
|
||||
unassert(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));
|
||||
ru->ru_utime = WindowsDurationToTimeVal(ReadFileTime(usertime));
|
||||
ru->ru_stime = WindowsDurationToTimeVal(ReadFileTime(kerneltime));
|
||||
struct NtIoCounters iocount;
|
||||
unassert(GetProcessIoCounters(h, &iocount));
|
||||
ru->ru_inblock = iocount.ReadOperationCount;
|
||||
ru->ru_oublock = iocount.WriteOperationCount;
|
||||
}
|
||||
|
||||
static textwindows dontinstrument uint32_t __proc_worker(void *arg) {
|
||||
__bootstrap_tls(&__proc.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;
|
||||
int64_t handles[64];
|
||||
int sic, dosignal = 0;
|
||||
struct Proc *pr, *objects[64];
|
||||
|
@ -64,33 +101,53 @@ static textwindows dontinstrument uint32_t __proc_worker(void *arg) {
|
|||
}
|
||||
dll_make_last(&__proc.list, samples);
|
||||
__proc_unlock();
|
||||
|
||||
// wait for win32 to report any status change
|
||||
millis = n == 64 ? __SIG_PROC_INTERVAL_MS : -1u;
|
||||
i = WaitForMultipleObjects(n, handles, false, millis);
|
||||
if (i == -1u) {
|
||||
STRACE("PROC WORKER DYING: WAIT FAILED: %s", strerror(errno));
|
||||
break;
|
||||
}
|
||||
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];
|
||||
if (status == 0xc9af3d51u) status = kNtStillActive;
|
||||
pr->wstatus = status;
|
||||
if ((__sighandrvas[SIGCHLD] == (uintptr_t)SIG_IGN ||
|
||||
(__sighandflags[SIGCHLD] & SA_NOCLDWAIT)) &&
|
||||
(!pr->waiters && !__proc.waiters)) {
|
||||
rusage_add(&pr->ru, &ru);
|
||||
rusage_add(&__proc.ruchlds, &ru);
|
||||
if ((status & 0xFF000000u) == 0x23000000u) {
|
||||
// handle child execve()
|
||||
CloseHandle(pr->handle);
|
||||
dll_remove(&__proc.list, &pr->elem);
|
||||
dll_make_first(&__proc.free, &pr->elem);
|
||||
pr->handle = status & 0x00FFFFFF;
|
||||
} 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);
|
||||
// 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 {
|
||||
dosignal = 1;
|
||||
sic = WIFSIGNALED(status) ? CLD_KILLED : CLD_EXITED;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
__proc_unlock();
|
||||
|
@ -189,4 +246,22 @@ textwindows void __proc_free(struct Proc *proc) {
|
|||
dll_make_first(&__proc.free, &proc->elem);
|
||||
}
|
||||
|
||||
// returns owned handle of direct child process
|
||||
// this is intended for the __proc_handle() implementation
|
||||
textwindows int64_t __proc_search(int pid) {
|
||||
struct Dll *e;
|
||||
int64_t handle = 0;
|
||||
BLOCK_SIGNALS;
|
||||
__proc_lock();
|
||||
for (e = dll_first(__proc.list); e; e = dll_next(__proc.list, e)) {
|
||||
if (pid == PROC_CONTAINER(e)->pid) {
|
||||
handle = PROC_CONTAINER(e)->handle;
|
||||
break;
|
||||
}
|
||||
}
|
||||
__proc_unlock();
|
||||
ALLOW_SIGNALS;
|
||||
return handle;
|
||||
}
|
||||
|
||||
#endif /* __x86_64__ */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue