]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_marte/tests/test_cpu_clocks/test_cpu_clocks.c
Migrating FOSA trunk to d-ac2v2. Phase 1 moving FRSH-FOSA to FOSA
[frescor/fosa.git] / src_marte / tests / test_cpu_clocks / test_cpu_clocks.c
1 #include "fosa_threads_and_signals.h"
2 #include "fosa_clocks_and_timers.h"
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <time.h> // for nanosleep
6
7 #include <assert.h>
8 #include <stdlib.h> // for exit in assert
9 #include <string.h> // for memset
10 #include <timespec_operations.h>
11
12
13 /*****************************/
14 /*   D E F I N I T I O N S   */
15 /*****************************/
16 #define RT_ERROR_SIGWAIT -2
17 #define RT_ERROR_TIMER   -3
18
19 #define SIGNAL_TIMER  FOSA_SIGNAL_MAX - 1
20
21 /***************************/
22 /*   P R O T O T Y P E S   */
23 /***************************/
24 static void * thread_body(void *thread_arg);
25
26
27 static struct timespec start_execution;
28 static struct timespec signal_reception;
29
30 int main () 
31 {
32     int err = -1;
33
34     fosa_thread_attr_t attr;
35     fosa_thread_id_t tid;
36
37     fosa_signal_t signal_received;
38     fosa_signal_info_t info_programmed, info_received;
39     struct timespec work_interval = {3, 200000000}; // 3.2 seconds
40     fosa_clock_id_t clockid;
41     fosa_timer_id_t timerid;
42
43     struct timespec budget = {-1, -1};
44     fosa_signal_t signal_set[1];
45
46
47
48     memset(&attr, 0, sizeof(attr) );
49     memset(&tid, 0, sizeof(tid) );
50     memset(&signal_received, 0, sizeof(signal_received) );
51     memset(&info_programmed, 0, sizeof(info_programmed) );
52     memset(&info_received, 0, sizeof(info_received) );
53     memset(&clockid, 0, sizeof(clockid) );
54     memset(&timerid, 0, sizeof(timerid) );
55
56     memset(&start_execution, 0xFF, sizeof(start_execution) );
57     memset(&signal_received, 0xFF, sizeof(signal_received) );
58
59     /* Set the signal mask */
60     /***********************/
61     signal_set[0] = SIGNAL_TIMER;
62     if (fosa_set_accepted_signals(signal_set, 1) !=0) 
63     {
64         printf ("Error while setting the signal mask\n"); 
65         exit (1);
66     }
67
68     /* Create the thread attributes and define its priority */
69     /********************************************************/
70     if (fosa_thread_attr_init (&attr) != 0) {
71         printf("Error while initializing the attributes\n");
72         exit(1);
73     }
74
75     if (fosa_thread_attr_set_prio (&attr,fosa_get_priority_min()+3) != 0) {
76         printf("Error while setting schedparam\n");
77         exit(1);
78     }
79
80     /* create the periodic thread.  It won't execute yet */
81     /* because it has lower priority than main().        */
82     /*****************************************************/
83     err = fosa_thread_create(&tid, &attr, thread_body, NULL);
84     if (err) {
85         printf("pthread_create failed thread\n");
86         exit(1);
87     }
88
89     /* Get the thread's cputime clock */
90     /**********************************/
91     if (fosa_thread_get_cputime_clock(tid, &clockid) !=0) 
92     {
93         exit(RT_ERROR_TIMER);
94     }
95
96     /* Create a timer and arm it with a given budget */
97     /*************************************************/
98     info_programmed.sival_int = 42;
99     err = fosa_timer_create(clockid, SIGNAL_TIMER, info_programmed, &timerid);
100     printf("timer created, err=%d\n", err);
101     assert(err == 0);
102
103     budget.tv_sec = 2;
104     budget.tv_nsec = 500000000;
105     err = fosa_timer_arm(timerid, 0, &budget);
106     printf("timer armed for 2.5 secs, err=%d\n", err);
107     assert(err == 0);
108
109     /* We execute a little bit to ensure that execution time */
110     /* and real time differ                                  */
111     /*********************************************************/
112     printf("Main works for some time...\n");
113     fosa_eat(&work_interval);
114
115     /* Now we do the wait in order to allow the thread to run */
116     /**********************************************************/
117     printf("About to do the wait\n");
118     err = fosa_signal_wait(signal_set, 1 ,&signal_received, &info_received);
119
120     fosa_clock_get_time(FOSA_CLOCK_REALTIME, &signal_reception);
121     decr_timespec(&signal_reception, &start_execution);
122
123     printf("signal received=%d value=%d (42?), err=%d\n",
124            signal_received, info_received.sival_int, err);
125
126     printf("Elapsed time between sigwait and timer expiration: %d %d\n",
127            signal_reception.tv_sec, signal_reception.tv_nsec);
128
129     return 0;
130 }
131
132
133 // ----------------------------------------------------------------
134
135 static void * thread_body(void *thread_arg)
136 {
137     struct timespec before_work_time = {-1, -1};
138     struct timespec after_work_time = {-1, -1};
139     struct timespec work_interval = {1, 400000000}; // 1.4 seconds
140     int err;
141
142
143     fosa_clock_get_time(FOSA_CLOCK_REALTIME, &start_execution);
144
145     while(1) 
146     {
147         err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &before_work_time);
148         assert(err == 0);
149         printf("Start periodic work  at %d, %d\n", 
150                before_work_time.tv_sec, before_work_time.tv_nsec);
151
152         fosa_eat(&work_interval);
153
154         err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &after_work_time);
155         assert(err == 0);
156
157         printf("End periodic work  at %d, %d\n", 
158                after_work_time.tv_sec, after_work_time.tv_nsec);
159
160         decr_timespec(&after_work_time, &before_work_time);
161         printf("Elapsed time:  %d %d\n", after_work_time.tv_sec,
162                after_work_time.tv_nsec);
163     } // while
164     return NULL;
165 }
166