From f9a2440866a2df05fcbacf31c4f4e5e18b996e15 Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Tue, 9 Jul 2019 21:04:07 +0900 Subject: Restore support library for only test files. --- test/lib/jit_support.rb | 68 +++++++++++++++++++++++++++++++++++++++++++ test/ruby/test_jit.rb | 2 +- test/ruby/test_rubyoptions.rb | 2 +- test/ruby/test_rubyvm_mjit.rb | 2 +- test/test_open3.rb | 2 +- 5 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 test/lib/jit_support.rb (limited to 'test') diff --git a/test/lib/jit_support.rb b/test/lib/jit_support.rb new file mode 100644 index 0000000000..c1624cc3b2 --- /dev/null +++ b/test/lib/jit_support.rb @@ -0,0 +1,68 @@ +require 'rbconfig' + +module JITSupport + JIT_TIMEOUT = 600 # 10min for each... + JIT_SUCCESS_PREFIX = 'JIT success \(\d+\.\dms\)' + UNSUPPORTED_COMPILERS = [ + %r[\Aicc\b], + %r[\A/opt/developerstudio\d+\.\d+/bin/cc\z], + ] + + module_function + # Run Ruby script with --jit-wait (Synchronous JIT compilation). + # Returns [stdout, stderr] + def eval_with_jit(env = nil, script, **opts) + stdout, stderr = nil, nil + # retry 3 times while cc1 error happens. + 3.times do |i| + stdout, stderr, status = eval_with_jit_without_retry(env, script, **opts) + assert_equal(true, status.success?, "Failed to run script with JIT:\n#{code_block(script)}\nstdout:\n#{code_block(stdout)}\nstderr:\n#{code_block(stderr)}") + break unless retried_stderr?(stderr) + end + [stdout, stderr] + end + + def eval_with_jit_without_retry(env = nil, script, verbose: 0, min_calls: 5, save_temps: false, max_cache: 1000, wait: true, timeout: JIT_TIMEOUT) + args = [ + '--disable-gems', "--jit-verbose=#{verbose}", + "--jit-min-calls=#{min_calls}", "--jit-max-cache=#{max_cache}", + ] + args << '--jit-wait' if wait + args << '--jit-save-temps' if save_temps + args << '-e' << script + base_env = { 'MJIT_SEARCH_BUILD_DIR' => 'true' } # workaround to skip requiring `make install` for `make test-all` + if preloadenv = RbConfig::CONFIG['PRELOADENV'] and !preloadenv.empty? + so = "mjit_build_dir.#{RbConfig::CONFIG['SOEXT']}" + base_env[preloadenv] = File.realpath(so) rescue nil + end + args.unshift(env ? base_env.merge!(env) : base_env) + EnvUtil.invoke_ruby(args, + '', true, true, timeout: timeout, + ) + end + + def supported? + return @supported if defined?(@supported) + @supported = UNSUPPORTED_COMPILERS.all? do |regexp| + !regexp.match?(RbConfig::CONFIG['CC']) + end + end + + def remove_mjit_logs(stderr) + if RubyVM::MJIT.enabled? # utility for -DFORCE_MJIT_ENABLE + stderr.gsub(/^MJIT warning: Skipped to compile unsupported instruction: \w+\n/m, '') + else + stderr + end + end + + def code_block(code) + %Q["""\n#{code}\n"""\n\n] + end + + # We're retrying cc1 not found error on gcc, which should be solved in the future but ignored for now. + def retried_stderr?(stderr) + RbConfig::CONFIG['CC'].start_with?('gcc') && + stderr.include?("error trying to exec 'cc1': execvp: No such file or directory") + end +end diff --git a/test/ruby/test_jit.rb b/test/ruby/test_jit.rb index 08494cbbbb..0cfc980aac 100644 --- a/test/ruby/test_jit.rb +++ b/test/ruby/test_jit.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require 'test/unit' require 'tmpdir' -require_relative '../../tool/lib/jit_support' +require_relative '../lib/jit_support' return if RbConfig::CONFIG["MJIT_SUPPORT"] == 'no' diff --git a/test/ruby/test_rubyoptions.rb b/test/ruby/test_rubyoptions.rb index 0d22ba21ef..c92d0e8212 100644 --- a/test/ruby/test_rubyoptions.rb +++ b/test/ruby/test_rubyoptions.rb @@ -4,7 +4,7 @@ require 'test/unit' require 'timeout' require 'tmpdir' require 'tempfile' -require_relative '../../tool/lib/jit_support' +require_relative '../lib/jit_support' class TestRubyOptions < Test::Unit::TestCase NO_JIT_DESCRIPTION = diff --git a/test/ruby/test_rubyvm_mjit.rb b/test/ruby/test_rubyvm_mjit.rb index ae55d58622..12772320f5 100644 --- a/test/ruby/test_rubyvm_mjit.rb +++ b/test/ruby/test_rubyvm_mjit.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true require 'test/unit' -require_relative '../../tool/lib/jit_support' +require_relative '../lib/jit_support' return if RbConfig::CONFIG["MJIT_SUPPORT"] == 'no' diff --git a/test/test_open3.rb b/test/test_open3.rb index c3a4e0a2bc..24bd08e597 100644 --- a/test/test_open3.rb +++ b/test/test_open3.rb @@ -2,7 +2,7 @@ require 'test/unit' require 'open3' -require_relative '../tool/lib/jit_support' +require_relative 'lib/jit_support' class TestOpen3 < Test::Unit::TestCase RUBY = EnvUtil.rubybin -- cgit v1.2.3