All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] pi_test: support more CPU configurations
@ 2015-12-18 14:35 Stanislav Kholmanskikh
  2015-12-18 14:35 ` [LTP] [PATCH 1/7] pi_test: move duplicated stuff to the header Stanislav Kholmanskikh
                   ` (6 more replies)
  0 siblings, 7 replies; 10+ messages in thread
From: Stanislav Kholmanskikh @ 2015-12-18 14:35 UTC (permalink / raw)
  To: ltp

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.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [LTP] [PATCH 1/7] pi_test: move duplicated stuff to the header
  2015-12-18 14:35 [LTP] pi_test: support more CPU configurations Stanislav Kholmanskikh
@ 2015-12-18 14:35 ` Stanislav Kholmanskikh
  2015-12-18 14:35 ` [LTP] [PATCH 2/7] pi_test: move thread_fn() " Stanislav Kholmanskikh
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: Stanislav Kholmanskikh @ 2015-12-18 14:35 UTC (permalink / raw)
  To: ltp

Signed-off-by: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
---
 .../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(-)

diff --git a/testcases/open_posix_testsuite/functional/threads/include/pitest.h b/testcases/open_posix_testsuite/functional/threads/include/pitest.h
index dd7a48f..89d2ca0 100644
--- a/testcases/open_posix_testsuite/functional/threads/include/pitest.h
+++ b/testcases/open_posix_testsuite/functional/threads/include/pitest.h
@@ -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)++;
+	}
+}
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c
index 11867fe..3db1134 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c
@@ -33,37 +33,11 @@
  * Thanks Inaky.Perez-Gonzalez's suggestion and code
  */
 
-#warning "Contains Linux-isms that need fixing."
-
-#include <errno.h>
-#include <pthread.h>
-#include <sched.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-#include <unistd.h>
-#include "test.h"
 #include "pitest.h"
 
-int cpus;
 pthread_mutex_t mutex;
 
