summaryrefslogtreecommitdiff
path: root/test/ruby/test_rubyvm_mjit.rb
blob: ef7475670cb52326c45747f089baeb51bc64230a (plain)
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
# frozen_string_literal: true
require 'test/unit'
require_relative '../lib/jit_support'

return if RbConfig::CONFIG["MJIT_SUPPORT"] == 'no'

class TestRubyVMMJIT < Test::Unit::TestCase
  include JITSupport

  def setup
    unless JITSupport.supported?
      skip 'JIT seems not supported on this platform'
    end
  end

  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
      print RubyVM::MJIT.pause
      print RubyVM::MJIT.pause
      while i < 10
        eval("def mjit#{i}; end; mjit#{i}")
        i += 1
      end
      print RubyVM::MJIT.pause # no JIT here
    EOS
    assert_equal('truefalsefalse', out)
    assert_equal(
      5, err.scan(/#{JITSupport::JIT_SUCCESS_PREFIX}/).size,
      "unexpected stdout:\n```\n#{out}```\n\nstderr:\n```\n#{err}```",
    )
  end

  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
      RubyVM::MJIT.pause
    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

  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
      print RubyVM::MJIT.pause
    EOS
    assert_equal('true', out)
  end

  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
      print RubyVM::MJIT.pause(wait: false)
      print RubyVM::MJIT.pause(wait: false)
    EOS
    assert_equal('truefalse', out)
    assert_equal(true, err.scan(/#{JITSupport::JIT_SUCCESS_PREFIX}/).size < 10)
  end

  def test_resume
    out, err = eval_with_jit(<<~'EOS', verbose: 1, min_calls: 1, wait: false)
      print RubyVM::MJIT.resume
      print RubyVM::MJIT.pause
      print RubyVM::MJIT.resume
      print RubyVM::MJIT.resume
      print RubyVM::MJIT.pause
    EOS
    assert_equal('falsetruetruefalsetrue', out)
    assert_equal(0, err.scan(/#{JITSupport::JIT_SUCCESS_PREFIX}/).size)
  end
end