Back to home page

LXR

 
 

    


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

0001 /*
0002  * Copyright (c) 2014 Daniel Ramirez. (javamonn@gmail.com)
0003  *
0004  * This file's license is 2-clause BSD as in this distribution's LICENSE file.
0005  */
0006 
0007 #include <timesys.h>
0008 #include <rtems/btimer.h>
0009 
0010 const char rtems_test_name[] = "RHSEMSHUFFLE";
0011 
0012 #define BENCHMARKS 50000
0013 
0014 rtems_task Task01( rtems_task_argument ignored );
0015 rtems_task Task02( rtems_task_argument ignored );
0016 rtems_task Init( rtems_task_argument ignored );
0017 
0018 rtems_id   Task_id[2];
0019 rtems_name Task_name[2];
0020 rtems_id    sem_id;
0021 rtems_name  sem_name;
0022 
0023 uint32_t    telapsed;
0024 uint32_t    tswitch_overhead;
0025 uint32_t    count;
0026 uint32_t    sem_exe;
0027 
0028 rtems_task Init( rtems_task_argument ignored )
0029 {
0030   rtems_status_code    status;
0031   rtems_attribute      sem_attr;
0032   rtems_task_priority  pri;
0033   rtems_mode           prev_mode;
0034 
0035   Print_Warning();
0036 
0037   TEST_BEGIN();
0038 
0039   sem_attr =  RTEMS_BINARY_SEMAPHORE | RTEMS_PRIORITY;
0040 
0041   sem_name = rtems_build_name( 'S','0',' ',' ' );
0042   status = rtems_semaphore_create(
0043     sem_name,
0044     1,
0045     sem_attr,
0046     0,
0047     &sem_id
0048   );
0049   directive_failed( status, "rtems_semaphore_create of S0" );
0050 
0051   Task_name[0] = rtems_build_name( 'T','A','0','1' );
0052   status = rtems_task_create(
0053     Task_name[0],
0054     30,
0055     RTEMS_MINIMUM_STACK_SIZE,
0056     RTEMS_DEFAULT_MODES,
0057     RTEMS_DEFAULT_ATTRIBUTES,
0058     &Task_id[0]
0059   );
0060   directive_failed( status, "rtems_task_create of TA01" );
0061 
0062   Task_name[1] = rtems_build_name( 'T','A','0','2' );
0063   status = rtems_task_create(
0064     Task_name[1],
0065     30,
0066     RTEMS_MINIMUM_STACK_SIZE,
0067     RTEMS_DEFAULT_MODES,
0068     RTEMS_DEFAULT_ATTRIBUTES,
0069     &Task_id[1]
0070   );
0071   directive_failed( status , "rtems_task_create of TA02\n" );
0072 
0073   rtems_task_mode( RTEMS_PREEMPT, RTEMS_PREEMPT_MASK, &prev_mode );
0074   /* Lower own priority so TA01 can start up and run */
0075   rtems_task_set_priority( RTEMS_SELF, 40, &pri);
0076 
0077   /* Get time of benchmark with no semaphore shuffling */
0078   sem_exe = 0;
0079   status = rtems_task_start( Task_id[0], Task01, 0 );
0080   directive_failed( status, "rtems_task_start of TA01" );
0081 
0082   /* Get time of benchmark with semaphore shuffling */
0083   sem_exe = 1;
0084   status = rtems_task_restart( Task_id[0], 0 );
0085   directive_failed( status, "rtems_task_restart of TA01" );
0086 
0087   /* Should never reach here */
0088   rtems_test_assert( false );
0089 }
0090 
0091 rtems_task Task01( rtems_task_argument ignored )
0092 {
0093   rtems_status_code status;
0094 
0095   /* Start up TA02, yield so it can run */
0096   if ( sem_exe == 0 ) {
0097     status = rtems_task_start( Task_id[1], Task02, 0 );
0098     directive_failed( status, "rtems_task_start of TA02" );
0099   } else {
0100     status = rtems_task_restart( Task_id[1], 0 );
0101     directive_failed( status, "rtems_task_restart of TA02" );
0102   }
0103   rtems_task_wake_after( RTEMS_YIELD_PROCESSOR );
0104 
0105   /* Benchmark code */
0106   for ( ; count < BENCHMARKS ; ) {
0107     if ( sem_exe == 1 ) {
0108       rtems_semaphore_obtain( sem_id, RTEMS_WAIT, 0 );
0109     }
0110     rtems_task_wake_after( RTEMS_YIELD_PROCESSOR );
0111 
0112     if ( sem_exe == 1 ) {
0113       rtems_semaphore_release( sem_id );
0114     }
0115     rtems_task_wake_after( RTEMS_YIELD_PROCESSOR );
0116   }
0117 
0118   /* Should never reach here */
0119   rtems_test_assert( false );
0120 }
0121 
0122 rtems_task Task02( rtems_task_argument ignored )
0123 {
0124 
0125   /* Benchmark code */
0126   benchmark_timer_initialize();
0127   for ( count = 0; count < BENCHMARKS; count++ ) {
0128     if ( sem_exe == 1 ) {
0129       rtems_semaphore_obtain( sem_id, RTEMS_WAIT, 0 );
0130     }
0131     rtems_task_wake_after( RTEMS_YIELD_PROCESSOR );
0132 
0133     if ( sem_exe == 1 ) {
0134       rtems_semaphore_release( sem_id );
0135     }
0136     rtems_task_wake_after( RTEMS_YIELD_PROCESSOR );
0137   }
0138   telapsed = benchmark_timer_read();
0139 
0140   /* Check which run this was */
0141   if (sem_exe == 0) {
0142     tswitch_overhead = telapsed;
0143     rtems_task_suspend( Task_id[0] );
0144     rtems_task_suspend( RTEMS_SELF );
0145   } else {
0146     put_time(
0147        "Rhealstone: Semaphore Shuffle",
0148        telapsed,
0149        (BENCHMARKS * 2),        /* Total number of semaphore-shuffles*/
0150        tswitch_overhead,        /* Overhead of loop and task switches */
0151        0
0152     );
0153     TEST_END();
0154     rtems_test_exit( 0 );
0155   }
0156 }
0157 
0158 /* configuration information */
0159 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0160 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0161 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0162 #define CONFIGURE_MAXIMUM_TASKS 3
0163 #define CONFIGURE_MAXIMUM_SEMAPHORES 1
0164 #define CONFIGURE_INIT
0165 #include <rtems/confdefs.h>