timespec helpers: Add wrappers with new test

This commit is contained in:
Krzysztof Mięsowicz
2012-08-29 07:38:54 -05:00
committed by Joel Sherrill
parent 3913e3cf93
commit a6eaa5489c
10 changed files with 675 additions and 1 deletions
+2
View File
@@ -15,6 +15,7 @@ include_rtems_HEADERS += include/rtems/cbs.h
include_rtems_HEADERS += include/rtems/rbheap.h
include_rtems_HEADERS += include/rtems/rbtree.h
include_rtems_HEADERS += include/rtems/sptables.h
include_rtems_HEADERS += include/rtems/timespec.h
EXTRA_DIST = include/rtems/README
@@ -22,6 +23,7 @@ include_rtems_HEADERS += inline/rtems/chain.inl
include_rtems_HEADERS += inline/rtems/extension.inl
include_rtems_HEADERS += inline/rtems/cbs.inl
include_rtems_HEADERS += inline/rtems/rbtree.inl
include_rtems_HEADERS += inline/rtems/timespec.inl
## src
AM_CPPFLAGS += -D__RTEMS_INSIDE__
+53
View File
@@ -0,0 +1,53 @@
/**
* @file rtems/sapi/timespec.h
*
* @ingroup
*
* @brief Timespec API
*
* This include file contains API for manipulating timespecs.
*/
/*
* Copyright (c) 2012-.
* Krzysztof Miesowicz krzysztof.miesowicz@gmail.com
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.com/license/LICENSE.
*/
#ifndef _RTEMS_TIMESPEC_H
#define _RTEMS_TIMESPEC_H
#include <rtems/score/timespec.h>
/**
* @defgroup TimespecAPI Timespec
*
* @ingroup ClassicRTEMS
*
* @brief Timespec API
*
*/
/**@{*/
#include <stdbool.h> /* bool */
#include <stdint.h> /* uint32_t */
#include <time.h> /* struct timespec */
#ifdef __cplusplus
extern "C" {
#endif
#include <rtems/timespec.inl>
#ifdef __cplusplus
}
#endif
/**@}*/
#endif
/* end of include file */
+296
View File
@@ -0,0 +1,296 @@
/**
* @file rtems/sapi/timespec.inl
*
* @ingroup
*
* @brief Timespec API.
*/
/*
* Copyright (c) 2012-.
* Krzysztof Miesowicz krzysztof.miesowicz@gmail.com
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.com/license/LICENSE.
*/
#ifndef _RTEMS_TIMESPEC_H
# error "Never use <rtems/timespec.inl> directly; include <rtems/timespec.h> instead."
#endif
#ifndef _RTEMS_TIMESPEC_INL
#define _RTEMS_TIMESPEC_INL
#include <rtems/score/timespec.h>
/**
* @addtogroup TimespecAPI Timespec
*
* @{
*/
/**
* @brief Is Timespec Valid
*
* This method determines the validity of a timespec.
*
* @param[in] time is the timespec instance to validate.
*
* @return This method returns true if @a time is valid and
* false otherwise.
*/
RTEMS_INLINE_ROUTINE bool rtems_timespec_is_valid(
const struct timespec *time
)
{
return _Timespec_Is_valid(time);
}
/**
* @brief Timespec Less Than Operator
*
* This method is the less than operator for timespecs.
*
* @param[in] lhs is the left hand side timespec
* @param[in] rhs is the right hand side timespec
*
* @return This method returns true if @a lhs is less than the @a rhs and
* false otherwise.
*/
RTEMS_INLINE_ROUTINE bool rtems_timespec_less_than(
const struct timespec *lhs,
const struct timespec *rhs
)
{
return _Timespec_Less_than(lhs,rhs);
}
/**
* @brief Add to a Timespec
*
* This routine adds two timespecs. The second argument is added
* to the first.
*
* @param[in] time is the base time to be added to
* @param[in] add is the timespec to add to the first argument
*
* @return This method returns the number of seconds @a time increased by.
*/
RTEMS_INLINE_ROUTINE uint32_t rtems_timespec_add_to(
struct timespec *time,
const struct timespec *add
)
{
return _Timespec_Add_to(time,add);
}
/**
* @brief Convert Timespec to Number of Ticks
*
* This routine convert the @a time timespec to the corresponding number
* of clock ticks.
*
* @param[in] time is the time to be converted
*
* @return This method returns the number of ticks computed.
*/
RTEMS_INLINE_ROUTINE uint32_t rtems_timespec_to_ticks(
const struct timespec *time
)
{
return _Timespec_To_ticks(time);
}
/**
* @brief Convert Ticks to Timespec
*
* This routine converts the @a ticks value to the corresponding
* timespec format @a time.
*
* @param[in] time is the timespec format time result
* @param[in] ticks is the number of ticks to convert
*/
RTEMS_INLINE_ROUTINE void rtems_timespec_from_ticks(
uint32_t ticks,
struct timespec *time
)
{
_Timespec_From_ticks(ticks,time);
}
/**
* @brief Subtract Two Timespec
*
* This routine subtracts two timespecs. @a result is set to
* @a end - @a start.
*
* @param[in] start is the starting time
* @param[in] end is the ending time
* @param[in] result is the difference between starting and ending time.
*
* @return This method fills in @a result.
*/
RTEMS_INLINE_ROUTINE void rtems_timespec_subtract(
const struct timespec *start,
const struct timespec *end,
struct timespec *result
)
{
_Timespec_Subtract(start,end,result);
}
/**
* @brief Divide Timespec By Integer
*
* This routine divides a timespec by an integer value. The expected
* use is to assist in benchmark calculations where you typically
* divide a duration by a number of iterations.
*
* @param[in] time is the total
* @param[in] iterations is the number of iterations
* @param[in] result is the average time.
*
* @return This method fills in @a result.
*/
RTEMS_INLINE_ROUTINE void rtems_timespec_divide_by_integer(
const struct timespec *time,
uint32_t iterations,
struct timespec *result
)
{
_Timespec_Divide_by_integer(time,iterations,result);
}
/**
* @brief Divide Timespec
*
* This routine divides a timespec by another timespec. The
* intended use is for calculating percentages to three decimal points.
*
* @param[in] lhs is the left hand number
* @param[in] rhs is the right hand number
* @param[in] ival_percentage is the integer portion of the average
* @param[in] fval_percentage is the thousandths of percentage
*
* @return This method fills in @a result.
*/
RTEMS_INLINE_ROUTINE void rtems_timespec_divide(
const struct timespec *lhs,
const struct timespec *rhs,
uint32_t *ival_percentage,
uint32_t *fval_percentage
)
{
_Timespec_Divide(lhs,rhs,ival_percentage,fval_percentage);
}
/**
* @brief Set Timespec to Seconds Nanosecond
*
* This method sets the timespec to the specified seconds and nanoseconds
* value.
*
* @param[in] _time points to the timespec instance to validate.
* @param[in] _seconds is the seconds portion of the timespec
* @param[in] _nanoseconds is the nanoseconds portion of the timespec
*/
RTEMS_INLINE_ROUTINE void rtems_timespec_set(
struct timespec *_time,
time_t _seconds,
uint32_t _nanoseconds
)
{
_Timespec_Set( _time, _seconds, _nanoseconds );
}
/**
* @brief Zero Timespec
*
* This method sets the timespec to zero.
* value.
*
* @param[in] _time points to the timespec instance to zero.
*/
RTEMS_INLINE_ROUTINE void rtems_timespec_zero(
struct timespec *_time
)
{
_Timespec_Set_to_zero( _time );
}
/**
* @brief Get Seconds Portion of Timespec
*
* This method returns the seconds portion of the specified timespec
*
* @param[in] _time points to the timespec
*
* @return The seconds portion of @a _time.
*/
RTEMS_INLINE_ROUTINE time_t rtems_timespec_get_seconds(
struct timespec *_time
)
{
return _Timespec_Get_seconds( _time );
}
/**
* @brief Get Nanoseconds Portion of Timespec
*
* This method returns the nanoseconds portion of the specified timespec
*
* @param[in] _time points to the timespec
*
* @return The nanoseconds portion of @a _time.
*/
RTEMS_INLINE_ROUTINE uint32_t rtems_timespec_get_nanoseconds(
struct timespec *_time
)
{
return _Timespec_Get_nanoseconds( _time );
}
/**
* @brief Timespec Greater Than Operator
*
* This method is the greater than operator for timespecs.
*
* @param[in] lhs is the left hand side timespec
* @param[in] rhs is the right hand side timespec
*
* @return This method returns true if @a lhs is greater than the @a rhs and
* false otherwise.
*/
RTEMS_INLINE_ROUTINE bool rtems_timespec_greater_than(
const struct timespec *_lhs,
const struct timespec *_rhs
)
{
return _Timespec_Greater_than( _lhs, _rhs );
}
/**
* @brief Timespec equal to Operator
*
* This method is the is equal to than operator for timespecs.
*
* @param[in] lhs is the left hand side timespec
* @param[in] rhs is the right hand side timespec
*
* @return This method returns true if @a lhs is equal to @a rhs and
* false otherwise.
*/
RTEMS_INLINE_ROUTINE bool rtems_timespec_equal_to(
const struct timespec *lhs,
const struct timespec *rhs
)
{
return _Timespec_Equal_to( lhs, rhs);
}
/** @} */
#endif
/* end of include file */
+8
View File
@@ -76,6 +76,10 @@ $(PROJECT_INCLUDE)/rtems/sptables.h: include/rtems/sptables.h $(PROJECT_INCLUDE)
$(INSTALL_DATA) $< $(PROJECT_INCLUDE)/rtems/sptables.h
PREINSTALL_FILES += $(PROJECT_INCLUDE)/rtems/sptables.h
$(PROJECT_INCLUDE)/rtems/timespec.h: include/rtems/timespec.h $(PROJECT_INCLUDE)/rtems/$(dirstamp)
$(INSTALL_DATA) $< $(PROJECT_INCLUDE)/rtems/timespec.h
PREINSTALL_FILES += $(PROJECT_INCLUDE)/rtems/timespec.h
$(PROJECT_INCLUDE)/rtems/chain.inl: inline/rtems/chain.inl $(PROJECT_INCLUDE)/rtems/$(dirstamp)
$(INSTALL_DATA) $< $(PROJECT_INCLUDE)/rtems/chain.inl
PREINSTALL_FILES += $(PROJECT_INCLUDE)/rtems/chain.inl
@@ -92,6 +96,10 @@ $(PROJECT_INCLUDE)/rtems/rbtree.inl: inline/rtems/rbtree.inl $(PROJECT_INCLUDE)/
$(INSTALL_DATA) $< $(PROJECT_INCLUDE)/rtems/rbtree.inl
PREINSTALL_FILES += $(PROJECT_INCLUDE)/rtems/rbtree.inl
$(PROJECT_INCLUDE)/rtems/timespec.inl: inline/rtems/timespec.inl $(PROJECT_INCLUDE)/rtems/$(dirstamp)
$(INSTALL_DATA) $< $(PROJECT_INCLUDE)/rtems/timespec.inl
PREINSTALL_FILES += $(PROJECT_INCLUDE)/rtems/timespec.inl
$(PROJECT_LIB)/libsapi.a: libsapi.a $(PROJECT_LIB)/$(dirstamp)
$(INSTALL_DATA) $< $(PROJECT_LIB)/libsapi.a
TMPINSTALL_FILES += $(PROJECT_LIB)/libsapi.a
+1 -1
View File
@@ -27,7 +27,7 @@ SUBDIRS = \
spintrcritical17 spmkdir spmountmgr01 spheapprot \
spsimplesched01 spsimplesched02 spsimplesched03 spnsext01 \
spedfsched01 spedfsched02 spedfsched03 \
spcbssched01 spcbssched02 spcbssched03 spqreslib
spcbssched01 spcbssched02 spcbssched03 spqreslib sptimespec01
include $(top_srcdir)/../automake/subdirs.am
include $(top_srcdir)/../automake/local.am
+1
View File
@@ -182,6 +182,7 @@ spsize/Makefile
spstkalloc/Makefile
spstkalloc02/Makefile
spthreadq01/Makefile
sptimespec01/Makefile
spwatchdog/Makefile
spwkspace/Makefile
])
@@ -0,0 +1,21 @@
rtems_tests_PROGRAMS = sptimespec01
sptimespec01_SOURCES = init.c
dist_rtems_tests_DATA = sptimespec01.scn
dist_rtems_tests_DATA += sptimespec01.doc
include $(RTEMS_ROOT)/make/custom/@RTEMS_BSP@.cfg
include $(top_srcdir)/../automake/compile.am
include $(top_srcdir)/../automake/leaf.am
AM_CPPFLAGS += -I$(top_srcdir)/../support/include
LINK_OBJS = $(sptimespec01_OBJECTS) $(sptimespec01_LDADD)
LINK_LIBS = $(sptimespec01_LDLIBS)
sptimespec01$(EXEEXT): $(sptimespec01_OBJECTS) $(sptimespec01_DEPENDENCIES)
@rm -f sptimespec01$(EXEEXT)
$(make-exe)
include $(top_srcdir)/../automake/local.am
+250
View File
@@ -0,0 +1,250 @@
/*
* Copyright (c) 2012-.
* Krzysztof Miesowicz krzysztof.miesowicz@gmail.com
*
* COPYRIGHT (c) 1989-2012.
* On-Line Applications Research Corporation (OAR).
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.com/license/LICENSE.
*/
#include <tmacros.h>
#include "test_support.h"
#include <rtems/timespec.h>
#include <rtems/score/tod.h>
/* forward declarations to avoid warnings */
rtems_task Init(rtems_task_argument argument);
void test_add(void);
void test_divide(void);
void test_divide_by_integer(void);
void test_compare(void);
void test_validity(void);
void test_subtract(void);
void test_convert(void);
struct timespec *timespec1;
struct timespec *timespec2;
struct timespec *tpointer;
struct timespec ts1;
struct timespec ts2;
struct timespec notvalid={-20,-50}; /* set very invalid timespec */
bool result;
rtems_task Init(
rtems_task_argument argument
)
{
timespec1=&ts1;
timespec2=&ts2;
puts( "\n\n*** TEST sptimespec01 ***" );
test_add();
test_divide();
test_divide_by_integer();
test_compare();
test_validity();
test_subtract();
test_convert();
puts( "\n*** END OF TEST sptimespec01 ***" );
rtems_test_exit(0);
}
void test_add(){
/* Basic add_to test. tv_nsec in result is in range
* (less than TOD_NANOSECONDS_PER_SECOND) */
rtems_timespec_set(timespec1, 13, 300);
rtems_timespec_set(timespec2, 26, 5000);
rtems_timespec_add_to(timespec1, timespec2);
rtems_test_assert( (timespec1->tv_sec==39)&&(timespec1->tv_nsec==5300) );
/* Another case. tv_nsec in result is greater than TOD_NANOSECONDS_PER_SECOND */
rtems_timespec_set(timespec1, 13, (TOD_NANOSECONDS_PER_SECOND-300));
rtems_timespec_set(timespec2, 26, 5000);
rtems_timespec_add_to(timespec1, timespec2);
rtems_test_assert( (timespec1->tv_sec==40)&&(timespec1->tv_nsec==4700) );
puts( "\n rtems_timespec_add_to test PASSED " );
}
void test_divide(){
/* RTEMS_TIMESPEC_DIVIDE TEST -PART1- divide by non-zero timespec */
uint32_t ival_percentage, fval_percentage;
rtems_timespec_set(timespec1, 20, 5000);
rtems_timespec_set(timespec2, 61, 5000);
rtems_timespec_divide(
timespec1,
timespec2,
&ival_percentage,
&fval_percentage
);
rtems_test_assert( (ival_percentage==32) || (fval_percentage=786) );
puts( " rtems_timespace_divide test -part1- divide by non-zero PASSED" );
/* RTEMS_TIMESPEC_DIVIDE TEST -PART2- divide by zero */
rtems_timespec_zero(timespec2);
rtems_timespec_divide(
timespec1,
timespec2,
&ival_percentage,
&fval_percentage
);
rtems_test_assert( (ival_percentage==0) && (fval_percentage==0) );
puts( " rtems_timespace_divide test -part2- divide by zero PASSED" );
}
void test_divide_by_integer(){
/* RTEMS_TIMESPEC_DIVIDE_BY_INTEGER TEST - simple divide */
rtems_timespec_set(timespec1, 40, 4700);
rtems_timespec_divide_by_integer(timespec1, 30, timespec2);
rtems_test_assert( ((timespec2->tv_sec)==1) &&
((timespec2->tv_nsec)==333333490) );
puts( " rtems_timespec_divide_by_integer test PASSED" );
}
void test_compare(){
/* Each case hit another single branch in timespeclessthan.c file */
/*
* Basic check if timespec1 is less than timespec2. Timespec1 has lower number
* of either seconds and nanoseconds.
*/
rtems_timespec_set(timespec1,2,300);
rtems_timespec_set(timespec2,3,400);
result = rtems_timespec_less_than(timespec1,timespec2);
rtems_test_assert(result);
/*
* Another check if timespec1 is less. Now number of seconds are equal in both
* timespec1 and timespec2. It hits another branch in rtems_timespec_less_than
* method.
*/
rtems_timespec_set(timespec2,2,400);
result = rtems_timespec_less_than(timespec1,timespec2);
rtems_test_assert(result);
/*
* Another check if timespec1 is less. In this case timespecs are equal so it
* should return false.
*/
rtems_timespec_set(timespec2,2,300);
result = rtems_timespec_less_than(timespec1,timespec2);
rtems_test_assert(!result);
/*
* Check if timespec1 is less. Both timespecs are equal on seconds, but
* timespec2 has smaller number of nanoseconds.
*/
rtems_timespec_set(timespec2,2,0);
result = rtems_timespec_less_than(timespec1,timespec2);
rtems_test_assert(!result);
/* In this case timespec2 is smaller in both seconds and nanoseconds. */
rtems_timespec_set(timespec2,1,400);
result = rtems_timespec_less_than(timespec1,timespec2);
rtems_test_assert(!result);
puts( " rtems_timespec_less_than PASSED ");
}
void test_validity(){
/* Each case hits another branch in timespecisvalid.c src file */
/* #1 INVALID - pointer to timespec is null */
result=rtems_timespec_is_valid(tpointer);
rtems_test_assert(!result);
tpointer=&notvalid; /* set pointer to invalid timespec */
/* #2 INVALID- tv_sec of timespec is less than 0 */
result=rtems_timespec_is_valid(tpointer);
rtems_test_assert(!result);
/* #3 INVALID- tv_nsec of timespec is less than 0 */
notvalid.tv_sec=20; /* correct seconds */
result=rtems_timespec_is_valid(tpointer);
rtems_test_assert(!result);
/* #4 INVALID- tv_nsec is not in the range - positive but too large */
notvalid.tv_nsec=TOD_NANOSECONDS_PER_SECOND+10;
result=rtems_timespec_is_valid(tpointer);
rtems_test_assert(!result);
/* #5 VALID- valid timespec */
rtems_timespec_set(tpointer,13, 500);
result=rtems_timespec_is_valid(tpointer);
rtems_test_assert(result);
puts(" rtems_timespec_is_valid test PASSED");
}
void test_subtract(){
/*
* Simple subtraction, number of nanoseconds in timespec2 is less than in
* timespec1.
*/
rtems_timespec_set(timespec1,10, 800);
rtems_timespec_set(timespec2,13, 500);
rtems_timespec_subtract(timespec1,timespec2,tpointer);
rtems_test_assert( (tpointer->tv_sec==2) &&
(tpointer->tv_nsec==(TOD_NANOSECONDS_PER_SECOND-300)) );
/*
* Simple subtraction, number of nanoseconds in timespec2 is greater than in
* timespec1. It hits another branch.
*/
rtems_timespec_set(timespec1,10,200);
rtems_timespec_subtract(timespec1,timespec2,tpointer);
rtems_test_assert( (tpointer->tv_sec==3) &&
(tpointer->tv_nsec==300) );
/*
* Case when timespec2 (end) is less than timespec1 (start). It produce
* negative result.
*/
rtems_timespec_set(timespec1,13,600);
rtems_timespec_subtract(timespec1,timespec2,tpointer);
rtems_test_assert( (tpointer->tv_sec==(-1)) && \
(tpointer->tv_nsec==999999900) );
puts(" rtems_timespec_subtract test PASSED");
}
void test_convert(){
/* RTEMS_TIMESPEC_FROM_TICKS TEST - basic conversions */
uint32_t ticks=0;
rtems_timespec_from_ticks(ticks, timespec1);
rtems_test_assert( ((timespec1->tv_sec)==0) && ((timespec1->tv_nsec)==0) );
ticks=1008;
rtems_timespec_from_ticks(ticks, timespec2);
puts( " rtems_timespec_from_ticks PASSED ");
/* RTEMS_TIMESPEC_TO_TICKS TEST - basic conversion - inverse to above ones */
ticks = rtems_timespec_to_ticks(timespec1);
rtems_test_assert(ticks==0);
ticks = rtems_timespec_to_ticks(timespec2);
rtems_test_assert(ticks==1008);
/*
* RTEMS_TIMESPEC_TO_TICKS TEST - test case when tv_nsec of timespec isn't
* multiplicity of nanoseconds_per_tick. Due to that, calculated number of ticks
* should be increased by one.
*/
uint32_t nanoseconds_per_tick;
uint32_t nanoseconds;
nanoseconds_per_tick = rtems_configuration_get_nanoseconds_per_tick();
nanoseconds = timespec2->tv_nsec;
if( (nanoseconds % nanoseconds_per_tick)==0 )
nanoseconds += 1;
rtems_timespec_set(timespec1,timespec2->tv_sec,nanoseconds);
ticks = rtems_timespec_to_ticks(timespec1);
rtems_test_assert(ticks==1009);
puts( " rtems_timespec_from_ticks and rtems_timespec_to_ticks test PASSED");
}
/* configuration information */
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_MAXIMUM_TASKS 1
#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
#define CONFIGURE_INIT
#include <rtems/confdefs.h>
/* end of file */
@@ -0,0 +1,30 @@
# COPYRIGHT (c) 1989-2011.
# On-Line Applications Research Corporation (OAR).
#
# Copyright (c) 2012
# Krzysztof Miesowicz
#
# The license and distribution terms for this file may be
# found in the file LICENSE in this distribution or at
# http://www.rtems.com/license/LICENSE.
#
This file describes the directives and concepts tested by this test set.
test set name: sp78
directives:
+ rtems_timespec_is_valid
+ rtems_timespec_less_than
+ rtems_timespec_add_to
+ rtems_timespec_to_ticks
+ rtems_timespec_from_ticks
+ rtems_timespec_subtract
+ rtems_timespec_divide_by_integer
+ rtems_timespec_divide
concepts:
This test's goal is to ensure if all timespec's related directives work as
expected.
@@ -0,0 +1,13 @@
*** TEST sptimespec01 ***
rtems_timespec_add_to test PASSED
rtems_timespace_divide test -part1- divide by non-zero PASSED
rtems_timespace_divide test -part2- divide by zero PASSED
rtems_timespec_divide_by_integer test PASSED
rtems_timespec_less_than PASSED
rtems_timespec_is_valid test PASSED
rtems_timespec_subtract test PASSED
rtems_timespec_from_ticks PASSED
rtems_timespec_from_ticks and rtems_timespec_to_ticks test PASSED
*** END OF TEST sptimespec01 ***