JRuby (please use github issues at http://bugs.jruby.org)
  1. JRuby (please use github issues at http://bugs.jruby.org)
  2. JRUBY-5213

--profile needs to dump even on ^C, other means of getting info, multi-thread

    Details

    • Type: Improvement Improvement
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: JRuby 1.6RC1
    • Fix Version/s: JRuby 1.6RC2
    • Component/s: None
    • Labels:
      None
    • Number of attachments :
      1

      Description

      --profile support was added in 1.5.5, but there are many improvements needed:

      • If the JVM exits with ^C, profile information is not dumped anywhere. For server apps like Rails, this means you can't get profile data out.
      • There's no way to get profile data from a running process. We should add an mbean and a command-line tool to allow dumping live profile data.
      • Only the main thread is profiled. We need to add profile dumping for all threads, and allow showing profile data for threads individually or in aggregate.

        Activity

        Hide
        Douglas Campos added a comment -

        How can we address it? As JRUBY-4705 says, we cannot trap INT, which would interfere with current VM behavior. Perhaps a shutdown hook would work?

        About getting info from a running process, can't we use the same approach visualvm's profiler uses, to "invasively" instrument the running vm?

        Show
        Douglas Campos added a comment - How can we address it? As JRUBY-4705 says, we cannot trap INT, which would interfere with current VM behavior. Perhaps a shutdown hook would work? About getting info from a running process, can't we use the same approach visualvm's profiler uses, to "invasively" instrument the running vm?
        Hide
        Charles Oliver Nutter added a comment -

        I think it's probably ok in this case to trap ^C when the user explicitly passes --profile at the command line. Perhaps we include a warning like "Profiling enabled; ^C shutdown will now dump profile info.". I think if we set up our replacement ^C hook to dump all running threads' profiles and then do a java.lang.System.exit, that would be a good way to handle this.

        See what you can come up with.

        Show
        Charles Oliver Nutter added a comment - I think it's probably ok in this case to trap ^C when the user explicitly passes --profile at the command line. Perhaps we include a warning like "Profiling enabled; ^C shutdown will now dump profile info.". I think if we set up our replacement ^C hook to dump all running threads' profiles and then do a java.lang.System.exit, that would be a good way to handle this. See what you can come up with.
        Hide
        Douglas Campos added a comment -

        ouch, Signal.handle(...) comes from sun.misc. Is there any portable signal handling?

        Show
        Douglas Campos added a comment - ouch, Signal.handle(...) comes from sun.misc. Is there any portable signal handling?
        Hide
        Charles Oliver Nutter added a comment -

        Other than using FFI (which requires us to do the porting), there's no portable way to do signal handling on the JVM. No public API has ever been added for signals.

        I think using sun.misc for this particular case is ok. We already expose signal handling via sun.misc.Signal for Ruby signal support. In fact, doing this all in Ruby would be just fine.

        I'm going to add a simple modification that allows dumping profiles via JMX, and that might advice a simple implementation of ^C profile dumping.

        Show
        Charles Oliver Nutter added a comment - Other than using FFI (which requires us to do the porting), there's no portable way to do signal handling on the JVM. No public API has ever been added for signals. I think using sun.misc for this particular case is ok. We already expose signal handling via sun.misc.Signal for Ruby signal support. In fact, doing this all in Ruby would be just fine. I'm going to add a simple modification that allows dumping profiles via JMX, and that might advice a simple implementation of ^C profile dumping.
        Hide
        Charles Oliver Nutter added a comment -

        Here's a very simple example of intercepting INT (^C) and dumping all threads. I had some trouble with JMX and will not fiddle with it more tonight.

        require 'jruby'
        
        trap 'INT' do
          runtime = JRuby.runtime
          runtime.thread_service.ruby_thread_map.each do |t, rubythread|
            context = JRuby.reference(rubythread).context
            context.profile_data.print_profile(
              context,
              runtime.profiled_names,
              runtime.profiled_methods,
              java.lang.System.out)
          end
          exit
        end
        
        5.times {
          Thread.new { (1..100).each {|i| i ** 2}; sleep }
        }
        
        sleep
        
        Show
        Charles Oliver Nutter added a comment - Here's a very simple example of intercepting INT (^C) and dumping all threads. I had some trouble with JMX and will not fiddle with it more tonight. require 'jruby' trap 'INT' do runtime = JRuby.runtime runtime.thread_service.ruby_thread_map.each do |t, rubythread| context = JRuby.reference(rubythread).context context.profile_data.print_profile( context, runtime.profiled_names, runtime.profiled_methods, java.lang.System.out) end exit end 5.times { Thread.new { (1..100).each {|i| i ** 2}; sleep } } sleep
        Hide
        Douglas Campos added a comment - - edited
        Show
        Douglas Campos added a comment - - edited tentative fix available here too => https://github.com/qmx/jruby/commit/25ff62e98799d5cb3fbe9306de59252cdeb7b5ca
        Hide
        Charles Oliver Nutter added a comment -

        I tweaked Douglas's patch for recent API changes and pushed it in. It works, it's good enough for now

        commit f18d84ab6d68bdc42372f2a8f99acfb6bd1d0354
        Author: Douglas Campos <qmx@qmx.me>
        Date: Wed Jan 19 02:55:37 2011 -0200

        profiler should dump all profile info on SIGINT (^C)

        Signed-off-by: Charles Oliver Nutter <headius@headius.com>

        commit 6dc571876ebf0f4210812f49d8a418e24141a17d
        Author: Charles Oliver Nutter <headius@headius.com>
        Date: Mon Feb 7 15:50:08 2011 -0600

        Clean up logic for ^C profile dumping to reflect changes to profile API.

        Show
        Charles Oliver Nutter added a comment - I tweaked Douglas's patch for recent API changes and pushed it in. It works, it's good enough for now commit f18d84ab6d68bdc42372f2a8f99acfb6bd1d0354 Author: Douglas Campos <qmx@qmx.me> Date: Wed Jan 19 02:55:37 2011 -0200 profiler should dump all profile info on SIGINT (^C) Signed-off-by: Charles Oliver Nutter <headius@headius.com> commit 6dc571876ebf0f4210812f49d8a418e24141a17d Author: Charles Oliver Nutter <headius@headius.com> Date: Mon Feb 7 15:50:08 2011 -0600 Clean up logic for ^C profile dumping to reflect changes to profile API.

          People

          • Assignee:
            Charles Oliver Nutter
            Reporter:
            Charles Oliver Nutter
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: