From 18ee38af90b527f617af7bf9bbcf2e8e7cb0df17 Mon Sep 17 00:00:00 2001 From: KJ Tsanaktsidis Date: Thu, 25 Nov 2021 17:56:19 +1100 Subject: Allow Raindrops objects to be backed by a file Currently, all memory used by Raindrops is mapped as MAP_ANONYMOUS. This means that although Raindrops counters can be shared between processes that have forked from each other, it is not possible to share the counter values with another, unrelated process. This patch adds support for backing the Raindrops mapping with a file descriptor obtained from an IO object. The #initialize API has been enhanced with two new keyword options: Raindrops.new(size, io: nil, zero: false) If an instance of IO is provided, then the underlying file descriptor for that IO will be used to back the memory mapping Raindrops creates. An unrelated process can then open the same file, and read the counters; either by mmap'ing the file itself (or using Raindrops to do so), or by making ordinary seek()/read() calls if performance is not a concern. Note that the provided IO object _must_ implement #truncate; this is used to set the size of the file to be right-sized for the memory mapping that is created. If the zero argument is passed as true, then the mapping will be zero'd by Raindrops as part of its initialization. If it's false, then the Raindrops counters existing in the file will be preserved. This allows counter values to be persisted (although note that Raindrops makes no attempt to msync the values, so they are not durable to e.g. system crashes). Counter values can easily be shared between processes in-memory only without touching the disk by passing in a File on a tmpfs as the io object. --- test/test_raindrops.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'test') diff --git a/test/test_raindrops.rb b/test/test_raindrops.rb index 0749694..6351c66 100644 --- a/test/test_raindrops.rb +++ b/test/test_raindrops.rb @@ -1,6 +1,7 @@ # -*- encoding: binary -*- require 'test/unit' require 'raindrops' +require 'tempfile' class TestRaindrops < Test::Unit::TestCase @@ -162,4 +163,45 @@ class TestRaindrops < Test::Unit::TestCase assert status.success? assert_equal [ 1, 2 ], tmp.to_ary end + + def test_io_backed + file = Tempfile.new('test_io_backed') + rd = Raindrops.new(4, io: file, zero: true) + rd[0] = 123 + rd[1] = 456 + + assert_equal 123, rd[0] + assert_equal 456, rd[1] + + rd.evaporate! + + file.rewind + data = file.read + assert_equal 123, data.unpack('L!')[0] + assert_equal 456, data[Raindrops::SIZE..data.size].unpack('L!')[0] + end + + def test_io_backed_reuse + file = Tempfile.new('test_io_backed') + rd = Raindrops.new(4, io: file, zero: true) + rd[0] = 123 + rd[1] = 456 + rd.evaporate! + + rd = Raindrops.new(4, io: file, zero: false) + assert_equal 123, rd[0] + assert_equal 456, rd[1] + end + + def test_iobacked_noreuse + file = Tempfile.new('test_io_backed') + rd = Raindrops.new(4, io: file, zero: true) + rd[0] = 123 + rd[1] = 456 + rd.evaporate! + + rd = Raindrops.new(4, io: file, zero: true) + assert_equal 0, rd[0] + assert_equal 0, rd[1] + end end -- cgit v1.2.3-24-ge0c7