Skip to content

Commit

Permalink
FW: Log a failure to set the CPU affinity as a platform problem
Browse files Browse the repository at this point in the history
It's not fatal, but it's bad. I thought about simply refusing to run,
but some tests will deadlock if not all threads get run, in which case
we'd compound the problem.

Removed the perror() calls.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
  • Loading branch information
thiagomacieira committed Mar 22, 2022
1 parent 145e2dc commit ef69bf7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
33 changes: 31 additions & 2 deletions framework/sandstone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,27 @@ static TestrunSelector *test_selector;

static_assert(LogicalProcessorSet::Size >= MAX_THREADS, "CPU_SETSIZE is too small on this platform");

#ifdef _WIN32
// Neither MSVCRT nor UCRT have strerror_r
template <size_t N>
const char *sandstone_strerror(char (&buf)[N], int errnum = errno)
{
strerror_s(buf, N, errnum);
return buf;
}
#else
template <size_t N, typename R = decltype(strerror_r(0, nullptr, 0))>
const char *sandstone_strerror(char (&buf)[N], int errnum = errno)
{
auto x = strerror_r(errnum, buf, N);
if (std::is_pointer_v<R>)
return x; // GNU strerror_r may return a static pointer

// XSI-compliant strerror_r always writes to our buffer
return buf;
}
#endif

static void find_thyself(char *argv0)
{
#ifndef __GLIBC__
Expand Down Expand Up @@ -823,9 +844,18 @@ int test_run_wrapper_function(const struct test *test, int thread_number)
static void *thread_runner(void *arg)
{
uintptr_t thread_number = uintptr_t(arg);
thread_num = thread_number;

// convert from internal Sandstone numbering to the system one
pin_to_logical_processor(LogicalProcessor(cpu_info[thread_number].cpu_number), current_test->id);
LogicalProcessor lp{cpu_info[thread_number].cpu_number};
if (!__builtin_expect(pin_to_logical_processor(lp, current_test->id), true)) {
char buf[256] = {};
log_platform_message(SANDSTONE_LOG_WARNING "Failed to set affinity to CPU %d "
"(logical CPU %d): %s",
unsigned(thread_number), unsigned(lp), sandstone_strerror(buf));
log_warning("Setting CPU affinity failed, content is likely not running on CPU %d (logical CPU %d)",
unsigned(thread_number), unsigned(lp));
}

struct TestRunWrapper {
struct per_thread_data *this_thread;
Expand All @@ -837,7 +867,6 @@ static void *thread_runner(void *arg)
: this_thread(cpu_data_for_thread(thread_number)),
thread_number(thread_number)
{
thread_num = thread_number;
this_thread->inner_loop_count = 0;
}

Expand Down
6 changes: 1 addition & 5 deletions framework/sysdeps/freebsd/cpu_affinity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ bool pin_to_logical_processor(LogicalProcessor n, const char *thread_name)
CPU_ZERO(&cpu_set);
CPU_SET(int(n), &cpu_set);

if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(cpu_set), &cpu_set)) {
perror("cpuset_setaffinity");
return false;
}
return true;
return cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(cpu_set), &cpu_set) == 0;
}

6 changes: 1 addition & 5 deletions framework/sysdeps/linux/cpu_affinity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ bool pin_to_logical_processor(LogicalProcessor n, const char *thread_name)
CPU_ZERO(&cpu_set);
CPU_SET(int(n), &cpu_set);

if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set)) {
perror("sched_setaffinity");
return false;
}
return true;
return sched_setaffinity(0, sizeof(cpu_set), &cpu_set) == 0;
}

0 comments on commit ef69bf7

Please sign in to comment.