Skip to content

Commit

Permalink
GC part already loaded from native -> at least delay JI loading on use
Browse files Browse the repository at this point in the history
... we simply generate the Java listener on demand as its to be used
  • Loading branch information
kares committed Jun 1, 2018
1 parent dcd5d62 commit 7b8d196
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
2 changes: 1 addition & 1 deletion core/src/main/ruby/jruby/kernel.rb
Expand Up @@ -26,7 +26,7 @@ module JRuby
load 'jruby/kernel/enumerable.rb'
load 'jruby/kernel/io.rb'
load 'jruby/kernel/time.rb'
autoload :GC, 'jruby/kernel/gc.rb'
load 'jruby/kernel/gc.rb'
load 'jruby/kernel/range.rb'
load 'jruby/kernel/file.rb'
load 'jruby/kernel/basicobject.rb'
Expand Down
55 changes: 31 additions & 24 deletions core/src/main/ruby/jruby/kernel/gc.rb
Expand Up @@ -70,12 +70,10 @@ def self.stat(all_stats_or_key = {})
module Profiler
def self.__setup__
@profiler ||= begin
java.lang.Class.forName('com.sun.management.GarbageCollectionNotificationInfo')

# class exists, proceed with GC notification version
@profiler = NotifiedProfiler.new
rescue java.lang.ClassNotFoundException
@profiler = NormalProfiler.new
NotifiedProfiler.new!
# class exists and Java dependecies are also present, proceed with GC notification version
rescue NameError
NormalProfiler.new
end
end

Expand Down Expand Up @@ -115,35 +113,42 @@ def self.total_time
end

begin
javax.management.NotificationListener # try to access
class NotifiedProfiler
HEADER = " ID Type Timestamp(sec) Before(kB) After(kB) Delta(kB) Heap(kB) GC Time(ms) \n"
FORMAT = "%5d %-20s %19.4f %13i %13i %12i %15i %20.10f\n"

class GCListener
include javax.management.NotificationListener

def initialize
@lines = []
end

attr_accessor :lines

def handleNotification(notification, o)
lines << notification
end

def clear
lines.clear
end
def self.new!
javax.management.NotificationListener # try to access
com.sun.management.GarbageCollectionNotificationInfo
new
end

def enabled?
@gc_listener != nil
end

def enable
@gc_listener ||= GCListener.new
@gc_listener ||= begin
# delay JI class-loading, generate listener on first use :
gc_listener = Class.new do # GCListener
include javax.management.NotificationListener

def initialize
@lines = []
end

attr_accessor :lines

def handleNotification(notification, o)
lines << notification
end

def clear
lines.clear
end
end
gc_listener.new
end
java.lang.management.ManagementFactory.garbage_collector_mx_beans.each do |gc_bean|
gc_bean.add_notification_listener @gc_listener, nil, nil
end
Expand Down Expand Up @@ -233,6 +238,7 @@ def total_time
return duration / 1000.0
end
end
private_constant :NotifiedProfiler
rescue Exception
# ignore, leave it undefined
end
Expand Down Expand Up @@ -278,6 +284,7 @@ def total_time
(time - @start_time) / 1000.0
end
end
private_constant :NormalProfiler
end
end
end

0 comments on commit 7b8d196

Please sign in to comment.