]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_marte/tests/test_cpu_clocks/test_cpu_clocks.c
885bc96141a3dbf220205d908e00225189b618f0
[frescor/fosa.git] / src_marte / tests / test_cpu_clocks / test_cpu_clocks.c
1 // -----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2008 FRESCOR consortium partners:
3 //
4 //    Universidad de Cantabria,              SPAIN
5 //    University of York,                    UK
6 //    Scuola Superiore Sant'Anna,            ITALY
7 //    Kaiserslautern University,             GERMANY
8 //    Univ. Politécnica  Valencia,           SPAIN
9 //    Czech Technical University in Prague,  CZECH REPUBLIC
10 //    ENEA                                   SWEDEN
11 //    Thales Communication S.A.              FRANCE
12 //    Visual Tools S.A.                      SPAIN
13 //    Rapita Systems Ltd                     UK
14 //    Evidence                               ITALY
15 //
16 //    See http://www.frescor.org for a link to partners' websites
17 //
18 //           FRESCOR project (FP6/2005/IST/5-034026) is funded
19 //        in part by the European Union Sixth Framework Programme
20 //        The European Union is not liable of any use that may be
21 //        made of this code.
22 //
23 //
24 //  based on previous work (FSF) done in the FIRST project
25 //
26 //   Copyright (C) 2005  Mälardalen University, SWEDEN
27 //                       Scuola Superiore S.Anna, ITALY
28 //                       Universidad de Cantabria, SPAIN
29 //                       University of York, UK
30 //
31 //   FSF API web pages: http://marte.unican.es/fsf/docs
32 //                      http://shark.sssup.it/contrib/first/docs/
33 //
34 //   This file is part of FOSA (Frsh Operating System Adaption)
35 //
36 //  FOSA is free software; you can redistribute it and/or modify it
37 //  under terms of the GNU General Public License as published by the
38 //  Free Software Foundation; either version 2, or (at your option) any
39 //  later version.  FOSA is distributed in the hope that it will be
40 //  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
41 //  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
42 //  General Public License for more details. You should have received a
43 //  copy of the GNU General Public License along with FOSA; see file
44 //  COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
45 //  Cambridge, MA 02139, USA.
46 //
47 //  As a special exception, including FOSA header files in a file,
48 //  instantiating FOSA generics or templates, or linking other files
49 //  with FOSA objects to produce an executable application, does not
50 //  by itself cause the resulting executable application to be covered
51 //  by the GNU General Public License. This exception does not
52 //  however invalidate any other reasons why the executable file might be
53 //  covered by the GNU Public License.
54 // -----------------------------------------------------------------------
55
56
57 #include <unistd.h>
58 #include <stdio.h>
59
60 #include <assert.h>
61 #include <stdlib.h> // for exit in assert
62 #include <string.h> // for memset
63
64 #include "fosa.h"
65
66 /*****************************/
67 /*   D E F I N I T I O N S   */
68 /*****************************/
69 #define RT_ERROR_SIGWAIT -2
70 #define RT_ERROR_TIMER   -3
71
72 #define SIGNAL_TIMER  FOSA_SIGNAL_MAX - 1
73
74 /***************************/
75 /*   P R O T O T Y P E S   */
76 /***************************/
77 static void * thread_body(void *thread_arg);
78
79
80 static fosa_abs_time_t start_execution;
81 static fosa_abs_time_t signal_reception;
82
83 int main ()
84 {
85     int err = -1;
86
87     fosa_thread_attr_t attr;
88     fosa_thread_id_t tid;
89
90     fosa_signal_t signal_received;
91     fosa_signal_info_t info_programmed, info_received;
92     fosa_rel_time_t work_interval = fosa_msec_to_rel_time(3200); // 3.2 seconds
93     fosa_clock_id_t clockid;
94     fosa_timer_id_t timerid;
95
96     fosa_rel_time_t budget;
97     fosa_rel_time_t elapsed_time;
98     fosa_signal_t signal_set[1];
99
100     /* Set the signal mask */
101     /***********************/
102     signal_set[0] = SIGNAL_TIMER;
103     if (fosa_set_accepted_signals(signal_set, 1) !=0)
104     {
105         printf ("Error while setting the signal mask\n");
106         exit (1);
107     }
108
109     /* Create the thread attributes and define its priority */
110     /********************************************************/
111     if (fosa_thread_attr_init (&attr) != 0) {
112         printf("Error while initializing the attributes\n");
113         exit(1);
114     }
115
116     if (fosa_thread_attr_set_prio (&attr,fosa_get_priority_min()+3) != 0) {
117         printf("Error while setting schedparam\n");
118         exit(1);
119     }
120
121     /* create the periodic thread.  It won't execute yet */
122     /* because it has lower priority than main().        */
123     /*****************************************************/
124     err = fosa_thread_create(&tid, &attr, thread_body, NULL);
125     if (err) {
126         printf("pthread_create failed thread\n");
127         exit(1);
128     }
129
130     /* Get the thread's cputime clock */
131     /**********************************/
132     if (fosa_thread_get_cputime_clock(tid, &clockid) !=0)
133     {
134         exit(RT_ERROR_TIMER);
135     }
136
137     /* Create a timer and arm it with a given budget */
138     /*************************************************/
139     info_programmed.sival_int = 42;
140     err = fosa_timer_create(clockid, SIGNAL_TIMER, info_programmed, &timerid);
141     printf("timer created, err=%d\n", err);
142     assert(err == 0);
143
144     budget = fosa_msec_to_rel_time(2500);
145     err = fosa_rel_timer_arm(timerid, &budget);
146     printf("timer armed for 2.5 secs, err=%d\n", err);
147     assert(err == 0);
148
149     /* We execute a little bit to ensure that execution time */
150     /* and real time differ                                  */
151     /*********************************************************/
152     printf("Main works for some time...\n");
153     fosa_eat(&work_interval);
154
155     /* Now we do the wait in order to allow the thread to run */
156     /**********************************************************/
157     printf("About to do the wait\n");
158     err = fosa_signal_wait(signal_set, 1 ,&signal_received, &info_received);
159
160     fosa_clock_get_time(FOSA_CLOCK_REALTIME, &signal_reception);
161     elapsed_time = fosa_abs_time_extract_interval(start_execution, signal_reception);
162
163     printf("signal received=%d value=%d (42?), err=%d\n",
164            signal_received, info_received.sival_int, err);
165
166     printf("Elapsed time between sigwait and timer expiration: %ld msecs\n",
167            fosa_rel_time_to_msec(elapsed_time) );
168
169     return 0;
170 }
171
172
173 // ----------------------------------------------------------------
174
175 static void * thread_body(void *thread_arg)
176 {
177     fosa_abs_time_t before_work_time;
178     fosa_abs_time_t after_work_time;
179     fosa_rel_time_t work_interval = fosa_msec_to_rel_time(1400);
180     fosa_rel_time_t elapsed_time;
181     int err;
182
183     fosa_clock_get_time(FOSA_CLOCK_REALTIME, &start_execution);
184
185     while(1)
186     {
187         err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &before_work_time);
188         assert(err == 0);
189         printf("Start periodic work  at %ld msecs\n", fosa_abs_time_to_msec(before_work_time) );
190
191         fosa_eat(&work_interval);
192
193         err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &after_work_time);
194         assert(err == 0);
195
196         printf("End periodic work  at %ld msecs\n", fosa_abs_time_to_msec(after_work_time) );
197         elapsed_time = fosa_abs_time_extract_interval(before_work_time, after_work_time);
198
199         printf("Elapsed time:  %ld msecs\n", fosa_rel_time_to_msec(elapsed_time) );
200
201     } // while
202     return NULL;
203 }
204