Skip to contentSkip to conversation

pi_test: support more CPU configurations

LTP New Stanislav Kholmanskikh 2 participants 10 messages Dec 18, 2015 – Jan 27, 2016

lore
Share & tools
lore
#0 lore raw

Hi!

It was found that pi_test test cases do not work correctly in environments with a large number of CPUs and/or where some of them are disabled.

This series aims to fix that.

Tested in ldoms, on a T7-4 and a couple x86 desktop machines.

Thanks.

#1 lore raw

PATCH 1/7 pi_test: move duplicated stuff to the header

---
 .../functional/threads/include/pitest.h            |   45 +++++++++++++++++++-
 .../functional/threads/pi_test/pitest-1.c          |   44 +-------------------
 .../functional/threads/pi_test/pitest-2.c          |   43 +------------------
 .../functional/threads/pi_test/pitest-3.c          |   43 +------------------
 .../functional/threads/pi_test/pitest-4.c          |   43 +------------------
 .../functional/threads/pi_test/pitest-5.c          |   43 +------------------
 .../functional/threads/pi_test/pitest-6.c          |   43 +------------------
 7 files changed, 50 insertions(+), 254 deletions(-)
testcases/open_posix_testsuite/functional/threads/include/pitest.h +44 -1
index dd7a48f..89d2ca0 100644
@@ -7,12 +7,41 @@
  *  source tree.
  */
 
-#include <sys/time.h>
+#ifndef __linux__
+#error "Contains Linux-isms that need fixing."
+#endif
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <pthread.h>
+#include <sched.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
+#include <sys/time.h>
+#include <time.h>
+#include <unistd.h>
 #include "test.h"
 
 #define PROTOCOL                PTHREAD_PRIO_INHERIT
 
+struct thread_param {
+	int index;
+	volatile int stop;
+	int sleep_ms;
+	int priority;
+	int policy;
+	const char *name;
+	int cpu;
+	volatile unsigned futex;
+	volatile unsigned should_stall;
+	volatile unsigned progress;
+};
+
+static int cpus;
+static volatile int ts_stop;
+static volatile double base_time;
+
 static inline
 double seconds_read(void)
 {
@@ -83,3 +112,17 @@ int threadattr_init(pthread_attr_t *threadattr)
 	return 0;
 }
 
+static volatile unsigned do_work_dummy;
+static void do_work(unsigned granularity_top, volatile unsigned *progress)
+{
+	unsigned granularity_cnt, i;
+	unsigned top = 5 * 1000 * 1000;
+	unsigned dummy = do_work_dummy;
+
+	for (granularity_cnt = 0; granularity_cnt < granularity_top;
+	     granularity_cnt++) {
+		for (i = 0; i < top; i++)
+			dummy = i | dummy;
+		(*progress)++;
+	}
+}
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c +1 -43

60 lines not on this page · raw message

testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c +2 -43

60 lines not on this page · raw message

testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c +2 -43

61 lines not on this page · raw message

testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c +2 -43

61 lines not on this page · raw message

testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c +1 -42

60 lines not on this page · raw message

testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c +2 -43

60 lines not on this page · raw message

Signature · 1 line
1.7.1
#2 lore raw

PATCH 2/7 pi_test: move thread_fn() to the header

In reply to Stanislav Kholmanskikh

All the test cases, excluding pitest-4, have absolutely identical copies of thread_fn(), so let's move it to the header.

thread_fn() in pitest-4 is a bit different, so on let's just rename it for simplicity.

---
 .../functional/threads/include/pitest.h            |   41 ++++++++++++++++++++
 .../functional/threads/pi_test/pitest-1.c          |   40 -------------------
 .../functional/threads/pi_test/pitest-2.c          |   38 ------------------
 .../functional/threads/pi_test/pitest-3.c          |   41 --------------------
 .../functional/threads/pi_test/pitest-4.c          |   10 ++--
 .../functional/threads/pi_test/pitest-5.c          |   41 --------------------
 .../functional/threads/pi_test/pitest-6.c          |   40 -------------------
 7 files changed, 46 insertions(+), 205 deletions(-)
testcases/open_posix_testsuite/functional/threads/include/pitest.h +41 -0
index 89d2ca0..2bda342 100644
@@ -126,3 +126,44 @@ static void do_work(unsigned granularity_top, volatile unsigned *progress)
 		(*progress)++;
 	}
 }
+
+static void *thread_fn(void *param)
+{
+	struct thread_param *tp = param;
+	struct timespec ts;
+	int rc;
+	unsigned long mask = 1 << tp->cpu;
+
+#if __linux__
+	rc = sched_setaffinity(0, sizeof(mask), &mask);
+	if (rc < 0) {
+		EPRINTF("UNRESOLVED: Thread %s index %d: Can't set affinity: "
+			"%d %s", tp->name, tp->index, rc, strerror(rc));
+		exit(UNRESOLVED);
+	}
+#endif
+	test_set_priority(pthread_self(), tp->policy, tp->priority);
+
+	DPRINTF(stdout, "#EVENT %f %s Thread Started\n",
+		seconds_read() - base_time, tp->name);
+
+	tp->progress = 0;
+	ts.tv_sec = 0;
+	ts.tv_nsec = tp->sleep_ms * 1000 * 1000;
+	while (!tp->stop) {
+		do_work(5, &tp->progress);
+		if (tp->sleep_ms == 0)
+			continue;
+		rc = nanosleep(&ts, NULL);
+		if (rc < 0) {
+			EPRINTF("UNRESOLVED: Thread %s %d: nanosleep returned "
+				"%d %s\n", tp->name, tp->index, rc,
+				strerror(rc));
+			exit(UNRESOLVED);
+		}
+	}
+
+	DPRINTF(stdout, "#EVENT %f %s Thread Stopped\n",
+		seconds_read() - base_time, tp->name);
+	return NULL;
+}
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c +0 -40
index 3db1134..7ef46f6 100644
@@ -49,46 +49,6 @@ struct thread_param tp[] = {
 	7, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
 };
 