-volatile int ts_stop = 0;
-volatile double base_time;
-
-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;
-} tp[] = {
+struct thread_param tp[] = {
 	{
 	0, 0, 0, 1, SCHED_FIFO, "TL", 0, 0, 0, 0}, {
 	1, 0, 50, 2, SCHED_FIFO, "TP", 0, 0, 0, 0}, {
@@ -75,22 +49,6 @@ struct thread_param {
 	7, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
 };
 
-volatile unsigned do_work_dummy;
-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)++;
-	}
-	return;
-}
-
 void *thread_fn(void *param)
 {
 	struct thread_param *tp = param;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c
index dda51c3..0220af8 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c
@@ -35,36 +35,11 @@
  *
  */
 
-#warning "Contains Linux-isms that need fixing."
-
-#include <errno.h>
-#include <pthread.h>
-#include <sched.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-#include <unistd.h>
-#include "test.h"
 #include "pitest.h"
 
-int cpus;
 pthread_mutex_t mutex;
-volatile int ts_stop = 0;
-volatile double base_time;
-
-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;
-} tp[] = {
+
+struct thread_param tp[] = {
 	{
 	0, 0, 0, 1, SCHED_FIFO, "TL", 0, 0, 0, 0}, {
 	1, 0, 500, 2, SCHED_FIFO, "TP1", 0, 0, 0, 0}, {
@@ -77,22 +52,6 @@ struct thread_param {
 	8, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
 };
 
-volatile unsigned do_work_dummy;
-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)++;
-	}
-	return;
-}
-
 void *thread_fn(void *param)
 {
 	struct thread_param *tp = param;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c
index 5e925a5..b9d63ec 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c
@@ -35,37 +35,12 @@
  *
  */
 
-#warning "Contains Linux-isms that need fixing."
-
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
-#include <stdlib.h>
-#include <time.h>
-#include <sched.h>
-#include <errno.h>
-#include "test.h"
 #include "pitest.h"
 
-int cpus;
 pthread_mutex_t mutex1;
 pthread_mutex_t mutex2;
-volatile int ts_stop = 0;
-volatile double base_time;
-
-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;
-} tp[] = {
+
+struct thread_param tp[] = {
 	{
 	0, 0, 0, 1, SCHED_FIFO, "TL", 0, 0, 0, 0}, {
 	1, 0, 500, 2, SCHED_FIFO, "TP1", 0, 0, 0, 0}, {
@@ -78,22 +53,6 @@ struct thread_param {
 	7, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
 };
 
-volatile unsigned do_work_dummy;
-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)++;
-	}
-	return;
-}
-
 void *thread_fn(void *param)
 {
 	struct thread_param *tp = param;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c
index dc4bd02..75a776b 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c
@@ -36,37 +36,12 @@
  *
  */
 
-#warning "Contains Linux-isms that need fixing."
-
-#include <errno.h>
-#include <pthread.h>
-#include <sched.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-#include <unistd.h>
-#include "test.h"
 #include "pitest.h"
 
-int cpus;
 pthread_mutex_t mutex1;
 pthread_mutex_t mutex2;
-volatile int ts_stop = 0;
-volatile double base_time;
-
-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;
-} tp[] = {
+
+struct thread_param tp[] = {
 	{
 	0, 0, 0, 1, SCHED_FIFO, "TL", 0, 0, 0, 0}, {
 	1, 0, 100, 4, SCHED_FIFO, "TP", 0, 0, 0, 0}, {
@@ -78,22 +53,6 @@ struct thread_param {
 	7, 0, 0, 2, SCHED_FIFO, "TF", 6, 0, 0, 0}
 };
 
-volatile unsigned do_work_dummy;
-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)++;
-	}
-	return;
-}
-
 void *thread_fn(void *param)
 {
 	struct thread_param *tp = param;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c
index 9540d06..b70c549 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c
@@ -34,37 +34,12 @@
  * NOTE: Most of the code is ported from test-11 written by inkay.
  */
 
-#warning "Contains Linux-isms that need fixing."
-
-#include <errno.h>
-#include <pthread.h>
-#include <sched.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-#include <unistd.h>
-#include "test.h"
 #include "pitest.h"
 
-int cpus;
 pthread_mutex_t mutex;
-volatile int ts_stop = 0;
-volatile double base_time;
 volatile int unlock_mutex = 0;
 
-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;
-} tp[] = {
+struct thread_param tp[] = {
 	{
 	0, 0, 0, 1, SCHED_FIFO, "TL", 0, 0, 0, 0}, {
 	1, 0, 200, 2, SCHED_FIFO, "TP", 0, 0, 0, 0}, {
@@ -76,22 +51,6 @@ struct thread_param {
 	7, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
 };
 
-volatile unsigned do_work_dummy;
-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)++;
-	}
-	return;
-}
-
 void *thread_fn(void *param)
 {
 	struct thread_param *tp = param;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c
index 19d6755..bd92613 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c
@@ -33,36 +33,11 @@
  * NOTE: Most of the code is ported from test-11 written by inkay.
  */
 
-#warning "Contains Linux-isms that need fixing."
-
-#include <errno.h>
-#include <pthread.h>
-#include <sched.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-#include <unistd.h>
-#include "test.h"
 #include "pitest.h"
 
-int cpus;
 pthread_mutex_t mutex;
-volatile int ts_stop = 0;
-volatile double base_time;
-
-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;
-} tp[] = {
+
+struct thread_param tp[] = {
 	{
 	0, 0, 0, 1, SCHED_FIFO, "TL", 0, 0, 0, 0}, {
 	1, 0, 200, 2, SCHED_FIFO, "TP", 0, 0, 0, 0}, {
@@ -74,22 +49,6 @@ struct thread_param {
 	7, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
 };
 
-volatile unsigned do_work_dummy;
-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)++;
-	}
-	return;
-}
-
 void *thread_fn(void *param)
 {
 	struct thread_param *tp = param;
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [LTP] [PATCH 2/7] pi_test: move thread_fn() to the header
  2015-12-18 14:35 [LTP] pi_test: support more CPU configurations Stanislav Kholmanskikh
  2015-12-18 14:35 ` [LTP] [PATCH 1/7] pi_test: move duplicated stuff to the header Stanislav Kholmanskikh
@ 2015-12-18 14:35 ` Stanislav Kholmanskikh
  2015-12-18 14:35 ` [LTP] [PATCH 3/7] pi_test: move thread_tl() " Stanislav Kholmanskikh
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: Stanislav Kholmanskikh @ 2015-12-18 14:35 UTC (permalink / raw)
  To: ltp

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.

Signed-off-by: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
---
 .../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(-)

diff --git a/testcases/open_posix_testsuite/functional/threads/include/pitest.h b/testcases/open_posix_testsuite/functional/threads/include/pitest.h
index 89d2ca0..2bda342 100644
--- a/testcases/open_posix_testsuite/functional/threads/include/pitest.h
+++ b/testcases/open_posix_testsuite/functional/threads/include/pitest.h
@@ -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;
+}
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c
index 3db1134..7ef46f6 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c
@@ -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;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c
index 0220af8..066a7b8 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c
@@ -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;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c
index b9d63ec..e37746c 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c
@@ -53,47 +53,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 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;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c
index 75a776b..84ad62c 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c
@@ -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);
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c
index b70c549..6cc07b7 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c
@@ -51,47 +51,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;
-
-	test_set_priority(pthread_self(), SCHED_FIFO, tp->priority);
-#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
-
-	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;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c
index bd92613..c6c3046 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c
@@ -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;
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [LTP] [PATCH 3/7] pi_test: move thread_tl() to the header
  2015-12-18 14:35 [LTP] pi_test: support more CPU configurations Stanislav Kholmanskikh
  2015-12-18 14:35 ` [LTP] [PATCH 1/7] pi_test: move duplicated stuff to the header Stanislav Kholmanskikh
  2015-12-18 14:35 ` [LTP] [PATCH 2/7] pi_test: move thread_fn() " Stanislav Kholmanskikh
@ 2015-12-18 14:35 ` Stanislav Kholmanskikh
  2015-12-18 14:35 ` [LTP] [PATCH 4/7] pi_test: move thread_sample() " Stanislav Kholmanskikh
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: Stanislav Kholmanskikh @ 2015-12-18 14:35 UTC (permalink / raw)
  To: ltp

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

Signed-off-by: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
---
 .../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(-)

diff --git a/testcases/open_posix_testsuite/functional/threads/include/pitest.h b/testcases/open_posix_testsuite/functional/threads/include/pitest.h
index 2bda342..a23693b 100644
--- a/testcases/open_posix_testsuite/functional/threads/include/pitest.h
+++ b/testcases/open_posix_testsuite/functional/threads/include/pitest.h
@@ -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;
+}
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c
index 7ef46f6..adcfb0c 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c
@@ -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);
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c
index 066a7b8..bd3ae84 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c
@@ -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);
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c
index e37746c..4e42259 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c
@@ -39,6 +39,7 @@
 
 pthread_mutex_t mutex1;
 pthread_mutex_t mutex2;
+pthread_mutex_t *mutexes[] = { &mutex1, &mutex2, NULL };
 
 struct thread_param tp[] = {
 	{
@@ -53,41 +54,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(stdout, "#EVENT %f Thread TL Started\n",
-		seconds_read() - base_time);
-	DPRINTF(stderr, "Thread %s index %d: started\n", tp->name, tp->index);
-
-	tp->progress = 0;
-	pthread_mutex_lock(&mutex1);
-	pthread_mutex_lock(&mutex2);
-	while (!tp->stop) {
-		do_work(5, &tp->progress);
-	}
-
-	pthread_mutex_unlock(&mutex1);
-	pthread_mutex_unlock(&mutex2);
-	DPRINTF(stdout, "#EVENT %f Thread TL Stopped\n",
-		seconds_read() - base_time);
-	return NULL;
-}
-
 void *thread_sample(void *arg)
 {
 	char buffer[1024];
@@ -188,7 +154,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;
@@ -240,7 +206,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);
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c
index 6cc07b7..44c1772 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c
@@ -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);
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c
index c6c3046..e1a56b3 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c
@@ -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);
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [LTP] [PATCH 4/7] pi_test: move thread_sample() to the header
  2015-12-18 14:35 [LTP] pi_test: support more CPU configurations Stanislav Kholmanskikh
                   ` (2 preceding siblings ...)
  2015-12-18 14:35 ` [LTP] [PATCH 3/7] pi_test: move thread_tl() " Stanislav Kholmanskikh
@ 2015-12-18 14:35 ` Stanislav Kholmanskikh
  2015-12-18 14:35 ` [LTP] [PATCH 5/7] pi_test: thread_sample: do not use the buffer explicitly Stanislav Kholmanskikh
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: Stanislav Kholmanskikh @ 2015-12-18 14:35 UTC (permalink / raw)
  To: ltp

Signed-off-by: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
---
 .../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(-)

diff --git a/testcases/open_posix_testsuite/functional/threads/include/pitest.h b/testcases/open_posix_testsuite/functional/threads/include/pitest.h
index a23693b..41c8096 100644
--- a/testcases/open_posix_testsuite/functional/threads/include/pitest.h
+++ b/testcases/open_posix_testsuite/functional/threads/include/pitest.h
@@ -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;
+}
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c
index adcfb0c..2c65ad9 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c
@@ -50,40 +50,6 @@ struct thread_param tp[] = {
 	7, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
 };
 
-void *thread_sample(void *arg)
-{
-	char buffer[1024];
-	struct timespec ts;
-	double period = 250;
-	double newtime;
-	size_t size;
-	int i;
-	int rc;
-
-	test_set_priority(pthread_self(), SCHED_FIFO, 5);
-	DPRINTF(stderr, "Thread Sampler: started \n");
-	DPRINTF(stdout, "# COLUMNS %d Time TL TP ", 2 + cpus);
-	for (i = 0; i < (cpus - 1); i++)
-		DPRINTF(stdout, "TF%d ", i);
-	DPRINTF(stdout, "\n");
-	ts.tv_sec = 0;
-	ts.tv_nsec = period * 1000 * 1000;
-	while (!ts_stop) {
-		newtime = seconds_read();
-		size = snprintf(buffer, 1023, "%f ", newtime - base_time);
-		for (i = 0; i < cpus + 1; 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;
-}
-
 void *thread_tb(void *arg)
 {
 	struct timespec boost_time;
@@ -122,6 +88,7 @@ int main(int argc, char **argv)
 	pthread_attr_t threadattr;
 	pthread_t threads[cpus - 1], threadsample, threadtp, threadtl, threadtb;
 	struct tl_param tlp;
+	struct sample_param sp;
 	int multiplier = 1;
 	int i;
 	int rc;
@@ -139,7 +106,11 @@ int main(int argc, char **argv)
 
 	/* Start the sample thread */
 	DPRINTF(stderr, "Main Thread: start sample thread \n");
-	rc = pthread_create(&threadsample, &threadattr, thread_sample, NULL);
+	sp.tp = tp;
+	sp.tplen = 2 + cpus - 1;
+	sp.period = 250;
+	sp.priority = 5;
+	rc = pthread_create(&threadsample, &threadattr, thread_sample, &sp);
 	if (rc != 0) {
 		EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc));
 		exit(UNRESOLVED);
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c
index bd3ae84..e1bbe43 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c
@@ -53,40 +53,6 @@ struct thread_param tp[] = {
 	8, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
 };
 
-void *thread_sample(void *arg)
-{
-	char buffer[1024];
-	struct timespec ts;
-	double period = 300;
-	double newtime;
-	size_t size;
-	int i;
-	int rc;
-
-	test_set_priority(pthread_self(), SCHED_FIFO, 7);
-	DPRINTF(stderr, "Thread Sampler: started \n");
-	DPRINTF(stdout, "# COLUMNS %d Time TL TP1 TP2 ", 3 + cpus);
-	for (i = 0; i < (cpus - 1); i++)
-		DPRINTF(stdout, "TF%d ", i);
-	DPRINTF(stdout, "\n");
-	ts.tv_sec = 0;
-	ts.tv_nsec = period * 1000 * 1000;
-	while (!ts_stop) {
-		newtime = seconds_read();
-		size = snprintf(buffer, 1023, "%f ", newtime - base_time);
-		for (i = 0; i < cpus + 2; 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;
-}
-
 void *thread_tb1(void *arg)
 {
 	struct timespec boost_time;
@@ -151,6 +117,7 @@ int main(int argc, char **argv)
 	pthread_t threads[cpus - 1];
 	pthread_t threadsample, threadtp, threadtl, threadtb1, threadtb2;
 	struct tl_param tlp;
+	struct sample_param sp;
 	time_t multiplier = 1;
 	int i;
 	int rc;
@@ -168,7 +135,11 @@ int main(int argc, char **argv)
 
 	/* Start the sample thread */
 	DPRINTF(stderr, "Main Thread: Creating sample thread \n");
-	rc = pthread_create(&threadsample, &threadattr, thread_sample, NULL);
+	sp.tp = tp;
+	sp.tplen = 3 + cpus - 1;
+	sp.period = 300;
+	sp.priority = 7;
+	rc = pthread_create(&threadsample, &threadattr, thread_sample, &sp);
 	if (rc != 0) {
 		EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc));
 		exit(UNRESOLVED);
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c
index 4e42259..809fbb8 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c
@@ -54,43 +54,6 @@ struct thread_param tp[] = {
 	7, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
 };
 
-void *thread_sample(void *arg)
-{
-	char buffer[1024];
-	struct timespec ts;
-	double period = 300;
-	double newtime;
-	size_t size;
-	int i;
-	int rc;
-
-	test_set_priority(pthread_self(), SCHED_FIFO, 7);
-	DPRINTF(stderr, "Thread Sampler: started\n");
-
-	DPRINTF(stdout, "# COLUMNS %d Time TL TP1 TP2  ", 3 + cpus);
-
-	for (i = 0; i < (cpus - 1); i++)
-		DPRINTF(stdout, "TF%d ", i);
-	DPRINTF(stdout, "\n");
-
-	ts.tv_sec = 0;
-	ts.tv_nsec = period * 1000 * 1000;
-	while (!ts_stop) {
-		newtime = seconds_read();
-		size = snprintf(buffer, 1023, "%f ", newtime - base_time);
-		for (i = 0; i < cpus + 2; 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;
-}
-
 void *thread_tb1(void *arg)
 {
 	struct timespec boost_time;
@@ -155,6 +118,7 @@ int main(int argc, char **argv)
 	pthread_t threads[cpus - 1];
 	pthread_t threadsample, threadtp, threadtl, threadtb1, threadtb2;
 	struct tl_param tlp;
+	struct sample_param sp;
 	time_t multiplier = 1;
 	int i;
 	int rc;
@@ -173,7 +137,11 @@ int main(int argc, char **argv)
 
 	/* Start the sample thread */
 	DPRINTF(stderr, "Main Thread: Creating sample thread\n");
-	rc = pthread_create(&threadsample, &threadattr, thread_sample, NULL);
+	sp.tp = tp;
+	sp.tplen = 3 + cpus - 1;
+	sp.period = 300;
+	sp.priority = 7;
+	rc = pthread_create(&threadsample, &threadattr, thread_sample, &sp);
 	if (rc != 0) {
 		EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc));
 		exit(UNRESOLVED);
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c
index 84ad62c..13c34ef 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c
@@ -98,40 +98,6 @@ void *thread_fn4(void *param)
 	return NULL;
 }
 
-void *thread_sample(void *arg)
-{
-	char buffer[1024];
-	struct timespec ts;
-	double period = 300;
-	double newtime;
-	size_t size;
-	int i;
-	int rc;
-
-	test_set_priority(pthread_self(), SCHED_FIFO, 6);
-	DPRINTF(stderr, "Thread Sampler: started\n");
-	DPRINTF(stdout, "# COLUMNS %d Time TL TP ", 2 + cpus);
-	for (i = 0; i < (cpus - 1); i++)
-		DPRINTF(stdout, "TF%d ", i);
-	DPRINTF(stdout, "\n");
-	ts.tv_sec = 0;
-	ts.tv_nsec = period * 1000 * 1000;
-	while (!ts_stop) {
-		newtime = seconds_read();
-		size = snprintf(buffer, 1023, "%f ", newtime - base_time);
-		for (i = 0; i < cpus + 1; 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;
-}
-
 void *thread_tb1(void *arg)
 {
 	struct timespec boost_time;
@@ -199,7 +165,7 @@ int main(int argc, char **argv)
 	pthread_attr_t threadattr;
 	pthread_t threads[cpus - 1];
 	pthread_t threadsample, threadtp, threadtl, threadtb1, threadtb2;
-
+	struct sample_param sp;
 	time_t multiplier = 1;
 	int i;
 	int rc;
@@ -218,7 +184,11 @@ int main(int argc, char **argv)
 
 	/* Start the sample thread */
 	DPRINTF(stderr, "Main Thread: Creating sample thread\n");
-	rc = pthread_create(&threadsample, &threadattr, thread_sample, NULL);
+	sp.tp = tp;
+	sp.tplen = 2 + cpus - 1;
+	sp.period = 300;
+	sp.priority = 6;
+	rc = pthread_create(&threadsample, &threadattr, thread_sample, &sp);
 	if (rc != 0) {
 		EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc));
 		exit(UNRESOLVED);
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c
index 44c1772..d180e1a 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c
@@ -97,43 +97,6 @@ void *thread_tl5(void *param)
 	return NULL;
 }
 
-void *thread_sample(void *arg)
-{
-	char buffer[1024];
-	struct timespec ts;
-	double period = 300;
-	size_t size;
-	int i;
-	int rc;
-
-	test_set_priority(pthread_self(), SCHED_FIFO, 5);
-
-	DPRINTF(stderr, "Thread Sampler: started\n");
-	DPRINTF(stdout, "# COLUMNS %d Time TL TP ", 2 + cpus);
-
-	for (i = 0; i < (cpus - 1); i++)
-		DPRINTF(stdout, "TF%d ", i);
-	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 < cpus + 1; 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;
-}
-
 void *thread_tb(void *arg)
 {
 	int rc;
@@ -175,6 +138,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 sample_param sp;
 	time_t multiplier = 1;
 	int i;
 	int rc;
@@ -192,7 +156,11 @@ int main(int argc, char **argv)
 
 	/* Start the sample thread */
 	DPRINTF(stderr, "Main Thread: Creating sample thread\n");
-	rc = pthread_create(&threadsample, &threadattr, thread_sample, NULL);
+	sp.tp = tp;
+	sp.tplen = 2 + cpus - 1;
+	sp.period = 300;
+	sp.priority = 5;
+	rc = pthread_create(&threadsample, &threadattr, thread_sample, &sp);
 	if (rc != 0) {
 		EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc));
 		exit(UNRESOLVED);
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c
index e1a56b3..1789a61 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c
@@ -50,41 +50,6 @@ struct thread_param tp[] = {
 	7, 0, 0, 3, SCHED_FIFO, "TF", 6, 0, 0, 0}
 };
 
-
-void *thread_sample(void *arg)
-{
-	char buffer[1024];
-	struct timespec ts;
-	double period = 250;
-	size_t size;
-	int i;
-	int rc;
-
-	test_set_priority(pthread_self(), SCHED_FIFO, 5);
-	DPRINTF(stderr, "Thread Sampler: started\n");
-	DPRINTF(stdout, "# COLUMNS %d Time TP TL ", 2 + cpus);
-	for (i = 0; i < (cpus - 1); i++)
-		DPRINTF(stdout, "TF%d ", i);
-	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 < cpus + 1; 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;
-}
-
 void *thread_tb(void *arg)
 {
 	struct timespec boost_time;
@@ -123,6 +88,7 @@ int main(int argc, char **argv)
 	pthread_attr_t threadattr;
 	pthread_t threads[cpus - 1], threadsample, threadtp, threadtl, threadtb;
 	struct tl_param tlp;
+	struct sample_param sp;
 	time_t multiplier = 1;
 	int i;
 	int rc;
@@ -140,7 +106,11 @@ int main(int argc, char **argv)
 
 	/* Start the sample thread */
 	DPRINTF(stderr, "Main Thread: Creating sample thread\n");
-	rc = pthread_create(&threadsample, &threadattr, thread_sample, NULL);
+	sp.tp = tp;
+	sp.tplen = 2 + cpus - 1;
+	sp.period = 250;
+	sp.priority = 5;
+	rc = pthread_create(&threadsample, &threadattr, thread_sample, &sp);
 	if (rc != 0) {
 		EPRINTF("UNRESOLVED: pthread_create: %d %s", rc, strerror(rc));
 		exit(UNRESOLVED);
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [LTP] [PATCH 5/7] pi_test: thread_sample: do not use the buffer explicitly
  2015-12-18 14:35 [LTP] pi_test: support more CPU configurations Stanislav Kholmanskikh
                   ` (3 preceding siblings ...)
  2015-12-18 14:35 ` [LTP] [PATCH 4/7] pi_test: move thread_sample() " Stanislav Kholmanskikh
@ 2015-12-18 14:35 ` Stanislav Kholmanskikh
  2015-12-18 14:35 ` [LTP] [PATCH 6/7] pi_test: remove unused arguments from main() Stanislav Kholmanskikh
  2015-12-18 14:35 ` [LTP] [PATCH 7/7] pi_test: use only available CPUs Stanislav Kholmanskikh
  6 siblings, 0 replies; 10+ messages in thread
From: Stanislav Kholmanskikh @ 2015-12-18 14:35 UTC (permalink / raw)
  To: ltp

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.

Signed-off-by: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
---
 .../functional/threads/include/pitest.h            |   26 ++++++++++++++-----
 1 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/testcases/open_posix_testsuite/functional/threads/include/pitest.h b/testcases/open_posix_testsuite/functional/threads/include/pitest.h
index 41c8096..8c719e5 100644
--- a/testcases/open_posix_testsuite/functional/threads/include/pitest.h
+++ b/testcases/open_posix_testsuite/functional/threads/include/pitest.h
@@ -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;
 }
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [LTP] [PATCH 6/7] pi_test: remove unused arguments from main()
  2015-12-18 14:35 [LTP] pi_test: support more CPU configurations Stanislav Kholmanskikh
                   ` (4 preceding siblings ...)
  2015-12-18 14:35 ` [LTP] [PATCH 5/7] pi_test: thread_sample: do not use the buffer explicitly Stanislav Kholmanskikh
