Project

General

Profile

Actions

Feature #20861

closed

Add an environment variable for tuning the default thread quantum

Added by tenderlovemaking (Aaron Patterson) about 2 months ago. Updated 9 days ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:119683]

Description

The default thread quantum is currently hard coded at 100ms. This can impact multithreaded systems that are trying to process Ruby level CPU bound work at the same time as IO work.

I would like to add an environment variable RUBY_THREAD_DEFAULT_QUANTUM_MS that allows users to specify the default thread quantum (in milliseconds) via an environment variable. It defaults to our current default of 100ms. I've submitted the patch here.

Here is a Ruby program to demonstrate the problem:

def measure
  x = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  yield
  Process.clock_gettime(Process::CLOCK_MONOTONIC) - x
end

def fib(n)
  if n < 2
    n
  else
    fib(n-2) + fib(n-1)
  end
end

# find fib that takes ~500ms
fib_i = 50.times.find { |i| measure { fib(i) } >= 0.05 }
sleep_i = measure { fib(fib_i) }

threads = [
  Thread.new {
    100.times {
      sleep(sleep_i)
      # sometimes stalled waiting for fib's quantum to finish
    }
    puts "done 1"
  },
  Thread.new { 100.times { fib(fib_i) }; puts "done 2" },
]

# We expect the total time to be about 100 * sleep_i (~5 seconds) because
# theoretically the sleep thread could be done nearly completely in parallel to
# the fib thread.
#
# But because the `sleep` thread is iterating over the sleep call, it must wait
# for the `fib` thread to complete its quantum, before it can start the next iteration.
#
# This means each sleep iteration could take up to `sleep_i + 100ms`
#
# We're calling that stalled time "waste"
total = measure { threads.each(&:join) }
waste = total - (sleep_i * 100)
p TOTAL: total, WASTE: waste

The program has two threads. One thread is using CPU time by computing fib in a loop. The other thread is simulating IO time by calling sleep in a loop. When the sleep call completes, it can stall, waiting for the quantum in the fib thread to expire. That means that each iteration on sleep can actually take sleep time + thread quantum, or in this case ~600ms when we expected it to only take ~500ms.

Ideally, the above program would take 500ms * 100 since all sleep calls should be able to execute in parallel with the fib calls. Of course this isn't true because the sleep thread must acquire the GVL before it can continue the next iteration, so there will always be some overhead. This feature is for allowing people to tune that overhead.

If we run this program with the default quantum the output looks like this:

$ ./miniruby -v fibtest.rb
ruby 3.4.0dev (2024-11-01T14:49:50Z quantum-computing c7708d22c3) +PRISM [arm64-darwin24]
done 2
done 1
{TOTAL: 12.672821999993175, WASTE: 4.960721996147186}

The output shows that our program spent about 5 seconds stalled, waiting to acquire the GVL.

With this patch we can lower the default quantum, and the output is like this:

$ RUBY_THREAD_DEFAULT_QUANTUM_MS=10 ./miniruby -v fibtest.rb
ruby 3.4.0dev (2024-11-01T22:06:35Z quantum-computing 087500643d) +PRISM [arm64-darwin24]
done 2
done 1
{TOTAL: 8.898526000091806, WASTE: 1.4168260043952614}

Specifying the ENV to change the quantum to 10ms lowered our waste in the program to ~1.4 seconds.

It's common for web applications to do mixed CPU and IO bound tasks in threads (see the Puma webserver), so it would be great if there was a way to customize the thread quantum depending on your application's workload.

Updated by tenderlovemaking (Aaron Patterson) about 2 months ago

I think I did my math a little wrong. It should be 50ms rather than 500ms, but the measurements are correct. Specifying the 10ms quantum reduces waste by ~3.5 seconds. 😅

Updated by ioquatix (Samuel Williams) about 2 months ago

This can be useful, so I agree with adding it.

