1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
|
# frozen_string_literal: true
require 'test/unit'
if RUBY_PLATFORM =~ /s390x/
warn "Currently, it is known that the compaction does not work well on s390x; contribution is welcome https://github.com/ruby/ruby/pull/5077"
return
end
class TestGCCompact < Test::Unit::TestCase
module CompactionSupportInspector
def supports_compact?
GC.respond_to?(:compact)
end
end
module OmitUnlessCompactSupported
include CompactionSupportInspector
def setup
omit "GC compaction not supported on this platform" unless supports_compact?
super
end
end
include OmitUnlessCompactSupported
class AutoCompact < Test::Unit::TestCase
include OmitUnlessCompactSupported
def test_enable_autocompact
before = GC.auto_compact
GC.auto_compact = true
assert GC.auto_compact
ensure
GC.auto_compact = before
end
def test_disable_autocompact
before = GC.auto_compact
GC.auto_compact = false
refute GC.auto_compact
ensure
GC.auto_compact = before
end
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
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
n = 1_000_000
n.times do
break if count < GC.stat(:compact_count)
list2 << Object.new
end and omit "implicit compaction didn't happen within #{n} objects"
compact_stats = GC.latest_compact_info
refute_predicate compact_stats[:considered], :empty?
refute_predicate compact_stats[:moved], :empty?
ensure
GC.auto_compact = before
end
end
class CompactMethodsNotImplemented < Test::Unit::TestCase
include CompactionSupportInspector
def assert_not_implemented(method, *args)
omit "autocompact is supported on this platform" if supports_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
end
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
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
end
def test_complex_hash_keys
list_of_objects = big_list
hash = list_of_objects.hash
GC.verify_compaction_references(toward: :empty)
assert_equal hash, list_of_objects.hash
GC.verify_compaction_references(expand_heap: false)
assert_equal hash, list_of_objects.hash
end
def test_ast_compacts
assert_separately([], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 10)
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;
end
def test_compact_count
count = GC.stat(:compact_count)
GC.compact
assert_equal count + 1, GC.stat(:compact_count)
end
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
def test_updating_references_for_heap_allocated_shared_arrays
assert_separately(%w[-robjspace], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 10)
begin;
ary = []
50.times { |i| ary << i }
# Pointer in slice should point to buffer of ary
slice = ary[10..40]
# Check that slice is pointing to buffer of ary
assert_include(ObjectSpace.dump(slice), '"shared":true')
# Run compaction to re-embed ary
GC.verify_compaction_references(expand_heap: true, toward: :empty)
# Assert that slice is pointer to updated buffer in ary
assert_equal(10, slice[0])
# Check that slice is still pointing to buffer of ary
assert_include(ObjectSpace.dump(slice), '"shared":true')
end;
end
def test_updating_references_for_embed_shared_arrays
omit if GC::INTERNAL_CONSTANTS[:SIZE_POOL_COUNT] == 1
assert_separately(%w[-robjspace], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 10)
begin;
ary = Array.new(50)
50.times { |i| ary[i] = i }
# Ensure ary is embedded
assert_include(ObjectSpace.dump(ary), '"embedded":true')
slice = ary[10..40]
# Check that slice is pointing to buffer of ary
assert_include(ObjectSpace.dump(slice), '"shared":true')
# Run compaction to re-embed ary
GC.verify_compaction_references(expand_heap: true, toward: :empty)
# Assert that slice is pointer to updated buffer in ary
assert_equal(10, slice[0])
# Check that slice is still pointing to buffer of ary
assert_include(ObjectSpace.dump(slice), '"shared":true')
end;
end
def test_updating_references_for_heap_allocated_frozen_shared_arrays
assert_separately(%w[-robjspace], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 10)
begin;
ary = []
50.times { |i| ary << i }
# Frozen arrays can become shared root without RARRAY_SHARED_ROOT_FLAG
ary.freeze
slice = ary[10..40]
# Check that slice is pointing to buffer of ary
assert_include(ObjectSpace.dump(slice), '"shared":true')
# Run compaction to re-embed ary
GC.verify_compaction_references(expand_heap: true, toward: :empty)
# Assert that slice is pointer to updated buffer in ary
assert_equal(10, slice[0])
# Check that slice is still pointing to buffer of ary
assert_include(ObjectSpace.dump(slice), '"shared":true')
end;
end
def test_updating_references_for_embed_frozen_shared_arrays
omit if GC::INTERNAL_CONSTANTS[:SIZE_POOL_COUNT] == 1
assert_separately(%w[-robjspace], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 10)
begin;
ary = Array.new(50)
50.times { |i| ary[i] = i }
# Frozen arrays can become shared root without RARRAY_SHARED_ROOT_FLAG
ary.freeze
# Ensure ary is embedded
assert_include(ObjectSpace.dump(ary), '"embedded":true')
slice = ary[10..40]
# Check that slice is pointing to buffer of ary
assert_include(ObjectSpace.dump(slice), '"shared":true')
# Run compaction to re-embed ary
GC.verify_compaction_references(expand_heap: true, toward: :empty)
# Assert that slice is pointer to updated buffer in ary
assert_equal(10, slice[0])
# Check that slice is still pointing to buffer of ary
assert_include(ObjectSpace.dump(slice), '"shared":true')
end;
end
def test_moving_arrays_down_heaps
omit if GC::INTERNAL_CONSTANTS[:SIZE_POOL_COUNT] == 1
assert_separately(%w[-robjspace], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 10)
begin;
ARY_COUNT = 50000
GC.verify_compaction_references(expand_heap: true, toward: :empty)
Fiber.new {
$arys = ARY_COUNT.times.map do
ary = "abbbbbbbbbb".chars
ary.uniq!
end
}.resume
stats = GC.verify_compaction_references(expand_heap: true, toward: :empty)
assert_operator(stats.dig(:moved_down, :T_ARRAY) || 0, :>=, ARY_COUNT - 10)
refute_empty($arys.keep_if { |o| ObjectSpace.dump(o).include?('"embedded":true') })
end;
end
def test_moving_arrays_up_heaps
omit if GC::INTERNAL_CONSTANTS[:SIZE_POOL_COUNT] == 1
assert_separately(%w[-robjspace], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 10)
begin;
ARY_COUNT = 50000
GC.verify_compaction_references(expand_heap: true, toward: :empty)
Fiber.new {
ary = "hello".chars
$arys = ARY_COUNT.times.map do
x = []
ary.each { |e| x << e }
x
end
}.resume
stats = GC.verify_compaction_references(expand_heap: true, toward: :empty)
assert_operator(stats.dig(:moved_up, :T_ARRAY) || 0, :>=, ARY_COUNT - 10)
refute_empty($arys.keep_if { |o| ObjectSpace.dump(o).include?('"embedded":true') })
end;
end
def test_moving_objects_between_heaps
omit if GC::INTERNAL_CONSTANTS[:SIZE_POOL_COUNT] == 1
assert_separately(%w[-robjspace], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 60)
begin;
class Foo
def add_ivars
10.times do |i|
instance_variable_set("@foo" + i.to_s, 0)
end
end
end
OBJ_COUNT = 50000
GC.verify_compaction_references(expand_heap: true, toward: :empty)
Fiber.new {
$ary = OBJ_COUNT.times.map { Foo.new }
$ary.each(&:add_ivars)
GC.start
Foo.new.add_ivars
}.resume
stats = GC.verify_compaction_references(expand_heap: true, toward: :empty)
assert_operator(stats.dig(:moved_up, :T_OBJECT) || 0, :>=, OBJ_COUNT - 10)
refute_empty($ary.keep_if { |o| ObjectSpace.dump(o).include?('"embedded":true') })
end;
end
def test_moving_strings_up_heaps
omit if GC::INTERNAL_CONSTANTS[:SIZE_POOL_COUNT] == 1
assert_separately(%w[-robjspace], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 30)
begin;
STR_COUNT = 50000
GC.verify_compaction_references(expand_heap: true, toward: :empty)
Fiber.new {
str = "a" * GC::INTERNAL_CONSTANTS[:BASE_SLOT_SIZE] * 4
$ary = STR_COUNT.times.map { +"" << str }
}.resume
stats = GC.verify_compaction_references(expand_heap: true, toward: :empty)
assert_operator(stats[:moved_up][:T_STRING], :>=, STR_COUNT - 10)
refute_empty($ary.keep_if { |o| ObjectSpace.dump(o).include?('"embedded":true') })
end;
end
def test_moving_strings_down_heaps
omit if GC::INTERNAL_CONSTANTS[:SIZE_POOL_COUNT] == 1
assert_separately(%w[-robjspace], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 30)
begin;
STR_COUNT = 50000
GC.verify_compaction_references(expand_heap: true, toward: :empty)
Fiber.new {
$ary = STR_COUNT.times.map { ("a" * GC::INTERNAL_CONSTANTS[:BASE_SLOT_SIZE] * 4).squeeze! }
}.resume
stats = GC.verify_compaction_references(expand_heap: true, toward: :empty)
assert_operator(stats[:moved_down][:T_STRING], :>=, STR_COUNT - 10)
refute_empty($ary.keep_if { |o| ObjectSpace.dump(o).include?('"embedded":true') })
end;
end
def test_moving_hashes_down_heaps
omit if GC::INTERNAL_CONSTANTS[:SIZE_POOL_COUNT] == 1
# AR and ST hashes are in the same size pool on 32 bit
omit unless RbConfig::SIZEOF["uint64_t"] <= RbConfig::SIZEOF["void*"]
assert_separately(%w[-robjspace], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 30)
begin;
HASH_COUNT = 50000
GC.verify_compaction_references(expand_heap: true, toward: :empty)
Fiber.new {
base_hash = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8 }
$ary = HASH_COUNT.times.map { base_hash.dup }
$ary.each_with_index { |h, i| h[:i] = 9 }
}.resume
stats = GC.verify_compaction_references(expand_heap: true, toward: :empty)
assert_operator(stats[:moved_down][:T_HASH], :>=, HASH_COUNT - 10)
end;
end
def test_moving_objects_between_heaps_keeps_shape_frozen_status
# [Bug #19536]
assert_separately([], "#{<<~"begin;"}\n#{<<~"end;"}")
begin;
class A
def add_ivars
@a = @b = @c = @d = 1
end
def set_a
@a = 10
end
end
a = A.new
a.add_ivars
a.freeze
b = A.new
b.add_ivars
b.set_a # Set the inline cache in set_a
GC.verify_compaction_references(expand_heap: true, toward: :empty)
assert_raise(FrozenError) { a.set_a }
end;
end
end
|