124 Commits
Author SHA1 Message Date
Hiroshi SHIBATAandClaude Opus 5.5 a3799e9203 win32: Add RUBY_DEBUG=wer to leave crashes to Windows Error Reporting
SEM_NOGPFAULTERRORBOX keeps a crashing test from waiting on the WER dialog, but it also keeps WER from writing a dump when the crash bypasses Ruby's handler, as in a thread other than the main one. Such a crash leaves no [BUG] report either, so a dump is the only clue.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 20:17:41 +09:00
Koichi SasadaandClaude Fable 5 8d46d62fb0 thread: keep context switches off the scheduler lock
thread_sched_setup_running_threads runs on every context switch (park,
resume, and both ends of every blocking region) and took the global
ractor.sched.lock to maintain the running_threads and timeslice lists
and running_cnt.  With many ractors switching concurrently this lock is
the scaling ceiling.

Replace all three with ntlist, a per-location registration of who runs
where, and no shared counter:

- a thread on a shared nt registers in that nt's running_th, under the
  nt's own lock -- uncontended, no cross-core traffic
- a dedicated nt goes on ntlist.running_dnts while its thread runs (the
  switch rate of dedicated threads is what it always was; only the lock
  shrank), so the fast path writes no shared cache line at all

The consumers walk the registrations: the barrier's interrupt walk also
counts them into a snapshot (barrier_running_cnt) and stamps each nt
with the barrier serial; a stamped nt deregistering during that barrier
decrements the snapshot under sched.lock, so the completion check stays
O(1) however many runners there are.

The timeslice list becomes timeslice.scheds, the scheds whose readyq
holds waiters.  The enq that fills an empty readyq (and the add path
taking a turn with waiters already queued) links the sched; the timer
thread interrupts each listed sched's running thread and prunes entries
whose readyq drained.  A steadily contended sched stays linked, so the
switch path never touches the list, and idle scheds are not walked at
all.  The timer takes each sched's lock by trylock, because the
switchers nest sched.lock -> timeslice.lock; pruning is lazy, so
ractor_free delists the dying ractor's sched.

Barrier pairing needs no fences: the barrier sets barrier_is_waiting
and then walks the registrations under their locks; a switch moves its
registration under the same lock and then reads the flag.  A walk that
missed a registration ran before that registration's critical section,
so its flag store is visible to that switcher.  Nothing may start
running while the flag stands -- an interrupt flag alone would let a
resuming thread run, and allocate, inside the stop-the-world section --
so the add path takes its registration back and waits for
rb_ractor_sched_barrier_end's broadcast.  While a scan holds a
running_th lock the registered thread cannot finish parking, so it
cannot die under the scan.  An snt turned dedicated under
rb_thread_lock_native_thread has no creation-time running_thread, so
the running_dnts registration writes nt->running_thread itself.

