int clock_getres( clockid_t clock_id, struct timespec *res ) { if ( !res ) rtems_set_errno_and_return_minus_one( EINVAL ); switch ( clock_id ) { /* * All time in rtems is based on the same clock tick. */ case CLOCK_REALTIME: case CLOCK_PROCESS_CPUTIME_ID: case CLOCK_THREAD_CPUTIME_ID: if ( res ) { res->tv_sec = rtems_configuration_get_microseconds_per_tick() / TOD_MICROSECONDS_PER_SECOND; res->tv_nsec = rtems_configuration_get_nanoseconds_per_tick(); } break; default: rtems_set_errno_and_return_minus_one( EINVAL ); } return 0; }
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"); }
void _TOD_Tickle_ticks( void ) { TOD_Control *tod = &_TOD; ISR_lock_Context lock_context; Timestamp_Control tick; uint32_t nanoseconds_per_tick; nanoseconds_per_tick = rtems_configuration_get_nanoseconds_per_tick(); /* Convert the tick quantum to a timestamp */ _Timestamp_Set( &tick, 0, nanoseconds_per_tick ); /* Update the counter of ticks since boot */ _Watchdog_Ticks_since_boot += 1; _TOD_Acquire( tod, &lock_context ); /* Update the uptime */ _Timestamp_Add_to( &tod->uptime, &tick ); /* Update the current TOD */ _Timestamp_Add_to( &tod->now, &tick ); _TOD_Release( tod, &lock_context ); _TOD.seconds_trigger += nanoseconds_per_tick; if ( _TOD.seconds_trigger >= 1000000000UL ) { _TOD.seconds_trigger -= 1000000000UL; _Watchdog_Tickle_seconds(); } }
/** * POSIX 1003.1b 4.5.2 - Get Process Times */ clock_t _times( struct tms *ptms ) { rtems_interval ticks, us_per_tick; Thread_Control *executing; if ( !ptms ) rtems_set_errno_and_return_minus_one( EFAULT ); /* * This call does not depend on TOD being initialized and can't fail. */ ticks = rtems_clock_get_ticks_since_boot(); us_per_tick = rtems_configuration_get_microseconds_per_tick(); /* * RTEMS technically has no notion of system versus user time * since there is no separation of OS from application tasks. * But we can at least make a distinction between the number * of ticks since boot and the number of ticks executed by this * this thread. */ { Timestamp_Control per_tick; uint32_t ticks_of_executing; uint32_t fractional_ticks; Per_CPU_Control *cpu_self; _Timestamp_Set( &per_tick, rtems_configuration_get_microseconds_per_tick() / TOD_MICROSECONDS_PER_SECOND, (rtems_configuration_get_nanoseconds_per_tick() % TOD_NANOSECONDS_PER_SECOND) ); cpu_self = _Thread_Dispatch_disable(); executing = _Thread_Executing; _Thread_Update_cpu_time_used( executing, &_Thread_Time_of_last_context_switch ); _Timestamp_Divide( &executing->cpu_time_used, &per_tick, &ticks_of_executing, &fractional_ticks ); _Thread_Dispatch_enable( cpu_self ); ptms->tms_utime = ticks_of_executing * us_per_tick; } ptms->tms_stime = ticks * us_per_tick; ptms->tms_cutime = 0; ptms->tms_cstime = 0; return ticks * us_per_tick; }
static uint64_t large_delta_to_ns(rtems_counter_ticks d) { uint64_t ns; ns = rtems_counter_ticks_to_nanoseconds(d); /* Special case for CPU counters using the clock driver counter */ if (ns < rtems_configuration_get_nanoseconds_per_tick()) { printf( "warning: the RTEMS counter seems to be unable to\n" " measure intervals greater than the clock tick interval\n" ); ns += rtems_configuration_get_nanoseconds_per_tick(); } return ns; }
clock_t _times( struct tms *ptms ) { rtems_interval ticks; if ( !ptms ) { errno = EFAULT; return -1; } /* * This call does not depend on TOD being initialized and can't fail. */ ticks = rtems_clock_get_ticks_since_boot(); /* * RTEMS technically has no notion of system versus user time * since there is no separation of OS from application tasks. * But we can at least make a distinction between the number * of ticks since boot and the number of ticks executed by this * this thread. */ #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__ { Timestamp_Control per_tick; uint32_t ticks; uint32_t fractional_ticks; _Timestamp_Set( &per_tick, rtems_configuration_get_microseconds_per_tick() / TOD_MICROSECONDS_PER_SECOND, (rtems_configuration_get_nanoseconds_per_tick() % TOD_NANOSECONDS_PER_SECOND) ); _Timestamp_Divide( &_Thread_Executing->cpu_time_used, &per_tick, &ticks, &fractional_ticks ); ptms->tms_utime = ticks; } #else ptms->tms_utime = _Thread_Executing->cpu_time_used; #endif ptms->tms_stime = ticks; ptms->tms_cutime = 0; ptms->tms_cstime = 0; return ticks; }
rtems_status_code rtems_clock_set( rtems_time_of_day *time_buffer ) { struct timespec newtime; if ( !time_buffer ) return RTEMS_INVALID_ADDRESS; if ( _TOD_Validate( time_buffer ) ) { newtime.tv_sec = _TOD_To_seconds( time_buffer ); newtime.tv_nsec = time_buffer->ticks * rtems_configuration_get_nanoseconds_per_tick(); _Thread_Disable_dispatch(); _TOD_Set( &newtime ); _Thread_Enable_dispatch(); return RTEMS_SUCCESSFUL; } return RTEMS_INVALID_CLOCK; }
void _TOD_Tickle_ticks( void ) { Timestamp_Control tick; uint32_t seconds; /* Convert the tick quantum to a timestamp */ _Timestamp_Set( &tick, 0, rtems_configuration_get_nanoseconds_per_tick() ); /* Update the counter of ticks since boot */ _Watchdog_Ticks_since_boot += 1; /* Update the timespec format uptime */ _Timestamp_Add_to( &_TOD_Uptime, &tick ); /* we do not care how much the uptime changed */ /* Update the timespec format TOD */ seconds = _Timestamp_Add_to_at_tick( &_TOD_Now, &tick ); while ( seconds ) { _Watchdog_Tickle_seconds(); seconds--; } }
uint32_t _Timespec_To_ticks( const struct timespec *time ) { uint32_t ticks; uint32_t nanoseconds_per_tick; if ( (time->tv_sec == 0) && (time->tv_nsec == 0) ) return 0; /** * We should ensure the ticks not be truncated by integer division. We * need to have it be greater than or equal to the requested time. It * should not be shorter. */ ticks = time->tv_sec * TOD_TICKS_PER_SECOND; nanoseconds_per_tick = rtems_configuration_get_nanoseconds_per_tick(); ticks += time->tv_nsec / nanoseconds_per_tick; if ( (time->tv_nsec % nanoseconds_per_tick) != 0 ) ticks += 1; return ticks; }
rtems_status_code rtems_clock_set( const rtems_time_of_day *tod ) { if ( !tod ) return RTEMS_INVALID_ADDRESS; if ( _TOD_Validate( tod ) ) { Timestamp_Control tod_as_timestamp; uint32_t seconds = _TOD_To_seconds( tod ); uint32_t nanoseconds = tod->ticks * rtems_configuration_get_nanoseconds_per_tick(); _Timestamp_Set( &tod_as_timestamp, seconds, nanoseconds ); _Thread_Disable_dispatch(); _TOD_Set_with_timestamp( &tod_as_timestamp ); _Thread_Enable_dispatch(); return RTEMS_SUCCESSFUL; } return RTEMS_INVALID_CLOCK; }