From owner-svn-src-all@freebsd.org Sat Nov 14 19:20:58 2020 Return-Path: Delivered-To: svn-src-all@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 EFD204630E0; Sat, 14 Nov 2020 19:20:58 +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 4CYQDB6YJgz3qGM; Sat, 14 Nov 2020 19:20:58 +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 D3F8A70D7; Sat, 14 Nov 2020 19:20:58 +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 0AEJKwKZ052067; Sat, 14 Nov 2020 19:20:58 GMT (envelope-from mjg@FreeBSD.org) Received: (from mjg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 0AEJKwjD052066; Sat, 14 Nov 2020 19:20:58 GMT (envelope-from mjg@FreeBSD.org) Message-Id: <202011141920.0AEJKwjD052066@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mjg set sender to mjg@FreeBSD.org using -f From: Mateusz Guzik Date: Sat, 14 Nov 2020 19:20:58 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r367693 - head/sys/kern X-SVN-Group: head X-SVN-Commit-Author: mjg X-SVN-Commit-Paths: head/sys/kern X-SVN-Commit-Revision: 367693 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 14 Nov 2020 19:20:59 -0000 Author: mjg Date: Sat Nov 14 19:20:58 2020 New Revision: 367693 URL: https://svnweb.freebsd.org/changeset/base/367693 Log: thread: rework tid batch to use helpers Modified: head/sys/kern/kern_thread.c Modified: head/sys/kern/kern_thread.c ============================================================================== --- head/sys/kern/kern_thread.c Sat Nov 14 19:20:37 2020 (r367692) +++ head/sys/kern/kern_thread.c Sat Nov 14 19:20:58 2020 (r367693) @@ -259,6 +259,54 @@ tid_free_batch(lwpid_t *batch, int n) } /* + * Batching for thread reapping. + */ +struct tidbatch { + lwpid_t tab[16]; + int n; +}; + +static void +tidbatch_prep(struct tidbatch *tb) +{ + + tb->n = 0; +} + +static void +tidbatch_add(struct tidbatch *tb, struct thread *td) +{ + + KASSERT(tb->n < nitems(tb->tab), + ("%s: count too high %d", __func__, tb->n)); + tb->tab[tb->n] = td->td_tid; + tb->n++; +} + +static void +tidbatch_process(struct tidbatch *tb) +{ + + KASSERT(tb->n <= nitems(tb->tab), + ("%s: count too high %d", __func__, tb->n)); + if (tb->n == nitems(tb->tab)) { + tid_free_batch(tb->tab, tb->n); + tb->n = 0; + } +} + +static void +tidbatch_final(struct tidbatch *tb) +{ + + KASSERT(tb->n <= nitems(tb->tab), + ("%s: count too high %d", __func__, tb->n)); + if (tb->n != 0) { + tid_free_batch(tb->tab, tb->n); + } +} + +/* * Prepare a thread for use. */ static int @@ -487,8 +535,8 @@ void thread_reap(void) { struct thread *itd, *ntd; - lwpid_t tidbatch[16]; - int tidbatchn; + struct tidbatch tidbatch; + int tdcount; /* * Reading upfront is pessimal if followed by concurrent atomic_swap, @@ -499,24 +547,29 @@ thread_reap(void) itd = (struct thread *)atomic_swap_ptr((uintptr_t *)&thread_zombies, (uintptr_t)NULL); - tidbatchn = 0; + if (itd == NULL) + return; + + tidbatch_prep(&tidbatch); + tdcount = 0; while (itd != NULL) { ntd = itd->td_zombie; - tidbatch[tidbatchn] = itd->td_tid; - tidbatchn++; + EVENTHANDLER_DIRECT_INVOKE(thread_dtor, itd); + tidbatch_add(&tidbatch, itd); thread_cow_free(itd); thread_free_batched(itd); - if (tidbatchn == nitems(tidbatch)) { - tid_free_batch(tidbatch, tidbatchn); - thread_count_sub(tidbatchn); - tidbatchn = 0; + tidbatch_process(&tidbatch); + tdcount++; + if (tdcount == 32) { + thread_count_sub(tdcount); + tdcount = 0; } itd = ntd; } - if (tidbatchn != 0) { - tid_free_batch(tidbatch, tidbatchn); - thread_count_sub(tidbatchn); + tidbatch_final(&tidbatch); + if (tdcount != 0) { + thread_count_sub(tdcount); } } @@ -567,7 +620,6 @@ static void thread_free_batched(struct thread *td) { - EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td); lock_profile_thread_exit(td); if (td->td_cpuset) cpuset_rel(td->td_cpuset); @@ -588,6 +640,7 @@ thread_free(struct thread *td) { lwpid_t tid; + EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td); tid = td->td_tid; thread_free_batched(td); tid_free(tid);