Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby & JVM: A (JRuby) Love Story - RubyConfTh

Ruby & JVM: A (JRuby) Love Story - RubyConfTh

Yarden Laifenfeld

December 12, 2022
Tweet

More Decks by Yarden Laifenfeld

Other Decks in Programming

Transcript

  1. Ruby & JVM:
    A (Jruby) Love Story
    Yarden Laifenfeld
    @yardenlaif
    @[email protected]

    View full-size slide

  2. 2
    Who Am I?
    • Software Engineer at Rookout
    • Background in low level C
    programming in linux IOT
    environments
    • Ruby, Java, Go
    • C#, Python, JavaScript, C++

    View full-size slide

  3. Non Breaking Breakpoints
    3

    View full-size slide

  4. What is JRuby?
    4
    “ JRuby is a 100% Java implementation of
    the Ruby programming language. It is Ruby
    for the JVM. ”
    - the JRuby wiki

    View full-size slide

  5. Running an app
    5
    ● rails server -p 8080 -e development

    View full-size slide

  6. Running an app
    6
    rvm install jruby
    rvm use jruby
    bundle install

    View full-size slide

  7. Running an app
    7

    View full-size slide

  8. Running an app
    8
    3 business days
    later…

    View full-size slide

  9. Running an app
    9
    bundle config unset force_ruby_platform

    View full-size slide

  10. Running an app
    10
    ➜ tutorial-ruby git:(master) ✗ jruby -S rails server -p 8080 -e development
    => Booting Puma
    => Rails 6.0.3.4 application starting in development
    => Run `rails server --help` for more startup options
    Puma starting in single mode...
    * Version 4.3.7 (jruby 9.2.9.0 - ruby 2.5.7), codename: Mysterious Traveller
    * Min threads: 5, max threads: 5
    * Environment: development
    * Listening on tcp://[::1]:8080
    * Listening on tcp://127.0.0.1:8080
    Use Ctrl-C to stop

    View full-size slide

  11. Running an app
    11
    ➜ tutorial-ruby git:(master) ✗ jruby -S rails server -p 8080 -e development
    => Booting Puma
    => Rails 6.0.3.4 application starting in development
    => Run `rails server --help` for more startup options
    Puma starting in single mode...
    * Version 4.3.7 (jruby 9.2.9.0 - ruby 2.5.7), codename: Mysterious Traveller
    * Min threads: 5, max threads: 5
    * Environment: development
    * Listening on tcp://[::1]:8080
    * Listening on tcp://127.0.0.1:8080
    Use Ctrl-C to stop

    View full-size slide

  12. Running an app
    12
    ➜ tutorial-ruby git:(master) ✗ jruby -S rails server -p 8080 -e development
    => Booting Puma
    => Rails 6.0.3.4 application starting in development
    => Run `rails server --help` for more startup options
    Puma starting in single mode...
    * Version 4.3.7 (jruby 9.2.9.0 - ruby 2.5.7), codename: Mysterious Traveller
    * Min threads: 5, max threads: 5
    * Environment: development
    * Listening on tcp://[::1]:8080
    * Listening on tcp://127.0.0.1:8080
    Use Ctrl-C to stop

    View full-size slide

  13. Supporting JRuby in one of
    Rookout’s debuggers
    13

    View full-size slide

  14. Decision Time
    14
    Java or Ruby

    View full-size slide

  15. Java
    15
    ● JVM API
    ● Instrument bytecode
    ● No debugger
    ● Ruby API
    ● Use built in functionality
    ● Pry already exists
    Ruby

    View full-size slide

  16. Decision Time
    16
    Java or Ruby

    View full-size slide

  17. Supporting JRuby
    17
    ❏ Running an app
    ❏ Adding a breakpoint
    ❏ Collecting data

    View full-size slide

  18. Running an app
    18
    ● bundle add rookout
    ● require 'rookout'
    ::Rookout.start

    View full-size slide

  19. Running an app
    19
    ➜ tutorial-ruby git:(master) ✗ jruby -S rails server -p 8080 -e
    development

    View full-size slide

  20. Running an app
    20
    ➜ tutorial-ruby git:(master) ✗ jruby -S rails server -p 8080 -e
    development
    [Rookout] Successfully connected to controller.

    View full-size slide

  21. Running an app
    21

    View full-size slide

  22. TracePoints
    22

    View full-size slide

  23. TracePoints
    23
    tracepoint = Tracepoint.new(:line) do
    # Collect locals, stacktrace...
    end

    View full-size slide

  24. TracePoints
    24
    t = TracePoint.new(:line) { |tp| p tp }
    t.enable(target: method(:m1))

    View full-size slide

  25. TracePoints
    25

    View full-size slide

  26. TracePoints
    26
    trace_point = TracePoint.new :script_compiled do |tp|
    iseq = tp.instruction_sequence
    @iseqs << iseq
    end
    trace_point.enable

    View full-size slide

  27. TracePoints
    27
    @iseqs.each do |iseq|
    if iseq.absolute_path == filename
    t = TracePoint.new :line do |tp|
    # Collect locals, stacktrace...
    end
    t.enable target: iseq, target_line: lineno
    end
    end

    View full-size slide

  28. TracePoints
    28

    View full-size slide

  29. TracePoints in JRuby
    29

    View full-size slide

  30. Workaround
    30
    tracepoint.enable

    View full-size slide

  31. Supporting JRuby
    31
    ✗ Running an app
    ❏ Adding a breakpoint
    ❏ Collecting data

    View full-size slide

  32. Decision Time
    32
    Java or Ruby

    View full-size slide

  33. Decision Time
    33
    Java or Ruby

    View full-size slide

  34. Supporting JRuby
    34
    ❏ Running an app
    ❏ Adding a breakpoint
    ❏ Collecting data

    View full-size slide

  35. Running an app - Java Agents
    35
    ● -javaagent flag
    ● premain entrypoint
    ● Instrumentation API
    App
    JavaAgent
    Instrumentation API

    View full-size slide

  36. Running an app
    36
    ➜ tutorial-ruby git:(master) ✗ jruby -J-javaagent:rook.jar -S rails
    server -p 8080 -e development

    View full-size slide

  37. Running an app
    37
    ➜ tutorial-ruby git:(master) ✗ jruby -J-javaagent:rook.jar -S rails
    server -p 8080 -e development
    [Rookout] Successfully connected to controller.

    View full-size slide

  38. Running an app
    38
    ➜ tutorial-ruby git:(master) ✗ jruby -J-javaagent:rook.jar -S rails server -p 8080 -e
    development
    [Rookout] Successfully connected to controller.
    => Booting Puma
    => Rails 6.0.6 application starting in development
    => Run `rails server --help` for more startup options
    Puma starting in single mode...
    * Version 4.3.12 (jruby 9.3.8.0 - ruby 2.6.8), codename: Mysterious Traveller
    * Min threads: 5, max threads: 5
    * Environment: development
    * Listening on tcp://[::1]:8080
    * Listening on tcp://127.0.0.1:8080
    Use Ctrl-C to stop

    View full-size slide

  39. Supporting JRuby
    39
    ✓ Running an app
    ❏ Adding a breakpoint
    ❏ Collecting data

    View full-size slide

  40. Adding a breakpoint - Class Loaders
    40
    JVM
    Compiler
    Code Bytecode

    View full-size slide

  41. Adding a breakpoint - Class Loaders
    41
    JVM
    32 | …
    33 | new Item()
    34 | …
    Class
    Loader
    Get Item
    class bytecode
    Return Item
    class bytecode

    View full-size slide

  42. Java Agent - ClassFileTransformer
    42
    JVM
    32 | …
    33 | new Item()
    34 | … Class Loader
    Get Item class
    bytecode
    Return Item
    class bytecode
    Class File
    Transformer
    Return new Item
    class bytecode
    Item class bytecode

    View full-size slide

  43. 50
    A few, spaced out invocations
    Many, rapid invocations
    Class isn’t loaded
    Class is loaded

    View full-size slide

  44. 51
    A few, spaced out invocations
    Many, rapid invocations
    Class isn’t loaded
    Class is loaded
    JIT

    View full-size slide

  45. JRuby Compilation Modes
    52
    ● Interpretation
    ● JIT
    ● Compilation

    View full-size slide

  46. JRuby Compilation Modes
    53
    compile.mode=FORCE

    View full-size slide

  47. Supporting JRuby
    57
    ✓ Running an app
    ✓ Adding a breakpoint
    ❏ Collecting data

    View full-size slide

  48. Collecting data
    59

    View full-size slide

  49. Collecting data
    60

    View full-size slide

  50. Supporting JRuby
    61
    ✓ Running an app
    ✓ Adding a breakpoint
    ✓ Collecting data

    View full-size slide

  51. Things I Learned
    62
    ● We can do awesome things
    ● Smash your keyboard
    ● Do meet your heroes

    View full-size slide

  52. 63
    Should you use JRuby?

    View full-size slide

  53. JRuby Advantages
    64
    ● Integration with Java libraries
    ● Running atop the JVM
    ○ Threads
    ○ Long running applications

    View full-size slide

  54. CRuby Advantages
    65
    ● Larger community
    ● More features

    View full-size slide

  55. 66
    Should you use JRuby?
    I don’t know, you choose.

    View full-size slide

  56. Thank You!
    @[email protected]
    yardenlaif.com
    67

    View full-size slide