From owner-svn-src-head@freebsd.org Wed Nov 11 08:51:04 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id EFABD2E1A5D; Wed, 11 Nov 2020 08:51:04 +0000 (UTC) (envelope-from mjg@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 "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4CWJNm6X1Fz4Vrc; Wed, 11 Nov 2020 08:51:04 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id C8B5711D59; Wed, 11 Nov 2020 08:51:04 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 0AB8p47V066193; Wed, 11 Nov 2020 08:51:04 GMT (envelope-from mjg@FreeBSD.org) Received: (from mjg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 0AB8p4aG066192; Wed, 11 Nov 2020 08:51:04 GMT (envelope-from mjg@FreeBSD.org) Message-Id: <202011110851.0AB8p4aG066192@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mjg set sender to mjg@FreeBSD.org using -f From: Mateusz Guzik Date: Wed, 11 Nov 2020 08:51:04 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r367585 - head/sys/kern X-SVN-Group: head X-SVN-Commit-Author: mjg X-SVN-Commit-Paths: head/sys/kern X-SVN-Commit-Revision: 367585 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 Nov 2020 08:51:05 -0000 Author: mjg Date: Wed Nov 11 08:51:04 2020 New Revision: 367585 URL: https://svnweb.freebsd.org/changeset/base/367585 Log: thread: add more fine-grained tidhash locking Note this still does not scale but is enough to move it out of the way for the foreseable future. In particular a trivial benchmark spawning/killing threads stops contesting on tidhash. Modified: head/sys/kern/kern_thread.c Modified: head/sys/kern/kern_thread.c ============================================================================== --- head/sys/kern/kern_thread.c Wed Nov 11 08:50:04 2020 (r367584) +++ head/sys/kern/kern_thread.c Wed Nov 11 08:51:04 2020 (r367585) @@ -149,8 +149,10 @@ static int nthreads; static LIST_HEAD(tidhashhead, thread) *tidhashtbl; static u_long tidhash; -static struct rwlock tidhash_lock; -#define TIDHASH(tid) (&tidhashtbl[(tid) & tidhash]) +static u_long tidhashlock; +static struct rwlock *tidhashtbl_lock; +#define TIDHASH(tid) (&tidhashtbl[(tid) & tidhash]) +#define TIDHASHLOCK(tid) (&tidhashtbl_lock[(tid) & tidhashlock]) EVENTHANDLER_LIST_DEFINE(thread_ctor); EVENTHANDLER_LIST_DEFINE(thread_dtor); @@ -355,6 +357,7 @@ extern int max_threads_per_proc; void threadinit(void) { + u_long i; lwpid_t tid0; uint32_t flags; @@ -395,7 +398,13 @@ threadinit(void) thread_ctor, thread_dtor, thread_init, thread_fini, 32 - 1, flags); tidhashtbl = hashinit(maxproc / 2, M_TIDHASH, &tidhash); - rw_init(&tidhash_lock, "tidhash"); + tidhashlock = (tidhash + 1) / 64; + if (tidhashlock > 0) + tidhashlock--; + tidhashtbl_lock = malloc(sizeof(*tidhashtbl_lock) * (tidhashlock + 1), + M_TIDHASH, M_WAITOK | M_ZERO); + for (i = 0; i < tidhashlock + 1; i++) + rw_init(&tidhashtbl_lock[i], "tidhash"); } /* @@ -1351,7 +1360,7 @@ tdfind_hash(lwpid_t tid, pid_t pid, struct proc **pp, bool locked; run = 0; - rw_rlock(&tidhash_lock); + rw_rlock(TIDHASHLOCK(tid)); locked = true; LIST_FOREACH(td, TIDHASH(tid), td_hash) { if (td->td_tid != tid) { @@ -1364,11 +1373,11 @@ tdfind_hash(lwpid_t tid, pid_t pid, struct proc **pp, break; } if (run > RUN_THRESH) { - if (rw_try_upgrade(&tidhash_lock)) { + if (rw_try_upgrade(TIDHASHLOCK(tid))) { LIST_REMOVE(td, td_hash); LIST_INSERT_HEAD(TIDHASH(td->td_tid), td, td_hash); - rw_wunlock(&tidhash_lock); + rw_wunlock(TIDHASHLOCK(tid)); locked = false; break; } @@ -1376,7 +1385,7 @@ tdfind_hash(lwpid_t tid, pid_t pid, struct proc **pp, break; } if (locked) - rw_runlock(&tidhash_lock); + rw_runlock(TIDHASHLOCK(tid)); if (td == NULL) return (false); *pp = p; @@ -1421,15 +1430,16 @@ tdfind(lwpid_t tid, pid_t pid) void tidhash_add(struct thread *td) { - rw_wlock(&tidhash_lock); + rw_wlock(TIDHASHLOCK(td->td_tid)); LIST_INSERT_HEAD(TIDHASH(td->td_tid), td, td_hash); - rw_wunlock(&tidhash_lock); + rw_wunlock(TIDHASHLOCK(td->td_tid)); } void tidhash_remove(struct thread *td) { - rw_wlock(&tidhash_lock); + + rw_wlock(TIDHASHLOCK(td->td_tid)); LIST_REMOVE(td, td_hash); - rw_wunlock(&tidhash_lock); + rw_wunlock(TIDHASHLOCK(td->td_tid)); }