Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 04 Mar 2026 13:58:47 +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: beccc030b4fb - stable/15 - smp: Use bitwise operation to count cpu number
Message-ID:  <69a83a97.21dd0.3c0c081f@gitrepo.freebsd.org>

index | next in thread | raw e-mail

The branch stable/15 has been updated by aokblast:

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

commit beccc030b4fbd411276858ba771b4228f3885568
Author:     ShengYi Hung <aokblast@FreeBSD.org>
AuthorDate: 2026-01-03 16:32:50 +0000
Commit:     ShengYi Hung <aokblast@FreeBSD.org>
CommitDate: 2026-03-04 13:58:23 +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 d80048f961bd..bd4e5c4b5aeb 100644
--- a/sys/kern/subr_smp.c
+++ b/sys/kern/subr_smp.c
@@ -581,7 +581,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) {
@@ -602,10 +602,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?69a83a97.21dd0.3c0c081f>