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


Jennifer Averett
On-Line Applications Research
From 16ea3cc81f9681af0c3102ef0d43720b4295cb6c Mon Sep 17 00:00:00 2001
From: Jennifer Averett <jennifer.aver...@oarcorp.com>
Date: Thu, 6 Feb 2014 12:59:08 -0600
Subject: [PATCH 09/15] posix: Add pthread attribute methods to get/set
 affinity.

---
 cpukit/posix/Makefile.am                    |  6 +++++
 cpukit/posix/src/pthreadattrgetaffinitynp.c | 32 ++++++++++++++++++++++++
 cpukit/posix/src/pthreadattrsetaffinitynp.c | 38 +++++++++++++++++++++++++++++
 3 files changed, 76 insertions(+)
 create mode 100644 cpukit/posix/src/pthreadattrgetaffinitynp.c
 create mode 100644 cpukit/posix/src/pthreadattrsetaffinitynp.c

diff --git a/cpukit/posix/Makefile.am b/cpukit/posix/Makefile.am
index e3228b6..70027d3 100644
--- a/cpukit/posix/Makefile.am
+++ b/cpukit/posix/Makefile.am
@@ -143,6 +143,12 @@ libposix_a_SOURCES += src/pthreadatfork.c src/pthreadattrdestroy.c \
 ## RTEMS specific support methods
 libposix_a_SOURCES += src/pthreadattrcompare.c 
 
+if HAS_SMP
+## PTHREAD_AFFINITY_C_FILES 
+libposix_a_SOURCES += src/pthreadattrsetaffinitynp.c \
+    src/pthreadattrgetaffinitynp.c
+endif
+
 ## PSIGNAL_C_FILES
 libposix_a_SOURCES += src/psignal.c src/alarm.c src/kill.c src/killinfo.c \
     src/kill_r.c src/pause.c src/psignalclearprocesssignals.c \
diff --git a/cpukit/posix/src/pthreadattrgetaffinitynp.c b/cpukit/posix/src/pthreadattrgetaffinitynp.c
new file mode 100644
index 0000000..425b575
--- /dev/null
+++ b/cpukit/posix/src/pthreadattrgetaffinitynp.c
@@ -0,0 +1,32 @@
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#if HAVE_DECL_PTHREAD_ATTR_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_attr_getaffinity_np(
+  const pthread_attr_t *attr,
+  size_t                cpusetsize,
+  cpu_set_t            *cpuset
+)
+{
+  if ( !cpuset )
+    return EFAULT;
+  if ( !attr )
+    return EFAULT;
+
+  if ( cpusetsize != attr->affinitysetsize)
+    return EINVAL;
+
+  CPU_COPY( cpuset, attr->affinityset );
+  return 0;
+}
+#endif
diff --git a/cpukit/posix/src/pthreadattrsetaffinitynp.c b/cpukit/posix/src/pthreadattrsetaffinitynp.c
new file mode 100644
index 0000000..8b4fb07
--- /dev/null
+++ b/cpukit/posix/src/pthreadattrsetaffinitynp.c
@@ -0,0 +1,38 @@
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#if HAVE_DECL_PTHREAD_ATTR_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_attr_setaffinity_np(
+  pthread_attr_t    *attr,
+  size_t             cpusetsize,
+  const cpu_set_t   *cpuset
+)
+{
+  int error;
+
+  if ( !cpuset )
+    return EFAULT;
+  if ( !attr )
+    return EFAULT;
+
+  error = _CPU_set_Is_valid( cpuset, cpusetsize );
+  if ( error != 0 )
+    return EINVAL;
+ 
+  CPU_COPY( attr->affinityset, cpuset );
+
+  return 0;
+}
+
+#endif
-- 
1.8.1.4

From 7a2dfbf6a53fd5beadb802a838e5237593c0f2cf Mon Sep 17 00:00:00 2001
From: Jennifer Averett <jennifer.aver...@oarcorp.com>
Date: Wed, 26 Feb 2014 09:41:04 -0600
Subject: [PATCH 12/15] smptests: Add smppsxaffinity01

