[ruby-core:117738] [Ruby master Feature#15438] Threads can't switch faster than TIME_QUANTUM_(NSEC|USEC|MSEC)

Issue #15438 has been updated by jhawthorn (John Hawthorn). Status changed from Open to Closed I think this is something we should improve more (I would like even faster switching times), but it does seem possible as of Ruby 3.3 to have threads switch faster than 100ms. ``` def test_switching(priority = 0) done = false started = false busy = Thread.new do Thread.current.priority = priority until done started = true end end Thread.pass until started times = [] while times.length < 10 before = Process.clock_gettime(Process::CLOCK_MONOTONIC) Thread.pass after = Process.clock_gettime(Process::CLOCK_MONOTONIC) times << (after - before) end done = true busy.join times end puts RUBY_VERSION (-3).upto(3) do |priority| print "Priority: #{priority}" times = test_switching(priority) times = times.sort[1..-2] # drop fastest and slowest average = (times.sum / times.length) puts " average: #{average}" end ``` ``` $ ruby test_measure_switching_times.rb 3.2.2 Priority: -3 average: 0.10010529025021242 Priority: -2 average: 0.10010091188087245 Priority: -1 average: 0.10010025675364886 Priority: 0 average: 0.10010364262052462 Priority: 1 average: 0.20017941037440323 Priority: 2 average: 0.4003785701279412 Priority: 3 average: 0.8007820281236491 ``` ``` $ ruby test_measure_switching_times.rb 3.3.1 Priority: -3 average: 0.02040818374371156 Priority: -2 average: 0.03023905074587674 Priority: -1 average: 0.05040335562443943 Priority: 0 average: 0.10088755612378009 Priority: 1 average: 0.20198591962616774 Priority: 2 average: 0.4033456226279668 Priority: 3 average: 0.8073137137507729 ``` ---------------------------------------- Feature #15438: Threads can't switch faster than TIME_QUANTUM_(NSEC|USEC|MSEC) https://bugs.ruby-lang.org/issues/15438#change-108145 * Author: sylvain.joyeux (Sylvain Joyeux) * Status: Closed ---------------------------------------- Thread#priority can be set to negative values, which when looking at the code is meant to reduce the time allocated to the thread. However, as far as I could understand in the codebase, the quantum of time is definitely hard-coded to 100ms (TIME_QUANTUM_...). This means that the "lower allocated time" would only work for threads that would often yield one way or the other (sleep, blocking calls, ...) My projects would definitely benefit from a faster switching period. I was wondering how best to implement this ability ? I thought of the following: 1. globally using an environment variable 2. globally using an API 3. trying to adapt dynamically, using the highest needed period 4. lowering the period when a priority lower than 0 is set, leaving it at the lower period. Obviously (3) would seem to be the best, but I'm not sure I would be able to get it right in a decent amount of time. (4) seem to be a good trade-off between simplicity and performance (nothing changes if you never use priorities lower than 0, and if you were you basically get what you wanted). What do you think ? ---Files-------------------------------- 0001-dynamically-modify-the-timer-thread-period-to-accoun.patch (3.12 KB) 0001-2.6-fix-handling-of-negative-priorities.patch (8.43 KB) -- https://bugs.ruby-lang.org/
participants (1)
-
jhawthorn (John Hawthorn)