Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 04 Mar 2026 14:21:55 +0000
From:      ShengYi Hung <aokblast@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: 4bac18002ab4 - stable/14 - smp: Use bitwise operation to count cpu number
Message-ID:  <69a84003.227f2.335c0e3a@gitrepo.freebsd.org>

index | next in thread | raw e-mail

The branch stable/14 has been updated by aokblast:

URL: https://cgit.FreeBSD.org/src/commit/?id=4bac18002ab418131a2c0bc41fba089700f1ba85

commit 4bac18002ab418131a2c0bc41fba089700f1ba85
Author:     ShengYi Hung <aokblast@FreeBSD.org>
AuthorDate: 2026-01-03 16:32:50 +0000
Commit:     ShengYi Hung <aokblast@FreeBSD.org>
CommitDate: 2026-03-04 14:21:13 +0000

    smp: Use bitwise operation to count cpu number
    
    Previously, we iterated over all CPUs using CPU_FOREACH and checked
    individual bits to count valid CPUs. Refactor this to use a bitwise AND
    and popcount to count the number of enabled bits directly.
    
    Approved by:    markj (mentor)
    MFC after:      2 weeks
    Differential Revision: https://reviews.freebsd.org/D54474
    
    (cherry picked from commit e387d9438ba0258b88ebe03ef139bc6fd70b5a46)
---
 sys/kern/subr_smp.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/sys/kern/subr_smp.c b/sys/kern/subr_smp.c
index 7cbf83928786..d78e65ee0af3 100644
--- a/sys/kern/subr_smp.c
+++ b/sys/kern/subr_smp.c
@@ -556,7 +556,7 @@ smp_rendezvous_cpus(cpuset_t map,
 	void (* teardown_func)(void *),
 	void *arg)
 {
-	int curcpumap, i, ncpus = 0;
+	int curcpumap, ncpus = 0;
 
 	/* See comments in the !SMP case. */
 	if (!smp_started) {
@@ -577,10 +577,8 @@ smp_rendezvous_cpus(cpuset_t map,
 	 */
 	MPASS(curthread->td_md.md_spinlock_count == 0);
 
-	CPU_FOREACH(i) {
-		if (CPU_ISSET(i, &map))
-			ncpus++;
-	}
+	CPU_AND(&map, &map, &all_cpus);
+	ncpus = CPU_COUNT(&map);
 	if (ncpus == 0)
 		panic("ncpus is 0 with non-zero map");
 


home | help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?69a84003.227f2.335c0e3a>