]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_marte/tests/test_non_local_jump/fosa_long_jump_calibrate.c
Reworking fosa_long_jump_calibrate to work with timespec instead of float
[frescor/fosa.git] / src_marte / tests / test_non_local_jump / fosa_long_jump_calibrate.c
1 /*
2 ** testbench_long_jump.c
3 ** 
4 ** Made by (Miguel marciano)
5 ** Login   <miguel@namir.ctr.unican.es>
6 ** 
7 ** Started on  Fri Nov 23 11:42:09 2007 Miguel marciano
8 ** Last update Sun May 12 01:17:25 2002 Speed Blue
9 */
10
11 /*
12   TO DO:
13
14   Objective:  To get the following overhead:
15
16   -  fixed_abort_ovhd:  install handler (only for worst case)
17                         save context
18                         jumped return
19                         fosa_long_jump_was_performed
20
21   -  fixed_memory_copy_ovhd:  Copying memory areas of 0 bytes
22   -  memory_copy_per_byte_ovhd:  Variable copying memory areas per byte.
23
24   NOTES:  I don't take the mutex
25
26 */
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <time.h> // For clock_nanosleep
33 #include <limits.h>
34
35 #include <assert.h>
36
37 #include "frsh_error.h"
38 #include "fosa.h"
39 #include "frsh_fosa.h"
40 #include "timespec_operations.h"
41
42
43
44 /*************************/
45 /* D E F I N I T I O N S */
46 /*************************/
47 #define NUMBER_OF_TESTS 100
48
49 static struct timespec minimum_budget_for_timer = {0, 100000};  // 100 us
50
51 #define NUMBER_OF_BYTES_TO_SIMULATE 10000  // Stack limit 40k in
52                                            // current MaRTE configuration
53
54 #define CALIBRATE_THREAD_PRIORITY (fosa_get_priority_min() + 4)
55 #define MAIN_THREAD_PRIORITY (fosa_get_priority_min() + 3)
56
57 #define MINIMUM_BUDGET_FOR_TIMER_USECS 100
58
59 #define SIGNAL_CALIBRATE_FINISHED (FRSH_SIGNAL_MIN + 6)
60
61 typedef struct _individual_results_t
62 {
63     int first_time_passed;
64     int second_time_passed;
65
66     struct timespec first_interval;
67     
68     /* The rest of min, max and average are taken over the REST of the
69        tests */
70     struct timespec average_interval;
71     struct timespec min_interval;
72     int iteration_min_interval;
73     struct timespec max_interval;
74     int iteration_max_interval;
75     struct timespec total_interval;
76     long int  number_of_tries;
77
78 } individual_results_t;
79
80 typedef struct _thread_data_t
81 {
82     individual_results_t *results;
83     frsh_thread_id_t parent_tid;
84 } thread_data_t;
85
86 typedef struct _measurements
87 {
88     individual_results_t fixed_abort_ovhd;
89     individual_results_t fixed_memory_copy_ovhd;
90     individual_results_t memory_copy_per_byte_ovhd;
91 } measurements_t;
92
93
94 /*************************/
95 /*  P R O T O T Y P E S  */
96 /*************************/
97 static void work_under_a_interruptible_budget();
98
99 static int frsh_sharedobj_calibrate(individual_results_t *fixed_abort_ovhd,
100                                     individual_results_t *fixed_memory_copy_ovhd,
101                                     individual_results_t *memory_copy_per_byte_ovhd);
102
103
104 static void *calibrate_thread_code(void *thread_arg);
105 static void process_result(individual_results_t *results, struct timespec interval);
106 static void print_results(individual_results_t results);
107
108 static struct timespec timespec_divide_by_int(struct timespec numerator, long int  denominator);
109
110
111     
112
113
114 int main()
115 {
116     int terror = -1;
117     measurements_t measurements;
118     
119     memset(&measurements, 0, sizeof(measurements) );
120
121     /* We set the signal mask */
122
123     PRW(  frsh_sharedobj_calibrate(&measurements.fixed_abort_ovhd,
124                                    &measurements.fixed_memory_copy_ovhd,
125                                    &measurements.memory_copy_per_byte_ovhd) );
126
127     /* Print info here */
128     printf("FIXED ABORT OVHD:\n");
129     print_results(measurements.fixed_abort_ovhd);
130     printf("\n\nFIXED MEMORY COPY OVHD: \n");
131     print_results(measurements.fixed_memory_copy_ovhd);
132     printf("\n\nMEMORY COPY PER BYTE OVHD: \n");
133     print_results(measurements.memory_copy_per_byte_ovhd);
134
135
136     printf("End of test\n");
137     return 0;
138 }
139
140 // -----------------------------------------------------------------
141     
142 static int frsh_sharedobj_calibrate(individual_results_t *fixed_abort_ovhd,
143                                     individual_results_t *fixed_memory_copy_ovhd,
144                                     individual_results_t *memory_copy_per_byte_ovhd)
145 {
146     int terror = -1;
147
148     frsh_signal_t signal_set[1];
149     thread_data_t thread_data;
150
151     frsh_thread_attr_t calibrate_thread_attr;
152     frsh_thread_id_t calibrate_tid;
153
154     frsh_signal_t signal_received;
155     frsh_signal_info_t signal_info_received;
156
157     fosa_clock_id_t cpu_clock;
158     struct timespec initial_time = {-1, -1};
159     struct timespec final_time = {-1, -1};
160
161     char memory_region_source[NUMBER_OF_BYTES_TO_SIMULATE];
162     char memory_region_destination[NUMBER_OF_BYTES_TO_SIMULATE];
163     int i = 0;
164
165     memset(&signal_set, 0, sizeof(signal_set) );
166     memset(&thread_data, 0, sizeof(thread_data) );
167
168     memset(&calibrate_thread_attr, 0, sizeof(calibrate_thread_attr) );
169     memset(&calibrate_tid, 0, sizeof(calibrate_tid) );
170
171     memset(&signal_received, 0, sizeof(signal_received) );
172     memset(&signal_info_received, 0, sizeof(signal_info_received) );
173     memset(&cpu_clock, 0, sizeof(cpu_clock) );
174
175     
176     /* We set the signal mask and adjust our priority */
177     /**************************************************/
178     signal_set[0] = SIGNAL_CALIBRATE_FINISHED;
179     PRW(  fosa_set_accepted_signals(signal_set, 1) );
180     PRW(  fosa_thread_set_prio(fosa_thread_self(), MAIN_THREAD_PRIORITY)  );
181
182     /* We measure the fixed_abort_ovhd */
183     /***********************************/
184     thread_data.results = fixed_abort_ovhd;
185     thread_data.parent_tid = pthread_self();
186
187     PRW(  frsh_thread_attr_init(&calibrate_thread_attr) );
188     PRW(  fosa_thread_attr_set_prio(&calibrate_thread_attr, CALIBRATE_THREAD_PRIORITY)  );
189     PRW(  fosa_thread_create(&calibrate_tid, &calibrate_thread_attr, calibrate_thread_code, 
190                              &thread_data ) );
191
192     printf("Main waits for the calibrate code to finish...\n");
193     PRW(  fosa_signal_wait(signal_set, 1, &signal_received, &signal_info_received) );
194     
195
196     /* We measure fixed_memory_copy_ovhd */
197     /*************************************/
198     PRW(  fosa_thread_get_cputime_clock( fosa_thread_self(), &cpu_clock) );
199     
200     
201     for(i = 0 ; i < NUMBER_OF_TESTS ; i++)
202     {
203         fosa_clock_get_time(FOSA_CLOCK_REALTIME, &initial_time);
204         memcpy(memory_region_destination, memory_region_source, 0);
205         fosa_clock_get_time(FOSA_CLOCK_REALTIME, &final_time);
206
207         decr_timespec(&final_time, &initial_time);
208
209         process_result(fixed_memory_copy_ovhd, final_time);
210     }
211
212     fixed_memory_copy_ovhd->average_interval = 
213         timespec_divide_by_int(fixed_memory_copy_ovhd->total_interval, 
214                                fixed_memory_copy_ovhd->number_of_tries - 1);
215
216     /* We measure memory_copy_per_byte_ovhd */
217     /****************************************/
218     for(i = 0 ; i < NUMBER_OF_TESTS ; i++)
219     {
220         fosa_clock_get_time(cpu_clock, &initial_time);  // Start measurement
221         memcpy(memory_region_destination, memory_region_source, NUMBER_OF_BYTES_TO_SIMULATE);
222         fosa_clock_get_time(cpu_clock, &final_time); // End measurement
223
224         decr_timespec(&final_time, &initial_time);
225         final_time = timespec_divide_by_int(final_time, NUMBER_OF_BYTES_TO_SIMULATE / 1024);
226
227         process_result(memory_copy_per_byte_ovhd, final_time);
228     }
229
230     memory_copy_per_byte_ovhd->average_interval =
231         timespec_divide_by_int(memory_copy_per_byte_ovhd->total_interval, 
232                                memory_copy_per_byte_ovhd->number_of_tries - 1);
233
234     return 0;
235 }
236
237
238 // ------------------------------------------------------------------------
239
240 typedef struct _protection_parameters
241 {
242     bool initialised;
243     fosa_clock_id_t cpu_clock;
244     frsh_thread_id_t jump_handler_thread;
245     frsh_signal_t jump_signal;
246     frsh_signal_info_t jump_signal_info;
247     fosa_timer_id_t jump_timer;
248 } protection_parameters_t;
249
250 // ------------------------------------------------------------------------
251
252 static void *calibrate_thread_code(void *thread_arg)
253 {
254     int terror = -1;
255
256     thread_data_t *thread_data = NULL;
257     individual_results_t *results = NULL;
258
259     protection_parameters_t protection_parameters;
260
261     frsh_signal_info_t signal_info_to_send;
262
263     memset(&protection_parameters, 0, sizeof(protection_parameters) );
264     memset(&signal_info_to_send, 0, sizeof(signal_info_to_send) );
265     
266
267     thread_data = (thread_data_t *) thread_arg;
268     results = thread_data->results;
269     
270
271     /* Periodic loop */
272     /*****************/
273     protection_parameters.initialised = false;
274
275     results->number_of_tries = 0;
276     while (results->number_of_tries < NUMBER_OF_TESTS)
277     {
278         int jumped = -1;
279         struct timespec budget = {-1, -1};
280         static fosa_long_jump_context_t context;
281
282         struct timespec before_timestamp = {-1, -1 };
283         struct timespec after_timestamp = {-1, -1 };
284
285         
286         /* We initialise variables */
287         jumped = 0;
288         memset(&before_timestamp, 0, sizeof(before_timestamp) );
289         memset(&after_timestamp, 0, sizeof(after_timestamp) );
290
291         budget.tv_sec = 0;
292         budget.tv_nsec = MINIMUM_BUDGET_FOR_TIMER_USECS * 1000;
293         
294
295         /*  S T A R T   M E A S U R I N G   H E R E  */
296         /*********************************************/
297         fosa_clock_get_time(FOSA_CLOCK_REALTIME, &before_timestamp);
298         
299
300         /* This is only executed once per thread */
301         if (protection_parameters.initialised == false)
302         {
303             PXW(  fosa_long_jump_install_handler(&protection_parameters.jump_signal, 
304                                            &protection_parameters.jump_handler_thread) );
305
306             PXW(  fosa_thread_get_cputime_clock( fosa_thread_self(), &protection_parameters.cpu_clock) );
307             protection_parameters.jump_signal_info.sival_ptr = &context;
308             
309             PXW(  fosa_timer_create_with_receiver(protection_parameters.cpu_clock, 
310                                                   protection_parameters.jump_signal, 
311                                                   protection_parameters.jump_signal_info,
312                                                   &protection_parameters.jump_timer,
313                                                   protection_parameters.jump_handler_thread)  );
314
315             protection_parameters.initialised = true;
316         }
317
318         /* We arm the jump_timer */
319         fosa_timer_arm(protection_parameters.jump_timer, false, &budget);
320         
321         /* This is the point where the jump returns */
322         fosa_long_jump_save_context(&context);
323
324         /* Query if we come from a jump */
325         fosa_long_jump_was_performed(&context, &jumped);
326         if (!jumped)
327         {
328             /* HERE COMES THE WORK THAT CAN BE INTERRUPTED */
329             work_under_a_interruptible_budget();
330             PERROR_AND_EXIT(FRSH_ERR_INTERNAL_ERROR, "The jump should always prevent us from arriving here\n");
331         }
332
333         /*  E N D    O F    M E A S U R I N G     H E R E  */
334         /***************************************************/
335         fosa_clock_get_time(FOSA_CLOCK_REALTIME, &after_timestamp);
336
337         results->number_of_tries++;
338         decr_timespec(&after_timestamp, &before_timestamp);
339
340         decr_timespec(&after_timestamp, &minimum_budget_for_timer);
341
342         process_result(results, after_timestamp);
343
344     }
345
346     results->average_interval = timespec_divide_by_int(results->total_interval, results->number_of_tries - 1);
347     
348     PXW(  fosa_signal_queue(SIGNAL_CALIBRATE_FINISHED, signal_info_to_send, thread_data->parent_tid) );
349
350     return NULL;
351 }
352
353
354 // ------------------------------------------------------------------------------
355
356 static void work_under_a_interruptible_budget()
357 {
358     struct timespec upper_execution_limit = {1, 0};
359
360     frsh_eat(&upper_execution_limit);
361 }
362
363 // ------------------------------------------------------------------------------
364
365 static void process_result(individual_results_t *results, struct timespec interval)
366 {
367     results->number_of_tries++;
368
369     if (results->first_time_passed == 0)
370     {
371         results->first_interval = interval;
372         results->first_time_passed = 1;
373     }
374     else if (results->second_time_passed == 0)
375     {
376         results->max_interval = interval;
377         results->iteration_max_interval = results->number_of_tries;
378             
379         results->min_interval = interval;
380         results->iteration_min_interval = results->number_of_tries;
381
382         results->second_time_passed = 1;
383         
384         incr_timespec(&results->total_interval, &interval);
385     }
386     else
387     {
388         if (smaller_timespec(&results->max_interval, &interval) )
389         {
390             results->max_interval = interval;
391             results->iteration_max_interval = results->number_of_tries;
392         }
393             
394         if (smaller_timespec(&interval, &results->min_interval) )
395         {
396             results->min_interval = interval;
397             results->iteration_min_interval = results->number_of_tries;
398         }
399
400         incr_timespec(&results->total_interval, &interval);
401     }
402 }
403
404 // ------------------------------------------------------------------------------
405
406 static void print_results(individual_results_t results)
407 {
408     assert(results.first_interval.tv_sec == 0);
409     assert(results.max_interval.tv_sec == 0);
410     assert(results.min_interval.tv_sec == 0);
411     assert(results.average_interval.tv_sec == 0);
412
413     printf("FIRST interval time:  %d ns.  All other stats apply to other invocations\n",
414            results.first_interval.tv_nsec);
415
416     printf("MAX interval time: %d ns at %d try,     MIN interval time: %d ns at %d try\n",
417            results.max_interval.tv_nsec, results.iteration_max_interval,
418            results.min_interval.tv_nsec, results.iteration_min_interval);
419
420     printf("AVERAGE interval time: %d ns, Number of tries %ld\n",
421            results.average_interval.tv_nsec, results.number_of_tries);
422 }
423
424
425 // ------------------------------------------------------------------------------
426
427 static struct timespec timespec_divide_by_int(struct timespec numerator, long int  denominator)
428 {
429     struct timespec result = {-1, -1};
430     long int reminder = -1;
431
432     assert(denominator < 1000000000);  // For simplicity
433
434     result.tv_sec = numerator.tv_sec/denominator;
435
436     reminder = numerator.tv_sec % denominator;
437
438     result.tv_nsec = (reminder * 1000000000 + numerator.tv_nsec)/denominator;
439
440     return result;
441 }