Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JIT heap free spaces checks to EE JIT #332

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/core/ee/ee_jit64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ uint8_t* exec_block_ee(EE_JIT64& jit, EmotionEngine& ee)

uint16_t EE_JIT64::run(EmotionEngine& ee)
{
//Need to check we have space on the heap before running any recompiled code
//Resetting the heap during recompilation wipes out the prologue/epilogue!
//Checks for maximum 5mb block size of space before continuing
if (!jit_heap.has_free_space(1024 * 1024 * 5))
Copy link
Owner

@PSI-Rockin PSI-Rockin Mar 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this is the best place to make the JIT space check, especially considering how expensive the check is. We already have a flush_jit_cache variable that gets checked when EmotionEngine::run_jit is called.

Since only recompilation can decrease free space, a better course of action would be to do the space check inside the JIT's recompile_block method. When the check fails, we can set the flush_jit_cache variable in EmotionEngine, which will not only prevent the prologue/epilogue blocks from being trashed but also provide a speed boost compared to the current method in this PR.

{
printf("[EE JIT] Not enough room for new blocks, clearing cache\n");
jit_heap.flush_all_blocks();
create_prologue_block();
}

prologue_block(*this, ee, &jit_heap.lookup_cache[0]);

return cycle_count;
Expand Down
37 changes: 36 additions & 1 deletion src/core/jitcommon/jitcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ void EEJitHeap::jit_free(void *ptr)
void* EEJitHeap::jit_malloc(std::size_t size)
{
size = std::max(get_min_bin_size(0), aligned_size(size));
if(size < sizeof(FreeList)) size = sizeof(FreeList);
size = std::max(sizeof(FreeList), size);
void* mem = find_memory(size);
if (mem)
heap_usage += *get_size_ptr(mem); // this is the aligned size. for tiny blocks they use
Expand Down Expand Up @@ -566,6 +566,41 @@ EEJitBlockRecord* EEJitHeap::find_block(uint32_t PC)
return nullptr;
}

// TODO: For just determining if there's enough space left to hold a block of a specific size in the heap,
// is there a faster solution? I'm ignorant as to this heap implementation - Souzooka
// We'll also probably, if possible, remove free space checks from EEJitHeap and
// JitUnorderedMapHeap and move it into JitHeap if possible.
bool EEJitHeap::has_free_space(std::size_t size)
{
// find smallest bin which contains chunks large enough for our object
uint8_t bin = JIT_ALLOC_BINS;
for (uint8_t i = 0; i < JIT_ALLOC_BINS; i++) {
if (get_min_bin_size(i) >= size) {
bin = i;
break;
}
}

// now find a bin list that's not the large bin
FreeList* selected_bin = nullptr;
for (uint8_t i = bin; i < JIT_ALLOC_BINS; i++) {
if (free_bin_lists[i])
return true;
}

std::size_t min_so_far = std::numeric_limits<std::size_t>::max();
// otherwise we need to search the large bin for the smallest chunk to split up.
selected_bin = free_bin_lists[JIT_ALLOC_BINS];
while (selected_bin) {
if (*get_size_ptr(selected_bin) >= size) {
return true;
}
selected_bin = selected_bin->next;
}

return false;
}

/*!
* Completely flush the heap
*/
Expand Down
1 change: 1 addition & 0 deletions src/core/jitcommon/jitcache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ class EEJitHeap : public JitHeap
EEJitBlockRecord* lookup_cache[1024 * 32];

EEJitBlockRecord *insert_block(uint32_t PC, JitBlock* block);
bool has_free_space(std::size_t size);
void flush_all_blocks();
void invalidate_ee_page(uint32_t page);
EEJitBlockRecord *find_block(uint32_t PC);
Expand Down