1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Throttle unload_units

Because d80226e7bd often reduces the number of unloaded units, it
increases the number of unload_units calls, which are heavy.

To mitigate that, this throttles unload_units per `max_cache_size / 10`.

Also hoping to fix
https://ci.appveyor.com/project/ruby/ruby/builds/36552382/job/kjmjgw9cjyf2ksd7
This commit is contained in:
Takashi Kokubun 2020-11-27 22:46:01 -08:00
parent 95bef7b69a
commit 122cd35939
No known key found for this signature in database
GPG key ID: 6FFC433B12EE23DD
2 changed files with 12 additions and 6 deletions

2
mjit.c
View file

@ -261,7 +261,7 @@ mjit_add_iseq_to_process(const rb_iseq_t *iseq, const struct rb_mjit_compile_inf
CRITICAL_SECTION_START(3, "in add_iseq_to_process"); CRITICAL_SECTION_START(3, "in add_iseq_to_process");
add_to_list(iseq->body->jit_unit, &unit_queue); add_to_list(iseq->body->jit_unit, &unit_queue);
if (active_units.length >= mjit_opts.max_cache_size) { if (active_units.length >= mjit_opts.max_cache_size) {
unload_units_p = true; unload_requests++;
} }
verbose(3, "Sending wakeup signal to workers in mjit_add_iseq_to_process"); verbose(3, "Sending wakeup signal to workers in mjit_add_iseq_to_process");
rb_native_cond_broadcast(&mjit_worker_wakeup); rb_native_cond_broadcast(&mjit_worker_wakeup);

View file

@ -225,8 +225,8 @@ static rb_nativethread_cond_t mjit_gc_wakeup;
static int in_gc = 0; static int in_gc = 0;
// True when JIT is working. // True when JIT is working.
static bool in_jit = false; static bool in_jit = false;
// True when unload_units is requested from Ruby threads. // The times when unload_units is requested. unload_units is called after some requests.
static bool unload_units_p = false; static int unload_requests = 0;
// Set to true to stop worker. // Set to true to stop worker.
static bool stop_worker_p; static bool stop_worker_p;
// Set to true if worker is stopped. // Set to true if worker is stopped.
@ -910,9 +910,11 @@ compile_compact_jit_code(char* c_file)
bool iseq_gced = false; bool iseq_gced = false;
struct rb_mjit_unit *child_unit = 0; struct rb_mjit_unit *child_unit = 0;
list_for_each(&active_units.head, child_unit, unode) { list_for_each(&active_units.head, child_unit, unode) {
if (child_unit->iseq == NULL) { if (child_unit->iseq == NULL) { // ISeq is GC-ed
iseq_gced = true; iseq_gced = true;
verbose(1, "JIT compaction: A method for JIT code u%d is obsoleted. Compaction will be skipped.", child_unit->id); verbose(1, "JIT compaction: A method for JIT code u%d is obsoleted. Compaction will be skipped.", child_unit->id);
remove_from_list(child_unit, &active_units);
free_unit(child_unit); // unload it without waiting for throttled unload_units to retry compaction quickly
} }
} }
in_jit = !iseq_gced; in_jit = !iseq_gced;
@ -1331,6 +1333,10 @@ mjit_worker(void)
int max_compact_size = mjit_opts.max_cache_size / 10; int max_compact_size = mjit_opts.max_cache_size / 10;
if (max_compact_size < 10) max_compact_size = 10; if (max_compact_size < 10) max_compact_size = 10;
// Run unload_units after it's requested `max_cache_size / 10` (default: 10) times.
// This throttles the call to mitigate locking in unload_units.
int unload_threshold = mjit_opts.max_cache_size / 10;
#ifndef _MSC_VER #ifndef _MSC_VER
if (pch_status == PCH_NOT_READY) { if (pch_status == PCH_NOT_READY) {
make_pch(); make_pch();
@ -1356,10 +1362,10 @@ mjit_worker(void)
rb_native_cond_wait(&mjit_worker_wakeup, &mjit_engine_mutex); rb_native_cond_wait(&mjit_worker_wakeup, &mjit_engine_mutex);
verbose(3, "Getting wakeup from client"); verbose(3, "Getting wakeup from client");
if (unload_units_p) { if (unload_requests >= unload_threshold) {
RB_DEBUG_COUNTER_INC(mjit_unload_units); RB_DEBUG_COUNTER_INC(mjit_unload_units);
unload_units(); unload_units();
unload_units_p = false; unload_requests = 0;
if (active_units.length == mjit_opts.max_cache_size && mjit_opts.wait) { // Sometimes all methods may be in use if (active_units.length == mjit_opts.max_cache_size && mjit_opts.wait) { // Sometimes all methods may be in use
mjit_opts.max_cache_size++; // avoid infinite loop on `rb_mjit_wait_call`. Note that --jit-wait is just for testing. mjit_opts.max_cache_size++; // avoid infinite loop on `rb_mjit_wait_call`. Note that --jit-wait is just for testing.