2 ** testbench_long_jump.c
4 ** Made by (Miguel marciano)
5 ** Login <miguel@namir.ctr.unican.es>
7 ** Started on Fri Nov 23 11:42:09 2007 Miguel marciano
8 ** Last update Sun May 12 01:17:25 2002 Speed Blue
14 Objective: To get the following overhead:
16 - fixed_abort_ovhd: install handler (only for worst case)
19 fosa_long_jump_was_performed
21 - fixed_memory_copy_ovhd: Copying memory areas of 0 bytes
22 - memory_copy_per_byte_ovhd: Variable copying memory areas per byte.
24 NOTES: I don't take the mutex
32 #include <time.h> // For clock_nanosleep
37 #include "frsh_error.h"
39 #include "frsh_fosa.h"
40 #include "timespec_operations.h"
44 /*************************/
45 /* D E F I N I T I O N S */
46 /*************************/
47 #define NUMBER_OF_TESTS 100
49 static struct timespec minimum_budget_for_timer = {0, 100000}; // 100 us
51 #define NUMBER_OF_BYTES_TO_SIMULATE 10000 // Stack limit 40k in
52 // current MaRTE configuration
54 #define CALIBRATE_THREAD_PRIORITY (fosa_get_priority_min() + 4)
55 #define MAIN_THREAD_PRIORITY (fosa_get_priority_min() + 3)
57 #define MINIMUM_BUDGET_FOR_TIMER_USECS 100
59 #define SIGNAL_CALIBRATE_FINISHED (FRSH_SIGNAL_MIN + 6)
61 typedef struct _individual_results_t
63 int first_time_passed;
64 int second_time_passed;
66 struct timespec first_interval;
68 /* The rest of min, max and average are taken over the REST of the
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;
78 } individual_results_t;
80 typedef struct _thread_data_t
82 individual_results_t *results;
83 frsh_thread_id_t parent_tid;
86 typedef struct _measurements
88 individual_results_t fixed_abort_ovhd;
89 individual_results_t fixed_memory_copy_ovhd;
90 individual_results_t memory_copy_per_byte_ovhd;
94 /*************************/
95 /* P R O T O T Y P E S */
96 /*************************/
97 static void work_under_a_interruptible_budget();
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);
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);
108 static struct timespec timespec_divide_by_int(struct timespec numerator, long int denominator);
117 measurements_t measurements;
119 memset(&measurements, 0, sizeof(measurements) );
121 /* We set the signal mask */
123 PRW( frsh_sharedobj_calibrate(&measurements.fixed_abort_ovhd,
124 &measurements.fixed_memory_copy_ovhd,
125 &measurements.memory_copy_per_byte_ovhd) );
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);
136 printf("End of test\n");
140 // -----------------------------------------------------------------
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)
148 frsh_signal_t signal_set[1];
149 thread_data_t thread_data;
151 frsh_thread_attr_t calibrate_thread_attr;
152 frsh_thread_id_t calibrate_tid;
154 frsh_signal_t signal_received;
155 frsh_signal_info_t signal_info_received;
157 fosa_clock_id_t cpu_clock;
158 struct timespec initial_time = {-1, -1};
159 struct timespec final_time = {-1, -1};
161 char memory_region_source[NUMBER_OF_BYTES_TO_SIMULATE];
162 char memory_region_destination[NUMBER_OF_BYTES_TO_SIMULATE];
165 memset(&signal_set, 0, sizeof(signal_set) );
166 memset(&thread_data, 0, sizeof(thread_data) );
168 memset(&calibrate_thread_attr, 0, sizeof(calibrate_thread_attr) );
169 memset(&calibrate_tid, 0, sizeof(calibrate_tid) );
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) );
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) );
182 /* We measure the fixed_abort_ovhd */
183 /***********************************/
184 thread_data.results = fixed_abort_ovhd;
185 thread_data.parent_tid = pthread_self();
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,
192 printf("Main waits for the calibrate code to finish...\n");
193 PRW( fosa_signal_wait(signal_set, 1, &signal_received, &signal_info_received) );
196 /* We measure fixed_memory_copy_ovhd */
197 /*************************************/
198 PRW( fosa_thread_get_cputime_clock( fosa_thread_self(), &cpu_clock) );
201 for(i = 0 ; i < NUMBER_OF_TESTS ; i++)
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);
207 decr_timespec(&final_time, &initial_time);
209 process_result(fixed_memory_copy_ovhd, final_time);
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);
216 /* We measure memory_copy_per_byte_ovhd */
217 /****************************************/
218 for(i = 0 ; i < NUMBER_OF_TESTS ; i++)
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
224 decr_timespec(&final_time, &initial_time);
225 final_time = timespec_divide_by_int(final_time, NUMBER_OF_BYTES_TO_SIMULATE / 1024);
227 process_result(memory_copy_per_byte_ovhd, final_time);
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);
238 // ------------------------------------------------------------------------
240 typedef struct _protection_parameters
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;
250 // ------------------------------------------------------------------------
252 static void *calibrate_thread_code(void *thread_arg)
256 thread_data_t *thread_data = NULL;
257 individual_results_t *results = NULL;
259 protection_parameters_t protection_parameters;
261 frsh_signal_info_t signal_info_to_send;
263 memset(&protection_parameters, 0, sizeof(protection_parameters) );
264 memset(&signal_info_to_send, 0, sizeof(signal_info_to_send) );
267 thread_data = (thread_data_t *) thread_arg;
268 results = thread_data->results;
273 protection_parameters.initialised = false;
275 results->number_of_tries = 0;
276 while (results->number_of_tries < NUMBER_OF_TESTS)
279 struct timespec budget = {-1, -1};
280 static fosa_long_jump_context_t context;
282 struct timespec before_timestamp = {-1, -1 };
283 struct timespec after_timestamp = {-1, -1 };
286 /* We initialise variables */
288 memset(&before_timestamp, 0, sizeof(before_timestamp) );
289 memset(&after_timestamp, 0, sizeof(after_timestamp) );
292 budget.tv_nsec = MINIMUM_BUDGET_FOR_TIMER_USECS * 1000;
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);
300 /* This is only executed once per thread */
301 if (protection_parameters.initialised == false)
303 PXW( fosa_long_jump_install_handler(&protection_parameters.jump_signal,
304 &protection_parameters.jump_handler_thread) );
306 PXW( fosa_thread_get_cputime_clock( fosa_thread_self(), &protection_parameters.cpu_clock) );
307 protection_parameters.jump_signal_info.sival_ptr = &context;
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) );
315 protection_parameters.initialised = true;
318 /* We arm the jump_timer */
319 fosa_timer_arm(protection_parameters.jump_timer, false, &budget);
321 /* This is the point where the jump returns */
322 fosa_long_jump_save_context(&context);
324 /* Query if we come from a jump */
325 fosa_long_jump_was_performed(&context, &jumped);
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");
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);
337 results->number_of_tries++;
338 decr_timespec(&after_timestamp, &before_timestamp);
340 decr_timespec(&after_timestamp, &minimum_budget_for_timer);
342 process_result(results, after_timestamp);
346 results->average_interval = timespec_divide_by_int(results->total_interval, results->number_of_tries - 1);
348 PXW( fosa_signal_queue(SIGNAL_CALIBRATE_FINISHED, signal_info_to_send, thread_data->parent_tid) );
354 // ------------------------------------------------------------------------------
356 static void work_under_a_interruptible_budget()
358 struct timespec upper_execution_limit = {1, 0};
360 frsh_eat(&upper_execution_limit);
363 // ------------------------------------------------------------------------------
365 static void process_result(individual_results_t *results, struct timespec interval)
367 results->number_of_tries++;
369 if (results->first_time_passed == 0)
371 results->first_interval = interval;
372 results->first_time_passed = 1;
374 else if (results->second_time_passed == 0)
376 results->max_interval = interval;
377 results->iteration_max_interval = results->number_of_tries;
379 results->min_interval = interval;
380 results->iteration_min_interval = results->number_of_tries;
382 results->second_time_passed = 1;
384 incr_timespec(&results->total_interval, &interval);
388 if (smaller_timespec(&results->max_interval, &interval) )
390 results->max_interval = interval;
391 results->iteration_max_interval = results->number_of_tries;
394 if (smaller_timespec(&interval, &results->min_interval) )
396 results->min_interval = interval;
397 results->iteration_min_interval = results->number_of_tries;
400 incr_timespec(&results->total_interval, &interval);
404 // ------------------------------------------------------------------------------
406 static void print_results(individual_results_t results)
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);
413 printf("FIRST interval time: %d ns. All other stats apply to other invocations\n",
414 results.first_interval.tv_nsec);
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);
420 printf("AVERAGE interval time: %d ns, Number of tries %ld\n",
421 results.average_interval.tv_nsec, results.number_of_tries);
425 // ------------------------------------------------------------------------------
427 static struct timespec timespec_divide_by_int(struct timespec numerator, long int denominator)
429 struct timespec result = {-1, -1};
430 long int reminder = -1;
432 assert(denominator < 1000000000); // For simplicity
434 result.tv_sec = numerator.tv_sec/denominator;
436 reminder = numerator.tv_sec % denominator;
438 result.tv_nsec = (reminder * 1000000000 + numerator.tv_nsec)/denominator;