Project

General

Profile

Bug #15438

Threads can't switch faster than TIME_QUANTUM_(NSEC|USEC|MSEC)

Added by sylvain.joyeux (Sylvain Joyeux) about 1 month ago. Updated 5 days ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]
[ruby-core:90624]

Description

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

History

Updated by sylvain.joyeux (Sylvain Joyeux) about 1 month ago

This is the implementation of option (4) on 2.5.3 (since that's what I am using). If it has a chance to be accepted, I will forward-port it to 2.6.

Updated by sylvain.joyeux (Sylvain Joyeux) 7 days ago

Anyone interested on the subject ?

Updated by nobu (Nobuyoshi Nakada) 7 days ago

Your patch can't apply to the latest code.
Could you rebase it?

Updated by sylvain.joyeux (Sylvain Joyeux) 7 days ago

Hi nobu. Thanks for looking into this !

Here's the patch for 2.6. I would be glad if you could consider also applying the other patch to 2.5.

Updated by sylvain.joyeux (Sylvain Joyeux) 5 days ago

Quick question ... what is the preferred method to provide patches now ? I'm seeing that there are PR on GitHub ... Should I propose the patch here instead ?

Updated by nobu (Nobuyoshi Nakada) 5 days ago

sylvain.joyeux (Sylvain Joyeux) wrote:

Here's the patch for 2.6. I would be glad if you could consider also applying the other patch to 2.5.

Thank you so much.

In thread_win32.c:

-#define TIME_QUANTUM_USEC (10 * 1000)
+#define TIME_QUANTUM_USEC_BASE (100 * 1000)

Why 10 times?

+#define TIME_QUANTUM_USEC TIME_QUANTUM_USEC

_BASE is missing at the last?

+static const rb_hrtime_t TIME_QUANTUM_NSEC = &TIME_QUANTUM_USEC * 1000;

The address of a lvalue is invalid, and the multiplication too.
Just & isn't needed?

sylvain.joyeux (Sylvain Joyeux) wrote:

Quick question ... what is the preferred method to provide patches now ? I'm seeing that there are PR on GitHub ... Should I propose the patch here instead ?

Anywhere you like.

Also available in: Atom PDF