For the sake of providing feedback, some thoughts:

  1. Does the default value of 100ms make sense? Should we update the default too?
  2. Should we mark this as experimental in the first pass, e.g. emit a warning if it is set? Maybe we can experiment with it in 3.4 and commit in 3.5 if it looks good? (I'm also okay with your proposal as is - just food for thought).
  3. Is there a way we could automatically tune this number according to the workload? For example, could we measure the unfairness of the scheduler and adjust accordingly?

Regarding (3), I think it’s fantastic to highlight this issue and provide a way to address it. However, IMHO, most users may not be familiar with tuning this setting effectively. A fixed value might perform well in some scenarios but may not hold up in others. Ideally, Ruby could adaptively determine the optimal value for the best performance, sparing users the need to tweak it themselves. In other words, if it were possible to determine this value automatically, this proposal might be less beneficial—or even detrimental if a fixed value were worse than an automatic adjustment system.

Updated by nobu (Nobuyoshi Nakada) about 2 months ago

Your patch misses pthread_win32.c, and if we add a new environment variable, man/ruby.1 must be updated as well.

Updated by byroot (Jean Boussier) about 2 months ago

This was discussed a few times at Kaigi, and IMO a quantum value on a per thread basis would make more sense.

Updated by tenderlovemaking (Aaron Patterson) about 2 months ago

ioquatix (Samuel Williams) wrote in #note-2:

This can be useful, so I agree with adding it.

For the sake of providing feedback, some thoughts:

  1. Does the default value of 100ms make sense? Should we update the default too?
  2. Should we mark this as experimental in the first pass, e.g. emit a warning if it is set? Maybe we can experiment with it in 3.4 and commit in 3.5 if it looks good? (I'm also okay with your proposal as is - just food for thought).
  3. Is there a way we could automatically tune this number according to the workload? For example, could we measure the unfairness of the scheduler and adjust accordingly?

Regarding (3), I think it’s fantastic to highlight this issue and provide a way to address it. However, IMHO, most users may not be familiar with tuning this setting effectively. A fixed value might perform well in some scenarios but may not hold up in others. Ideally, Ruby could adaptively determine the optimal value for the best performance, sparing users the need to tweak it themselves. In other words, if it were possible to determine this value automatically, this proposal might be less beneficial—or even detrimental if a fixed value were worse than an automatic adjustment system.

Thank you for the feedback, but I think these points should be addressed as a different feature. I do think the default quantum should be lowered, but it's hard to experiment with other values while this one is hard coded. If people are allowed to experiment with other values, I think we can make a more informed decision about a "good" default.

nobu (Nobuyoshi Nakada) wrote in #note-3:

Your patch misses pthread_win32.c, and if we add a new environment variable, man/ruby.1 must be updated as well.

Thanks. I think I've fixed it.

byroot (Jean Boussier) wrote in #note-4:

This was discussed a few times at Kaigi, and IMO a quantum value on a per thread basis would make more sense.

Yes, I think it's good to control the quantum on a per thread basis. We can already do that with Thread#priority (as you know). Are you thinking something different (like specify quantum in time rather than priority?)

Most threaded apps don't set thread priority. Puma, Sidekiq, and even the thread pools in concurrent-ruby don't offer a setting to change the quantum/priority. I think that they should offer this setting, but priority is relative to the default quantum, and there is no way to experiment with the default quantum.

I think being able to adjust the default quantum via env var is a good feature because people can experiment without changing any application code, and would be a good way for us to find a better default.

Updated by byroot (Jean Boussier) about 2 months ago

Are you thinking something different (like specify quantum in time rather than priority?)

Yes, quite literally: Thread.current.quantum = 20 or something like that.

Which IMO is much easier to reason about than priorities. And generally you have threads that are meant as "main" threads and some that are meant as "background" and you'd want them to have different quantums, hence why I'd rather skip the environment variable and go straight to an accessor.

But perhaps a Thread.default_quantum = XX would be needed too.

Updated by tenderlovemaking (Aaron Patterson) about 2 months ago

byroot (Jean Boussier) wrote in #note-6:

Are you thinking something different (like specify quantum in time rather than priority?)

Yes, quite literally: Thread.current.quantum = 20 or something like that.

Which IMO is much easier to reason about than priorities. And generally you have threads that are meant as "main" threads and some that are meant as "background" and you'd want them to have different quantums, hence why I'd rather skip the environment variable and go straight to an accessor.

Makes sense, and I agree.

But perhaps a Thread.default_quantum = XX would be needed too.

I think a Thread.default_quantum= would be very useful in the same way I mentioned the environment variable being useful. Specifically for apps where you can't specify the quantum, like with Puma / Sidekiq etc.

I will make a patches and tickets for these.

Updated by jhawthorn (John Hawthorn) about 2 months ago

I don't think we should expose the quantum per-thread inside Ruby. I worry it will prevent future improvements. The fact that Thread scheduling and priorities are currently done by giving a shorter/longer quantum is an implementation detail that could (and I'd like to) change. Thread priority is good because it is more abstract, which gives flexibility on implementation.

Similarly I don't love Thread.default_quantum= as it's implementation specific - I wouldn't expect it to make sense for JRuby/TruffleRuby for example, and it's possible CRuby could have a different implementation in the future. Though I don't feel as strongly.

I think an environment variable is best as it's very similar to how we allow tuning the garbage collector via environment variables. In the future if end up being able to remove this, that's much easier/safer with an environment variable.

Updated by byroot (Jean Boussier) about 2 months ago

Thread priority is good because it is more abstract, which gives flexibility on implementation.

Then we should document what it does, even if stated that the behavior may change between version. Because in its current state it's pretty much impossible to use it.

Updated by ivoanjo (Ivo Anjo) about 2 months ago

I think this is really nice first step and worth having to enable experimentation as well.

Having said that, I'm not sure there's ever going to be a good value single (even per-thread). It may be worth considering on the medium/long-term the introduction of an actual scheduling algorithm, even if a very simple one. Such an algorithm could make sure to respect priorities, take into account sleep/wake up timelines, and introduce (some amount of) fairness.

Updated by ko1 (Koichi Sasada) about 2 months ago · Edited

This example doesn't make sense for the real app because nobody repeat sleeping for the constant.
Do you have any example similar to the example?

But the current 100ms is not based on strong opinion (it equals to Linux time quantum), so I'm okay to make it configurable (I didn't check the patch though).

