Commit graph

363 commits

Author SHA1 Message Date
Samuel Williams
cf93f98a60
Better usage of rb_ec_clear_vm_stack to maintain invariants. 2019-07-19 15:46:56 +12:00
Samuel Williams
6eef80d824
Improve ec assertions. 2019-07-19 15:46:56 +12:00
Koichi Sasada
4ec5b39ce8 initialize only Fiber's cfp.
fiber->cont.saved_ec.cfp should be initialized by NULL
because no vm_stack is allocated. However, cont_init()
captures current Fiber's cfp for continuation, so it should
only initialize fibers.
2019-07-19 08:53:19 +09:00
Samuel Williams
8ca32020b0
Revert "Ensure cfp is initialized to NULL."
This reverts commit d7fdf45a4a.
2019-07-19 11:32:40 +12:00
Samuel Williams
d7fdf45a4a
Ensure cfp is initialized to NULL.
`cont_init` didn't initialize `cont->saved_ec.cfp`. Calling `cont_mark`
would result in an invalid `cfp` in `rb_execution_context_mark`. Because
fibers lazy-initialize the stack, fibers that are created but not resumed
could cause this problem to occur.
2019-07-19 11:10:01 +12:00
Samuel Williams
9dda0a03cc
Remove rb_vm_push_frame as it is no longer used. 2019-07-19 11:10:01 +12:00
Nobuyoshi Nakada
a036a8a038
Adjust styles and indents 2019-07-19 06:35:15 +09:00
Koichi Sasada
d40d8b3caf check saved_ec.cfp 2019-07-18 22:59:44 +09:00
Samuel Williams
9790b778a3
Ensure we don't have dangling cfp. 2019-07-19 00:33:03 +12:00
git
78bc6cd8df * remove trailing spaces. 2019-07-18 17:55:22 +09:00
Samuel Williams
38e3c65a33
Improve fiber_pool_expand allocation strategy.
If `mmap` fails to allocate memory, try half the size, and so on.

Limit FIBER_POOL_ALLOCATION_MAXIMUM_SIZE to 1024 stacks. In typical
configurations this limits the memory mapped region to ~128MB per
allocation.
2019-07-18 20:54:55 +12:00
Samuel Williams
311007bf40
Add experimental RUBY_SHARED_FIBER_POOL_FREE_STACKS to control madvise. 2019-07-18 20:54:55 +12:00
Samuel Williams
001f187ed6
Make fiber_pool more conservative on platforms with limited address space.
We use COROUTINE_LIMITED_ADDRESS_SPACE to select platforms where address
space is 32-bits or less. Fiber pool implementation enables more book
keeping, and reduces upper limits, in order to minimise address space
utilisation.
2019-07-18 20:54:55 +12:00
Samuel Williams
385ea910fc
Add struct fiber_pool {int free_stacks;} to control usage of madvise.
`madvise(free)` and similar operations are good because they avoid swap
usage by clearing the dirty bit on memory pages which are mapped but no
longer needed. However, there is some performance penalty if there is no
memory pressure. Therefore, we do it by default, but it can be avoided.
2019-07-18 20:54:55 +12:00
Samuel Williams
4d60a5820a
Add FIBER_POOL_ALLOCATION_FREE to control allocation/free strategy. 2019-07-18 20:54:55 +12:00
Samuel Williams
8ac9a7be0f
Limit expansion of fiber pool on 32-bit platforms.
On 32-bit platforms, expanding the fiber pool by a large amount may fail,
even if a smaller amount may succeed. We limit the maximum size of a single
allocation to maximise the number of fibers that can be allocated.

Additionally, we implement the book-keeping required to free allocations
when their usage falls to zero.
2019-07-18 20:54:54 +12:00
Samuel Williams
77f3319071
Enable madvise to release stack space back to OS. 2019-07-18 20:54:54 +12:00
Samuel Williams
7291fef55c
Improve build process and coroutine implementation selection. 2019-07-18 20:54:54 +12:00
Samuel Williams
91aae651bf
Stack copying implementation of coroutines. 2019-07-18 20:54:54 +12:00
Samuel Williams
14cf95cff3
Implement fiber pool for reduced fiber allocation overhead.
Replace previous stack cache with fiber pool cache. The fiber pool
allocates many stacks in a single memory region. Stack allocation
becomes O(log N) and fiber creation is amortized O(1). Around 10x
performance improvement was measured in micro-benchmarks.
2019-07-18 20:54:53 +12:00
Samuel Williams
1b82c877df
Make FIBER_USE_NATIVE the default and reformat code. 2019-07-18 20:54:53 +12:00
git
93c9405e0e * expand tabs. 2019-07-08 16:00:29 +09:00
Nobuyoshi Nakada
663d58ffde
Fix indent 2019-07-08 15:13:59 +09:00
Nobuyoshi Nakada
a8c54932ba
Renamed fib to fiber 2019-07-08 15:07:43 +09:00
Jeremy Evans
7172ab0ec5 Use native coroutine implementation on OpenBSD-amd64
When using native fibers, do not load ucontext, as it isn't needed.
2019-06-26 16:23:11 -07:00
Samuel Williams
6c6bf9ffcb Add ucontext coroutine implementation for generic fallback. 2019-06-26 20:19:53 +12:00
git
5d4dfcd808 * remove trailing spaces. 2019-06-24 11:25:17 +09:00
Samuel Williams
a071bed7a5 Print warning if not using native coroutine. 2019-06-24 14:24:49 +12:00
Samuel Williams
f607e43352 Transition root fiber into state FIBER_TERMINATED.
During fork, it's possible that threads with root fibers are terminated,
but fiber state is not updated. `fiber_verify` will subsequently fail. We
forcefully enter the FIBER_TERMINATED state when terminating the root
fiber.
2019-06-20 22:36:30 +12:00
Samuel Williams
e4cafa393f Ensure that vm_stack is cleared in thread_cleanup_func_before_exec.
If `vm_stack` is left dangling in a forked process, the gc attempts to scan
it, but it is invalid and will cause a segfault. Therefore, we clear it
before forking.

