conformance: the CPU clocks and host pages have a Windows form; Windows answers no load average

The conformance command did not build for Windows: `src/clocks.rs` read the two CPU clocks with
`libc::clock_gettime` and the load average with `getloadavg`, and `src/host_pages.rs` mapped a
no-copy buffer's pages with `libc::mmap`, none of which `libc` has on Windows. So the Windows host
(a supported metal2vulkan-rt host) could not replay the suite.

On Windows the thread's and the process's CPU clocks are `GetThreadTimes` and `GetProcessTimes`
(kernel plus user, in 100 ns units), and the pages are a committed `VirtualAlloc` released with
`VirtualFree`, the forms metal2vulkan-runtime already uses. Windows keeps no load average, so
`host_load` answers `None` there, which the timing report already reads as "this host answers no
load average" and the per-call gate already judges on what the host does answer. `libc` is now a
unix-only dependency and `windows-sys` a Windows-only one, as in the runtimes.

A new unit test holds both CPU clocks to answering and moving while the thread computes, so a host
form whose call fails or reads a constant is seen. CI's Windows clippy now names the conformance
crate, so the Windows build is held from Linux.

Verified: unverified at this commit; the compile check and the Windows run follow.
This commit is contained in:
Anees Iqbal committed 2026-11-18 23:36:38 -08:00
1 parent bc6dc40a14
commit dea5618aa7
4 files changed
+193 -27

No files matched your search

