From b0918b17db45593d495d4ceab732833370f12d81 Mon Sep 17 00:00:00 2001 From: Matt Valentine-House Date: Thu, 19 Nov 2026 12:13:58 +0000 Subject: [PATCH] wip --- README.md | 5 ++++- lib/gc_stats.rb | 29 ++++++++++++++++++++++------- test/gc_stats_test.rb | 13 +++++++++++++ test/ractor_gc_harness_test.rb | 4 ++-- 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index fb758955..bf46b574 100644 --- a/README.md +++ b/README.md @@ -489,7 +489,10 @@ data when it is zero or `N/A` in every row. * Tables marked `worker sum` add the Ractor-local counters and GC times of the sampled workers of each iteration. `GCs/iter` is the sum of `minor/iter`, `major/iter`, and `global/iter`; a global cycle counts under `global` on - the Ractor that initiated it, not under `major`. Single-executable reports + the Ractor that initiated it, not under `major`. On targets that have + `GC.stat(:global_gc_invocation_count)`, the harness reads `global` from it, + because `GC.stat(:global_gc_count)` there counts a global cycle in every + Ractor whose heap it collects. Single-executable reports also show `GC ms/worker`, which divides each iteration's worker-sum GC time by its sampled worker count, then averages. * `compacts*` shows the main Ractor's diff --git a/lib/gc_stats.rb b/lib/gc_stats.rb index 1106bef3..e1a79471 100644 --- a/lib/gc_stats.rb +++ b/lib/gc_stats.rb @@ -3,9 +3,12 @@ module GCStats module_function + PROCESS_STAT = GC.method(:stat).parameters.include?([:key, :process]) + INITIATED_GLOBAL_GC_KEY = GC.stat.key?(:global_gc_invocation_count) ? :global_gc_invocation_count : :global_gc_count + SCALAR_FIELDS = [ ["gc_count", :count], - ["gc_global_count", :global_gc_count], + ["gc_global_count", INITIATED_GLOBAL_GC_KEY], ["gc_major_count", :major_gc_count], ["gc_minor_count", :minor_gc_count], ["gc_marking_time", :marking_time], @@ -32,6 +35,10 @@ def stat_available?(key) false end + def process_stat(key) + PROCESS_STAT ? GC.stat(key, process: true) : GC.stat(key, scope: :global) + end + def ractor_local_gc_supported?(version = RUBY_VERSION) major, minor = version.split(".").first(2).map(&:to_i) major > 4 || (major == 4 && minor >= 1) @@ -44,11 +51,15 @@ def global_gc_attributed? Warning[:experimental] = false $stderr.reopen(File::NULL, "w") begin - main_before = GC.stat(:global_gc_count) - process_before = GC.stat(:global_gc_count, scope: :global) - Ractor.new { GC.start(full_mark: true, immediate_mark: true, immediate_sweep: true, global: true) }.join - attributed = GC.stat(:global_gc_count) == main_before - ran = GC.stat(:global_gc_count, scope: :global) > process_before + main_before = GC.stat(INITIATED_GLOBAL_GC_KEY) + process_before = process_stat(:global_gc_count) + initiated = Ractor.new(INITIATED_GLOBAL_GC_KEY) do |key| + before = GC.stat(key) + GC.start(full_mark: true, immediate_mark: true, immediate_sweep: true, global: true) + GC.stat(key) - before + end.value + attributed = initiated > 0 && GC.stat(INITIATED_GLOBAL_GC_KEY) == main_before + ran = process_stat(:global_gc_count) > process_before exit!(attributed && ran ? 0 : 1) rescue StandardError exit!(2) @@ -95,7 +106,7 @@ def numeric_delta(before, after) def snapshot { - stat: GC.stat(scope: :ractor), + stat: GC.stat, heap: heap_snapshot, total_time_ns: GC.respond_to?(:total_time) ? GC.total_time : nil, } @@ -106,6 +117,10 @@ def delta(before, after) result[name] = numeric_delta(before[:stat][key], after[:stat][key]) end sample[TOTAL_TIME_FIELD] = numeric_delta(before[:total_time_ns], after[:total_time_ns]) + if INITIATED_GLOBAL_GC_KEY == :global_gc_invocation_count + initiated = sample.values_at("gc_minor_count", "gc_major_count", "gc_global_count") + sample["gc_count"] = initiated.sum if initiated.all?(Numeric) + end sample["gc_stat_heap_delta"] = heap_delta(before[:heap], after[:heap]) sample["gc_heap_after"] = after[:heap] sample diff --git a/test/gc_stats_test.rb b/test/gc_stats_test.rb index d807ca24..8b788b77 100644 --- a/test/gc_stats_test.rb +++ b/test/gc_stats_test.rb @@ -95,6 +95,19 @@ def snapshot(count:, major:, minor:, marking: nil, sweeping: nil, total_ns: nil, assert_same after[:heap], sample['gc_heap_after'] end + + it 'counts a global GC only in the Ractor that started it' do + skip 'target lacks GC.stat(:global_gc_invocation_count)' unless GCStats::INITIATED_GLOBAL_GC_KEY == :global_gc_invocation_count + + before = snapshot(count: 0, major: 0, minor: 0) + before[:stat].merge!(global_gc_count: 0, global_gc_invocation_count: 0) + after = snapshot(count: 4, major: 1, minor: 1) + after[:stat].merge!(global_gc_count: 2, global_gc_invocation_count: 1) + + sample = GCStats.delta(before, after) + + assert_equal [3, 1], sample.values_at('gc_count', 'gc_global_count') + end end describe '.aggregate' do diff --git a/test/ractor_gc_harness_test.rb b/test/ractor_gc_harness_test.rb index 8c76c51d..9b62bfe5 100644 --- a/test/ractor_gc_harness_test.rb +++ b/test/ractor_gc_harness_test.rb @@ -307,9 +307,9 @@ def run_workload(body, env: {}) worker = Ractor.new do GC.disable Ractor.receive - before = GC.stat(:count, scope: :ractor) + before = GC.stat(:count) 10.times { GC.start(full_mark: false, immediate_sweep: true, global: false) } - Ractor.main.send(GC.stat(:count, scope: :ractor) - before) + Ractor.main.send(GC.stat(:count) - before) Ractor.receive end