Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FW: Log a failure to set the CPU affinity as a platform problem #67

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}