Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  COPYRIGHT (c) 1989-2011.
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 NUM_CPUS   4
0034 
0035 #include "tmacros.h"
0036 
0037 const char rtems_test_name[] = "SMPAFFINITY 1";
0038 
0039 rtems_id           Init_id;
0040 rtems_id           Med_id[NUM_CPUS-1];
0041 rtems_id           Low_id[NUM_CPUS];
0042 
0043 /* forward declarations to avoid warnings */
0044 void Task_1(rtems_task_argument arg);
0045 void Validate_setaffinity_errors(void);
0046 void Validate_getaffinity_errors(void);
0047 void Validate_affinity(void);
0048 
0049 void Task_1(rtems_task_argument arg)
0050 {
0051   while(1);
0052 }
0053 
0054 void Validate_setaffinity_errors(void)
0055 {
0056   int                 sc;
0057   cpu_set_t           cpuset;
0058 
0059   /* Verify rtems_task_set_affinity checks that all cpu's exist. */
0060   /* Note this check assumes you are running with less than 32 CPUs */
0061   CPU_FILL(&cpuset);
0062   puts( "Init - rtems_task_set_affinity - Lots of cpus - SUCCESS" );
0063   sc = rtems_task_set_affinity( Init_id, sizeof(cpu_set_t), &cpuset );
0064   rtems_test_assert( sc == RTEMS_SUCCESSFUL );
0065 
0066   /* Verify rtems_task_set_affinity checks that at least one cpu is set */
0067   CPU_ZERO(&cpuset);
0068   puts( "Init - rtems_task_set_affinity - no cpu - RTEMS_INVALID_NUMBER" );
0069   sc = rtems_task_set_affinity( Init_id, sizeof(cpu_set_t), &cpuset );
0070   rtems_test_assert( sc == RTEMS_INVALID_NUMBER );
0071 
0072   /* Verify rtems_task_set_affinity checks that at thread id is valid */
0073   CPU_ZERO(&cpuset);
0074   CPU_SET(0, &cpuset);
0075   puts( "Init - rtems_task_set_affinity - Invalid thread - RTEMS_INVALID_ID" );
0076   sc = rtems_task_set_affinity( 999, sizeof(cpu_set_t), &cpuset );
0077   rtems_test_assert( sc == RTEMS_INVALID_ID );
0078 
0079   /* Verify rtems_task_set_affinity validates cpusetsize */
0080   puts( "Init - rtems_task_set_affinity - Invalid cpusetsize - RTEMS_INVALID_NUMBER" );
0081   sc = rtems_task_set_affinity( Init_id,  1, &cpuset );
0082   rtems_test_assert( sc == RTEMS_INVALID_NUMBER );
0083 
0084   /* Verifyrtems_task_set_affinity validates cpuset */
0085   puts( "Init - rtems_task_set_affinity - Invalid cpuset - RTEMS_INVALID_ADDRESS" );
0086   sc = rtems_task_set_affinity( Init_id, sizeof(cpu_set_t), NULL );
0087   rtems_test_assert( sc == RTEMS_INVALID_ADDRESS );
0088 }
0089 
0090 void Validate_getaffinity_errors(void)
0091 {
0092   int                 sc;
0093   cpu_set_t           cpuset;
0094 
0095   /* Verify rtems_task_get_affinity checks that at thread id is valid */
0096   CPU_ZERO(&cpuset);
0097   CPU_SET(0, &cpuset);
0098   puts( "Init - rtems_task_get_affinity - Invalid thread - RTEMS_INVALID_ID" );
0099   sc = rtems_task_get_affinity( 999, sizeof(cpu_set_t), &cpuset );
0100   rtems_test_assert( sc == RTEMS_INVALID_ID );
0101 
0102   /* Verify rtems_task_get_affinity validates cpusetsize */
0103   puts(
0104     "Init - rtems_task_get_affinity - Invalid cpusetsize - RTEMS_INVALID_NUMBER"
0105   );
0106   sc = rtems_task_get_affinity( Init_id,  1, &cpuset );
0107   rtems_test_assert( sc == RTEMS_INVALID_SIZE );
0108 
0109   /* Verify rtems_task_get_affinity validates cpuset */
0110   puts("Init - rtems_task_get_affinity - Invalid cpuset - RTEMS_INVALID_ADDRESS");
0111   sc = rtems_task_get_affinity( Init_id, sizeof(cpu_set_t), NULL );
0112   rtems_test_assert( sc == RTEMS_INVALID_ADDRESS );
0113 }
0114 
0115 void Validate_affinity(void )
0116 {
0117   cpu_set_t            cpuset0;
0118   cpu_set_t            cpuset1;
0119   cpu_set_t            cpuset2;
0120   uint32_t             i;
0121   int                  sc;
0122   int                  cpu_count;
0123   rtems_task_priority  priority;
0124   char                 ch[2];
0125 
0126   puts( "Init - Set Init priority to high");
0127   sc = rtems_task_set_priority( Init_id, 1, &priority );
0128   directive_failed( sc, "Set Init Priority" );
0129 
0130   sc = rtems_task_get_affinity( Init_id, sizeof(cpu_set_t), &cpuset0 );
0131   directive_failed( sc, "Get Affinity of Init Task" );
0132 
0133   /* Get the number of processors that we are using. */
0134   cpu_count = rtems_scheduler_get_processor_maximum();
0135 
0136   /* Fill the remaining cpus with med priority tasks */
0137   puts( "Init - Create Medium priority tasks");
0138   for (i=0; i<(cpu_count-1); i++){
0139     sprintf(ch, "%01" PRId32, i+1 );
0140     sc = rtems_task_create(
0141       rtems_build_name( 'C', 'P', 'U', ch[0] ),
0142       2,
0143       RTEMS_MINIMUM_STACK_SIZE,
0144       RTEMS_DEFAULT_MODES,
0145       RTEMS_DEFAULT_ATTRIBUTES,
0146       &Med_id[i]
0147     );
0148     directive_failed( sc, "task create" );
0149 
0150     sc = rtems_task_start( Med_id[i], Task_1, i+1 );
0151     directive_failed( sc, "task start" );
0152 
0153     sc = rtems_task_get_affinity( Med_id[i], sizeof(cpu_set_t), &cpuset2 );
0154     directive_failed( sc, "Get Affinity of Medium Priority Task" );
0155     rtems_test_assert( CPU_EQUAL(&cpuset0, &cpuset2) );
0156   }
0157 
0158   /*
0159    * Create low priority thread for each remaining cpu with the affinity
0160    * set to only run on one cpu.
0161    */
0162   puts( "Init - Create  Low priority tasks");
0163   for (i=0; i<cpu_count; i++){
0164     CPU_ZERO(&cpuset1);
0165     CPU_SET(i, &cpuset1);
0166 
0167     sprintf(ch, "%01" PRId32, (uint32_t) 0 );
0168     sc = rtems_task_create(
0169       rtems_build_name( 'X', 'T', 'R', ch[0] ),
0170       10,
0171       RTEMS_MINIMUM_STACK_SIZE,
0172       RTEMS_DEFAULT_MODES,
0173       RTEMS_DEFAULT_ATTRIBUTES,
0174       &Low_id[i]
0175     );
0176     directive_failed( sc, "task create" );
0177 
0178     sc = rtems_task_set_affinity( Low_id[i], sizeof(cpu_set_t), &cpuset1 );
0179     directive_failed( sc, "Low priority task set affinity" );
0180 
0181     sc = rtems_task_start( Low_id[i], Task_1, i+1 );
0182     directive_failed( sc, "task start" );
0183   }
0184 
0185 
0186   /* Verify affinity on low priority tasks */
0187   puts("Init - Verify affinity on Low priority tasks");
0188   for (i=0; i<cpu_count; i++){
0189     CPU_ZERO(&cpuset1);
0190     CPU_SET(i, &cpuset1);
0191 
0192     sc = rtems_task_get_affinity( Low_id[i], sizeof(cpu_set_t), &cpuset2 );
0193     directive_failed( sc, "Low priority task get affinity" );
0194     rtems_test_assert( CPU_EQUAL(&cpuset1, &cpuset2) );
0195   }
0196 
0197   /* Change the affinity for each low priority task */
0198   puts("Init - Change affinity on Low priority tasks");
0199   CPU_COPY(&cpuset0, &cpuset1);
0200   for (i=0; i<cpu_count; i++){
0201 
0202     CPU_CLR(i, &cpuset1);
0203     sc = rtems_task_set_affinity( Low_id[i], sizeof(cpu_set_t), &cpuset1 );
0204 
0205     /* Verify no cpu's are now set in the cpuset */
0206     if (i== (cpu_count-1)) {
0207       rtems_test_assert( sc == RTEMS_INVALID_NUMBER );
0208       sc = rtems_task_set_affinity( Low_id[i], sizeof(cpu_set_t), &cpuset0 );
0209     }
0210 
0211     directive_failed( sc, "Low priority task set affinity" );
0212   }
0213 
0214   puts("Init - Validate affinity on Low priority tasks");
0215   CPU_COPY(&cpuset0, &cpuset1);
0216   for (i=0; i<cpu_count; i++){
0217     CPU_CLR(i, &cpuset1);
0218 
0219     sc = rtems_task_get_affinity( Low_id[i], sizeof(cpu_set_t), &cpuset2 );
0220     directive_failed( sc, "Low priority task get affinity" );
0221     if (i== (cpu_count-1))
0222       rtems_test_assert( CPU_EQUAL(&cpuset0, &cpuset2) );
0223     else
0224       rtems_test_assert( CPU_EQUAL(&cpuset1, &cpuset2) );
0225   }
0226 }
0227 
0228 static void Init(rtems_task_argument arg)
0229 {
0230   int                  sc;
0231 
0232   TEST_BEGIN();
0233 
0234   /* Initialize thread id */
0235   sc = rtems_task_ident( RTEMS_WHO_AM_I, RTEMS_SEARCH_ALL_NODES, &Init_id );
0236   directive_failed( sc, "Identify Init Task" );
0237 
0238   Validate_setaffinity_errors();
0239   Validate_getaffinity_errors();
0240   Validate_affinity();
0241 
0242   TEST_END();
0243   rtems_test_exit(0);
0244 }
0245 
0246 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0247 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0248 
0249 #define CONFIGURE_SCHEDULER_PRIORITY_AFFINITY_SMP
0250 
0251 #define CONFIGURE_MAXIMUM_PROCESSORS NUM_CPUS
0252 
0253 #define CONFIGURE_MAXIMUM_TASKS         (NUM_CPUS*2)
0254 
0255 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0256 
0257 #define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT
0258 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0259 
0260 #define CONFIGURE_INIT
0261 
0262 #include <rtems/confdefs.h>