8000 Remove setaffinity of pthread for getaddrinfo · github/ruby@a4890e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit a4890e4

Browse files
mamedjensenius
authored andcommitted
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]
1 parent dff080d commit a4890e4

File tree

2 files changed

+4
-46
lines changed

2 files changed

+4
-46
lines changed

ext/socket/extconf.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,8 +706,6 @@ def %(s) s || self end
706706

707707
have_func("pthread_create")
708708
have_func("pthread_detach")
709-
have_func("pthread_attr_setaffinity_np")
710-
have_func("sched_getcpu")
711709

712710
$VPATH << '$(topdir)' << '$(top_srcdir)'
713711
create_makefile("socket")

ext/socket/raddrinfo.c

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -461,15 +461,15 @@ cancel_getaddrinfo(void *ptr)
461461
}
462462

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

492-
pthread_attr_t attr;
493-
if (pthread_attr_init(&attr) != 0) {
494-
free_getaddrinfo_arg(arg);
495-
return EAI_AGAIN;
496-
}
497-
#if defined(HAVE_PTHREAD_ATTR_SETAFFINITY_NP) && defined(HAVE_SCHED_GETCPU)
498-
cpu_set_t tmp_cpu_set;
499-
CPU_ZERO(&tmp_cpu_set);
500-
int cpu = sched_getcpu();
501-
if (cpu < CPU_SETSIZE) {
502-
CPU_SET(cpu, &tmp_cpu_set);
503-
pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &tmp_cpu_set);
504-
}
505-
#endif
506-
507492
pthread_t th;
508-
if (do_pthread_create(&th, &attr, do_getaddrinfo, arg) != 0) {
493+
if (do_pthread_create(&th, do_getaddrinfo, arg) != 0) {
509494
free_getaddrinfo_arg(arg);
510495
return EAI_AGAIN;
511496
}
512497
pthread_detach(th);
513498

514-
int r;
515-
if ((r = pthread_attr_destroy(&attr)) != 0) {
516-
rb_bug_errno("pthread_attr_destroy", r);
517-
}
518-
519499
rb_thread_call_without_gvl2(wait_getaddrinfo, arg, cancel_getaddrinfo, arg);
520500

521501
int need_free = 0;
@@ -721,33 +701,13 @@ rb_getnameinfo(const struct sockaddr *sa, socklen_t salen,
721701
return EAI_MEMORY;
722702
}
723703

724-
pthread_attr_t attr;
725-
if (pthread_attr_init(&attr) != 0) {
726-
free_getnameinfo_arg(arg);
727-
return EAI_AGAIN;
728-
}
729-
#if defined(HAVE_PTHREAD_ATTR_SETAFFINITY_NP) && defined(HAVE_SCHED_GETCPU)
730-
cpu_set_t tmp_cpu_set;
731-
CPU_ZERO(&tmp_cpu_set);
732-
int cpu = sched_getcpu();
733-
if (cpu < CPU_SETSIZE) {
734-
CPU_SET(cpu, &tmp_cpu_set);
735-
pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &tmp_cpu_set);
736-
}
737-
#endif
738-
739704
pthread_t th;
740-
if (do_pthread_create(&th, &attr, do_getnameinfo, arg) != 0) {
705+
if (do_pthread_create(&th, do_getnameinfo, arg) != 0) {
741706
free_getnameinfo_arg(arg);
742707
return EAI_AGAIN;
743708
}
744709
pthread_detach(th);
745710

746-
int r;
747-
if ((r = pthread_attr_destroy(&attr)) != 0) {
748-
rb_bug_errno("pthread_attr_destroy", r);
749-
}
750-
751711
rb_thread_call_without_gvl2(wait_getnameinfo, arg, cancel_getnameinfo, arg);
752712

753713
int need_free = 0;

0 commit comments

Comments
 (0)
0