Replace all GC.disable with EnvUtil.without_gc

This commit is contained in:
Peter Zhu 2024-09-16 15:29:38 -04:00
parent 82151a8630
commit 0160dafc4c
Notes: git 2024-09-17 14:34:47 +00:00
5 changed files with 98 additions and 113 deletions

View File

@ -244,39 +244,38 @@ class TestObjSpace < Test::Unit::TestCase
def test_trace_object_allocations_start_stop_clear
ObjectSpace.trace_object_allocations_clear # clear object_table to get rid of erroneous detection for obj3
GC.disable # suppress potential object reuse. see [Bug #11271]
begin
ObjectSpace.trace_object_allocations_start
EnvUtil.without_gc do # suppress potential object reuse. see [Bug #11271]
begin
ObjectSpace.trace_object_allocations_start
begin
ObjectSpace.trace_object_allocations_start
obj0 = Object.new
begin
ObjectSpace.trace_object_allocations_start
obj0 = Object.new
ensure
ObjectSpace.trace_object_allocations_stop
obj1 = Object.new
end
ensure
ObjectSpace.trace_object_allocations_stop
obj1 = Object.new
obj2 = Object.new
end
ensure
ObjectSpace.trace_object_allocations_stop
obj2 = Object.new
obj3 = Object.new
end
ensure
ObjectSpace.trace_object_allocations_stop
obj3 = Object.new
assert_equal(__FILE__, ObjectSpace.allocation_sourcefile(obj0))
assert_equal(__FILE__, ObjectSpace.allocation_sourcefile(obj1))
assert_equal(__FILE__, ObjectSpace.allocation_sourcefile(obj2))
assert_equal(nil , ObjectSpace.allocation_sourcefile(obj3)) # after tracing
ObjectSpace.trace_object_allocations_clear
assert_equal(nil, ObjectSpace.allocation_sourcefile(obj0))
assert_equal(nil, ObjectSpace.allocation_sourcefile(obj1))
assert_equal(nil, ObjectSpace.allocation_sourcefile(obj2))
assert_equal(nil, ObjectSpace.allocation_sourcefile(obj3))
end
assert_equal(__FILE__, ObjectSpace.allocation_sourcefile(obj0))
assert_equal(__FILE__, ObjectSpace.allocation_sourcefile(obj1))
assert_equal(__FILE__, ObjectSpace.allocation_sourcefile(obj2))
assert_equal(nil , ObjectSpace.allocation_sourcefile(obj3)) # after tracing
ObjectSpace.trace_object_allocations_clear
assert_equal(nil, ObjectSpace.allocation_sourcefile(obj0))
assert_equal(nil, ObjectSpace.allocation_sourcefile(obj1))
assert_equal(nil, ObjectSpace.allocation_sourcefile(obj2))
assert_equal(nil, ObjectSpace.allocation_sourcefile(obj3))
ensure
GC.enable
end
def test_trace_object_allocations_gc_stress

View File