Updated by ivoanjo (Ivo Anjo) about 2 months ago

https://github.com/ivoanjo/gvl-tracing/blob/master/examples/rubykaigi2023/rk-example6.rb (from here) is probably a good example without sleep -- the thread doing I/O keeps being penalized because it uses very little time, but needs to wait for the full 100ms period.

Updated by tenderlovemaking (Aaron Patterson) about 1 month ago

ko1 (Koichi Sasada) wrote in #note-11:

This example doesn't make sense for the real app because nobody repeat sleeping for the constant.
Do you have any example similar to the example?

But the current 100ms is not based on strong opinion (it equals to Linux time quantum), so I'm okay to make it configurable (I didn't check the patch though).

As @ivoanjo (Ivo Anjo) posted, it's usually some kind of IO mixed with CPU. For example using Net::HTTP and the response is slow, or even making database queries. The sleep example is just a simple way to demonstrate the impact of the quantum on IO calls.

Updated by tenderlovemaking (Aaron Patterson) about 1 month ago

At RubyConf we discussed this a bit and it seemed like the feature is fine, but maybe the name isn't good enough.

How about RUBY_THREAD_SWITCH_INTERVAL?

I like having "MS" in the name so that the units are obvious, but I really don't have a strong opinion.

Updated by jhawthorn (John Hawthorn) about 1 month ago

Another option for naming is to use "time slice": RUBY_THREAD_TIMESLICE. I'm okay with any of the proposed names

For other prior art Linux's CFS scheduler exposes /sys/kernel/debug/sched/base_slice_ns

Updated by Dan0042 (Daniel DeLorme) 27 days ago

byroot (Jean Boussier) wrote in #note-6:

Yes, quite literally: Thread.current.quantum = 20 or something like that.

Which IMO is much easier to reason about than priorities. And generally you have threads that are meant as "main" threads and some that are meant as "background" and you'd want them to have different quantums, hence why I'd rather skip the environment variable and go straight to an accessor.

But perhaps a Thread.default_quantum = XX would be needed too.

What happened to this idea? According to the latest comments it seems we're back to the idea of an environment variable, but that means we

  • can't set the quantum after process start (let's say some performance monitoring periodically adjusts the value)
  • can't set the quantum per thread (but that seem to be fine according to #note-8)
  • can't set the quantum to a different value in a forked process

As long as those limitations are not a problem, then an environment variable is fine.

Updated by ko1 (Koichi Sasada) 12 days ago

I like RUBY_THREAD_TIME_QUANTUM for environment variable.

Updated by matz (Yukihiro Matsumoto) 10 days ago

I vote for the environment variable RUBY_THREAD_TIMESLICE. The term quantum is unfamiliar to me.

Matz.

Actions #20

Updated by tenderlovemaking (Aaron Patterson) 9 days ago

  • Status changed from Open to Closed

Applied in changeset git|fffef9aa5d89b6cca3dc634f5278b1fc67fb3d08.


Add an environment variable for controlling the default Thread quantum

This commit adds an environment variable RUBY_THREAD_TIMESLICE for
specifying the default thread quantum in milliseconds. You can adjust
this variable to tune throughput, which is especially useful on
multithreaded systems that are mixing CPU bound work and IO bound work.

The default quantum remains 100ms.

[Feature #20861]

Co-Authored-By: John Hawthorn

Actions

Also available in: Atom PDF

Like3
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0