Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:42

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  *  @file
0005  *
0006  *  Nanoseconds accuracy timestamp test
0007  */
0008 
0009 /*
0010  *  COPYRIGHT (c) 1989-2012.
0011  *  On-Line Applications Research Corporation (OAR).
0012  *
0013  * Redistribution and use in source and binary forms, with or without
0014  * modification, are permitted provided that the following conditions
0015  * are met:
0016  * 1. Redistributions of source code must retain the above copyright
0017  *    notice, this list of conditions and the following disclaimer.
0018  * 2. Redistributions in binary form must reproduce the above copyright
0019  *    notice, this list of conditions and the following disclaimer in the
0020  *    documentation and/or other materials provided with the distribution.
0021  *
0022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0023  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0025  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0026  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0027  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0028  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0029  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0030  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0032  * POSSIBILITY OF SUCH DAMAGE.
0033  */
0034 
0035 #ifdef HAVE_CONFIG_H
0036 #include "config.h"
0037 #endif
0038 
0039 #include <rtems.h>
0040 #include <inttypes.h>
0041 #include <stdio.h>
0042 #include <string.h>
0043 #include <stdlib.h>
0044 #include <unistd.h>
0045 #include <time.h>
0046 #include <sys/time.h>
0047 
0048 #define CONFIGURE_INIT
0049 #include "system.h"
0050 
0051 #include <rtems/score/timespec.h> /* _Timespec_Substract */
0052 
0053 #include "tmacros.h"
0054 #include "pritime.h"
0055 
0056 const char rtems_test_name[] = "NANOSECOND CLOCK";
0057 
0058 static char *my_ctime( time_t t )
0059 {
0060   static char b[32];
0061   ctime_r(&t, b);
0062   b[ strlen(b) - 1] = '\0';
0063   return b;
0064 }
0065 
0066 static void subtract_em(
0067   struct timespec *start,
0068   struct timespec *stop,
0069   struct timespec *t
0070 )
0071 {
0072   t->tv_sec = 0;
0073   t->tv_nsec = 0;
0074   _Timespec_Subtract( start, stop, t );
0075 }
0076 
0077 
0078 rtems_task Init(
0079   rtems_task_argument argument
0080 )
0081 {
0082   rtems_status_code status;
0083   rtems_time_of_day time;
0084   int index;
0085 
0086   TEST_BEGIN();
0087 
0088   time.year   = 2007;
0089   time.month  = 03;
0090   time.day    = 24;
0091   time.hour   = 11;
0092   time.minute = 15;
0093   time.second = 0;
0094   time.ticks  = 0;
0095 
0096   status = rtems_clock_set( &time );
0097   directive_failed( status, "clock set" ); 
0098 
0099   /*
0100    *  Iterate 10 times showing difference in TOD
0101    */
0102   printf( "10 iterations of getting TOD\n" );
0103   for (index=0 ; index <10 ; index++ ) {
0104     struct timespec start, stop;
0105     struct timespec diff;
0106 
0107     clock_gettime( CLOCK_REALTIME, &start );
0108     clock_gettime( CLOCK_REALTIME, &stop );
0109 
0110     subtract_em( &start, &stop, &diff );
0111     printf( "Start: %s:%ld\nStop : %s:%ld",
0112       my_ctime(start.tv_sec), start.tv_nsec,
0113       my_ctime(stop.tv_sec), stop.tv_nsec
0114     );
0115 
0116     printf( " --> %" PRIdtime_t ":%ld\n", diff.tv_sec, diff.tv_nsec );
0117   }
0118 
0119   /*
0120    *  Iterate 10 times showing difference in Uptime
0121    */
0122   printf( "\n10 iterations of getting Uptime\n" );
0123   for (index=0 ; index <10 ; index++ ) {
0124     struct timespec start, stop;
0125     struct timespec diff;
0126     rtems_clock_get_uptime( &start );
0127     rtems_clock_get_uptime( &stop );
0128 
0129     subtract_em( &start, &stop, &diff );
0130     printf( "%" PRIdtime_t ":%ld %" PRIdtime_t ":%ld --> %" PRIdtime_t ":%ld\n",
0131       start.tv_sec, start.tv_nsec,
0132       stop.tv_sec, stop.tv_nsec,
0133       diff.tv_sec, diff.tv_nsec
0134    );
0135   }
0136 
0137   /*
0138    *  Iterate 10 times showing difference in Uptime with different counts
0139    */
0140   printf( "\n10 iterations of getting Uptime with different loop values\n" );
0141   for (index=1 ; index <=10 ; index++ ) {
0142     struct timespec start, stop;
0143     struct timespec diff;
0144     long j, max = (index * 10000L);
0145     rtems_clock_get_uptime( &start );
0146       for (j=0 ; j<max ; j++ )
0147         dummy_function_empty_body_to_force_call();
0148     rtems_clock_get_uptime( &stop );
0149 
0150     subtract_em( &start, &stop, &diff );
0151     printf( "loop of %ld %" PRIdtime_t
0152               ":%ld %" PRIdtime_t ":%ld --> %" PRIdtime_t ":%ld\n",
0153       max,
0154       start.tv_sec, start.tv_nsec,
0155       stop.tv_sec, stop.tv_nsec,
0156       diff.tv_sec, diff.tv_nsec
0157    );
0158   }
0159 
0160   sleep(1);
0161 
0162   TEST_END();
0163   exit(0);
0164 }
0165