|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +# High-frequency regression test: does supervisor-side ftruncate + mmap remap |
| 5 | +# invalidate worker-side mmap slices (as created by Observer.open)? |
| 6 | +# |
| 7 | +# The supervisor (parent process) holds @file open and calls: |
| 8 | +# @file.truncate(new_size) |
| 9 | +# @buffer.free |
| 10 | +# @buffer = IO::Buffer.map(@file, new_size) |
| 11 | +# |
| 12 | +# The worker (child process) independently opens the same file, maps a |
| 13 | +# page-aligned window covering its segment, closes the fd, and holds a slice |
| 14 | +# of the resulting buffer — exactly what Observer.open does. |
| 15 | +# |
| 16 | +# This test runs both sides concurrently at maximum speed to expose any |
| 17 | +# platform/version scenario where ftruncate or munmap (via #free) on one |
| 18 | +# process's mapping invalidates another process's independent mapping. |
| 19 | +# |
| 20 | +# Usage: |
| 21 | +# ruby examples/resize_mmap/test.rb |
| 22 | + |
| 23 | +require "tmpdir" |
| 24 | + |
| 25 | +PAGE_SIZE = IO::Buffer::PAGE_SIZE |
| 26 | +SEGMENT_SIZE = 512 |
| 27 | +ITERATIONS = 200_000 # write iterations per worker |
| 28 | +RESIZES = 20 # supervisor-side resizes per test run |
| 29 | + |
| 30 | +# Reproduce the exact mapping logic from Observer.open. |
| 31 | +# Returns [slice, parent_buffer] — caller must hold parent_buffer to prevent GC |
| 32 | +# (IO::Buffer#slice keeps a back-reference to the parent in Ruby's C layer, but |
| 33 | +# we return it explicitly to make the lifetime contract obvious in tests). |
| 34 | +def worker_map(path, segment_offset, segment_size) |
| 35 | + page_size = IO::Buffer::PAGE_SIZE |
| 36 | + aligned_offset = (segment_offset / page_size) * page_size |
| 37 | + offset_adjustment = segment_offset - aligned_offset |
| 38 | + aligned_end = (((segment_offset + segment_size) + page_size - 1) / page_size) * page_size |
| 39 | + map_size = [aligned_end - aligned_offset, page_size].max |
| 40 | + |
| 41 | + file = File.open(path, "r+b") |
| 42 | + # Mirror Observer.open: fd is closed after mapping; mapping persists independently. |
| 43 | + parent = IO::Buffer.map(file, map_size, aligned_offset) |
| 44 | + file.close |
| 45 | + |
| 46 | + slice = (offset_adjustment > 0 || map_size > segment_size) ? |
| 47 | + parent.slice(offset_adjustment, segment_size) : |
| 48 | + parent |
| 49 | + |
| 50 | + [slice, parent] |
| 51 | +end |
| 52 | + |
| 53 | +# Run one test scenario. |
| 54 | +# |
| 55 | +# Layout within a segment (as seen by both sides): |
| 56 | +# [0..7] u64 iteration counter — final value must equal ITERATIONS |
| 57 | +# [8..15] u64 write-error count — must be 0 |
| 58 | +# |
| 59 | +# Synchronisation: |
| 60 | +# mapped_w/r child → parent: "I have mapped the file, you may start resizing" |
| 61 | +# done_w/r child → parent: "I have finished writing all ITERATIONS values" |
| 62 | +# exit_w/r parent → child: "you may exit now" |
| 63 | +def run_test(path, segment_offset, label) |
| 64 | + File.open(path, "w+b") { |f| f.truncate(PAGE_SIZE) } |
| 65 | + |
| 66 | + mapped_r, mapped_w = IO.pipe # child signals: file mapped |
| 67 | + done_r, done_w = IO.pipe # child signals: writes complete |
| 68 | + exit_r, exit_w = IO.pipe # parent signals: child may exit |
| 69 | + |
| 70 | + child_pid = fork do |
| 71 | + [mapped_r, done_r, exit_w].each(&:close) |
| 72 | + |
| 73 | + slice, _parent = worker_map(path, segment_offset, SEGMENT_SIZE) |
| 74 | + |
| 75 | + mapped_w.write("1") |
| 76 | + mapped_w.close # unblock parent resize loop |
| 77 | + |
| 78 | + errors = 0 |
| 79 | + ITERATIONS.times do |i| |
| 80 | + begin |
| 81 | + slice.set_value(:u64, 0, i + 1) |
| 82 | + rescue => e |
| 83 | + errors += 1 |
| 84 | + end |
| 85 | + end |
| 86 | + slice.set_value(:u64, 8, errors) |
| 87 | + |
| 88 | + done_w.write("1") |
| 89 | + done_w.close # unblock parent result read |
| 90 | + |
| 91 | + exit_r.read |
| 92 | + exit_r.close |
| 93 | + exit 0 |
| 94 | + end |
| 95 | + |
| 96 | + [mapped_w, done_w, exit_r].each(&:close) |
| 97 | + |
| 98 | + # Wait for the worker to have mapped the file before resizing. |
| 99 | + mapped_r.read |
| 100 | + mapped_r.close |
| 101 | + |
| 102 | + # Supervisor side: resize at maximum speed, concurrently with the child writes. |
| 103 | + # Use a thread so the resize loop and the done_r.read can overlap in time. |
| 104 | + sup_file = File.open(path, "r+b") |
| 105 | + sup_buffer = IO::Buffer.map(sup_file, PAGE_SIZE) |
| 106 | + curr_size = PAGE_SIZE |
| 107 | + |
| 108 | + resize_thread = Thread.new do |
| 109 | + RESIZES.times do |
| 110 | + curr_size *= 2 |
| 111 | + sup_file.truncate(curr_size) |
| 112 | + sup_buffer.free |
| 113 | + sup_buffer = IO::Buffer.map(sup_file, curr_size) |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + # Block until the child has finished all writes. |
| 118 | + done_r.read |
| 119 | + done_r.close |
| 120 | + |
| 121 | + # Drain the resize thread before reading results. |
| 122 | + resize_thread.join |
| 123 | + |
| 124 | + final_counter = sup_buffer.get_value(:u64, segment_offset + 0) |
| 125 | + write_errors = sup_buffer.get_value(:u64, segment_offset + 8) |
| 126 | + |
| 127 | + sup_buffer.free |
| 128 | + sup_file.close |
| 129 | + |
| 130 | + exit_w.write("1") |
| 131 | + exit_w.close |
| 132 | + |
| 133 | + Process.waitpid(child_pid) |
| 134 | + File.unlink(path) rescue nil |
| 135 | + |
| 136 | + [final_counter, write_errors] |
| 137 | +end |
| 138 | + |
| 139 | +# ── Main ────────────────────────────────────────────────────────────────────── |
| 140 | + |
| 141 | +puts "Platform: #{RUBY_PLATFORM}" |
| 142 | +puts "Ruby: #{RUBY_VERSION}" |
| 143 | +puts "PAGE_SIZE: #{PAGE_SIZE}" |
| 144 | +puts "SEGMENT_SIZE: #{SEGMENT_SIZE}" |
| 145 | +puts "ITERATIONS: #{ITERATIONS}" |
| 146 | +puts "RESIZES: #{RESIZES} (#{PAGE_SIZE} → #{PAGE_SIZE * 2**RESIZES} bytes)" |
| 147 | +puts |
| 148 | + |
| 149 | +failures = 0 |
| 150 | + |
| 151 | +Dir.mktmpdir do |dir| |
| 152 | + path = File.join(dir, "test.shm") |
| 153 | + |
| 154 | + tests = { |
| 155 | + "page-aligned offset (segment_offset=0, exercises map_size > segment_size slice path)" => 0, |
| 156 | + "non-page-aligned offset (segment_offset=#{SEGMENT_SIZE}, exercises offset_adjustment > 0 slice path)" => SEGMENT_SIZE, |
| 157 | + } |
| 158 | + |
| 159 | + tests.each_with_index do |(label, segment_offset), index| |
| 160 | + print "Test #{index + 1}: #{label}\n ... " |
| 161 | + $stdout.flush |
| 162 | + |
| 163 | + final, errors = run_test(path, segment_offset, label) |
| 164 | + |
| 165 | + if errors > 0 || final != ITERATIONS |
| 166 | + puts "FAIL" |
| 167 | + puts " expected counter = #{ITERATIONS}, got #{final}" |
| 168 | + puts " write errors (IO::Buffer exceptions) = #{errors}" |
| 169 | + if final != ITERATIONS |
| 170 | + puts " => writes went to stale/invalid pages and were not visible to the supervisor" |
| 171 | + end |
| 172 | + failures += 1 |
| 173 | + else |
| 174 | + puts "PASS (counter=#{ITERATIONS}, errors=0)" |
| 175 | + end |
| 176 | + end |
| 177 | +end |
| 178 | + |
| 179 | +puts |
| 180 | +if failures == 0 |
| 181 | + puts "All tests passed — ftruncate + IO::Buffer.map remap does NOT invalidate existing worker mmap slices on this platform." |
| 182 | + exit 0 |
| 183 | +else |
| 184 | + puts "#{failures} test(s) FAILED — existing mmap slices were invalidated by ftruncate/remap on this platform!" |
| 185 | + exit 1 |
| 186 | +end |
0 commit comments