diff --git a/patches/README.md b/patches/README.md index c5014b5..358dbf2 100644 --- a/patches/README.md +++ b/patches/README.md @@ -20,6 +20,8 @@ | [`reims-vgpu-cargo-lock.patch`](reims-vgpu-cargo-lock.patch) | `reims-vgpu` (`Cargo.lock`) | 补齐 `metal2vulkan` 三个 crate 的 `source` 锁定信息,使源码包与实际 Cargo 构建逐字节一致 | 仅用于构建可复现;上游补齐 `Cargo.lock` 后删除 | | [`reims-vgpu-win32-unicode-input.patch`](reims-vgpu-win32-unicode-input.patch) | `reims-vgpu` (`crates/reims-vgpu-window`) | 通过 `SDL_SetWindowsMessageHook` 在设置 `REIMS_GO_INPUT_URL`(仅限 `127.0.0.1`)时转发非物理按键的 `WM_CHAR`/`WM_UNICHAR`/`WM_IME_CHAR`,解决 Windows IME 与中文输入被吞 | 待编入 `alpha` 冒烟验证后提 PR | | [`reims-ctl-win32-random-bytes.patch`](reims-ctl-win32-random-bytes.patch) | `reims-vgpu` (`crates/reims-ctl/src/vm.rs`) | 修复 Windows 下打不开 `/dev/urandom` 时回退到低熵时间戳种子的缺陷,改用标准库 `RandomState`(底层调用 `ProcessPrng`)生成 MAC/ROM 随机字节 | `git apply --check` 通过;待编入 `alpha` 验证后提 PR | +| [`reims-ctl-virtio-discard-iothread.patch`](reims-ctl-virtio-discard-iothread.patch) | `reims-vgpu` (`crates/reims-ctl/src/qemu.rs`) | 为 VirtIO 系统盘补齐 `discard=unmap`(与 NVMe 一致透传客体 TRIM)并启用独立 `-object iothread,id=io0`,避免块完成回调与 QEMU 主循环争抢 | `git apply --check` 在 `3731afd9` 与上游 `main` 均通过;真机实测与休眠唤醒通过,暂留 `patches/` 待编入 `alpha` 验证后提 PR | +| [`reims-ctl-qcow2-l2-cache.patch`](reims-ctl-qcow2-l2-cache.patch) | `reims-vgpu` (`crates/reims-ctl/src/qemu.rs`) | 按 `qcow2` 文件头虚拟容量与簇大小计算全覆盖所需 `l2-cache-size`(上限 32 MiB,仅大于 8 MiB 默认值时追加),避免非 Linux 宿主默认 8 MiB 上限仅覆盖 64 GiB | `git apply --check` 在 `3731afd9` 与上游 `main` 均通过;128 GiB 镜像真机实测通过,暂留 `patches/` 待编入 `alpha` 验证后提 PR | | [`qemu-whpx-win10-compat.patch`](qemu-whpx-win10-compat.patch) | `vendor/qemu` (`target/i386/whpx/whpx-all.c`) | 修复 Windows 10 19045 上两处 WHPX 必现崩溃(探测 TSC deadline 污染主分区、`WHvResetPartition` 未判空) | 已在 Win10 19045 真机验证并随稳定运行时发布;待提 PR | | [`qemu-whpx-win10-startup-ipis.patch`](qemu-whpx-win10-startup-ipis.patch) | `vendor/qemu` (`target/i386/whpx/whpx-all.c`) | 在 Win10 开启 `kernel-irqchip` 且多核启动时经 `WHvRequestInterrupt` 转发 INIT/SIPI,避免 XNU 从核卡死(依赖上一项) | 8/12 核冷启动通过;客体软重启待验证,待提 PR | | [`qemu-whpx-nested-virt-nonfatal.patch`](qemu-whpx-nested-virt-nonfatal.patch) | `vendor/qemu` (`target/i386/whpx/whpx-all.c`) | 部分 Win11 宿主设置嵌套虚拟化属性返回 `0x80370302` 时改为警告并清零特性位,保留 `kernel-irqchip=on` 加速(依赖前两项) | 待编入 `alpha` 并在 Win11 真机验证后提 PR | @@ -73,6 +75,24 @@ - **最小修复**:改用 `CreateFiberEx(64 * 1024, COROUTINE_STACK_SIZE, 0, ...)` 将初始提交降为 64 KiB;用 `IsThreadAFiber()` 复用已有 Fiber;对 `NULL` 打印 `GetLastError()` 并显式 `abort()`。 - **实测状态**:已通过 `git apply --check`,待编入 `alpha` 运行时真机验证。 +### 7. `reims-ctl-virtio-discard-iothread.patch` +- **建议 PR 标题**:`reims-ctl: enable discard=unmap and dedicated iothread on VirtIO system disk` +- **目标**:`steelbrain/reims-vgpu`(`crates/reims-ctl/src/qemu.rs`) +- **影响环境**:使用 VirtIO 系统盘控制器的 macOS 26 虚拟机(Windows WHPX 与 Linux KVM)。 +- **根因**:`q35()` 的 `DiskController::Virtio` 分支未追加 `discard=unmap`(导致客体 TRIM 被丢弃,与 NVMe 分支不一致),且块处理与图形/事件共享 QEMU 主循环。 +- **最小修复**:在 `DiskController::Virtio` 分支追加 `discard=unmap` 与 `-object iothread,id=io0`,并在 `virtio-blk-pci` 上设置 `iothread=io0`,同步更新同文件单元测试。 +- **实测证据**(Windows 10 19045 WHPX + i7-12700K + NVMe SSD + macOS 26.7.3,128 GiB / 54 GiB 已分配,每项 5 轮客体冷缓存 `sudo purge`): + - 消融对照(纯 VirtIO 无 `iothread` vs VirtIO + `iothread`):启用 `iothread` 使 `read-rand` 降 22.3%(297.8 → 231.5 ms)、`create-fullfsync` 降 14.6%(7,723 → 6,597 ms)、`rand-read-large` 降 14.2%(1,222 → 1,049 ms)、`inc-make` 降 11.5%(2,488 → 2,203 ms)。 + - 对比 NVMe 基线:`read-rand` 降 19.3%(286.9 → 231.5 ms)、`rand-read-large` 冷/热分别降 15.5% / 36.5%(1,242 → 1,049 ms / 1,624 → 1,032 ms)、`create-fullfsync` 降 7.5%(7,132 → 6,597 ms);休眠唤醒与校验写入通过。 + +### 8. `reims-ctl-qcow2-l2-cache.patch` +- **建议 PR 标题**:`reims-ctl: size qcow2 l2-cache-size from image header (up to 32 MiB)` +- **目标**:`steelbrain/reims-vgpu`(`crates/reims-ctl/src/qemu.rs`) +- **影响环境**:Windows / macOS 非 Linux 宿主上虚拟容量大于 64 GiB(64 KiB 簇)的 `qcow2` 系统盘。 +- **根因**:QEMU 在非 Linux 宿主将默认 L2 缓存上限设为 8 MiB(Linux 为 32 MiB),128 GiB 镜像全覆盖需 16 MiB,超出 64 GiB 范围的随机访问产生额外 L2 未命中。 +- **最小修复**:仅当磁盘为 `qcow2` 且本地可读有效头时,读取虚拟容量与 `cluster_bits` 计算 `virtual_size / cluster_size * 8` 向上取整至 MiB(上限 32 MiB),大于 8 MiB 时追加 `,l2-cache-size={mib}M`;不存在文件时静默忽略,不影响现有单元测试。 +- **实测证据**(128 GiB 镜像 16 MiB L2 缓存):`rand-read-large`(热缓存)QEMU 块层平均读延迟从 128.5 μs 降至 70.6 μs(-45.1%),端到端耗时从 1,623.6 ms 降至 1,498.7 ms(-7.7%),仅增加 8 MiB 内存占用且无稳定性风险。 + --- ## 关注中的上游 PR 与演进 diff --git a/patches/reims-ctl-qcow2-l2-cache.patch b/patches/reims-ctl-qcow2-l2-cache.patch new file mode 100644 index 0000000..9674588 --- /dev/null +++ b/patches/reims-ctl-qcow2-l2-cache.patch @@ -0,0 +1,52 @@ +diff --git a/crates/reims-ctl/src/qemu.rs b/crates/reims-ctl/src/qemu.rs +index 66c7952..67101dc 100644 +--- a/crates/reims-ctl/src/qemu.rs ++++ b/crates/reims-ctl/src/qemu.rs +@@ -195,6 +195,33 @@ pub fn opt(s: &str) -> String { + s.replace(',', ",,") + } + ++/// Compute the qcow2 L2 cache size in MiB needed to cover the full virtual disk ++/// (`virtual_size / cluster_size * 8` bytes), clamped to `..=32 MiB`, returning `Some(mib)` only ++/// when the image needs more than QEMU's 8 MiB non-Linux default. ++fn qcow2_l2_cache_mib(path: &Path) -> Option { ++ use std::io::Read; ++ let mut hdr = [0u8; 32]; ++ let mut f = std::fs::File::open(path).ok()?; ++ f.read_exact(&mut hdr).ok()?; ++ if &hdr[0..4] != b"QFI\xfb" { ++ return None; ++ } ++ let version = u32::from_be_bytes(hdr[4..8].try_into().ok()?); ++ if version < 2 || version > 3 { ++ return None; ++ } ++ let cluster_bits = u32::from_be_bytes(hdr[20..24].try_into().ok()?); ++ if !(9..=21).contains(&cluster_bits) { ++ return None; ++ } ++ let virtual_size = u64::from_be_bytes(hdr[24..32].try_into().ok()?); ++ let cluster_size = 1u64 << cluster_bits; ++ let clusters = virtual_size.div_ceil(cluster_size); ++ let l2_bytes = clusters.saturating_mul(8); ++ let mib = l2_bytes.div_ceil(1024 * 1024).min(32); ++ (mib > 8).then_some(mib) ++} ++ + fn optp(p: &Path) -> String { + opt(&p.to_string_lossy()) + } +@@ -378,6 +405,13 @@ fn q35(vm: &Vm, ctx: &Ctx, a: &mut Vec) -> Result<()> { + c.disk_format.as_str(), + optp(&vm.disk()) + ); ++ // QEMU caps default qcow2 L2 cache at 8 MiB on non-Linux hosts (covering only 64 GiB at 64 KiB ++ // clusters). Size l2-cache-size to cover the image's virtual capacity (capped at 32 MiB). ++ if matches!(c.disk_format, crate::vm::ImageFormat::Qcow2) { ++ if let Some(mib) = qcow2_l2_cache_mib(&vm.disk()) { ++ disk.push_str(&format!(",l2-cache-size={mib}M")); ++ } ++ } + // An install's flushes, which the module's doc measures, are dropped while the installer is + // attached. + if q.installer.is_some() { diff --git a/patches/reims-ctl-virtio-discard-iothread.patch b/patches/reims-ctl-virtio-discard-iothread.patch new file mode 100644 index 0000000..25d17db --- /dev/null +++ b/patches/reims-ctl-virtio-discard-iothread.patch @@ -0,0 +1,53 @@ +diff --git a/crates/reims-ctl/src/qemu.rs b/crates/reims-ctl/src/qemu.rs +index 4c80cfb..66c7952 100644 +--- a/crates/reims-ctl/src/qemu.rs ++++ b/crates/reims-ctl/src/qemu.rs +@@ -398,12 +398,15 @@ fn q35(vm: &Vm, ctx: &Ctx, a: &mut Vec) -> Result<()> { + "-device", + &format!("ide-hd,bus=sata.{},drive=disk,rotation_rate=1", q.disk_port), + ]), +- // SATA's drive options, at the first slot no other device takes. ++ // Pass guest TRIMs (`discard=unmap`) like NVMe and offload block handling to a dedicated ++ // iothread so disk completions do not contend with QEMU's main event loop. + DiskController::Virtio => push(&[ ++ "-object", ++ "iothread,id=io0", + "-drive", +- &disk, ++ &format!("{disk},discard=unmap"), + "-device", +- "virtio-blk-pci,drive=disk,addr=0x6", ++ "virtio-blk-pci,drive=disk,iothread=io0,addr=0x6", + ]), + } + // The installer, on port 4, past OpenCore's, or on 5 past an AHCI disk on 4. +@@ -1754,10 +1757,12 @@ pub(crate) mod tests { + assert!(has( + &a, + &[ ++ "-object", ++ "iothread,id=io0", + "-drive", +- "id=disk,if=none,format=qcow2,file=/vms/t/disk.qcow2", ++ "id=disk,if=none,format=qcow2,file=/vms/t/disk.qcow2,discard=unmap", + "-device", +- "virtio-blk-pci,drive=disk,addr=0x6", ++ "virtio-blk-pci,drive=disk,iothread=io0,addr=0x6", + ] + )); + assert!( +@@ -1782,10 +1787,12 @@ pub(crate) mod tests { + assert!(has( + &a, + &[ ++ "-object", ++ "iothread,id=io0", + "-drive", +- "id=disk,if=none,format=qcow2,file=/vms/t/disk.qcow2,cache.no-flush=on", ++ "id=disk,if=none,format=qcow2,file=/vms/t/disk.qcow2,cache.no-flush=on,discard=unmap", + "-device", +- "virtio-blk-pci,drive=disk,addr=0x6", ++ "virtio-blk-pci,drive=disk,iothread=io0,addr=0x6", + ] + )); + } diff --git a/patches/series.txt b/patches/series.txt index ef8f836..718e0c0 100644 --- a/patches/series.txt +++ b/patches/series.txt @@ -6,6 +6,8 @@ reims-vgpu reims-vgpu-cargo-lock.patch reims-vgpu reims-vgpu-win32-unicode-input.patch reims-vgpu reims-ctl-win32-random-bytes.patch +reims-vgpu reims-ctl-virtio-discard-iothread.patch +reims-vgpu reims-ctl-qcow2-l2-cache.patch vendor/qemu qemu-whpx-win10-compat.patch vendor/qemu qemu-whpx-win10-startup-ipis.patch vendor/qemu qemu-whpx-nested-virt-nonfatal.patch