From nobody Fri Mar 04 03:12:44 2022 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 EF00D19F0753; Fri, 4 Mar 2022 03:12: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 4K8tFm6PjJz3Blm; Fri, 4 Mar 2022 03:12:44 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1646363564; 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=t6ou/2TfIhdT0QRrpWEc3TgjenEnQA7XVrWhd7jvI2Q=; b=gnT46Q2kWXV5pdM/Xmn8nyqVwxmfyy9KnfnQvW/3GGfWt9l1IAjUUTjbHWLai42g02fwWj mspYGR7JmQ/Svtdtae1p+fdz34SAYBbmWHJWCEm7PWe5RLNYkLgabBbQA4sCRaZZJx0Q9+ Q1K8Jf0KGPcuvqkN44JaqqMTpVmSDk8yHBFGmMqWudnLwq7AnJynV8P+Y474ECC8VT8g/8 3luJe2q5SgOrMgpw68+2khlRsKZTKBIrayw7fTklGkbX89/dX5J+scgUYWFAgiu78SKUbt WgHRnH/Je8SWYSYdIHoHG/IrtjyJ8DhyWQQ3aanS+hr/K5r/3TcszeJObWKZ7g== 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 BD17924624; Fri, 4 Mar 2022 03:12: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 2243CiwF036579; Fri, 4 Mar 2022 03:12:44 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2243CicD036578; Fri, 4 Mar 2022 03:12:44 GMT (envelope-from git) Date: Fri, 4 Mar 2022 03:12:44 GMT Message-Id: <202203040312.2243CicD036578@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Alexander Motin Subject: git: 56070dd2e4df - main - Improve timeout precision of pthread_cond_timedwait(). 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: mav X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 56070dd2e4df70e4de2a10c7ace684ceb8b0f0bd Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1646363564; 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=t6ou/2TfIhdT0QRrpWEc3TgjenEnQA7XVrWhd7jvI2Q=; b=SF9LRHvaMpVrITYdhFOZeCrYt5pAjvhmWX1sKnm3jG4PKeaDoG8bBbZJ/vsK8KVaHE6XZv vqueyeF15NNvK2tCVwu63x3nA/mzOCRQRKnn/4V3xaMvDqV9GJFU0S2/8wXF34DGU+aH0T MgdGupsyBgS5BmKkq/s/s7qP08snzzHkfUlXvGuArNkTc1RC2d3aIYtFtU2NSCzc+febJG YlEARAAWVrXnl9f8keSoKZ/vqjLv3bC+P/FfKdc0PVa9qNoddIKzlKvsdcuKi/kDr03cno 7dbicW4DKQB1EJ4Sdp/g6Dnkp6S2FCltGEQPJkHiw0ful6vIqdUIfeJ31xAibw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1646363564; a=rsa-sha256; cv=none; b=BDrk5+wZqmy6Q4m7RHkQo2SfblT7pt8GxzkMsb/4qCXi4V2FVnQzljdbcWcHgudeqvnjjM Y5i2Llqaz03mEpgIIY87IoMl9grFTOBCBvWYKuQZdHSbaGeOB0B0gPZPKnt68ma0r6ZX82 wvUbTxYNBY+9C6/LJfK2vCo0rwrBeUV9skq0r0JDSW0nkWuYQCtRqkb2Pd73q237Ut9wWs RK0VtPl6ewzGcT+i2zQlGeSs2kdNEwDA4iSB2whS8heTVKMQ0dfjEgb+1ufEBCxAmKShro tQMieg5VvAq2cGywjSeazuUjG12xHPT6G89PSMvXDkgM1a5htOdbWLG/PkuJ2Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by mav: URL: https://cgit.FreeBSD.org/src/commit/?id=56070dd2e4df70e4de2a10c7ace684ceb8b0f0bd commit 56070dd2e4df70e4de2a10c7ace684ceb8b0f0bd Author: Alexander Motin AuthorDate: 2022-03-04 03:03:09 +0000 Commit: Alexander Motin CommitDate: 2022-03-04 03:03:09 +0000 Improve timeout precision of pthread_cond_timedwait(). This code was not touched when all other user-space sleep functions were switched to sbintime_t and decoupled from hardclock. When it is possible, convert supplied times into sbinuptime to supply directly to msleep_sbt() with C_ABSOLUTE. This provides the timeout resolution of few microseconds instead of 2 milliseconds, plus avoids few clock reads and conversions. Reviewed by: vangyzen MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D34163 --- sys/kern/kern_umtx.c | 130 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 82 insertions(+), 48 deletions(-) diff --git a/sys/kern/kern_umtx.c b/sys/kern/kern_umtx.c index 97673ed77e11..b864e6fe9e49 100644 --- a/sys/kern/kern_umtx.c +++ b/sys/kern/kern_umtx.c @@ -175,8 +175,6 @@ static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD | CTLFLAG_MPSAFE, 0 static inline void umtx_abs_timeout_init2(struct umtx_abs_timeout *timo, const struct _umtx_time *umtxtime); -static int umtx_abs_timeout_gethz(struct umtx_abs_timeout *timo); -static inline void umtx_abs_timeout_update(struct umtx_abs_timeout *timo); static void umtx_shm_init(void); static void umtxq_sysinit(void *); @@ -683,20 +681,14 @@ umtx_abs_timeout_init(struct umtx_abs_timeout *timo, int clockid, timo->clockid = clockid; if (!absolute) { timo->is_abs_real = false; - umtx_abs_timeout_update(timo); + kern_clock_gettime(curthread, timo->clockid, &timo->cur); timespecadd(&timo->cur, timeout, &timo->end); } else { timo->end = *timeout; timo->is_abs_real = clockid == CLOCK_REALTIME || clockid == CLOCK_REALTIME_FAST || - clockid == CLOCK_REALTIME_PRECISE; - /* - * If is_abs_real, umtxq_sleep will read the clock - * after setting td_rtcgen; otherwise, read it here. - */ - if (!timo->is_abs_real) { - umtx_abs_timeout_update(timo); - } + clockid == CLOCK_REALTIME_PRECISE || + clockid == CLOCK_SECOND; } } @@ -709,22 +701,71 @@ umtx_abs_timeout_init2(struct umtx_abs_timeout *timo, (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout); } -static void -umtx_abs_timeout_update(struct umtx_abs_timeout *timo) -{ - - kern_clock_gettime(curthread, timo->clockid, &timo->cur); -} - static int -umtx_abs_timeout_gethz(struct umtx_abs_timeout *timo) +umtx_abs_timeout_getsbt(struct umtx_abs_timeout *timo, sbintime_t *sbt, + int *flags) { + struct bintime bt, bbt; struct timespec tts; - if (timespeccmp(&timo->end, &timo->cur, <=)) - return (-1); - timespecsub(&timo->end, &timo->cur, &tts); - return (tstohz(&tts)); + switch (timo->clockid) { + + /* Clocks that can be converted into absolute time. */ + case CLOCK_REALTIME: + case CLOCK_REALTIME_PRECISE: + case CLOCK_REALTIME_FAST: + case CLOCK_MONOTONIC: + case CLOCK_MONOTONIC_PRECISE: + case CLOCK_MONOTONIC_FAST: + case CLOCK_UPTIME: + case CLOCK_UPTIME_PRECISE: + case CLOCK_UPTIME_FAST: + case CLOCK_SECOND: + timespec2bintime(&timo->end, &bt); + switch (timo->clockid) { + case CLOCK_REALTIME: + case CLOCK_REALTIME_PRECISE: + case CLOCK_REALTIME_FAST: + case CLOCK_SECOND: + getboottimebin(&bbt); + bintime_sub(&bt, &bbt); + break; + } + if (bt.sec < 0) + return (ETIMEDOUT); + if (bt.sec >= (SBT_MAX >> 32)) { + *sbt = 0; + *flags = 0; + return (0); + } + *sbt = bttosbt(bt); + switch (timo->clockid) { + case CLOCK_REALTIME_FAST: + case CLOCK_MONOTONIC_FAST: + case CLOCK_UPTIME_FAST: + *sbt += tc_tick_sbt; + break; + case CLOCK_SECOND: + *sbt += SBT_1S; + break; + } + *flags = C_ABSOLUTE; + return (0); + + /* Clocks that has to be periodically polled. */ + case CLOCK_VIRTUAL: + case CLOCK_PROF: + case CLOCK_THREAD_CPUTIME_ID: + case CLOCK_PROCESS_CPUTIME_ID: + default: + kern_clock_gettime(curthread, timo->clockid, &timo->cur); + if (timespeccmp(&timo->end, &timo->cur, <=)) + return (ETIMEDOUT); + timespecsub(&timo->end, &timo->cur, &tts); + *sbt = tick_sbt * tstohz(&tts); + *flags = C_HARDCLOCK; + return (0); + } } static uint32_t @@ -746,15 +787,11 @@ umtx_unlock_val(uint32_t flags, bool rb) */ int umtxq_sleep(struct umtx_q *uq, const char *wmesg, - struct umtx_abs_timeout *abstime) + struct umtx_abs_timeout *timo) { struct umtxq_chain *uc; - int error, timo; - - if (abstime != NULL && abstime->is_abs_real) { - curthread->td_rtcgen = atomic_load_acq_int(&rtc_generation); - umtx_abs_timeout_update(abstime); - } + sbintime_t sbt = 0; + int error, flags = 0; uc = umtxq_getchain(&uq->uq_key); UMTXQ_LOCKED_ASSERT(uc); @@ -763,26 +800,22 @@ umtxq_sleep(struct umtx_q *uq, const char *wmesg, error = 0; break; } - if (abstime != NULL) { - timo = umtx_abs_timeout_gethz(abstime); - if (timo < 0) { - error = ETIMEDOUT; - break; - } - } else - timo = 0; - error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo); - if (error == EINTR || error == ERESTART) { - umtxq_lock(&uq->uq_key); - break; - } - if (abstime != NULL) { - if (abstime->is_abs_real) + if (timo != NULL) { + if (timo->is_abs_real) curthread->td_rtcgen = atomic_load_acq_int(&rtc_generation); - umtx_abs_timeout_update(abstime); + error = umtx_abs_timeout_getsbt(timo, &sbt, &flags); + if (error != 0) + break; + } + error = msleep_sbt(uq, &uc->uc_lock, PCATCH, wmesg, + sbt, 0, flags); + if (error == EINTR || error == ERESTART) + break; + if (error == EWOULDBLOCK && (flags & C_ABSOLUTE) != 0) { + error = ETIMEDOUT; + break; } - umtxq_lock(&uq->uq_key); } curthread->td_rtcgen = 0; @@ -3652,7 +3685,8 @@ again: if (error == ERESTART) error = EINTR; if (error == EINTR) { - umtx_abs_timeout_update(&timo); + kern_clock_gettime(curthread, timo.clockid, + &timo.cur); timespecsub(&timo.end, &timo.cur, &timeout->_timeout); }