Attached are patches for pthread get and set
affinity, as well as a test for the new functions.

Jennifer Averett
On-Line Applications Research
From c5d1bae56e5720232799844a76a444abad9008e3 Mon Sep 17 00:00:00 2001
From: Jennifer Averett <jennifer.aver...@oarcorp.com>
Date: Mon, 10 Feb 2014 10:01:31 -0600
Subject: [PATCH 10/15] posix: Add pthread get and set affinity.

---
 cpukit/posix/Makefile.am                |  3 +-
 cpukit/posix/src/pthreadgetaffinitynp.c | 51 +++++++++++++++++++++++++++++++
 cpukit/posix/src/pthreadsetaffinitynp.c | 53 +++++++++++++++++++++++++++++++++
 3 files changed, 106 insertions(+), 1 deletion(-)
 create mode 100644 cpukit/posix/src/pthreadgetaffinitynp.c
 create mode 100644 cpukit/posix/src/pthreadsetaffinitynp.c

diff --git a/cpukit/posix/Makefile.am b/cpukit/posix/Makefile.am
index 70027d3..ccadd63 100644
--- a/cpukit/posix/Makefile.am
+++ b/cpukit/posix/Makefile.am
@@ -146,7 +146,8 @@ libposix_a_SOURCES += src/pthreadattrcompare.c
 if HAS_SMP
 ## PTHREAD_AFFINITY_C_FILES 
 libposix_a_SOURCES += src/pthreadattrsetaffinitynp.c \
-    src/pthreadattrgetaffinitynp.c
+    src/pthreadattrgetaffinitynp.c  src/pthreadgetaffinitynp.c   \
+    src/pthreadsetaffinitynp.c
 endif
 
 ## PSIGNAL_C_FILES
diff --git a/cpukit/posix/src/pthreadgetaffinitynp.c b/cpukit/posix/src/pthreadgetaffinitynp.c
new file mode 100644
index 0000000..d69947b
--- /dev/null
+++ b/cpukit/posix/src/pthreadgetaffinitynp.c
@@ -0,0 +1,51 @@
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#if HAVE_DECL_PTHREAD_GETAFFINITY_NP
+
+#define  _GNU_SOURCE
+#include <pthread.h>
+#include <errno.h>
+
+#include <rtems/posix/pthreadimpl.h>
+#include <rtems/posix/priorityimpl.h>
+#include <rtems/score/threadimpl.h>
+
+int pthread_getaffinity_np(
+  const pthread_t       id,
+  size_t                cpusetsize,
+  cpu_set_t            *cpuset
+)
+{
+  Objects_Locations        location;
+  Thread_Control          *the_thread;
+  int                      error;
+
+  if ( !cpuset )
+    return EFAULT;
+ 
+  the_thread = _Thread_Get( id, &location );
+  switch ( location ) {
+
+    case OBJECTS_LOCAL:
+      error = 0;
+      if ( cpusetsize != the_thread->affinity.setsize )
+        error = EINVAL;
+      else
+        CPU_COPY( cpuset, the_thread->affinity.set );
+
+      _Objects_Put( &the_thread->Object );
+      return error;
+      break;
+
+#if defined(RTEMS_MULTIPROCESSING)
+    case OBJECTS_REMOTE:
+#endif
+    case OBJECTS_ERROR:
+      break;
+  }
+  return ESRCH;
+}
+
+#endif
diff --git a/cpukit/posix/src/pthreadsetaffinitynp.c b/cpukit/posix/src/pthreadsetaffinitynp.c
new file mode 100644
index 0000000..70b1d52
--- /dev/null
+++ b/cpukit/posix/src/pthreadsetaffinitynp.c
@@ -0,0 +1,53 @@
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#if HAVE_DECL_PTHREAD_SETAFFINITY_NP
+
+#define  _GNU_SOURCE
+#include <pthread.h>
+#include <errno.h>
+
+#include <rtems/posix/pthreadimpl.h>
+#include <rtems/posix/priorityimpl.h>
+#include <rtems/score/threadimpl.h>
+#include <rtems/score/cpusetimpl.h>
+
+int pthread_setaffinity_np(
+  pthread_t          id,
+  size_t             cpusetsize,
+  const cpu_set_t   *cpuset)
+{
+  Objects_Locations        location;
+  POSIX_API_Control       *api;
+  Thread_Control          *the_thread;
+  int                      error;
+
+  if ( !cpuset )
+    return EFAULT;
+
+  error = _CPU_set_Is_valid( cpuset, cpusetsize );
+  if ( error != 0 )
+    return EINVAL;
+   
+  the_thread = _Thread_Get( id, &location );
+  switch ( location ) {
+
+    case OBJECTS_LOCAL:
+      api = the_thread->API_Extensions[ THREAD_API_POSIX ];
+      CPU_COPY( the_thread->affinity.set, cpuset );
+      CPU_COPY( api->Attributes.affinityset, cpuset );
+      _Objects_Put( &the_thread->Object );
+      return 0;
+      break;
+
+#if defined(RTEMS_MULTIPROCESSING)
+    case OBJECTS_REMOTE:
+#endif
+    case OBJECTS_ERROR:
+      break;
+  }
+
+  return ESRCH;
+}
+#endif
-- 
1.8.1.4

