Skip to content

Commit 3fba335

Browse files
majnemercopybara-github
authored andcommitted
Debugging: Report the CPU we are running on under Darwin
This can be helpful to understand complex post-mortem failures. PiperOrigin-RevId: 715435340 Change-Id: I7e5f6abcbba043055c1b33fbc09701fa8a56984c
1 parent 1b0f252 commit 3fba335

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

absl/base/config.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,23 @@ static_assert(ABSL_INTERNAL_INLINE_NAMESPACE_STR[0] != 'h' ||
405405
#define ABSL_HAVE_SCHED_GETCPU 1
406406
#endif
407407

408+
// ABSL_HAVE_PTHREAD_CPU_NUMBER_NP
409+
//
410+
// Checks whether pthread_cpu_number_np is available.
411+
#ifdef ABSL_HAVE_PTHREAD_CPU_NUMBER_NP
412+
#error ABSL_HAVE_PTHREAD_CPU_NUMBER_NP cannot be directly set
413+
#elif defined(__APPLE__) && defined(__has_include) && \
414+
((defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \
415+
__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 110000) || \
416+
(defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && \
417+
__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 140200) || \
418+
(defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && \
419+
__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ >= 70100) || \
420+
(defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && \
421+
__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ >= 140200))
422+
#define ABSL_HAVE_PTHREAD_CPU_NUMBER_NP 1
423+
#endif
424+
408425
// ABSL_HAVE_SCHED_YIELD
409426
//
410427
// Checks whether the platform implements sched_yield(2) as defined in

absl/debugging/failure_signal_handler.cc

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#ifdef _WIN32
2222
#include <windows.h>
2323
#else
24+
#include <pthread.h>
2425
#include <sched.h>
2526
#include <unistd.h>
2627
#endif
@@ -330,6 +331,20 @@ static void ImmediateAbortSignalHandler(int) { RaiseToDefaultHandler(SIGABRT); }
330331
using GetTidType = decltype(absl::base_internal::GetTID());
331332
ABSL_CONST_INIT static std::atomic<GetTidType> failed_tid(0);
332333

334+
static int GetCpuNumber() {
335+
#ifdef ABSL_HAVE_SCHED_GETCPU
336+
return sched_getcpu();
337+
#elif defined(ABSL_HAVE_PTHREAD_CPU_NUMBER_NP)
338+
size_t cpu_num;
339+
if (pthread_cpu_number_np(&cpu_num) == 0) {
340+
return static_cast<int>(cpu_num);
341+
}
342+
return -1;
343+
#else
344+
return -1;
345+
#endif
346+
}
347+
333348
#ifndef ABSL_HAVE_SIGACTION
334349
static void AbslFailureSignalHandler(int signo) {
335350
void* ucontext = nullptr;
@@ -360,10 +375,7 @@ static void AbslFailureSignalHandler(int signo, siginfo_t*, void* ucontext) {
360375
// Increase the chance that the CPU we report was the same CPU on which the
361376
// signal was received by doing this as early as possible, i.e. after
362377
// verifying that this is not a recursive signal handler invocation.
363-
int my_cpu = -1;
364-
#ifdef ABSL_HAVE_SCHED_GETCPU
365-
my_cpu = sched_getcpu();
366-
#endif
378+
int my_cpu = GetCpuNumber();
367379

368380
#ifdef ABSL_HAVE_ALARM
369381
// Set an alarm to abort the program in case this code hangs or deadlocks.

0 commit comments

Comments
 (0)