+4 -4
View File
@@ -121,9 +121,9 @@ jobs:
CARGO_TARGET_DIR: target/gpucfg
run: cargo clippy --workspace --all-targets --all-features -- -D warnings
# The published crates (and devtools, which their tests use) for Windows, checked from Linux:
# clippy links nothing, so neither target needs a Windows SDK or linker. gnullvm is the
# primary Windows target, msvc the second.
# The published crates (and devtools, which their tests use) and the conformance suite, which
# the Windows host replays, for Windows, checked from Linux: clippy links nothing, so neither
# target needs a Windows SDK or linker. gnullvm is the primary Windows target, msvc the second.
- name: Clippy for Windows (warnings denied, Linux)
if: runner.os == 'Linux'
run: |
@@ -131,7 +131,7 @@ jobs:
rustup target add x86_64-pc-windows-gnullvm x86_64-pc-windows-msvc
for target in x86_64-pc-windows-gnullvm x86_64-pc-windows-msvc; do
echo "::group::$target"
cargo clippy --target "$target" -p metal2vulkan-air -p metal2vulkan -p metal2vulkan-rt -p metal2vulkan-runtime -p metal2vulkan-devtools --all-targets --all-features -- -D warnings
cargo clippy --target "$target" -p metal2vulkan-air -p metal2vulkan -p metal2vulkan-rt -p metal2vulkan-runtime -p metal2vulkan-devtools -p metal2vulkan-conformance --all-targets --all-features -- -D warnings
echo "::endgroup::"
done
+15 -3
View File
@@ -23,13 +23,25 @@ metal2vulkan-rt = { path = "../metal2vulkan-rt" }
metal2vulkan-devtools = { path = "../metal2vulkan-devtools" }
ash = { workspace = true, features = ["loaded"] }
base64 = "0.22"
# The two CPU clocks the cost table reads: the calling thread's and the whole process's
# (`src/clocks.rs`). `std` exposes neither.
libc.workspace = true
serde.workspace = true
serde_json.workspace = true
sha2.workspace = true
# The two CPU clocks the cost table reads, the calling thread's and the whole process's, and the
# load average (`src/clocks.rs`), and the host pages of a no-copy buffer (`src/host_pages.rs`), on
# unix. `std` exposes none of them.
[target.'cfg(unix)'.dependencies]
libc.workspace = true
# The same on Windows: `GetThreadTimes`, `GetProcessTimes`, and `VirtualAlloc` with `VirtualFree`.
# Windows keeps no load average.
[target.'cfg(windows)'.dependencies]
windows-sys = { workspace = true, features = [
"Win32_Foundation",
"Win32_System_Memory",
"Win32_System_Threading",
] }
[target.'cfg(target_os = "macos")'.dependencies]
# The command-buffer handler blocks the event cases add: `addScheduledHandler:` and
# `addCompletedHandler:` take Objective-C blocks, which is how a probe bounds a wait Metal has no
+123 -3
View File
@@ -28,6 +28,13 @@
//! machinery's cost on the host it runs on and prints it beside the table
//! ([`crate::times::instrument_nanos`]). It is never subtracted from a figure: a measured overhead
//! a reader can see is honest, and an estimate quietly removed from every row is not.
//!
//! # On Windows
//!
//! The two CPU clocks are `GetThreadTimes` and `GetProcessTimes`, kernel and user time summed,
//! which Windows counts in 100 ns units; their step there was not measured, and the table above is
//! the M3's. Windows keeps no load average, so [`host_load`] answers `None` there and a timing
//! report says the host answers none.
/// The calling thread's CPU time, user and system, in nanoseconds, or `None` where the host does
/// not answer the clock.
@@ -36,7 +43,26 @@
/// difference between a call's elapsed time and this is the time the caller was not computing: it
/// was waiting.
pub fn thread_cpu_nanos() -> Option<u64> {
read(libc::CLOCK_THREAD_CPUTIME_ID)
#[cfg(unix)]
{
read(libc::CLOCK_THREAD_CPUTIME_ID)
}
#[cfg(windows)]
{
use windows_sys::Win32::System::Threading::{GetCurrentThread, GetThreadTimes};
let mut times = Times::default();
// SAFETY: the pseudo-handle of the calling thread, and four valid `FILETIME`s to fill.
let answered = unsafe {
GetThreadTimes(
GetCurrentThread(),
&mut times.created,
&mut times.exited,
&mut times.kernel,
&mut times.user,
)
};
times.on_cpu(answered != 0)
}
}
/// The whole process's CPU time, every thread of it, in nanoseconds, or `None` where the host does
@@ -50,11 +76,31 @@ pub fn thread_cpu_nanos() -> Option<u64> {
/// on the Metal leg it covers Metal's own. That is the honest denominator: the suite compares two
/// whole stacks and has no way to cut one into layers ([`crate::times`]).
pub fn process_cpu_nanos() -> Option<u64> {
read(libc::CLOCK_PROCESS_CPUTIME_ID)
#[cfg(unix)]
{
read(libc::CLOCK_PROCESS_CPUTIME_ID)
}
#[cfg(windows)]
{
use windows_sys::Win32::System::Threading::{GetCurrentProcess, GetProcessTimes};
let mut times = Times::default();
// SAFETY: the pseudo-handle of this process, and four valid `FILETIME`s to fill.
let answered = unsafe {
GetProcessTimes(
GetCurrentProcess(),
&mut times.created,
&mut times.exited,
&mut times.kernel,
&mut times.user,
)
};
times.on_cpu(answered != 0)
}
}
/// One POSIX clock, in nanoseconds. `None` where the call failed, which is how a host that does
/// not answer a clock reaches the report as a missing attribution rather than as a zero.
#[cfg(unix)]
fn read(clock: libc::clockid_t) -> Option<u64> {
let mut when = libc::timespec {
tv_sec: 0,
@@ -71,7 +117,35 @@ fn read(clock: libc::clockid_t) -> Option<u64> {
seconds.checked_mul(1_000_000_000)?.checked_add(nanos)
}
/// The host's one-minute load average, or `None` where the host does not answer it.
/// The four times `GetThreadTimes` and `GetProcessTimes` fill.
#[cfg(windows)]
#[derive(Default)]
struct Times {
created: windows_sys::Win32::Foundation::FILETIME,
exited: windows_sys::Win32::Foundation::FILETIME,
kernel: windows_sys::Win32::Foundation::FILETIME,
user: windows_sys::Win32::Foundation::FILETIME,
}
#[cfg(windows)]
impl Times {
/// The kernel and user times the call filled, in nanoseconds: each is counted in 100 ns
/// units. `None` where the call did not answer, as a POSIX clock's -1 is.
fn on_cpu(&self, answered: bool) -> Option<u64> {
if !answered {
return None;
}
let units = |time: &windows_sys::Win32::Foundation::FILETIME| {
(u64::from(time.dwHighDateTime) << 32) | u64::from(time.dwLowDateTime)
};
units(&self.kernel)
.checked_add(units(&self.user))?
.checked_mul(100)
}
}
/// The host's one-minute load average, or `None` where the host does not answer it: Windows keeps
/// none.
///
/// **Why a timing report reads this.** The two legs are not loaded alike: ours spends its cost on
/// the calling thread's CPU where Metal's largest calls sit blocked on the GPU, so host load does
@@ -81,6 +155,19 @@ fn read(clock: libc::clockid_t) -> Option<u64> {
/// to 13.3 ms (1.6x), and `replace_region`, which is four fifths blocked rather than CPU, moved
/// the *other* way, 60.9x to 12.1x. A ratio read on a busy host is not a ratio of the two stacks.
pub fn host_load() -> Option<f64> {
#[cfg(unix)]
{
load_average()
}
#[cfg(windows)]
{
None
}
}
/// The one-minute entry of `getloadavg`, or `None` where the host keeps no load average.
#[cfg(unix)]
fn load_average() -> Option<f64> {
let mut averages = [0.0f64; 3];
// SAFETY: `getloadavg` writes at most the number of entries it is given into the pointer it
// is given, and reads nothing else. It answers -1 where the host keeps no load average, and
@@ -91,3 +178,36 @@ pub fn host_load() -> Option<f64> {
}
Some(averages[0])
}
#[cfg(test)]
mod tests {
use super::{process_cpu_nanos, thread_cpu_nanos};
use std::time::{Duration, Instant};
/// **Both CPU clocks answer on the host the suite runs on, and both move while its thread
/// computes.** A host form whose call fails answers `None` for every case, and one that reads
/// the wrong thing answers a constant; either would leave the cost table's attribution empty or
/// flat on that host with nothing else saying so.
#[test]
fn the_cpu_clocks_answer_and_move_while_the_thread_computes() {
let thread = thread_cpu_nanos().expect("this host answers the thread's CPU clock");
let process = process_cpu_nanos().expect("this host answers the process's CPU clock");
let started = Instant::now();
let mut spun = 0_u64;
loop {
spun = std::hint::black_box(spun.wrapping_add(1));
if spun.is_multiple_of(4096) {
let moved = thread_cpu_nanos().is_some_and(|now| now > thread)
&& process_cpu_nanos().is_some_and(|now| now > process);
if moved {
break;
}
assert!(
started.elapsed() < Duration::from_secs(5),
"after 5 s of computing, the thread's CPU clock has not moved past \
{thread} ns or the process's past {process} ns"
);
}
}
}
}
@@ -133,27 +133,14 @@ impl HostPages {
/// `length` bytes of private anonymous memory, every byte `pattern`: `None` where the system
/// maps none.
///
/// `mmap` rather than an allocation, because a no-copy buffer's pointer and length have to be
/// the host's own pages — page-aligned and whole pages — which no allocator promises.
/// `mmap` (`VirtualAlloc` on Windows) rather than an allocation, because a no-copy buffer's
/// pointer and length have to be the host's own pages — page-aligned and whole pages — which
/// no allocator promises.
pub fn of(length: usize, pattern: u8) -> Option<Self> {
if length == 0 {
return None;
}
// SAFETY: a new private anonymous mapping, which reads no memory of ours.
let mapped = unsafe {
libc::mmap(
std::ptr::null_mut(),
length,
libc::PROT_READ | libc::PROT_WRITE,
libc::MAP_PRIVATE | libc::MAP_ANON,
-1,
0,
)
};
if mapped == libc::MAP_FAILED {
return None;
}
let pointer = NonNull::new(mapped.cast::<u8>())?;
let pointer = map(length)?;
// SAFETY: the mapping is this value's own, just made writable, and `length` bytes long.
unsafe { std::ptr::write_bytes(pointer.as_ptr(), pattern, length) };
Some(Self { pointer, length })
@@ -183,14 +170,61 @@ impl HostPages {
impl Drop for HostPages {
fn drop(&mut self) {
#[cfg(unix)]
// SAFETY: the mapping is this value's, and nothing reads it any more: the buffer over it
// was dropped first, because the field holding these is declared last in its side.
unsafe {
libc::munmap(self.pointer.as_ptr().cast(), self.length);
}
#[cfg(windows)]
// SAFETY: as above; a release frees the allocation whole, from its base, with a size of 0.
unsafe {
use windows_sys::Win32::System::Memory::{VirtualFree, MEM_RELEASE};
VirtualFree(self.pointer.as_ptr().cast(), 0, MEM_RELEASE);
}
}
}
/// `length` bytes of new private anonymous pages, read-write, at an address the kernel picks;
/// `None` where the system maps none.
#[cfg(unix)]
fn map(length: usize) -> Option<NonNull<u8>> {
// SAFETY: a new private anonymous mapping, which reads no memory of ours.
let mapped = unsafe {
libc::mmap(
std::ptr::null_mut(),
length,
libc::PROT_READ | libc::PROT_WRITE,
libc::MAP_PRIVATE | libc::MAP_ANON,
-1,
0,
)
};
if mapped == libc::MAP_FAILED {
return None;
}
NonNull::new(mapped.cast::<u8>())
}
/// `length` bytes of new private pages, read-write, at an address the system picks: a committed
/// allocation of their own (`VirtualAlloc`); `None` where the system makes none.
#[cfg(windows)]
fn map(length: usize) -> Option<NonNull<u8>> {
use windows_sys::Win32::System::Memory::{
VirtualAlloc, MEM_COMMIT, MEM_RESERVE, PAGE_READWRITE,
};
// SAFETY: a new allocation, which reads no memory of ours.
let mapped = unsafe {
VirtualAlloc(
std::ptr::null(),
length,
MEM_RESERVE | MEM_COMMIT,
PAGE_READWRITE,
)
};
NonNull::new(mapped.cast::<u8>())
}
#[cfg(test)]
mod tests {
use super::*;