From: Paolo Bonzini <pbonzini@redhat.com>
To: Nicolas Saenz Julienne <nsaenzju@redhat.com>, qemu-devel@nongnu.org
Cc: stefanha@redhat.com, "Lukáš Doktor" <ldoktor@redhat.com>
Subject: Re: [PATCH v3 2/3] thread-pool: replace semaphore with condition variable
Date: Tue, 17 May 2022 16:18:07 +0200 [thread overview]
Message-ID: <ecf91e34-b588-f3ee-45eb-34fbde597cad@redhat.com> (raw)
In-Reply-To: <c5fcbce258e2671f1ee22b3f4fdddea361cb2509.camel@redhat.com>
On 5/17/22 14:46, Nicolas Saenz Julienne wrote:
>> - while (!pool->stopping) {
>> + while (!pool->stopping && pool->cur_threads <= pool->max_threads) {
>> ThreadPoolElement *req;
>> int ret;
>>
>> - do {
>> + if (QTAILQ_EMPTY(&pool->request_list)) {
>> pool->idle_threads++;
>> - qemu_mutex_unlock(&pool->lock);
>> - ret = qemu_sem_timedwait(&pool->sem, 10000);
>> - qemu_mutex_lock(&pool->lock);
>> + ret = qemu_cond_timedwait(&pool->request_cond, &pool->lock, 10000);
>> pool->idle_threads--;
>> - } while (back_to_sleep(pool, ret));
>> - if (ret == -1 || pool->stopping ||
>> - pool->cur_threads > pool->max_threads) {
>> - break;
>> + if (ret == 0 &&
>> + QTAILQ_EMPTY(&pool->request_list) &&
>> + pool->cur_threads > pool->min_threads) {
>> + /* Timed out + no work to do + no need for warm threads = exit. */
>> + break;
>> + }
>
> Some comments:
>
> - A completely idle pool will now never be able to lose its threads, as the
> 'pool->cur_threads <= pool->max_threads' condition is only checked after a
> non-timeout wakeup.
Are you sure? The full code is:
ret = qemu_cond_timedwait(&pool->request_cond, &pool->lock, 10000);
pool->idle_threads--;
if (ret == 0 &&
QTAILQ_EMPTY(&pool->request_list) &&
pool->cur_threads > pool->min_threads) {
/* Timed out + no work to do + no need for warm threads exit. */
break;
}
/*
* Even if there was some work to do, check if there aren't
* too many worker threads before picking it up.
*/
continue;
That is, it won't immediately pick up the job after _any_ wait,
whether successful or not. It will first of all "continue" to
check pool->cur_threads <= pool->max_threads.
This is also the reason why I had to add a qemu_cond_signal() at the
bottom of the worker thread (because maybe it got a signal to act on a
non-empty queue, but decided to exit instead).
> - You don't take into account the possibility of being woken up with an empty
> queue. Which I belive possible:
It's absolutely possible, but the difference between v2 and v3 _should_
be the fix. Of course I could have screwed up, but it seems correct
this time.
Paolo
next prev parent reply other threads:[~2022-05-17 14:49 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-14 6:50 [PATCH v3 0/3] thread-pool: fix performance regression Paolo Bonzini
2022-05-14 6:50 ` [PATCH v3 1/3] thread-pool: optimize scheduling of completion bottom half Paolo Bonzini
2022-05-17 15:20 ` Stefan Hajnoczi
2022-05-14 6:50 ` [PATCH v3 2/3] thread-pool: replace semaphore with condition variable Paolo Bonzini
2022-05-17 12:46 ` Nicolas Saenz Julienne
2022-05-17 14:18 ` Paolo Bonzini [this message]
2022-05-17 14:37 ` Nicolas Saenz Julienne
2022-05-17 15:19 ` Stefan Hajnoczi
2022-05-17 15:21 ` Paolo Bonzini
2022-05-14 6:50 ` [PATCH v3 3/3] thread-pool: remove stopping variable Paolo Bonzini
2022-05-17 15:20 ` Stefan Hajnoczi
2022-05-17 14:30 ` [PATCH v3 0/3] thread-pool: fix performance regression Nicolas Saenz Julienne
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ecf91e34-b588-f3ee-45eb-34fbde597cad@redhat.com \
--to=pbonzini@redhat.com \
--cc=ldoktor@redhat.com \
--cc=nsaenzju@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).