Dynarec: Force interpreter on pages that undergo excessive invalidation, improves % on TES Daggerfall and potentially other self-modifying code

This commit is contained in:
RichardG867
2025-12-13 16:45:04 -03:00
parent d0583d62d6
commit 791daafceb
4 changed files with 31 additions and 9 deletions

View File

@@ -331,6 +331,7 @@ int scrnsz_y = SCREEN_RES_Y; /* current screen size, Y */
int config_changed; /* config has changed */
int title_update;
int framecountx = 0;
int seconds_elapsed = 0;
int hard_reset_pending = 0;
#if 0
@@ -1974,6 +1975,8 @@ pc_onesec(void)
framecount = 0;
title_update = 1;
seconds_elapsed++;
}
void

View File

@@ -404,14 +404,15 @@ exec386_dynarec_dyn(void)
# endif
int valid_block = 0;
# define INVALIDATION_LIMIT 100 /* amount of invalidations *per second* to kick a page to the interpreter */
page_t *page = &pages[phys_addr >> 12];
int can_recompile = (page->inv_count < INVALIDATION_LIMIT);
# ifdef USE_NEW_DYNAREC
if (!cpu_state.abrt)
if (!cpu_state.abrt && can_recompile)
# else
if (block && !cpu_state.abrt)
if (block && !cpu_state.abrt && can_recompile)
# endif
{
page_t *page = &pages[phys_addr >> 12];
/* Block must match current CS, PC, code segment size,
and physical address. The physical address check will
also catch any page faults at this stage */
@@ -445,16 +446,26 @@ exec386_dynarec_dyn(void)
if (valid_block && (block->page_mask & *block->dirty_mask)) {
# ifdef USE_NEW_DYNAREC
codegen_check_flush(page, page->dirty_mask, phys_addr);
if (block->pc == BLOCK_PC_INVALID)
if (block->pc == BLOCK_PC_INVALID) {
valid_block = 0;
else if (block->flags & CODEBLOCK_IN_DIRTY_LIST)
goto invalid_block;
} else if (block->flags & CODEBLOCK_IN_DIRTY_LIST) {
block->flags &= ~CODEBLOCK_WAS_RECOMPILED;
invalid_block:
# else
codegen_check_flush(page, page->dirty_mask[(phys_addr >> 10) & 3], phys_addr);
page->dirty_mask[(phys_addr >> 10) & 3] = 0;
if (!block->valid)
if (!block->valid) {
valid_block = 0;
# endif
if (page->inv_timestamp != seconds_elapsed) {
page->inv_timestamp = seconds_elapsed;
page->inv_count = 1;
} else if (++page->inv_count >= INVALIDATION_LIMIT) {
pclog("Forcing interpreter on page %08X\n", phys_addr & 0xfffff000);
can_recompile = 0;
}
}
}
if (valid_block && block->page_mask2) {
/* We don't want the second page to cause a page
@@ -653,7 +664,8 @@ exec386_dynarec_dyn(void)
cpu_block_end = 0;
x86_was_reset = 0;
codegen_block_init(phys_addr);
if (can_recompile)
codegen_block_init(phys_addr);
while (!cpu_block_end) {
# ifndef USE_NEW_DYNAREC
@@ -731,7 +743,7 @@ exec386_dynarec_dyn(void)
cpu_end_block_after_ins = 0;
if ((!cpu_state.abrt || (cpu_state.abrt & ABRT_EXPECTED)) && !new_ne && !x86_was_reset)
if ((!cpu_state.abrt || (cpu_state.abrt & ABRT_EXPECTED)) && !new_ne && !x86_was_reset && can_recompile)
codegen_block_end();
if (x86_was_reset)

View File

@@ -320,6 +320,7 @@ extern void do_pause(int p);
extern double isa_timing;
extern int io_delay;
extern int framecountx;
extern int seconds_elapsed;
extern volatile int cpu_thread_run;
extern uint8_t postcard_codes[POSTCARDS_NUM];

View File

@@ -224,6 +224,9 @@ typedef struct page_t {
uint64_t *byte_dirty_mask;
uint64_t *byte_code_present_mask;
uint32_t inv_count;
uint32_t inv_timestamp;
} page_t;
extern uint32_t purgable_page_list_head;
@@ -250,6 +253,9 @@ typedef struct _page_ {
/*Head of codeblock tree associated with this page*/
struct codeblock_t *head;
uint32_t inv_count;
uint32_t inv_timestamp;
} page_t;
#endif