Skip to content

Commit 776cb8a

Browse files
authored
Merge pull request 86Box#7602 from Cacodemon345/mmap-improvements
Report usage of large page allocations in the logs
2 parents d9bb222 + c257c1c commit 776cb8a

12 files changed

Lines changed: 67 additions & 23 deletions

‎src/codegen/codegen_x86-64.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,13 @@ static int last_ssegs;
6161
void
6262
codegen_init(void)
6363
{
64-
codeblock = plat_mmap(BLOCK_SIZE * sizeof(codeblock_t), 1);
65-
codeblock_hash = plat_mmap(HASH_SIZE * sizeof(codeblock_t *), 0);
64+
uint8_t large_block = 0, large_hash = 0;
65+
codeblock = plat_mmap(BLOCK_SIZE * sizeof(codeblock_t), 1, &large_block);
66+
codeblock_hash = plat_mmap(HASH_SIZE * sizeof(codeblock_t *), 0, &large_hash);
67+
if (large_block)
68+
pclog("Allocated %llu bytes of large pages for codeblocks\n", BLOCK_SIZE * sizeof(codeblock_t));
69+
if (large_hash)
70+
pclog("Allocated %llu bytes of large pages for codeblock hashes\n", HASH_SIZE * sizeof(codeblock_t));
6671

6772
memset(codeblock, 0, BLOCK_SIZE * sizeof(codeblock_t));
6873

‎src/codegen_new/codegen_allocator.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ int codegen_allocator_usage = 0;
8989
void
9090
codegen_allocator_init(void)
9191
{
92-
mem_block_alloc = plat_mmap(MEM_BLOCK_NR * MEM_BLOCK_SIZE, 1);
92+
uint8_t large = 0;
93+
mem_block_alloc = plat_mmap(MEM_BLOCK_NR * MEM_BLOCK_SIZE, 1, &large);
94+
95+
if (large)
96+
pclog("Allocated %llu bytes of large pages of recompiled code memory\n", MEM_BLOCK_NR * MEM_BLOCK_SIZE);
9397

9498
for (uint32_t c = 0; c < MEM_BLOCK_NR; c++) {
9599
mem_blocks[c].offset = c * MEM_BLOCK_SIZE;

‎src/codegen_new/codegen_backend_arm64.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,16 @@ void
285285
codegen_backend_init(void)
286286
{
287287
codeblock_t *block;
288+
uint8_t large_block = 0;
289+
uint8_t large_hash = 0;
288290

289-
codeblock = plat_mmap(BLOCK_SIZE * sizeof(codeblock_t), 0);
290-
codeblock_hash = plat_mmap(HASH_SIZE * sizeof(codeblock_t *), 0);
291+
codeblock = plat_mmap(BLOCK_SIZE * sizeof(codeblock_t), 0, &large_block);
292+
codeblock_hash = plat_mmap(HASH_SIZE * sizeof(codeblock_t *), 0, &large_hash);
293+
294+
if (large_block)
295+
pclog("Allocated %llu bytes of large pages for codeblock pointers\n", BLOCK_SIZE * sizeof(codeblock_t));
296+
if (large_hash)
297+
pclog("Allocated %llu bytes of large pages for codeblock hashes\n", HASH_SIZE * sizeof(codeblock_t));
291298

292299
for (int c = 0; c < BLOCK_SIZE; c++) {
293300
codeblock[c].valid = 0;

‎src/codegen_new/codegen_backend_x86-64.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,16 @@ codegen_backend_init(void)
293293
{
294294
codeblock_t *block;
295295
int c;
296+
uint8_t large_block = 0;
297+
uint8_t large_hash = 0;
296298

297-
codeblock = plat_mmap(BLOCK_SIZE * sizeof(codeblock_t), 0);
298-
codeblock_hash = plat_mmap(HASH_SIZE * sizeof(codeblock_t *), 0);
299+
codeblock = plat_mmap(BLOCK_SIZE * sizeof(codeblock_t), 0, &large_block);
300+
codeblock_hash = plat_mmap(HASH_SIZE * sizeof(codeblock_t *), 0, &large_hash);
301+
302+
if (large_block)
303+
pclog("Allocated %llu bytes of large pages for codeblock pointers\n", BLOCK_SIZE * sizeof(codeblock_t));
304+
if (large_hash)
305+
pclog("Allocated %llu bytes of large pages for codeblock hashes\n", HASH_SIZE * sizeof(codeblock_t));
299306

300307
for (c = 0; c < BLOCK_SIZE; c++)
301308
codeblock[c].valid = 0;

‎src/device/vfio.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2336,7 +2336,7 @@ vfio_region_init(vfio_device_t *dev, struct vfio_region_info *reg, vfio_region_t
23362336
region->bar_id = 0xff;
23372337

23382338
/* Allocate ROM shadow area. */
2339-
region->mmap_base = region->mmap_precalc = plat_mmap(region->size, 0);
2339+
region->mmap_base = region->mmap_precalc = plat_mmap(region->size, 0, NULL);
23402340
if (region->mmap_base == ((void *) -1)) {
23412341
pclog("VFIO %s: ROM mmap(%" PRIu64 ") failed\n", dev->name, region->size);
23422342
region->mmap_base = NULL;

‎src/include/86box/plat.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ extern void plat_init_asset_paths(void);
147147
extern int plat_dir_check(char *path);
148148
extern int plat_file_check(const char *path);
149149
extern int plat_dir_create(char *path);
150-
extern void *plat_mmap(size_t size, uint8_t executable);
150+
extern void *plat_mmap(size_t size, uint8_t executable, uint8_t* large);
151151
extern void plat_munmap(void *ptr, size_t size);
152152
extern uint64_t plat_timer_read(void);
153153
extern uint32_t plat_get_ticks(void);

‎src/include/86box/vid_voodoo_codegen_arm64.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4634,15 +4634,15 @@ voodoo_codegen_init(voodoo_t *voodoo)
46344634
voodoo_arm64_data_t *voodoo_arm64_data;
46354635
uint32_t slot;
46364636

4637-
voodoo->codegen_data = plat_mmap(sizeof(voodoo_arm64_data_t) * BLOCK_NUM * 4, 0);
4637+
voodoo->codegen_data = plat_mmap(sizeof(voodoo_arm64_data_t) * BLOCK_NUM * 4, 0, NULL);
46384638
if (!voodoo->codegen_data) {
46394639
fatal("ARM64 JIT: failed to allocate codegen metadata buffer\n");
46404640
}
46414641
voodoo_arm64_data = voodoo->codegen_data;
46424642
memset(voodoo_arm64_data, 0, sizeof(voodoo_arm64_data_t) * BLOCK_NUM * 4);
46434643

46444644
for (slot = 0; slot < (uint32_t) (BLOCK_NUM * 4); slot++) {
4645-
voodoo_arm64_data[slot].code_block = plat_mmap(BLOCK_SIZE, 1);
4645+
voodoo_arm64_data[slot].code_block = plat_mmap(BLOCK_SIZE, 1, NULL);
46464646
if (!voodoo_arm64_data[slot].code_block) {
46474647
while (slot > 0) {
46484648
slot--;

‎src/include/86box/vid_voodoo_codegen_x86-64.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3535,7 +3535,7 @@ voodoo_get_block(voodoo_t *voodoo, voodoo_params_t *params, voodoo_state_t *stat
35353535
void
35363536
voodoo_codegen_init(voodoo_t *voodoo)
35373537
{
3538-
voodoo->codegen_data = plat_mmap(sizeof(voodoo_x86_data_t) * BLOCK_NUM * 4, 1);
3538+
voodoo->codegen_data = plat_mmap(sizeof(voodoo_x86_data_t) * BLOCK_NUM * 4, 1, NULL);
35393539

35403540
for (uint16_t c = 0; c < 256; c++) {
35413541
int d[4];

‎src/mem/mem.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2733,6 +2733,7 @@ void
27332733
mem_reset(void)
27342734
{
27352735
size_t m;
2736+
uint8_t large_mem = 0;
27362737

27372738
memset(page_ff, 0xff, sizeof(page_ff));
27382739

@@ -2764,12 +2765,14 @@ mem_reset(void)
27642765

27652766
ram_size = m;
27662767
/* Allocate 16 extra bytes of RAM to mitigate some dynarec recompiler memory access quirks. */
2767-
ram = (uint8_t *) plat_mmap(ram_size + 16, 0); /* allocate and clear the RAM block */
2768+
ram = (uint8_t *) plat_mmap(ram_size + 16, 0, &large_mem); /* allocate and clear the RAM block */
27682769
if (ram == NULL) {
27692770
fatal("Failed to allocate RAM block. Make sure you have enough RAM available.\n");
27702771
return;
27712772
}
2772-
memset(ram, 0x00, ram_size + 16);
2773+
2774+
if (large_mem)
2775+
pclog("Allocated %.02lf megabytes of large pages for RAM\n", ram_size / (double)(1024 * 1024));
27732776

27742777
/*
27752778
* Allocate the page table based on how much RAM we have.

‎src/qt/qt_platform.cpp‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,10 @@ plat_remove(char *path)
618618
}
619619

620620
void *
621-
plat_mmap(size_t size, uint8_t executable)
621+
plat_mmap(size_t size, uint8_t executable, uint8_t* large)
622622
{
623+
if (large)
624+
*large = 0;
623625
#if defined Q_OS_WINDOWS
624626
static bool priv_tried = false;
625627
if (!priv_tried) {
@@ -639,8 +641,11 @@ plat_mmap(size_t size, uint8_t executable)
639641
if (lp) {
640642
const size_t rounded = (size + lp - 1) & ~(lp - 1);
641643
void* p = VirtualAlloc(nullptr, rounded, MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES, executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE);
642-
if (p)
644+
if (p) {
645+
if (large)
646+
*large = 1;
643647
return p;
648+
}
644649
}
645650
return VirtualAlloc(NULL, size, MEM_COMMIT, executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE);
646651
#elif defined Q_OS_UNIX
@@ -653,8 +658,11 @@ plat_mmap(size_t size, uint8_t executable)
653658
# else
654659
void *ret = mmap(0, size, PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0), MAP_ANON | MAP_PRIVATE, -1, 0);
655660
# ifdef MADV_HUGEPAGE
656-
if (ret)
657-
(void)madvise(ret, size, MADV_HUGEPAGE);
661+
if (ret && ret != MAP_FAILED) {
662+
if (large) {
663+
*large = !madvise(ret, size, MADV_HUGEPAGE);
664+
}
665+
}
658666
# endif
659667
# endif
660668
return (ret == MAP_FAILED) ? nullptr : ret;

0 commit comments

Comments
 (0)