@ 2015-12-18 14:35 ` Stanislav Kholmanskikh
  2015-12-18 14:35 ` [LTP] [PATCH 7/7] pi_test: use only available CPUs Stanislav Kholmanskikh
  6 siblings, 0 replies; 10+ messages in thread
From: Stanislav Kholmanskikh @ 2015-12-18 14:35 UTC (permalink / raw)
  To: ltp

Signed-off-by: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
---
 .../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(-)

diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c
index 2c65ad9..3b4b360 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c
@@ -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;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c
index e1bbe43..76716f4 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c
@@ -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;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c
index 809fbb8..17cc43d 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c
@@ -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;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c
index 13c34ef..84f9878 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c
@@ -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;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c
index d180e1a..3ba3a94 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c
@@ -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;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c
index 1789a61..46b36f7 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c
@@ -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;
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [LTP] [PATCH 7/7] pi_test: use only available CPUs
  2015-12-18 14:35 [LTP] pi_test: support more CPU configurations Stanislav Kholmanskikh
                   ` (5 preceding siblings ...)
  2015-12-18 14:35 ` [LTP] [PATCH 6/7] pi_test: remove unused arguments from main() Stanislav Kholmanskikh
@ 2015-12-18 14:35 ` Stanislav Kholmanskikh
  2015-12-25 16:18   ` [LTP] [PATCH V2 " Stanislav Kholmanskikh
  2016-01-27 15:35   ` [LTP] [PATCH " Cyril Hrubis
  6 siblings, 2 replies; 10+ messages in thread
From: Stanislav Kholmanskikh @ 2015-12-18 14:35 UTC (permalink / raw)
  To: ltp

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.

Signed-off-by: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
---
 .../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(-)

diff --git a/testcases/open_posix_testsuite/functional/threads/include/pitest.h b/testcases/open_posix_testsuite/functional/threads/include/pitest.h
index 8c719e5..0ad2c7f 100644
--- a/testcases/open_posix_testsuite/functional/threads/include/pitest.h
+++ b/testcases/open_posix_testsuite/functional/threads/include/pitest.h
@@ -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);
 
 
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c
index 3b4b360..2b8a372 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c
@@ -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;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c
index 76716f4..d4c9852 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c
@@ -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;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c
index 17cc43d..50912a5 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c
@@ -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;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c
index 84f9878..7669536 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c
@@ -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;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c
index 3ba3a94..0320095 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c
@@ -39,34 +39,21 @@
 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}
 };
+struct thread_param *tp;
 
 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 +124,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 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);
@@ -157,7 +148,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 +157,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 +207,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;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c
index 46b36f7..bbb3010 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c
@@ -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;
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [LTP] [PATCH V2 7/7] pi_test: use only available CPUs
  2015-12-18 14:35 ` [LTP] [PATCH 7/7] pi_test: use only available CPUs Stanislav Kholmanskikh
@ 2015-12-25 16:18   ` Stanislav Kholmanskikh
  2016-01-27 15:35   ` [LTP] [PATCH " Cyril Hrubis
  1 sibling, 0 replies; 10+ messages in thread
From: Stanislav Kholmanskikh @ 2015-12-25 16:18 UTC (permalink / raw)
  To: ltp

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.

Signed-off-by: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
---
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(-)

diff --git a/testcases/open_posix_testsuite/functional/threads/include/pitest.h b/testcases/open_posix_testsuite/functional/threads/include/pitest.h
index 8c719e5..0dd7d67 100644
--- a/testcases/open_posix_testsuite/functional/threads/include/pitest.h
+++ b/testcases/open_posix_testsuite/functional/threads/include/pitest.h
@@ -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);
 
 
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c
index 3b4b360..cb0290c 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-1.c
@@ -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;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c
index 76716f4..a4cea52 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-2.c
@@ -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;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c
index 17cc43d..aa44c55 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-3.c
@@ -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;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c
index 84f9878..4e8cbed 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-4.c
@@ -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;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c
index 3ba3a94..4966ad9 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-5.c
@@ -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;
diff --git a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c
index 46b36f7..44936c5 100644
--- a/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c
+++ b/testcases/open_posix_testsuite/functional/threads/pi_test/pitest-6.c
@@ -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;
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [LTP] [PATCH 7/7] pi_test: use only available CPUs
  2015-12-18 14:35 ` [LTP] [PATCH 7/7] pi_test: use only available CPUs Stanislav Kholmanskikh
  2015-12-25 16:18   ` [LTP] [PATCH V2 " Stanislav Kholmanskikh
@ 2016-01-27 15:35   ` Cyril Hrubis
  1 sibling, 0 replies; 10+ messages in thread
From: Cyril Hrubis @ 2016-01-27 15:35 UTC (permalink / raw)
  To: ltp

Hi!
> +#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);
> +	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.

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2016-01-27 15:35 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-12-18 14:35 [LTP] pi_test: support more CPU configurations Stanislav Kholmanskikh
2015-12-18 14:35 ` [LTP] [PATCH 1/7] pi_test: move duplicated stuff to the header Stanislav Kholmanskikh
2015-12-18 14:35 ` [LTP] [PATCH 2/7] pi_test: move thread_fn() " Stanislav Kholmanskikh
2015-12-18 14:35 ` [LTP] [PATCH 3/7] pi_test: move thread_tl() " Stanislav Kholmanskikh
2015-12-18 14:35 ` [LTP] [PATCH 4/7] pi_test: move thread_sample() " Stanislav Kholmanskikh
2015-12-18 14:35 ` [LTP] [PATCH 5/7] pi_test: thread_sample: do not use the buffer explicitly Stanislav Kholmanskikh
2015-12-18 14:35 ` [LTP] [PATCH 6/7] pi_test: remove unused arguments from main() Stanislav Kholmanskikh
2015-12-18 14:35 ` [LTP] [PATCH 7/7] pi_test: use only available CPUs Stanislav Kholmanskikh
2015-12-25 16:18   ` [LTP] [PATCH V2 " Stanislav Kholmanskikh
2016-01-27 15:35   ` [LTP] [PATCH " Cyril Hrubis

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.