2019-04-19 21:19:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require 'test/unit'
|
|
|
|
require 'fiddle'
|
2020-11-25 12:24:50 -05:00
|
|
|
require 'etc'
|
2019-04-19 21:19:47 -04:00
|
|
|
|
2021-11-04 01:14:52 -04:00
|
|
|
if RUBY_PLATFORM =~ /s390x/
|
2021-12-16 21:03:27 -05:00
|
|
|
warn "Currently, it is known that the compaction does not work well on s390x; contribution is welcome https://github.com/ruby/ruby/pull/5077"
|
2021-11-04 01:14:52 -04:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-04-19 21:19:47 -04:00
|
|
|
class TestGCCompact < Test::Unit::TestCase
|
2022-05-23 17:31:14 -04:00
|
|
|
module CompactionSupportInspector
|
2021-05-25 19:20:52 -04:00
|
|
|
def supports_auto_compact?
|
2022-06-02 02:32:00 -04:00
|
|
|
GC::OPTS.include?("GC_COMPACTION_SUPPORTED")
|
2021-05-25 19:20:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-23 17:31:14 -04:00
|
|
|
module OmitUnlessCompactSupported
|
|
|
|
include CompactionSupportInspector
|
|
|
|
|
|
|
|
def setup
|
|
|
|
omit "autocompact not supported on this platform" unless supports_auto_compact?
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
include OmitUnlessCompactSupported
|
2021-05-25 19:20:52 -04:00
|
|
|
|
|
|
|
class AutoCompact < Test::Unit::TestCase
|
2022-05-23 17:31:14 -04:00
|
|
|
include OmitUnlessCompactSupported
|
2021-05-25 19:20:52 -04:00
|
|
|
|
2020-11-25 12:24:50 -05:00
|
|
|
def test_enable_autocompact
|
|
|
|
before = GC.auto_compact
|
|
|
|
GC.auto_compact = true
|
|
|
|
assert GC.auto_compact
|
|
|
|
ensure
|
|
|
|
GC.auto_compact = before
|
|
|
|
end
|
2020-11-24 17:33:12 -05:00
|
|
|
|
2020-11-25 12:24:50 -05:00
|
|
|
def test_disable_autocompact
|
|
|
|
before = GC.auto_compact
|
|
|
|
GC.auto_compact = false
|
|
|
|
refute GC.auto_compact
|
|
|
|
ensure
|
|
|
|
GC.auto_compact = before
|
|
|
|
end
|
2020-11-24 17:33:12 -05:00
|
|
|
|
2020-11-25 12:24:50 -05:00
|
|
|
def test_major_compacts
|
|
|
|
before = GC.auto_compact
|
|
|
|
GC.auto_compact = true
|
|
|
|
compact = GC.stat :compact_count
|
|
|
|
GC.start
|
|
|
|
assert_operator GC.stat(:compact_count), :>, compact
|
|
|
|
ensure
|
|
|
|
GC.auto_compact = before
|
|
|
|
end
|
2020-11-24 17:33:12 -05:00
|
|
|
|
2020-11-25 12:24:50 -05:00
|
|
|
def test_implicit_compaction_does_something
|
|
|
|
before = GC.auto_compact
|
|
|
|
list = []
|
|
|
|
list2 = []
|
|
|
|
|
|
|
|
# Try to make some fragmentation
|
|
|
|
500.times {
|
|
|
|
list << Object.new
|
|
|
|
Object.new
|
|
|
|
Object.new
|
|
|
|
}
|
|
|
|
count = GC.stat :compact_count
|
|
|
|
GC.auto_compact = true
|
2021-08-01 19:38:30 -04:00
|
|
|
n = 1_000_000
|
|
|
|
n.times do
|
2020-11-25 12:24:50 -05:00
|
|
|
break if count < GC.stat(:compact_count)
|
|
|
|
list2 << Object.new
|
2022-01-04 03:25:30 -05:00
|
|
|
end and omit "implicit compaction didn't happen within #{n} objects"
|
2020-11-25 12:24:50 -05:00
|
|
|
compact_stats = GC.latest_compact_info
|
|
|
|
refute_predicate compact_stats[:considered], :empty?
|
|
|
|
refute_predicate compact_stats[:moved], :empty?
|
|
|
|
ensure
|
|
|
|
GC.auto_compact = before
|
2020-11-24 17:33:12 -05:00
|
|
|
end
|
2020-11-25 12:24:50 -05:00
|
|
|
end
|
|
|
|
|
2022-05-23 17:31:14 -04:00
|
|
|
class CompactMethodsNotImplemented < Test::Unit::TestCase
|
|
|
|
include CompactionSupportInspector
|
|
|
|
|
|
|
|
def assert_not_implemented(method, *args)
|
|
|
|
omit "autocompact is supported on this platform" if supports_auto_compact?
|
|
|
|
|
|
|
|
assert_raise(NotImplementedError) { GC.send(method, *args) }
|
|
|
|
refute(GC.respond_to?(method), "GC.#{method} should be defined as rb_f_notimplement")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_gc_compact_not_implemented
|
|
|
|
assert_not_implemented(:compact)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_gc_auto_compact_get_not_implemented
|
|
|
|
assert_not_implemented(:auto_compact)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_gc_auto_compact_set_not_implemented
|
|
|
|
assert_not_implemented(:auto_compact=, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_gc_latest_compact_info_not_implemented
|
|
|
|
assert_not_implemented(:latest_compact_info)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_gc_verify_compaction_references_not_implemented
|
|
|
|
assert_not_implemented(:verify_compaction_references)
|
|
|
|
end
|
2020-11-02 17:40:29 -05:00
|
|
|
end
|
|
|
|
|
2022-05-23 17:31:14 -04:00
|
|
|
def os_page_size
|
|
|
|
return true unless defined?(Etc::SC_PAGE_SIZE)
|
2021-05-25 19:20:52 -04:00
|
|
|
end
|
|
|
|
|
2020-11-02 17:40:29 -05:00
|
|
|
def test_gc_compact_stats
|
|
|
|
list = []
|
|
|
|
|
|
|
|
# Try to make some fragmentation
|
|
|
|
500.times {
|
|
|
|
list << Object.new
|
|
|
|
Object.new
|
|
|
|
Object.new
|
|
|
|
}
|
|
|
|
compact_stats = GC.compact
|
|
|
|
refute_predicate compact_stats[:considered], :empty?
|
|
|
|
refute_predicate compact_stats[:moved], :empty?
|
|
|
|
end
|
|
|
|
|
2019-04-19 21:19:47 -04:00
|
|
|
def memory_location(obj)
|
|
|
|
(Fiddle.dlwrap(obj) >> 1)
|
|
|
|
end
|
|
|
|
|
2019-04-19 21:59:34 -04:00
|
|
|
def big_list(level = 10)
|
|
|
|
if level > 0
|
|
|
|
big_list(level - 1)
|
|
|
|
else
|
|
|
|
1000.times.map {
|
|
|
|
# try to make some empty slots by allocating an object and discarding
|
|
|
|
Object.new
|
|
|
|
Object.new
|
|
|
|
} # likely next to each other
|
|
|
|
end
|
2019-04-19 21:19:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Find an object that's allocated in a slot that had a previous
|
|
|
|
# tenant, and that tenant moved and is still alive
|
|
|
|
def find_object_in_recycled_slot(addresses)
|
|
|
|
new_object = nil
|
|
|
|
|
|
|
|
100_000.times do
|
|
|
|
new_object = Object.new
|
2019-04-19 23:00:49 -04:00
|
|
|
if addresses.index memory_location(new_object)
|
2019-04-19 21:19:47 -04:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
new_object
|
|
|
|
end
|
|
|
|
|
2019-04-23 15:10:52 -04:00
|
|
|
def test_complex_hash_keys
|
|
|
|
list_of_objects = big_list
|
|
|
|
hash = list_of_objects.hash
|
2019-05-09 16:13:56 -04:00
|
|
|
GC.verify_compaction_references(toward: :empty)
|
2019-04-23 15:10:52 -04:00
|
|
|
assert_equal hash, list_of_objects.hash
|
2022-07-07 17:32:35 -04:00
|
|
|
GC.verify_compaction_references(expand_heap: false)
|
2020-01-26 20:46:57 -05:00
|
|
|
assert_equal hash, list_of_objects.hash
|
2019-04-23 15:10:52 -04:00
|
|
|
end
|
2019-09-09 18:46:07 -04:00
|
|
|
|
|
|
|
def test_ast_compacts
|
2021-04-09 00:59:46 -04:00
|
|
|
assert_separately([], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 10, signal: :SEGV)
|
|
|
|
begin;
|
|
|
|
def walk_ast ast
|
|
|
|
children = ast.children.grep(RubyVM::AbstractSyntaxTree::Node)
|
|
|
|
children.each do |child|
|
|
|
|
assert child.type
|
|
|
|
walk_ast child
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ast = RubyVM::AbstractSyntaxTree.parse_file #{__FILE__.dump}
|
|
|
|
assert GC.compact
|
|
|
|
walk_ast ast
|
|
|
|
end;
|
2019-09-09 18:46:07 -04:00
|
|
|
end
|
2019-11-07 15:46:14 -05:00
|
|
|
|
|
|
|
def test_compact_count
|
|
|
|
count = GC.stat(:compact_count)
|
|
|
|
GC.compact
|
|
|
|
assert_equal count + 1, GC.stat(:compact_count)
|
|
|
|
end
|
2022-04-29 18:54:16 -04:00
|
|
|
|
|
|
|
def test_compacting_from_trace_point
|
|
|
|
obj = Object.new
|
|
|
|
def obj.tracee
|
|
|
|
:ret # expected to emit both line and call event from one instruction
|
|
|
|
end
|
|
|
|
|
|
|
|
results = []
|
|
|
|
TracePoint.new(:call, :line) do |tp|
|
|
|
|
results << tp.event
|
|
|
|
GC.verify_compaction_references
|
|
|
|
end.enable(target: obj.method(:tracee)) do
|
|
|
|
obj.tracee
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal([:call, :line], results)
|
|
|
|
end
|
2022-04-06 04:55:23 -04:00
|
|
|
|
2022-06-09 10:59:08 -04:00
|
|
|
def test_moving_arrays_down_size_pools
|
|
|
|
omit if !GC.using_rvargc?
|
|
|
|
assert_separately([], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 10, signal: :SEGV)
|
|
|
|
begin;
|
|
|
|
ARY_COUNT = 500
|
|
|
|
|
|
|
|
GC.verify_compaction_references(expand_heap: true, toward: :empty)
|
|
|
|
|
|
|
|
arys = ARY_COUNT.times.map do
|
|
|
|
ary = "abbbbbbbbbb".chars
|
|
|
|
ary.uniq!
|
|
|
|
end
|
|
|
|
|
|
|
|
stats = GC.verify_compaction_references(expand_heap: true, toward: :empty)
|
|
|
|
assert_operator(stats.dig(:moved_down, :T_ARRAY), :>=, ARY_COUNT)
|
|
|
|
assert(arys) # warning: assigned but unused variable - arys
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_moving_arrays_up_size_pools
|
|
|
|
omit if !GC.using_rvargc?
|
|
|
|
assert_separately([], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 10, signal: :SEGV)
|
|
|
|
begin;
|
|
|
|
ARY_COUNT = 500
|
|
|
|
|
|
|
|
GC.verify_compaction_references(expand_heap: true, toward: :empty)
|
|
|
|
|
|
|
|
ary = "hello".chars
|
|
|
|
arys = ARY_COUNT.times.map do
|
|
|
|
x = []
|
|
|
|
ary.each { |e| x << e }
|
|
|
|
x
|
|
|
|
end
|
|
|
|
|
|
|
|
stats = GC.verify_compaction_references(expand_heap: true, toward: :empty)
|
|
|
|
assert_operator(stats.dig(:moved_up, :T_ARRAY), :>=, ARY_COUNT)
|
|
|
|
assert(arys) # warning: assigned but unused variable - arys
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
2022-07-11 10:09:39 -04:00
|
|
|
def test_moving_objects_between_size_pools
|
|
|
|
assert_separately([], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 10, signal: :SEGV)
|
|
|
|
begin;
|
|
|
|
class Foo
|
|
|
|
def add_ivars
|
|
|
|
10.times do |i|
|
|
|
|
instance_variable_set("@foo" + i.to_s, 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
OBJ_COUNT = 500
|
|
|
|
|
|
|
|
GC.verify_compaction_references(expand_heap: true, toward: :empty)
|
|
|
|
|
|
|
|
ary = OBJ_COUNT.times.map { Foo.new }
|
|
|
|
ary.each(&:add_ivars)
|
|
|
|
|
|
|
|
stats = GC.verify_compaction_references(expand_heap: true, toward: :empty)
|
|
|
|
|
|
|
|
assert_operator(stats[:moved_up][:T_OBJECT], :>=, OBJ_COUNT)
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
2022-06-29 14:04:04 -04:00
|
|
|
def test_moving_strings_up_size_pools
|
2022-07-07 09:17:39 -04:00
|
|
|
omit if !GC.using_rvargc?
|
2022-04-06 04:55:23 -04:00
|
|
|
assert_separately([], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 10, signal: :SEGV)
|
|
|
|
begin;
|
2022-06-29 14:04:04 -04:00
|
|
|
STR_COUNT = 500
|
2022-04-06 04:55:23 -04:00
|
|
|
|
2022-07-07 17:32:35 -04:00
|
|
|
GC.verify_compaction_references(expand_heap: true, toward: :empty)
|
2022-04-06 04:55:23 -04:00
|
|
|
|
2022-06-29 14:04:04 -04:00
|
|
|
str = "a" * GC::INTERNAL_CONSTANTS[:BASE_SLOT_SIZE]
|
|
|
|
ary = STR_COUNT.times.map { "" << str }
|
2022-04-06 04:55:23 -04:00
|
|
|
|
2022-07-07 17:32:35 -04:00
|
|
|
stats = GC.verify_compaction_references(expand_heap: true, toward: :empty)
|
2022-04-06 04:55:23 -04:00
|
|
|
|
2022-06-29 14:04:04 -04:00
|
|
|
assert_operator(stats[:moved_up][:T_STRING], :>=, STR_COUNT)
|
|
|
|
assert(ary) # warning: assigned but unused variable - ary
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_moving_strings_down_size_pools
|
2022-07-07 09:17:39 -04:00
|
|
|
omit if !GC.using_rvargc?
|
2022-06-29 14:04:04 -04:00
|
|
|
assert_separately([], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 10, signal: :SEGV)
|
|
|
|
begin;
|
|
|
|
STR_COUNT = 500
|
|
|
|
|
2022-07-07 17:32:35 -04:00
|
|
|
GC.verify_compaction_references(expand_heap: true, toward: :empty)
|
2022-06-29 14:04:04 -04:00
|
|
|
|
|
|
|
ary = STR_COUNT.times.map { ("a" * GC::INTERNAL_CONSTANTS[:BASE_SLOT_SIZE]).squeeze! }
|
2022-04-06 04:55:23 -04:00
|
|
|
|
2022-07-07 17:32:35 -04:00
|
|
|
stats = GC.verify_compaction_references(expand_heap: true, toward: :empty)
|
2022-04-06 04:55:23 -04:00
|
|
|
|
2022-06-29 14:04:04 -04:00
|
|
|
assert_operator(stats[:moved_down][:T_STRING], :>=, STR_COUNT)
|
|
|
|
assert(ary) # warning: assigned but unused variable - ary
|
2022-04-06 04:55:23 -04:00
|
|
|
end;
|
|
|
|
end
|
2019-04-19 21:19:47 -04:00
|
|
|
end
|