In order to simplify this, `rb_ec_clear_vm_stack` was introduced.
2019-06-20 16:44:50 +12:00
Samuel Williams
c26c514494 Revert failed attempt at fixing invalid usage of vm_stack. 2019-06-20 15:30:29 +12:00
Samuel Williams
dbc2b89bc0 Ensure vm_stack is cleared after fork. 2019-06-20 13:41:18 +12:00
Samuel Williams
626483721a Set cfp to null (along with vm_stack) in rb_fiber_close. 2019-06-20 11:56:06 +12:00
Nobuyoshi Nakada
ab6d8d0b65
Adjust indent 2019-06-19 20:40:49 +09:00
Samuel Williams
d17344cfc5 Remove IA64 support. 2019-06-19 23:30:04 +12:00
Samuel Williams
cb5da39f20 Use shared implementation of rb_ec_initialize_vm_stack. 2019-06-19 20:39:10 +12:00
Samuel Williams
b8e4bea780 Track how stack was allocated for cont_free. 2019-06-19 20:39:10 +12:00
Samuel Williams
38791145eb Better handling of root fiber. 2019-06-19 20:39:10 +12:00
git
23e3c1704b * expand tabs. 2019-06-12 01:16:45 +09:00
Aaron Patterson
6db2d6d852
Add compaction support for more types.
This commit adds compaction support for:

* Fibers
* Continuations
* Autoload Constants
2019-06-11 09:16:14 -07:00
James Clarke
ecf660e438
ia64: Don't clear register_stack_start
r59829 stopped clearing stack_start and enabled the code for
!FIBER_USE_NATIVE, but we need to do the same for register_stack_start
on ia64, otherwise we end up with NULL in cont_save_machine_stack.

Closes: https://github.com/ruby/ruby/pull/2155
2019-04-27 12:35:41 +09:00
nobu
56557ec28a [DOC] fix markups [ci skip]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67337 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-03-22 11:04:59 +00:00
nobu
78d6e33702 Fix styles [ci skip]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66762 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-01-09 13:58:49 +00:00
samuel
78e3cd2bb1 Fix #endif annotation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66612 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-28 13:03:18 +00:00
samuel
9ee79b6581 Don't allow rb_fiber_resume to raise exception on unborn fiber.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66611 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-28 13:03:14 +00:00
samuel
5fb9d1e11f Implement Fiber#raise. Fixes #10344.
This allows raising exceptions in another fiber, similarly to
Thread#raise.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66610 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-28 13:03:09 +00:00
samuel
4b99725de9 Avoid GCing dead stack after switching away from a fiber
Fixes <https://bugs.ruby-lang.org/issues/14561> and discussed
<https://bugs.ruby-lang.org/issues/15362>.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66111 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-01 03:49:52 +00:00
ko1
6e33c16ffd Give up support for cross-callcc set_trace_func.
* cont.c (cont_restore_thread): cause error if trace-status is changed.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66007 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-26 19:59:08 +00:00
ko1
96990203b7 Support targetting TracePoint [Feature #15289]
* vm_trace.c (rb_tracepoint_enable_for_target): support targetting
  TracePoint. [Feature #15289]

  Tragetting TracePoint is only enabled on specified method, proc
  and so on, example: `tp.enable(target: code)`.

  `code` should be consisted of InstructionSeuqnece (iseq)
  (RubyVM::InstructionSeuqnece.of(code) should not return nil)
  If code is a tree of iseq, TracePoint is enabled on all of
  iseqs in a tree.

  Enabled tragetting TracePoints can not enabled again with
  and without target.

* vm_core.h (rb_iseq_t): introduce `rb_iseq_t::local_hooks`
  to store local hooks.
  `rb_iseq_t::aux::trace_events` is renamed to
  `global_trace_events` to contrast with `local_hooks`.

* vm_core.h (rb_hook_list_t): add `rb_hook_list_t::running`
  to represent how many Threads/Fibers are used this list.
  If this field is 0, nobody using this hooks and we can
  delete it.

  This is why we can remove code from cont.c.

* vm_core.h (rb_vm_t): because of above change, we can eliminate
  `rb_vm_t::trace_running` field.
  Also renamed from `rb_vm_t::event_hooks` to `global_hooks`.

* vm_core.h, vm.c (ruby_vm_event_enabled_global_flags): renamed
  from `ruby_vm_event_enabled_flags.

* vm_core.h, vm.c (ruby_vm_event_local_num): added to count
  enabled targetting TracePoints.

* vm_core.h, vm_trace.c (rb_exec_event_hooks): accepts
  hook list.

* vm_core.h (rb_vm_global_hooks): added for convinience.

* method.h (rb_method_bmethod_t): added to maintain Proc
  and `rb_hook_list_t` for bmethod (defined by define_method).

* prelude.rb (TracePoint#enable): extracet a keyword parameter
  (because it is easy than writing in C).
  It calls `TracePoint#__enable` internal method written in C.

* vm_insnhelper.c (vm_trace): check also iseq->local_hooks.

* vm.c (invoke_bmethod): check def->body.bmethod.hooks.

* vm.c (hook_before_rewind): check iseq->local_hooks
  and def->body.bmethod.hooks before rewind by exception.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66003 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-26 18:16:39 +00:00
samuel
38f7bb481e Use VirtualAlloc/VirtualProtect/VirtualFree for windows stack allocation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65909 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-22 02:17:44 +00:00