source: rtems/testsuites/psxtests/psx12/init.c @ 9a4eca5

Last change on this file since 9a4eca5 was 661e0e63, checked in by Sebastian Huber <sebastian.huber@…>, on Jun 15, 2016 at 9:18:10 AM

psxtests/psx12: Use and print proper rounded ms

Update #2738.

  • Property mode set to 100644
File size: 5.8 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2009.
3 *  On-Line Applications Research Corporation (OAR).
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.org/license/LICENSE.
8 */
9
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14#include <sys/time.h>
15#include <errno.h>
16#include <inttypes.h>
17#include <sched.h>
18#include <stdint.h>
19#include <unistd.h>
20
21#include <pmacros.h>
22
23const char rtems_test_name[] = "PSX 12";
24
25#define SS_REPL_PERIOD_NS 200000000
26
27#define SS_INIT_BUDGET_NS 100000000
28
29#define SS_REPL_PERIOD_MS ( SS_REPL_PERIOD_NS / 1000000 )
30
31#define SS_PRIO_LOW 1
32
33#define SS_PRIO_HIGH 2
34
35#define SS_SAMPLE_PERIODS 3
36
37typedef struct {
38  uint64_t start;
39  struct {
40    uint32_t high;
41    uint32_t low;
42  } samples[ SS_SAMPLE_PERIODS ];
43} test_context;
44
45static test_context test_instance;
46
47static void wait_for_prio( int prio )
48{
49  int                status;
50  int                policy;
51  struct sched_param param;
52
53  do {
54    status = pthread_getschedparam( pthread_self(), &policy, &param );
55    rtems_test_assert( status == 0 );
56  } while ( prio != param.sched_priority );
57}
58
59static uint64_t timeval_to_us( const struct timeval *tv )
60{
61  uint64_t t;
62
63  t = tv->tv_sec;
64  t *= 1000000;
65  t += tv->tv_usec;
66
67  return t;
68}
69
70static uint64_t now( void )
71{
72  struct timeval now;
73
74  gettimeofday( &now, NULL );
75
76  return timeval_to_us( &now );
77}
78
79static uint32_t delta_in_ms( test_context *ctx )
80{
81  uint32_t d;
82
83  d = (uint32_t) ( now() - ctx->start );
84  return ( d + 499 ) / 1000;
85}
86
87static void *sporadic_server( void *argument )
88{
89  test_context *ctx;
90  size_t        i;
91
92  ctx = argument;
93
94  for ( i = 0 ; i < SS_SAMPLE_PERIODS ; ++i ) {
95    wait_for_prio( SS_PRIO_LOW );
96    ctx->samples[ i ].high = delta_in_ms( ctx );
97    wait_for_prio( SS_PRIO_HIGH );
98    ctx->samples[ i ].low = delta_in_ms( ctx );
99  }
100
101  puts( "Sporadic Server: exitting" );
102
103  return NULL;
104}
105
106static void *POSIX_Init( void *argument )
107{
108  test_context       *ctx;
109  int                 status;
110  pthread_attr_t      attr;
111  pthread_t           thread;
112  struct sched_param  schedparam;
113  size_t              i;
114
115  TEST_BEGIN();
116
117  ctx = &test_instance;
118
119  /* set the time of day, and print our buffer in multiple ways */
120
121  set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 );
122
123  /* get id of this thread */
124
125  printf( "Init's ID is 0x%08" PRIxpthread_t "\n", pthread_self() );
126
127  /* invalid scheduling policy error */
128
129  puts( "Init: pthread_attr_init - SUCCESSFUL" );
130  status = pthread_attr_init( &attr );
131  rtems_test_assert( !status );
132
133  status = pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
134  rtems_test_assert( !status );
135  attr.schedpolicy = -1;
136
137  puts( "Init: pthread_create - EINVAL (invalid scheduling policy)" );
138  status = pthread_create( &thread, &attr, sporadic_server, NULL );
139  rtems_test_assert( status == EINVAL );
140
141  /* replenish period < budget error */
142
143  puts( "Init: pthread_attr_init - SUCCESSFUL" );
144  status = pthread_attr_init( &attr );
145  rtems_test_assert( !status );
146
147  puts( "Init: set scheduling parameter attributes for sporadic server" );
148  status = pthread_attr_setschedpolicy( &attr, SCHED_SPORADIC );
149  rtems_test_assert( !status );
150
151  schedparam.sched_ss_repl_period.tv_sec = 1;
152  schedparam.sched_ss_repl_period.tv_nsec = 0;
153  schedparam.sched_ss_init_budget.tv_sec = 2;
154  schedparam.sched_ss_init_budget.tv_nsec = 0;
155
156  schedparam.sched_priority = 200;
157  schedparam.sched_ss_low_priority = 100;
158
159  status = pthread_attr_setschedparam( &attr, &schedparam );
160  rtems_test_assert( !status );
161
162  status = pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
163  rtems_test_assert( !status );
164
165  puts( "Init: pthread_create - EINVAL (replenish < budget)" );
166  status = pthread_create( &thread, &attr, sporadic_server, NULL );
167  rtems_test_assert( status == EINVAL );
168
169  /* invalid sched_ss_low_priority error */
170
171  schedparam.sched_ss_repl_period.tv_sec = 0;
172  schedparam.sched_ss_repl_period.tv_nsec = SS_REPL_PERIOD_NS;
173  schedparam.sched_ss_init_budget.tv_sec = 0;
174  schedparam.sched_ss_init_budget.tv_nsec = SS_INIT_BUDGET_NS;
175
176  schedparam.sched_priority = SS_PRIO_HIGH;
177  schedparam.sched_ss_low_priority = -1;
178
179  status = pthread_attr_setschedparam( &attr, &schedparam );
180  rtems_test_assert( !status );
181
182  puts( "Init: pthread_create - EINVAL (invalid sched_ss_low_priority)" );
183  status = pthread_create( &thread, &attr, sporadic_server, NULL );
184  rtems_test_assert( status == EINVAL );
185
186  /* create a thread as a sporadic server */
187
188  schedparam.sched_ss_repl_period.tv_sec = 0;
189  schedparam.sched_ss_repl_period.tv_nsec = SS_REPL_PERIOD_NS;
190  schedparam.sched_ss_init_budget.tv_sec = 0;
191  schedparam.sched_ss_init_budget.tv_nsec = SS_INIT_BUDGET_NS;
192
193  schedparam.sched_priority = SS_PRIO_HIGH;
194  schedparam.sched_ss_low_priority = SS_PRIO_LOW;
195
196  status = pthread_attr_setschedparam( &attr, &schedparam );
197  rtems_test_assert( !status );
198
199  puts( "Init: pthread_create - SUCCESSFUL" );
200
201  /* Align with clock tick */
202  usleep( 1 );
203
204  ctx->start = now();
205
206  status = pthread_create( &thread, &attr, sporadic_server, ctx );
207  rtems_test_assert( !status );
208
209  status = pthread_join( thread, NULL );
210  rtems_test_assert( !status );
211
212  for ( i = 0 ; i < SS_SAMPLE_PERIODS ; ++i ) {
213    printf( "[%zu] H %3" PRIu32 "ms\n", i, ctx->samples[ i ].high );
214    printf( "[%zu] L %3" PRIu32 "ms\n", i, ctx->samples[ i ].low );
215    rtems_test_assert( ctx->samples[ i ].low / SS_REPL_PERIOD_MS == i + 1 );
216  }
217
218  TEST_END();
219  rtems_test_exit( 0 );
220
221  return NULL; /* just so the compiler thinks we returned something */
222}
223
224#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
225#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
226
227#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
228
229#define CONFIGURE_MAXIMUM_POSIX_THREADS 2
230
231#define CONFIGURE_POSIX_INIT_THREAD_TABLE
232
233#define CONFIGURE_INIT
234
235#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.