2004-03-12 19:07:18 +00:00
|
|
|
.\" Copyright (c) 2000-2004 John H. Baldwin <jhb@FreeBSD.org>
|
2001-01-23 19:24:35 +00:00
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
.\"
|
|
|
|
.\" $FreeBSD$
|
|
|
|
.\"
|
Add wakeup_any(), cheaper wakeup_one() for taskqueue(9).
wakeup_one() and underlying sleepq_signal() spend additional time trying
to be fair, waking thread with highest priority, sleeping longest time.
But in case of taskqueue there are many absolutely identical threads, and
any fairness between them is quite pointless. It makes even worse, since
round-robin wakeups not only make previous CPU affinity in scheduler quite
useless, but also hide from user chance to see CPU bottlenecks, when
sequential workload with one request at a time looks evenly distributed
between multiple threads.
This change adds new SLEEPQ_UNFAIR flag to sleepq_signal(), making it wakeup
thread that went to sleep last, but no longer in context switch (to avoid
immediate spinning on the thread lock). On top of that new wakeup_any()
function is added, equivalent to wakeup_one(), but setting the flag.
On top of that taskqueue(9) is switchied to wakeup_any() to wakeup its
threads.
As result, on 72-core Xeon v4 machine sequential ZFS write to 12 ZVOLs
with 16KB block size spend 34% less time in wakeup_any() and descendants
then it was spending in wakeup_one(), and total write throughput increased
by ~10% with the same as before CPU usage.
Reviewed by: markj, mmacy
MFC after: 2 weeks
Sponsored by: iXsystems, Inc.
Differential Revision: https://reviews.freebsd.org/D20669
2019-06-20 01:15:33 +00:00
|
|
|
.Dd June 19, 2019
|
2001-01-23 19:24:35 +00:00
|
|
|
.Dt SLEEPQUEUE 9
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
2004-03-12 19:07:18 +00:00
|
|
|
.Nm init_sleepqueues ,
|
|
|
|
.Nm sleepq_abort ,
|
|
|
|
.Nm sleepq_add ,
|
|
|
|
.Nm sleepq_alloc ,
|
|
|
|
.Nm sleepq_broadcast ,
|
|
|
|
.Nm sleepq_free ,
|
2005-04-19 16:30:25 +00:00
|
|
|
.Nm sleepq_lock ,
|
2004-03-12 19:07:18 +00:00
|
|
|
.Nm sleepq_lookup ,
|
|
|
|
.Nm sleepq_release ,
|
|
|
|
.Nm sleepq_remove ,
|
|
|
|
.Nm sleepq_signal ,
|
|
|
|
.Nm sleepq_set_timeout ,
|
2013-03-04 19:10:39 +00:00
|
|
|
.Nm sleepq_set_timeout_sbt ,
|
In current code, threads performing an interruptible sleep (on both
sxlock, via the sx_{s, x}lock_sig() interface, or plain lockmgr), will
leave the waiters flag on forcing the owner to do a wakeup even when if
the waiter queue is empty.
That operation may lead to a deadlock in the case of doing a fake wakeup
on the "preferred" (based on the wakeup algorithm) queue while the other
queue has real waiters on it, because nobody is going to wakeup the 2nd
queue waiters and they will sleep indefinitively.
A similar bug, is present, for lockmgr in the case the waiters are
sleeping with LK_SLEEPFAIL on. In this case, even if the waiters queue
is not empty, the waiters won't progress after being awake but they will
just fail, still not taking care of the 2nd queue waiters (as instead the
lock owned doing the wakeup would expect).
In order to fix this bug in a cheap way (without adding too much locking
and complicating too much the semantic) add a sleepqueue interface which
does report the actual number of waiters on a specified queue of a
waitchannel (sleepq_sleepcnt()) and use it in order to determine if the
exclusive waiters (or shared waiters) are actually present on the lockmgr
(or sx) before to give them precedence in the wakeup algorithm.
This fix alone, however doesn't solve the LK_SLEEPFAIL bug. In order to
cope with it, add the tracking of how many exclusive LK_SLEEPFAIL waiters
a lockmgr has and if all the waiters on the exclusive waiters queue are
LK_SLEEPFAIL just wake both queues.
The sleepq_sleepcnt() introduction and ABI breakage require
__FreeBSD_version bumping.
Reported by: avg, kib, pho
Reviewed by: kib
Tested by: pho
2009-12-12 21:31:07 +00:00
|
|
|
.Nm sleepq_sleepcnt ,
|
2004-03-12 19:07:18 +00:00
|
|
|
.Nm sleepq_timedwait ,
|
|
|
|
.Nm sleepq_timedwait_sig ,
|
2010-01-09 01:46:38 +00:00
|
|
|
.Nm sleepq_type ,
|
2004-03-12 19:07:18 +00:00
|
|
|
.Nm sleepq_wait ,
|
|
|
|
.Nm sleepq_wait_sig
|
|
|
|
.Nd manage the queues of sleeping threads
|
2001-01-23 19:24:35 +00:00
|
|
|
.Sh SYNOPSIS
|
2001-10-01 16:09:29 +00:00
|
|
|
.In sys/param.h
|
2004-03-12 19:07:18 +00:00
|
|
|
.In sys/sleepqueue.h
|
2001-01-23 19:24:35 +00:00
|
|
|
.Ft void
|
2004-03-12 19:07:18 +00:00
|
|
|
.Fn init_sleepqueues "void"
|
2008-08-07 20:47:01 +00:00
|
|
|
.Ft int
|
2004-03-12 19:07:18 +00:00
|
|
|
.Fn sleepq_abort "struct thread *td"
|
2001-01-23 19:24:35 +00:00
|
|
|
.Ft void
|
2019-12-24 16:19:33 +00:00
|
|
|
.Fn sleepq_add "const void *wchan" "struct lock_object *lock" "const char *wmesg" "int flags" "int queue"
|
2004-03-12 19:07:18 +00:00
|
|
|
.Ft struct sleepqueue *
|
|
|
|
.Fn sleepq_alloc "void"
|
2008-08-07 20:47:01 +00:00
|
|
|
.Ft int
|
2019-12-24 16:19:33 +00:00
|
|
|
.Fn sleepq_broadcast "const void *wchan" "int flags" "int pri" "int queue"
|
2004-03-12 19:07:18 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn sleepq_free "struct sleepqueue *sq"
|
|
|
|
.Ft struct sleepqueue *
|
2019-12-24 16:19:33 +00:00
|
|
|
.Fn sleepq_lookup "const void *wchan"
|
2004-03-12 19:07:18 +00:00
|
|
|
.Ft void
|
2019-12-24 16:19:33 +00:00
|
|
|
.Fn sleepq_lock "const void *wchan"
|
2005-04-19 16:30:25 +00:00
|
|
|
.Ft void
|
2019-12-24 16:19:33 +00:00
|
|
|
.Fn sleepq_release "const void *wchan"
|
2004-03-12 19:07:18 +00:00
|
|
|
.Ft void
|
2019-12-24 16:19:33 +00:00
|
|
|
.Fn sleepq_remove "struct thread *td" "const void *wchan"
|
2008-08-07 20:47:01 +00:00
|
|
|
.Ft int
|
2019-12-24 16:19:33 +00:00
|
|
|
.Fn sleepq_signal "const void *wchan" "int flags" "int pri" "int queue"
|
2004-03-12 19:07:18 +00:00
|
|
|
.Ft void
|
2019-12-24 16:19:33 +00:00
|
|
|
.Fn sleepq_set_timeout "const void *wchan" "int timo"
|
2013-03-04 19:10:39 +00:00
|
|
|
.Ft void
|
2019-12-24 16:19:33 +00:00
|
|
|
.Fn sleepq_set_timeout_sbt "const void *wchan" "sbintime_t sbt" \
|
2013-03-04 19:10:39 +00:00
|
|
|
"sbintime_t pr" "int flags"
|
In current code, threads performing an interruptible sleep (on both
sxlock, via the sx_{s, x}lock_sig() interface, or plain lockmgr), will
leave the waiters flag on forcing the owner to do a wakeup even when if
the waiter queue is empty.
That operation may lead to a deadlock in the case of doing a fake wakeup
on the "preferred" (based on the wakeup algorithm) queue while the other
queue has real waiters on it, because nobody is going to wakeup the 2nd
queue waiters and they will sleep indefinitively.
A similar bug, is present, for lockmgr in the case the waiters are
sleeping with LK_SLEEPFAIL on. In this case, even if the waiters queue
is not empty, the waiters won't progress after being awake but they will
just fail, still not taking care of the 2nd queue waiters (as instead the
lock owned doing the wakeup would expect).
In order to fix this bug in a cheap way (without adding too much locking
and complicating too much the semantic) add a sleepqueue interface which
does report the actual number of waiters on a specified queue of a
waitchannel (sleepq_sleepcnt()) and use it in order to determine if the
exclusive waiters (or shared waiters) are actually present on the lockmgr
(or sx) before to give them precedence in the wakeup algorithm.
This fix alone, however doesn't solve the LK_SLEEPFAIL bug. In order to
cope with it, add the tracking of how many exclusive LK_SLEEPFAIL waiters
a lockmgr has and if all the waiters on the exclusive waiters queue are
LK_SLEEPFAIL just wake both queues.
The sleepq_sleepcnt() introduction and ABI breakage require
__FreeBSD_version bumping.
Reported by: avg, kib, pho
Reviewed by: kib
Tested by: pho
2009-12-12 21:31:07 +00:00
|
|
|
.Ft u_int
|
2019-12-24 16:19:33 +00:00
|
|
|
.Fn sleepq_sleepcnt "const void *wchan" "int queue"
|
2004-03-12 19:07:18 +00:00
|
|
|
.Ft int
|
2019-12-24 16:19:33 +00:00
|
|
|
.Fn sleepq_timedwait "const void *wchan" "int pri"
|
2004-03-12 19:07:18 +00:00
|
|
|
.Ft int
|
2019-12-24 16:19:33 +00:00
|
|
|
.Fn sleepq_timedwait_sig "const void *wchan" "int pri"
|
2010-01-09 01:46:38 +00:00
|
|
|
.Ft int
|
2019-12-24 16:19:33 +00:00
|
|
|
.Fn sleepq_type "const void *wchan"
|
2004-03-12 19:07:18 +00:00
|
|
|
.Ft void
|
2019-12-24 16:19:33 +00:00
|
|
|
.Fn sleepq_wait "const void *wchan" "int pri"
|
2004-03-12 19:07:18 +00:00
|
|
|
.Ft int
|
2019-12-24 16:19:33 +00:00
|
|
|
.Fn sleepq_wait_sig "const void *wchan" "int pri"
|
2001-01-23 19:24:35 +00:00
|
|
|
.Sh DESCRIPTION
|
2004-03-12 19:07:18 +00:00
|
|
|
Sleep queues provide a mechanism for suspending execution of a thread until
|
|
|
|
some condition is met.
|
|
|
|
Each queue is associated with a specific wait channel when it is active,
|
|
|
|
and only one queue may be associated with a wait channel at any given point
|
|
|
|
in time.
|
2007-09-28 11:13:40 +00:00
|
|
|
The implementation of each wait channel splits its sleepqueue into 2 sub-queues
|
|
|
|
in order to enable some optimizations on threads' wakeups.
|
2004-03-12 19:07:18 +00:00
|
|
|
An active queue holds a list of threads that are blocked on the associated
|
|
|
|
wait channel.
|
|
|
|
Threads that are not blocked on a wait channel have an associated inactive
|
|
|
|
sleep queue.
|
|
|
|
When a thread blocks on a wait channel it donates its inactive sleep queue
|
|
|
|
to the wait channel.
|
|
|
|
When a thread is resumed,
|
|
|
|
the wait channel that it was blocked on gives it an inactive sleep queue for
|
|
|
|
later use.
|
2005-04-19 16:30:25 +00:00
|
|
|
.Pp
|
2004-03-12 19:07:18 +00:00
|
|
|
The
|
|
|
|
.Fn sleepq_alloc
|
2005-04-19 16:30:25 +00:00
|
|
|
function allocates an inactive sleep queue and is used to assign a
|
|
|
|
sleep queue to a thread during thread creation.
|
2004-03-12 19:07:18 +00:00
|
|
|
The
|
|
|
|
.Fn sleepq_free
|
|
|
|
function frees the resources associated with an inactive sleep queue and is
|
|
|
|
used to free a queue during thread destruction.
|
|
|
|
.Pp
|
|
|
|
Active sleep queues are stored in a hash table hashed on the addresses pointed
|
|
|
|
to by wait channels.
|
|
|
|
Each bucket in the hash table contains a sleep queue chain.
|
|
|
|
A sleep queue chain contains a spin mutex and a list of sleep queues that hash
|
|
|
|
to that specific chain.
|
|
|
|
Active sleep queues are protected by their chain's spin mutex.
|
|
|
|
The
|
|
|
|
.Fn init_sleepqueues
|
|
|
|
function initializes the hash table of sleep queue chains.
|
|
|
|
.Pp
|
|
|
|
The
|
2005-04-19 16:30:25 +00:00
|
|
|
.Fn sleepq_lock
|
|
|
|
function locks the sleep queue chain associated with wait channel
|
|
|
|
.Fa wchan .
|
|
|
|
.Pp
|
|
|
|
The
|
2004-03-12 19:07:18 +00:00
|
|
|
.Fn sleepq_lookup
|
2005-04-19 16:30:25 +00:00
|
|
|
returns a pointer to the currently active sleep queue for that wait
|
|
|
|
channel associated with
|
2004-03-12 19:07:18 +00:00
|
|
|
.Fa wchan
|
2001-01-23 19:24:35 +00:00
|
|
|
or
|
2004-03-12 19:07:18 +00:00
|
|
|
.Dv NULL
|
2005-04-19 16:30:25 +00:00
|
|
|
if there is no active sleep queue associated with
|
|
|
|
argument
|
|
|
|
.Fa wchan .
|
|
|
|
It requires the sleep queue chain associated with
|
|
|
|
.Fa wchan
|
|
|
|
to have been locked by a prior call to
|
|
|
|
.Fn sleepq_lock .
|
|
|
|
.Pp
|
2004-03-12 19:07:18 +00:00
|
|
|
The
|
|
|
|
.Fn sleepq_release
|
|
|
|
function unlocks the sleep queue chain associated with
|
|
|
|
.Fn wchan
|
|
|
|
and is primarily useful when aborting a pending sleep request before one of
|
|
|
|
the wait functions is called.
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn sleepq_add
|
|
|
|
function places the current thread on the sleep queue associated with the
|
|
|
|
wait channel
|
|
|
|
.Fa wchan .
|
2005-04-19 16:30:25 +00:00
|
|
|
The sleep queue chain associated with argument
|
|
|
|
.Fa wchan
|
|
|
|
must be locked by a prior call to
|
|
|
|
.Fn sleepq_lock
|
|
|
|
when this function is called.
|
2006-11-16 01:02:00 +00:00
|
|
|
If a lock is specified via the
|
2004-03-12 19:07:18 +00:00
|
|
|
.Fa lock
|
2005-04-19 16:30:25 +00:00
|
|
|
argument, and if the kernel was compiled with
|
|
|
|
.Cd "options INVARIANTS" ,
|
|
|
|
then the sleep queue code will perform extra checks to ensure that
|
2006-11-16 01:02:00 +00:00
|
|
|
the lock is used by all threads sleeping on
|
2004-03-12 19:07:18 +00:00
|
|
|
.Fa wchan .
|
|
|
|
The
|
|
|
|
.Fa wmesg
|
|
|
|
parameter should be a short description of
|
|
|
|
.Fa wchan .
|
|
|
|
The
|
|
|
|
.Fa flags
|
2004-08-19 12:46:02 +00:00
|
|
|
parameter is a bitmask consisting of the type of sleep queue being slept on
|
|
|
|
and zero or more optional flags.
|
2007-09-28 11:13:40 +00:00
|
|
|
The
|
|
|
|
.Fa queue
|
|
|
|
parameter specifies the sub-queue, in which the contending thread will be
|
|
|
|
inserted.
|
2005-04-19 16:30:25 +00:00
|
|
|
.Pp
|
2007-03-09 18:06:36 +00:00
|
|
|
There are currently three types of sleep queues:
|
2005-04-19 16:30:25 +00:00
|
|
|
.Pp
|
|
|
|
.Bl -tag -width ".Dv SLEEPQ_CONDVAR" -compact
|
2004-08-19 12:46:02 +00:00
|
|
|
.It Dv SLEEPQ_CONDVAR
|
|
|
|
A sleep queue used to implement condition variables.
|
2007-03-09 22:41:01 +00:00
|
|
|
.It Dv SLEEPQ_SLEEP
|
2004-08-19 12:46:02 +00:00
|
|
|
A sleep queue used to implement
|
2007-03-09 22:41:01 +00:00
|
|
|
.Xr sleep 9 ,
|
2004-08-19 12:46:02 +00:00
|
|
|
.Xr wakeup 9
|
|
|
|
and
|
|
|
|
.Xr wakeup_one 9 .
|
2007-03-09 18:06:36 +00:00
|
|
|
.It Dv SLEEPQ_PAUSE
|
|
|
|
A sleep queue used to implement
|
|
|
|
.Xr pause 9 .
|
2004-08-19 12:46:02 +00:00
|
|
|
.El
|
|
|
|
.Pp
|
2009-12-12 22:08:37 +00:00
|
|
|
There are currently two optional flag:
|
2005-04-19 16:30:25 +00:00
|
|
|
.Pp
|
|
|
|
.Bl -tag -width ".Dv SLEEPQ_INTERRUPTIBLE" -compact
|
2004-08-19 12:46:02 +00:00
|
|
|
.It Dv SLEEPQ_INTERRUPTIBLE
|
|
|
|
The current thread is entering an interruptible sleep.
|
|
|
|
.El
|
2009-12-12 22:08:37 +00:00
|
|
|
.Bl -tag -width ".Dv SLEEPQ_STOP_ON_BDRY" -compact
|
|
|
|
.It Dv SLEEPQ_STOP_ON_BDRY
|
|
|
|
When thread is entering an interruptible sleep, do not stop it upon
|
|
|
|
arrival of stop action, like
|
|
|
|
.Dv SIGSTOP .
|
|
|
|
Wake it up instead.
|
|
|
|
.El
|
2004-08-19 12:46:02 +00:00
|
|
|
.Pp
|
2004-03-12 19:07:18 +00:00
|
|
|
A timeout on the sleep may be specified by calling
|
|
|
|
.Fn sleepq_set_timeout
|
|
|
|
after
|
|
|
|
.Fn sleepq_add .
|
|
|
|
The
|
|
|
|
.Fa wchan
|
|
|
|
parameter should be the same value from the preceding call to
|
2005-04-19 16:30:25 +00:00
|
|
|
.Fn sleepq_add ,
|
|
|
|
and the sleep queue chain associated with
|
|
|
|
.Fa wchan
|
|
|
|
must have been locked by a prior call to
|
|
|
|
.Fn sleepq_lock .
|
2004-03-12 19:07:18 +00:00
|
|
|
The
|
|
|
|
.Fa timo
|
|
|
|
parameter should specify the timeout value in ticks.
|
2005-04-19 16:30:25 +00:00
|
|
|
.Pp
|
2013-03-04 19:10:39 +00:00
|
|
|
.Fn sleepq_set_timeout_sbt
|
|
|
|
function takes
|
|
|
|
.Fa sbt
|
|
|
|
argument instead of
|
|
|
|
.Fa timo .
|
|
|
|
It allows to specify relative or absolute wakeup time with higher resolution
|
|
|
|
in form of
|
|
|
|
.Vt sbintime_t .
|
|
|
|
The parameter
|
|
|
|
.Fa pr
|
|
|
|
allows to specify wanted absolute event precision.
|
|
|
|
The parameter
|
|
|
|
.Fa flags
|
|
|
|
allows to pass additional
|
|
|
|
.Fn callout_reset_sbt
|
|
|
|
flags.
|
|
|
|
.Pp
|
2004-03-12 19:07:18 +00:00
|
|
|
Once the thread is ready to suspend,
|
2005-04-19 16:30:25 +00:00
|
|
|
one of the wait functions is called to put the current thread to sleep
|
|
|
|
until it is awakened and to context switch to another thread.
|
2004-03-12 19:07:18 +00:00
|
|
|
The
|
|
|
|
.Fn sleepq_wait
|
|
|
|
function is used for non-interruptible sleeps that do not have a timeout.
|
|
|
|
The
|
|
|
|
.Fn sleepq_timedwait
|
|
|
|
function is used for non-interruptible sleeps that have had a timeout set via
|
|
|
|
.Fn sleepq_set_timeout .
|
|
|
|
The
|
|
|
|
.Fn sleepq_wait_sig
|
|
|
|
function is used for interruptible sleeps that do not have a timeout.
|
|
|
|
The
|
|
|
|
.Fn sleepq_timedwait_sig
|
|
|
|
function is used for interruptible sleeps that do have a timeout set.
|
|
|
|
The
|
|
|
|
.Fa wchan
|
2005-04-19 16:30:25 +00:00
|
|
|
argument to all of the wait functions is the wait channel being slept
|
|
|
|
on.
|
|
|
|
The sleep queue chain associated with argument
|
|
|
|
.Fa wchan
|
|
|
|
needs to have been locked with a prior call to
|
|
|
|
.Fn sleepq_lock .
|
2004-03-12 19:07:18 +00:00
|
|
|
The
|
2014-09-22 19:14:27 +00:00
|
|
|
.Fa pri
|
|
|
|
argument is used to set the priority of the thread when it is awakened.
|
|
|
|
If it is set to zero, the thread's priority is left alone.
|
2004-03-12 19:07:18 +00:00
|
|
|
.Pp
|
|
|
|
When the thread is resumed,
|
|
|
|
the wait functions return a non-zero value if the thread was awakened due to
|
|
|
|
an interrupt other than a signal or a timeout.
|
|
|
|
If the sleep timed out, then
|
2004-06-16 15:47:45 +00:00
|
|
|
.Er EWOULDBLOCK
|
2004-03-12 19:07:18 +00:00
|
|
|
is returned.
|
|
|
|
If the sleep was interrupted by something other than a signal,
|
|
|
|
then some other return value will be returned.
|
2001-01-23 19:24:35 +00:00
|
|
|
.Pp
|
2004-03-12 19:07:18 +00:00
|
|
|
A sleeping thread is normally resumed by the
|
|
|
|
.Fn sleepq_broadcast
|
|
|
|
and
|
|
|
|
.Fn sleepq_signal
|
|
|
|
functions.
|
|
|
|
The
|
|
|
|
.Fn sleepq_signal
|
Add wakeup_any(), cheaper wakeup_one() for taskqueue(9).
wakeup_one() and underlying sleepq_signal() spend additional time trying
to be fair, waking thread with highest priority, sleeping longest time.
But in case of taskqueue there are many absolutely identical threads, and
any fairness between them is quite pointless. It makes even worse, since
round-robin wakeups not only make previous CPU affinity in scheduler quite
useless, but also hide from user chance to see CPU bottlenecks, when
sequential workload with one request at a time looks evenly distributed
between multiple threads.
This change adds new SLEEPQ_UNFAIR flag to sleepq_signal(), making it wakeup
thread that went to sleep last, but no longer in context switch (to avoid
immediate spinning on the thread lock). On top of that new wakeup_any()
function is added, equivalent to wakeup_one(), but setting the flag.
On top of that taskqueue(9) is switchied to wakeup_any() to wakeup its
threads.
As result, on 72-core Xeon v4 machine sequential ZFS write to 12 ZVOLs
with 16KB block size spend 34% less time in wakeup_any() and descendants
then it was spending in wakeup_one(), and total write throughput increased
by ~10% with the same as before CPU usage.
Reviewed by: markj, mmacy
MFC after: 2 weeks
Sponsored by: iXsystems, Inc.
Differential Revision: https://reviews.freebsd.org/D20669
2019-06-20 01:15:33 +00:00
|
|
|
function awakens the highest priority thread sleeping on a wait channel
|
|
|
|
(if SLEEPQ_UNFAIR flag is set, thread that went to sleep recently) while
|
2004-03-12 19:07:18 +00:00
|
|
|
.Fn sleepq_broadcast
|
2004-06-21 14:11:45 +00:00
|
|
|
awakens all of the threads sleeping on a wait channel.
|
2004-03-12 19:07:18 +00:00
|
|
|
The
|
|
|
|
.Fa wchan
|
|
|
|
argument specifics which wait channel to awaken.
|
|
|
|
The
|
|
|
|
.Fa flags
|
2004-08-19 12:46:02 +00:00
|
|
|
argument must match the sleep queue type contained in the
|
2004-03-12 19:07:18 +00:00
|
|
|
.Fa flags
|
|
|
|
argument passed to
|
|
|
|
.Fn sleepq_add
|
|
|
|
by the threads sleeping on the wait channel.
|
|
|
|
If the
|
|
|
|
.Fa pri
|
2004-06-16 15:47:45 +00:00
|
|
|
argument does not equal \-1,
|
2004-03-12 19:07:18 +00:00
|
|
|
then each thread that is awakened will have its priority raised to
|
|
|
|
.Fa pri
|
|
|
|
if it has a lower priority.
|
2005-04-19 16:30:25 +00:00
|
|
|
The sleep queue chain associated with argument
|
|
|
|
.Fa wchan
|
|
|
|
must be locked by a prior call to
|
|
|
|
.Fn sleepq_lock
|
|
|
|
before calling any of these functions.
|
2007-09-28 11:13:40 +00:00
|
|
|
The
|
|
|
|
.Fa queue
|
|
|
|
argument specifies the sub-queue, from which threads need to be woken up.
|
2004-03-12 19:07:18 +00:00
|
|
|
.Pp
|
|
|
|
A thread in an interruptible sleep can be interrupted by another thread via
|
|
|
|
the
|
|
|
|
.Fn sleepq_abort
|
|
|
|
function.
|
|
|
|
The
|
|
|
|
.Fa td
|
|
|
|
argument specifies the thread to interrupt.
|
|
|
|
An individual thread can also be awakened from sleeping on a specific wait
|
|
|
|
channel via the
|
|
|
|
.Fn sleepq_remove
|
|
|
|
function.
|
|
|
|
The
|
2002-01-10 12:55:36 +00:00
|
|
|
.Fa td
|
2004-03-12 19:07:18 +00:00
|
|
|
argument specifies the thread to awaken and the
|
|
|
|
.Fa wchan
|
|
|
|
argument specifies the wait channel to awaken it from.
|
|
|
|
If the thread
|
|
|
|
.Fa td
|
2011-10-16 14:30:28 +00:00
|
|
|
is not blocked on the wait channel
|
2004-03-12 19:07:18 +00:00
|
|
|
.Fa wchan
|
|
|
|
then this function will not do anything,
|
|
|
|
even if the thread is asleep on a different wait channel.
|
|
|
|
This function should only be used if one of the other functions above is not
|
|
|
|
sufficient.
|
|
|
|
One possible use is waking up a specific thread from a widely shared sleep
|
|
|
|
channel.
|
2001-01-23 19:24:35 +00:00
|
|
|
.Pp
|
2008-08-07 20:47:01 +00:00
|
|
|
The
|
In current code, threads performing an interruptible sleep (on both
sxlock, via the sx_{s, x}lock_sig() interface, or plain lockmgr), will
leave the waiters flag on forcing the owner to do a wakeup even when if
the waiter queue is empty.
That operation may lead to a deadlock in the case of doing a fake wakeup
on the "preferred" (based on the wakeup algorithm) queue while the other
queue has real waiters on it, because nobody is going to wakeup the 2nd
queue waiters and they will sleep indefinitively.
A similar bug, is present, for lockmgr in the case the waiters are
sleeping with LK_SLEEPFAIL on. In this case, even if the waiters queue
is not empty, the waiters won't progress after being awake but they will
just fail, still not taking care of the 2nd queue waiters (as instead the
lock owned doing the wakeup would expect).
In order to fix this bug in a cheap way (without adding too much locking
and complicating too much the semantic) add a sleepqueue interface which
does report the actual number of waiters on a specified queue of a
waitchannel (sleepq_sleepcnt()) and use it in order to determine if the
exclusive waiters (or shared waiters) are actually present on the lockmgr
(or sx) before to give them precedence in the wakeup algorithm.
This fix alone, however doesn't solve the LK_SLEEPFAIL bug. In order to
cope with it, add the tracking of how many exclusive LK_SLEEPFAIL waiters
a lockmgr has and if all the waiters on the exclusive waiters queue are
LK_SLEEPFAIL just wake both queues.
The sleepq_sleepcnt() introduction and ABI breakage require
__FreeBSD_version bumping.
Reported by: avg, kib, pho
Reviewed by: kib
Tested by: pho
2009-12-12 21:31:07 +00:00
|
|
|
.Fn sleepq_sleepcnt
|
|
|
|
function offer a simple way to retrieve the number of threads sleeping for
|
|
|
|
the specified
|
|
|
|
.Fa queue ,
|
|
|
|
given a
|
|
|
|
.Fa wchan .
|
|
|
|
.Pp
|
|
|
|
The
|
2010-01-09 01:46:38 +00:00
|
|
|
.Fn sleepq_type
|
|
|
|
function returns the type of
|
|
|
|
.Fa wchan
|
|
|
|
associated to a sleepqueue.
|
|
|
|
.Pp
|
|
|
|
The
|
2008-08-07 20:47:01 +00:00
|
|
|
.Fn sleepq_abort ,
|
|
|
|
.Fn sleepq_broadcast ,
|
|
|
|
and
|
|
|
|
.Fn sleepq_signal
|
|
|
|
functions all return a boolean value.
|
|
|
|
If the return value is true,
|
|
|
|
then at least one thread was resumed that is currently swapped out.
|
|
|
|
The caller is responsible for awakening the scheduler process so that the
|
|
|
|
resumed thread will be swapped back in.
|
|
|
|
This is done by calling the
|
|
|
|
.Fn kick_proc0
|
|
|
|
function after releasing the sleep queue chain lock via a call to
|
|
|
|
.Fn sleepq_release .
|
|
|
|
.Pp
|
2004-03-12 19:07:18 +00:00
|
|
|
The sleep queue interface is currently used to implement the
|
2007-03-09 22:41:01 +00:00
|
|
|
.Xr sleep 9
|
2004-03-12 19:07:18 +00:00
|
|
|
and
|
|
|
|
.Xr condvar 9
|
|
|
|
interfaces.
|
|
|
|
Almost all other code in the kernel should use one of those interfaces rather
|
|
|
|
than manipulating sleep queues directly.
|
2001-01-23 19:24:35 +00:00
|
|
|
.Sh SEE ALSO
|
2004-03-12 19:07:18 +00:00
|
|
|
.Xr condvar 9 ,
|
2001-01-23 19:24:35 +00:00
|
|
|
.Xr runqueue 9 ,
|
2007-03-09 22:41:01 +00:00
|
|
|
.Xr scheduler 9 ,
|
2013-03-04 19:10:39 +00:00
|
|
|
.Xr sleep 9 ,
|
|
|
|
.Xr timeout 9
|