<commit_before>#include <rtems.h>

#include <stdio.h>
#include "tmacros.h"
#include "timesys.h"

/* configuration information */
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
#define CONFIGURE_MAXIMUM_TASKS 4
#define CONFIGURE_MAXIMUM_SEMAPHORES 2
#define CONFIGURE_INIT
#include <rtems/confdefs.h>

#define BENCHMARKS 50000

rtems_task Task01( rtems_task_argument ignored );
rtems_task Task02( rtems_task_argument ignored );
rtems_task Init( rtems_task_argument ignored );

rtems_id   Task_id[3];
rtems_name Task_name[3];
rtems_id    sem_id;
rtems_name  sem_name;

uint32_t    telapsed;
uint32_t    loop_overhead;
uint32_t    count;

rtems_task Init( rtems_task_argument ignored )
{
  rtems_status_code status;
  rtems_attribute sem_attr;

  sem_attr =  RTEMS_BINARY_SEMAPHORE | RTEMS_PRIORITY;

  sem_name = rtems_build_name( 'S','0',' ',' ');
  status = rtems_semaphore_create(
    sem_name,
    1,
    sem_attr,
    0,
    &sem_id
  );
  directive_failed( status, "rtems_semaphore_create of S0" );

  Task_name[0] = rtems_build_name( 'T','A','0','1' );
  status = rtems_task_create(
    Task_name[0],
    30,
    RTEMS_MINIMUM_STACK_SIZE,
    RTEMS_DEFAULT_MODES,
    RTEMS_DEFAULT_ATTRIBUTES,
    &Task_id[0]
  );
  directive_failed( status, "rtems_task_create of TA01" );

  Task_name[1] = rtems_build_name( 'T','A','0','2' );
  status = rtems_task_create(
    Task_name[1],
    30,
    RTEMS_MINIMUM_STACK_SIZE,
    RTEMS_DEFAULT_MODES,
    RTEMS_DEFAULT_ATTRIBUTES,
    &Task_id[1]
  );
  directive_failed( status , "rtems_task_create of TA02\n" );

  status = rtems_task_start( Task_id[0], Task01, 0 );
  directive_failed( status, "rtems_task_start of TA01" );

  status = rtems_task_delete( RTEMS_SELF );
  directive_failed( status, "rtems_task_delete of INIT");
}

rtems_task Task01( rtems_task_argument ignored )
{
  rtems_status_code status;

  status = rtems_semaphore_obtain( sem_id, RTEMS_WAIT, 0 );
  directive_failed( status, "rtems_semaphore_obtain of S0\n" );

  /* Start up TA02, yield so it can run */
  status = rtems_task_start( Task_id[1], Task02, 0 );
  directive_failed( status, "rtems_task_start of TA01" );

  rtems_task_wake_after( RTEMS_YIELD_PROCESSOR );

  /* Benchmark code */
  for ( ; count < BENCHMARKS ; ) {
    /* TA02 now owns semaphore after we release it, no preempt */
    rtems_semaphore_release( sem_id );
    /* Attempt to aquire semaphore, block on call */
    rtems_semaphore_obtain( sem_id, RTEMS_WAIT, 0 );
  }

  /* Should never reach here */
  rtems_test_assert( false );
}

rtems_task Task02( rtems_task_argument ignored )
{

  /* Benchmark code */
  benchmark_timer_initialize();
  for ( count = 0; count < BENCHMARKS - 1; count++ ) {
    /* Block on semaphore, switch to TA01 */
    rtems_semaphore_obtain( sem_id, RTEMS_WAIT, 0 );
    /* Release semaphore, TA01 now owns, loop, no preempt */
    rtems_semaphore_release( sem_id );
  }
  telapsed = benchmark_timer_read();

  put_time(
     "Rhealstone: Semaphore Shuffle",
     telapsed,
     (BENCHMARKS * 2) - 1, /* Total number of semaphore-shuffles*/
     loop_overhead,        /* Overhead of loop */
     0                     /* No overhead, obtain/relase part of shuffle */
  );
}<commit_msg>more tweaks to semaphore-shuffle benchmark<commit_after>#include <rtems.h>

#include <stdio.h>
#include "tmacros.h"
#include "timesys.h"

/* configuration information */
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
#define CONFIGURE_MAXIMUM_TASKS 4
#define CONFIGURE_MAXIMUM_SEMAPHORES 2
#define CONFIGURE_INIT
#include <rtems/confdefs.h>

#define BENCHMARKS 50000

rtems_task Task01( rtems_task_argument ignored );
rtems_task Task02( rtems_task_argument ignored );
rtems_task Init( rtems_task_argument ignored );

rtems_id   Task_id[3];
rtems_name Task_name[3];
rtems_id    sem_id;
rtems_name  sem_name;

