From: Laurent Mazet Date: Sat, 27 Sep 2025 19:01:51 +0000 (+0200) Subject: add thread test X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=0b3b43e21eb4c80beacca8cc790a28069cb9a039;p=benchmarks.git add thread test --- diff --git a/thread.c b/thread.c new file mode 100644 index 0000000..70306a7 --- /dev/null +++ b/thread.c @@ -0,0 +1,55 @@ +/* depend: */ +/* cflags: */ +/* linker: mustime.o test.o stat.o -lm -lpthread */ + +#include +#include + +#include "mustime.h" +#include "stat.h" +#include "test.h" + +/* global variables */ + +mustime_t *deltas = NULL; +int nb_measurements = 0; + +void* dummy_thread(void *arg) { + (void)arg; + return NULL; +} + +sec_t test (int64_t *buffer, int nb) +{ + + /* set global variables */ + + deltas = buffer; + nb_measurements = nb; + + pthread_t posix_t; + pthread_attr_t attr; + struct sched_param param; + + pthread_attr_init(&attr); + pthread_attr_setschedpolicy(&attr, SCHED_FIFO); + param.sched_priority = sched_get_priority_max(SCHED_FIFO); + pthread_attr_setschedparam(&attr, ¶m); + pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); + + for (int i = 0; i < nb_measurements; i++) { + + mustime_t timestamp1 = sys_timestamp (); + + pthread_create(&posix_t, &attr, dummy_thread, NULL); + pthread_join(posix_t, NULL); + + mustime_t timestamp2 = sys_timestamp (); + + deltas[i++] = timestamp2 - timestamp1; + } + + pthread_attr_destroy(&attr); + + return musec_e; +} diff --git a/todo/instantiation_thread.c b/todo/instantiation_thread.c deleted file mode 100644 index e72fe22..0000000 --- a/todo/instantiation_thread.c +++ /dev/null @@ -1,75 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#define NUM_MEASUREMENTS 10000 - -double deltas_us[NUM_MEASUREMENTS]; - -void* dummy_thread(void *arg) { - (void)arg; - return NULL; -} - -double timespec_diff_us(struct timespec *start, struct timespec *end) { - return (end->tv_sec - start->tv_sec) * 1e6 + - (end->tv_nsec - start->tv_nsec) / 1e3; -} - -int main(void) { - pthread_t posix_t; - pthread_attr_t attr; - struct sched_param param; - - pthread_attr_init(&attr); - pthread_attr_setschedpolicy(&attr, SCHED_FIFO); - param.sched_priority = sched_get_priority_max(SCHED_FIFO); - pthread_attr_setschedparam(&attr, ¶m); - pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); - - struct timespec t_start, t_end; - int count = 0; - - while (count < NUM_MEASUREMENTS) { - clock_gettime(CLOCK_MONOTONIC, &t_start); - - pthread_create(&posix_t, &attr, dummy_thread, NULL); - pthread_join(posix_t, NULL); - - clock_gettime(CLOCK_MONOTONIC, &t_end); - - deltas_us[count] = timespec_diff_us(&t_start, &t_end); - count++; - } - - pthread_attr_destroy(&attr); - - // Calcul des statistiques - double sum = 0.0, min = deltas_us[0], max = deltas_us[0]; - for (int i = 0; i < NUM_MEASUREMENTS; i++) { - double d = deltas_us[i]; - sum += d; - if (d < min) min = d; - if (d > max) max = d; - } - - double avg = sum / NUM_MEASUREMENTS; - - double stddev_sum = 0.0; - for (int i = 0; i < NUM_MEASUREMENTS; i++) { - stddev_sum += (deltas_us[i] - avg) * (deltas_us[i] - avg); - } - double stddev = sqrt(stddev_sum / NUM_MEASUREMENTS); - - printf("\n==== Timing Statistics (%d measurements) ====\n", NUM_MEASUREMENTS); - printf("Moyenne : %.3f µs\n", avg); - printf("Minimum : %.3f µs\n", min); - printf("Maximum : %.3f µs\n", max); - printf("Ecart-type : %.3f µs\n", stddev); - printf("============================================\n"); - - return 0; -}