Files
d2r-mobile/dm/host/tests/t_oe.c
T
2026-10-12 16:51:08 +08:00

175 lines
15 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// oe状态通知单测:不消费时同一类型只留首次排位;弹出后新变化重新排队。
// 三种消息仅通知「查询当前状态」,不携带历史值。期望顺序/状态手写,不由实现生成。
// oe state notification unit test: when not consumed, each type keeps only its first queue position; after a pop a new change is queued again.
// The three messages only signal "query the current state" and carry no historical value. Expected order/state are hand-written, not generated by the implementation.
#ifndef HLE_OE_SOURCE
#define HLE_OE_SOURCE "../hle_oe.c"
#endif
#include HLE_OE_SOURCE
#include <signal.h>
#include <sys/wait.h>
uint64_t now_ns(void) { return 0; }
// 3D 渲染比例(scale3d_tick)的外部依赖:单测里没有模块表与图形层,桩成「没有 main 模块」→ 该函数直接返回
// External dependencies of the 3D render scale (scale3d_tick): the unit test has no module table or graphics layer, so they are stubbed as "no main module" -> the function returns immediately
module_t g_mod[MAX_MOD]; int g_nmod;
void dm_guest_write_prepare(void *a, size_t n) { (void)a; (void)n; }
uint64_t dm_nvn_frame_count(void) { return 0; }
int dm_nvn_ruler_truth(void) { return 0; }
int dm_ingame_now(void) { return 1; }
int mtl_fps_target(void) { return 60; } // 目标帧率(nvn_mtl.m):单测里恒为 60,30 帧档的用例直接给 period_ms | target frame rate (nvn_mtl.m): always 60 in the unit test, and the 30 fps mode cases pass period_ms directly
pthread_key_t g_keyres[2]; // 分辨率参数位(loader.c)的桩:没建键,取走 31 时不写线程槽 | stub for the resolution parameter slot (loader.c): no key created, so taking 31 does not write the thread slot
uint64_t dm_game_res(int docked) { (void)docked; return 0; } // 出图分辨率(dm_uiscreen.c)的桩 | stub for the render resolution (dm_uiscreen.c)
void dm_uiscreen_res_reread(void) {}
static int checks, fails;
#define CHECK(c, s) do { checks++; int ok = !!(c); fails += !ok; printf("%s %s\n", ok ? "✓" : "✗", s); } while (0)
static void reset(void) { g_mqh = g_mqn = 0; g_focus = 1; g_opmode = 1; g_ntf_opmode = g_ntf_perfmode = 0; }
static int pop_is(uint32_t want) { uint32_t m = 999; return h_oe_TryPopMsg(&m) && m == want; }
static int empty(void) { uint32_t m = 999; return !h_oe_TryPopMsg(&m) && m == 999; }
static int child_kind;
static void abort_exit(int sig) { (void)sig; dprintf(2, "【红】t_oe child kind=%d:10 秒看门狗超时\n", child_kind); _exit(97); } // 子进程旧版die可核,避免产生崩溃报告或core | the old-version die in the child process can be checked, avoiding a crash report or core
static void *focus_burst(void *p) { (void)p; for (int i = 0; i < 17; i++) { dm_focus_set(2); dm_focus_set(1); } return NULL; }
static void *mode_burst(void *p) { (void)p; for (int i = 0; i < 17; i++) dm_set_opmode(i % 2); return NULL; }
static pthread_mutex_t stage_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t stage_cond = PTHREAD_COND_INITIALIZER;
static int stage, interleaved_ok;
static void stage_set(int n) { pthread_mutex_lock(&stage_lock); stage = n; pthread_cond_broadcast(&stage_cond); pthread_mutex_unlock(&stage_lock); }
static void stage_wait(int n) { pthread_mutex_lock(&stage_lock); while (stage < n) pthread_cond_wait(&stage_cond, &stage_lock); pthread_mutex_unlock(&stage_lock); }
static void *consume_interleaved(void *p) {
(void)p; stage_wait(1); int ok = pop_is(15); stage_set(2); // 已弹出,尚未查询:精确让生产者在此更新 | popped but not yet queried: let the producer update exactly here
stage_wait(3); interleaved_ok = ok && h_oe_GetFocus() == 1 && pop_is(15) && pop_is(30) && pop_is(31) && empty() && h_oe_GetOpMode() == 0 && h_oe_GetPerfMode() == 0;
return NULL;
}
static int child_burst(int kind) {
fflush(stdout); pid_t p = fork(); if (p < 0) return 0;
if (!p) {
child_kind = kind; signal(SIGABRT, abort_exit); signal(SIGALRM, abort_exit); alarm(10); reset();
g_ntf_opmode = g_ntf_perfmode = 1;
if (kind == 0) { focus_burst(NULL); _exit(h_oe_GetFocus() == 1 && pop_is(15) && empty() ? 0 : 1); }
if (kind == 1) { mode_burst(NULL); _exit(h_oe_GetOpMode() == 0 && h_oe_GetPerfMode() == 0 && pop_is(30) && pop_is(31) && empty() ? 0 : 1); }
if (kind == 3) {
pthread_t c; pthread_create(&c, NULL, consume_interleaved, NULL);
dm_focus_set(2); stage_set(1); stage_wait(2);
dm_focus_set(1); dm_set_opmode(0); stage_set(3); pthread_join(c, NULL);
_exit(interleaved_ok ? 0 : 1);
}
pthread_t f, m; pthread_create(&f, NULL, focus_burst, NULL); pthread_create(&m, NULL, mode_burst, NULL);
pthread_join(f, NULL); pthread_join(m, NULL);
uint32_t msg; int nf = 0, no = 0, np = 0, order = 1;
while (h_oe_TryPopMsg(&msg)) { if (msg == 15) nf++; else if (msg == 30) no++; else if (msg == 31) { order &= no == 1; np++; } else order = 0; }
_exit(nf == 1 && no == 1 && np == 1 && order && h_oe_GetFocus() == 1 && h_oe_GetOpMode() == 0 ? 0 : 1);
}
int st; if (waitpid(p, &st, 0) != p) return 0;
return WIFEXITED(st) && WEXITSTATUS(st) == 0;
}
// 动态 3D 比例控制器(scale3d_ctl_step,纯函数):期望值按规则手算,不由实现生成(规则见 hle_oe.c:2 秒窗帧率;连两窗 < 目标 ×0.92 降;连 10 窗 ≥ ×0.97 升;刚升 10 秒内掉 → 下次升要 30 窗)
// Dynamic 3D scale controller (scale3d_ctl_step, a pure function): expected values are computed by hand from the rules, not generated by the implementation (rules in hle_oe.c: frame rate over 2-second windows; two consecutive windows < target *0.92 steps down; 10 consecutive windows >= *0.97 steps up; a drop within 10 seconds of stepping up -> the next step-up needs 30 windows)
static uint64_t s3_now; static scale3d_ctl s3; static int s3_dm; // s3_dm:累计换级次数 | s3_dm: cumulative number of level changes
static int s3_one(double iv) { int b = s3.level; s3_now += (uint64_t)((iv > 0 ? iv : 16.6667) * 1e6); int lv = scale3d_ctl_step(&s3, iv, s3_now); s3_dm += lv != b; return lv; }
static int s3_n(int n, double iv) { int lv = s3.level; for (int i = 0; i < n; i++) lv = s3_one(iv); return lv; }
#define F60 16.6667 // 60 帧:每窗 120 帧(120 × 16.6667 = 2000.004 ms) | 60 fps: 120 frames per window (120 * 16.6667 = 2000.004 ms)
// 真机实玩第二段每 10 秒窗的「25 ms 档 / ≥33 ms 档」帧数(手抄自 perf 行)
// Frame counts of the "25 ms bucket / >=33 ms bucket" for each 10-second window of the second section of real play on a real device (copied by hand from perf lines)
static const int s3_rows[18][2] = {{0,1},{2,4},{3,3},{0,0},{10,12},{0,0},{0,0},{0,0},{2,1},{0,0},{6,8},{0,0},{0,0},{0,0},{2,3},{5,4},{0,0},{0,0}};
static int s3_replay(int clustered) { // clustered = 1:每 10 秒的慢帧挤在开头连续出现(最坏情况);0:均匀散开。返回换级次数 | clustered = 1: the slow frames of each 10 seconds are packed consecutively at the start (worst case); 0: spread evenly. Returns the number of level changes
s3 = (scale3d_ctl){.max_level = 2}; s3_now = 5000 * S3_SEC; s3_dm = 0;
for (int r = 0; r < 18; r++) { int ns = s3_rows[r][0] + s3_rows[r][1], every = ns ? 600 / ns : 0;
for (int i = 0; i < 600; i++) { int slow = clustered ? i < ns : (ns && i % every == 0 && i / every < ns); s3_one(slow ? (i % 2 ? 25.0 : 40.0) : F60); } }
return s3_dm;
}
static void test_scale3d(void) {
s3 = (scale3d_ctl){.max_level = 2}; s3_now = 1000 * S3_SEC;
CHECK(s3_n(1200, F60) == 0, "动态比例:60 帧稳 20 秒 → 保持 0 级");
CHECK(s3_n(199, 20.0) == 0, "动态比例:50 帧(< 55.2)第 1 个窗 + 第 2 个窗差 1 帧 → 还没降");
CHECK(s3_n(1, 20.0) == 1, "动态比例:连续两个 2 秒窗都 50 帧 → 降到 1 级");
CHECK(s3_n(100, 20.0) == 1, "动态比例:换级后 2 秒内的帧不计(照样 50 帧也不降)");
CHECK(s3_n(200, 20.0) == 2, "动态比例:停判结束后再连两窗 50 帧 → 降到 2 级");
CHECK(s3_n(1000, 25.0) == 2, "动态比例:已到下限,再掉也停在 2 级");
s3 = (scale3d_ctl){.max_level = 2, .level = 2}; s3_now = 1500 * S3_SEC; // 从干净的 2 级起测升档(不带上一段留在窗里的半个差窗) | test stepping up from a clean level 2 (without the half bad window the previous section left in the window)
CHECK(s3_n(1199, F60) == 2, "动态比例:60 帧连续 9 个窗多 → 还不升");
CHECK(s3_n(1, F60) == 1, "动态比例:连续 10 个窗(20 秒)≥ 58.2 → 升到 1 级");
s3 = (scale3d_ctl){.max_level = 2}; s3_now = 2000 * S3_SEC;
for (int k = 0; k < 10; k++) { s3_n(100, 20.0); s3_n(120, F60); }
CHECK(s3.level == 0, "动态比例:差窗与好窗交替(从不连两窗差)→ 不降");
s3 = (scale3d_ctl){.max_level = 2, .level = 1}; s3_now = 2500 * S3_SEC;
CHECK(s3_n(1200 * 3, 1000.0 / 57) == 1, "动态比例:57 帧(介于 55.2 与 58.2 之间)60 秒 → 不降也不升");
s3 = (scale3d_ctl){.max_level = 2}; s3_now = 3000 * S3_SEC;
s3_n(90, F60); s3_one(500.0); s3_n(120 * 3, F60);
CHECK(s3.level == 0, "动态比例:窗里一次 500 ms 长卡顿(该窗约 45 帧)、下一窗正常 → 不降(要连两窗)");
s3 = (scale3d_ctl){.max_level = 2}; s3_now = 3500 * S3_SEC; int lv = 0;
for (int i = 0; i < 2000; i++) lv = s3_one(i % 4 == 0 ? -1 : F60);
CHECK(lv == 0, "动态比例:被系统丢弃的帧(−1)不计数,其余照 60 帧 → 不降");
for (int i = 0; i < 2000; i++) lv = s3_one(0);
CHECK(lv == 0, "动态比例:全是无读数(0)的帧 → 不动");
// 防抖:刚升上去 10 秒内又被打回 → 下次升要 30 窗(60 秒)
// Debounce: knocked back down within 10 seconds of stepping up -> the next step-up needs 30 windows (60 seconds)
s3 = (scale3d_ctl){.max_level = 2, .level = 1}; s3_now = 4000 * S3_SEC;
CHECK(s3_n(1200, F60) == 0, "防抖:20 秒好窗 → 升到 0 级");
s3_n(120, F60); // 换级后停判 2 秒 | judging is suspended for 2 seconds after a level change
CHECK(s3_n(200, 20.0) == 1, "防抖:升上去后约 6 秒内连两窗 50 帧 → 打回 1 级");
s3_n(120, F60);
CHECK(s3_n(2400, F60) == 1, "防抖:刚升又掉,之后好窗 40 秒 → 不升(要 60 秒)");
CHECK(s3_n(1200, F60) == 0, "防抖:好窗满 60 秒 → 升到 0 级");
s3_n(120, F60); s3_n(120 * 6, F60); // 升上去 12 秒(> 10 秒)后才掉 | drops only 12 seconds (> 10 seconds) after stepping up
CHECK(s3_n(200, 20.0) == 1, "防抖:升上去 12 秒后才掉 → 降到 1 级");
s3_n(120, F60);
CHECK(s3_n(1200, F60) == 0, "防抖:不算刚升又掉,20 秒好窗即升回");
// 30 帧档:目标 30,降线 27.6、升线 29.1
// 30 fps mode: target 30, step-down line 27.6, step-up line 29.1
s3 = (scale3d_ctl){.max_level = 2, .period_ms = 1000.0 / 30}; s3_now = 6000 * S3_SEC;
CHECK(s3_n(600, 33.34) == 0, "30 帧档:33.34 ms(每窗 60 帧)稳 20 秒 → 保持 0 级");
CHECK(s3_n(100, 40.0) == 1, "30 帧档:25 帧(< 27.6)连两窗 → 降到 1 级");
// 注入:rds 局把一段帧的间隔当 25 ms(40 帧)
// Injection: the rds run treats the interval of a stretch of frames as 25 ms (40 fps)
s3 = (scale3d_ctl){.max_level = 2}; s3_now = 7000 * S3_SEC;
CHECK(s3_n(160, 25.0) == 1 && s3_n(80, 25.0) == 1 && s3_n(160, 25.0) == 2, "注入 25 ms:4 秒降到 1 级,停判 2 秒后再 4 秒降到 2 级");
s3_n(120, F60);
CHECK(s3_n(1200, F60) == 1 && (s3_n(120, F60), s3_n(1200, F60)) == 0, "注入结束:每 20 秒升一级,回到 0 级");
int a = s3_replay(0), b = s3_replay(1);
printf(" (实玩第二段回放:慢帧均匀散开换级 %d 次,挤在一起换级 %d 次;旧控制器真机上 12 次)\n", a, b);
CHECK(a == 0 && b == 0, "动态比例:实玩第二段的慢帧序列(帧率 59+)不论散开还是挤在一起都不换级");
}
int main(void) {
test_scale3d();
unsetenv("DM_OE_SCRIPT"); reset();
CHECK(empty(), "初始空队列:返回false且不改输出");
dm_focus_set(1); dm_set_opmode(1); CHECK(empty(), "相同状态不发通知");
dm_focus_set(3); CHECK(h_oe_GetFocus() == 3, "消费前即可查询后台状态3");
CHECK(pop_is(15) && empty(), "后台变化通知15恰一次");
dm_focus_set(2); dm_focus_set(1);
CHECK(pop_is(15) && empty() && h_oe_GetFocus() == 1, "未消费的失焦/恢复合并,查询最终焦点1");
reset(); g_ntf_opmode = g_ntf_perfmode = 1;
dm_focus_set(2); dm_set_opmode(0); dm_focus_set(3); dm_set_opmode(1);
CHECK(pop_is(15) && pop_is(30) && pop_is(31) && empty(), "三类首次排位FIFO=15,30,31,重复不挪位");
CHECK(h_oe_GetFocus() == 3 && h_oe_GetOpMode() == 1 && h_oe_GetPerfMode() == 1, "重复通知不保存陈旧值,查询最新3/1/1");
reset(); g_ntf_opmode = g_ntf_perfmode = 1;
dm_set_opmode(0); dm_focus_set(2); dm_set_opmode(1);
CHECK(pop_is(30) && pop_is(31) && pop_is(15) && empty(), "模式先变化时FIFO=30,31,15,成对顺序不变");
reset(); g_ntf_opmode = g_ntf_perfmode = 1;
dm_focus_set(2); CHECK(pop_is(15), "先弹出焦点通知");
dm_set_opmode(0); dm_focus_set(1);
CHECK(pop_is(30) && pop_is(31) && pop_is(15) && empty(), "弹出后的恢复再次通知并排在待取模式消息之后");
CHECK(h_oe_GetFocus() == 1, "恢复后的当前焦点=1");
reset(); dm_set_opmode(0);
CHECK(empty() && h_oe_GetOpMode() == 0, "通知关闭只更新状态、不入队");
h_oe_SetOpModeNtf(0x100); h_oe_SetPerfModeNtf(0x101); dm_set_opmode(1);
CHECK(pop_is(31) && empty(), "通知bool只读低8位,性能单开只发31");
h_oe_SetOpModeNtf(0x101); h_oe_SetPerfModeNtf(0x100); dm_set_opmode(0);
CHECK(pop_is(30) && empty(), "运行模式单开只发30");
reset(); mq_push(16); mq_push(16);
CHECK(pop_is(16) && pop_is(16) && empty(), "逐次事件16不合并,保留两次FIFO");
reset(); g_ntf_opmode = g_ntf_perfmode = 1; dm_set_opmode(0);
CHECK(pop_is(30), "模式对中先消费30,31尚未消费");
dm_set_opmode(1);
CHECK(pop_is(31) && pop_is(30) && empty() && h_oe_GetOpMode() == 1, "消费跨新变化:旧31保留排位,已弹30重新排队,最终模式1");
CHECK(child_burst(0), "34次焦点变化跨旧32槽,不终止且最终恢复1/一条15");
CHECK(child_burst(1), "17次模式变化跨旧32槽,不终止且最终0/30,31");
CHECK(child_burst(2), "两生产者并发,各类恰一条、30先于31、最终焦点1/模式0");
reset(); g_ntf_opmode = g_ntf_perfmode = 1; int rotated = 1;
for (int i = 0; i < 31; i++) { dm_focus_set(2 - i % 2); rotated &= pop_is(15) && empty(); }
CHECK(rotated, "31次逐条消费,把环头推进到尾槽");
dm_focus_set(1); dm_set_opmode(0); dm_focus_set(3); dm_set_opmode(1);
CHECK(pop_is(15) && pop_is(30) && pop_is(31) && empty(), "跨32槽回绕后仍按15,30,31去重并保持首次顺序");
CHECK(child_burst(3), "消费者弹出后、查询前发生新变化:新恢复不漏通知,最终焦点1/模式0");
printf("== oe %d 项,失败 %d ==\n", checks, fails); return fails != 0;
}