summaryrefslogtreecommitdiffstats
path: root/testsuites/sptests/spqreslib/task_periodic.c
blob: add0f9bd06f9147f0e44846d026ae251e8d68853 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*  Tasks_Periodic
 *
 *  This routine serves as a test task for the CBS scheduler
 *  implementation.
 *
 *  Input parameters:
 *    argument - task argument
 *
 *  Output parameters:  NONE
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#include "system.h"

rtems_task Task_Periodic(
  rtems_task_argument argument
)
{
  rtems_id          rmid;
  rtems_status_code status;

  time_t approved_budget, exec_time, abs_time, current_budget;

  int start, stop, now;

  qres_sid_t server_id, tsid;
  qres_params_t params, tparams;

  params.P = Period;
  params.Q = Execution+1;

  printf( "Periodic task: Create server and Attach thread\n" );
  if ( qres_create_server( &params, &server_id ) )
    printf( "ERROR: CREATE SERVER FAILED\n" );
  if ( qres_attach_thread( server_id, 0, Task_id ) )
    printf( "ERROR: ATTACH THREAD FAILED\n" );

  printf( "Periodic task: ID and Get parameters\n" );
  if ( qres_get_sid( 0, Task_id, &tsid ) )
    printf( "ERROR: GET SERVER ID FAILED\n" );
  if ( tsid != server_id )
    printf( "ERROR: SERVER ID MISMATCH\n" );
  if ( qres_get_params( server_id, &tparams ) )
    printf( "ERROR: GET PARAMETERS FAILED\n" );
  if ( params.P != tparams.P ||
       params.Q != tparams.Q )
    printf( "ERROR: PARAMETERS MISMATCH\n" );

  printf( "Periodic task: Detach thread and Destroy server\n" );
  if ( qres_detach_thread( server_id, 0, Task_id ) )
    printf( "ERROR: DETACH THREAD FAILED\n" );
  if ( qres_destroy_server( server_id ) )
    printf( "ERROR: DESTROY SERVER FAILED\n" );
  if ( qres_create_server( &params, &server_id ) )
    printf( "ERROR: CREATE SERVER FAILED\n" );

  printf( "Periodic task: Current budget and Execution time\n" );
  if ( qres_get_curr_budget( server_id, &current_budget ) )
    printf( "ERROR: GET REMAINING BUDGET FAILED\n" );
  if ( current_budget != params.Q )
    printf( "ERROR: REMAINING BUDGET MISMATCH\n" );
  if ( qres_get_exec_time( server_id, &exec_time, &abs_time ) )
    printf( "ERROR: GET EXECUTION TIME FAILED\n" );

  printf( "Periodic task: Set parameters\n" );
  if ( qres_attach_thread( server_id, 0, Task_id ) )
    printf( "ERROR: ATTACH THREAD FAILED\n" );
  params.P = Period * 2;
  params.Q = Execution * 2 +1;
  if ( qres_set_params( server_id, &params ) )
    printf( "ERROR: SET PARAMS FAILED\n" );
  if ( qres_get_params( server_id, &tparams ) )
    printf( "ERROR: GET PARAMS FAILED\n" );
  if ( params.P != tparams.P ||
       params.Q != tparams.Q )
    printf( "ERROR: PARAMS MISMATCH\n" );
  params.P = Period;
  params.Q = Execution+1;
  if ( qres_set_params( server_id, &params ) )
    printf( "ERROR: SET PARAMS FAILED\n" );
  if ( qres_get_appr_budget( server_id, &approved_budget ) )
    printf( "ERROR: GET APPROVED BUDGET FAILED\n" );

  printf( "Periodic task: Approved budget\n" );
  if ( approved_budget != params.Q )
    printf( "ERROR: APPROVED BUDGET MISMATCH\n" );

  status = rtems_rate_monotonic_create( argument, &rmid );
  directive_failed( status, "rtems_rate_monotonic_create" );

  /* Starting periodic behavior of the task */
  printf( "Periodic task: Starting periodic behavior\n" );
  status = rtems_task_wake_after( 1 + Phase );
  directive_failed( status, "rtems_task_wake_after" );

  while ( FOREVER ) {
    if ( rtems_rate_monotonic_period(rmid, Period) == RTEMS_TIMEOUT )
      printf( "P%" PRIdPTR " - Deadline miss\n", argument );

    rtems_clock_get( RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &start );
    printf( "P%" PRIdPTR "-S ticks:%d\n", argument, start );
    if ( start > 4*Period+Phase ) break; /* stop */
    /* active computing */
    while(FOREVER) {
      rtems_clock_get( RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now );
      if ( now >= start + Execution ) break;

      if ( qres_get_exec_time( server_id, &exec_time, &abs_time ) )
        printf( "ERROR: GET EXECUTION TIME FAILED\n" );
      if ( qres_get_curr_budget( server_id, &current_budget) )
        printf( "ERROR: GET CURRENT BUDGET FAILED\n" );
      if ( (current_budget + exec_time) > (Execution + 1) ) {
        printf( "ERROR: CURRENT BUDGET AND EXECUTION TIME MISMATCH\n" );
        rtems_test_exit( 0 );
      }
    }
    rtems_clock_get( RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &stop );
    printf( "P%" PRIdPTR "-F ticks:%d\n", argument, stop );
  }

  /* delete period and SELF */
  status = rtems_rate_monotonic_delete( rmid );
  if ( status != RTEMS_SUCCESSFUL ) {
    printf("rtems_rate_monotonic_delete failed with status of %d.\n", status);
    rtems_test_exit( 0 );
  }
  if ( qres_cleanup() )
    printf( "ERROR: QRES CLEANUP\n" );

  fflush(stdout);
  puts( "*** END OF TEST QRES LIBRARY ***" );
  rtems_test_exit( 0 );
}