157 lines
12 KiB
C
157 lines
12 KiB
C
// 客体内存写保护脏页跟踪:预排布贴图(CPU 写、GPU 读)的池页上传后设只读,游戏一写即缺页 → 标脏、解保护;
|
|
// 使用前查覆盖页的写代号,比上传时新就重传。NVN 语义:贴图描述符指向池内存,GPU 每次读的是当前内容
|
|
// (实测游戏会把流式素材写进仍被采样的贴图存储,不重新登记)。
|
|
// 信号处理约束:缺页路径只用原子操作与 mprotect,不分配、不加锁。
|
|
// Write-protect dirty-page tracking for guest memory: pool pages of packaged textures (CPU writes, GPU reads) are set read-only after upload, and as soon as the game writes it faults -> mark dirty, unprotect;
|
|
// before use, check the write generation of the covered pages and re-upload if newer than at upload time. NVN semantics: a texture descriptor points to pool memory, and the GPU reads the current content every time
|
|
// (measured: the game writes streamed assets into texture storage that is still being sampled, without re-registering).
|
|
// Signal handler constraint: the page-fault path uses only atomic operations and mprotect, no allocation, no locks.
|
|
#include "nvn_dirty.h"
|
|
#include "../../host/dm.h"
|
|
#include <stdatomic.h>
|
|
#include <pthread.h>
|
|
#include <sys/mman.h>
|
|
#include <unistd.h>
|
|
#include <mach/mach_time.h>
|
|
|
|
enum { PG_NONE = 0, PG_PROT = 1, PG_BUSY = 2 }; // BUSY:正在解除保护(另一线程同时缺页时自旋等它) | BUSY: being unprotected (another thread faulting at the same time spins waiting for it)
|
|
// 跟踪区间 = 各内存池(MemoryPoolInitialize 登记)。池销毁后复用 live=0 的槽,信号处理里无锁扫描。
|
|
// Tracked ranges = the memory pools (registered by MemoryPoolInitialize). After a pool is destroyed its live=0 slot is reused; the signal handler scans without locks.
|
|
#define MAX_RANGE 4096 // 原为 256:内存池多于它时超出的池不跟踪,里面的贴图每次取用都当成脏的重传(暗黑实测峰值见内存读数行的「内存池」;塞尔达约 300 个,踩过)。扫描只到已用的槽数,容量大不增加开销 | was 256: with more memory pools than that the excess pools are untracked and their textures are treated as dirty and re-uploaded on every use (for Diablo's measured peak see "memory pools" in the memory readout line; Zelda has about 300 and hit this). The scan only covers slots in use, so a larger capacity costs nothing
|
|
typedef struct { uintptr_t base, end; _Atomic uint8_t *state; _Atomic uint64_t *gen; size_t cap; _Atomic int live; } range_t;
|
|
static range_t g_r[MAX_RANGE];
|
|
static _Atomic int g_nr;
|
|
static uintptr_t g_pg, g_shift;
|
|
static _Atomic uint64_t g_now = 1;
|
|
static _Atomic uint64_t g_faults, g_preps;
|
|
static _Atomic uint64_t g_adds_total;
|
|
static _Atomic int g_live, g_live_peak; // 在册的池数与峰值(内存读数行用) | number of registered pools and peak (for the memory readout line)
|
|
void nvn_dirty_stat(int *live, int *peak, int *cap) { *live = atomic_load(&g_live); *peak = atomic_load(&g_live_peak); *cap = MAX_RANGE; }
|
|
static _Atomic int g_overflow_logged;
|
|
static pthread_mutex_t g_slot_mu = PTHREAD_MUTEX_INITIALIZER; // 建池 / 销池(客体线程,可能并发)互斥选槽;缺页处理路径不碰它 | Pool creation / destruction (guest threads, possibly concurrent) pick slots under a mutex; the page-fault handling path does not touch it
|
|
static _Atomic uint64_t g_mp_calls, g_mp_pages, g_mp_ticks; // 本模块的 mprotect:调用次数、覆盖页数、耗时(mach 时基,缺页路径里也安全) | This module's mprotect: call count, pages covered, time spent (mach timebase, safe in the page-fault path too)
|
|
static void mp(uintptr_t a, size_t len, int prot) {
|
|
uint64_t t = mach_absolute_time(); mprotect((void *)a, len, prot);
|
|
atomic_fetch_add(&g_mp_ticks, mach_absolute_time() - t); atomic_fetch_add(&g_mp_calls, 1); atomic_fetch_add(&g_mp_pages, len >> g_shift);
|
|
}
|
|
|
|
static range_t *find(uintptr_t a) {
|
|
int n = atomic_load(&g_nr);
|
|
for (int i = 0; i < n; i++) if (atomic_load_explicit(&g_r[i].live, memory_order_acquire) && a >= g_r[i].base && a < g_r[i].end) return &g_r[i];
|
|
return NULL;
|
|
}
|
|
static void unprotect_page(range_t *r, uintptr_t p) { // 调用方已把状态从 PROT 改成 BUSY | The caller has already changed the state from PROT to BUSY
|
|
mp(r->base + (p << g_shift), g_pg, PROT_READ | PROT_WRITE);
|
|
atomic_store(&r->gen[p], atomic_fetch_add(&g_now, 1) + 1);
|
|
atomic_store(&r->state[p], PG_NONE);
|
|
}
|
|
static int on_fault(void *addr, int is_write, void *uc) {
|
|
(void)uc;
|
|
range_t *r = is_write ? find((uintptr_t)addr) : NULL;
|
|
if (!r) return 0;
|
|
uintptr_t p = ((uintptr_t)addr - r->base) >> g_shift;
|
|
uint8_t st = PG_PROT;
|
|
if (atomic_compare_exchange_strong(&r->state[p], &st, PG_BUSY)) { unprotect_page(r, p); atomic_fetch_add(&g_faults, 1); return 1; }
|
|
if (st == PG_BUSY) { while (atomic_load(&r->state[p]) == PG_BUSY) {} return 1; } // 别的线程正在解:等它解完重执行 | Another thread is unprotecting it: wait for it to finish and re-execute
|
|
return atomic_load(&r->gen[p]) != 0; // 刚被别的线程解掉(本次缺页发生在解之前):重执行;从未保护过的页不是我们的 | Just unprotected by another thread (this fault happened before the unprotect): re-execute; pages that were never protected are not ours
|
|
}
|
|
static void prep_write(void *addr, size_t len) {
|
|
uintptr_t a = (uintptr_t)addr, e = a + len;
|
|
while (len && a < e) {
|
|
range_t *r = find(a);
|
|
if (!r) { a = (a + g_pg) & ~(g_pg - 1); continue; }
|
|
uintptr_t stop = e < r->end ? e : r->end;
|
|
for (uintptr_t p = (a - r->base) >> g_shift, q = (stop - 1 - r->base) >> g_shift; p <= q; p++) {
|
|
uint8_t st = PG_PROT;
|
|
if (atomic_compare_exchange_strong(&r->state[p], &st, PG_BUSY)) { unprotect_page(r, p); atomic_fetch_add(&g_preps, 1); }
|
|
else while (atomic_load(&r->state[p]) == PG_BUSY) {}
|
|
}
|
|
a = stop;
|
|
}
|
|
}
|
|
|
|
void nvn_dirty_add_range(const void *mem, size_t size) {
|
|
static int inited;
|
|
if (!inited) { inited = 1; g_pg = (uintptr_t)getpagesize(); g_shift = __builtin_ctzl(g_pg); dm_fault_hook_register(on_fault); dm_guest_write_prep_register(prep_write); }
|
|
atomic_fetch_add(&g_adds_total, 1);
|
|
pthread_mutex_lock(&g_slot_mu);
|
|
int n = atomic_load(&g_nr);
|
|
int slot = -1;
|
|
for (int i = 0; i < n; i++) if (!atomic_load_explicit(&g_r[i].live, memory_order_acquire)) { slot = i; break; }
|
|
if (slot < 0) {
|
|
if (n >= MAX_RANGE) {
|
|
int expected = 0;
|
|
if (atomic_compare_exchange_strong(&g_overflow_logged, &expected, 1)) LOG("【红】脏页跟踪区间已满(%d),池 %p 不跟踪", MAX_RANGE, mem);
|
|
pthread_mutex_unlock(&g_slot_mu); return;
|
|
}
|
|
slot = n;
|
|
}
|
|
{ int lv = atomic_fetch_add(&g_live, 1) + 1, pk = atomic_load(&g_live_peak); while (lv > pk && !atomic_compare_exchange_weak(&g_live_peak, &pk, lv)) {} }
|
|
range_t *r = &g_r[slot];
|
|
uintptr_t base = (uintptr_t)mem & ~(g_pg - 1), end = ((uintptr_t)mem + size + g_pg - 1) & ~(g_pg - 1);
|
|
size_t np = (end - base) >> g_shift;
|
|
_Atomic uint8_t *state = r->state;
|
|
_Atomic uint64_t *gen = r->gen;
|
|
if (r->cap < np) {
|
|
state = calloc(np, 1); gen = calloc(np, sizeof *gen);
|
|
if (!state || !gen) die("脏页表分配失败(%zu 页)", np);
|
|
// 旧数组不 free:信号处理里可能仍有极短暂的陈旧读者,泄漏量 = 每页 9 字节。
|
|
// The old array is not freed: the signal handler may still have a very brief stale reader; leak = 9 bytes per page.
|
|
r->cap = np;
|
|
} else {
|
|
memset((void *)state, 0, np); memset((void *)gen, 0, np * sizeof *gen);
|
|
}
|
|
r->base = base; r->end = end; r->state = state; r->gen = gen;
|
|
if (slot == n) atomic_store(&g_nr, n + 1);
|
|
atomic_store_explicit(&r->live, 1, memory_order_release);
|
|
pthread_mutex_unlock(&g_slot_mu);
|
|
}
|
|
// 池销毁:先把仍受保护的页还原成可写(池内存归还游戏后可能另作他用),再标失效
|
|
// Pool destruction: first restore still-protected pages to writable (the pool memory may be reused for something else once returned to the game), then mark invalid
|
|
void nvn_dirty_remove_range(const void *mem) {
|
|
pthread_mutex_lock(&g_slot_mu);
|
|
range_t *r = find((uintptr_t)mem);
|
|
if (r) { prep_write((void *)r->base, r->end - r->base); atomic_store_explicit(&r->live, 0, memory_order_release); atomic_fetch_sub(&g_live, 1); }
|
|
pthread_mutex_unlock(&g_slot_mu);
|
|
}
|
|
int nvn_dirty_tracked(const void *addr, size_t len) {
|
|
range_t *r = find((uintptr_t)addr); return r && (uintptr_t)addr + len <= r->end;
|
|
}
|
|
// 设只读:先置状态再 mprotect(反过来会有「页已只读、状态仍为 0」的窗口,那时的合法写入会被当成崩溃)
|
|
// Set read-only: set the state first, then mprotect (the reverse leaves a window of "page already read-only, state still 0", during which a legitimate write would be treated as a crash)
|
|
void nvn_dirty_protect(const void *addr, size_t len) {
|
|
if (!len || !nvn_dirty_tracked(addr, len)) return;
|
|
range_t *r = find((uintptr_t)addr);
|
|
uintptr_t a = (uintptr_t)addr, p = (a - r->base) >> g_shift, q = (a + len - 1 - r->base) >> g_shift;
|
|
// 连续置成 PROT 的页合成一次 mprotect(逐页调用在首传大贴图时一帧上千次;选角页启动帧 5:4888 次)。
|
|
// 已是 PROT / BUSY 的页断开连续段:不重复保护,也不碰别的线程正在解的页
|
|
// Consecutive pages set to PROT are merged into one mprotect (per-page calls reach thousands per frame on the first upload of large textures; character select page startup frame 5: 4888 calls).
|
|
// Pages already PROT / BUSY break the consecutive run: do not protect again, and do not touch pages another thread is unprotecting
|
|
uintptr_t run = 0; int open = 0;
|
|
for (uintptr_t i = p; i <= q + 1; i++) {
|
|
uint8_t st = PG_NONE;
|
|
int took = i <= q && atomic_compare_exchange_strong(&r->state[i], &st, PG_PROT);
|
|
if (took && !open) { run = i; open = 1; }
|
|
if (!took && open) { mp(r->base + (run << g_shift), (i - run) << g_shift, PROT_READ); open = 0; }
|
|
}
|
|
}
|
|
uint64_t nvn_dirty_now(void) { return atomic_load(&g_now); }
|
|
int nvn_dirty_since(const void *addr, size_t len, uint64_t stamp) {
|
|
if (!len) return 0;
|
|
if (!nvn_dirty_tracked(addr, len)) return 1; // 未跟踪时保守视为脏,宁可多传也不显示旧内容 | When untracked, conservatively treat as dirty: better to upload more than to show stale content
|
|
range_t *r = find((uintptr_t)addr);
|
|
uintptr_t a = (uintptr_t)addr, p = (a - r->base) >> g_shift, q = (a + len - 1 - r->base) >> g_shift;
|
|
for (uintptr_t i = p; i <= q; i++) if (atomic_load(&r->gen[i]) > stamp) return 1; // 上传前最后一次写的代号 ≤ 戳;之后的写必 > 戳 | The generation of the last write before upload is <= the stamp; any later write must be > the stamp
|
|
return 0;
|
|
}
|
|
void nvn_dirty_slot_stats(int *live, int *used_slots, uint64_t *adds_total) {
|
|
int n = atomic_load(&g_nr), active = 0;
|
|
for (int i = 0; i < n; i++) active += atomic_load_explicit(&g_r[i].live, memory_order_acquire) != 0;
|
|
*live = active; *used_slots = n; *adds_total = atomic_load(&g_adds_total);
|
|
}
|
|
void nvn_dirty_stats(uint64_t *faults, uint64_t *preps) { *faults = atomic_load(&g_faults); *preps = atomic_load(&g_preps); }
|
|
void nvn_dirty_mp_stats(uint64_t *calls, uint64_t *pages, uint64_t *ns) {
|
|
static mach_timebase_info_data_t tb; if (!tb.denom) mach_timebase_info(&tb);
|
|
*calls = atomic_load(&g_mp_calls); *pages = atomic_load(&g_mp_pages); *ns = atomic_load(&g_mp_ticks) * tb.numer / tb.denom;
|
|
}
|