Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:43

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  COPYRIGHT (c) 1989-2014.
0005  *  On-Line Applications Research Corporation (OAR).
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions
0009  * are met:
0010  * 1. Redistributions of source code must retain the above copyright
0011  *    notice, this list of conditions and the following disclaimer.
0012  * 2. Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0026  * POSSIBILITY OF SUCH DAMAGE.
0027  */
0028 
0029 #ifdef HAVE_CONFIG_H
0030 #include "config.h"
0031 #endif
0032 
0033 #define  _GNU_SOURCE
0034 
0035 #include <tmacros.h>
0036 #include <errno.h>
0037 #include <pthread.h>
0038 #include <sched.h>
0039 
0040 const char rtems_test_name[] = "SMPPSXAFFINITY 1";
0041 
0042 #define CPU_COUNT 4
0043 
0044 pthread_t           Init_id;
0045 
0046 /* forward declarations to avoid warnings */
0047 void *POSIX_Init(void *argument);
0048 void Validate_attrgetaffinity_errors(void);
0049 void Validate_attrsetaffinity_errors(void);
0050 void Validate_attr(void);
0051 
0052 void Validate_attrgetaffinity_errors(void)
0053 {
0054   int                 sc;
0055   cpu_set_t           cpuset;
0056   pthread_attr_t      attr;
0057 
0058   sc = pthread_attr_init( &attr );
0059   rtems_test_assert( sc == 0 );
0060 
0061   /* Verify pthread_attr_getaffinity_np validates attr  */
0062   puts( "Init - pthread_attr_getaffinity_np - Invalid attr - EINVAL" );
0063   sc = pthread_attr_getaffinity_np( NULL, sizeof( cpuset ), &cpuset );
0064   rtems_test_assert( sc == EINVAL );
0065 
0066   /* Verify pthread_attr_getaffinity_np validates cpuset */
0067   puts( "Init - pthread_attr_getaffinity_np - Invalid attr - EINVAL" );
0068   sc = pthread_attr_getaffinity_np( &attr, sizeof( cpuset ), NULL );
0069   rtems_test_assert( sc == EINVAL );
0070 
0071   /* Verify pthread_attr_getaffinity_np validates cpusetsize */
0072   puts( "Init - pthread_attr_getaffinity_np - Invalid cpusetsize - EINVAL" );
0073   sc = pthread_attr_getaffinity_np( &attr, sizeof( cpuset ) * 2 , &cpuset );
0074   rtems_test_assert( sc == EINVAL );
0075 
0076   sc = pthread_attr_destroy( &attr );
0077   rtems_test_assert( sc == 0 );
0078 
0079   puts( "Init - pthread_attr_getaffinity_np - Not initialized attr - EINVAL" );
0080   sc = pthread_attr_getaffinity_np( &attr, sizeof( cpuset ), &cpuset );
0081   rtems_test_assert( sc == EINVAL );
0082 }
0083 
0084 void Validate_attrsetaffinity_errors(void)
0085 {
0086   int                 sc;
0087   cpu_set_t           cpuset;
0088   pthread_attr_t      attr;
0089 
0090   sc = pthread_attr_init( &attr );
0091   rtems_test_assert( sc == 0 );
0092 
0093   /* Verify pthread_attr_setaffinity_np validates attr.  */
0094   puts( "Init - pthread_attr_setaffinity_np - Invalid attr - EINVAL" );
0095   sc = pthread_attr_setaffinity_np( NULL, sizeof( cpuset ), &cpuset );
0096   rtems_test_assert( sc == EINVAL );
0097 
0098   /* Verify pthread_attr_setaffinity_np validates cpuset    */
0099   puts( "Init - pthread_attr_setaffinity_np - Invalid attr - EINVAL" );
0100   sc = pthread_attr_setaffinity_np( &attr, sizeof( cpuset ), NULL );
0101   rtems_test_assert( sc == EINVAL );
0102 
0103   /* Verify pthread_attr_setaffinity_np validates cpusetsize */
0104   puts( "Init - pthread_attr_setaffinity_np - Invalid cpusetsize - EINVAL" );
0105   sc = pthread_attr_setaffinity_np( &attr, sizeof( cpuset ) * 2 , &cpuset );
0106   rtems_test_assert( sc == EINVAL );
0107 
0108   sc = pthread_attr_destroy( &attr );
0109   rtems_test_assert( sc == 0 );
0110 
0111   puts( "Init - pthread_attr_setaffinity_np - Not initialized attr - EINVAL" );
0112   sc = pthread_attr_setaffinity_np( &attr, sizeof( cpuset ), &cpuset  );
0113   rtems_test_assert( sc == EINVAL );
0114 }
0115 
0116 void Validate_attr(void )
0117 {
0118   int                 sc;
0119   pthread_attr_t      attr;
0120   uint32_t            cpus;
0121   cpu_set_t           cpuset1;
0122   cpu_set_t           cpuset2;
0123   int                 i;
0124   int                 priority;
0125 
0126   sc = pthread_getattr_np( Init_id, &attr );
0127   rtems_test_assert( sc == 0 );
0128 
0129   priority = sched_get_priority_min( SCHED_FIFO );
0130   rtems_test_assert( priority != -1 );
0131 
0132 
0133   cpus = rtems_scheduler_get_processor_maximum();
0134   puts(
0135     "Init - Validate pthread_attr_setaffinity_np and "
0136     "pthread_attr_getaffinity_np"
0137   );
0138 
0139   /* Set each cpu seperately in the affinity set */
0140   for ( i=0; i<cpus; i++ ){
0141     CPU_ZERO(&cpuset1);
0142     CPU_SET(i, &cpuset1);
0143 
0144     sc = pthread_attr_setaffinity_np( &attr, sizeof(cpu_set_t), &cpuset1 );
0145     rtems_test_assert( sc == 0 );
0146 
0147     sc = pthread_attr_getaffinity_np( &attr, sizeof(cpu_set_t), &cpuset2 );
0148     rtems_test_assert( sc == 0 );
0149 
0150     rtems_test_assert( CPU_EQUAL(&cpuset1, &cpuset2) );
0151   }
0152 }
0153 
0154 void *POSIX_Init(
0155   void *ignored
0156 )
0157 {
0158   TEST_BEGIN();
0159 
0160   /* Initialize thread id */
0161   Init_id = pthread_self();
0162 
0163   Validate_attrsetaffinity_errors();
0164   Validate_attrgetaffinity_errors();
0165   Validate_attr();
0166 
0167   TEST_END();
0168   rtems_test_exit(0);
0169 }
0170 
0171 /* configuration information */
0172 
0173 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0174 #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
0175 
0176 #define CONFIGURE_MAXIMUM_PROCESSORS CPU_COUNT
0177 
0178 #define CONFIGURE_SCHEDULER_PRIORITY_AFFINITY_SMP
0179 
0180 #define CONFIGURE_MAXIMUM_POSIX_THREADS  1
0181 
0182 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0183 
0184 #define CONFIGURE_POSIX_INIT_THREAD_TABLE
0185 
0186 #define CONFIGURE_INIT
0187 #include <rtems/confdefs.h>
0188 
0189 /* global variables */