Skip to content

Instantly share code, notes, and snippets.

@kares
Created September 27, 2019 12:29
Show Gist options
  • Save kares/7a15f344485042c6e27a7ae4e5d33cc1 to your computer and use it in GitHub Desktop.
Save kares/7a15f344485042c6e27a7ae4e5d33cc1 to your computer and use it in GitHub Desktop.
force -Xjit.max setting on JRuby <= 9.2.8
# force -Xjit.max setting on JRuby <= 9.2.8
#
# forcing is not perfect -
# counter for existing methods will still keep incrementing but compilation is expected to halt
#
# @note -Xjit.max has no effect on JRuby 9K - its only enforced since 9.2.9
#
(Class.new do
JIT_MAX = ENV_JAVA['jruby.jit.max'].to_i
if JRUBY_VERSION >= '9.2.9'
fail "-Xjit.max enforcing no longer necessary on JRuby #{JRUBY_VERSION}, remove #{__FILE__}"
end
def self.start_thread
return unless JIT_MAX > 0
Thread.start do
loop { sleep 90; new.check_and_force_jit_max }
end
end
def check_and_force_jit_max
success_count = mbean_server.getAttribute jit_compiler_mbean_name, 'SuccessCount'
if success_count > JIT_MAX
org.jruby.util.cli.Options::JIT_THRESHOLD.force '1000000000'
warn "halting JRuby JIT by forcing JIT_THRESHOLD to #{org.jruby.util.cli.Options::JIT_THRESHOLD.load}"
raise StopIteration
end
end
def jit_compiler_mbean_name
name = javax.management.ObjectName.getInstance("org.jruby:*,service=JITCompiler")
mbean_server.queryNames(name, nil).first
end
def mbean_server
java.lang.management.ManagementFactory.getPlatformMBeanServer
end
end.start_thread) if defined?(JRUBY_VERSION)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment