summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Maidanski <ivmai@mail.ru>2023-04-25 09:42:58 +0300
committerIvan Maidanski <ivmai@mail.ru>2023-04-25 11:13:42 +0300
commit11dc704f55251f5c53b3dc6f79bec148659bdd88 (patch)
treec5940305692f9d544e8e67065fb1130bd9aa5154
parent4bdf9b9c327673b71344a63e368d8b2386bae6af (diff)
downloadbdwgc-11dc704f55251f5c53b3dc6f79bec148659bdd88.tar.gz
Allow to randomly choose a CPU core if AO ops are emulated with locks
This is needed for test purpose to have all test processes to run on different CPU cores (even if each process occupies just one core). * pthread_support.c [!GC_WIN32_THREADS && BASE_ATOMIC_OPS_EMULATED && SIGNAL_BASED_STOP_WORLD && RANDOM_ONE_CPU_CORE] (GC_thr_init): Define and set cpu_highest_set variable; update cpu_lowest_set based getpid() and cpu_highest_set values (so that to have cpu_lowest_set randomly set between lowest and highest set bits in the mask); add comment.
-rw-r--r--pthread_support.c13
1 files changed, 13 insertions, 0 deletions
diff --git a/pthread_support.c b/pthread_support.c
index 6430f60b..c1232cef 100644
--- a/pthread_support.c
+++ b/pthread_support.c
@@ -1648,6 +1648,9 @@ GC_INNER void GC_thr_init(void)
cpu_set_t mask;
int cpu_set_cnt = 0;
int cpu_lowest_set = 0;
+# ifdef RANDOM_ONE_CPU_CORE
+ int cpu_highest_set = 0;
+# endif
int i = GC_nprocs > 1 ? GC_nprocs : 2; /* check at least 2 cores */
if (sched_getaffinity(0 /* current process */,
@@ -1655,12 +1658,22 @@ GC_INNER void GC_thr_init(void)
ABORT_ARG1("sched_getaffinity failed", ": errno= %d", errno);
while (i-- > 0)
if (CPU_ISSET(i, &mask)) {
+# ifdef RANDOM_ONE_CPU_CORE
+ if (i + 1 != cpu_lowest_set) cpu_highest_set = i;
+# endif
cpu_lowest_set = i;
cpu_set_cnt++;
}
if (0 == cpu_set_cnt)
ABORT("sched_getaffinity returned empty mask");
if (cpu_set_cnt > 1) {
+# ifdef RANDOM_ONE_CPU_CORE
+ if (cpu_lowest_set < cpu_highest_set) {
+ /* Pseudo-randomly adjust the bit to set among valid ones. */
+ cpu_lowest_set += (unsigned)getpid() %
+ (cpu_highest_set - cpu_lowest_set + 1);
+ }
+# endif
CPU_ZERO(&mask);
CPU_SET(cpu_lowest_set, &mask); /* select just one CPU */
if (sched_setaffinity(0, sizeof(mask), &mask) == -1)