-void *thread_fn(void *param)
-{
-	struct thread_param *tp = param;
-	struct timespec ts;
-	int rc;
-	unsigned long mask = 1 << tp->cpu;
-
-#if __linux__
-	rc = sched_setaffinity(0, sizeof(mask), &mask);
-	if (rc < 0) {
-		EPRINTF("UNRESOLVED: Thread %s index %d: Can't set affinity: "
-			"%d %s", tp->name, tp->index, rc, strerror(rc));
-		exit(UNRESOLVED);
-	}
-#endif
-	test_set_priority(pthread_self(), SCHED_FIFO, tp->priority);
-
-	DPRINTF(stdout, "#EVENT %f %s Thread Started\n",
-		seconds_read() - base_time, tp->name);
-	tp->progress = 0;
-	ts.tv_sec = 0;
-	ts.tv_nsec = tp->sleep_ms * 1000 * 1000;
-	while (!tp->stop) {
-		do_work(5, &tp->progress);
-		if (tp->sleep_ms == 0)
-			continue;
-		rc = nanosleep(&ts, NULL);
-		if (rc < 0) {
-			EPRINTF("UNRESOLVED: Thread %s %d: nanosleep returned "
-				"%d %s \n", tp->name, tp->index, rc,
-				strerror(rc));
-			exit(UNRESOLVED);
-		}
-	}
-
-	DPRINTF(stdout, "#EVENT %f %s Thread Stopped\n",
-		seconds_read() - base_time, tp->name);
-	return NULL;
-}
-
 void *thread_tl(void *param)
 {
 	struct thread_param *tp = param;
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c +0 -38
index 0220af8..066a7b8 100644
@@ -52,44 +52,6 @@ struct thread_param tp[] = {
 	8, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
 };
 
-void *thread_fn(void *param)
-{
-	struct thread_param *tp = param;
-	struct timespec ts;
-	int rc;
-	unsigned long mask = 1 << tp->cpu;
-
-#if __linux__
-	rc = sched_setaffinity(0, sizeof(mask), &mask);
-#endif
-	if (rc < 0) {
-		EPRINTF("UNRESOLVED: Thread %s index %d: Can't set affinity: "
-			"%d %s", tp->name, tp->index, rc, strerror(rc));
-		exit(UNRESOLVED);
-	}
-	test_set_priority(pthread_self(), SCHED_FIFO, tp->priority);
-	DPRINTF(stdout, "#EVENT %f Thread %s Started\n",
-		seconds_read() - base_time, tp->name);
-	DPRINTF(stderr, "Thread %s index %d: started \n", tp->name, tp->index);
-	tp->progress = 0;
-	ts.tv_sec = 0;
-	ts.tv_nsec = tp->sleep_ms * 1000 * 1000;
-	while (!tp->stop) {
-		do_work(5, &tp->progress);
-		if (tp->sleep_ms == 0)
-			continue;
-		rc = nanosleep(&ts, NULL);
-		if (rc < 0) {
-			EPRINTF("UNRESOLVED: Thread %s %d: nanosleep returned "
-				"%d %s", tp->name, tp->index, rc, strerror(rc));
-			exit(UNRESOLVED);
-		}
-	}
-	DPRINTF(stdout, "#EVENT %f Thread %s Stopped\n",
-		seconds_read() - base_time, tp->name);
-	return NULL;
-}
-
 void *thread_tl(void *param)
 {
 	struct thread_param *tp = param;
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c +0 -41

47 lines not on this page · raw message

testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c +5 -5
index 75a776b..84ad62c 100644
@@ -53,7 +53,7 @@ struct thread_param tp[] = {
 	7, 0, 0, 2, SCHED_FIFO, "TF", 6, 0, 0, 0}
 };
 
-void *thread_fn(void *param)
+void *thread_fn4(void *param)
 {
 	struct thread_param *tp = param;
 	struct timespec ts;
@@ -68,7 +68,7 @@ void *thread_fn(void *param)
 		exit(UNRESOLVED);
 	}
 #endif
-	test_set_priority(pthread_self(), SCHED_FIFO, tp->priority);
+	test_set_priority(pthread_self(), tp->policy, tp->priority);
 
 	DPRINTF(stdout, "#EVENT %f Thread %s Started\n",
 		seconds_read() - base_time, tp->name);
@@ -227,7 +227,7 @@ int main(int argc, char **argv)
 	/* Start the TF threads */
 	DPRINTF(stderr, "Main Thread: Creating %d TF threads\n", cpus - 1);
 	for (i = 0; i < cpus - 1; i++) {
-		rc = pthread_create(&threads[i], &threadattr, thread_fn,
+		rc = pthread_create(&threads[i], &threadattr, thread_fn4,
 				    &tp[i + 2]);
 		if (rc != 0) {
 			EPRINTF("UNRESOLVED: pthread_create: %d %s",
@@ -239,7 +239,7 @@ int main(int argc, char **argv)
 
 	/* Start TP thread */
 	DPRINTF(stderr, "Main Thread: Creating TP thread\n");
-	rc = pthread_create(&threadtp, &threadattr, thread_fn, &tp[1]);
+	rc = pthread_create(&threadtp, &threadattr, thread_fn4, &tp[1]);
 	if (rc != 0) {
 		EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc));
 		exit(UNRESOLVED);
@@ -248,7 +248,7 @@ int main(int argc, char **argv)
 
 	/* Start TL thread */
 	DPRINTF(stderr, "Main Thread: Creating TL thread\n");
-	rc = pthread_create(&threadtl, &threadattr, thread_fn, &tp[0]);
+	rc = pthread_create(&threadtl, &threadattr, thread_fn4, &tp[0]);
 	if (rc != 0) {
 		EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc));
 		exit(UNRESOLVED);
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c +0 -41

47 lines not on this page · raw message

testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c +0 -40
index bd92613..c6c3046 100644
@@ -49,46 +49,6 @@ struct thread_param tp[] = {
 	7, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
 };
 
-void *thread_fn(void *param)
-{
-	struct thread_param *tp = param;
-	struct timespec ts;
-	int rc;
-	unsigned long mask = 1 << tp->cpu;
-
-#if __linux__
-	rc = sched_setaffinity(0, sizeof(mask), &mask);
-	if (rc < 0) {
-		EPRINTF("UNRESOLVED: Thread %s index %d: Can't set affinity: "
-			"%d %s", tp->name, tp->index, rc, strerror(rc));
-		exit(UNRESOLVED);
-	}
-#endif
-	test_set_priority(pthread_self(), SCHED_FIFO, tp->priority);
-	DPRINTF(stderr, "Thread %s index %d: started\n", tp->name, tp->index);
-	DPRINTF(stdout, "#EVENT %f Thread %s Started\n",
-		seconds_read() - base_time, tp->name);
-
-	tp->progress = 0;
-	ts.tv_sec = 0;
-	ts.tv_nsec = tp->sleep_ms * 1000 * 1000;
-	while (!tp->stop) {
-		do_work(5, &tp->progress);
-		if (tp->sleep_ms == 0)
-			continue;
-		rc = nanosleep(&ts, NULL);
-		if (rc < 0) {
-			EPRINTF("UNRESOLVED: Thread %s %d: nanosleep returned "
-				"%d %s", tp->name, tp->index, rc, strerror(rc));
-			exit(UNRESOLVED);
-		}
-	}
-
-	DPRINTF(stdout, "#EVENT %f Thread %s Started\n",
-		seconds_read() - base_time, tp->name);
-	return NULL;
-}
-
 void *thread_tl(void *param)
 {
 	struct thread_param *tp = param;
Signature · 1 line
1.7.1
#3 lore raw

PATCH 3/7 pi_test: move thread_tl() to the header

In reply to Stanislav Kholmanskikh

thread_tl() in pitest-5 is a bit different, so
just rename it for simplicity.

---
 .../functional/threads/include/pitest.h            |   51 ++++++++++++++++++++
 .../functional/threads/pi_test/pitest-1.c          |   38 ++-------------
 .../functional/threads/pi_test/pitest-2.c          |   38 ++-------------
 .../functional/threads/pi_test/pitest-3.c          |   42 ++--------------
 .../functional/threads/pi_test/pitest-5.c          |    6 +-
 .../functional/threads/pi_test/pitest-6.c          |   38 ++-------------
 6 files changed, 74 insertions(+), 139 deletions(-)
testcases/open_posix_testsuite/functional/threads/include/pitest.h +51 -0
index 2bda342..a23693b 100644
@@ -38,6 +38,12 @@ struct thread_param {
 	volatile unsigned progress;
 };
 
+/* Arguments to thread_tl */
+struct tl_param {
+	struct thread_param *tp;
+	pthread_mutex_t **mutexes; /* NULL-terminated */
+};
+
 static int cpus;
 static volatile int ts_stop;
 static volatile double base_time;
@@ -167,3 +173,48 @@ static void *thread_fn(void *param)
 		seconds_read() - base_time, tp->name);
 	return NULL;
 }
+
+static void *thread_tl(void *param)
+{
+	struct tl_param *tlp = param;
+	struct thread_param *tp = tlp->tp;
+	unsigned long mask = 1 << tp->cpu;
+	pthread_mutex_t **mutex;
+	int rc;
+
+#if __linux__
+	rc = sched_setaffinity((pid_t) 0, sizeof(mask), &mask);
+	if (rc < 0) {
+		EPRINTF
+		    ("UNRESOLVED: Thread %s index %d: Can't set affinity: %d %s",
+		     tp->name, tp->index, rc, strerror(rc));
+		exit(UNRESOLVED);
+	}
+#endif
+	test_set_priority(pthread_self(), tp->policy, tp->priority);
+
+
+	DPRINTF(stdout, "#EVENT %f Thread TL Started\n",
+		seconds_read() - base_time);
+
+	tp->progress = 0;
+
+	mutex = tlp->mutexes;
+	while (*mutex != NULL) {
+		pthread_mutex_lock(*mutex);
+		mutex++;
+	}
+
+	while (!tp->stop)
+		do_work(5, &tp->progress);
+
+	mutex = tlp->mutexes;
+	while (*mutex != NULL) {
+		pthread_mutex_unlock(*mutex);
+		mutex++;
+	}
+
+	DPRINTF(stdout, "#EVENT %f Thread TL Stopped\n",
+		seconds_read() - base_time);
+	return NULL;
+}
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c +5 -33
index 7ef46f6..adcfb0c 100644
@@ -36,6 +36,7 @@
 #include "pitest.h"
 
 pthread_mutex_t mutex;
+pthread_mutex_t *mutexes[] = { &mutex, NULL };
 
 struct thread_param tp[] = {
 	{
@@ -49,37 +50,6 @@ struct thread_param tp[] = {
 	7, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
 };
 
-void *thread_tl(void *param)
-{
-	struct thread_param *tp = param;
-	unsigned long mask = 1 << tp->cpu;
-	int rc;
-
-#if __linux__
-	rc = sched_setaffinity((pid_t) 0, sizeof(mask), &mask);
-#endif
-	test_set_priority(pthread_self(), SCHED_FIFO, tp->priority);
-	if (rc < 0) {
-		EPRINTF
-		    ("UNRESOLVED: Thread %s index %d: Can't set affinity: %d %s",
-		     tp->name, tp->index, rc, strerror(rc));
-		exit(UNRESOLVED);
-	}
-
-	DPRINTF(stdout, "#EVENT %f Thread TL Started\n",
-		seconds_read() - base_time);
-	tp->progress = 0;
-	pthread_mutex_lock(&mutex);
-	while (!tp->stop) {
-		do_work(5, &tp->progress);
-	}
-	pthread_mutex_unlock(&mutex);
-
-	DPRINTF(stdout, "#EVENT %f Thread TL Stopped\n",
-		seconds_read() - base_time);
-	return NULL;
-}
-
 void *thread_sample(void *arg)
 {
 	char buffer[1024];
@@ -151,7 +121,7 @@ int main(int argc, char **argv)
 	pthread_mutexattr_t mutex_attr;
 	pthread_attr_t threadattr;
 	pthread_t threads[cpus - 1], threadsample, threadtp, threadtl, threadtb;
-
+	struct tl_param tlp;
 	int multiplier = 1;
 	int i;
 	int rc;
@@ -200,7 +170,9 @@ int main(int argc, char **argv)
 
 	/* Start TL thread */
 	DPRINTF(stderr, "Main Thread: start TL thread\n");
-	rc = pthread_create(&threadtl, &threadattr, thread_tl, &tp[0]);
+	tlp.tp = tp;
+	tlp.mutexes = mutexes;
+	rc = pthread_create(&threadtl, &threadattr, thread_tl, &tlp);
 	if (rc != 0) {
 		EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc));
 		exit(UNRESOLVED);
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c +5 -33
index 066a7b8..bd3ae84 100644
@@ -38,6 +38,7 @@
 #include "pitest.h"
 
 pthread_mutex_t mutex;
+pthread_mutex_t *mutexes[] = { &mutex, NULL };
 
 struct thread_param tp[] = {
 	{
@@ -52,37 +53,6 @@ struct thread_param tp[] = {
 	8, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
 };
 
-void *thread_tl(void *param)
-{
-	struct thread_param *tp = param;
-	unsigned long mask = 1 << tp->cpu;
-	int rc;
-
-#if __linux__
-	rc = sched_setaffinity((pid_t) 0, sizeof(mask), &mask);
-#endif
-	test_set_priority(pthread_self(), SCHED_FIFO, tp->priority);
-
-	DPRINTF(stdout, "#EVENT %f Thread TL Started\n",
-		seconds_read() - base_time);
-	DPRINTF(stderr, "Thread %s index %d: started\n", tp->name, tp->index);
-	if (rc < 0) {
-		EPRINTF
-		    ("UNRESOLVED: Thread %s index %d: Can't set affinity: %d %s",
-		     tp->name, tp->index, rc, strerror(rc));
-		exit(UNRESOLVED);
-	}
-	tp->progress = 0;
-	pthread_mutex_lock(&mutex);
-	while (!tp->stop) {
-		do_work(5, &tp->progress);
-	}
-	pthread_mutex_unlock(&mutex);
-	DPRINTF(stdout, "#EVENT %f Thread TL Stoped\n",
-		seconds_read() - base_time);
-	return NULL;
-}
-
 void *thread_sample(void *arg)
 {
 	char buffer[1024];
@@ -180,7 +150,7 @@ int main(int argc, char **argv)
 	pthread_attr_t threadattr;
 	pthread_t threads[cpus - 1];
 	pthread_t threadsample, threadtp, threadtl, threadtb1, threadtb2;
-
+	struct tl_param tlp;
 	time_t multiplier = 1;
 	int i;
 	int rc;
@@ -231,7 +201,9 @@ int main(int argc, char **argv)
 
 	/* Start TL thread */
 	DPRINTF(stderr, "Main Thread: Creating TL thread\n");
-	rc = pthread_create(&threadtl, &threadattr, thread_tl, &tp[0]);
+	tlp.tp = tp;
+	tlp.mutexes = mutexes;
+	rc = pthread_create(&threadtl, &threadattr, thread_tl, &tlp);
 	if (rc != 0) {
 		EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc));
 		exit(UNRESOLVED);
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c +5 -37

66 lines not on this page · raw message

testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c +3 -3
index 6cc07b7..44c1772 100644
@@ -51,13 +51,13 @@ struct thread_param tp[] = {
 	7, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
 };
 
-void *thread_tl(void *param)
+void *thread_tl5(void *param)
 {
 	struct thread_param *tp = param;
 	unsigned long mask = 1 << tp->cpu;
 	int rc;
 
-	test_set_priority(pthread_self(), SCHED_FIFO, tp->priority);
+	test_set_priority(pthread_self(), tp->policy, tp->priority);
 #if __linux__
 	rc = sched_setaffinity((pid_t) 0, sizeof(mask), &mask);
 	if (rc < 0) {
@@ -222,7 +222,7 @@ int main(int argc, char **argv)
 
 	/* Start TL thread */
 	DPRINTF(stderr, "Main Thread: Creating TL thread\n");
-	rc = pthread_create(&threadtl, &threadattr, thread_tl, &tp[0]);
+	rc = pthread_create(&threadtl, &threadattr, thread_tl5, &tp[0]);
 	if (rc != 0) {
 		EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc));
 		exit(UNRESOLVED);
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c +5 -33
index c6c3046..e1a56b3 100644
@@ -36,6 +36,7 @@
 #include "pitest.h"
 
 pthread_mutex_t mutex;
+pthread_mutex_t *mutexes[] = { &mutex, NULL };
 
 struct thread_param tp[] = {
 	{
@@ -49,37 +50,6 @@ struct thread_param tp[] = {
 	7, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
 };
 
-void *thread_tl(void *param)
-{
-	struct thread_param *tp = param;
-	unsigned long mask = 1 << tp->cpu;
-	int rc;
-
-#if __linux__
-	rc = sched_setaffinity((pid_t) 0, sizeof(mask), &mask);
-	if (rc < 0) {
-		EPRINTF
-		    ("UNRESOLVED: Thread %s index %d: Can't set affinity: %d %s",
-		     tp->name, tp->index, rc, strerror(rc));
-		exit(UNRESOLVED);
-	}
-#endif
-
-	test_set_priority(pthread_self(), SCHED_FIFO, tp->priority);
-
-	DPRINTF(stderr, "Thread %s index %d: started\n", tp->name, tp->index);
-	DPRINTF(stdout, "#EVENT %f Thread %s Started\n",
-		seconds_read() - base_time, tp->name);
-	tp->progress = 0;
-	pthread_mutex_lock(&mutex);
-	while (!tp->stop) {
-		do_work(5, &tp->progress);
-	}
-	pthread_mutex_unlock(&mutex);
-	DPRINTF(stdout, "#EVENT %f Thread %s Stopted\n",
-		seconds_read() - base_time, tp->name);
-	return NULL;
-}
 
 void *thread_sample(void *arg)
 {
@@ -152,7 +122,7 @@ int main(int argc, char **argv)
 	pthread_mutexattr_t mutex_attr;
 	pthread_attr_t threadattr;
 	pthread_t threads[cpus - 1], threadsample, threadtp, threadtl, threadtb;
-
+	struct tl_param tlp;
 	time_t multiplier = 1;
 	int i;
 	int rc;
@@ -201,7 +171,9 @@ int main(int argc, char **argv)
 
 	/* Start TL thread */
 	DPRINTF(stderr, "Main Thread: Creating TL thread\n");
-	rc = pthread_create(&threadtl, &threadattr, thread_tl, &tp[0]);
+	tlp.tp = tp;
+	tlp.mutexes = mutexes;
+	rc = pthread_create(&threadtl, &threadattr, thread_tl, &tlp);
 	if (rc != 0) {
 		EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc));
 		exit(UNRESOLVED);
Signature · 1 line
1.7.1
#4 lore raw

PATCH 4/7 pi_test: move thread_sample() to the header

In reply to Stanislav Kholmanskikh
---
 .../functional/threads/include/pitest.h            |   52 ++++++++++++++++++++
 .../functional/threads/pi_test/pitest-1.c          |   41 ++-------------
 .../functional/threads/pi_test/pitest-2.c          |   41 ++-------------
 .../functional/threads/pi_test/pitest-3.c          |   44 ++--------------
 .../functional/threads/pi_test/pitest-4.c          |   42 ++--------------
 .../functional/threads/pi_test/pitest-5.c          |   44 ++--------------
 .../functional/threads/pi_test/pitest-6.c          |   42 ++--------------
 7 files changed, 88 insertions(+), 218 deletions(-)
testcases/open_posix_testsuite/functional/threads/include/pitest.h +52 -0
index a23693b..41c8096 100644
@@ -44,6 +44,14 @@ struct tl_param {
 	pthread_mutex_t **mutexes; /* NULL-terminated */
 };
 
+/* Arguments to thread_sample */
+struct sample_param {
+	struct thread_param *tp;
+	int tplen;
+	double period;
+	int priority;
+};
+
 static int cpus;
 static volatile int ts_stop;
 static volatile double base_time;
@@ -218,3 +226,47 @@ static void *thread_tl(void *param)
 		seconds_read() - base_time);
 	return NULL;
 }
+
+static void *thread_sample(void *arg)
+{
+	struct sample_param *sp = arg;
+	struct thread_param *tp = sp->tp;
+	char buffer[1024];
+	struct timespec ts;
+	double period = sp->period;
+	size_t size;
+	int i, j, rc;
+	int tplen = sp->tplen;
+	int priority = sp->priority;
+
+	test_set_priority(pthread_self(), SCHED_FIFO, priority);
+
+	DPRINTF(stderr, "Thread Sampler: started\n");
+	DPRINTF(stdout, "# COLUMNS %d Time ", 1 + tplen);
+	j = 0;
+	for (i = 0; i < tplen; i++) {
+		if (strcmp(tp[i].name, "TF") == 0) {
+			DPRINTF(stdout, "TF%d ", j++);
+		} else {
+			DPRINTF(stdout, "%s ", tp[i].name);
+		}
+	}
+	DPRINTF(stdout, "\n");
+
+	ts.tv_sec = 0;
+	ts.tv_nsec = period * 1000 * 1000;
+
+	while (!ts_stop) {
+		size = snprintf(buffer, 1023, "%f ", seconds_read() - base_time);
+		for (i = 0; i < tplen; i++)
+			size +=
+			    snprintf(buffer + size, 1023 - size, "%u ",
+				     tp[i].progress);
+		DPRINTF(stdout, "%s\n", buffer);
+		rc = nanosleep(&ts, NULL);
+		if (rc < 0)
+			EPRINTF("UNRESOLVED: Thread %s %d: nanosleep returned "
+				"%d %s", tp->name, tp->index, rc, strerror(rc));
+	}
+	return NULL;
+}
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c +6 -35

59 lines not on this page · raw message

testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c +6 -35

59 lines not on this page · raw message

testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c +6 -38

62 lines not on this page · raw message

testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c +6 -36

60 lines not on this page · raw message

testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c +6 -38

62 lines not on this page · raw message

testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c +6 -36

60 lines not on this page · raw message

Signature · 1 line
1.7.1
#5 lore raw

PATCH 5/7 pi_test: thread_sample: do not use the buffer explicitly

In reply to Stanislav Kholmanskikh

This buffer is small to keep the output for a large number of CPUs (100s). We might either allocate it dynamically or do not use it at all.

Removing the buffer is easier, and I'm not sure we really need the buffer at all. Therefore, I removed it.

---
 .../functional/threads/include/pitest.h            |   26 ++++++++++++++-----
 1 files changed, 19 insertions(+), 7 deletions(-)
testcases/open_posix_testsuite/functional/threads/include/pitest.h +19 -7
index 41c8096..8c719e5 100644
@@ -231,16 +231,22 @@ static void *thread_sample(void *arg)
 {
 	struct sample_param *sp = arg;
 	struct thread_param *tp = sp->tp;
-	char buffer[1024];
 	struct timespec ts;
 	double period = sp->period;
-	size_t size;
+	double reltime;
 	int i, j, rc;
 	int tplen = sp->tplen;
 	int priority = sp->priority;
+	unsigned int *progress;
 
 	test_set_priority(pthread_self(), SCHED_FIFO, priority);
 
+	progress = malloc(tplen * sizeof(*progress));
+	if (progress == NULL) {
+		EPRINTF("UNRESOLVED: malloc failed");
+		exit(UNRESOLVED);
+	}
+
 	DPRINTF(stderr, "Thread Sampler: started\n");
 	DPRINTF(stdout, "# COLUMNS %d Time ", 1 + tplen);
 	j = 0;
@@ -257,16 +263,22 @@ static void *thread_sample(void *arg)
 	ts.tv_nsec = period * 1000 * 1000;
 
 	while (!ts_stop) {
-		size = snprintf(buffer, 1023, "%f ", seconds_read() - base_time);
+		reltime = seconds_read() - base_time;
+		for (i = 0; i < tplen; i++)
+			progress[i] = tp[i].progress;
+
+		DPRINTF(stdout, "%f ", reltime);
 		for (i = 0; i < tplen; i++)
-			size +=
-			    snprintf(buffer + size, 1023 - size, "%u ",
-				     tp[i].progress);
-		DPRINTF(stdout, "%s\n", buffer);
+			DPRINTF(stdout, "%u ", progress[i]);
+		DPRINTF(stdout, "\n");
+
 		rc = nanosleep(&ts, NULL);
 		if (rc < 0)
 			EPRINTF("UNRESOLVED: Thread %s %d: nanosleep returned "
 				"%d %s", tp->name, tp->index, rc, strerror(rc));
 	}
+
+	free(progress);
+
 	return NULL;
 }
Signature · 1 line
1.7.1
#6 lore raw

PATCH 6/7 pi_test: remove unused arguments from main()

In reply to Stanislav Kholmanskikh
---
 .../functional/threads/pi_test/pitest-1.c          |    2 +-
 .../functional/threads/pi_test/pitest-2.c          |    2 +-
 .../functional/threads/pi_test/pitest-3.c          |    2 +-
 .../functional/threads/pi_test/pitest-4.c          |    2 +-
 .../functional/threads/pi_test/pitest-5.c          |    2 +-
 .../functional/threads/pi_test/pitest-6.c          |    2 +-
 6 files changed, 6 insertions(+), 6 deletions(-)
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c +1 -1
index 2c65ad9..3b4b360 100644
@@ -82,7 +82,7 @@ void *thread_tb(void *arg)
 	    return NULL;
 }
 
-int main(int argc, char **argv)
+int main(void)
 {
 	pthread_mutexattr_t mutex_attr;
 	pthread_attr_t threadattr;
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c +1 -1
index e1bbe43..76716f4 100644
@@ -110,7 +110,7 @@ void *thread_tb2(void *arg)
 	return NULL;
 }
 
-int main(int argc, char **argv)
+int main(void)
 {
 	pthread_mutexattr_t mutex_attr;
 	pthread_attr_t threadattr;
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c +1 -1
index 809fbb8..17cc43d 100644
@@ -111,7 +111,7 @@ void *thread_tb2(void *arg)
 	return NULL;
 }
 
-int main(int argc, char **argv)
+int main(void)
 {
 	pthread_mutexattr_t mutex_attr;
 	pthread_attr_t threadattr;
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c +1 -1
index 13c34ef..84f9878 100644
@@ -159,7 +159,7 @@ void *thread_tb2(void *arg)
 	return NULL;
 }
 
-int main(int argc, char **argv)
+int main(void)
 {
 	pthread_mutexattr_t mutex_attr;
 	pthread_attr_t threadattr;
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c +1 -1
index d180e1a..3ba3a94 100644
@@ -133,7 +133,7 @@ void *thread_tb(void *arg)
 	return NULL;
 }
 
-int main(int argc, char **argv)
+int main(void)
 {
 	pthread_mutexattr_t mutex_attr;
 	pthread_attr_t threadattr;
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c +1 -1
index 1789a61..46b36f7 100644
@@ -81,7 +81,7 @@ void *thread_tb(void *arg)
 	return NULL;
 }
 
-int main(int argc, char **argv)
+int main(void)
 {
 	cpus = sysconf(_SC_NPROCESSORS_ONLN);
 	pthread_mutexattr_t mutex_attr;
Signature · 1 line
1.7.1
#7 lore raw

PATCH 7/7 pi_test: use only available CPUs

In reply to Stanislav Kholmanskikh

The current code expects that cpu0..cpu7 are online and breaks if the system has more CPUs and/or some of them are offline.

This patch removes these expectations. Now we determine the set of available CPUs at runtime and bind each thread only to a CPU in this set.

---
 .../functional/threads/include/pitest.h            |  176 +++++++++++++++++---
 .../functional/threads/pi_test/pitest-1.c          |   31 ++--
 .../functional/threads/pi_test/pitest-2.c          |   30 ++--
 .../functional/threads/pi_test/pitest-3.c          |   30 ++--
 .../functional/threads/pi_test/pitest-4.c          |   40 ++---
 .../functional/threads/pi_test/pitest-5.c          |   42 ++---
 .../functional/threads/pi_test/pitest-6.c          |   32 ++--
 7 files changed, 244 insertions(+), 137 deletions(-)
testcases/open_posix_testsuite/functional/threads/include/pitest.h +155 -21
index 8c719e5..0ad2c7f 100644
@@ -25,6 +25,31 @@
 
 #define PROTOCOL                PTHREAD_PRIO_INHERIT
 
+#ifndef CPU_ALLOC
+#define CPU_ALLOC(ncpus) malloc(sizeof(cpu_set_t)); \
+if (ncpus > CPU_SETSIZE) { \
+	EPRINTF("UNRESOLVED: Your libc does not support masks with %ld cpus", \
+		ncpus); \
+	exit(UNRESOLVED); \
+}
+#endif
+
+#ifndef CPU_FREE
+#define CPU_FREE(ptr) free(ptr)
+#endif
+
+#ifndef CPU_ALLOC_SIZE
+#define CPU_ALLOC_SIZE(size) sizeof(cpu_set_t)
+#endif
+
+#ifndef CPU_ZERO_S
+#define CPU_ZERO_S(size, mask) CPU_ZERO(mask)
+#endif
+
+#ifndef CPU_SET_S
+#define CPU_SET_S(cpu, size, mask) CPU_SET(cpu, mask)
+#endif
+
 struct thread_param {
 	int index;
 	volatile int stop;
@@ -52,7 +77,6 @@ struct sample_param {
 	int priority;
 };
 
-static int cpus;
 static volatile int ts_stop;
 static volatile double base_time;
 
@@ -141,21 +165,141 @@ static void do_work(unsigned granularity_top, volatile unsigned *progress)
 	}
 }
 
+static void *test_calloc(size_t nmemb, size_t size)
+{
+	void *r;
+
+	r = calloc(nmemb, size);
+	if (r == NULL) {
+		EPRINTF("UNRESOLVED: calloc failed");
+		exit(UNRESOLVED);
+	}
+
+	return r;
+}
+
+static int first_available_cpu(int ncpus_max, size_t mask_size, cpu_set_t *mask)
+{
+	int i;
+
+	for (i = 0; i < ncpus_max; i++) {
+		if (CPU_ISSET_S(i, mask_size, mask))
+			return i;
+	}
+	return -1;
+}
+
+/*
+ * Allocates a new array of 'struct thread_param' with
+ * the number of TF entries increased to 'online CPUs - 1'.
+ * All non-TF entries are bind to the first available online CPU,
+ * each TF-entry is bind to its own online CPU.
+ */
+static void init_tparam(struct thread_param *tpbase, int len,
+		  struct thread_param **tpnew, int *newlen)
+{
+	struct thread_param *tp;
+	cpu_set_t *mask;
+	size_t mask_size;
+	int i, cpu, ncpus_max, ncpus_online;
+
+	ncpus_max = sysconf(_SC_NPROCESSORS_CONF);
+	ncpus_online = sysconf(_SC_NPROCESSORS_ONLN);
+
+	if (ncpus_online < 2) {
+		EPRINTF("UNRESOLVED: we need@least %d online CPUs, got %d",
+			2, ncpus_online);
+		exit(UNRESOLVED);
+	}
+
+	*newlen = len - 1 + (ncpus_online - 1);
+
+	mask_size = CPU_ALLOC_SIZE(ncpus_max);
+	mask = CPU_ALLOC(ncpus_max);
+	if (mask == NULL) {
+		EPRINTF("UNRESOLVED: failed to allocate a CPU mask");
+		exit(UNRESOLVED);
+	}
+
+	if (sched_getaffinity(0, mask_size, mask)) {
+		EPRINTF("UNRESOLVED: sched_getaffinity: %s", strerror(errno));
+		CPU_FREE(mask);
+		exit(UNRESOLVED);
+	}
+
+	/* This is the CPU where all non-TF threads will be running */
+	cpu = first_available_cpu(ncpus_max, mask_size, mask);
+	if (cpu == -1) {
+		EPRINTF("UNRESOLVED: first_available_cpu failed");
+		CPU_FREE(mask);
+		exit(UNRESOLVED);
+	}
+	CPU_CLR_S(cpu, mask_size, mask);
+
+	tp = calloc(*newlen, sizeof(struct thread_param));
+
+	for (i = 0; i < len - 1; i++) {
+		tp[i] = tpbase[i];
+		tp[i].cpu = cpu;
+	}
+	for (i = len - 1; i < *newlen; i++) {
+		tp[i] = tpbase[len - 1];
+
+		cpu = first_available_cpu(ncpus_max, mask_size, mask);
+		if (cpu == -1) {
+			EPRINTF("UNRESOLVED: first_available_cpu failed");
+			CPU_FREE(mask);
+			exit(UNRESOLVED);
+		}
+		tp[i].cpu = cpu;
+		CPU_CLR_S(cpu, mask_size, mask);
+	}
+
+	CPU_FREE(mask);
+
+	*tpnew = tp;
+}
+
+static void test_set_affinity(pid_t pid, int cpu)
+{
+	cpu_set_t *mask;
+	size_t mask_size;
+	int ncpus_max, ncpus_online;
+
+	ncpus_max = sysconf(_SC_NPROCESSORS_CONF);
+	ncpus_online = sysconf(_SC_NPROCESSORS_ONLN);
+
+	if (cpu >= ncpus_max) {
+		EPRINTF("UNRESOLVED: invalid CPU specified: %d", cpu);
+		exit(UNRESOLVED);
+	}
+
+	mask_size = CPU_ALLOC_SIZE(ncpus_max);
+	mask = CPU_ALLOC(ncpus_max);
+	if (mask == NULL) {
+		EPRINTF("UNRESOLVED: failed to allocate a CPU mask");
+		exit(UNRESOLVED);
+	}
+
+	CPU_ZERO_S(mask_size, mask);
+	CPU_SET_S(cpu, mask_size, mask);
+
+	if (sched_setaffinity(pid, mask_size, mask)) {
+		EPRINTF("UNRESOLVED: pid %d: sched_setaffinity failed: %s",
+			pid, strerror(errno));
+		CPU_FREE(mask);
+		exit(UNRESOLVED);
+	}
+	CPU_FREE(mask);
+}
+
 static void *thread_fn(void *param)
 {
 	struct thread_param *tp = param;
 	struct timespec ts;
 	int rc;
-	unsigned long mask = 1 << tp->cpu;
 
-#if __linux__
-	rc = sched_setaffinity(0, sizeof(mask), &mask);
-	if (rc < 0) {
-		EPRINTF("UNRESOLVED: Thread %s index %d: Can't set affinity: "
-			"%d %s", tp->name, tp->index, rc, strerror(rc));
-		exit(UNRESOLVED);
-	}
-#endif
+	test_set_affinity(0, tp->cpu);
 	test_set_priority(pthread_self(), tp->policy, tp->priority);
 
 	DPRINTF(stdout, "#EVENT %f %s Thread Started\n",
@@ -186,19 +330,9 @@ static void *thread_tl(void *param)
 {
 	struct tl_param *tlp = param;
 	struct thread_param *tp = tlp->tp;
-	unsigned long mask = 1 << tp->cpu;
 	pthread_mutex_t **mutex;
-	int rc;
 
-#if __linux__
-	rc = sched_setaffinity((pid_t) 0, sizeof(mask), &mask);
-	if (rc < 0) {
-		EPRINTF
-		    ("UNRESOLVED: Thread %s index %d: Can't set affinity: %d %s",
-		     tp->name, tp->index, rc, strerror(rc));
-		exit(UNRESOLVED);
-	}
-#endif
+	test_set_affinity(0, tp->cpu);
 	test_set_priority(pthread_self(), tp->policy, tp->priority);
 
 
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c +15 -16
index 3b4b360..2b8a372 100644
@@ -38,17 +38,13 @@
 pthread_mutex_t mutex;
 pthread_mutex_t *mutexes[] = { &mutex, NULL };
 
-struct thread_param tp[] = {
+struct thread_param tpbase[] = {
 	{
 	0, 0, 0, 1, SCHED_FIFO, "TL", 0, 0, 0, 0}, {
 	1, 0, 50, 2, SCHED_FIFO, "TP", 0, 0, 0, 0}, {
-	2, 0, 0, 3, SCHED_FIFO, "TF", 1, 0, 0, 0}, {
-	3, 0, 0, 3, SCHED_FIFO, "TF", 2, 0, 0, 0}, {
-	4, 0, 0, 3, SCHED_FIFO, "TF", 3, 0, 0, 0}, {
-	5, 0, 0, 3, SCHED_FIFO, "TF", 4, 0, 0, 0}, {
-	6, 0, 0, 3, SCHED_FIFO, "TF", 5, 0, 0, 0}, {
-	7, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
+	2, 0, 0, 3, SCHED_FIFO, "TF", 1, 0, 0, 0},
 };
+struct thread_param *tp;
 
 void *thread_tb(void *arg)
 {
@@ -86,15 +82,19 @@ int main(void)
 {
 	pthread_mutexattr_t mutex_attr;
 	pthread_attr_t threadattr;
-	pthread_t threads[cpus - 1], threadsample, threadtp, threadtl, threadtb;
+	pthread_t *threads;
+	pthread_t threadsample, threadtp, threadtl, threadtb;
 	struct tl_param tlp;
 	struct sample_param sp;
 	int multiplier = 1;
-	int i;
-	int rc;
+	int i, rc, tplen;
 
 	test_set_priority(pthread_self(), SCHED_FIFO, 6);
-	cpus = sysconf(_SC_NPROCESSORS_ONLN);
+
+	init_tparam(tpbase, 3, &tp, &tplen);
+
+	threads = test_calloc(tplen - 2, sizeof(pthread_t));
+
 	base_time = seconds_read();
 
 	/* Initialize a mutex with PTHREAD_PRIO_INHERIT protocol */
@@ -107,7 +107,7 @@ int main(void)
 	/* Start the sample thread */
 	DPRINTF(stderr, "Main Thread: start sample thread \n");
 	sp.tp = tp;
-	sp.tplen = 2 + cpus - 1;
+	sp.tplen = tplen;
 	sp.period = 250;
 	sp.priority = 5;
 	rc = pthread_create(&threadsample, &threadattr, thread_sample, &sp);
@@ -117,8 +117,8 @@ int main(void)
 	}
 
 	/* Start the TF threads */
-	DPRINTF(stderr, "Main Thread: start %d TF thread\n", cpus - 1);
-	for (i = 0; i < cpus - 1; i++) {
+	DPRINTF(stderr, "Main Thread: start %d TF thread\n", tplen - 2);
+	for (i = 0; i < tplen - 2; i++) {
 		rc = pthread_create(&threads[i], &threadattr, thread_fn,
 				    &tp[i + 2]);
 		if (rc != 0) {
@@ -173,9 +173,8 @@ int main(void)
 
 	/* Stop TF threads */
 	DPRINTF(stderr, "Main Thread: stop TF threads\n");
-	for (i = 2; i < cpus - 1; i++) {
+	for (i = 2; i < tplen; i++)
 		tp[i].stop = 1;
-	}
 
 	/* Stop sampler */
 	ts_stop = 1;
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c +14 -16
index 76716f4..d4c9852 100644
@@ -40,18 +40,14 @@
 pthread_mutex_t mutex;
 pthread_mutex_t *mutexes[] = { &mutex, NULL };
 
-struct thread_param tp[] = {
+struct thread_param tpbase[] = {
 	{
 	0, 0, 0, 1, SCHED_FIFO, "TL", 0, 0, 0, 0}, {
 	1, 0, 500, 2, SCHED_FIFO, "TP1", 0, 0, 0, 0}, {
 	2, 0, 500, 5, SCHED_FIFO, "TP2", 0, 0, 0, 0}, {
-	3, 0, 0, 3, SCHED_FIFO, "TF", 1, 0, 0, 0}, {
-	4, 0, 0, 3, SCHED_FIFO, "TF", 2, 0, 0, 0}, {
-	5, 0, 0, 3, SCHED_FIFO, "TF", 3, 0, 0, 0}, {
-	6, 0, 0, 3, SCHED_FIFO, "TF", 4, 0, 0, 0}, {
-	7, 0, 0, 3, SCHED_FIFO, "TF", 5, 0, 0, 0}, {
-	8, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
+	3, 0, 0, 3, SCHED_FIFO, "TF", 1, 0, 0, 0}
 };
+struct thread_param *tp;
 
 void *thread_tb1(void *arg)
 {
@@ -114,16 +110,19 @@ int main(void)
 {
 	pthread_mutexattr_t mutex_attr;
 	pthread_attr_t threadattr;
-	pthread_t threads[cpus - 1];
+	pthread_t *threads;
 	pthread_t threadsample, threadtp, threadtl, threadtb1, threadtb2;
 	struct tl_param tlp;
 	struct sample_param sp;
 	time_t multiplier = 1;
-	int i;
-	int rc;
+	int i, rc, tplen;
 
 	test_set_priority(pthread_self(), SCHED_FIFO, 8);
-	cpus = sysconf(_SC_NPROCESSORS_ONLN);
+
+	init_tparam(tpbase, 4, &tp, &tplen);
+
+	threads = test_calloc(tplen - 3, sizeof(pthread_t));
+
 	base_time = seconds_read();
 
 	/* Initialize a mutex with PTHREAD_PRIO_INHERIT protocol */
@@ -136,7 +135,7 @@ int main(void)
 	/* Start the sample thread */
 	DPRINTF(stderr, "Main Thread: Creating sample thread \n");
 	sp.tp = tp;
-	sp.tplen = 3 + cpus - 1;
+	sp.tplen = tplen;
 	sp.period = 300;
 	sp.priority = 7;
 	rc = pthread_create(&threadsample, &threadattr, thread_sample, &sp);
@@ -146,8 +145,8 @@ int main(void)
 	}
 
 	/* Start the TF threads */
-	DPRINTF(stderr, "Main Thread: Creating %d TF threads \n", cpus - 1);
-	for (i = 0; i < cpus - 1; i++) {
+	DPRINTF(stderr, "Main Thread: Creating %d TF threads\n", tplen - 3);
+	for (i = 0; i < tplen - 3; i++) {
 		rc = pthread_create(&threads[i], &threadattr, thread_fn,
 				    &tp[i + 3]);
 		if (rc != 0) {
@@ -210,9 +209,8 @@ int main(void)
 	sleep(base_time + multiplier * 120 - seconds_read());
 
 	/* Stop TF threads */
-	for (i = 2; i < cpus - 1; i++) {
+	for (i = 3; i < tplen; i++)
 		tp[i].stop = 1;
-	}
 
 	/* Stop sampler */
 	ts_stop = 1;
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c +14 -16
index 17cc43d..50912a5 100644
@@ -41,18 +41,14 @@ pthread_mutex_t mutex1;
 pthread_mutex_t mutex2;
 pthread_mutex_t *mutexes[] = { &mutex1, &mutex2, NULL };
 
-struct thread_param tp[] = {
+struct thread_param tpbase[] = {
 	{
 	0, 0, 0, 1, SCHED_FIFO, "TL", 0, 0, 0, 0}, {
 	1, 0, 500, 2, SCHED_FIFO, "TP1", 0, 0, 0, 0}, {
 	1, 0, 500, 5, SCHED_FIFO, "TP2", 0, 0, 0, 0}, {
-	2, 0, 0, 3, SCHED_FIFO, "TF", 1, 0, 0, 0}, {
-	3, 0, 0, 3, SCHED_FIFO, "TF", 2, 0, 0, 0}, {
-	4, 0, 0, 3, SCHED_FIFO, "TF", 3, 0, 0, 0}, {
-	5, 0, 0, 3, SCHED_FIFO, "TF", 4, 0, 0, 0}, {
-	6, 0, 0, 3, SCHED_FIFO, "TF", 5, 0, 0, 0}, {
-	7, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
+	2, 0, 0, 3, SCHED_FIFO, "TF", 1, 0, 0, 0}
 };
+struct thread_param *tp;
 
 void *thread_tb1(void *arg)
 {
@@ -115,17 +111,20 @@ int main(void)
 {
 	pthread_mutexattr_t mutex_attr;
 	pthread_attr_t threadattr;
-	pthread_t threads[cpus - 1];
+	pthread_t *threads;
 	pthread_t threadsample, threadtp, threadtl, threadtb1, threadtb2;
 	struct tl_param tlp;
 	struct sample_param sp;
 	time_t multiplier = 1;
-	int i;
-	int rc;
+	int i, rc, tplen;
 
 	test_set_priority(pthread_self(), SCHED_FIFO, 8);
+
+	init_tparam(tpbase, 4, &tp, &tplen);
+
+	threads = test_calloc(tplen - 3, sizeof(pthread_t));
+
 	base_time = seconds_read();
-	cpus = sysconf(_SC_NPROCESSORS_ONLN);
 
 	/* Initialize mutex1, mutex2 with PTHREAD_PRIO_INHERIT protocol */
 	mutex_attr_init(&mutex_attr);
@@ -138,7 +137,7 @@ int main(void)
 	/* Start the sample thread */
 	DPRINTF(stderr, "Main Thread: Creating sample thread\n");
 	sp.tp = tp;
-	sp.tplen = 3 + cpus - 1;
+	sp.tplen = tplen;
 	sp.period = 300;
 	sp.priority = 7;
 	rc = pthread_create(&threadsample, &threadattr, thread_sample, &sp);
@@ -148,8 +147,8 @@ int main(void)
 	}
 
 	/* Start the TF threads */
-	DPRINTF(stderr, "Main Thread: Creating %d TF threads\n", cpus - 1);
-	for (i = 0; i < cpus - 1; i++) {
+	DPRINTF(stderr, "Main Thread: Creating %d TF threads\n", tplen - 3);
+	for (i = 0; i < tplen - 3; i++) {
 		rc = pthread_create(&threads[i], &threadattr, thread_fn,
 				    &tp[i + 3]);
 		if (rc != 0) {
@@ -212,9 +211,8 @@ int main(void)
 	sleep(base_time + multiplier * 120 - seconds_read());
 
 	/* Stop TF threads */
-	for (i = 2; i < cpus - 1; i++) {
+	for (i = 3; i < tplen; i++)
 		tp[i].stop = 1;
-	}
 
 	/* Stop sampler */
 	ts_stop = 1;
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c +15 -25
index 84f9878..7669536 100644
@@ -41,33 +41,21 @@
 pthread_mutex_t mutex1;
 pthread_mutex_t mutex2;
 
-struct thread_param tp[] = {
+struct thread_param tpbase[] = {
 	{
 	0, 0, 0, 1, SCHED_FIFO, "TL", 0, 0, 0, 0}, {
 	1, 0, 100, 4, SCHED_FIFO, "TP", 0, 0, 0, 0}, {
-	2, 0, 0, 2, SCHED_FIFO, "TF", 1, 0, 0, 0}, {
-	3, 0, 0, 2, SCHED_FIFO, "TF", 2, 0, 0, 0}, {
-	4, 0, 0, 2, SCHED_FIFO, "TF", 3, 0, 0, 0}, {
-	5, 0, 0, 2, SCHED_FIFO, "TF", 4, 0, 0, 0}, {
-	6, 0, 0, 2, SCHED_FIFO, "TF", 5, 0, 0, 0}, {
-	7, 0, 0, 2, SCHED_FIFO, "TF", 6, 0, 0, 0}
+	2, 0, 0, 2, SCHED_FIFO, "TF", 1, 0, 0, 0}
 };
+struct thread_param *tp;
 
 void *thread_fn4(void *param)
 {
 	struct thread_param *tp = param;
 	struct timespec ts;
 	int rc;
-	unsigned long mask = 1 << tp->cpu;
 
-#if __linux__
-	rc = sched_setaffinity(0, sizeof(mask), &mask);
-	if (rc < 0) {
-		EPRINTF("UNRESOLVED: Thread %s index %d: Can't set affinity: "
-			"%d %s", tp->name, tp->index, rc, strerror(rc));
-		exit(UNRESOLVED);
-	}
-#endif
+	test_set_affinity(0, tp->cpu);
 	test_set_priority(pthread_self(), tp->policy, tp->priority);
 
 	DPRINTF(stdout, "#EVENT %f Thread %s Started\n",
@@ -163,16 +151,19 @@ int main(void)
 {
 	pthread_mutexattr_t mutex_attr;
 	pthread_attr_t threadattr;
-	pthread_t threads[cpus - 1];
+	pthread_t *threads;
 	pthread_t threadsample, threadtp, threadtl, threadtb1, threadtb2;
 	struct sample_param sp;
 	time_t multiplier = 1;
-	int i;
-	int rc;
+	int i, rc, tplen;
 
 	test_set_priority(pthread_self(), SCHED_FIFO, 8);
+
+	init_tparam(tpbase, 3, &tp, &tplen);
+
+	threads = test_calloc(tplen - 2, sizeof(pthread_t));
+
 	base_time = seconds_read();
-	cpus = sysconf(_SC_NPROCESSORS_ONLN);
 
 	/* Initialize mutex1, mutex2 with PTHREAD_PRIO_INHERIT protocol */
 	mutex_attr_init(&mutex_attr);
@@ -185,7 +176,7 @@ int main(void)
 	/* Start the sample thread */
 	DPRINTF(stderr, "Main Thread: Creating sample thread\n");
 	sp.tp = tp;
-	sp.tplen = 2 + cpus - 1;
+	sp.tplen = tplen;
 	sp.period = 300;
 	sp.priority = 6;
 	rc = pthread_create(&threadsample, &threadattr, thread_sample, &sp);
@@ -195,8 +186,8 @@ int main(void)
 	}
 
 	/* Start the TF threads */
-	DPRINTF(stderr, "Main Thread: Creating %d TF threads\n", cpus - 1);
-	for (i = 0; i < cpus - 1; i++) {
+	DPRINTF(stderr, "Main Thread: Creating %d TF threads\n", tplen - 2);
+	for (i = 0; i < tplen - 2; i++) {
 		rc = pthread_create(&threads[i], &threadattr, thread_fn4,
 				    &tp[i + 2]);
 		if (rc != 0) {
@@ -252,9 +243,8 @@ int main(void)
 	sleep(base_time + multiplier * 95 - seconds_read());
 
 	/* Stop TF threads */
-	for (i = 2; i < cpus - 1; i++) {
+	for (i = 2; i < tplen; i++)
 		tp[i].stop = 1;
-	}
 
 	/* Stop sampler */
 	ts_stop = 1;
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c +16 -26

89 lines not on this page · raw message

testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c +15 -17
index 46b36f7..bbb3010 100644
@@ -38,17 +38,13 @@
 pthread_mutex_t mutex;
 pthread_mutex_t *mutexes[] = { &mutex, NULL };
 
-struct thread_param tp[] = {
+struct thread_param tpbase[] = {
 	{
 	0, 0, 0, 1, SCHED_FIFO, "TL", 0, 0, 0, 0}, {
 	1, 0, 200, 2, SCHED_FIFO, "TP", 0, 0, 0, 0}, {
-	2, 0, 0, 3, SCHED_FIFO, "TF", 1, 0, 0, 0}, {
-	3, 0, 0, 3, SCHED_FIFO, "TF", 2, 0, 0, 0}, {
-	4, 0, 0, 3, SCHED_FIFO, "TF", 3, 0, 0, 0}, {
-	5, 0, 0, 3, SCHED_FIFO, "TF", 4, 0, 0, 0}, {
-	6, 0, 0, 3, SCHED_FIFO, "TF", 5, 0, 0, 0}, {
-	7, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
+	2, 0, 0, 3, SCHED_FIFO, "TF", 1, 0, 0, 0}
 };
+struct thread_param *tp;
 
 void *thread_tb(void *arg)
 {
@@ -83,19 +79,22 @@ void *thread_tb(void *arg)
 
 int main(void)
 {
-	cpus = sysconf(_SC_NPROCESSORS_ONLN);
 	pthread_mutexattr_t mutex_attr;
 	pthread_attr_t threadattr;
-	pthread_t threads[cpus - 1], threadsample, threadtp, threadtl, threadtb;
+	pthread_t *threads;
+	pthread_t threadsample, threadtp, threadtl, threadtb;
 	struct tl_param tlp;
 	struct sample_param sp;
 	time_t multiplier = 1;
-	int i;
-	int rc;
+	int i, rc, tplen;
 
 	test_set_priority(pthread_self(), SCHED_FIFO, 6);
+
+	init_tparam(tpbase, 3, &tp, &tplen);
+
+	threads = test_calloc(tplen - 2, sizeof(pthread_t));
+
 	base_time = seconds_read();
-	cpus = sysconf(_SC_NPROCESSORS_ONLN);
 
 	/* Initialize a mutex with PTHREAD_PRIO_INHERIT protocol */
 	mutex_attr_init(&mutex_attr);
@@ -107,7 +106,7 @@ int main(void)
 	/* Start the sample thread */
 	DPRINTF(stderr, "Main Thread: Creating sample thread\n");
 	sp.tp = tp;
-	sp.tplen = 2 + cpus - 1;
+	sp.tplen = tplen;
 	sp.period = 250;
 	sp.priority = 5;
 	rc = pthread_create(&threadsample, &threadattr, thread_sample, &sp);
@@ -117,8 +116,8 @@ int main(void)
 	}
 
 	/* Start the TF threads */
-	DPRINTF(stderr, "Main Thread: Creating %d TF threads\n", cpus - 1);
-	for (i = 0; i < cpus - 1; i++) {
+	DPRINTF(stderr, "Main Thread: Creating %d TF threads\n", tplen - 2);
+	for (i = 0; i < tplen - 2; i++) {
 		rc = pthread_create(&threads[i], &threadattr, thread_fn,
 				    &tp[i + 2]);
 		if (rc != 0) {
@@ -175,9 +174,8 @@ int main(void)
 	sleep(base_time + multiplier * 80 - seconds_read());
 
 	/* Stop TF threads */
-	for (i = 2; i < cpus - 1; i++) {
+	for (i = 2; i < tplen; i++)
 		tp[i].stop = 1;
-	}
 
 	/* Stop sampler */
 	ts_stop = 1;
Signature · 1 line
1.7.1
#8 lore raw

PATCH v2 7/7 pi_test: use only available CPUs

The current code expects that cpu0..cpu7 are online and breaks if the system has more CPUs and/or some of them are offline.

This patch removes these expectations. Now we determine the set of available CPUs at runtime and bind each thread only to a CPU in this set.

---
Changes since V1:
* It turned out that calling sysconf(_SC_NPROCESSORS_*) from many threads at the same time is not safe. Sometimes it returns odd results. Therefore, I made ncpus_online, ncpus_max global. They are assigned in setup() once and only from the main thread.

* Moved the definition of *tp and tplen into the header. Anyway I it's defined in each *.c file

* Renamed init_tparam() into setup() and changed its arguments.

* Removed test_calloc(), since it's used only in one place.

 .../functional/threads/include/pitest.h            |  161 +++++++++++++++++---
 .../functional/threads/pi_test/pitest-1.c          |   28 ++--
 .../functional/threads/pi_test/pitest-2.c          |   27 ++--
 .../functional/threads/pi_test/pitest-3.c          |   27 ++--
 .../functional/threads/pi_test/pitest-4.c          |   37 ++---
 .../functional/threads/pi_test/pitest-5.c          |   39 ++---
 .../functional/threads/pi_test/pitest-6.c          |   29 ++--
 7 files changed, 211 insertions(+), 137 deletions(-)
testcases/open_posix_testsuite/functional/threads/include/pitest.h +140 -21
index 8c719e5..0dd7d67 100644
@@ -25,6 +25,31 @@
 
 #define PROTOCOL                PTHREAD_PRIO_INHERIT
 
+#ifndef CPU_ALLOC
+#define CPU_ALLOC(ncpus) malloc(sizeof(cpu_set_t)); \
+if (ncpus > CPU_SETSIZE) { \
+	EPRINTF("UNRESOLVED: Your libc does not support masks with %ld cpus", \
+		ncpus); \
+	exit(UNRESOLVED); \
+}
+#endif
+
+#ifndef CPU_FREE
+#define CPU_FREE(ptr) free(ptr)
+#endif
+
+#ifndef CPU_ALLOC_SIZE
+#define CPU_ALLOC_SIZE(size) sizeof(cpu_set_t)
+#endif
+
+#ifndef CPU_ZERO_S
+#define CPU_ZERO_S(size, mask) CPU_ZERO(mask)
+#endif
+
+#ifndef CPU_SET_S
+#define CPU_SET_S(cpu, size, mask) CPU_SET(cpu, mask)
+#endif
+
 struct thread_param {
 	int index;
 	volatile int stop;
@@ -52,9 +77,11 @@ struct sample_param {
 	int priority;
 };
 
-static int cpus;
 static volatile int ts_stop;
 static volatile double base_time;
+static int ncpus_max, ncpus_online;
+static struct thread_param *tp;
+static int tplen;
 
 static inline
 double seconds_read(void)
@@ -141,21 +168,123 @@ static void do_work(unsigned granularity_top, volatile unsigned *progress)
 	}
 }
 
+static int first_available_cpu(int ncpus_max, size_t mask_size, cpu_set_t *mask)
+{
+	int i;
+
+	for (i = 0; i < ncpus_max; i++) {
+		if (CPU_ISSET_S(i, mask_size, mask))
+			return i;
+	}
+	return -1;
+}
+
+static void setup(struct thread_param *tpbase, int len, pthread_t **threads)
+{
+	cpu_set_t *mask;
+	size_t mask_size;
+	int i, cpu;
+
+	ncpus_max = sysconf(_SC_NPROCESSORS_CONF);
+	ncpus_online = sysconf(_SC_NPROCESSORS_ONLN);
+
+	if (ncpus_online < 2) {
+		EPRINTF("UNRESOLVED: we need@least %d online CPUs, got %d",
+			2, ncpus_online);
+		exit(UNRESOLVED);
+	}
+
+	tplen = len - 1 + (ncpus_online - 1);
+
+	mask_size = CPU_ALLOC_SIZE(ncpus_max);
+	mask = CPU_ALLOC(ncpus_max);
+	if (mask == NULL) {
+		EPRINTF("UNRESOLVED: failed to allocate a CPU mask");
+		exit(UNRESOLVED);
+	}
+
+	if (sched_getaffinity(0, mask_size, mask)) {
+		EPRINTF("UNRESOLVED: sched_getaffinity: %s", strerror(errno));
+		CPU_FREE(mask);
+		exit(UNRESOLVED);
+	}
+
+	/* This is the CPU where all non-TF threads will be running */
+	cpu = first_available_cpu(ncpus_max, mask_size, mask);
+	if (cpu == -1) {
+		EPRINTF("UNRESOLVED: first_available_cpu failed");
+		CPU_FREE(mask);
+		exit(UNRESOLVED);
+	}
+	CPU_CLR_S(cpu, mask_size, mask);
+
+	tp = calloc(tplen, sizeof(struct thread_param));
+
+	for (i = 0; i < len - 1; i++) {
+		tp[i] = tpbase[i];
+		tp[i].cpu = cpu;
+	}
+
+	/* Each TF thread is bind to its own online CPU */
+	for (i = len - 1; i < tplen; i++) {
+		tp[i] = tpbase[len - 1];
+		tp[i].index = i;
+
+		cpu = first_available_cpu(ncpus_max, mask_size, mask);
+		if (cpu == -1) {
+			EPRINTF("UNRESOLVED: first_available_cpu failed");
+			CPU_FREE(mask);
+			exit(UNRESOLVED);
+		}
+		tp[i].cpu = cpu;
+		CPU_CLR_S(cpu, mask_size, mask);
+	}
+
+	CPU_FREE(mask);
+
+	*threads = calloc(tplen - 1, sizeof(pthread_t));
+	if (*threads == NULL) {
+		EPRINTF("UNRESOLVED: calloc failed");
+		exit(UNRESOLVED);
+	}
+}
+
+static void test_set_affinity(pid_t pid, int cpu)
+{
+	cpu_set_t *mask;
+	size_t mask_size;
+
+	if (cpu >= ncpus_max) {
+		EPRINTF("UNRESOLVED: invalid CPU specified: %d", cpu);
+		exit(UNRESOLVED);
+	}
+
+	mask_size = CPU_ALLOC_SIZE(ncpus_max);
+	mask = CPU_ALLOC(ncpus_max);
+	if (mask == NULL) {
+		EPRINTF("UNRESOLVED: failed to allocate a CPU mask");
+		exit(UNRESOLVED);
+	}
+
+	CPU_ZERO_S(mask_size, mask);
+	CPU_SET_S(cpu, mask_size, mask);
+
+	if (sched_setaffinity(pid, mask_size, mask)) {
+		EPRINTF("UNRESOLVED: pid %d: sched_setaffinity failed: %s",
+			pid, strerror(errno));
+		CPU_FREE(mask);
+		exit(UNRESOLVED);
+	}
+	CPU_FREE(mask);
+}
+
 static void *thread_fn(void *param)
 {
 	struct thread_param *tp = param;
 	struct timespec ts;
 	int rc;
-	unsigned long mask = 1 << tp->cpu;
 
-#if __linux__
-	rc = sched_setaffinity(0, sizeof(mask), &mask);
-	if (rc < 0) {
-		EPRINTF("UNRESOLVED: Thread %s index %d: Can't set affinity: "
-			"%d %s", tp->name, tp->index, rc, strerror(rc));
-		exit(UNRESOLVED);
-	}
-#endif
+	test_set_affinity(0, tp->cpu);
 	test_set_priority(pthread_self(), tp->policy, tp->priority);
 
 	DPRINTF(stdout, "#EVENT %f %s Thread Started\n",
@@ -186,19 +315,9 @@ static void *thread_tl(void *param)
 {
 	struct tl_param *tlp = param;
 	struct thread_param *tp = tlp->tp;
-	unsigned long mask = 1 << tp->cpu;
 	pthread_mutex_t **mutex;
-	int rc;
 
-#if __linux__
-	rc = sched_setaffinity((pid_t) 0, sizeof(mask), &mask);
-	if (rc < 0) {
-		EPRINTF
-		    ("UNRESOLVED: Thread %s index %d: Can't set affinity: %d %s",
-		     tp->name, tp->index, rc, strerror(rc));
-		exit(UNRESOLVED);
-	}
-#endif
+	test_set_affinity(0, tp->cpu);
 	test_set_priority(pthread_self(), tp->policy, tp->priority);
 
 
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c +12 -16
index 3b4b360..cb0290c 100644
@@ -38,16 +38,11 @@
 pthread_mutex_t mutex;
 pthread_mutex_t *mutexes[] = { &mutex, NULL };
 
-struct thread_param tp[] = {
+struct thread_param tpbase[] = {
 	{
 	0, 0, 0, 1, SCHED_FIFO, "TL", 0, 0, 0, 0}, {
 	1, 0, 50, 2, SCHED_FIFO, "TP", 0, 0, 0, 0}, {
-	2, 0, 0, 3, SCHED_FIFO, "TF", 1, 0, 0, 0}, {
-	3, 0, 0, 3, SCHED_FIFO, "TF", 2, 0, 0, 0}, {
-	4, 0, 0, 3, SCHED_FIFO, "TF", 3, 0, 0, 0}, {
-	5, 0, 0, 3, SCHED_FIFO, "TF", 4, 0, 0, 0}, {
-	6, 0, 0, 3, SCHED_FIFO, "TF", 5, 0, 0, 0}, {
-	7, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
+	2, 0, 0, 3, SCHED_FIFO, "TF", 1, 0, 0, 0},
 };
 
 void *thread_tb(void *arg)
@@ -86,15 +81,17 @@ int main(void)
 {
 	pthread_mutexattr_t mutex_attr;
 	pthread_attr_t threadattr;
-	pthread_t threads[cpus - 1], threadsample, threadtp, threadtl, threadtb;
+	pthread_t *threads;
+	pthread_t threadsample, threadtp, threadtl, threadtb;
 	struct tl_param tlp;
 	struct sample_param sp;
 	int multiplier = 1;
-	int i;
-	int rc;
+	int i, rc;
 
 	test_set_priority(pthread_self(), SCHED_FIFO, 6);
-	cpus = sysconf(_SC_NPROCESSORS_ONLN);
+
+	setup(tpbase, 3, &threads);
+
 	base_time = seconds_read();
 
 	/* Initialize a mutex with PTHREAD_PRIO_INHERIT protocol */
@@ -107,7 +104,7 @@ int main(void)
 	/* Start the sample thread */
 	DPRINTF(stderr, "Main Thread: start sample thread \n");
 	sp.tp = tp;
-	sp.tplen = 2 + cpus - 1;
+	sp.tplen = tplen;
 	sp.period = 250;
 	sp.priority = 5;
 	rc = pthread_create(&threadsample, &threadattr, thread_sample, &sp);
@@ -117,8 +114,8 @@ int main(void)
 	}
 
 	/* Start the TF threads */
-	DPRINTF(stderr, "Main Thread: start %d TF thread\n", cpus - 1);
-	for (i = 0; i < cpus - 1; i++) {
+	DPRINTF(stderr, "Main Thread: start %d TF thread\n", tplen - 2);
+	for (i = 0; i < tplen - 2; i++) {
 		rc = pthread_create(&threads[i], &threadattr, thread_fn,
 				    &tp[i + 2]);
 		if (rc != 0) {
@@ -173,9 +170,8 @@ int main(void)
 
 	/* Stop TF threads */
 	DPRINTF(stderr, "Main Thread: stop TF threads\n");
-	for (i = 2; i < cpus - 1; i++) {
+	for (i = 2; i < tplen; i++)
 		tp[i].stop = 1;
-	}
 
 	/* Stop sampler */
 	ts_stop = 1;
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c +11 -16
index 76716f4..a4cea52 100644
@@ -40,17 +40,12 @@
 pthread_mutex_t mutex;
 pthread_mutex_t *mutexes[] = { &mutex, NULL };
 
-struct thread_param tp[] = {
+struct thread_param tpbase[] = {
 	{
 	0, 0, 0, 1, SCHED_FIFO, "TL", 0, 0, 0, 0}, {
 	1, 0, 500, 2, SCHED_FIFO, "TP1", 0, 0, 0, 0}, {
 	2, 0, 500, 5, SCHED_FIFO, "TP2", 0, 0, 0, 0}, {
-	3, 0, 0, 3, SCHED_FIFO, "TF", 1, 0, 0, 0}, {
-	4, 0, 0, 3, SCHED_FIFO, "TF", 2, 0, 0, 0}, {
-	5, 0, 0, 3, SCHED_FIFO, "TF", 3, 0, 0, 0}, {
-	6, 0, 0, 3, SCHED_FIFO, "TF", 4, 0, 0, 0}, {
-	7, 0, 0, 3, SCHED_FIFO, "TF", 5, 0, 0, 0}, {
-	8, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
+	3, 0, 0, 3, SCHED_FIFO, "TF", 1, 0, 0, 0}
 };
 
 void *thread_tb1(void *arg)
@@ -114,16 +109,17 @@ int main(void)
 {
 	pthread_mutexattr_t mutex_attr;
 	pthread_attr_t threadattr;
-	pthread_t threads[cpus - 1];
+	pthread_t *threads;
 	pthread_t threadsample, threadtp, threadtl, threadtb1, threadtb2;
 	struct tl_param tlp;
 	struct sample_param sp;
 	time_t multiplier = 1;
-	int i;
-	int rc;
+	int i, rc;
 
 	test_set_priority(pthread_self(), SCHED_FIFO, 8);
-	cpus = sysconf(_SC_NPROCESSORS_ONLN);
+
+	setup(tpbase, 4, &threads);
+
 	base_time = seconds_read();
 
 	/* Initialize a mutex with PTHREAD_PRIO_INHERIT protocol */
@@ -136,7 +132,7 @@ int main(void)
 	/* Start the sample thread */
 	DPRINTF(stderr, "Main Thread: Creating sample thread \n");
 	sp.tp = tp;
-	sp.tplen = 3 + cpus - 1;
+	sp.tplen = tplen;
 	sp.period = 300;
 	sp.priority = 7;
 	rc = pthread_create(&threadsample, &threadattr, thread_sample, &sp);
@@ -146,8 +142,8 @@ int main(void)
 	}
 
 	/* Start the TF threads */
-	DPRINTF(stderr, "Main Thread: Creating %d TF threads \n", cpus - 1);
-	for (i = 0; i < cpus - 1; i++) {
+	DPRINTF(stderr, "Main Thread: Creating %d TF threads\n", tplen - 3);
+	for (i = 0; i < tplen - 3; i++) {
 		rc = pthread_create(&threads[i], &threadattr, thread_fn,
 				    &tp[i + 3]);
 		if (rc != 0) {
@@ -210,9 +206,8 @@ int main(void)
 	sleep(base_time + multiplier * 120 - seconds_read());
 
 	/* Stop TF threads */
-	for (i = 2; i < cpus - 1; i++) {
+	for (i = 3; i < tplen; i++)
 		tp[i].stop = 1;
-	}
 
 	/* Stop sampler */
 	ts_stop = 1;
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c +11 -16
index 17cc43d..aa44c55 100644
@@ -41,17 +41,12 @@ pthread_mutex_t mutex1;
 pthread_mutex_t mutex2;
 pthread_mutex_t *mutexes[] = { &mutex1, &mutex2, NULL };
 
-struct thread_param tp[] = {
+struct thread_param tpbase[] = {
 	{
 	0, 0, 0, 1, SCHED_FIFO, "TL", 0, 0, 0, 0}, {
 	1, 0, 500, 2, SCHED_FIFO, "TP1", 0, 0, 0, 0}, {
 	1, 0, 500, 5, SCHED_FIFO, "TP2", 0, 0, 0, 0}, {
-	2, 0, 0, 3, SCHED_FIFO, "TF", 1, 0, 0, 0}, {
-	3, 0, 0, 3, SCHED_FIFO, "TF", 2, 0, 0, 0}, {
-	4, 0, 0, 3, SCHED_FIFO, "TF", 3, 0, 0, 0}, {
-	5, 0, 0, 3, SCHED_FIFO, "TF", 4, 0, 0, 0}, {
-	6, 0, 0, 3, SCHED_FIFO, "TF", 5, 0, 0, 0}, {
-	7, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
+	2, 0, 0, 3, SCHED_FIFO, "TF", 1, 0, 0, 0}
 };
 
 void *thread_tb1(void *arg)
@@ -115,17 +110,18 @@ int main(void)
 {
 	pthread_mutexattr_t mutex_attr;
 	pthread_attr_t threadattr;
-	pthread_t threads[cpus - 1];
+	pthread_t *threads;
 	pthread_t threadsample, threadtp, threadtl, threadtb1, threadtb2;
 	struct tl_param tlp;
 	struct sample_param sp;
 	time_t multiplier = 1;
-	int i;
-	int rc;
+	int i, rc;
 
 	test_set_priority(pthread_self(), SCHED_FIFO, 8);
+
+	setup(tpbase, 4, &threads);
+
 	base_time = seconds_read();
-	cpus = sysconf(_SC_NPROCESSORS_ONLN);
 
 	/* Initialize mutex1, mutex2 with PTHREAD_PRIO_INHERIT protocol */
 	mutex_attr_init(&mutex_attr);
@@ -138,7 +134,7 @@ int main(void)
 	/* Start the sample thread */
 	DPRINTF(stderr, "Main Thread: Creating sample thread\n");
 	sp.tp = tp;
-	sp.tplen = 3 + cpus - 1;
+	sp.tplen = tplen;
 	sp.period = 300;
 	sp.priority = 7;
 	rc = pthread_create(&threadsample, &threadattr, thread_sample, &sp);
@@ -148,8 +144,8 @@ int main(void)
 	}
 
 	/* Start the TF threads */
-	DPRINTF(stderr, "Main Thread: Creating %d TF threads\n", cpus - 1);
-	for (i = 0; i < cpus - 1; i++) {
+	DPRINTF(stderr, "Main Thread: Creating %d TF threads\n", tplen - 3);
+	for (i = 0; i < tplen - 3; i++) {
 		rc = pthread_create(&threads[i], &threadattr, thread_fn,
 				    &tp[i + 3]);
 		if (rc != 0) {
@@ -212,9 +208,8 @@ int main(void)
 	sleep(base_time + multiplier * 120 - seconds_read());
 
 	/* Stop TF threads */
-	for (i = 2; i < cpus - 1; i++) {
+	for (i = 3; i < tplen; i++)
 		tp[i].stop = 1;
-	}
 
 	/* Stop sampler */
 	ts_stop = 1;
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c +12 -25
index 84f9878..4e8cbed 100644
@@ -41,16 +41,11 @@
 pthread_mutex_t mutex1;
 pthread_mutex_t mutex2;
 
-struct thread_param tp[] = {
+struct thread_param tpbase[] = {
 	{
 	0, 0, 0, 1, SCHED_FIFO, "TL", 0, 0, 0, 0}, {
 	1, 0, 100, 4, SCHED_FIFO, "TP", 0, 0, 0, 0}, {
-	2, 0, 0, 2, SCHED_FIFO, "TF", 1, 0, 0, 0}, {
-	3, 0, 0, 2, SCHED_FIFO, "TF", 2, 0, 0, 0}, {
-	4, 0, 0, 2, SCHED_FIFO, "TF", 3, 0, 0, 0}, {
-	5, 0, 0, 2, SCHED_FIFO, "TF", 4, 0, 0, 0}, {
-	6, 0, 0, 2, SCHED_FIFO, "TF", 5, 0, 0, 0}, {
-	7, 0, 0, 2, SCHED_FIFO, "TF", 6, 0, 0, 0}
+	2, 0, 0, 2, SCHED_FIFO, "TF", 1, 0, 0, 0}
 };
 
 void *thread_fn4(void *param)
@@ -58,16 +53,8 @@ void *thread_fn4(void *param)
 	struct thread_param *tp = param;
 	struct timespec ts;
 	int rc;
-	unsigned long mask = 1 << tp->cpu;
 
-#if __linux__
-	rc = sched_setaffinity(0, sizeof(mask), &mask);
-	if (rc < 0) {
-		EPRINTF("UNRESOLVED: Thread %s index %d: Can't set affinity: "
-			"%d %s", tp->name, tp->index, rc, strerror(rc));
-		exit(UNRESOLVED);
-	}
-#endif
+	test_set_affinity(0, tp->cpu);
 	test_set_priority(pthread_self(), tp->policy, tp->priority);
 
 	DPRINTF(stdout, "#EVENT %f Thread %s Started\n",
@@ -163,16 +150,17 @@ int main(void)
 {
 	pthread_mutexattr_t mutex_attr;
 	pthread_attr_t threadattr;
-	pthread_t threads[cpus - 1];
+	pthread_t *threads;
 	pthread_t threadsample, threadtp, threadtl, threadtb1, threadtb2;
 	struct sample_param sp;
 	time_t multiplier = 1;
-	int i;
-	int rc;
+	int i, rc;
 
 	test_set_priority(pthread_self(), SCHED_FIFO, 8);
+
+	setup(tpbase, 3, &threads);
+
 	base_time = seconds_read();
-	cpus = sysconf(_SC_NPROCESSORS_ONLN);
 
 	/* Initialize mutex1, mutex2 with PTHREAD_PRIO_INHERIT protocol */
 	mutex_attr_init(&mutex_attr);
@@ -185,7 +173,7 @@ int main(void)
 	/* Start the sample thread */
 	DPRINTF(stderr, "Main Thread: Creating sample thread\n");
 	sp.tp = tp;
-	sp.tplen = 2 + cpus - 1;
+	sp.tplen = tplen;
 	sp.period = 300;
 	sp.priority = 6;
 	rc = pthread_create(&threadsample, &threadattr, thread_sample, &sp);
@@ -195,8 +183,8 @@ int main(void)
 	}
 
 	/* Start the TF threads */
-	DPRINTF(stderr, "Main Thread: Creating %d TF threads\n", cpus - 1);
-	for (i = 0; i < cpus - 1; i++) {
+	DPRINTF(stderr, "Main Thread: Creating %d TF threads\n", tplen - 2);
+	for (i = 0; i < tplen - 2; i++) {
 		rc = pthread_create(&threads[i], &threadattr, thread_fn4,
 				    &tp[i + 2]);
 		if (rc != 0) {
@@ -252,9 +240,8 @@ int main(void)
 	sleep(base_time + multiplier * 95 - seconds_read());
 
 	/* Stop TF threads */
-	for (i = 2; i < cpus - 1; i++) {
+	for (i = 2; i < tplen; i++)
 		tp[i].stop = 1;
-	}
 
 	/* Stop sampler */
 	ts_stop = 1;
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c +13 -26
index 3ba3a94..4966ad9 100644
@@ -39,34 +39,20 @@
 pthread_mutex_t mutex;
 volatile int unlock_mutex = 0;
 
-struct thread_param tp[] = {
+struct thread_param tpbase[] = {
 	{
 	0, 0, 0, 1, SCHED_FIFO, "TL", 0, 0, 0, 0}, {
 	1, 0, 200, 2, SCHED_FIFO, "TP", 0, 0, 0, 0}, {
-	2, 0, 0, 3, SCHED_FIFO, "TF", 1, 0, 0, 0}, {
-	3, 0, 0, 3, SCHED_FIFO, "TF", 2, 0, 0, 0}, {
-	4, 0, 0, 3, SCHED_FIFO, "TF", 3, 0, 0, 0}, {
-	5, 0, 0, 3, SCHED_FIFO, "TF", 4, 0, 0, 0}, {
-	6, 0, 0, 3, SCHED_FIFO, "TF", 5, 0, 0, 0}, {
-	7, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
+	2, 0, 0, 3, SCHED_FIFO, "TF", 1, 0, 0, 0}
 };
 
 void *thread_tl5(void *param)
 {
 	struct thread_param *tp = param;
-	unsigned long mask = 1 << tp->cpu;
 	int rc;
 
 	test_set_priority(pthread_self(), tp->policy, tp->priority);
-#if __linux__
-	rc = sched_setaffinity((pid_t) 0, sizeof(mask), &mask);
-	if (rc < 0) {
-		EPRINTF
-		    ("UNRESOLVED: Thread %s index %d: Can't set affinity: %d %s",
-		     tp->name, tp->index, rc, strerror(rc));
-		exit(UNRESOLVED);
-	}
-#endif
+	test_set_affinity(0, tp->cpu);
 
 	DPRINTF(stdout, "#EVENT %f Thread TL started\n",
 		seconds_read() - base_time);
@@ -137,15 +123,17 @@ int main(void)
 {
 	pthread_mutexattr_t mutex_attr;
 	pthread_attr_t threadattr;
-	pthread_t threads[cpus - 1], threadsample, threadtp, threadtl, threadtb;
+	pthread_t *threads;
+	pthread_t threadsample, threadtp, threadtl, threadtb;
 	struct sample_param sp;
 	time_t multiplier = 1;
-	int i;
-	int rc;
+	int i, rc;
 
 	test_set_priority(pthread_self(), SCHED_FIFO, 6);
+
+	setup(tpbase, 3, &threads);
+
 	base_time = seconds_read();
-	cpus = sysconf(_SC_NPROCESSORS_ONLN);
 
 	/* Initialize a mutex with PTHREAD_PRIO_INHERIT protocol */
 	mutex_attr_init(&mutex_attr);
@@ -157,7 +145,7 @@ int main(void)
 	/* Start the sample thread */
 	DPRINTF(stderr, "Main Thread: Creating sample thread\n");
 	sp.tp = tp;
-	sp.tplen = 2 + cpus - 1;
+	sp.tplen = tplen;
 	sp.period = 300;
 	sp.priority = 5;
 	rc = pthread_create(&threadsample, &threadattr, thread_sample, &sp);
@@ -166,8 +154,8 @@ int main(void)
 		exit(UNRESOLVED);
 	}
 	/* Start the TF threads */
-	DPRINTF(stderr, "Main Thread: Creating %d TF threads\n", cpus - 1);
-	for (i = 0; i < cpus - 1; i++) {
+	DPRINTF(stderr, "Main Thread: Creating %d TF threads\n", tplen - 2);
+	for (i = 0; i < tplen - 2; i++) {
 		rc = pthread_create(&threads[i], &threadattr, thread_fn,
 				    &tp[i + 2]);
 		if (rc != 0) {
@@ -216,9 +204,8 @@ int main(void)
 	sleep(base_time + multiplier * 70 - seconds_read());
 
 	/* Stop TF threads */
-	for (i = 2; i < cpus - 1; i++) {
+	for (i = 2; i < tplen; i++)
 		tp[i].stop = 1;
-	}
 
 	/* Stop sampler */
 	ts_stop = 1;
testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c +12 -17
index 46b36f7..44936c5 100644
@@ -38,16 +38,11 @@
 pthread_mutex_t mutex;
 pthread_mutex_t *mutexes[] = { &mutex, NULL };
 
-struct thread_param tp[] = {
+struct thread_param tpbase[] = {
 	{
 	0, 0, 0, 1, SCHED_FIFO, "TL", 0, 0, 0, 0}, {
 	1, 0, 200, 2, SCHED_FIFO, "TP", 0, 0, 0, 0}, {
-	2, 0, 0, 3, SCHED_FIFO, "TF", 1, 0, 0, 0}, {
-	3, 0, 0, 3, SCHED_FIFO, "TF", 2, 0, 0, 0}, {
-	4, 0, 0, 3, SCHED_FIFO, "TF", 3, 0, 0, 0}, {
-	5, 0, 0, 3, SCHED_FIFO, "TF", 4, 0, 0, 0}, {
-	6, 0, 0, 3, SCHED_FIFO, "TF", 5, 0, 0, 0}, {
-	7, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
+	2, 0, 0, 3, SCHED_FIFO, "TF", 1, 0, 0, 0}
 };
 
 void *thread_tb(void *arg)
@@ -83,19 +78,20 @@ void *thread_tb(void *arg)
 
 int main(void)
 {
-	cpus = sysconf(_SC_NPROCESSORS_ONLN);
 	pthread_mutexattr_t mutex_attr;
 	pthread_attr_t threadattr;
-	pthread_t threads[cpus - 1], threadsample, threadtp, threadtl, threadtb;
+	pthread_t *threads;
+	pthread_t threadsample, threadtp, threadtl, threadtb;
 	struct tl_param tlp;
 	struct sample_param sp;
 	time_t multiplier = 1;
-	int i;
-	int rc;
+	int i, rc;
 
 	test_set_priority(pthread_self(), SCHED_FIFO, 6);
+
+	setup(tpbase, 3, &threads);
+
 	base_time = seconds_read();
-	cpus = sysconf(_SC_NPROCESSORS_ONLN);
 
 	/* Initialize a mutex with PTHREAD_PRIO_INHERIT protocol */
 	mutex_attr_init(&mutex_attr);
@@ -107,7 +103,7 @@ int main(void)
 	/* Start the sample thread */
 	DPRINTF(stderr, "Main Thread: Creating sample thread\n");
 	sp.tp = tp;
-	sp.tplen = 2 + cpus - 1;
+	sp.tplen = tplen;
 	sp.period = 250;
 	sp.priority = 5;
 	rc = pthread_create(&threadsample, &threadattr, thread_sample, &sp);
@@ -117,8 +113,8 @@ int main(void)
 	}
 
 	/* Start the TF threads */
-	DPRINTF(stderr, "Main Thread: Creating %d TF threads\n", cpus - 1);
-	for (i = 0; i < cpus - 1; i++) {
+	DPRINTF(stderr, "Main Thread: Creating %d TF threads\n", tplen - 2);
+	for (i = 0; i < tplen - 2; i++) {
 		rc = pthread_create(&threads[i], &threadattr, thread_fn,
 				    &tp[i + 2]);
 		if (rc != 0) {
@@ -175,9 +171,8 @@ int main(void)
 	sleep(base_time + multiplier * 80 - seconds_read());
 
 	/* Stop TF threads */
-	for (i = 2; i < cpus - 1; i++) {
+	for (i = 2; i < tplen; i++)
 		tp[i].stop = 1;
-	}
 
 	/* Stop sampler */
 	ts_stop = 1;
Signature · 1 line
1.7.1
#9 lore raw

PATCH 7/7 pi_test: use only available CPUs

In reply to Stanislav Kholmanskikh

Hi!

Quoted 157 lines
testcases/open_posix_testsuite/functional/threads/include/pitest.h +142 -1
+#ifndef CPU_ALLOC
+#define CPU_ALLOC(ncpus) malloc(sizeof(cpu_set_t)); \
+if (ncpus > CPU_SETSIZE) { \
+	EPRINTF("UNRESOLVED: Your libc does not support masks with %ld cpus", \
+		ncpus); \
+	exit(UNRESOLVED); \
+}
+#endif
+
+#ifndef CPU_FREE
+#define CPU_FREE(ptr) free(ptr)
+#endif
+
+#ifndef CPU_ALLOC_SIZE
+#define CPU_ALLOC_SIZE(size) sizeof(cpu_set_t)
+#endif
+
+#ifndef CPU_ZERO_S
+#define CPU_ZERO_S(size, mask) CPU_ZERO(mask)
+#endif
+
+#ifndef CPU_SET_S
+#define CPU_SET_S(cpu, size, mask) CPU_SET(cpu, mask)
+#endif
+
 struct thread_param {
 	int index;
 	volatile int stop;
@@ -52,7 +77,6 @@ struct sample_param {
 	int priority;
 };
 
-static int cpus;
 static volatile int ts_stop;
 static volatile double base_time;
 
@@ -141,21 +165,141 @@ static void do_work(unsigned granularity_top, volatile unsigned *progress)
 	}
 }
 
+static void *test_calloc(size_t nmemb, size_t size)
+{
+	void *r;
+
+	r = calloc(nmemb, size);
+	if (r == NULL) {
+		EPRINTF("UNRESOLVED: calloc failed");
+		exit(UNRESOLVED);
+	}
+
+	return r;
+}
+
+static int first_available_cpu(int ncpus_max, size_t mask_size, cpu_set_t *mask)
+{
+	int i;
+
+	for (i = 0; i < ncpus_max; i++) {
+		if (CPU_ISSET_S(i, mask_size, mask))
+			return i;
+	}
+	return -1;
+}
+
+/*
+ * Allocates a new array of 'struct thread_param' with
+ * the number of TF entries increased to 'online CPUs - 1'.
+ * All non-TF entries are bind to the first available online CPU,
+ * each TF-entry is bind to its own online CPU.
+ */
+static void init_tparam(struct thread_param *tpbase, int len,
+		  struct thread_param **tpnew, int *newlen)
+{
+	struct thread_param *tp;
+	cpu_set_t *mask;
+	size_t mask_size;
+	int i, cpu, ncpus_max, ncpus_online;
+
+	ncpus_max = sysconf(_SC_NPROCESSORS_CONF);
+	ncpus_online = sysconf(_SC_NPROCESSORS_ONLN);
+
+	if (ncpus_online < 2) {
+		EPRINTF("UNRESOLVED: we need at least %d online CPUs, got %d",
+			2, ncpus_online);
+		exit(UNRESOLVED);
+	}
+
+	*newlen = len - 1 + (ncpus_online - 1);
+
+	mask_size = CPU_ALLOC_SIZE(ncpus_max);
+	mask = CPU_ALLOC(ncpus_max);
+	if (mask == NULL) {
+		EPRINTF("UNRESOLVED: failed to allocate a CPU mask");
+		exit(UNRESOLVED);
+	}
+
+	if (sched_getaffinity(0, mask_size, mask)) {
+		EPRINTF("UNRESOLVED: sched_getaffinity: %s", strerror(errno));
+		CPU_FREE(mask);
+		exit(UNRESOLVED);
+	}
+
+	/* This is the CPU where all non-TF threads will be running */
+	cpu = first_available_cpu(ncpus_max, mask_size, mask);
+	if (cpu == -1) {
+		EPRINTF("UNRESOLVED: first_available_cpu failed");
+		CPU_FREE(mask);
+		exit(UNRESOLVED);
+	}
+	CPU_CLR_S(cpu, mask_size, mask);
+
+	tp = calloc(*newlen, sizeof(struct thread_param));
+
+	for (i = 0; i < len - 1; i++) {
+		tp[i] = tpbase[i];
+		tp[i].cpu = cpu;
+	}
+	for (i = len - 1; i < *newlen; i++) {
+		tp[i] = tpbase[len - 1];
+
+		cpu = first_available_cpu(ncpus_max, mask_size, mask);
+		if (cpu == -1) {
+			EPRINTF("UNRESOLVED: first_available_cpu failed");
+			CPU_FREE(mask);
+			exit(UNRESOLVED);
+		}
+		tp[i].cpu = cpu;
+		CPU_CLR_S(cpu, mask_size, mask);
+	}
+
+	CPU_FREE(mask);
+
+	*tpnew = tp;
+}
+
+static void test_set_affinity(pid_t pid, int cpu)
+{
+	cpu_set_t *mask;
+	size_t mask_size;
+	int ncpus_max, ncpus_online;
+
+	ncpus_max = sysconf(_SC_NPROCESSORS_CONF);
+	ncpus_online = sysconf(_SC_NPROCESSORS_ONLN);
+
+	if (cpu >= ncpus_max) {
+		EPRINTF("UNRESOLVED: invalid CPU specified: %d", cpu);
+		exit(UNRESOLVED);
+	}
+
+	mask_size = CPU_ALLOC_SIZE(ncpus_max);
+	mask = CPU_ALLOC(ncpus_max);
+	if (mask == NULL) {
+		EPRINTF("UNRESOLVED: failed to allocate a CPU mask");
+		exit(UNRESOLVED);
+	}
+
+	CPU_ZERO_S(mask_size, mask);

testcases/open_posix_testsuite/functional/threads/include/pitest.h static void do_work(unsigned granularity_top, volatile unsigned *progress) · line 294

Quoted 10 lines
testcases/open_posix_testsuite/functional/threads/include/pitest.h +10 -0
+	CPU_SET_S(cpu, mask_size, mask);
+
+	if (sched_setaffinity(pid, mask_size, mask)) {
+		EPRINTF("UNRESOLVED: pid %d: sched_setaffinity failed: %s",
+			pid, strerror(errno));
+		CPU_FREE(mask);
+		exit(UNRESOLVED);
+	}
+	CPU_FREE(mask);
+}

Technically we must not use linux specific calls in the open posix testsuite without #ifdefing them around.

And we have most of this functionality allready in open_posix_testsuite/include/affinity.h you should really use or modify and use functions from there since that makes it the only place that needs to be ported to other POSIX compatible OSes.

Signature · 2 lines
Cyril Hrubis
[email protected]