@@ -474,12 +474,11 @@ std::string GetSystemName() {
474
474
#endif // Catch-all POSIX block.
475
475
}
476
476
477
- int GetNumCPUs () {
477
+ int GetNumCPUsImpl () {
478
478
#ifdef BENCHMARK_HAS_SYSCTL
479
479
int num_cpu = -1 ;
480
480
if (GetSysctl (" hw.ncpu" , &num_cpu)) return num_cpu;
481
- fprintf (stderr, " Err: %s\n " , strerror (errno));
482
- std::exit (EXIT_FAILURE);
481
+ PrintErrorAndDie (" Err: " , strerror (errno));
483
482
#elif defined(BENCHMARK_OS_WINDOWS)
484
483
SYSTEM_INFO sysinfo;
485
484
// Use memset as opposed to = {} to avoid GCC missing initializer false
@@ -493,8 +492,8 @@ int GetNumCPUs() {
493
492
// Returns -1 in case of a failure.
494
493
long num_cpu = sysconf (_SC_NPROCESSORS_ONLN);
495
494
if (num_cpu < 0 ) {
496
- fprintf (stderr, " sysconf(_SC_NPROCESSORS_ONLN) failed with error: %s \n " ,
497
- strerror (errno));
495
+ PrintErrorAndDie ( " sysconf(_SC_NPROCESSORS_ONLN) failed with error: " ,
496
+ strerror (errno));
498
497
}
499
498
return (int )num_cpu;
500
499
#elif defined(BENCHMARK_OS_QNX)
@@ -510,8 +509,7 @@ int GetNumCPUs() {
510
509
int max_id = -1 ;
511
510
std::ifstream f (" /proc/cpuinfo" );
512
511
if (!f.is_open ()) {
513
- std::cerr << " failed to open /proc/cpuinfo\n " ;
514
- return -1 ;
512
+ PrintErrorAndDie (" Failed to open /proc/cpuinfo" );
515
513
}
516
514
#if defined(__alpha__)
517
515
const std::string Key = " cpus detected" ;
@@ -540,12 +538,10 @@ int GetNumCPUs() {
540
538
}
541
539
}
542
540
if (f.bad ()) {
543
- std::cerr << " Failure reading /proc/cpuinfo\n " ;
544
- return -1 ;
541
+ PrintErrorAndDie (" Failure reading /proc/cpuinfo" );
545
542
}
546
543
if (!f.eof ()) {
547
- std::cerr << " Failed to read to end of /proc/cpuinfo\n " ;
548
- return -1 ;
544
+ PrintErrorAndDie (" Failed to read to end of /proc/cpuinfo" );
549
545
}
550
546
f.close ();
551
547
@@ -559,6 +555,16 @@ int GetNumCPUs() {
559
555
BENCHMARK_UNREACHABLE ();
560
556
}
561
557
558
+ int GetNumCPUs () {
559
+ const int num_cpus = GetNumCPUsImpl ();
560
+ if (num_cpus < 1 ) {
561
+ PrintErrorAndDie (
562
+ " Unable to extract number of CPUs. If your platform uses "
563
+ " /proc/cpuinfo, custom support may need to be added." );
564
+ }
565
+ return num_cpus;
566
+ }
567
+
562
568
class ThreadAffinityGuard final {
563
569
public:
564
570
ThreadAffinityGuard () : reset_affinity(SetAffinity()) {
0 commit comments