@@ -317,8 +317,6 @@ impl Cores {
317317 let tokio = TokioCores {
318318 workers : Some ( tokio_workers) ,
319319 #[ cfg( target_os = "linux" ) ]
320- workers_shared : None ,
321- #[ cfg( target_os = "linux" ) ]
322320 blocking : remaining,
323321 } ;
324322
@@ -352,7 +350,6 @@ impl Cores {
352350 #[ cfg( target_os = "linux" ) ]
353351 let tokio = TokioCores {
354352 workers : None ,
355- workers_shared : Some ( ( shared. len ( ) , shared_cpuset) ) ,
356353 blocking : Some ( shared_cpuset) ,
357354 } ;
358355
@@ -381,7 +378,7 @@ impl Cores {
381378 }
382379
383380 #[ cfg( target_os = "linux" ) ]
384- fn get_core_ids ( ) -> Option < Vec < CoreId > > {
381+ pub fn get_core_ids ( ) -> Option < Vec < CoreId > > {
385382 if cfg ! ( feature = "no-core-pinning" ) {
386383 return None ;
387384 }
@@ -422,11 +419,17 @@ fn cores_to_cpuset(cores: &[CoreId]) -> Option<nix::sched::CpuSet> {
422419 } )
423420}
424421
422+ #[ cfg( target_os = "linux" ) ]
423+ fn cpuset_cardinality ( cpuset : & nix:: sched:: CpuSet ) -> usize {
424+ ( 0 ..nix:: sched:: CpuSet :: count ( ) )
425+ . filter_map ( |cpu| cpuset. is_set ( cpu) . ok ( ) )
426+ . filter ( |is_set| * is_set)
427+ . count ( )
428+ }
429+
425430#[ derive( Default ) ]
426431pub struct TokioCores {
427432 pub workers : Option < Vec < CoreId > > ,
428- #[ cfg( target_os = "linux" ) ]
429- pub workers_shared : Option < ( usize , nix:: sched:: CpuSet ) > ,
430433 // For blocking threads, we don't want to limit them to a specific number
431434 // and pin them to their own cores - they're supposed to run concurrently
432435 // with each other. However, `core_affinity` doesn't support affinity masks,
@@ -463,8 +466,8 @@ impl TokioCores {
463466 } ) ;
464467 } else {
465468 #[ cfg( target_os = "linux" ) ]
466- if let Some ( ( threads , cpuset) ) = self . workers_shared {
467- builder. worker_threads ( threads ) ;
469+ if let Some ( cpuset) = self . blocking {
470+ builder. worker_threads ( cpuset_cardinality ( & cpuset ) ) ;
468471 builder. on_thread_start ( move || {
469472 let this = nix:: unistd:: Pid :: from_raw ( 0 ) ;
470473 let _ = nix:: sched:: sched_setaffinity ( this, & cpuset) ;
@@ -588,7 +591,10 @@ mod tests {
588591 #[ cfg( target_os = "linux" ) ]
589592 {
590593 assert ! ( split. tokio. workers. is_none( ) ) ;
591- assert_eq ! ( split. tokio. workers_shared. as_ref( ) . unwrap( ) . 0 , 20 ) ;
594+ assert_eq ! (
595+ cpuset_cardinality( split. tokio. blocking. as_ref( ) . unwrap( ) ) ,
596+ 20
597+ ) ;
592598 assert ! ( split. rayon. dedicated. is_none( ) ) ;
593599 assert_eq ! ( split. rayon. shared. as_ref( ) . unwrap( ) . 0 , 20 ) ;
594600 }
0 commit comments