Back to home page

LXR

 
 

    


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

0001 /*  Tasks_Periodic
0002  *
0003  *  This routine serves as a test task for the CBS scheduler
0004  *  implementation.
0005  *
0006  *  Input parameters:
0007  *    argument - task argument
0008  *
0009  *  Output parameters:  NONE
0010  *
0011  *  The license and distribution terms for this file may be
0012  *  found in the file LICENSE in this distribution or at
0013  *  http://www.rtems.org/license/LICENSE.
0014  */
0015 
0016 #ifdef HAVE_CONFIG_H
0017 #include "config.h"
0018 #endif
0019 
0020 #include "system.h"
0021 
0022 rtems_task Task_Periodic(
0023   rtems_task_argument argument
0024 )
0025 {
0026   rtems_id          rmid;
0027   rtems_status_code status;
0028 
0029   time_t approved_budget, exec_time, abs_time, remaining_budget;
0030 
0031   int start, stop, now;
0032 
0033   rtems_cbs_server_id server_id = 0, tsid;
0034   rtems_cbs_parameters params, tparams;
0035 
0036   params.deadline = Period;
0037   params.budget = Execution+1;
0038 
0039   /* Taks 1 will be attached to a server, task 2 not. */
0040   if ( argument == 1 ) {
0041     printf( "Periodic task: Create server and Attach thread\n" );
0042     if ( rtems_cbs_create_server( &params, NULL, &server_id ) )
0043       printf( "ERROR: CREATE SERVER FAILED\n" );
0044     if ( rtems_cbs_attach_thread( server_id, Task_id ) )
0045       printf( "ERROR: ATTACH THREAD FAILED\n" );
0046 
0047     printf( "Periodic task: ID and Get parameters\n" );
0048     if ( rtems_cbs_get_server_id( Task_id, &tsid ) )
0049       printf( "ERROR: GET SERVER ID FAILED\n" );
0050     if ( tsid != server_id )
0051       printf( "ERROR: SERVER ID MISMATCH\n" );
0052     if ( rtems_cbs_get_parameters( server_id, &tparams ) )
0053       printf( "ERROR: GET PARAMETERS FAILED\n" );
0054     if ( params.deadline != tparams.deadline ||
0055          params.budget != tparams.budget )
0056       printf( "ERROR: PARAMETERS MISMATCH\n" );
0057 
0058     printf( "Periodic task: Detach thread and Destroy server\n" );
0059     if ( rtems_cbs_detach_thread( server_id, Task_id ) )
0060       printf( "ERROR: DETACH THREAD FAILED\n" );
0061     if ( rtems_cbs_destroy_server( server_id ) )
0062       printf( "ERROR: DESTROY SERVER FAILED\n" );
0063     if ( rtems_cbs_create_server( &params, NULL, &server_id ) )
0064       printf( "ERROR: CREATE SERVER FAILED\n" );
0065 
0066     printf( "Periodic task: Remaining budget and Execution time\n" );
0067     if ( rtems_cbs_get_remaining_budget( server_id, &remaining_budget ) )
0068       printf( "ERROR: GET REMAINING BUDGET FAILED\n" );
0069     if ( remaining_budget != params.budget )
0070       printf( "ERROR: REMAINING BUDGET MISMATCH\n" );
0071     if ( rtems_cbs_get_execution_time( server_id, &exec_time, &abs_time ) )
0072       printf( "ERROR: GET EXECUTION TIME FAILED\n" );
0073 
0074     printf( "Periodic task: Set parameters\n" );
0075     if ( rtems_cbs_attach_thread( server_id, Task_id ) )
0076       printf( "ERROR: ATTACH THREAD FAILED\n" );
0077     params.deadline = Period * 2;
0078     params.budget = Execution * 2 +1;
0079     if ( rtems_cbs_set_parameters( server_id, &params ) )
0080       printf( "ERROR: SET PARAMS FAILED\n" );
0081     if ( rtems_cbs_get_parameters( server_id, &tparams ) )
0082       printf( "ERROR: GET PARAMS FAILED\n" );
0083     if ( params.deadline != tparams.deadline ||
0084          params.budget != tparams.budget )
0085       printf( "ERROR: PARAMS MISMATCH\n" );
0086     params.deadline = Period;
0087     params.budget = Execution+1;
0088     if ( rtems_cbs_set_parameters( server_id, &params ) )
0089       printf( "ERROR: SET PARAMS FAILED\n" );
0090     if ( rtems_cbs_get_approved_budget( server_id, &approved_budget ) )
0091       printf( "ERROR: GET APPROVED BUDGET FAILED\n" );
0092 
0093     printf( "Periodic task: Approved budget\n" );
0094     if ( approved_budget != params.budget )
0095       printf( "ERROR: APPROVED BUDGET MISMATCH\n" );
0096   }
0097 
0098   status = rtems_rate_monotonic_create( argument, &rmid );
0099   directive_failed( status, "rtems_rate_monotonic_create" );
0100 
0101   /* Starting periodic behavior of the task */
0102   printf( "Periodic task: Starting periodic behavior\n" );
0103   status = rtems_task_wake_after( 1 + Phase );
0104   directive_failed( status, "rtems_task_wake_after" );
0105 
0106   while ( FOREVER ) {
0107     if ( rtems_rate_monotonic_period(rmid, Period) == RTEMS_TIMEOUT )
0108       printf( "P%" PRIdPTR " - Deadline miss\n", argument );
0109 
0110     start = rtems_clock_get_ticks_since_boot();
0111     printf( "P%" PRIdPTR "-S ticks:%d\n", argument, start );
0112     if ( start > 4*Period+Phase ) break; /* stop */
0113     /* active computing */
0114     while(FOREVER) {
0115       now = rtems_clock_get_ticks_since_boot();
0116       if ( now >= start + Execution ) break;
0117 
0118       if ( server_id != 0 ) {
0119         if ( rtems_cbs_get_execution_time( server_id, &exec_time, &abs_time ) )
0120           printf( "ERROR: GET EXECUTION TIME FAILED\n" );
0121         if ( rtems_cbs_get_remaining_budget( server_id, &remaining_budget) )
0122           printf( "ERROR: GET REMAINING BUDGET FAILED\n" );
0123         if ( (remaining_budget + exec_time) > (Execution + 1) ) {
0124           printf( "ERROR: REMAINING BUDGET AND EXECUTION TIME MISMATCH\n" );
0125           rtems_test_exit( 0 );
0126         }
0127       }
0128     }
0129     stop = rtems_clock_get_ticks_since_boot();
0130     printf( "P%" PRIdPTR "-F ticks:%d\n", argument, stop );
0131   }
0132 
0133   /* delete period and SELF */
0134   status = rtems_rate_monotonic_delete( rmid );
0135   if ( status != RTEMS_SUCCESSFUL ) {
0136     printf("rtems_rate_monotonic_delete failed with status of %d.\n", status);
0137     rtems_test_exit( 0 );
0138   }
0139   printf( "Periodic task: Deleting self\n" );
0140   rtems_task_exit();
0141 }