ruby--ruby/test/ruby
Eileen M. Uchitelle 473ee328c5
Add ability to trace exit locations in yjit (#5970)
When running with `--yjit-stats` turned on, yjit can inform the user
what the most common exits are. While this is useful information it
doesn't tell you the source location of the code that exited or what the
code that exited looks like. This change intends to fix that.

To use the feature, run yjit with the `--yjit-trace-exits` option,
which will record the backtrace for every exit that occurs. This functionality
requires the stats feature to be turned on. Calling `--yjit-trace-exits`
will automatically set the `--yjit-stats` option.

Users must call `RubyVM::YJIT.dump_exit_locations(filename)` which will
Marshal dump the contents of `RubyVM::YJIT.exit_locations` into a file
based on the passed filename.

*Example usage:*

Given the following script, we write to a file called
`concat_array.dump` the results of `RubyVM::YJIT.exit_locations`.

```ruby
def concat_array
  ["t", "r", *x = "u", "e"].join
end

1000.times do
  concat_array
end

RubyVM::YJIT.dump_exit_locations("concat_array.dump")
```

When we run the file with this branch and the appropriate flags the
stacktrace will be recorded. Note Stackprof needs to be installed or you
need to point to the library directly.

```
./ruby --yjit --yjit-call-threshold=1 --yjit-trace-exits -I/Users/eileencodes/open_source/stackprof/lib test.rb
```

We can then read the dump file with Stackprof:

```
./ruby -I/Users/eileencodes/open_source/stackprof/lib/ /Users/eileencodes/open_source/stackprof/bin/stackprof --text concat_array.dump
```

Results will look similar to the following:

```
==================================
  Mode: ()
  Samples: 1817 (0.00% miss rate)
  GC: 0 (0.00%)
==================================
     TOTAL    (pct)     SAMPLES    (pct)     FRAME
      1001  (55.1%)        1001  (55.1%)     concatarray
       335  (18.4%)         335  (18.4%)     invokeblock
       178   (9.8%)         178   (9.8%)     send
       140   (7.7%)         140   (7.7%)     opt_getinlinecache
       ...etc...
```

Simply inspecting the `concatarray` method will give `SOURCE
UNAVAILABLE` because the source is insns.def.

```
./ruby -I/Users/eileencodes/open_source/stackprof/lib/ /Users/eileencodes/open_source/stackprof/bin/stackprof --text concat_array.dump --method concatarray
```

Result:

```
concatarray (nonexistent.def:1)
  samples:  1001 self (55.1%)  /   1001 total (55.1%)
  callers:
    1000  (   99.9%)  Object#concat_array
       1  (    0.1%)  Gem.suffixes
  callees (0 total):
  code:
        SOURCE UNAVAILABLE
```

However if we go deeper to the callee we can see the exact
source of the `concatarray` exit.

```
./ruby -I/Users/eileencodes/open_source/stackprof/lib/ /Users/eileencodes/open_source/stackprof/bin/stackprof --text concat_array.dump --method Object#concat_array
```

```
Object#concat_array (/Users/eileencodes/open_source/rust_ruby/test.rb:1)
  samples:     0 self (0.0%)  /   1000 total (55.0%)
  callers:
    1000  (  100.0%)  block in <main>
  callees (1000 total):
    1000  (  100.0%)  concatarray
  code:
                                  |     1  | def concat_array
 1000   (55.0%)                   |     2  |   ["t", "r", *x = "u", "e"].join
                                  |     3  | end
```

The `--walk` option is recommended for this feature as it make it
easier to traverse the tree of exits.

*Goals of this feature:*

This feature is meant to give more information when working on YJIT.
The idea is that if we know what code is exiting we can decide what
areas to prioritize when fixing exits. In some cases this means adding
prioritizing avoiding certain exits in yjit. In more complex cases it
might mean changing the Ruby code to be more performant when run with
yjit. Ultimately the more information we have about what code is exiting
AND why, the better we can make yjit.

*Known limitations:*

* Due to tracing exits, running this on large codebases like Rails
can be quite slow.
* On complex methods it can still be difficult to pinpoint the exact cause of
an exit.
* Stackprof is a requirement to to view the backtrace information from
the dump file.

Co-authored-by: Aaron Patterson <tenderlove@ruby-lang.org>

Co-authored-by: Aaron Patterson <tenderlove@ruby-lang.org>
2022-06-09 12:59:39 -04:00
..
enc Avoid defining the same test class in multiple files 2022-04-22 15:00:16 -07:00
allpairs.rb
beginmainend.rb
bug-11928.rb
bug-13526.rb
lbtest.rb
marshaltestlib.rb
sentence.rb
test_alias.rb Fix visibility of alias of zsuper methods 2022-03-10 08:35:26 -08:00
test_argf.rb
test_arithmetic_sequence.rb
test_arity.rb Make a dedecated assertion to clarify failed assertions 2022-03-19 14:33:04 +09:00
test_array.rb Add missing write barriers to Array#replace 2022-04-28 11:31:22 -04:00
test_assignment.rb
test_ast.rb
test_autoload.rb Fix Module#const_source_location for autoload constants with direct requires 2022-06-06 11:12:55 -07:00
test_backtrace.rb
test_basicinstructions.rb
test_beginendblock.rb
test_bignum.rb Fix the condition when a new buffer is needed without GMP 2022-06-02 10:29:53 +09:00
test_call.rb
test_case.rb
test_class.rb
test_clone.rb
test_comparable.rb
test_complex.rb
test_complex2.rb
test_complexrational.rb
test_condition.rb
test_const.rb
test_continuation.rb
test_default_gems.rb
test_defined.rb
test_dir.rb
test_dir_m17n.rb
test_econv.rb
test_encoding.rb
test_enum.rb Raise ArgumentError when calling Enumberable#inject without block or arguments 2022-03-23 07:55:49 -07:00
test_enumerator.rb
test_env.rb
test_eval.rb
test_exception.rb Revert "error.c: Let Exception#inspect inspect its message" 2022-06-07 11:52:44 +09:00
test_fiber.rb
test_file.rb
test_file_exhaustive.rb Skip failing test with freebsd 2022-05-30 18:40:31 +09:00
test_fixnum.rb
test_flip.rb
test_float.rb Fix dtoa buffer overrun 2022-04-12 21:30:49 +09:00
test_fnmatch.rb
test_frozen_error.rb
test_gc.rb Add key force_major_gc_count to GC.stat_heap 2022-06-08 10:03:00 -04:00
test_gc_compact.rb Move `GC.verify_compaction_references` [Bug #18779] 2022-06-02 15:32:00 +09:00
test_hash.rb
test_ifunless.rb
test_inlinecache.rb Avoid defining the same test class in multiple files 2022-04-22 15:00:16 -07:00
test_insns_leaf.rb
test_integer.rb
test_integer_comb.rb
test_io.rb Stop ignoring 4th positional argument to IO.#{foreach,readlines} 2022-06-09 08:22:06 -07:00
test_io_buffer.rb Fix the order of assert_eqaul and remove unused variables 2022-05-10 10:15:27 +09:00
test_io_m17n.rb
test_iseq.rb
test_iterator.rb
test_key_error.rb
test_keyword.rb test/ruby/test_keyword.rb: Prevent warning: assigned but unused variable 2022-04-11 10:05:16 +09:00
test_lambda.rb
test_lazy_enumerator.rb
test_literal.rb
test_m17n.rb
test_m17n_comb.rb
test_marshal.rb
test_math.rb
test_memory_view.rb
test_metaclass.rb
test_method.rb Make define_singleton_method always define a public method 2022-03-29 12:10:13 -07:00
test_method_cache.rb
test_mixed_unicode_escapes.rb
test_mjit.rb Fix MJIT's ISEQ_BODY macro usage at 5f10bd634f 2022-06-08 22:39:18 -07:00
test_mjit_debug.rb Rename test_jit to test_mjit 2022-05-20 21:32:55 -07:00
test_module.rb Add Module#undefined_instance_methods 2022-06-06 09:57:32 -07:00
test_name_error.rb
test_nomethod_error.rb
test_not.rb
test_numeric.rb
test_object.rb
test_objectspace.rb
test_optimization.rb
test_pack.rb
test_parse.rb Private local variables should shadow outer variables [Bug #18629] 2022-04-21 16:04:36 +09:00
test_path.rb
test_pattern_matching.rb
test_pipe.rb
test_primitive.rb
test_proc.rb Raise RuntimeError if Kernel#binding is called from a non-Ruby frame 2022-04-06 19:14:03 -07:00
test_process.rb
test_rand.rb
test_random_formatter.rb
test_range.rb Fix Range#cover? returning true for beginless ranges of different types 2022-06-06 09:59:22 -07:00
test_rational.rb
test_rational2.rb
test_readpartial.rb
test_refinement.rb
test_regexp.rb Ignore invalid escapes in regexp comments 2022-06-06 13:50:03 -07:00
test_require.rb Skip failing test with freebsd 2022-05-31 11:40:38 +09:00
test_require_lib.rb
test_rubyoptions.rb Rename test_jit to test_mjit 2022-05-20 21:32:55 -07:00
test_rubyvm.rb Finer-grained constant cache invalidation (take 2) 2022-04-01 14:48:22 -04:00
test_rubyvm_mjit.rb Make the test class naming consistent 2022-05-20 21:34:47 -07:00
test_settracefunc.rb Fix use-after-free with interacting TracePoints 2022-05-30 13:54:22 -04:00
test_signal.rb
test_sleep.rb
test_sprintf.rb
test_sprintf_comb.rb
test_stack.rb
test_string.rb Add String#bytesplice 2022-03-18 11:51:03 +09:00
test_stringchar.rb
test_struct.rb A positional Hash is not keyword arguments [Bug #18632] 2022-03-17 20:53:41 +09:00
test_super.rb
test_symbol.rb
test_syntax.rb Fix using anonymous block in method accepting explicit keywords 2022-04-05 07:35:25 -07:00
test_system.rb
test_thread.rb Skip failing test with freebsd 2022-05-30 10:53:45 +09:00
test_thread_cv.rb
test_thread_queue.rb Also skip failing test with freebsd 12 2022-05-30 14:28:32 +09:00
test_threadgroup.rb
test_time.rb
test_time_tz.rb
test_trace.rb
test_transcode.rb
test_undef.rb
test_unicode_escape.rb
test_variable.rb
test_vm_dump.rb
test_weakmap.rb
test_whileuntil.rb
test_yield.rb
test_yjit.rb Rename test_jit to test_mjit 2022-05-20 21:32:55 -07:00
test_yjit_exit_locations.rb Add ability to trace exit locations in yjit (#5970) 2022-06-09 12:59:39 -04:00
ut_eof.rb