Feature #9190

Expose serial helper macros

Added by Simeon Willbanks 5 months ago. Updated 5 months ago.

[ruby-core:58750]
Status:Closed
Priority:Normal
Assignee:-
Category:core
Target version:-

Description

I've defined RubyVM::InstructionHelper with singleton methods #methodserial and #constantserial. They delegate to helper macros GETMETHODSERIAL() and GETCONSTANTSERIAL().

With these methods, users have visibility into the method and constant caches. If it makes sense, I hope this patch is included in Ruby 2.1.0.

def constantsbusted?(&blk)
starting = RubyVM::InstructionHelper.constant
serial
yield
ending = RubyVM::InstructionHelper.constant_serial
ending > starting
end

busted = constants_busted? do
class Foo; end
end
puts busted.inspect

true

https://github.com/ruby/ruby/pull/462.patch

Thanks for considering; any input is appreciated!

Associated revisions

Revision 44062
Added by Aman Gupta 5 months ago

vm.c: add RubyVM.stat for accessing cache serials

  • vm.c (rubyvmstat): add RubyVM.stat() for access to internal cache counters. this methods behaves like GC.stat, accepting an optional hash or symbol argument. [Bug #9190]
  • test/ruby/test_rubyvm.rb: test for new method

Revision 44103
Added by Aman Gupta 4 months ago

vm_method.c: add new ruby::method-cache-clear dtrace probe

  • vmmethod.c (rbclearmethodcachebyclass): fire ruby::method-cache-clear probe on global or klass-level method cache clear [Bug #9190]
  • probes.d (provider ruby): new dtrace probe
  • doc/dtrace_probes.rdoc: docs for new probe
  • test/dtrace/testmethodcache.rb: test for new probe

History

#1 Updated by Simeon Willbanks 5 months ago

After addressing a Pull Request comment, the methods are defined on RubyVM:

RubyVM.methodserial
RubyVM.constant
serial

#2 Updated by Nobuyoshi Nakada 5 months ago

  • Category set to core
  • % Done changed from 100 to 0

Why do you need those internal values?

#3 Updated by Nobuyoshi Nakada 5 months ago

  • Status changed from Open to Feedback

#4 Updated by Charlie Somerville 5 months ago

Why do you need those internal values?

We have something similar in our custom build of Ruby. It's very useful for performance metrics.

#5 Updated by Aman Gupta 5 months ago

I would like to see these exposed as well. They provide visibility into VM behavior, and are very useful for measuring how many constant or method table invalidations are happening in large applications.

#6 Updated by Simeon Willbanks 5 months ago

@charliesome and @tmm1, thanks for the support, and I concur.

@nobu I've hacked together a gem. It's an alpha version, and it depends upon this patch.

https://github.com/simeonwillbanks/busted

The gem is a continuation of the description's example. Basically, an application wraps code in a block. Once the block executes, it reports whether or not the constant or method cache was busted. The tests have specific examples.

https://github.com/simeonwillbanks/busted/blob/master/test/busted_test.rb

Hope this helps. If you have anymore questions, please ask. Thanks!

#7 Updated by Aman Gupta 5 months ago

My main use-case for exposing these serials is to collect them automatically before and after each request in a rails app and graph how many times the caches are busted per request.

I would also like to see a Tracepoint or other hook that fires whenever the cache is busted. This would allow capturing a stack trace to see exactly where the cache busting is happening.

#8 Updated by Aman Gupta 5 months ago

If we're opposed to adding multiple methods to RubyVM, maybe a RubyVM.stat will suffice. It can behave the same way that GC.stat does (optional hash argument to avoid allocations).

#9 Updated by Simeon Willbanks 5 months ago

I'm cool with RubyVM.stat; copying the GC.stat implementation is a good idea.

RubyVM.stat
=> {
:methodserial => 200,
:constant
serial => 210,
}

#10 Updated by Nobuyoshi Nakada 5 months ago

It sounds better than exposing those values directly.

#11 Updated by Simeon Willbanks 5 months ago

OK, cool. I'll refactor the Pull Request. Thanks!

#12 Updated by Aman Gupta 5 months ago

  • Status changed from Feedback to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r44062.
Simeon, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


vm.c: add RubyVM.stat for accessing cache serials

  • vm.c (rubyvmstat): add RubyVM.stat() for access to internal cache counters. this methods behaves like GC.stat, accepting an optional hash or symbol argument. [Bug #9190]
  • test/ruby/test_rubyvm.rb: test for new method

#13 Updated by Simeon Willbanks 5 months ago

Thanks everyone!

Also available in: Atom PDF