From nobody Thu Feb 9 16:02:44 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4PCM8N4Fhyz3pkQp; Thu, 9 Feb 2023 16:02:44 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4PCM8N3XwCz4F1b; Thu, 9 Feb 2023 16:02:44 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1675958564; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=XcU++aCE1Y5dTEZZNjd5cEoZOk1mi3CWFWC+X8dqxH4=; b=Sh0FmdkB0jjusAxicSwgsN4dmPkSt0CJDNHVMxjst4KgJJBUC9UDRNNFsFKdvj6RKuSo8D iGFWgRuglgofyq4WwXhW6Z6TkKmEkBDVcR82d/ic9ACn7a4VU68JnM5jksRxLbBcoKTjD6 S1gmm8aisdPLUHAV2ltsO1+oaHiuWItJ5HwNemGFllmk9ePaCns9mpecoZmdNpx8Dz1g25 mNpE06uDOhgak8qU752Wpkma9mH6kb/ZDFdw/tGJPuNqPCVzLEpm3CbvwNtD2PORUYSs5j B5ETGFTEDaoPuFFk27KgqmmvmZYD+q8S1V31tvYKt5jHjvmjFD8Tqfc2RSBkyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1675958564; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=XcU++aCE1Y5dTEZZNjd5cEoZOk1mi3CWFWC+X8dqxH4=; b=WKjI+ccAvL3jeQ6FYveFvpsplKGY8V9cXLeW0xmgMzqCoeJMz8i7YsuUJLC25vUE37HUIB lDFSPMT3GyQWT2IwMWCnA4oOtQ1jWrQPrzzM/0TDcB+2R4i73b7kg68opXP6KKNItWP2hS JW+WNbjBqIxgEiQi4KkIoUkXbCgmQt6yiuB8/23i1QQMftf7rGEmZNXah7MiMrkByUpdHg mFgRvUYt3dylwabzkMvDz7WfbGq81+LKloRpfk5S/2Hm6/HrLVNr7BtnQ1J+g+7qBGWL8k I6Q3vN1hGIQ71HkzyDKLQMCRKBZ9uigaDvV4L1q3qXKYdPmepCpfTocZMcGxnQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1675958564; a=rsa-sha256; cv=none; b=PeobvDliOqnMVdU0itUUZrsomH5oVISFUddhgQ/vhaS3f+RKfFmpemDlPARXBV/X1K8D3Y i0VNK3PucOJiBxfI3jve7T4vy/c7Z6wNj3FRX080lU63fMOLRRer/JzN2e0iFvSnZ6OQxb frF4rZwEo9WEqZFlNl6PYffn/bxk8TVp6BxK0P+hVdPYo01OrV+3ElI7+U5XQqzMFMH8C5 NnmyNUhqV4umGKvVQ9i9XT/rbZKFtAHuXXhKqcZ15g4igIrPZ08rtkDKcpdck9wz6i6MIo tcv6w4BLhd2B7zlGA6OFNh8H/F+ksT6/6c3WLKRC38YadrlAR45sIX0XrtnheA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4PCM8N2chTzjjj; Thu, 9 Feb 2023 16:02:44 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 319G2ikC094689; Thu, 9 Feb 2023 16:02:44 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 319G2iTV094688; Thu, 9 Feb 2023 16:02:44 GMT (envelope-from git) Date: Thu, 9 Feb 2023 16:02:44 GMT Message-Id: <202302091602.319G2iTV094688@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mitchell Horne Subject: git: 30cd6fd75d46 - main - man9: Add man page for kern_yield() and friends List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mhorne X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 30cd6fd75d4639a468ab5ae2c8041e8cf9b14e08 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by mhorne: URL: https://cgit.FreeBSD.org/src/commit/?id=30cd6fd75d4639a468ab5ae2c8041e8cf9b14e08 commit 30cd6fd75d4639a468ab5ae2c8041e8cf9b14e08 Author: Mitchell Horne AuthorDate: 2023-02-09 15:35:39 +0000 Commit: Mitchell Horne CommitDate: 2023-02-09 15:59:38 +0000 man9: Add man page for kern_yield() and friends The page is fairly simple and will be referenced by mi_switch(9). Provide some usage notes so that the broader implications of how/when to use these functions are understood. Reviewed by: kib, markj MFC after: 1 week Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D38182 --- share/man/man9/Makefile | 3 + share/man/man9/kern_yield.9 | 134 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) diff --git a/share/man/man9/Makefile b/share/man/man9/Makefile index d7d5b10f13d5..b2c141210ab9 100644 --- a/share/man/man9/Makefile +++ b/share/man/man9/Makefile @@ -196,6 +196,7 @@ MAN= accept_filter.9 \ KASSERT.9 \ kern_reboot.9 \ kern_testfrwk.9 \ + kern_yield.9 \ kernacc.9 \ kernel_mount.9 \ khelp.9 \ @@ -1332,6 +1333,8 @@ MLINKS+=intr_event.9 intr_event_add_handler.9 \ intr_event.9 intr_event_handle.9 \ intr_event.9 intr_event_remove_handler.9 \ intr_event.9 intr_priority.9 +MLINKS+=kern_yield.9 maybe_yield.9 \ + kern_yield.9 should_yield.9 MLINKS+=kernacc.9 useracc.9 MLINKS+=kernel_mount.9 free_mntarg.9 \ kernel_mount.9 mount_arg.9 \ diff --git a/share/man/man9/kern_yield.9 b/share/man/man9/kern_yield.9 new file mode 100644 index 000000000000..ba8d64a66b1f --- /dev/null +++ b/share/man/man9/kern_yield.9 @@ -0,0 +1,134 @@ +.\" +.\" Copyright (c) 2023 The FreeBSD Foundation +.\" +.\" This documentation was written by Mitchell Horne under +.\" sponsorship from the FreeBSD Foundation. +.\" +.\" 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 AUTHOR AND CONTRIBUTORS ``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 AUTHOR OR CONTRIBUTORS 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. +.\" +.Dd January 30, 2023 +.Dt KERN_YIELD 9 +.Os +.Sh NAME +.Nm kern_yield , +.Nm maybe_yield , +.Nm should_yield +.Nd "functions for yielding execution of the current thread" +.Sh SYNOPSIS +.Ft void +.Fn kern_yield "int prio" +.Ft void +.Fn maybe_yield +.Ft bool +.Fn should_yield +.Sh DESCRIPTION +The +.Fn kern_yield +function causes the currently running thread to voluntarily, but +unconditionally, surrender its execution to the scheduler. +The +.Va prio +argument specifies the scheduling priority to be assigned before the context +switch, which has an influence on when execution will resume. +Note that the requested priority will take effect until the thread returns to +usermode, after which its base user priority will be restored. +Valid values for +.Va prio +are any of the +.Dv PRI_* +values defined in +.In sys/priority.h , +as well as the following special values: +.Bl -tag -width "PRI_UNCHANGED" +.It Dv PRI_USER +Schedule the thread with its base user priority; the value corresponding to +.Xr setpriority 2 / +.Xr nice 3 . +.It Dv PRI_UNCHANGED +Yield the thread without changing its priority. +.El +.Pp +The +.Fn should_yield +function checks if sufficient time has passed since the thread's last voluntary +context switch that yielding would be a useful service to other threads. +It returns +.Va true +when this is the case. +See +.Sx USAGE NOTES +for an elaboration of what this means. +.Pp +The +.Fn maybe_yield +function is a helper function for the common task of optionally yielding the +processor. +Internally, +.Fn kern_yield "PRI_USER" +will be called if +.Fn should_yield +returns +.Va true . +.Sh USAGE NOTES +Although the kernel supports preemption, this is usually reserved for +high-priority realtime or interrupt threads. +Kernel worker threads and timesharing threads are not guaranteed to preempt +each another. +Thus, threads executing in the kernel are expected to behave cooperatively +with respect to other threads in the system. +The yield functions are mostly intended to be used by threads which perform a +lot of work inside the kernel. +For example: +.Fn maybe_yield +is called by the +.Dv vlnru +process each time it reclaims a vnode. +.Pp +The scheduler aims to identify threads which monopolize the CPU, and will +schedule them with decreased priority. +Threads which regularly yield the processor will be given the chance to run +more often. +The possibly surprising effect of this is that, depending on the disposition of +other threads on the CPU's runqueue, a call to +.Fn kern_yield +does not guarantee that the yielding thread will be taken off the CPU. +.Pp +With the above considerations in mind, it is advised that code written using +.Fn kern_yield +be measured to confirm that its use has a positive effect on relevant +performance or responsiveness metrics. +Switching thread contexts has a non-zero cost, and thus yielding the processor +too eagerly could have a negative impact on performance. +.Pp +Additionally, since yielding is a cooperative action, it is advised that the +yielding thread release any locks that it may be holding, when possible. +Otherwise, threads which have been given the chance to run could end up waiting +on the yielding thread to release the lock, largely defeating the purpose of +the yield. +.Sh SEE ALSO +.Xr setpriority 2 , +.Xr nice 3 , +.Xr mi_switch 9 +.Sh AUTHORS +.An -nosplit +This manual page was written by +.An Mitchell Horne Aq Mt mhorne@FreeBSD.org .