@ -40,16 +40,16 @@ class TestGc < Test::Unit::TestCase
end
def test_enable_disable
GC.enable
assert_equal(false, GC.enable)
assert_equal(false, GC.disable)
assert_equal(true, GC.disable)
assert_equal(true, GC.disable)
assert_nil(GC.start)
assert_equal(true, GC.enable)
assert_equal(false, GC.enable)
ensure
GC.enable
EnvUtil.without_gc do
GC.enable
assert_equal(false, GC.enable)
assert_equal(false, GC.disable)
assert_equal(true, GC.disable)
assert_equal(true, GC.disable)
assert_nil(GC.start)
assert_equal(true, GC.enable)
assert_equal(false, GC.enable)
end
end
def test_gc_config_full_mark_by_default
@ -227,12 +227,9 @@ class TestGc < Test::Unit::TestCase
GC.stat(stat)
GC::INTERNAL_CONSTANTS[:SIZE_POOL_COUNT].times do |i|
begin
reenable_gc = !GC.disable
EnvUtil.without_gc do
GC.stat_heap(i, stat_heap)
GC.stat(stat)
ensure
GC.enable if reenable_gc
end
assert_equal (GC::INTERNAL_CONSTANTS[:BASE_SLOT_SIZE] + GC::INTERNAL_CONSTANTS[:RVALUE_OVERHEAD]) * (2**i), stat_heap[:slot_size]
@ -768,23 +765,17 @@ class TestGc < Test::Unit::TestCase
end
def test_gc_disabled_start
begin
disabled = GC.disable
EnvUtil.without_gc do
c = GC.count
GC.start
assert_equal 1, GC.count - c
ensure
GC.enable unless disabled
end
begin
disabled = GC.disable
EnvUtil.without_gc do
c = GC.count
GC.start(immediate_mark: false, immediate_sweep: false)
10_000.times { Object.new }
assert_equal 1, GC.count - c
ensure
GC.enable unless disabled
end
end
@ -859,28 +850,26 @@ class TestGc < Test::Unit::TestCase
end
def test_old_to_young_reference
original_gc_disabled = GC.disable
EnvUtil.without_gc do
require "objspace"
require "objspace"
old_obj = Object.new
4.times { GC.start }
old_obj = Object.new
4.times { GC.start }
assert_include ObjectSpace.dump(old_obj), '"old":true'
assert_include ObjectSpace.dump(old_obj), '"old":true'
young_obj = Object.new
old_obj.instance_variable_set(:@test, young_obj)
young_obj = Object.new
old_obj.instance_variable_set(:@test, young_obj)
# Not immediately promoted to old generation
3.times do
assert_not_include ObjectSpace.dump(young_obj), '"old":true'
GC.start
end
# Not immediately promoted to old generation
3.times do
assert_not_include ObjectSpace.dump(young_obj), '"old":true'
# Takes 4 GC to promote to old generation
GC.start
assert_include ObjectSpace.dump(young_obj), '"old":true'
end
# Takes 4 GC to promote to old generation
GC.start
assert_include ObjectSpace.dump(young_obj), '"old":true'
ensure
GC.enable if !original_gc_disabled
end
end

View File

@ -191,30 +191,29 @@ End
end
def test_finalizer_thread_raise
GC.disable
fzer = proc do |id|
sleep 0.2
end
2.times do
o = Object.new
ObjectSpace.define_finalizer(o, fzer)
end
my_error = Class.new(RuntimeError)
begin
main_th = Thread.current
Thread.new do
sleep 0.1
main_th.raise(my_error)
EnvUtil.without_gc do
fzer = proc do |id|
sleep 0.2
end
2.times do
o = Object.new
ObjectSpace.define_finalizer(o, fzer)
end
my_error = Class.new(RuntimeError)
begin
main_th = Thread.current
Thread.new do
sleep 0.1
main_th.raise(my_error)
end
GC.start
puts "After GC"
sleep(10)
assert(false)
rescue my_error
end
GC.start
puts "After GC"
sleep(10)
assert(false)
rescue my_error
end
ensure
GC.enable
end
def test_each_object

View File

@ -4,14 +4,13 @@ require 'etc'
class TestSleep < Test::Unit::TestCase
def test_sleep_5sec
GC.disable
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
sleep 5
slept = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
bottom = 5.0
assert_operator(slept, :>=, bottom)
assert_operator(slept, :<=, 6.0, "[ruby-core:18015]: longer than expected")
ensure
GC.enable
EnvUtil.without_gc do
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
sleep 5
slept = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
bottom = 5.0
assert_operator(slept, :>=, bottom)
assert_operator(slept, :<=, 6.0, "[ruby-core:18015]: longer than expected")
end
end
end

View File

@ -6,32 +6,31 @@ class TestStringMemory < Test::Unit::TestCase
def capture_allocations(klass)
allocations = []
GC.start
GC.disable
generation = GC.count
EnvUtil.without_gc do
GC.start
generation = GC.count
ObjectSpace.trace_object_allocations do
yield
ObjectSpace.trace_object_allocations do
yield
ObjectSpace.each_object(klass) do |instance|
allocations << instance if ObjectSpace.allocation_generation(instance) == generation
ObjectSpace.each_object(klass) do |instance|
allocations << instance if ObjectSpace.allocation_generation(instance) == generation
end
end
return allocations.map do |instance|
[
ObjectSpace.allocation_sourcefile(instance),
ObjectSpace.allocation_sourceline(instance),
instance.class,
instance,
]
end.select do |path,|
# drop strings not created in this file
# (the parallel testing framework may create strings in a separate thread)
path == __FILE__
end
end
return allocations.map do |instance|
[
ObjectSpace.allocation_sourcefile(instance),
ObjectSpace.allocation_sourceline(instance),
instance.class,
instance,
]
end.select do |path,|
# drop strings not created in this file
# (the parallel testing framework may create strings in a separate thread)
path == __FILE__
end
ensure
GC.enable
end
def test_byteslice_prefix