From cdffde8ec1b06cc5528dfdaa9e2d5b728f40026b Mon Sep 17 00:00:00 2001
From: Jennifer Averett <jennifer.aver...@oarcorp.com>
Date: Wed, 26 Feb 2014 09:57:16 -0600
Subject: [PATCH 13/15] smptests: Add smppsxaffinity02.

---
 testsuites/smptests/Makefile.am                    |   1 +
 testsuites/smptests/configure.ac                   |   1 +
 testsuites/smptests/smppsxaffinity02/Makefile.am   |  19 ++
 testsuites/smptests/smppsxaffinity02/init.c        | 240 +++++++++++++++++++++
 .../smptests/smppsxaffinity02/smppsxaffinity02.doc |  26 +++
 .../smptests/smppsxaffinity02/smppsxaffinity02.scn |  17 ++
 6 files changed, 304 insertions(+)
 create mode 100644 testsuites/smptests/smppsxaffinity02/Makefile.am
 create mode 100644 testsuites/smptests/smppsxaffinity02/init.c
 create mode 100644 testsuites/smptests/smppsxaffinity02/smppsxaffinity02.doc
 create mode 100644 testsuites/smptests/smppsxaffinity02/smppsxaffinity02.scn

diff --git a/testsuites/smptests/Makefile.am b/testsuites/smptests/Makefile.am
index d75867a..ff641af 100644
--- a/testsuites/smptests/Makefile.am
+++ b/testsuites/smptests/Makefile.am
@@ -20,6 +20,7 @@ SUBDIRS += smpswitchextension01
 SUBDIRS += smpunsupported01
 if HAS_POSIX
 SUBDIRS += smppsxaffinity01
+SUBDIRS += smppsxaffinity02
 SUBDIRS += smppsxsignal01
 endif
 endif
diff --git a/testsuites/smptests/configure.ac b/testsuites/smptests/configure.ac
index ef53071..2645b88 100644
--- a/testsuites/smptests/configure.ac
+++ b/testsuites/smptests/configure.ac
@@ -69,6 +69,7 @@ smpatomic01/Makefile
 smplock01/Makefile
 smpmigration01/Makefile
 smppsxaffinity01/Makefile
+smppsxaffinity02/Makefile
 smppsxsignal01/Makefile
 smpschedule01/Makefile
 smpsignal01/Makefile
