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

Best Effort Thread Pinning to CPU_ID #1096

Merged
merged 28 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions source/posix/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ int aws_thread_launch(
attr_return = pthread_attr_setaffinity_np(attributes_ptr, sizeof(cpuset), &cpuset);

if (attr_return) {
AWS_LOGF_ERROR(
AWS_LOGF_WARN(
AWS_LS_COMMON_THREAD,
"id=%p: pthread_attr_setaffinity_np() failed with %d.",
"id=%p: pthread_attr_setaffinity_np() failed with %d. Continuing without cpu affinity",
waahm7 marked this conversation as resolved.
Show resolved Hide resolved
(void *)thread,
attr_return);
goto cleanup;
waahm7 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -382,7 +382,20 @@ int aws_thread_launch(

if (attr_return) {
s_thread_wrapper_destroy(wrapper);

if (options && options->cpu_id >= 0) {
/*
* `pthread_create` can fail with an `EINVAL` error or `EDEADLK` on freebasd if the `cpu_id` is
* restricted/invalid. Since the pinning to a particular `cpu_id` is supposed to be best-effort, try to
* launch a thread again without pinning to a specific cpu_id.
*/
AWS_LOGF_INFO(
AWS_LS_COMMON_THREAD,
"id=%p: Attempting to launch the thread again without pinning to a cpu_id",
(void *)thread);
struct aws_thread_options new_options = *options;
new_options.cpu_id = -1;
return aws_thread_launch(thread, func, arg, &new_options);
}
switch (attr_return) {
case EINVAL:
return aws_raise_error(AWS_ERROR_THREAD_INVALID_SETTINGS);
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ add_test_case(aws_load_error_strings_test)
add_test_case(aws_assume_compiles_test)

add_test_case(thread_creation_join_test)
add_test_case(thread_creation_join_invalid_cpu_id_test)
add_test_case(thread_atexit_test)
add_test_case(test_managed_thread_join)
add_test_case(test_managed_thread_join_timeout)
Expand Down
34 changes: 34 additions & 0 deletions tests/thread_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,40 @@ static int s_test_thread_creation_join_fn(struct aws_allocator *allocator, void

AWS_TEST_CASE(thread_creation_join_test, s_test_thread_creation_join_fn)

static int s_test_thread_creation_join_invalid_cpu_id_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
aws_common_library_init(allocator);
struct thread_test_data test_data = {.allocator = allocator};

struct aws_thread thread;
aws_thread_init(&thread, allocator);

struct aws_thread_options thread_options = *aws_default_thread_options();
/* invalid cpu_id. Ensure that the cpu_id is best-effort based. */
thread_options.cpu_id = 4096;

ASSERT_SUCCESS(
aws_thread_launch(&thread, s_thread_fn, (void *)&test_data, &thread_options), "thread creation failed");
ASSERT_INT_EQUALS(
AWS_THREAD_JOINABLE, aws_thread_get_detach_state(&thread), "thread state should have returned JOINABLE");
ASSERT_SUCCESS(aws_thread_join(&thread), "thread join failed");
ASSERT_TRUE(
aws_thread_thread_id_equal(test_data.thread_id, aws_thread_get_id(&thread)),
"get_thread_id should have returned the same id as the thread calling current_thread_id");
ASSERT_INT_EQUALS(
AWS_THREAD_JOIN_COMPLETED,
aws_thread_get_detach_state(&thread),
"thread state should have returned JOIN_COMPLETED");

aws_string_destroy(test_data.thread_name);
aws_thread_clean_up(&thread);
aws_common_library_clean_up();

return 0;
}

AWS_TEST_CASE(thread_creation_join_invalid_cpu_id_test, s_test_thread_creation_join_invalid_cpu_id_fn)

static uint32_t s_atexit_call_count = 0;
static void s_thread_atexit_fn(void *user_data) {
(void)user_data;
Expand Down
Loading