summaryrefslogtreecommitdiffstats
path: root/testsuites/sptests/spcbssched02/task_periodic.c
blob: bcdec3c37898fcee42caf9713164d8714d9c987d (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
137
138
139
140
141
142
/*  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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#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, remaining_budget;

  int start, stop, now;

  rtems_cbs_server_id server_id = 0, tsid;
  rtems_cbs_parameters params, tparams;

  params.deadline = Period;
  params.budget = Execution+1;

  /* Taks 1 will be attached to a server, task 2 not. */
  if ( argument == 1 ) {
    printf( "Periodic task: Create server and Attach thread\n" );
    if ( rtems_cbs_create_server( &params, NULL, &server_id ) )
      printf( "ERROR: CREATE SERVER FAILED\n" );
    if ( rtems_cbs_attach_thread( server_id, Task_id ) )
      printf( "ERROR: ATTACH THREAD FAILED\n" );

    printf( "Periodic task: ID and Get parameters\n" );
    if ( rtems_cbs_get_server_id( Task_id, &tsid ) )
      printf( "ERROR: GET SERVER ID FAILED\n" );
    if ( tsid != server_id )
      printf( "ERROR: SERVER ID MISMATCH\n" );
    if ( rtems_cbs_get_parameters( server_id, &tparams ) )
      printf( "ERROR: GET PARAMETERS FAILED\n" );
    if ( params.deadline != tparams.deadline ||
         params.budget != tparams.budget )
      printf( "ERROR: PARAMETERS MISMATCH\n" );

    printf( "Periodic task: Detach thread and Destroy server\n" );
    if ( rtems_cbs_detach_thread( server_id, Task_id ) )
      printf( "ERROR: DETACH THREAD FAILED\n" );
    if ( rtems_cbs_destroy_server( server_id ) )
      printf( "ERROR: DESTROY SERVER FAILED\n" );
    if ( rtems_cbs_create_server( &params, NULL, &server_id ) )
      printf( "ERROR: CREATE SERVER FAILED\n" );

    printf( "Periodic task: Remaining budget and Execution time\n" );
    if ( rtems_cbs_get_remaining_budget( server_id, &remaining_budget ) )
      printf( "ERROR: GET REMAINING BUDGET FAILED\n" );
    if ( remaining_budget != params.budget )
      printf( "ERROR: REMAINING BUDGET MISMATCH\n" );
    if ( rtems_cbs_get_execution_time( server_id, &exec_time, &abs_time ) )
      printf( "ERROR: GET EXECUTION TIME FAILED\n" );

    printf( "Periodic task: Set parameters\n" );
    if ( rtems_cbs_attach_thread( server_id, Task_id ) )
      printf( "ERROR: ATTACH THREAD FAILED\n" );
    params.deadline = Period * 2;
    params.budget = Execution * 2 +1;
    if ( rtems_cbs_set_parameters( server_id, &params ) )
      printf( "ERROR: SET PARAMS FAILED\n" );
    if ( rtems_cbs_get_parameters( server_id, &tparams ) )
      printf( "ERROR: GET PARAMS FAILED\n" );
    if ( params.deadline != tparams.deadline ||
         params.budget != tparams.budget )
      printf( "ERROR: PARAMS MISMATCH\n" );
    params.deadline = Period;
    params.budget = Execution+1;
    if ( rtems_cbs_set_parameters( server_id, &params ) )
      printf( "ERROR: SET PARAMS FAILED\n" );
    if ( rtems_cbs_get_approved_budget( server_id, &approved_budget ) )
      printf( "ERROR: GET APPROVED BUDGET FAILED\n" );

    printf( "Periodic task: Approved budget\n" );
    if ( approved_budget != params.budget )
      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 ( server_id != 0 ) {
        if ( rtems_cbs_get_execution_time( server_id, &exec_time, &abs_time ) )
          printf( "ERROR: GET EXECUTION TIME FAILED\n" );
        if ( rtems_cbs_get_remaining_budget( server_id, &remaining_budget) )
          printf( "ERROR: GET REMAINING BUDGET FAILED\n" );
        if ( (remaining_budget + exec_time) > (Execution + 1) ) {
          printf( "ERROR: REMAINING 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 );
  }
  printf( "Periodic task: Deleting self\n" );
  status = rtems_task_delete( RTEMS_SELF );
  directive_failed( status, "rtems_task_delete of RTEMS_SELF" );
}