From a3ca2d26e93aabc7dcc62ce3b29bf18eafe11caf Mon Sep 17 00:00:00 2001 From: luc-blaeser Date: Thu, 3 Aug 2023 14:08:57 +0200 Subject: [PATCH 1/4] More conservative incremental GC scheduling --- rts/motoko-rts/src/gc/incremental.rs | 32 ++++++++++++++++++---------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/rts/motoko-rts/src/gc/incremental.rs b/rts/motoko-rts/src/gc/incremental.rs index b136ddea147..1bbc7182221 100644 --- a/rts/motoko-rts/src/gc/incremental.rs +++ b/rts/motoko-rts/src/gc/incremental.rs @@ -72,22 +72,32 @@ unsafe fn should_start() -> bool { use self::partitioned_heap::PARTITION_SIZE; use crate::memory::ic::partitioned_memory; - const CRITICAL_HEAP_LIMIT: Bytes = Bytes(u32::MAX - 768 * 1024 * 1024); - const CRITICAL_GROWTH_THRESHOLD: f64 = 0.01; - const NORMAL_GROWTH_THRESHOLD: f64 = 0.65; - let heap_size = partitioned_memory::get_heap_size(); - let growth_threshold = if heap_size > CRITICAL_HEAP_LIMIT { - CRITICAL_GROWTH_THRESHOLD - } else { - NORMAL_GROWTH_THRESHOLD - }; + if heap_size.as_usize() < PARTITION_SIZE { + return false; + } + + const ABSOLUT_GROWTH_THRESHOLD: Bytes = Bytes(512 * 1024 * 1024); let current_allocations = partitioned_memory::get_total_allocations(); debug_assert!(current_allocations >= LAST_ALLOCATIONS); let absolute_growth = current_allocations - LAST_ALLOCATIONS; + if absolute_growth > ABSOLUT_GROWTH_THRESHOLD { + return true; + } + + const CRITICAL_HEAP_LIMIT: Bytes = Bytes(u32::MAX - 768 * 1024 * 1024); + const CRITICAL_RELATIVE_GROWTH_THRESHOLD: f64 = 0.01; + const NORMAL_RELATIVE_GROWTH_THRESHOLD: f64 = 0.65; + + let relative_growth_threshold = if heap_size > CRITICAL_HEAP_LIMIT { + CRITICAL_RELATIVE_GROWTH_THRESHOLD + } else { + NORMAL_RELATIVE_GROWTH_THRESHOLD + }; + let relative_growth = absolute_growth.0 as f64 / heap_size.as_usize() as f64; - relative_growth > growth_threshold && heap_size.as_usize() >= PARTITION_SIZE + relative_growth > relative_growth_threshold } #[cfg(feature = "ic")] @@ -124,7 +134,7 @@ unsafe fn record_gc_stop() { /// The limit on the GC increment has a fix base with a linear increase depending on the number of /// allocations that were performed during a running GC. The allocation-proportional term adapts /// to the allocation rate and helps the GC to reduce reclamation latency. -const INCREMENT_BASE_LIMIT: usize = 3_500_000; // Increment limit without concurrent allocations. +const INCREMENT_BASE_LIMIT: usize = 7_000_000; // Increment limit without concurrent allocations. const INCREMENT_ALLOCATION_FACTOR: usize = 10; // Additional time factor per concurrent allocation. // Performance note: Storing the phase-specific state in the enum would be nicer but it is much slower. From 611b80473a0a3aac07bb172b81ec971714859987 Mon Sep 17 00:00:00 2001 From: luc-blaeser Date: Thu, 3 Aug 2023 14:42:15 +0200 Subject: [PATCH 2/4] Tune increment limit --- rts/motoko-rts/src/gc/incremental.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rts/motoko-rts/src/gc/incremental.rs b/rts/motoko-rts/src/gc/incremental.rs index 1bbc7182221..f2640142dca 100644 --- a/rts/motoko-rts/src/gc/incremental.rs +++ b/rts/motoko-rts/src/gc/incremental.rs @@ -134,7 +134,7 @@ unsafe fn record_gc_stop() { /// The limit on the GC increment has a fix base with a linear increase depending on the number of /// allocations that were performed during a running GC. The allocation-proportional term adapts /// to the allocation rate and helps the GC to reduce reclamation latency. -const INCREMENT_BASE_LIMIT: usize = 7_000_000; // Increment limit without concurrent allocations. +const INCREMENT_BASE_LIMIT: usize = 5_000_000; // Increment limit without concurrent allocations. const INCREMENT_ALLOCATION_FACTOR: usize = 10; // Additional time factor per concurrent allocation. // Performance note: Storing the phase-specific state in the enum would be nicer but it is much slower. From a012daeeddb8bdf8cb9cc5c124e79d9b8cd0a6c8 Mon Sep 17 00:00:00 2001 From: luc-blaeser Date: Thu, 3 Aug 2023 15:21:27 +0200 Subject: [PATCH 3/4] Tune incremental GC limit --- rts/motoko-rts/src/gc/incremental.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rts/motoko-rts/src/gc/incremental.rs b/rts/motoko-rts/src/gc/incremental.rs index f2640142dca..a03634737f4 100644 --- a/rts/motoko-rts/src/gc/incremental.rs +++ b/rts/motoko-rts/src/gc/incremental.rs @@ -134,7 +134,7 @@ unsafe fn record_gc_stop() { /// The limit on the GC increment has a fix base with a linear increase depending on the number of /// allocations that were performed during a running GC. The allocation-proportional term adapts /// to the allocation rate and helps the GC to reduce reclamation latency. -const INCREMENT_BASE_LIMIT: usize = 5_000_000; // Increment limit without concurrent allocations. +const INCREMENT_BASE_LIMIT: usize = 3_500_000; // Increment limit without concurrent allocations. const INCREMENT_ALLOCATION_FACTOR: usize = 10; // Additional time factor per concurrent allocation. // Performance note: Storing the phase-specific state in the enum would be nicer but it is much slower. From 38afa45201d70f9c584f0e617d7914b05c62b642 Mon Sep 17 00:00:00 2001 From: Luc Blaeser <112870813+luc-blaeser@users.noreply.github.com> Date: Wed, 20 Sep 2023 14:19:41 +0200 Subject: [PATCH 4/4] Fix typo --- rts/motoko-rts/src/gc/incremental.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rts/motoko-rts/src/gc/incremental.rs b/rts/motoko-rts/src/gc/incremental.rs index a03634737f4..1ebb257b310 100644 --- a/rts/motoko-rts/src/gc/incremental.rs +++ b/rts/motoko-rts/src/gc/incremental.rs @@ -77,12 +77,12 @@ unsafe fn should_start() -> bool { return false; } - const ABSOLUT_GROWTH_THRESHOLD: Bytes = Bytes(512 * 1024 * 1024); + const ABSOLUTE_GROWTH_THRESHOLD: Bytes = Bytes(512 * 1024 * 1024); let current_allocations = partitioned_memory::get_total_allocations(); debug_assert!(current_allocations >= LAST_ALLOCATIONS); let absolute_growth = current_allocations - LAST_ALLOCATIONS; - if absolute_growth > ABSOLUT_GROWTH_THRESHOLD { + if absolute_growth > ABSOLUTE_GROWTH_THRESHOLD { return true; }