R ractor pairs ping-ponging via ports (msgs/sec) and R ractors doing
pipe write/read round-trips (rt/sec), 16-HT machine (Ryzen 9 5900HX),
mean of 2 alternating same-tree runs:

                     before    after
    port pairs  8      503k     815k   +62%
    port pairs 32      453k     799k   +76%
    pipe self   4R     837k     940k   +12%
    pipe self  16R     820k     898k   +10%

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-24 02:52:02 +09:00
Jean Boussier 86951165b8 Get rid of ROBJECT_HEAP flag
This flag is now entirely redundant with the shape_id layout bits.
2026-07-15 15:43:28 +02:00
Nobuyoshi Nakada 2d159fbbb6 Use EXIT_SUCCESS/EXIT_FAILURE instead of 0/1 2026-02-12 19:17:44 +09:00
Nobuyoshi Nakada a797561e25 Update Typed Data dump on debuggers [ci skip] 2026-01-16 16:47:34 +09:00
Koichi Sasada 3bb97e7707 _RUBY_DEBUG_LOG usable anywhere
even if `USE_RUBY_DEBUG_LOG=0`.
It becomes `fprintf(stderr, ...)`.
2025-12-10 10:23:37 +09:00
Nobuyoshi Nakada cdb9893c55 Win32: Drop support for older than MSVC 8.0/_MSC_VER 1400
Visual C++ 2005 (8.0):
- _MSC_VER: 1400
- MSVCRT_VERSION: 80
2025-11-19 11:03:42 +09:00
Peter Zhu f14e52c8c4 Fix setting GC stress at boot when objspace not available 2024-03-27 09:39:23 -04:00
Peter Zhu 19752cf4aa Use macro SET_WHEN_UINT 2024-03-26 10:02:17 -04:00
Peter Zhu 9cf754b648 Fix --debug=gc_stress flag
ruby_env_debug_option gets called after Init_gc_stress, so the
--debug=gc_stress flag never works.
2024-03-25 13:07:39 -04:00
Yusuke Endoh 25d74b9527 Do not include a backtick in error messages and backtraces
[Feature #16495]
2024-02-15 18:42:31 +09:00
S.H a6ba45e9b0 Remove unnecessary semicolons (#9469) 2024-01-10 16:21:55 -08:00
yui-knk 0c990994c7 Add a shorthand for ruby_debug_print_node 2024-01-06 09:32:45 +09:00
yui-knk da23f8fe1a Print node id and location by ruby_debug_print_node 2024-01-06 09:32:45 +09:00
Koichi Sasada be1bbd5b7d M:N thread scheduler for Ractors
This patch introduce M:N thread scheduler for Ractor system.

In general, M:N thread scheduler employs N native threads (OS threads)
to manage M user-level threads (Ruby threads in this case).
On the Ruby interpreter, 1 native thread is provided for 1 Ractor
and all Ruby threads are managed by the native thread.

From Ruby 1.9, the interpreter uses 1:1 thread scheduler which means
1 Ruby thread has 1 native thread. M:N scheduler change this strategy.

Because of compatibility issue (and stableness issue of the implementation)
main Ractor doesn't use M:N scheduler on default. On the other words,
threads on the main Ractor will be managed with 1:1 thread scheduler.

There are additional settings by environment variables:

`RUBY_MN_THREADS=1` enables M:N thread scheduler on the main ractor.
Note that non-main ractors use the M:N scheduler without this
configuration. With this configuration, single ractor applications
run threads on M:1 thread scheduler (green threads, user-level threads).

`RUBY_MAX_CPU=n` specifies maximum number of native threads for
M:N scheduler (default: 8).

This patch will be reverted soon if non-easy issues are found.

[Bug #19842]
2023-10-12 14:47:01 +09:00
Koichi Sasada 802ca3a102 RUBY_DEBUG_LOG supports %p for pid 2023-10-12 03:14:39 +09:00
Samuel Williams a218ed5692 Hide the usage of rb_io_t where possible. (#7880)
This retries the compatible parts of the previously reverted PR so we can continue to update related code without breaking backwards compatibility.
2023-06-01 14:23:30 +09:00
NARUSE, Yui 85dcc4866d Revert "Hide most of the implementation of struct rb_io. (#6511)"
This reverts commit 18e55fc1e1.

fix [Bug #19704]
https://bugs.ruby-lang.org/issues/19704
This breaks compatibility for extension libraries. Such changes
need a discussion.
2023-06-01 08:43:22 +09:00
Samuel WilliamsandKJ Tsanaktsidis 18e55fc1e1 Hide most of the implementation of struct rb_io. (#6511)
* Add rb_io_path and rb_io_open_descriptor.

* Use rb_io_open_descriptor to create PTY objects

* Rename FMODE_PREP -> FMODE_EXTERNAL and expose it

FMODE_PREP I believe refers to the concept of a "pre-prepared" file, but
FMODE_EXTERNAL is clearer about what the file descriptor represents and
aligns with language in the IO::Buffer module.

* Ensure that rb_io_open_descriptor closes the FD if it fails

If FMODE_EXTERNAL is not set, then it's guaranteed that Ruby will be
responsible for closing your file, eventually, if you pass it to
rb_io_open_descriptor, even if it raises an exception.

* Rename IS_EXTERNAL_FD -> RUBY_IO_EXTERNAL_P

* Expose `rb_io_closed_p`.

* Add `rb_io_mode` to get IO mode.

---------

Co-authored-by: KJ Tsanaktsidis <ktsanaktsidis@zendesk.com>
2023-05-30 10:02:40 +09:00
yui-knk 5f65e8c5d5 Rename rb_node_name to the original name
98637d421d changes the name of
the function. However this function is exported as global,
then change the name to origin one for keeping compatibility.
2023-05-24 20:54:48 +09:00
yui-knk 98637d421d Move ruby_node_name to node.c and rename prefix of the function 2023-05-23 18:05:35 +09:00
Nobuyoshi Nakada 8d242a33af rb_bug prints a newline after the message 2023-05-20 21:43:30 +09:00
Koichi Sasada 06b68db7cf show a separator even if ec is NULL.
`RUBY_DEBUG_LOG()` doesn't show anything if `GET_EC()` returns NULL,
but print a separator "\t" to make consistent TSV.
2023-04-26 17:57:32 +09:00
Koichi Sasada 457824e2d3 RUBY_DEBUG_LOG_PID for RUBY_DEBUG_LOG()
`RUBY_DEBUG_LOG=stderr RUBY_DEBUG_LOG_PID=1 ruby ...` will prints
debug logs with PID.
2023-04-26 17:57:32 +09:00
Peter Zhu 1da2e7fca3 [Feature #19579] Remove !USE_RVARGC code (#7655)
Remove !USE_RVARGC code

[Feature #19579]

The Variable Width Allocation feature was turned on by default in Ruby
3.2. Since then, we haven't received bug reports or backports to the
non-Variable Width Allocation code paths, so we assume that nobody is
using it. We also don't plan on maintaining the non-Variable Width
Allocation code, so we are going to remove it.
2023-04-04 17:30:06 -04:00
Koichi Sasada 2093e4c2db nt->serial for RUBY_DEBUG_LOG
Show native thread's serial on `RUBY_DEBUG_LOG`.
`nt->serial` is also stored into `ruby_nt_serial` if the compiler
supports `RB_THREAD_LOCAL_SPECIFIER`.
2023-03-31 11:28:18 +09:00
John Bampton 2f7270c681 Fix spelling (#7389) 2023-02-27 09:56:06 -08:00
Matt Valentine-House 72aba64fff Merge gc.h and internal/gc.h
[Feature #19425]
2023-02-09 10:32:29 -05:00
Nobuyoshi Nakada d6acaa4d42 Undefine ruby_debug_log macro before the function definition
Fix up 27173e3735.
2022-08-30 11:23:16 +09:00
Nobuyoshi Nakada f42230ff22 Adjust styles [ci skip] 2022-07-27 18:42:27 +09:00
Takashi Kokubun 5b21e94beb Expand tabs [ci skip]
[Misc #18891]
2022-07-21 09:42:04 -07:00
Peter Zhu 7424ea184f Implement Objects on VWA
This commit implements Objects on Variable Width Allocation. This allows
Objects with more ivars to be embedded (i.e. contents directly follow the
object header) which improves performance through better cache locality.
2022-07-15 09:21:07 -04:00
Koichi Sasada 1e528e8cbe small fix on setup_debug_log()
* print `ruby_debug_log_mode` at first.
* show filters when `ruby_debug_log_mode` is not "disabled".
2022-06-10 23:56:49 +09:00
Koichi Sasada 5a4f997b2e func: and file: prefix for RUBY_DEBUG_LOG_FILTER
`RUBY_DEBUG_LOG_FILTER` specified only function names but this
patch also check file names for each log events.
If you specify `file:` or `func:` prefix, it's only filter
file names or func names (otherwize check both).

  foo
  # show log when file or func names are mached with foo

  func:foo
  # show log when func name matches foo

  file:foo
  # show log when file name matches foo

  -file:foo,func:bar
  # show log when file name does not contains foo
  #           and func name matches bar
2022-06-09 01:51:19 +09:00
Koichi Sasada f3235ac095 add rb_th_serial()
`rb_th_serial(th)` returns th's serial for debug print purpose.
2022-05-24 10:06:51 +09:00
Koichi Sasada a97fbc108b extend RUBY_DEBUG_LOG_FILTER to reject words
support reject words with `-word` like
RUBY_DEBUG_LOG_FILTER=-foo,-bar,baz,boo`.
2022-05-21 08:35:04 +09:00
Koichi Sasada eab99b1d4b rb_thread_t::serial for debug
`rb_thread_t::serial` is auto-incremented serial number for
threads and it can overflow, it means the serial is not a ID
for each thread, it is only for debug print.

`RUBY_DEBUG_LOG` shows this information.

Also skip EC related information if EC is NULL. This patch
enable to use `RUBY_DEBUG_LOG` without setup EC.
2022-05-20 17:37:46 +09:00
Koichi Sasada c9af8a32a0 USE_RUBY_DEBUG_LOG doesn't check RUBY_DEVEL
`USE_RUBY_DEBUG_LOG` was only defined when `RUBY_DEVEL` is defined.
This patch removes this dependency (`USE_RUBY_DEBUG_LOG` is defined
independently from `RUBY_DEVEL`).

Do not commit a patch which enables `USE_RUBY_DEBUG_LOG`.
2021-12-29 00:22:02 +09:00
Peter Zhu a5b6598192 [Feature #18239] Implement VWA for strings
This commit adds support for embedded strings with variable capacity and
uses Variable Width Allocation to allocate strings.
2021-10-25 13:26:23 -04:00
Nobuyoshi Nakada 545e01645f lldb: Show encoding of String [ci skip] 2021-09-28 20:03:54 +09:00
Nobuyoshi Nakada c38c2d8ee2 Moved exported symbols in internal/util.h to ruby/util.h
[Feature #18051]
2021-08-24 10:37:41 +09:00
Nobuyoshi Nakada a12e950816 Revert "Enclose crtitical sections in thread_exclusive block"
19cc24b34b and fixups.
2021-02-24 03:13:07 +09:00
Nobuyoshi Nakada 7563d542d6 Use the system getenv in setup_debug_log
As ruby_set_debug_option() is called before ruby_sysinit(),
CRITICAL_SECTIONs are not initialized yet.
2021-02-23 23:11:44 +09:00
Koichi Sasada 45f3d48106 USE_RUBY_DEBUG_LOG is not defined !devel build 2020-12-25 09:52:18 +09:00
Koichi Sasada 23f9447429 show ractor info on non-single ractor mode
Without this patch, Ruby doesn't show ractor's information when
there is only 1 ractor. However it is hard to read the log when
some ractors are created and terminated. This patch makes to keep
showing ractor's information on multi-ractor mode.
2020-12-07 08:28:36 +09:00
Koichi Sasada 5e3259ea74 fix public interface
To make some kind of Ractor related extensions, some functions
should be exposed.

* include/ruby/thread_native.h
  * rb_native_mutex_*
  * rb_native_cond_*
* include/ruby/ractor.h
  * RB_OBJ_SHAREABLE_P(obj)
  * rb_ractor_shareable_p(obj)
  * rb_ractor_std*()
  * rb_cRactor

and rm ractor_pub.h
and rename srcdir/ractor.h to srcdir/ractor_core.h
    (to avoid conflict with include/ruby/ractor.h)
2020-11-18 03:52:41 +09:00
Koichi Sasada 79df14c04b Introduce Ractor mechanism for parallel execution
This commit introduces Ractor mechanism to run Ruby program in
parallel. See doc/ractor.md for more details about Ractor.
See ticket [Feature #17100] to see the implementation details
and discussions.

[Feature #17100]

This commit does not complete the implementation. You can find
many bugs on using Ractor. Also the specification will be changed
so that this feature is experimental. You will see a warning when
you make the first Ractor with `Ractor.new`.

I hope this feature can help programmers from thread-safety issues.
2020-09-03 21:11:06 +09:00
卜部昌平 69b5241c36 ruby_debug_log: suppress warning
Old gcc (< 5 maybe?) warns that this variable is not initialized:

    debug.c: In function 'ruby_debug_log':
    debug.c:441:13: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
             if (r && len < MAX_DEBUG_LOG_MESSAGE_LEN) {
                 ^

I don't know if that is true, but adding "= 0" here must harm no one.
2020-08-13 10:14:20 +09:00
Koichi Sasada a1246270c5 remove accidentally introduced debug code 2020-08-02 04:51:26 +09:00
Koichi Sasada b4f58ea300 support multiple filters by RUBY_DEBUG_LOG_FILTER
Now you can specify multiple filters for RUBY_DEBUG_LOG output
by RUBY_DEBUG_LOG_FILTER=a,b,c (in this case, logs that the
function name contains a, b or c).
2020-08-02 04:24:47 +09:00