|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +# Smoke test for MMTk heap modes. |
| 5 | +# |
| 6 | +# Runs an allocation-heavy loop under a given MMTK_HEAP_MODE and reports: |
| 7 | +# - the mode Ruby actually booted with (GC.config) |
| 8 | +# - GC cycle count triggered during the loop |
| 9 | +# - wall-clock time and process CPU time |
| 10 | +# - peak resident set size (peak RSS) |
| 11 | +# |
| 12 | +# Usage (after `rake install:release` against a modular-GC Ruby): |
| 13 | +# |
| 14 | +# bin/smoke-test # defaults to MMTK_HEAP_MODE=cpu |
| 15 | +# MMTK_HEAP_MODE=ruby bin/smoke-test |
| 16 | +# MMTK_HEAP_MODE=cpu MMTK_GC_CPU_TARGET=10 bin/smoke-test |
| 17 | +# SMOKE_ITERATIONS=2_000_000 bin/smoke-test # longer run for trigger to adapt |
| 18 | +# |
| 19 | +# If this script is run without RUBY_GC_LIBRARY=mmtk set, it will re-exec |
| 20 | +# itself with that env var plus whatever other MMTK_* vars you passed. |
| 21 | + |
| 22 | +unless ENV["RUBY_GC_LIBRARY"] == "mmtk" |
| 23 | + ENV["RUBY_GC_LIBRARY"] = "mmtk" |
| 24 | + ENV["MMTK_HEAP_MODE"] ||= "cpu" |
| 25 | + exec(RbConfig.ruby, __FILE__, *ARGV) |
| 26 | +end |
| 27 | + |
| 28 | +impl = GC.config[:implementation] |
| 29 | +unless impl == "mmtk" |
| 30 | + abort "smoke-test: expected GC implementation 'mmtk', got #{impl.inspect}. " \ |
| 31 | + "Is your Ruby built with --with-modular-gc and is the binding installed?" |
| 32 | +end |
| 33 | + |
| 34 | +require "fiddle" |
| 35 | + |
| 36 | +# getrusage(RUSAGE_SELF) returns peak RSS in ru_maxrss. On macOS the value is |
| 37 | +# in bytes; on Linux it's in kilobytes. |
| 38 | +module Rusage |
| 39 | + extend self |
| 40 | + |
| 41 | + RUSAGE_SELF = 0 |
| 42 | + |
| 43 | + # struct rusage on macOS/Linux: first two fields are ru_utime / ru_stime |
| 44 | + # (struct timeval = { long, long }), then a series of long integers. |
| 45 | + # ru_maxrss is the 3rd long integer (offset after the 2 timevals). |
| 46 | + # Each field here is a 64-bit long on 64-bit platforms. |
| 47 | + # Layout (all i64): |
| 48 | + # [0..1] ru_utime (sec, usec) |
| 49 | + # [2..3] ru_stime (sec, usec) |
| 50 | + # [4] ru_maxrss <-- what we want |
| 51 | + # ... more fields we don't use |
| 52 | + STRUCT_LONGS = 18 |
| 53 | + |
| 54 | + def peak_rss_bytes |
| 55 | + libc = Fiddle::Handle::DEFAULT |
| 56 | + getrusage = Fiddle::Function.new( |
| 57 | + libc["getrusage"], [Fiddle::TYPE_INT, Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT |
| 58 | + ) |
| 59 | + buf = Fiddle::Pointer.malloc(STRUCT_LONGS * Fiddle::SIZEOF_LONG, Fiddle::RUBY_FREE) |
| 60 | + raise "getrusage failed" unless getrusage.call(RUSAGE_SELF, buf) == 0 |
| 61 | + maxrss = buf[4 * Fiddle::SIZEOF_LONG, Fiddle::SIZEOF_LONG].unpack1("q") |
| 62 | + # macOS reports bytes, Linux reports kilobytes. |
| 63 | + RbConfig::CONFIG["host_os"].include?("darwin") ? maxrss : maxrss * 1024 |
| 64 | + end |
| 65 | +end |
| 66 | + |
| 67 | +puts "== MMTk smoke test ==" |
| 68 | +puts "implementation: #{GC.config[:implementation]}" |
| 69 | +puts "mmtk_plan: #{GC.config[:mmtk_plan]}" |
| 70 | +puts "mmtk_heap_mode: #{GC.config[:mmtk_heap_mode]}" |
| 71 | +puts "mmtk_heap_min: #{GC.config[:mmtk_heap_min]}" if GC.config[:mmtk_heap_min] |
| 72 | +puts "mmtk_heap_max: #{GC.config[:mmtk_heap_max]}" |
| 73 | +puts "mmtk_worker_count: #{GC.config[:mmtk_worker_count]}" |
| 74 | +if GC.config[:mmtk_heap_mode] == "cpu" |
| 75 | + puts "cpu target (env): #{ENV.fetch('MMTK_GC_CPU_TARGET', '5')}%" |
| 76 | + puts "cpu window (env): #{ENV.fetch('MMTK_GC_CPU_WINDOW', '3')}" |
| 77 | +end |
| 78 | +puts "---" |
| 79 | + |
| 80 | +ITERATIONS = Integer(ENV.fetch("SMOKE_ITERATIONS", 500_000)) |
| 81 | +OBJECT_SIZE = Integer(ENV.fetch("SMOKE_OBJECT_SIZE", 256)) |
| 82 | +LIVE_SET = Integer(ENV.fetch("SMOKE_LIVE_SET", 2_000)) |
| 83 | + |
| 84 | +# The workload: maintain a rolling working set of LIVE_SET objects, each |
| 85 | +# OBJECT_SIZE bytes. Each iteration allocates a new object and drops an old |
| 86 | +# one. This produces a steady stream of garbage and a predictable live-set |
| 87 | +# size, so the CPU trigger has a stable signal to converge on. |
| 88 | + |
| 89 | +gc_before = GC.count |
| 90 | +t_wall_start = Process.clock_gettime(Process::CLOCK_MONOTONIC) |
| 91 | +t_cpu_start = Process.clock_gettime(Process::CLOCK_PROCESS_CPUTIME_ID) |
| 92 | + |
| 93 | +sink = Array.new(LIVE_SET) { String.new("x" * OBJECT_SIZE) } |
| 94 | +i = 0 |
| 95 | +while i < ITERATIONS |
| 96 | + sink[i % LIVE_SET] = String.new("x" * OBJECT_SIZE) |
| 97 | + i += 1 |
| 98 | +end |
| 99 | + |
| 100 | +t_wall_end = Process.clock_gettime(Process::CLOCK_MONOTONIC) |
| 101 | +t_cpu_end = Process.clock_gettime(Process::CLOCK_PROCESS_CPUTIME_ID) |
| 102 | +gc_after = GC.count |
| 103 | + |
| 104 | +wall_s = t_wall_end - t_wall_start |
| 105 | +cpu_s = t_cpu_end - t_cpu_start |
| 106 | +rss = Rusage.peak_rss_bytes |
| 107 | + |
| 108 | +printf "iterations: %d (live set %d x %dB)\n", ITERATIONS, LIVE_SET, OBJECT_SIZE |
| 109 | +printf "gc cycles: %d (before=%d, after=%d)\n", (gc_after - gc_before), gc_before, gc_after |
| 110 | +printf "wall time: %.3fs\n", wall_s |
| 111 | +printf "cpu time: %.3fs (%.1f%% of wall)\n", cpu_s, (cpu_s / wall_s) * 100.0 |
| 112 | +printf "peak rss: %.1f MiB (%d bytes)\n", rss / 1024.0 / 1024.0, rss |
| 113 | +puts "OK" |
0 commit comments