Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Randomize thread unblocking operation #4032

Open
tiif opened this issue Nov 13, 2024 · 8 comments
Open

Randomize thread unblocking operation #4032

tiif opened this issue Nov 13, 2024 · 8 comments
Assignees
Labels
A-concurrency Area: affects our concurrency (multi-thread) support A-shims Area: This affects the external function shims C-enhancement Category: a PR with an enhancement or an issue tracking an accepted enhancement

Comments

@tiif
Copy link
Contributor

tiif commented Nov 13, 2024

In eventfd, we would unblock multiple threads in a deterministic sequence. In epoll, we always choose the thread id from the end of the list if multiple threads are blocked on the same epoll instance. We could randomize these operation depending on the seed.

Related FIXME:

// FIXME: We can randomize the order of unblocking.
for thread_id in waiting_threads {
ecx.unblock_thread(thread_id, BlockReason::Eventfd)?;
}

// FIXME: We can randomly pick a thread to unblock.
let epoll = epfd.downcast::<Epoll>().unwrap();
if let Some(thread_id) = epoll.thread_id.borrow_mut().pop() {
waiter.push(thread_id);
};

Side note:
Blocking socketpair #3665 would need to use this too.

@tiif
Copy link
Contributor Author

tiif commented Nov 13, 2024

@rustbot label +C-enhancement +A-concurrency

@rustbot claim

@rustbot rustbot added A-concurrency Area: affects our concurrency (multi-thread) support C-enhancement Category: a PR with an enhancement or an issue tracking an accepted enhancement labels Nov 13, 2024
@oli-obk
Copy link
Contributor

oli-obk commented Nov 13, 2024

Do real systems randomize? It may cause starvation, which is usually attempted to be avoided by having some order

@RalfJung
Copy link
Member

RalfJung commented Nov 13, 2024 via email

@RalfJung
Copy link
Member

RalfJung commented Nov 13, 2024 via email

@rustbot rustbot added the A-shims Area: This affects the external function shims label Nov 13, 2024
@oli-obk
Copy link
Contributor

oli-obk commented Nov 13, 2024

I wonder if the rules here are just the usual scheduler rules. Because at least on linux, according to https://www.man7.org/linux/man-pages/man7/sched.7.html the answer seems to be "unblock all threads and run them according to the normal scheduler rules"

@tiif
Copy link
Contributor Author

tiif commented Nov 13, 2024

For epoll (edge-triggered), since it can only wake up one thread, I think which thread the kernel choose to unblock might be implementation specific (I suspect the kernel has a wakeup queue too).

For eventfd, since we are waking up all threads, would it makes sense to randomize the unblocking sequence, while still prioritizing threads that have been waiting the longest?

I am still not sure what is the behaviour of actual system, though.

@oli-obk
Copy link
Contributor

oli-obk commented Nov 13, 2024

If we can pull out the "pick next thread to resume from a list of thread ids" logic into a function, we can reuse it here. I'd like them to be the same logic, so if we change one, the other changes, too

@discord9
Copy link
Contributor

It would seems for epoll the order of waking up depend on how scheduler decide? Correct me if I'm wrong, but seems here use schedule_hrtimeout_range which wait a given time and may return early if a signal is delivered to the current task(which is determined by scheduler?). eventpoll itself also have a wait queue but it doesn't seem to affect unblocking order it seems?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-concurrency Area: affects our concurrency (multi-thread) support A-shims Area: This affects the external function shims C-enhancement Category: a PR with an enhancement or an issue tracking an accepted enhancement
Projects
None yet
Development

No branches or pull requests

5 participants