source: rtems/testsuites/samples/nsecs/init.c @ 88c74ab

4.11
Last change on this file since 88c74ab was 88c74ab, checked in by Sebastian Huber <sebastian.huber@…>, on Jul 25, 2013 at 1:10:11 PM

score: Merge tod implementation into one file

Delete TOD_MICROSECONDS_PER_SECOND, TOD_MICROSECONDS_TO_TICKS() and
TOD_MILLISECONDS_TO_TICKS().

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