From: telleriam Date: Mon, 19 Nov 2007 19:18:09 +0000 (+0000) Subject: Adding the test_cpu_clocks to FOSA X-Git-Url: https://rtime.felk.cvut.cz/gitweb/frescor/fosa.git/commitdiff_plain/dff23ea56ecb5622d555d991754094a4a2e55c69 Adding the test_cpu_clocks to FOSA git-svn-id: http://www.frescor.org/private/svn/frescor/fosa/trunk@862 35b4ef3e-fd22-0410-ab77-dab3279adceb --- diff --git a/src_marte/tests/test_cpu_clocks/Makefile b/src_marte/tests/test_cpu_clocks/Makefile new file mode 100644 index 0000000..529e02a --- /dev/null +++ b/src_marte/tests/test_cpu_clocks/Makefile @@ -0,0 +1,12 @@ +include ../../../config.mk +include ../../../rules.mk + + +test_fosa_long_jump.exe: test_fosa_long_jump.o $(FOSA_PATH)/lib/libfosa_$(PLATFORM).a $(FOSA_PATH)/marte_non_local_jump/non_local_jump.o + $(CC) -L$(FOSA_PATH)/lib $< -lfosa_$(PLATFORM) $(FOSA_PATH)/marte_non_local_jump/non_local_jump.o -o $@ + + +simple_test_non_local_jump.exe: simple_test_non_local_jump.o $(FOSA_PATH)/lib/libfosa_$(PLATFORM).a $(FOSA_PATH)/marte_non_local_jump/non_local_jump.o + $(CC) -L$(FOSA_PATH)/lib $< -lfosa_$(PLATFORM) $(FOSA_PATH)/marte_non_local_jump/non_local_jump.o -o $@ + + diff --git a/src_marte/tests/test_cpu_clocks/test_cpu_clocks.c b/src_marte/tests/test_cpu_clocks/test_cpu_clocks.c new file mode 100644 index 0000000..164a6d4 --- /dev/null +++ b/src_marte/tests/test_cpu_clocks/test_cpu_clocks.c @@ -0,0 +1,164 @@ +#include "fosa_threads_and_signals.h" +#include "fosa_clocks_and_timers.h" +#include +#include +#include // for nanosleep + +#include +#include // for exit in assert +#include // for memset +#include + + +/*****************************/ +/* D E F I N I T I O N S */ +/*****************************/ +#define RT_ERROR_SIGWAIT -2 +#define RT_ERROR_TIMER -3 + +#define SIGNAL_TIMER FOSA_SIGNAL_MAX - 1 + +/***************************/ +/* P R O T O T Y P E S */ +/***************************/ +static void * thread_body(void *thread_arg); + + +static struct timespec start_execution; +static struct timespec signal_reception; + +int main () +{ + int err = -1; + + frsh_thread_attr_t attr; + frsh_thread_id_t tid; + + frsh_signal_t signal_received; + frsh_signal_info_t info_programmed, info_received; + + fosa_clock_id_t clockid; + fosa_timer_id_t timerid; + + struct timespec budget = {-1, -1}; + frsh_signal_t signal_set[1]; + + + + memset(&attr, 0, sizeof(attr) ); + memset(&tid, 0, sizeof(tid) ); + memset(&signal_received, 0, sizeof(signal_received) ); + memset(&info_programmed, 0, sizeof(info_programmed) ); + memset(&info_received, 0, sizeof(info_received) ); + memset(&clockid, 0, sizeof(clockid) ); + memset(&timerid, 0, sizeof(timerid) ); + + memset(&start_execution, 0xFF, sizeof(start_execution) ); + memset(&signal_received, 0xFF, sizeof(signal_received) ); + + /* Set the signal mask */ + /***********************/ + signal_set[0] = SIGNAL_TIMER; + if (fosa_set_accepted_signals(signal_set, 1) !=0) + { + printf ("Error while setting the signal mask\n"); + exit (1); + } + + /* Create the thread attributes and define its priority */ + /********************************************************/ + if (frsh_thread_attr_init (&attr) != 0) { + printf("Error while initializing the attributes\n"); + exit(1); + } + + if (fosa_thread_attr_set_prio (&attr,fosa_get_priority_min()+3) != 0) { + printf("Error while setting schedparam\n"); + exit(1); + } + + /* create the periodic thread. It won't execute yet */ + /* because it has lower priority than main(). */ + /*****************************************************/ + err = fosa_thread_create(&tid, &attr, thread_body, NULL); + if (err) { + printf("pthread_create failed thread\n"); + exit(1); + } + + /* Get the thread's cputime clock */ + /**********************************/ + if (fosa_thread_get_cputime_clock(tid, &clockid) !=0) + { + exit(RT_ERROR_TIMER); + } + + /* Create a timer and arm it with a given budget */ + /*************************************************/ + info_programmed.sival_int = 42; + err = fosa_timer_create(clockid, SIGNAL_TIMER, info_programmed, &timerid); + printf("timer created, err=%d\n", err); + assert(err == 0); + + budget.tv_sec = 2; + budget.tv_nsec = 500000000; + err = fosa_timer_arm(timerid, 0, &budget); + printf("timer armed for 2.5 secs, err=%d\n", err); + assert(err == 0); + + /* We execute a little bit to ensure that execution time */ + /* and real time differ */ + /*********************************************************/ + + + /* Now we do the wait in order to allow the thread to run */ + /**********************************************************/ + printf("About to do the wait\n"); + err = fosa_signal_wait(signal_set, 1 ,&signal_received, &info_received); + + fosa_clock_get_time(FOSA_CLOCK_REALTIME, &signal_reception); + decr_timespec(&signal_reception, &start_execution); + + printf("signal received=%d value=%d (42?), err=%d\n", + signal_received, info_received.sival_int, err); + + printf("Elapsed time between sigwait and timer expiration: %d %d\n", + signal_reception.tv_sec, signal_reception.tv_nsec); + + return 0; +} + + +// ---------------------------------------------------------------- + +static void * thread_body(void *thread_arg) +{ + struct timespec before_work_time = {-1, -1}; + struct timespec after_work_time = {-1, -1}; + struct timespec work_interval = {1, 400000000}; // 1.4 seconds + int err; + + + fosa_clock_get_time(FOSA_CLOCK_REALTIME, &start_execution); + + while(1) + { + err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &before_work_time); + assert(err == 0); + printf("Start periodic work at %d, %d\n", + before_work_time.tv_sec, before_work_time.tv_nsec); + + frsh_eat(&work_interval); + + err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &after_work_time); + assert(err == 0); + + printf("End periodic work at %d, %d\n", + after_work_time.tv_sec, after_work_time.tv_nsec); + + decr_timespec(&after_work_time, &before_work_time); + printf("Elapsed time: %d %d\n", after_work_time.tv_sec, + after_work_time.tv_nsec); + } // while +} +