Give the game's main heap more headroom; add heap readouts to the memory log line (#18)
The game carves out a fixed ~3.08 GB and its main heap was left with roughly 100-180 MB free on entering a game, down to a few tens of MB while changing maps. When an allocation fails there, the original code writes through the null result. The pool reported to the game is now 512 MB larger, which turns one-to-one into main heap headroom; untouched heap pages do not count toward resident memory. The periodic memory line now also logs the main heap total / free / largest allocatable block and, on iOS, the distance to the system memory limit. Measured in the iOS package on a Mac; devices with 6 GB of RAM have not been measured yet.
This commit is contained in:
@@ -906,7 +906,8 @@ static void dump_frame(void) {
|
||||
// Every 300 frames read resident memory and hand it to the graphics layer to print the memory readout line (mtl_mem_stat, low frequency, capped). Memory watchdog: when resident memory exceeds the cap, log a failure and exit on its own. On by default only on the Mac host (24 GB: runaway host memory
|
||||
// drags the whole machine into a kernel panic, which happened twice with other programs on the same machine), or set for tests with DM_MEM_CAP_GB=<integer>; the player's iOS app has no cap (exiting before the system kills it is pointless and only adds another kind of crash)
|
||||
if (g_frame % 300 == 0) { task_vm_info_data_t vi; mach_msg_type_number_t cnt = TASK_VM_INFO_COUNT;
|
||||
if (task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&vi, &cnt) == KERN_SUCCESS) { unsigned long long mb = (unsigned long long)(vi.phys_footprint >> 20); void mtl_mem_stat(unsigned long long, unsigned long long); mtl_mem_stat(g_frame, mb);
|
||||
if (task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&vi, &cnt) == KERN_SUCCESS) { unsigned long long mb = (unsigned long long)(vi.phys_footprint >> 20); unsigned long long ht = 0, hf = 0, hm = 0; int dm_guest_heap_stat(unsigned long long *, unsigned long long *, unsigned long long *); int hok = dm_guest_heap_stat(&ht, &hf, &hm);
|
||||
void mtl_mem_stat(unsigned long long, unsigned long long, int, unsigned long long, unsigned long long, unsigned long long); mtl_mem_stat(g_frame, mb, hok, ht, hf, hm);
|
||||
static long cap = -1; if (cap < 0) { const char *e = getenv("DM_MEM_CAP_GB"); cap = e ? atol(e) : DM_MEM_CAP_DEFAULT_GB; }
|
||||
if (cap > 0 && mb > (unsigned long long)cap * 1024) { LOG("【红】内存看门狗:常驻 %llu MB 超过上限 %ld GB(帧 %llu),自行退出", mb, cap, (unsigned long long)g_frame); _exit(86); } } }
|
||||
dm_guest_frame_tick(); // 确定性客体时钟(DM_DETCLOCK,宿主 hle_os.c):每次出图推进固定步长;未开为空操作 | Deterministic guest clock (DM_DETCLOCK, host hle_os.c): each present advances a fixed step; a no-op when not enabled
|
||||
|
||||
+21
-9
@@ -6,6 +6,10 @@
|
||||
#import <ImageIO/ImageIO.h>
|
||||
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
|
||||
#include <dlfcn.h>
|
||||
#include <TargetConditionals.h>
|
||||
#if TARGET_OS_IPHONE
|
||||
#include <os/proc.h>
|
||||
#endif
|
||||
#include <sys/stat.h>
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
@@ -1215,20 +1219,27 @@ static id<MTLTexture> tex_mtl(const tex_t *ct) {
|
||||
serial_put(t->serial, mt);
|
||||
return mt;
|
||||
}
|
||||
// 内存读数行(常开、低频、封顶):常驻内存、显卡已分配、延迟释放队列、驻留集对象数、在册内存池数 / 峰值 / 跟踪表容量、贴图重传累计。
|
||||
// 内存读数行(常开、低频、封顶):常驻内存、显卡已分配、延迟释放队列、驻留集对象数、在册内存池数 / 峰值 / 跟踪表容量、贴图重传累计、游戏堆总量 / 余量 / 最大可分配块(宿主 dm_guest_heap_stat)、离系统内存上限还有多少(仅 iOS)。
|
||||
// 用途:定「没有日志的闪退」——系统按内存杀进程前不给任何通知,只有事先留下的读数能说明是哪个数一路往上涨。
|
||||
// 每 300 帧由帧钩子调一次;常驻或显卡已分配比上一行变了 ≥ 64 MB 才打,否则每 3000 帧打一行;一局封顶 400 行。全是整数(帧钩子在客体线程的小栈上,不用浮点格式)
|
||||
// Memory readout line (always on, low frequency, capped): resident memory, GPU allocated, deferred-release queue, residency set object count, registered memory pools / peak / tracking table capacity, cumulative texture re-uploads.
|
||||
// Purpose: pin down "crashes with no log". The system gives no notice before killing a process for memory, so only readouts left beforehand can show which number kept climbing.
|
||||
// Called by the frame hook every 300 frames; printed only when resident or GPU allocated changed by ≥ 64 MB since the last line, otherwise one line every 3000 frames; capped at 400 lines per game. All integers (the frame hook runs on the guest thread's small stack, so no floating-point formatting)
|
||||
void mtl_mem_stat(unsigned long long frame, unsigned long long rss_mb) {
|
||||
static unsigned long long last_rss, last_gpu, last_frame; static int lines; unsigned long long gpu = (unsigned long long)(g_dev.currentAllocatedSize >> 20);
|
||||
// 每 300 帧由帧钩子调一次;常驻、显卡已分配变了 ≥ 64 MB,或游戏堆余量、最大可分配块变了 ≥ 16 MB(主堆平时只剩约 100 MB)才打,否则每 3000 帧打一行;一局封顶 400 行。全是整数(帧钩子在客体线程的小栈上,不用浮点格式)
|
||||
// Memory readout line (always on, low frequency, capped): resident memory, GPU allocated, deferred release queue, residency set object count, registered memory pool count / peak / tracking table capacity, cumulative texture re-uploads, game heap total / free / largest allocatable block (host dm_guest_heap_stat), and how far from the system memory limit (iOS only).
|
||||
// Purpose: to pin down "crashes with no log" -- the system gives no notice before killing a process for memory, and only readouts left beforehand can show which number kept climbing.
|
||||
// Called by the frame hook every 300 frames; printed when resident or GPU allocated changed by >= 64 MB, or game heap free space or largest allocatable block changed by >= 16 MB (the main heap normally has only about 100 MB left), otherwise one line every 3000 frames; capped at 400 lines per run. All integers (the frame hook runs on the small stack of a guest thread, so no floating-point formatting)
|
||||
void mtl_mem_stat(unsigned long long frame, unsigned long long rss_mb, int heap_ok, unsigned long long heap_mb, unsigned long long heap_free_mb, unsigned long long heap_maxblk_kb) {
|
||||
static unsigned long long last_rss, last_gpu, last_frame, last_hfree, last_hmax; static int lines; unsigned long long gpu = (unsigned long long)(g_dev.currentAllocatedSize >> 20);
|
||||
unsigned long long dr = rss_mb > last_rss ? rss_mb - last_rss : last_rss - rss_mb, dg = gpu > last_gpu ? gpu - last_gpu : last_gpu - gpu;
|
||||
if (lines >= 400 || (lines && dr < 64 && dg < 64 && frame - last_frame < 3000)) return; lines++; last_rss = rss_mb; last_gpu = gpu; last_frame = frame;
|
||||
unsigned long long dh = heap_free_mb > last_hfree ? heap_free_mb - last_hfree : last_hfree - heap_free_mb, hmax_mb = heap_maxblk_kb >> 10, dm = hmax_mb > last_hmax ? hmax_mb - last_hmax : last_hmax - hmax_mb;
|
||||
static int all = -1; if (all < 0) all = getenv("DM_MEM_STAT_ALL") != NULL; // 量测用:每次都打、不封顶 | For measurement: printed every time, uncapped
|
||||
if (!all && (lines >= 400 || (lines && dr < 64 && dg < 64 && dh < 16 && dm < 16 && frame - last_frame < 3000))) return; lines++; last_rss = rss_mb; last_gpu = gpu; last_frame = frame; last_hfree = heap_free_mb; last_hmax = hmax_mb;
|
||||
pthread_mutex_lock(&g_deferred_mu); unsigned long nd = (unsigned long)g_deferred.count; pthread_mutex_unlock(&g_deferred_mu);
|
||||
pthread_mutex_lock(&g_res_mu); unsigned long nr = (unsigned long)g_res_objects.count; pthread_mutex_unlock(&g_res_mu);
|
||||
int live = 0, peak = 0, cap = 0; nvn_dirty_stat(&live, &peak, &cap);
|
||||
MLOG("内存 至帧 %llu:常驻 %llu MB,显卡已分配 %llu MB,延迟释放队列 %lu,驻留集 %lu 个,内存池 %d 个(峰值 %d / 跟踪表容量 %d),贴图重传累计 %llu 次", frame, rss_mb, gpu, nd, nr, live, peak, cap, (unsigned long long)g_pkg_reuploads);
|
||||
char avail[32] = "无"; // os_proc_available_memory:本进程离被系统按内存终止还差多少,只有 iOS 有;Mac 宿主写「无」 | os_proc_available_memory: how far this process is from being terminated by the system for memory, iOS only; the Mac host writes "none"
|
||||
#if TARGET_OS_IPHONE
|
||||
snprintf(avail, sizeof avail, "%llu MB", (unsigned long long)(os_proc_available_memory() >> 20));
|
||||
#endif
|
||||
MLOG("内存 至帧 %llu:常驻 %llu MB,显卡已分配 %llu MB,延迟释放队列 %lu,驻留集 %lu 个,内存池 %d 个(峰值 %d / 跟踪表容量 %d),贴图重传累计 %llu 次;游戏堆 %llu MB,余量 %llu MB,最大可分配块 %llu KB%s;离系统内存上限还有 %s", frame, rss_mb, gpu, nd, nr, live, peak, cap, (unsigned long long)g_pkg_reuploads,
|
||||
heap_mb, heap_free_mb, heap_maxblk_kb, heap_ok ? "" : "(未读到)", avail);
|
||||
}
|
||||
void mtl_pkg_stats(uint64_t *uploads, uint64_t *reuploads) { *uploads = g_pkg_uploads; *reuploads = g_pkg_reuploads; }
|
||||
uint64_t mtl_pkg_miss(void) { return g_pkg_miss; }
|
||||
@@ -1305,6 +1316,7 @@ static void blit_note(void) { if (g_enc) PD("E %llu 拷贝 / 缓冲更新结束
|
||||
// For intersecting with the inflate output ranges (which data decompressed before character select is really used by the character select screen). Columns: kind guest_addr len first_frame tex_id fmt w×h
|
||||
__attribute__((weak)) uint64_t dm_prof_ribbon_ns(void) { return 0; } // diag.c 提供强定义;未合入时恒 0 → 时间窗不开,结束时报红 | diag.c provides the strong definition; when not merged it is always 0 → the time window does not open, and a failure is reported at the end
|
||||
__attribute__((weak)) void dm_shot_written(const char *path, int ok) { (void)path; (void)ok; }
|
||||
__attribute__((weak)) int dm_guest_heap_stat(unsigned long long *t, unsigned long long *f, unsigned long long *m) { (void)t; (void)f; (void)m; return 0; } // hle_svc.c 提供强定义(游戏主堆读数);图形单测不链宿主时读不到 | hle_svc.c provides the strong definition (game main heap readout); the graphics unit tests do not link the host and cannot read it
|
||||
__attribute__((weak)) void dm_guest_frame_tick(void) {} // hle_os.c 提供强定义(DM_DETCLOCK 确定性客体时钟);图形单测不链宿主时为空操作 // diag.c 提供强定义(DM_TICK_HOLD=E:SHOT 解冻);图形单测不链宿主时为空操作 | hle_os.c provides the strong definition (DM_DETCLOCK deterministic guest clock); a no-op when graphics unit tests do not link the host // diag.c provides the strong definition (DM_TICK_HOLD=E:SHOT unfreeze); a no-op when graphics unit tests do not link the host
|
||||
__attribute__((weak)) int dm_detclock_enabled(void) { return 0; } // 同上:宿主 hle_os.c 提供强定义;图形单测不链宿主时确定性时钟视为关闭 | Same as above: the host's hle_os.c provides the strong definition; when graphics unit tests do not link the host, the deterministic clock is treated as off
|
||||
__attribute__((weak)) uint64_t guest_ns(void) { return 0; } // 同上:仅在确定性时钟开启时被读,图形单测里不会走到 | Same as above: read only when the deterministic clock is on; never reached in graphics unit tests
|
||||
|
||||
Reference in New Issue
Block a user