2018-08-09 05:58:07 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require 'test/unit'
|
2019-07-09 08:04:07 -04:00
|
|
|
require_relative '../lib/jit_support'
|
2018-08-09 05:58:07 -04:00
|
|
|
|
2018-10-20 04:06:50 -04:00
|
|
|
return if RbConfig::CONFIG["MJIT_SUPPORT"] == 'no'
|
|
|
|
|
2022-05-21 00:34:46 -04:00
|
|
|
class TestRubyVMMJIT < Test::Unit::TestCase
|
2018-08-09 05:58:07 -04:00
|
|
|
include JITSupport
|
|
|
|
|
2018-10-19 10:26:29 -04:00
|
|
|
def setup
|
|
|
|
unless JITSupport.supported?
|
2022-01-04 03:25:30 -05:00
|
|
|
omit 'JIT seems not supported on this platform'
|
2018-10-19 10:26:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-09 05:58:07 -04:00
|
|
|
def test_pause
|
|
|
|
out, err = eval_with_jit(<<~'EOS', verbose: 1, min_calls: 1, wait: false)
|
|
|
|
i = 0
|
|
|
|
while i < 5
|
|
|
|
eval("def mjit#{i}; end; mjit#{i}")
|
|
|
|
i += 1
|
|
|
|
end
|
2021-12-14 02:07:46 -05:00
|
|
|
print RubyVM::MJIT.pause
|
|
|
|
print RubyVM::MJIT.pause
|
2018-08-09 05:58:07 -04:00
|
|
|
while i < 10
|
|
|
|
eval("def mjit#{i}; end; mjit#{i}")
|
|
|
|
i += 1
|
|
|
|
end
|
2021-12-14 02:07:46 -05:00
|
|
|
print RubyVM::MJIT.pause # no JIT here
|
2018-08-09 05:58:07 -04:00
|
|
|
EOS
|
|
|
|
assert_equal('truefalsefalse', out)
|
2018-08-11 10:09:26 -04:00
|
|
|
assert_equal(
|
|
|
|
5, err.scan(/#{JITSupport::JIT_SUCCESS_PREFIX}/).size,
|
|
|
|
"unexpected stdout:\n```\n#{out}```\n\nstderr:\n```\n#{err}```",
|
|
|
|
)
|
2018-08-09 05:58:07 -04:00
|
|
|
end
|
|
|
|
|
2019-09-25 23:53:41 -04:00
|
|
|
def test_pause_waits_until_compaction
|
|
|
|
out, err = eval_with_jit(<<~'EOS', verbose: 1, min_calls: 1, wait: false)
|
|
|
|
def a() end; a
|
|
|
|
def b() end; b
|
2021-12-14 02:07:46 -05:00
|
|
|
RubyVM::MJIT.pause
|
2019-09-25 23:53:41 -04:00
|
|
|
EOS
|
|
|
|
assert_equal(
|
|
|
|
2, err.scan(/#{JITSupport::JIT_SUCCESS_PREFIX}/).size,
|
|
|
|
"unexpected stdout:\n```\n#{out}```\n\nstderr:\n```\n#{err}```",
|
|
|
|
)
|
|
|
|
assert_equal(
|
|
|
|
1, err.scan(/#{JITSupport::JIT_COMPACTION_PREFIX}/).size,
|
|
|
|
"unexpected stdout:\n```\n#{out}```\n\nstderr:\n```\n#{err}```",
|
|
|
|
) unless RUBY_PLATFORM.match?(/mswin|mingw/) # compaction is not supported on Windows yet
|
|
|
|
end
|
|
|
|
|
2022-06-15 01:40:49 -04:00
|
|
|
def test_pause_after_waitall
|
|
|
|
out, err = eval_with_jit(<<~'EOS', verbose: 1, min_calls: 1, wait: false)
|
|
|
|
def test() = nil
|
|
|
|
test
|
|
|
|
Process.waitall
|
|
|
|
print RubyVM::MJIT.pause
|
|
|
|
EOS
|
|
|
|
assert_equal(
|
|
|
|
1, err.scan(/#{JITSupport::JIT_SUCCESS_PREFIX}/).size,
|
|
|
|
"unexpected stdout:\n```\n#{out}```\n\nstderr:\n```\n#{err}```",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-10-19 11:08:55 -04:00
|
|
|
def test_pause_does_not_hang_on_full_units
|
|
|
|
out, _ = eval_with_jit(<<~'EOS', verbose: 1, min_calls: 1, max_cache: 10, wait: false)
|
|
|
|
i = 0
|
|
|
|
while i < 11
|
|
|
|
eval("def mjit#{i}; end; mjit#{i}")
|
|
|
|
i += 1
|
|
|
|
end
|
2021-12-14 02:07:46 -05:00
|
|
|
print RubyVM::MJIT.pause
|
2018-10-19 11:08:55 -04:00
|
|
|
EOS
|
|
|
|
assert_equal('true', out)
|
|
|
|
end
|
|
|
|
|
2018-08-09 05:58:07 -04:00
|
|
|
def test_pause_wait_false
|
|
|
|
out, err = eval_with_jit(<<~'EOS', verbose: 1, min_calls: 1, wait: false)
|
|
|
|
i = 0
|
|
|
|
while i < 10
|
|
|
|
eval("def mjit#{i}; end; mjit#{i}")
|
|
|
|
i += 1
|
|
|
|
end
|
2021-12-14 02:07:46 -05:00
|
|
|
print RubyVM::MJIT.pause(wait: false)
|
|
|
|
print RubyVM::MJIT.pause(wait: false)
|
2018-08-09 05:58:07 -04:00
|
|
|
EOS
|
|
|
|
assert_equal('truefalse', out)
|
2022-06-15 12:40:54 -04:00
|
|
|
if RUBY_PLATFORM.match?(/mswin|mingw/) # MJIT synchronously compiles methods on Windows
|
|
|
|
assert_equal(10, err.scan(/#{JITSupport::JIT_SUCCESS_PREFIX}/).size)
|
|
|
|
else
|
|
|
|
assert_equal(true, err.scan(/#{JITSupport::JIT_SUCCESS_PREFIX}/).size < 10)
|
|
|
|
end
|
2018-08-09 05:58:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_resume
|
|
|
|
out, err = eval_with_jit(<<~'EOS', verbose: 1, min_calls: 1, wait: false)
|
2021-12-14 02:07:46 -05:00
|
|
|
print RubyVM::MJIT.resume
|
|
|
|
print RubyVM::MJIT.pause
|
|
|
|
print RubyVM::MJIT.resume
|
|
|
|
print RubyVM::MJIT.resume
|
|
|
|
print RubyVM::MJIT.pause
|
2018-08-09 05:58:07 -04:00
|
|
|
EOS
|
|
|
|
assert_equal('falsetruetruefalsetrue', out)
|
|
|
|
assert_equal(0, err.scan(/#{JITSupport::JIT_SUCCESS_PREFIX}/).size)
|
|
|
|
end
|
|
|
|
end
|