Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Remove setaffinity of pthread for getaddrinfo
It looks like `sched_getcpu(3)` returns a strange number on some
(virtual?) environments.

I decided to remove the setaffinity mechanism because the performance
does not appear to degrade on a quick benchmark even if removed.

[Bug #20172]
  • Loading branch information
mame authored and djensenius committed Jan 31, 2024
commit a4890e4b90cd0433ab880363b36ce362c0d538d9
2 changes: 0 additions & 2 deletions ext/socket/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -706,8 +706,6 @@ def %(s) s || self end

have_func("pthread_create")
have_func("pthread_detach")
have_func("pthread_attr_setaffinity_np")
have_func("sched_getcpu")

$VPATH << '$(topdir)' << '$(top_srcdir)'
create_makefile("socket")
Expand Down
48 changes: 4 additions & 44 deletions ext/socket/raddrinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,15 +461,15 @@ cancel_getaddrinfo(void *ptr)
}

static int
do_pthread_create(pthread_t *th, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg)
do_pthread_create(pthread_t *th, void *(*start_routine) (void *), void *arg)
{
int limit = 3, ret;
do {
// It is said that pthread_create may fail spuriously, so we follow the JDK and retry several times.
//
// https://bugs.openjdk.org/browse/JDK-8268605
// https://github.com/openjdk/jdk/commit/e35005d5ce383ddd108096a3079b17cb0bcf76f1
ret = pthread_create(th, attr, start_routine, arg);
ret = pthread_create(th, 0, start_routine, arg);
} while (ret == EAGAIN && limit-- > 0);
return ret;
}
Expand All @@ -489,33 +489,13 @@ rb_getaddrinfo(const char *hostp, const char *portp, const struct addrinfo *hint
return EAI_MEMORY;
}

pthread_attr_t attr;
if (pthread_attr_init(&attr) != 0) {
free_getaddrinfo_arg(arg);
return EAI_AGAIN;
}
#if defined(HAVE_PTHREAD_ATTR_SETAFFINITY_NP) && defined(HAVE_SCHED_GETCPU)
cpu_set_t tmp_cpu_set;
CPU_ZERO(&tmp_cpu_set);
int cpu = sched_getcpu();
if (cpu < CPU_SETSIZE) {
CPU_SET(cpu, &tmp_cpu_set);
pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &tmp_cpu_set);
}
#endif

pthread_t th;
if (do_pthread_create(&th, &attr, do_getaddrinfo, arg) != 0) {
if (do_pthread_create(&th, do_getaddrinfo, arg) != 0) {
free_getaddrinfo_arg(arg);
return EAI_AGAIN;
}
pthread_detach(th);

int r;
if ((r = pthread_attr_destroy(&attr)) != 0) {
rb_bug_errno("pthread_attr_destroy", r);
}

rb_thread_call_without_gvl2(wait_getaddrinfo, arg, cancel_getaddrinfo, arg);

int need_free = 0;
Expand Down Expand Up @@ -721,33 +701,13 @@ rb_getnameinfo(const struct sockaddr *sa, socklen_t salen,
return EAI_MEMORY;
}

pthread_attr_t attr;
if (pthread_attr_init(&attr) != 0) {
free_getnameinfo_arg(arg);
return EAI_AGAIN;
}
#if defined(HAVE_PTHREAD_ATTR_SETAFFINITY_NP) && defined(HAVE_SCHED_GETCPU)
cpu_set_t tmp_cpu_set;
CPU_ZERO(&tmp_cpu_set);
int cpu = sched_getcpu();
if (cpu < CPU_SETSIZE) {
CPU_SET(cpu, &tmp_cpu_set);
pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &tmp_cpu_set);
}
#endif

pthread_t th;
if (do_pthread_create(&th, &attr, do_getnameinfo, arg) != 0) {
if (do_pthread_create(&th, do_getnameinfo, arg) != 0) {
free_getnameinfo_arg(arg);
return EAI_AGAIN;
}
pthread_detach(th);

int r;
if ((r = pthread_attr_destroy(&attr)) != 0) {
rb_bug_errno("pthread_attr_destroy", r);
}

rb_thread_call_without_gvl2(wait_getnameinfo, arg, cancel_getnameinfo, arg);

int need_free = 0;
Expand Down