Commit graph

38 commits

Author SHA1 Message Date
Aaron Patterson
50c2c4bdde Make rb_vm_insns_count a thread local variable
`rb_vm_insns_count` is a global variable used for reporting YJIT
statistics. It is a counter that tallies the number of interpreter
instructions that have been executed, this way we can approximate how
much time we're spending in YJIT compared to the interpreter.

Unfortunately keeping this statistic means that every instruction
executed in the interpreter loop must increment the counter. Normally
this isn't a problem, but in multi-threaded situations (when Ractors are
used), incrementing this counter can become quite costly due to page
caching issues.

Additionally, since there is no locking when incrementing this global,
the count can't really make sense in a multi-threaded environment.

This commit changes `rb_vm_insns_count` to a thread local. That way each
Ractor has it's own copy of the counter and incrementing the counter
becomes quite cheap. Of course this means that in multi-threaded
situations, the value doesn't really make sense (but it didn't make
sense before because of the lack of locking).

The counter is used for YJIT statistics, and since YJIT is basically
disabled when Ractors are in use, I don't think we care about
inaccuracies (for the time being). We can revisit this counter when we
give YJIT multi-threading support, but for the time being this commit
restores multi-threaded performance.

To test this, I used the benchmark in [Bug #20489].

Here is the performance on Ruby 3.2:

```
$ time RUBY_MAX_CPU=12 ./miniruby -v ../test.rb 8 8
ruby 3.2.0 (2022-12-25 revision a528908271) [x86_64-linux]
[0...1, 1...2, 2...3, 3...4, 4...5, 5...6, 6...7, 7...8]
../test.rb:43: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues.

________________________________________________________
Executed in    2.53 secs    fish           external
   usr time   19.86 secs  370.00 micros   19.86 secs
   sys time    0.02 secs  320.00 micros    0.02 secs
```

We can see the regression in performance on the master branch:

```
$ time RUBY_MAX_CPU=12 ./miniruby -v ../test.rb 8 8
ruby 3.5.0dev (2025-01-10T16:22:26Z master 4a2702dafb) +PRISM [x86_64-linux]
[0...1, 1...2, 2...3, 3...4, 4...5, 5...6, 6...7, 7...8]
../test.rb:43: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues.

________________________________________________________
Executed in   24.87 secs    fish           external
   usr time  195.55 secs    0.00 micros  195.55 secs
   sys time    0.00 secs  716.00 micros    0.00 secs
```

Here are the stats after this commit:

```
$ time RUBY_MAX_CPU=12 ./miniruby -v ../test.rb 8 8
ruby 3.5.0dev (2025-01-10T20:37:06Z tl 3ef0432779) +PRISM [x86_64-linux]
[0...1, 1...2, 2...3, 3...4, 4...5, 5...6, 6...7, 7...8]
../test.rb:43: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues.

________________________________________________________
Executed in    2.46 secs    fish           external
   usr time   19.34 secs  381.00 micros   19.34 secs
   sys time    0.01 secs  321.00 micros    0.01 secs
```

[Bug #20489]
2025-01-10 13:39:21 -08:00
Peter Zhu
a58675386c Prefix asan_poison_object with rb 2024-12-19 09:14:34 -05:00
Kunshan Wang
8ae7c22972 Annotate anonymous mmap
Use PR_SET_VMA_ANON_NAME to set human-readable names for anonymous
virtual memory areas mapped by `mmap()` when compiled and run on Linux
5.17 or higher.  This makes it convenient for developers to debug mmap.
2024-11-21 13:48:05 -05:00
Nobuyoshi Nakada
9a90cd2284 Cast via uintptr_t function pointer between object pointer
- ISO C forbids conversion of function pointer to object pointer type
- ISO C forbids conversion of object pointer to function pointer type
2024-10-08 23:29:49 +09:00
Nobuyoshi Nakada
49fcd33e13 Introduce a specialize instruction for Array#pack
Instructions for this code:

```ruby
  # frozen_string_literal: true

[a].pack("C")
```

Before this commit:

```
== disasm: #<ISeq:<main>@test.rb:1 (1,0)-(3,13)>
0000 putself                                                          (   3)[Li]
0001 opt_send_without_block                 <calldata!mid:a, argc:0, FCALL|VCALL|ARGS_SIMPLE>
0003 newarray                               1
0005 putobject                              "C"
0007 opt_send_without_block                 <calldata!mid:pack, argc:1, ARGS_SIMPLE>
0009 leave
```

After this commit:

```
== disasm: #<ISeq:<main>@test.rb:1 (1,0)-(3,13)>
0000 putself                                                          (   3)[Li]
0001 opt_send_without_block                 <calldata!mid:a, argc:0, FCALL|VCALL|ARGS_SIMPLE>
0003 putobject                              "C"
0005 opt_newarray_send                      2, :pack
0008 leave
```

Co-authored-by: Maxime Chevalier-Boisvert <maxime.chevalierboisvert@shopify.com>
Co-authored-by: Aaron Patterson <tenderlove@ruby-lang.org>
2024-05-23 12:11:50 -07:00
Takashi Kokubun
8671cd59f7 RJIT: Correct the type of rb_vm_insns_count 2023-12-19 00:02:38 -08:00
Takashi Kokubun
edd6581eec RJIT: Declare rb_vm_insns_count 2023-12-19 00:00:18 -08:00
Nobuyoshi Nakada
8d242a33af
rb_bug prints a newline after the message 2023-05-20 21:43:30 +09:00
Aaron Patterson
bdffcd6df3 Update RJIT to support newarray_send
This also adds max / hash support
2023-04-18 17:16:22 -07:00
Nobuyoshi Nakada
80fa9b0404 Fix declaration of rb_rjit_entry_stub_hit in rjit_c.c 2023-04-13 14:09:22 +09:00
Aaron Patterson
a9bfb64153 Expose rb_sym_to_proc via RJIT
This is needed for getblockparamproxy
2023-04-07 09:49:15 -07:00
Takashi Kokubun
6002b12611 RJIT: Support entry with different PCs 2023-04-02 15:27:40 -07:00
Takashi Kokubun
66f8efc342 RJIT: Simplify cfunc implementation 2023-04-02 13:58:39 -07:00
Takashi Kokubun
1b475fcd10 Remove an unneeded function copy 2023-04-01 23:09:05 -07:00
Takashi Kokubun
6c55c3eb7f RJIT: Fix a leaked-globals failure 2023-04-01 23:07:22 -07:00
Takashi Kokubun
a077b7e36b RJIT: Support rest args 2023-04-01 23:00:36 -07:00
Takashi Kokubun
dc270fc632 RJIT: Implement attr_writer 2023-03-26 18:02:25 -07:00
Takashi Kokubun
59b86da82c RJIT: Implement ifunc invokeblock 2023-03-19 23:32:07 -07:00
Takashi Kokubun
83ad1cac81 RJIT: Optimize Kernel#respond_to? 2023-03-19 14:04:58 -07:00
Takashi Kokubun
2121282753 RJIT: Optimize String#<< 2023-03-19 13:25:41 -07:00
Takashi Kokubun
32e0c97dfa RJIT: Optimize String#bytesize 2023-03-18 23:35:42 -07:00
Takashi Kokubun
71bcab4519 RJIT: Implement setclassvariable 2023-03-18 21:49:46 -07:00
Takashi Kokubun
9c2792c3d3 RJIT: Implement toregexp 2023-03-18 21:37:49 -07:00
Takashi Kokubun
45a17013aa RJIT: Implement throw insn 2023-03-17 23:27:16 -07:00
Takashi Kokubun
644c998525 RJIT: Support --rjit-stats on release build as well 2023-03-17 22:31:41 -07:00
Takashi Kokubun
d8344559b2 RJIT: Simplify how Capstone is used in tests 2023-03-13 20:42:19 -07:00
Takashi Kokubun
07d3af22d0 RJIT: Fix -Wshorten-64-to-32 2023-03-12 20:41:07 -07:00
Takashi Kokubun
9cd5441d28 RJIT: Implement --rjit-trace-exits 2023-03-12 15:15:08 -07:00
Takashi Kokubun
58f7e8b7f8 RJIT: Automate function pointer imports 2023-03-11 21:26:40 -08:00
Takashi Kokubun
46a3634bcf RJIT: Use SIZET macros instead of original PTR ones 2023-03-11 20:47:08 -08:00
Takashi Kokubun
c364e0745d RJIT: Introduce --rjit-exec-mem-size 2023-03-10 13:04:45 -08:00
Takashi Kokubun
1a0d3ec4b9 RJIT: Make functions in rjit_c.c static
They don't need to be global.
2023-03-08 23:38:02 -08:00
Takashi Kokubun
f5909ac6d9 RJIT: Stop allowing leaked globals rjit_* 2023-03-08 23:24:38 -08:00
Takashi Kokubun
7d7b67a472 RJIT: Clean up the declaration mess 2023-03-08 23:07:30 -08:00
Takashi Kokubun
6d91df08b5
Allow enabling YJIT and RJIT independently (#7474)
We used to require MJIT is supported when YJIT is supported. However,
now that RJIT dropped some platforms that YJIT supports, it no longer
makes sense. We should be able to enable only YJIT, and vice versa.
2023-03-07 22:43:37 -08:00
Takashi Kokubun
23ec248e48 s/mjit/rjit/ 2023-03-06 23:44:01 -08:00
Takashi Kokubun
2e875549a9 s/MJIT/RJIT/ 2023-03-06 23:44:01 -08:00
Takashi Kokubun
eaccdc1941 Rename MJIT filenames to RJIT 2023-03-06 23:44:01 -08:00
Renamed from mjit_c.c (Browse further)