uint32_t    telapsed;
uint32_t    tswitch_overhead;
uint32_t    count;
uint32_t    sem_exe;

rtems_task Init( rtems_task_argument ignored )
{
  rtems_status_code    status;
  rtems_attribute      sem_attr;
  rtems_task_priority  pri;
  rtems_mode           prev_mode;

  sem_attr =  RTEMS_BINARY_SEMAPHORE | RTEMS_PRIORITY;

  sem_name = rtems_build_name( 'S','0',' ',' ');
  status = rtems_semaphore_create(
    sem_name,
    1,
    sem_attr,
    0,
    &sem_id
  );
  directive_failed( status, "rtems_semaphore_create of S0" );

  Task_name[0] = rtems_build_name( 'T','A','0','1' );
  status = rtems_task_create(
    Task_name[0],
    30,
    RTEMS_MINIMUM_STACK_SIZE,
    RTEMS_DEFAULT_MODES,
    RTEMS_DEFAULT_ATTRIBUTES,
    &Task_id[0]
  );
  directive_failed( status, "rtems_task_create of TA01" );

  Task_name[1] = rtems_build_name( 'T','A','0','2' );
  status = rtems_task_create(
    Task_name[1],
    30,
    RTEMS_MINIMUM_STACK_SIZE,
    RTEMS_DEFAULT_MODES,
    RTEMS_DEFAULT_ATTRIBUTES,
    &Task_id[1]
  );
  directive_failed( status , "rtems_task_create of TA02\n" );


  /* Get time of benchmark with no semaphore shuffling */
  sem_exe = 0;
  status = rtems_task_start( Task_id[0], Task01, 0 );
  directive_failed( status, "rtems_task_start of TA01" );

  rtems_task_mode( RTEMS_PREEMPT, RTEMS_PREEMPT_MASK, &prev_mode );

  /* Lower own priority so TA01 can start up and run */
  rtems_task_set_priority( RTEMS_SELF, 40, &pri);

  /* Get time of benchmark with semaphore shuffling */
  sem_exe = 1;
  status = rtems_task_restart( Task_id[0], 0 );
  directive_failed( status, "rtems_task_restart of TA01" ); 

  /* Should never reach here */
  rtems_test_assert( false );
}

rtems_task Task01( rtems_task_argument ignored )
{
  rtems_status_code status;

  /* Start up TA02, yield so it can run */
  if ( sem_exe == 0 ) {
    status = rtems_task_start( Task_id[1], Task02, 0 );
    directive_failed( status, "rtems_task_start of TA02" );
  } else {
    status = rtems_task_restart( Task_id[1], 0 );
    directive_failed( status, "rtems_task_restart of TA02" );
  }
  rtems_task_wake_after( RTEMS_YIELD_PROCESSOR );

  /* Benchmark code */
  for ( ; count < BENCHMARKS ; ) {
    if ( sem_exe == 1 ) {
      rtems_semaphore_obtain( sem_id, RTEMS_WAIT, 0 );
    }
    rtems_task_wake_after( RTEMS_YIELD_PROCESSOR );

    if ( sem_exe == 1 ) {
      rtems_semaphore_release( sem_id );
    }
    rtems_task_wake_after( RTEMS_YIELD_PROCESSOR );
  }

  /* Should never reach here */
  rtems_test_assert( false );
}

rtems_task Task02( rtems_task_argument ignored )
{

  /* Benchmark code */
  benchmark_timer_initialize();
  for ( count = 0; count < BENCHMARKS; count++ ) {
    if ( sem_exe == 1 ) {
      rtems_semaphore_obtain( sem_id, RTEMS_WAIT, 0 );
    }
    rtems_task_wake_after( RTEMS_YIELD_PROCESSOR );

    if ( sem_exe == 1 ) {
      rtems_semaphore_release( sem_id );
    }
    rtems_task_wake_after( RTEMS_YIELD_PROCESSOR );
  }
  telapsed = benchmark_timer_read();

  /* Check which run this was */
  if (sem_exe == 0) {
    tswitch_overhead = telapsed;
    rtems_task_suspend( Task_id[0] );
    rtems_task_suspend( RTEMS_SELF );
  } else {
    put_time(
       "Rhealstone: Semaphore Shuffle",
       telapsed,
       (BENCHMARKS * 2),        /* Total number of semaphore-shuffles*/
       tswitch_overhead,        /* Overhead of loop and task switches */
       0
    );
    rtems_test_exit( 0 );
  }
}
{'fraction_non_alphanumeric': 0.10345755415041033, 'fraction_numerical': 0.021794699313870576, 'mean_word_length': 3.868369351669941, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1771131', 'n_tokens_mistral': 3056, 'n_tokens_neox': 2904, 'n_words': 578}