diff options
| author | Stefan Boberg <[email protected]> | 2026-03-24 15:47:23 +0100 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2026-03-24 15:47:23 +0100 |
| commit | 21c2abb1bde697c31bee562465cb986a0429a299 (patch) | |
| tree | 3734d235e79a8fbed307ae5c248936d356553b61 /src/zenutil/process/exitwatcher.h | |
| parent | v5.7.25 hotpatch (#874) (diff) | |
| download | zen-21c2abb1bde697c31bee562465cb986a0429a299.tar.xz zen-21c2abb1bde697c31bee562465cb986a0429a299.zip | |
Subprocess Manager (#889)
Adds a `SubprocessManager` for managing child processes with ASIO-integrated async exit detection, stdout/stderr pipe capture, and periodic metrics sampling. Also introduces `ProcessGroup` for OS-backed process grouping (Windows JobObjects / POSIX process groups).
### SubprocessManager
- Async process exit detection using platform-native mechanisms (Windows `object_handle`, Linux `pidfd_open`, macOS `kqueue EVFILT_PROC`) — no polling
- Stdout/stderr capture via async pipe readers with per-process or default callbacks
- Periodic round-robin metrics sampling (CPU, memory) across managed processes
- Spawn, adopt, remove, kill, and enumerate managed processes
### ProcessGroup
- OS-level process grouping: Windows JobObject (kill-on-close guarantee), POSIX `setpgid` (bulk signal delivery)
- Atomic group kill via `TerminateJobObject` (Windows) or `kill(-pgid, sig)` (POSIX)
- Per-group aggregate metrics and enumeration
### ProcessHandle improvements
- Added explicit constructors from `int` (pid) and `void*` (native handle)
- Added move constructor and move assignment operator
### ProcessMetricsTracker
- Cross-platform process metrics (CPU time, working set, page faults) via `QueryProcessMetrics()`
- ASIO timer-driven periodic sampling with configurable interval and batch size
- Aggregate metrics across tracked processes
### Other changes
- Fixed `zentest-appstub` writing a spurious `Versions` file to cwd on every invocation
Diffstat (limited to 'src/zenutil/process/exitwatcher.h')
| -rw-r--r-- | src/zenutil/process/exitwatcher.h | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/zenutil/process/exitwatcher.h b/src/zenutil/process/exitwatcher.h new file mode 100644 index 000000000..24906d7d0 --- /dev/null +++ b/src/zenutil/process/exitwatcher.h @@ -0,0 +1,48 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#pragma once + +#include <zencore/process.h> +#include <zencore/zencore.h> + +#include <functional> +#include <memory> + +namespace asio { +class io_context; +} + +namespace zen { + +/// Async process exit watcher. +/// +/// Uses platform-specific mechanisms for scalable, non-polling exit detection: +/// Linux: pidfd_open() + asio::posix::stream_descriptor +/// Windows: asio::windows::object_handle +/// macOS: kqueue EVFILT_PROC/NOTE_EXIT + asio::posix::stream_descriptor +/// +/// The callback is invoked exactly once when the process exits, posted to the +/// io_context. Call Cancel() to suppress the callback. +class ProcessExitWatcher +{ +public: + explicit ProcessExitWatcher(asio::io_context& IoContext); + ~ProcessExitWatcher(); + + ProcessExitWatcher(const ProcessExitWatcher&) = delete; + ProcessExitWatcher& operator=(const ProcessExitWatcher&) = delete; + + /// Begin watching the given process. The callback is posted to the io_context + /// when the process exits. Only one Watch() may be active at a time. + void Watch(const ProcessHandle& Handle, std::function<void(int ExitCode)> OnExit); + + /// Cancel any outstanding watch. The callback will not be invoked after this + /// returns. Safe to call if no watch is active. + void Cancel(); + +private: + struct Impl; + std::unique_ptr<Impl> m_Impl; +}; + +} // namespace zen |