summaryrefslogtreecommitdiff
path: root/vm.c
AgeCommit message (Collapse)Author
2021-03-11merge revision(s) ↵NARUSE, Yui
abdc634f64a440afcdc7f23c9757d27aab4db8a9,083c5f08ec4e95c9b75810d46f933928327a5ab3,1ecda213668644d656eb0d60654737482447dd92,813fe4c256f89babebb8ab53821ae5eb6bb138c6: [Backport #17497] remove unused decl --- internal/vm.h | 6 ------ vm_args.c | 2 -- 2 files changed, 8 deletions(-) Check stack overflow in recursive glob_helper [Bug #17162] --- dir.c | 2 ++ internal/vm.h | 1 + vm_eval.c | 10 ++++++++++ 3 files changed, 13 insertions(+) global call-cache cache table for rb_funcall* rb_funcall* (rb_funcall(), rb_funcallv(), ...) functions invokes Ruby's method with given receiver. Ruby 2.7 introduced inline method cache with static memory area. However, Ruby 3.0 reimplemented the method cache data structures and the inline cache was removed. Without inline cache, rb_funcall* searched methods everytime. Most of cases per-Class Method Cache (pCMC) will be helped but pCMC requires VM-wide locking and it hurts performance on multi-Ractor execution, especially all Ractors calls methods with rb_funcall*. This patch introduced Global Call-Cache Cache Table (gccct) for rb_funcall*. Call-Cache was introduced from Ruby 3.0 to manage method cache entry atomically and gccct enables method-caching without VM-wide locking. This table solves the performance issue on multi-ractor execution. [Bug #17497] Ruby-level method invocation does not use gccct because it has inline-method-cache and the table size is limited. Basically rb_funcall* is not used frequently, so 1023 entries can be enough. We will revisit the table size if it is not enough. --- debug_counter.h | 3 + vm.c | 12 +++ vm_callinfo.h | 12 --- vm_core.h | 5 + vm_eval.c | 288 ++++++++++++++++++++++++++++++++++++++++++-------------- vm_insnhelper.c | 11 ++- vm_method.c | 14 ++- 7 files changed, 255 insertions(+), 90 deletions(-) opt_equality_by_mid for rb_equal_opt This patch improves the performance of sequential and parallel execution of rb_equal() (and rb_eql()). [Bug #17497] rb_equal_opt (and rb_eql_opt) does not have own cd and it waste a time to initialize cd. This patch introduces opt_equality_by_mid() to check equality without cd. Furthermore, current master uses "static" cd on rb_equal_opt (and rb_eql_opt) and it hurts CPU caches on multi-thread execution. Now they are gone so there are no bottleneck on parallel execution. --- vm_insnhelper.c | 99 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 63 insertions(+), 36 deletions(-)
2021-03-11merge revision(s) 2a83650b0fd25719fb6c03bfec7bd895734d3ceb: [Backport #15852]NARUSE, Yui
Destroy VM-wise locks before freeing [Bug #15852] --- thread.c | 7 ------- vm.c | 2 ++ 2 files changed, 2 insertions(+), 7 deletions(-)
2021-03-02merge revision(s) 15dbaa0b54f10e43976d594ef987da5f51e0c7c1: [Backport #17622]NARUSE, Yui
[Fixes #17622] Mark and move the previous ep --- vm.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
2020-12-24shareable_constant_value: experimental_copyKoichi Sasada
"experimental_everything" makes the assigned value, it means the assignment change the state of assigned value. "experimental_copy" tries to make a deep copy and make copyied object sharable. Notes: Merged: https://github.com/ruby/ruby/pull/3989
2020-12-23Changed shareable literal semantics [Feature #17397]Nobuyoshi Nakada
When `literal`, check if the literal about to be assigned to a constant is ractor-shareable, otherwise raise `Ractor::Error` at runtime instead of `SyntaxError`. Notes: Merged: https://github.com/ruby/ruby/pull/3950
2020-12-21Prefer stdbool in vm_execTakashi Kokubun
Make the code a bit modern and consistent with some other places.
2020-12-22TracePoint.new(&block) should be ractor-localKoichi Sasada
TracePoint should be ractor-local because the Proc can violate the Ractor-safe. Notes: Merged: https://github.com/ruby/ruby/pull/3943
2020-12-21Introduce Ractor::IsolationErrorKoichi Sasada
Ractor has several restrictions to keep each ractor being isolated and some operation such as `CONST="foo"` in non-main ractor raises an exception. This kind of operation raises an error but there is confusion (some code raises RuntimeError and some code raises NameError). To make clear we introduce Ractor::IsolationError which is raised when the isolation between ractors is violated. Notes: Merged: https://github.com/ruby/ruby/pull/3957
2020-12-20Mark active_unitsTakashi Kokubun
to avoid SEGV on mjit_recompile and compact_all_jit_code. For some reason, ISeqs on stack are sometimes GC-ed (why?) and therefore it may run mjit_recompile on a GC-ed ISeq, which I expected d07183ec85d to fix but apparently it may refer to random things if already GC-ed. Marking active_units would workaround the situation. http://ci.rvm.jp/results/trunk-mjit-wait@phosphorus-docker/3292740 Also, while compact_all_jit_code was executed, we saw some SEGVs where CCs seemed to be already GC-ed, meaning their owner ISeq was not marked properly. Even if units are still in active_units, it's not guaranteed that their ISeqs are in use. So in this case we need to mark active_units for a legitimate reason. http://ci.rvm.jp/results/trunk-mjit-wait@phosphorus-docker/3293277 http://ci.rvm.jp/results/trunk-mjit-wait@phosphorus-docker/3293090
2020-12-20Stop marking unit_queueTakashi Kokubun
The original motivation of this marking was https://github.com/k0kubun/yarv-mjit/issues/20. As wanabe said, there are multiple options to mitigate the issue, and Eric Wong introduced another fix at 143776f6fe by checking unit->iseq inside the lock. Therefore this particular condition has been covered in two ways, and the script given by wanabe no longer crashes without mjit_mark().
2020-12-19fix method cache debug toolKoichi Sasada
2020-12-14Support shareable_constant_value: literalNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/3681
2020-12-14Make the value shareable deeplyNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/3681
2020-12-14Call FrozenCore.make_shareableNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/3681
2020-12-14Introduce negative method cacheKoichi Sasada
pCMC doesn't have negative method cache so this patch implements it. Notes: Merged: https://github.com/ruby/ruby/pull/3892
2020-12-07Removed deprecated Time#succNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/3841
2020-12-07fix decl of ruby_single_main_ractorKoichi Sasada
On windows, MJIT doesn't work without this patch because of the declaration of ruby_single_main_ractor. This patch fix this issue and move the definition of it from ractor.c to vm.c to locate near place of ruby_current_vm_ptr. Notes: Merged: https://github.com/ruby/ruby/pull/3842
2020-12-04fix initialize orderKoichi Sasada
`captured->code.val` should be initialize before because it can be a GC point by another ractor because `RB_OBJ_WRITE` can issue VM locking.
2020-12-03add GC guardKoichi Sasada
new_prev_env is stored in the env_body memory block but this is not a GC root, so new_prev_env could be freed.
2020-12-01rb_ext_ractor_safe() to declare ractor-safe extKoichi Sasada
C extensions can violate the ractor-safety, so only ractor-safe C extensions (C methods) can run on non-main ractors. rb_ext_ractor_safe(true) declares that the successive defined methods are ractor-safe. Otherwiwze, defined methods checked they are invoked in main ractor and raise an error if invoked at non-main ractors. [Feature #17307] Notes: Merged: https://github.com/ruby/ruby/pull/3824
2020-11-30Only check if the current ep is a local or not, then markAaron Patterson
The vm mark function should only check if the current frame is a local or not and then mark values in that frame. Since it's walking up the stack looking at each cfp, then all ep's should be examined. This fixes a bug in the Rails tests where we're seeing segv in railties. Thanks Yasuo Honda for giving me a reliable repro! Notes: Merged: https://github.com/ruby/ruby/pull/3829
2020-11-22Remove obsoleted internal/mjit.h inclusionTakashi Kokubun
:bow:
2020-11-18fix public interfaceKoichi Sasada
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) Notes: Merged: https://github.com/ruby/ruby/pull/3775
2020-11-09rb_vm_add_root_module(): Remove unused parameterAlan Wu
Notes: Merged: https://github.com/ruby/ruby/pull/3741
2020-11-02Add `GC.auto_compact= true/false` and `GC.auto_compact`Aaron Patterson
* `GC.auto_compact=`, `GC.auto_compact` can be used to control when compaction runs. Setting `auto_compact=` to true will cause compaction to occurr duing major collections. At the moment, compaction adds significant overhead to major collections, so please test first! [Feature #17176]
2020-10-30Fix a typo [ci skip]Kazuhiro NISHIYAMA
2020-10-30Ractor.make_shareable(a_proc)Koichi Sasada
Ractor.make_shareable() supports Proc object if (1) a Proc only read outer local variables (no assignments) (2) read outer local variables are shareable. Read local variables are stored in a snapshot, so after making shareable Proc, any assignments are not affeect like that: ```ruby a = 1 pr = Ractor.make_shareable(Proc.new{p a}) pr.call #=> 1 a = 2 pr.call #=> 1 # `a = 2` doesn't affect ``` [Feature #17284] Notes: Merged: https://github.com/ruby/ruby/pull/3722
2020-10-29check isolated Proc more strictlyKoichi Sasada
Isolated Proc prohibit to access outer local variables, but it was violated by binding and so on, so they should be error. Notes: Merged: https://github.com/ruby/ruby/pull/3721
2020-10-20Dump FrozenCore speciallyNobuyoshi Nakada
2020-10-20Some global variables can be accessed from ractorsKoichi Sasada
Some global variables should be used from non-main Ractors. [Bug #17268] ```ruby # ractor-local (derived from created ractor): debug '$DEBUG' => $DEBUG, '$-d' => $-d, # ractor-local (derived from created ractor): verbose '$VERBOSE' => $VERBOSE, '$-w' => $-w, '$-W' => $-W, '$-v' => $-v, # process-local (readonly): other commandline parameters '$-p' => $-p, '$-l' => $-l, '$-a' => $-a, # process-local (readonly): getpid '$$' => $$, # thread local: process result '$?' => $?, # scope local: match '$~' => $~.inspect, '$&' => $&, '$`' => $`, '$\'' => $', '$+' => $+, '$1' => $1, # scope local: last line '$_' => $_, # scope local: last backtrace '$@' => $@, '$!' => $!, # ractor local: stdin, out, err '$stdin' => $stdin.inspect, '$stdout' => $stdout.inspect, '$stderr' => $stderr.inspect, ``` Notes: Merged: https://github.com/ruby/ruby/pull/3670
2020-10-20Use language TLS specifier if it is possible.Koichi Sasada
To access TLS, it is faster to use language TLS specifier instead of using pthread_get/setspecific functions. Original proposal is: Use native thread locals. #3665 Notes: Merged: https://github.com/ruby/ruby/pull/3667
2020-10-14ruby_vm_global_method_state is no longer needed.Koichi Sasada
Now ruby_vm_global_method_state is not used so let's remove it. Notes: Merged: https://github.com/ruby/ruby/pull/3657
2020-10-01Update the thread's self / wrapper addressAaron Patterson
Threads can move, and if they do, their self pointer may go bad. We need to update it.
2020-09-28Fix ASAN support when invalidating CCsAaron Patterson
Again, this code is walking the heap. Empty slots can be poisoned, so we need to unpoison before checking the type Notes: Merged: https://github.com/ruby/ruby/pull/3592
2020-09-25Fibers should update themselves on compactionAaron Patterson
We should let fibers update their own references on compaction. I don't think we need the thread to update the associated fiber because there will be a fiber object on the heap that knows how to update itself. Notes: Merged: https://github.com/ruby/ruby/pull/3588
2020-09-04Initialize loop variables of list_for_each for MS VCNobuyoshi Nakada
2020-09-03Introduce Ractor mechanism for parallel executionKoichi Sasada
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. Notes: Merged: https://github.com/ruby/ruby/pull/3365
2020-06-29vm_exec_handle_exception: do not goto into a branch卜部昌平
I'm not necessarily against every goto in general, but jumping into a branch is definitely a bad idea. Better refactor. Notes: Merged: https://github.com/ruby/ruby/pull/3247
2020-06-09vm_empty_cc: fix MSVC compile error卜部昌平
Seems they do not allow compound literals for static variables. See https://github.com/ruby/ruby/runs/733735274 Notes: Merged: https://github.com/ruby/ruby/pull/3179
2020-06-09vm_empty_cc: refactor use macro卜部昌平
Notes: Merged: https://github.com/ruby/ruby/pull/3179
2020-06-09rb_equal_opt: fully static call data卜部昌平
This changeset reduces the generated binary of rb_equal_opt from 129 bytes to 17 bytes on my machine, according to nm(1). Notes: Merged: https://github.com/ruby/ruby/pull/3179
2020-06-04Moved vm_empty_cc to local in vm.c [Bug #16934]Nobuyoshi Nakada
As it is referenced only from compile.c, except for MJIT, simply use the accessor function.
2020-05-22Fix origin iclass pointer for modulesJeremy Evans
If a module has an origin, and that module is included in another module or class, previously the iclass created for the module had an origin pointer to the module's origin instead of the iclass's origin. Setting the origin pointer correctly requires using a stack, since the origin iclass is not created until after the iclass itself. Use a hidden ruby array to implement that stack. Correctly assigning the origin pointers in the iclass caused a use-after-free in GC. If a module with an origin is included in a class, the iclass shares a method table with the module and the iclass origin shares a method table with module origin. Mark iclass origin with a flag that notes that even though the iclass is an origin, it shares a method table, so the method table should not be garbage collected. The shared method table will be garbage collected when the module origin is garbage collected. I've tested that this does not introduce a memory leak. This change caused a VM assertion failure, which was traced to callable method entries using the incorrect defined_class. Update rb_vm_check_redefinition_opt_method and find_defined_class_by_owner to treat iclass origins different than class origins to avoid this issue. This also includes a fix for Module#included_modules to skip iclasses with origins. Fixes [Bug #16736] Notes: Merged: https://github.com/ruby/ruby/pull/3136
2020-05-21Unpin and update VM referencesAaron Patterson
This commit just unpins and updates VM references
2020-05-18Allow references stored in the VM stack to moveAaron Patterson
We can update these references too, so lets allow them to move.
2020-05-14Thread scheduler for light weight concurrency.Samuel Williams
Notes: Merged: https://github.com/ruby/ruby/pull/3032 Merged-By: ioquatix <samuel@codeotaku.com>
2020-04-08Merge pull request #2991 from shyouhei/ruby.h卜部昌平
Split ruby.h Notes: Merged-By: shyouhei <shyouhei@ruby-lang.org>
2020-03-17Reduce allocations for keyword argument hashesJeremy Evans
Previously, passing a keyword splat to a method always allocated a hash on the caller side, and accepting arbitrary keywords in a method allocated a separate hash on the callee side. Passing explicit keywords to a method that accepted a keyword splat did not allocate a hash on the caller side, but resulted in two hashes allocated on the callee side. This commit makes passing a single keyword splat to a method not allocate a hash on the caller side. Passing multiple keyword splats or a mix of explicit keywords and a keyword splat still generates a hash on the caller side. On the callee side, if arbitrary keywords are not accepted, it does not allocate a hash. If arbitrary keywords are accepted, it will allocate a hash, but this commit uses a callinfo flag to indicate whether the caller already allocated a hash, and if so, the callee can use the passed hash without duplicating it. So this commit should make it so that a maximum of a single hash is allocated during method calls. To set the callinfo flag appropriately, method call argument compilation checks if only a single keyword splat is given. If only one keyword splat is given, the VM_CALL_KW_SPLAT_MUT callinfo flag is not set, since in that case the keyword splat is passed directly and not mutable. If more than one splat is used, a new hash needs to be generated on the caller side, and in that case the callinfo flag is set, indicating the keyword splat is mutable by the callee. In compile_hash, used for both hash and keyword argument compilation, if compiling keyword arguments and only a single keyword splat is used, pass the argument directly. On the caller side, in vm_args.c, the callinfo flag needs to be recognized and handled. Because the keyword splat argument may not be a hash, it needs to be converted to a hash first if not. Then, unless the callinfo flag is set, the hash needs to be duplicated. The temporary copy of the callinfo flag, kw_flag, is updated if a hash was duplicated, to prevent the need to duplicate it again. If we are converting to a hash or duplicating a hash, we need to update the argument array, which can including duplicating the positional splat array if one was passed. CALLER_SETUP_ARG and a couple other places needs to be modified to handle similar issues for other types of calls. This includes fairly comprehensive tests for different ways keywords are handled internally, checking that you get equal results but that keyword splats on the caller side result in distinct objects for keyword rest parameters. Included are benchmarks for keyword argument calls. Brief results when compiled without optimization: def kw(a: 1) a end def kws(**kw) kw end h = {a: 1} kw(a: 1) # about same kw(**h) # 2.37x faster kws(a: 1) # 1.30x faster kws(**h) # 2.19x faster kw(a: 1, **h) # 1.03x slower kw(**h, **h) # about same kws(a: 1, **h) # 1.16x faster kws(**h, **h) # 1.14x faster Notes: Merged: https://github.com/ruby/ruby/pull/2945
2020-03-11add debug method RubyVM::mtbl2 (disabled)Koichi Sasada
2020-02-22Introduce disposable call-cache.Koichi Sasada
This patch contains several ideas: (1) Disposable inline method cache (IMC) for race-free inline method cache * Making call-cache (CC) as a RVALUE (GC target object) and allocate new CC on cache miss. * This technique allows race-free access from parallel processing elements like RCU. (2) Introduce per-Class method cache (pCMC) * Instead of fixed-size global method cache (GMC), pCMC allows flexible cache size. * Caching CCs reduces CC allocation and allow sharing CC's fast-path between same call-info (CI) call-sites. (3) Invalidate an inline method cache by invalidating corresponding method entries (MEs) * Instead of using class serials, we set "invalidated" flag for method entry itself to represent cache invalidation. * Compare with using class serials, the impact of method modification (add/overwrite/delete) is small. * Updating class serials invalidate all method caches of the class and sub-classes. * Proposed approach only invalidate the method cache of only one ME. See [Feature #16614] for more details. Notes: Merged: https://github.com/ruby/ruby/pull/2888