summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakashi Kokubun <takashikkbn@gmail.com>2020-11-27 22:46:01 -0800
committerTakashi Kokubun <takashikkbn@gmail.com>2020-11-27 23:06:40 -0800
commit122cd35939ddf8ef7bfa17ad75570c01d0cf06ab (patch)
tree99312201b420877bb73698133e57aff3114b7cbd
parent95bef7b69a6fb42687a6200b338060be307259f5 (diff)
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
-rw-r--r--mjit.c2
-rw-r--r--mjit_worker.c16
2 files changed, 12 insertions, 6 deletions
diff --git a/mjit.c b/mjit.c
index 94857df524..c65355fba9 100644
--- a/mjit.c
+++ b/mjit.c
@@ -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");
add_to_list(iseq->body->jit_unit, &unit_queue);
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");
rb_native_cond_broadcast(&mjit_worker_wakeup);
diff --git a/mjit_worker.c b/mjit_worker.c
index 426f1ace9f..6432785c74 100644
--- a/mjit_worker.c
+++ b/mjit_worker.c
@@ -225,8 +225,8 @@ static rb_nativethread_cond_t mjit_gc_wakeup;
static int in_gc = 0;
// True when JIT is working.
static bool in_jit = false;
-// True when unload_units is requested from Ruby threads.
-static bool unload_units_p = false;
+// The times when unload_units is requested. unload_units is called after some requests.
+static int unload_requests = 0;
// Set to true to stop worker.
static bool stop_worker_p;
// Set to true if worker is stopped.
@@ -910,9 +910,11 @@ compile_compact_jit_code(char* c_file)
bool iseq_gced = false;
struct rb_mjit_unit *child_unit = 0;
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;
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;
@@ -1331,6 +1333,10 @@ mjit_worker(void)
int max_compact_size = mjit_opts.max_cache_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
if (pch_status == PCH_NOT_READY) {
make_pch();
@@ -1356,10 +1362,10 @@ mjit_worker(void)
rb_native_cond_wait(&mjit_worker_wakeup, &mjit_engine_mutex);
verbose(3, "Getting wakeup from client");
- if (unload_units_p) {
+ if (unload_requests >= unload_threshold) {
RB_DEBUG_COUNTER_INC(mjit_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
mjit_opts.max_cache_size++; // avoid infinite loop on `rb_mjit_wait_call`. Note that --jit-wait is just for testing.