---
 testsuites/smptests/Makefile.am                    |   1 +
 testsuites/smptests/configure.ac                   |   1 +
 testsuites/smptests/smppsxaffinity01/Makefile.am   |  23 +++
 testsuites/smptests/smppsxaffinity01/init.c        | 174 +++++++++++++++++++++
 .../smptests/smppsxaffinity01/smppsxaffinity01.doc |  22 +++
 .../smptests/smppsxaffinity01/smppsxaffinity01.scn |  11 ++
 6 files changed, 232 insertions(+)
 create mode 100644 testsuites/smptests/smppsxaffinity01/Makefile.am
 create mode 100644 testsuites/smptests/smppsxaffinity01/init.c
 create mode 100644 testsuites/smptests/smppsxaffinity01/smppsxaffinity01.doc
 create mode 100644 testsuites/smptests/smppsxaffinity01/smppsxaffinity01.scn

diff --git a/testsuites/smptests/Makefile.am b/testsuites/smptests/Makefile.am
index 19dd1e7..d75867a 100644
--- a/testsuites/smptests/Makefile.am
+++ b/testsuites/smptests/Makefile.am
@@ -19,6 +19,7 @@ SUBDIRS += smpsignal01
 SUBDIRS += smpswitchextension01
 SUBDIRS += smpunsupported01
 if HAS_POSIX
+SUBDIRS += smppsxaffinity01
 SUBDIRS += smppsxsignal01
 endif
 endif
diff --git a/testsuites/smptests/configure.ac b/testsuites/smptests/configure.ac
index 29f963e..ef53071 100644
--- a/testsuites/smptests/configure.ac
+++ b/testsuites/smptests/configure.ac
@@ -68,6 +68,7 @@ smpaffinity01/Makefile
 smpatomic01/Makefile
 smplock01/Makefile
 smpmigration01/Makefile
+smppsxaffinity01/Makefile
 smppsxsignal01/Makefile
 smpschedule01/Makefile
 smpsignal01/Makefile
