From 58ef4e6df81713b2bfd55b2845927a75c0ec36c8 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Mon, 21 Aug 2023 21:25:33 -0700 Subject: [PATCH] Add i/o statistics to wait4() about child process --- libc/calls/getrusage-nt.c | 6 ++---- libc/calls/wait4-nt.c | 8 ++++++++ libc/stdio/fscanf.c | 7 ++++++- libc/stdio/scanf.c | 8 +++++++- libc/stdio/vfscanf.c | 9 ++++++++- libc/stdio/vscanf.c | 9 ++++++++- 6 files changed, 39 insertions(+), 8 deletions(-) diff --git a/libc/calls/getrusage-nt.c b/libc/calls/getrusage-nt.c index 2265305dc..08bedde7e 100644 --- a/libc/calls/getrusage-nt.c +++ b/libc/calls/getrusage-nt.c @@ -16,19 +16,17 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/calls/calls.h" #include "libc/calls/sig.internal.h" -#include "libc/calls/state.internal.h" -#include "libc/calls/struct/rusage.h" +#include "libc/calls/struct/rusage.internal.h" #include "libc/calls/syscall_support-nt.internal.h" #include "libc/fmt/conv.h" #include "libc/nt/accounting.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/thread.h" -#include "libc/str/str.h" #include "libc/sysv/consts/rusage.h" #include "libc/sysv/errfuns.h" diff --git a/libc/calls/wait4-nt.c b/libc/calls/wait4-nt.c index 6b22256ee..d6c52a128 100644 --- a/libc/calls/wait4-nt.c +++ b/libc/calls/wait4-nt.c @@ -35,6 +35,7 @@ #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/processentry32.h" #include "libc/nt/struct/processmemorycounters.h" #include "libc/nt/synchronization.h" @@ -68,6 +69,13 @@ static textwindows void AddProcessStats(int64_t h, struct rusage *ru) { } else { STRACE("%s failed %u", "GetProcessTimes", GetLastError()); } + struct NtIoCounters iocount; + if (GetProcessIoCounters(h, &iocount)) { + ru->ru_inblock += iocount.ReadOperationCount; + ru->ru_oublock += iocount.WriteOperationCount; + } else { + STRACE("%s failed %u", "GetProcessIoCounters", GetLastError()); + } } static textwindows int sys_wait4_nt_impl(int *pid, int *opt_out_wstatus, diff --git a/libc/stdio/fscanf.c b/libc/stdio/fscanf.c index 0e5913b03..736caff76 100644 --- a/libc/stdio/fscanf.c +++ b/libc/stdio/fscanf.c @@ -17,6 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/fmt/fmt.h" +#include "libc/stdio/lock.internal.h" #include "libc/stdio/stdio.h" /** @@ -38,7 +39,11 @@ int fscanf(FILE *stream, const char *fmt, ...) { int rc; va_list va; va_start(va, fmt); - rc = __vcscanf((int (*)(void *))fgetc, (void *)ungetc, stream, fmt, va); + flockfile(stream); + rc = __vcscanf((void *)fgetc_unlocked, // + (void *)ungetc_unlocked, // + stream, fmt, va); + funlockfile(stream); va_end(va); return rc; } diff --git a/libc/stdio/scanf.c b/libc/stdio/scanf.c index 497b47ed2..09475dc41 100644 --- a/libc/stdio/scanf.c +++ b/libc/stdio/scanf.c @@ -16,7 +16,9 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/calls/struct/fd.internal.h" #include "libc/fmt/fmt.h" +#include "libc/stdio/lock.internal.h" #include "libc/stdio/stdio.h" /** @@ -27,7 +29,11 @@ int scanf(const char *fmt, ...) { int rc; va_list va; va_start(va, fmt); - rc = __vcscanf((int (*)(void *))fgetc, NULL, stdin, fmt, va); + flockfile(stdin); + rc = __vcscanf((void *)fgetc_unlocked, // + (void *)ungetc_unlocked, // + stdin, fmt, va); + funlockfile(stdin); va_end(va); return rc; } diff --git a/libc/stdio/vfscanf.c b/libc/stdio/vfscanf.c index 29a911b2b..e899c08bd 100644 --- a/libc/stdio/vfscanf.c +++ b/libc/stdio/vfscanf.c @@ -17,6 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/fmt/fmt.h" +#include "libc/stdio/lock.internal.h" #include "libc/stdio/stdio.h" /** @@ -24,5 +25,11 @@ * @see libc/fmt/vcscanf.h */ int vfscanf(FILE *stream, const char *fmt, va_list ap) { - return __vcscanf((void *)fgetc, (void *)ungetc, stream, fmt, ap); + int rc; + flockfile(stream); + rc = __vcscanf((void *)fgetc_unlocked, // + (void *)ungetc_unlocked, // + stream, fmt, ap); + funlockfile(stream); + return rc; } diff --git a/libc/stdio/vscanf.c b/libc/stdio/vscanf.c index 26e64f989..aea2d144c 100644 --- a/libc/stdio/vscanf.c +++ b/libc/stdio/vscanf.c @@ -17,6 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/fmt/fmt.h" +#include "libc/stdio/lock.internal.h" #include "libc/stdio/stdio.h" /** @@ -24,5 +25,11 @@ * @see libc/fmt/vcscanf.h */ int vscanf(const char *fmt, va_list ap) { - return __vcscanf((int (*)(void *))fgetc, (void *)ungetc, stdin, fmt, ap); + int rc; + flockfile(stdin); + rc = __vcscanf((void *)fgetc_unlocked, // + (void *)ungetc_unlocked, // + stdin, fmt, ap); + flockfile(stdout); + return rc; }