source: rtems/testsuites/samples/nsecs/init.c @ d44c86b

4.115
Last change on this file since d44c86b was d44c86b, checked in by Ralf Corsepius <ralf.corsepius@…>, on 09/30/11 at 02:55:30

2011-09-30 Ralf Corsépius <ralf.corsepius@…>

  • nsecs/init.c: Include "pritime.h". Use PRIdtime_t to print time_t.
  • Property mode set to 100644
File size: 3.6 KB
Line 
1/*
2 *  Nanoseconds accuracy timestamp test
3 */
4
5/*  COPYRIGHT (c) 1989-2011.
6 *  On-Line Applications Research Corporation (OAR).
7 *
8 *  The license and distribution terms for this file may be
9 *  found in the file LICENSE in this distribution or at
10 *  http://www.rtems.com/license/LICENSE.
11 *
12 *  $Id$
13 */
14
15#define CONFIGURE_INIT
16
17#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <rtems.h>
22#include <inttypes.h>
23#include <stdio.h>
24#include <string.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <sys/time.h>
28#include <rtems/score/timespec.h> /* _Timespec_Substract */
29
30#include "tmacros.h"
31#include "pritime.h"
32
33char *my_ctime( time_t t )
34{
35  static char b[32];
36  ctime_r(&t, b);
37  b[ strlen(b) - 1] = '\0';
38  return b;
39}
40
41void subtract_em(
42  struct timespec *start,
43  struct timespec *stop,
44  struct timespec *t
45)
46{
47  t->tv_sec = 0;
48  t->tv_nsec = 0;
49  _Timespec_Subtract( start, stop, t );
50}
51
52/* body below .. hoping it isn't inlined */
53extern void dummy_function_empty_body_to_force_call(void);
54
55rtems_task Init(
56  rtems_task_argument argument
57)
58{
59  rtems_status_code status;
60  rtems_time_of_day time;
61  int index;
62
63  puts( "\n\n*** NANOSECOND CLOCK TEST ***" );
64
65  time.year   = 2007;
66  time.month  = 03;
67  time.day    = 24;
68  time.hour   = 11;
69  time.minute = 15;
70  time.second = 0;
71  time.ticks  = 0;
72
73  status = rtems_clock_set( &time );
74  directive_failed( status, "clock set" );
75
76  /*
77   *  Iterate 10 times showing difference in TOD
78   */
79  printf( "10 iterations of getting TOD\n" );
80  for (index=0 ; index <10 ; index++ ) {
81    struct timespec start, stop;
82    struct timespec diff;
83#if 0
84    clock_gettime( CLOCK_REALTIME, &start );
85    clock_gettime( CLOCK_REALTIME, &stop );
86#else
87    _TOD_Get( &start );
88    _TOD_Get( &stop );
89#endif
90
91    subtract_em( &start, &stop, &diff );
92    printf( "Start: %s:%ld\nStop : %s:%ld",
93      my_ctime(start.tv_sec), start.tv_nsec,
94      my_ctime(stop.tv_sec), stop.tv_nsec
95    );
96
97    printf( " --> %" PRIdtime_t ":%ld\n", diff.tv_sec, diff.tv_nsec );
98  }
99
100  /*
101   *  Iterate 10 times showing difference in Uptime
102   */
103  printf( "\n10 iterations of getting Uptime\n" );
104  for (index=0 ; index <10 ; index++ ) {
105    struct timespec start, stop;
106    struct timespec diff;
107    rtems_clock_get_uptime( &start );
108    rtems_clock_get_uptime( &stop );
109
110    subtract_em( &start, &stop, &diff );
111    printf( "%" PRIdtime_t ":%ld %" PRIdtime_t ":%ld --> %" PRIdtime_t ":%ld\n",
112      start.tv_sec, start.tv_nsec,
113      stop.tv_sec, stop.tv_nsec,
114      diff.tv_sec, diff.tv_nsec
115   );
116  }
117
118  /*
119   *  Iterate 10 times showing difference in Uptime with different counts
120   */
121  printf( "\n10 iterations of getting Uptime with different loop values\n" );
122  for (index=1 ; index <=10 ; index++ ) {
123    struct timespec start, stop;
124    struct timespec diff;
125    int j, max = (index * 10000);
126    rtems_clock_get_uptime( &start );
127      for (j=0 ; j<max ; j++ )
128        dummy_function_empty_body_to_force_call();
129    rtems_clock_get_uptime( &stop );
130
131    subtract_em( &start, &stop, &diff );
132    printf( "loop of %d %" PRIdtime_t ":%ld %" PRIdtime_t ":%ld --> %" PRIdtime_t ":%ld\n",
133      max,
134      start.tv_sec, start.tv_nsec,
135      stop.tv_sec, stop.tv_nsec,
136      diff.tv_sec, diff.tv_nsec
137   );
138  }
139
140  sleep(1);
141
142  puts( "*** END OF NANOSECOND CLOCK TEST ***" );
143  exit(0);
144}
145
146
147#include <bsp.h> /* for device driver prototypes */
148
149#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
150#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
151
152#define CONFIGURE_MICROSECONDS_PER_TICK 1000
153#define CONFIGURE_MAXIMUM_TASKS            1
154#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
155
156#include <rtems/confdefs.h>
157
Note: See TracBrowser for help on using the repository browser.