diff --git a/testsuites/smptests/smppsxaffinity01/Makefile.am b/testsuites/smptests/smppsxaffinity01/Makefile.am
new file mode 100644
index 0000000..1788923
--- /dev/null
+++ b/testsuites/smptests/smppsxaffinity01/Makefile.am
@@ -0,0 +1,23 @@
+
+rtems_tests_PROGRAMS = smppsxaffinity01
+smppsxaffinity01_SOURCES = init.c
+
+dist_rtems_tests_DATA = smppsxaffinity01.scn
+dist_rtems_tests_DATA += smppsxaffinity01.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)/include
+AM_CPPFLAGS += -I$(top_srcdir)/../support/include
+
+LINK_OBJS = $(smppsxaffinity01_OBJECTS)
+LINK_LIBS = $(smppsxaffinity01_LDLIBS)
+
+smppsxaffinity01$(EXEEXT): $(smppsxaffinity01_OBJECTS) $(smppsxaffinity01_DEPENDENCIES)
+	@rm -f smppsxaffinity01$(EXEEXT)
+	$(make-exe)
+
+include $(top_srcdir)/../automake/local.am
diff --git a/testsuites/smptests/smppsxaffinity01/init.c b/testsuites/smptests/smppsxaffinity01/init.c
new file mode 100644
index 0000000..525b5f7
--- /dev/null
+++ b/testsuites/smptests/smppsxaffinity01/init.c
@@ -0,0 +1,174 @@
+/*
+ *  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.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#define  _GNU_SOURCE
+
+#include <tmacros.h>
+#include <errno.h>
+#include <pthread.h>
+#include <sched.h>
+
+#if HAVE_DECL_PTHREAD_GETAFFINITY_NP
+
+#define CPU_COUNT 4
+
+pthread_t           Init_id;
+
+/* forward declarations to avoid warnings */
+void *POSIX_Init(void *argument);
+void Validate_attrgetaffinity_errors(void);
+void Validate_attrsetaffinity_errors(void);
+void Validate_attr(void);
+
+void Validate_attrgetaffinity_errors(void) 
+{
+  int                 sc;
+  cpu_set_t           cpuset;
+  pthread_attr_t      attr;
+
+  /* Verify pthread_attr_getaffinity_np validates attr  */
+  puts( "Init - pthread_attr_getaffinity_np - Invalid attr - EFAULT" );
+  sc = pthread_attr_getaffinity_np( NULL, sizeof(cpu_set_t), &cpuset );
+  rtems_test_assert( sc == EFAULT );
+
+  /* Verify pthread_attr_getaffinity_np validates cpuset */
+  puts( "Init - pthread_attr_getaffinity_np - Invalid attr - EFAULT" );
+  sc = pthread_attr_getaffinity_np( &attr, sizeof(cpu_set_t), NULL );
+  rtems_test_assert( sc == EFAULT );
+
+  /* Verify pthread_attr_getaffinity_np validates cpusetsize */
+  puts( "Init - pthread_attr_getaffinity_np - Invalid cpusetsize - EINVAL" );
+  sc = pthread_attr_getaffinity_np( &attr, sizeof(cpu_set_t) * 2 , &cpuset );
+  rtems_test_assert( sc == EINVAL );
+
+}
+
+void Validate_attrsetaffinity_errors(void) 
+{
+  int                 sc;
+  cpu_set_t           cpuset;
+  pthread_attr_t      attr;
+
+  /* Verify pthread_attr_setaffinity_np validates attr.  */
+  puts( "Init - pthread_attr_setaffinity_np - Invalid attr - EFAULT" );
+  sc = pthread_attr_setaffinity_np( NULL, sizeof(cpu_set_t), &cpuset );
+  rtems_test_assert( sc == EFAULT );
+
+  /* Verify pthread_attr_setaffinity_np validates cpuset    */
+  puts( "Init - pthread_attr_setaffinity_np - Invalid attr - EFAULT" );
+  sc = pthread_attr_setaffinity_np( &attr, sizeof(cpu_set_t), NULL );
+  rtems_test_assert( sc == EFAULT );
+
+  /* Verify pthread_attr_setaffinity_np validates cpusetsize */
+  puts( "Init - pthread_attr_setaffinity_np - Invalid cpusetsize - EINVAL" );
+  sc = pthread_attr_setaffinity_np( &attr, sizeof(cpu_set_t) * 2 , &cpuset );
+  rtems_test_assert( sc == EINVAL );
+
+  /* Verify pthread_attr_setaffinity_np validates cpuset greater than 0  */
+  CPU_ZERO(&cpuset);
+  puts( "Init - pthread_attr_setaffinity_np - No cpus in cpuset - EINVAL" );
+  sc = pthread_attr_setaffinity_np( &attr, sizeof(cpu_set_t) , &cpuset );
+  rtems_test_assert( sc == EINVAL );
+
+  /* Verify pthread_attr_setaffinity_np validates invalid cpu in cpuset */
+  CPU_FILL(&cpuset);
+  puts( "Init - pthread_attr_setaffinity_np - Too many cpus in cpuset - EINVAL" );
+  sc = pthread_attr_setaffinity_np( &attr, sizeof(cpu_set_t) , &cpuset );
+  rtems_test_assert( sc == EINVAL );
+}
+
+void Validate_attr(void )
+{
+  int                 sc;
+  pthread_attr_t      attr;
+  uint32_t            cpus;
+  cpu_set_t           cpuset1;
+  cpu_set_t           cpuset2;
+  int                 i;
+  int                 priority;
+
+  sc = pthread_getattr_np( Init_id, &attr );
+  rtems_test_assert( sc == 0 );
+
+  priority = sched_get_priority_min( SCHED_FIFO );
+  rtems_test_assert( priority != -1 );
+
+
+  cpus = rtems_smp_get_processor_count();
+  puts( 
+    "Init - Validate pthread_attr_setaffinity_np and "
+    "pthread_attr_getaffinity_np"
+  );
+  
+  /* Set each cpu seperately in the affinity set */
+  for ( i=0; i<cpus; i++ ){
+    CPU_ZERO(&cpuset1);
+    CPU_SET(i, &cpuset1);
+
+    sc = pthread_attr_setaffinity_np( &attr, sizeof(cpu_set_t), &cpuset1 );
+    rtems_test_assert( sc == 0 );
+
+    sc = pthread_attr_getaffinity_np( &attr, sizeof(cpu_set_t), &cpuset2 );
+    rtems_test_assert( sc == 0 );
+    
+    rtems_test_assert( CPU_EQUAL(&cpuset1, &cpuset2) );
+  }
+}
+
+void *POSIX_Init(
+  void *ignored
+)
+{
+  puts( "\n\n*** SMP POSIX AFFINITY ATTRIBUTE TEST 1 ***" );
+
+  /* Initialize thread id */ 
+  Init_id = pthread_self();
+
+  Validate_attrsetaffinity_errors();
+  Validate_attrgetaffinity_errors();
+  Validate_attr();
+ 
+  puts( "*** END OF SMP POSIX AFFINITY ATTRIBUTE TEST 1 ***" );
+  rtems_test_exit(0);
+}
+
+#else
+void *POSIX_Init(
+  void *ignored
+)
+{
+  puts( "\n\n*** SMP POSIX AFFINITY ATTRIBUTE TEST 1 ***" );
+  puts( " POSIX Affinity Methods NOT Supported");
+  puts( "*** END OF SMP POSIX AFFINITY ATTRIBUTE TEST 1 ***" );
+  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 CPU_COUNT
+
+
+#define CONFIGURE_MAXIMUM_POSIX_THREADS  1
+
+#define CONFIGURE_POSIX_INIT_THREAD_TABLE
+
+#define CONFIGURE_INIT
+#include <rtems/confdefs.h>
+
+/* global variables */
diff --git a/testsuites/smptests/smppsxaffinity01/smppsxaffinity01.doc b/testsuites/smptests/smppsxaffinity01/smppsxaffinity01.doc
new file mode 100644
index 0000000..b6333c4
--- /dev/null
+++ b/testsuites/smptests/smppsxaffinity01/smppsxaffinity01.doc
@@ -0,0 +1,22 @@
+#  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.
+#
+
+This file describes the directives and concepts tested by this test set.
+
+test set name:  psxpsxaffinity01
+
+directives:
+
+  pthread_attr_getaffinity_np
+  pthread_attr_setaffinity_np
+  
+concepts:
+
++ Verify error conditions and functionality of pthread_attr_getaffinity_np
+
++ Verify error conditions and functionality of pthread_attr_setaffinity_np
diff --git a/testsuites/smptests/smppsxaffinity01/smppsxaffinity01.scn b/testsuites/smptests/smppsxaffinity01/smppsxaffinity01.scn
new file mode 100644
index 0000000..70bd6d3
--- /dev/null
+++ b/testsuites/smptests/smppsxaffinity01/smppsxaffinity01.scn
@@ -0,0 +1,11 @@
+*** SMP POSIX AFFINITY ATTRIBUTE TEST 1 ***
+Init - pthread_attr_setaffinity_np - Invalid attr - EFAULT
+Init - pthread_attr_setaffinity_np - Invalid attr - EFAULT
+Init - pthread_attr_setaffinity_np - Invalid cpusetsize - EINVAL
+Init - pthread_attr_setaffinity_np - No cpus in cpuset - EINVAL
+Init - pthread_attr_setaffinity_np - Too many cpus in cpuset - EINVAL
+Init - pthread_attr_getaffinity_np - Invalid attr - EFAULT
+Init - pthread_attr_getaffinity_np - Invalid attr - EFAULT
+Init - pthread_attr_getaffinity_np - Invalid cpusetsize - EINVAL
+Init - Validate pthread_attr_setaffinity_np and pthread_attr_getaffinity_np
+*** END OF SMP POSIX AFFINITY ATTRIBUTE TEST 1 ***
-- 
1.8.1.4

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

Reply via email to