diff --git a/testsuites/smptests/smppsxaffinity02/Makefile.am b/testsuites/smptests/smppsxaffinity02/Makefile.am
new file mode 100644
index 0000000..c7c160e
--- /dev/null
+++ b/testsuites/smptests/smppsxaffinity02/Makefile.am
@@ -0,0 +1,19 @@
+rtems_tests_PROGRAMS = smppsxaffinity02
+smppsxaffinity02_SOURCES = init.c
+
+dist_rtems_tests_DATA = smppsxaffinity02.scn smppsxaffinity02.doc
+
+include $(RTEMS_ROOT)/make/custom/@RTEMS_BSP@.cfg
+include $(top_srcdir)/../automake/compile.am
+include $(top_srcdir)/../automake/leaf.am
+
+AM_CPPFLAGS += -I$(top_srcdir)/../support/include
+
+LINK_OBJS = $(smppsxaffinity02_OBJECTS)
+LINK_LIBS = $(smppsxaffinity02_LDLIBS)
+
+smppsxaffinity02$(EXEEXT): $(smppsxaffinity02_OBJECTS) $(smppsxaffinity02_DEPENDENCIES)
+	@rm -f smppsxaffinity02$(EXEEXT)
+	$(make-exe)
+
+include $(top_srcdir)/../automake/local.am
diff --git a/testsuites/smptests/smppsxaffinity02/init.c b/testsuites/smptests/smppsxaffinity02/init.c
new file mode 100644
index 0000000..03723e1
--- /dev/null
+++ b/testsuites/smptests/smppsxaffinity02/init.c
@@ -0,0 +1,240 @@
+/*
+ *  COPYRIGHT (c) 1989-2011.
+ *  On-Line Applications Research Corporation (OAR).
+ *
+ *  The license and distribution terms for this file may be
+ *  found in the file LICENSE in this distribution or at
+ *  http://www.rtems.com/license/LICENSE.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#define NUM_CPUS   4
+
+#define  _GNU_SOURCE
+
+#include <tmacros.h>
+#include <errno.h>
+#include <pthread.h>
+#include <sched.h>
+
+#if HAVE_DECL_PTHREAD_GETAFFINITY_NP
+
+pthread_t           Init_id;
+pthread_t           Med_id[NUM_CPUS-1];
+pthread_t           Low_id[NUM_CPUS];
+
+/* forward declarations to avoid warnings */
+void *POSIX_Init(void *argument);
+void Validate_setaffinity_errors(void);
+void Validate_getaffinity_errors(void);
+void Validate_affinity(void);
+void *Thread_1(void *unused);
+
+void *Thread_1(void *unused)
+{
+  while(1);
+}
+
+void Validate_setaffinity_errors(void)
+{
+  int                 sc;
+  cpu_set_t           cpuset;
+
+  /* Verify pthread_setaffinity_np checks that all cpu's exist. */
+  CPU_FILL(&cpuset);
+  puts( "Init - pthread_setaffinity_np - Invalid cpu - EINVAL" );
+  sc = pthread_setaffinity_np( Init_id, sizeof(cpu_set_t), &cpuset ); 
+  rtems_test_assert( sc == EINVAL );
+  
+  /* Verify pthread_setaffinity_np checks that at least one cpu is set */
+  CPU_ZERO(&cpuset);
+  puts( "Init - pthread_setaffinity_np - no cpu - EINVAL" );
+  sc = pthread_setaffinity_np( Init_id, sizeof(cpu_set_t), &cpuset ); 
+  rtems_test_assert( sc == EINVAL );
+
+  /* Verify pthread_setaffinity_np checks that at thread id is valid */
+  CPU_SET(0, &cpuset);
+  puts( "Init - pthread_setaffinity_np - Invalid thread - ESRCH" );
+  sc = pthread_setaffinity_np( 999, sizeof(cpu_set_t), &cpuset );
+  rtems_test_assert( sc == ESRCH );
+  
+  /* Verify pthread_setaffinity_np validates cpusetsize */
+  puts( "Init - pthread_setaffinity_np - Invalid cpusetsize - EINVAL" );
+  sc = pthread_setaffinity_np( Init_id,  sizeof(cpu_set_t) * 2, &cpuset );
+  rtems_test_assert( sc == EINVAL );
+
+  /* Verify pthread_setaffinity_np validates cpuset */
+  puts( "Init - pthread_setaffinity_np - Invalid cpuset - EFAULT" );
+  sc = pthread_setaffinity_np( Init_id, sizeof(cpu_set_t), NULL );
+  rtems_test_assert( sc == EFAULT );
+}
+
+void Validate_getaffinity_errors(void) 
+{
+  int                 sc;
+  cpu_set_t           cpuset;
+
+  /* Verify pthread_getaffinity_np checks that at thread id is valid */
+  CPU_SET(0, &cpuset);
+  puts( "Init - pthread_getaffinity_np - Invalid thread - ESRCH" );
+  sc = pthread_getaffinity_np( 999, sizeof(cpu_set_t), &cpuset );
+  rtems_test_assert( sc == ESRCH );
+  
+  /* Verify pthread_getaffinity_np validates cpusetsize */
+  puts( "Init - pthread_getaffinity_np - Invalid cpusetsize - EINVAL" );
+  sc = pthread_getaffinity_np( Init_id,  sizeof(cpu_set_t) * 2, &cpuset );
+  rtems_test_assert( sc == EINVAL );
+
+  /* Verify pthread_getaffinity_np validates cpuset */
+  puts("Init - pthread_getaffinity_np - Invalid cpuset - EFAULT");
+  sc = pthread_getaffinity_np( Init_id, sizeof(cpu_set_t), NULL );
+  rtems_test_assert( sc == EFAULT );
+}
+
+void Validate_affinity(void )
+{
+  pthread_attr_t       attr;
+  cpu_set_t            cpuset0;
+  cpu_set_t            cpuset1;
+  cpu_set_t            cpuset2;
+  uint32_t             i;
+  int                  sc;
+  int                  cpu_count; 
+  struct sched_param   param;
+ 
+ 
+  puts( "Init - Set Init priority to high");
+  sc = pthread_getattr_np( Init_id, &attr );
+  rtems_test_assert( sc == 0 );
+  sc = pthread_attr_getschedparam( &attr, &param );
+  rtems_test_assert( sc == 0 );
+  param.sched_priority = sched_get_priority_max( SCHED_FIFO );
+  sc = pthread_setschedparam( Init_id, SCHED_FIFO, &param );
+  rtems_test_assert( !sc );
+
+  sc = pthread_getaffinity_np( Init_id, sizeof(cpu_set_t), &cpuset0 );
+  rtems_test_assert( !sc );
+ 
+  /* Get the number of processors that we are using. */
+  cpu_count = rtems_smp_get_processor_count();
+
+  /* Fill the remaining cpus with med priority tasks */
+  puts( "Init - Create Medium priority tasks");
+  for (i=0; i<(cpu_count-1); i++){
+    sc = pthread_create( &Med_id[i], &attr, Thread_1, NULL );
+    rtems_test_assert( !sc );
+  }
+
+  puts( "Init - Verify Medium priority tasks");
+  for (i=0; i<(cpu_count-1); i++){
+    sc = pthread_getaffinity_np( Med_id[i], sizeof(cpu_set_t), &cpuset2 );
+    rtems_test_assert( !sc );
+    rtems_test_assert( CPU_EQUAL(&cpuset0, &cpuset2) );
+  }
+
+  /* 
+   * Create low priority thread for each remaining cpu with the affinity 
+   * set to only run on one cpu.
+   */
+  puts( "Init - Create  Low priority tasks");
+  for (i=0; i<cpu_count; i++){
+    CPU_ZERO(&cpuset1);
+    CPU_SET(i, &cpuset1);
+
+    sc = pthread_attr_setaffinity_np( &attr, sizeof(cpu_set_t), &cpuset1 );
+    rtems_test_assert( !sc );
+
+    sc = pthread_create( &Low_id[i], &attr, Thread_1, NULL );
+    rtems_test_assert( !sc );
+  }
+
+  /* Verify affinity on low priority tasks */
+  puts( "Init - Verify Low priority tasks");
+  for (i=0; i<(cpu_count-1); i++){
+    CPU_ZERO(&cpuset1);
+    CPU_SET(i, &cpuset1);
+
+    sc = pthread_getaffinity_np( Low_id[i], sizeof(cpu_set_t), &cpuset2 );
+    rtems_test_assert( !sc );
+    rtems_test_assert( CPU_EQUAL(&cpuset1, &cpuset2) );
+  }
+
+  /* Change the affinity for each low priority task */
+  puts("Init - Change affinity on Low priority tasks");
+  CPU_COPY(&cpuset1, &cpuset0);
+  for (i=0; i<cpu_count; i++){
+
+    CPU_CLR(i, &cpuset1);
+    sc = pthread_setaffinity_np( Low_id[i], sizeof(cpu_set_t), &cpuset1 );
+
+    /* Verify no cpu's are now set in the cpuset */
+    if (i== (cpu_count-1)) {
+      rtems_test_assert( sc == EINVAL );
+      sc = pthread_setaffinity_np( Low_id[i], sizeof(cpu_set_t), &cpuset0 );
+    }
+    rtems_test_assert( !sc );
+  }
+  
+  puts("Init - Validate affinity on Low priority tasks");
+  CPU_COPY(&cpuset1, &cpuset0);
+  for (i=0; i<cpu_count; i++){
+    CPU_CLR(i, &cpuset1);
+
+    sc = pthread_getaffinity_np( Low_id[i], sizeof(cpu_set_t), &cpuset2 );
+    rtems_test_assert( !sc );
+    if (i== (cpu_count-1))
+      rtems_test_assert( CPU_EQUAL(&cpuset0, &cpuset2) );
+    else
+      rtems_test_assert( CPU_EQUAL(&cpuset1, &cpuset2) );
+  }
+}
+
+void *POSIX_Init(
+  void *ignored
+)
+{
+  puts( "\n\n*** SMP POSIX AFFINITY ATTRIBUTE TEST 2 ***" );
+
+  /* Initialize thread id */ 
+  Init_id = pthread_self();
+  
+  Validate_setaffinity_errors();
+  Validate_getaffinity_errors();
+  Validate_affinity();
+ 
+  puts( "*** SMP POSIX AFFINITY ATTRIBUTE TEST 2 ***" );
+  rtems_test_exit(0);
+}
+
+#else
+void *POSIX_Init(
+  void *ignored
+)
+{
+  puts( "\n\n*** SMP POSIX AFFINITY ATTRIBUTE TEST 2 ***" );
+  puts( " Affinity NOT Supported");
+  puts( "*** END OF SMP POSIX AFFINITY ATTRIBUTE TEST 2 ***" );
+  rtems_test_exit(0);
+}
+
+#endif
+/* configuration information */
+
+#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
+#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
+
+#define CONFIGURE_SMP_APPLICATION
+
+#define CONFIGURE_SMP_MAXIMUM_PROCESSORS NUM_CPUS
+
+#define CONFIGURE_MAXIMUM_POSIX_THREADS (NUM_CPUS*2) 
+
+#define CONFIGURE_POSIX_INIT_THREAD_TABLE
+
+#define CONFIGURE_INIT
+#include <rtems/confdefs.h>
+
+/* global variables */
diff --git a/testsuites/smptests/smppsxaffinity02/smppsxaffinity02.doc b/testsuites/smptests/smppsxaffinity02/smppsxaffinity02.doc
new file mode 100644
index 0000000..ca735fc
--- /dev/null
+++ b/testsuites/smptests/smppsxaffinity02/smppsxaffinity02.doc
@@ -0,0 +1,26 @@
+#  COPYRIGHT (c) 1989-2014.
+#  On-Line Applications Research Corporation (OAR).
+#
+#  The license and distribution terms for this file may be
+#  found in the file LICENSE in this distribution or at
+#  http://www.rtems.com/license/LICENSE.
+#
+
+directives:
+
+This file describes the directives and concepts tested by this test set.
+
+test set name:  psxpsxaffinity02
+
+directives:
+
+  pthread_getaffinity_np
+  pthread_setaffinity_np
+  
+concepts:
+
++ Verify error conditions and functionality of pthread_getaffinity_np
+
++ Verify error conditions and functionality of pthread_setaffinity_np
+
+Note: This does not test scheduling.
diff --git a/testsuites/smptests/smppsxaffinity02/smppsxaffinity02.scn b/testsuites/smptests/smppsxaffinity02/smppsxaffinity02.scn
new file mode 100644
index 0000000..7f3d3f9
--- /dev/null
+++ b/testsuites/smptests/smppsxaffinity02/smppsxaffinity02.scn
@@ -0,0 +1,17 @@
+*** SMP POSIX AFFINITY ATTRIBUTE TEST 2 ***
+Init - pthread_setaffinity_np - Invalid cpu - EINVAL
+Init - pthread_setaffinity_np - no cpu - EINVAL
+Init - pthread_setaffinity_np - Invalid thread - ESRCH
+Init - pthread_setaffinity_np - Invalid cpusetsize - EINVAL
+Init - pthread_setaffinity_np - Invalid cpuset - EFAULT
+Init - pthread_getaffinity_np - Invalid thread - ESRCH
+Init - pthread_getaffinity_np - Invalid cpusetsize - EINVAL
+Init - pthread_getaffinity_np - Invalid cpuset - EFAULT
+Init - Set Init priority to high
+Init - Create Medium priority tasks
+Init - Verify Medium priority tasks
+Init - Create  Low priority tasks
+Init - Verify Medium priority tasks
+Init - Change affinity on Low priority tasks
+Init - Validate affinity on Low priority tasks
+*** SMP POSIX AFFINITY ATTRIBUTE TEST 2 ***
-- 
1.8.1.4

_______________________________________________
rtems-devel mailing list
rtems-devel@rtems.org
http://www.rtems.org/mailman/listinfo/rtems-devel

Reply via email to