/* depend: */
/* cflags: */
-/* linker: mtime.o test.o stat.o -lm -lrt */
+/* linker: mtime.o test.o stat.o -lm -lpthread -lrt */
+#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
dts_t *deltas = NULL;
int nb_measurements = 0;
+pthread_mutex_t *mutex = NULL;
char *message = "Clock accurency";
void (*usage_ext) (FILE *) = NULL;
#define MAXDUR 1000
#define MINDUR 100
-int test (dts_t *buffer, int nb)
+int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
{
/* set global variables */
deltas = buffer;
nb_measurements = nb;
+ mutex = synchro;
- /* thread test */
+ return 0;
+}
+
+void *ping (__attribute__((unused)) void *arg)
+{
+
+ pthread_mutex_unlock (mutex);
for (int i = 0; i < nb_measurements; i++) {
deltas[i] = diff_timestamp (&ts2, &ts1);
}
+ pthread_exit (NULL);
+}
+
+void *pong (__attribute__((unused)) void *arg)
+{
+ pthread_mutex_unlock (mutex);
+ pthread_exit (NULL);
+}
+
+int finish ()
+{
return 0;
}
/* depend: */
/* cflags: */
-/* linker: mtime.o test.o stat.o -lm -lrt */
+/* linker: mtime.o test.o stat.o -lm -lpthread -lrt */
+#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
dts_t *deltas = NULL;
int nb_measurements = 0;
+pthread_mutex_t *mutex = NULL;
char *message = "Cycle latency";
void (*usage_ext) (FILE *) = NULL;
#define MAXDUR 1000
#define MINDUR 100
-int test (dts_t *buffer, int nb)
+int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
{
/* set global variables */
deltas = buffer;
nb_measurements = nb;
+ mutex = synchro;
- /* thread test */
+ return 0;
+}
+
+void *ping (__attribute__((unused)) void *arg)
+{
+
+ pthread_mutex_unlock (mutex);
for (int i = 0; i < nb_measurements; i++) {
deltas[i] = diff_timestamp (&ts2, &ts1) - duration;
}
+ pthread_exit (NULL);
+}
+
+void *pong (__attribute__((unused)) void *arg)
+{
+ pthread_mutex_unlock (mutex);
+ pthread_exit (NULL);
+}
+
+int finish ()
+{
return 0;
}
/* cflags: */
/* linker: msg.o mtime.o test.o stat.o -lm -lpthread -lrt */
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void (*usage_ext) (FILE *) = NULL;
int (*parse_arg_ext) (char *) = NULL;
+#define QNAME "/test_queue"
#define MSGLEN 128
#define MAXBUF 1024
-#define TIMER 10000
+#define TIMER 1000
-pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_mutex_t *mutex = NULL;
ts_t ts1, ts2;
int rc = 0;
void *ping (__attribute__((unused)) void *arg)
{
- /* open mq */
-
- struct mq_attr attr;
- attr.mq_flags = 0;
- attr.mq_maxmsg = 5;
- attr.mq_msgsize = MAXBUF;
- attr.mq_curmsgs = 0;
-
- mqd_t mq = mq_open ("/test_queue", O_CREAT | O_RDWR, 0644, &attr);
- if (mq == -1) {
- fprintf (stderr, "ping: mq_open\n");
+ mqd_t mq_out = mq_open (QNAME, O_WRONLY);
+ if (mq_out == -1) {
+ fprintf (stderr, "ping error: mq_open\n");
rc = 1;
pthread_exit (NULL);
}
printf ("Sending ping...\n");
- pthread_mutex_unlock (&mutex);
+ pthread_mutex_unlock (mutex);
usleep (TIMER);
for (int i = 0; i < nb_measurements; i++) {
- //char *msg = "ping";
char *msg = get_msg (MSGLEN);
- pthread_mutex_lock (&mutex);
- pthread_mutex_unlock (&mutex);
+ pthread_mutex_lock (mutex);
+ pthread_mutex_unlock (mutex);
sys_timestamp (&ts1);
- if (mq_send (mq, msg, strlen (msg) + 1, 0) == -1) {
+ if (mq_send (mq_out, msg, MSGLEN, 0) == -1) {
fprintf (stderr, "ping error: mq_send (%d)\n", i);
rc = 1;
pthread_exit (NULL);
usleep (TIMER);
}
- mq_close (mq);
+ mq_close (mq_out);
pthread_exit (NULL);
}
void *pong (__attribute__((unused)) void *arg)
{
- /* open mq */
-
- struct mq_attr attr;
- attr.mq_flags = 0;
- attr.mq_maxmsg = 5;
- attr.mq_msgsize = MAXBUF;
- attr.mq_curmsgs = 0;
-
- mqd_t mq = mq_open ("/test_queue", O_CREAT | O_RDWR, 0644, &attr);
- if (mq == -1) {
- fprintf (stderr, "ping: mq_open\n");
+ mqd_t mq_in = mq_open (QNAME, O_RDONLY);
+ if (mq_in == -1) {
+ fprintf (stderr, "error pong: mq_open\n");
rc = 1;
pthread_exit (NULL);
}
- /* main loop */
-
printf ("Responding pong...\n");
for (int i = 0; i < nb_measurements; i++) {
- pthread_mutex_unlock (&mutex);
+ pthread_mutex_unlock (mutex);
char buffer[MAXBUF] = { 0 };
- if (mq_receive (mq, buffer, sizeof (buffer) - 1, NULL) == -1) {
+ if (mq_receive (mq_in, buffer, sizeof (buffer), NULL) == -1) {
fprintf (stderr, "pong error: mq_receive (%d)\n", i);
rc = 1;
pthread_exit (NULL);
}
+
sys_timestamp (&ts2);
deltas[i] = diff_timestamp (&ts2, &ts1);
- pthread_mutex_lock (&mutex);
+ pthread_mutex_lock (mutex);
usleep (TIMER);
}
- mq_close (mq);
+ mq_close (mq_in);
pthread_exit (NULL);
}
-int test (dts_t *buffer, int nb)
+int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
{
/* set global variables */
deltas = buffer;
nb_measurements = nb;
+ mutex = synchro;
- /* mq latency test */
+ /* open mq */
- pthread_mutex_lock (&mutex);
+ struct mq_attr mq_attr = { 0 };
+ mq_attr.mq_flags = 0;
+ mq_attr.mq_maxmsg = 5;
+ mq_attr.mq_msgsize = MAXBUF;
+ mq_attr.mq_curmsgs = 0;
- pthread_t tid1;
- if (pthread_create (&tid1, NULL, ping, NULL) != 0) {
- fprintf (stderr, "error on pthread_create\n");
+ if ((mq_unlink (QNAME) != 0) && errno != ENOENT) {
+ fprintf (stderr, "error: mq_unlink\n");
return 1;
}
-
- pthread_mutex_lock (&mutex);
-
- pthread_t tid2;
- if (pthread_create (&tid2, NULL, pong, NULL) != 0) {
- fprintf (stderr, "error on pthread_create\n");
+ mqd_t mq = mq_open (QNAME, O_CREAT | O_RDWR, S_IWUSR | S_IRUSR, &mq_attr);
+ if (mq == -1) {
+ fprintf (stderr, "error: mq_open\n");
return 1;
}
+ mq_close (mq);
- pthread_join (tid1, NULL);
- pthread_join (tid2, NULL);
+ return 0;
+}
- mq_unlink ("/test_queue");
+int finish ()
+{
- pthread_mutex_destroy (&mutex);
+ if ((mq_unlink (QNAME) != 0) && errno != ENOENT) {
+ fprintf (stderr, "error: mq_unlink\n");
+ return 1;
+ }
return rc;
}
void (*usage_ext) (FILE *) = NULL;
int (*parse_arg_ext) (char *) = NULL;
-pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_mutex_t *mutex = NULL;
+pthread_mutex_t test = PTHREAD_MUTEX_INITIALIZER;
volatile int shared_flag = 0;
ts_t ts1, ts2;
int try = 0;
#define TIMER 1000
-void *mutex_giver_task (__attribute__((unused)) void *arg)
+void *ping (__attribute__((unused)) void *arg)
{
+ pthread_mutex_unlock (mutex);
+
while (try < nb_measurements) {
- pthread_mutex_lock (&mutex);
+ pthread_mutex_lock (&test);
usleep (TIMER);
shared_flag = 1;
sys_timestamp (&ts1);
- pthread_mutex_unlock (&mutex);
+ pthread_mutex_unlock (&test);
//shared_flag = 0;
usleep (TIMER);
}
+
pthread_exit (NULL);
}
-void *mutex_taker_task (__attribute__((unused)) void *arg)
+void *pong (__attribute__((unused)) void *arg)
{
- /* real-time process */
-/*
- struct sched_param param = {0};
- if (sched_getparam (0, ¶m) != 0) {
- fprintf (stderr, "error: sched_getparam\n");
- return 1;
- }
- param.sched_priority += 10;
- int rc = sched_setscheduler (0, SCHED_FIFO, ¶m);
- if (rc != 0) {
- fprintf (stderr, "error: sched_setscheduler\n");
- return 1;
- }
-*/
+ pthread_mutex_unlock (mutex);
while (try < nb_measurements) {
ts_t ts;
sys_timestamp (&ts);
- pthread_mutex_lock (&mutex);
+ pthread_mutex_lock (&test);
if (shared_flag) {
sys_timestamp (&ts2);
if (diff_timestamp (&ts2, &ts) < (9 * TIMER / 10)) {
}
shared_flag = 0;
}
- pthread_mutex_unlock (&mutex);
+ pthread_mutex_unlock (&test);
}
pthread_exit (NULL);
}
-int test (dts_t *buffer, int nb)
+int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
{
/* set global variables */
deltas = buffer;
nb_measurements = nb;
-
- /* mutex test */
+ mutex = synchro;
shared_flag = 1;
- pthread_t giver_thread;
- if (pthread_create (&giver_thread, NULL, mutex_giver_task, NULL) != 0) {
- fprintf (stderr, "error on pthread_create for giver_task\n");
- return 1;
- }
-
- pthread_t taker_thread;
- if (pthread_create (&taker_thread, NULL, mutex_taker_task, NULL) != 0) {
- fprintf (stderr, "error on pthread_create for taker_task\n");
- return 1;
- }
+ return 0;
+}
- pthread_join (giver_thread, NULL);
- pthread_join (taker_thread, NULL);
+int finish ()
+{
- pthread_mutex_destroy (&mutex);
+ pthread_mutex_destroy (&test);
printf ("Too fast: %d\n", nbtoofast);
- return 0;
+ return nbtoofast;
}
#define MAXBUF 1024
#define TIMER 1000
-pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_mutex_t *mutex = NULL;
ts_t ts1, ts2;
int pipefd[2] = { 0 };
int rc = 0;
printf ("Sending ping...\n");
- pthread_mutex_unlock (&mutex);
+ pthread_mutex_unlock (mutex);
usleep (TIMER);
for (int i = 0; i < nb_measurements; i++) {
char *msg = get_msg (MSGLEN);
- pthread_mutex_lock (&mutex);
- pthread_mutex_unlock (&mutex);
+ pthread_mutex_lock (mutex);
+ pthread_mutex_unlock (mutex);
sys_timestamp (&ts1);
if (write (fdout, msg, MSGLEN) == -1) {
for (int i = 0; i < nb_measurements; i++) {
- pthread_mutex_unlock (&mutex);
+ pthread_mutex_unlock (mutex);
char buffer[MAXBUF] = { 0 };
if (read (fdin, buffer, sizeof(buffer)) == -1) {
- fprintf (stderr, "ping error: read (%d)\n", i);
+ fprintf (stderr, "pong error: read (%d)\n", i);
rc = 1;
pthread_exit (NULL);
}
sys_timestamp (&ts2);
deltas[i] = diff_timestamp (&ts2, &ts1);
- pthread_mutex_lock (&mutex);
+ pthread_mutex_lock (mutex);
usleep (TIMER);
}
pthread_exit (NULL);
}
-int test (dts_t *buffer, int nb)
+int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
{
/* set global variables */
deltas = buffer;
nb_measurements = nb;
+ mutex = synchro;
/* open pipe */
return 1;
}
- /* pipe test */
-
- pthread_mutex_lock (&mutex);
-
- pthread_t tid1;
- if (pthread_create (&tid1, NULL, ping, NULL) != 0) {
- fprintf (stderr, "error on pthread_create\n");
- return 1;
- }
-
- pthread_mutex_lock (&mutex);
-
- pthread_t tid2;
- if (pthread_create (&tid2, NULL, pong, NULL) != 0) {
- fprintf (stderr, "error on pthread_create\n");
- return 1;
- }
+ return 0;
+}
- pthread_join (tid1, NULL);
- pthread_join (tid2, NULL);
+int finish ()
+{
close (pipefd[0]);
close (pipefd[1]);
- pthread_mutex_destroy (&mutex);
-
return rc;
}
void (*usage_ext) (FILE *) = NULL;
int (*parse_arg_ext) (char *) = NULL;
-pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_mutex_t *mutex = NULL;
volatile int shared_flag = 0;
ts_t ts1, ts2;
sem_t sem;
#define TIMER 1000
-void *semaphore_giver_task (__attribute__((unused)) void *arg)
+void *ping (__attribute__((unused)) void *arg)
{
+ pthread_mutex_unlock (mutex);
+
while (try < nb_measurements) {
sem_wait (&sem);
usleep (TIMER);
pthread_exit (NULL);
}
-void *semaphore_taker_task (__attribute__((unused)) void *arg)
+void *pong (__attribute__((unused)) void *arg)
{
+ pthread_mutex_unlock (mutex);
+
while (try < nb_measurements) {
ts_t ts;
sys_timestamp (&ts);
pthread_exit (NULL);
}
-int test (dts_t *buffer, int nb)
+int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
{
/* set global variables */
deltas = buffer;
nb_measurements = nb;
+ mutex = synchro;
/* semaphore test */
shared_flag = 1;
- pthread_t giver_thread;
- if (pthread_create (&giver_thread, NULL, semaphore_giver_task, NULL) != 0) {
- fprintf (stderr, "error on pthread_create for giver_task\n");
- return 1;
- }
-
- pthread_t taker_thread;
- if (pthread_create (&taker_thread, NULL, semaphore_taker_task, NULL) != 0) {
- fprintf (stderr, "error on pthread_create for taker_task\n");
- return 1;
- }
+ return 0;
+}
- pthread_join (giver_thread, NULL);
- pthread_join (taker_thread, NULL);
+int finish ()
+{
sem_destroy (&sem);
printf ("Too fast: %d\n", nbtoofast);
- return 0;
+ return nbtoofast;
}
#include <assert.h>
+#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
printf ("Test: %s\n", (message) ? message : "unknown");
- if (test (buffer, nb)) {
+ pthread_mutex_t synchro = PTHREAD_MUTEX_INITIALIZER;
+
+ if (init (buffer, nb, &synchro)) {
+ fprintf (stderr, "error on init\n");
+ return 1;
+ }
+
+ pthread_mutex_lock (&synchro);
+
+ pthread_t tid1;
+ if (pthread_create (&tid1, NULL, ping, NULL) != 0) {
+ fprintf (stderr, "error on pthread_create\n");
+ return 1;
+ }
+
+ pthread_mutex_lock (&synchro);
+
+ pthread_t tid2;
+ if (pthread_create (&tid2, NULL, pong, NULL) != 0) {
+ fprintf (stderr, "error on pthread_create\n");
+ return 1;
+ }
+
+ pthread_join (tid1, NULL);
+ pthread_join (tid2, NULL);
+
+ pthread_mutex_destroy (&synchro);
+
+ if (finish ()) {
printf ("\033[1;31mKO\033[0;0m\n");
return 1;
} else {
#ifndef __TEST_H__
#define __TEST_H__
+#include <pthread.h>
+
#include "mtime.h"
-int test (dts_t *buffer, int nb);
+int init (dts_t *buffer, int nb, pthread_mutex_t *synchro);
+
+void *ping (void *arg);
+
+void *pong (void *arg);
+
+int finish ();
#endif /* __TEST_H__ */
dts_t *deltas = NULL;
int nb_measurements = 0;
+pthread_mutex_t *mutex = NULL;
char *message = "Thread (create and join) latency";
void (*usage_ext) (FILE *) = NULL;
int (*parse_arg_ext) (char *) = NULL;
+int rc = 0;
-void* dummy_thread (__attribute__((unused)) void *arg)
-{
- pthread_exit (NULL);
-}
-
-int test (dts_t *buffer, int nb)
+int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
{
/* set global variables */
deltas = buffer;
nb_measurements = nb;
+ mutex = synchro;
+
+ return 0;
+}
+
+void* dummy_thread (__attribute__((unused)) void *arg)
+{
+ pthread_exit (NULL);
+}
+
+void *ping (__attribute__((unused)) void *arg)
+{
- /* thread test */
+ pthread_mutex_unlock (mutex);
for (int i = 0; i < nb_measurements; i++) {
pthread_t posix_t;
if (pthread_create(&posix_t, NULL, dummy_thread, NULL) != 0) {
fprintf (stderr, "error on pthread_create\n");
- return 1;
+ rc = 1;
+ pthread_exit (NULL);
}
pthread_join(posix_t, NULL);
deltas[i] = diff_timestamp (&ts2, &ts1);
}
- return 0;
+ pthread_exit (NULL);
+}
+
+void *pong (__attribute__((unused)) void *arg)
+{
+ pthread_mutex_unlock (mutex);
+ pthread_exit (NULL);
+}
+
+int finish ()
+{
+ return rc;
}
dts_t *deltas = NULL;
int nb_measurements = 0;
+pthread_mutex_t *mutex = NULL;
char *message = "Thread (create) latency";
void (*usage_ext) (FILE *) = NULL;
int (*parse_arg_ext) (char *) = NULL;
ts_t ts1, ts2;
-int try = 0;
+int rc = 0;
+
+int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
+{
+
+ /* set global variables */
+
+ deltas = buffer;
+ nb_measurements = nb;
+ mutex = synchro;
+
+ return 0;
+}
void* dummy_thread (__attribute__((unused)) void *arg)
{
pthread_exit (NULL);
}
-int test (dts_t *buffer, int nb)
+void *ping (__attribute__((unused)) void *arg)
{
- /* set global variables */
-
- deltas = buffer;
- nb_measurements = nb;
-
- /* thread test */
+ pthread_mutex_unlock (mutex);
for (int i = 0; i < nb_measurements; i++) {
pthread_t posix_t;
if (pthread_create(&posix_t, NULL, dummy_thread, NULL) != 0) {
fprintf (stderr, "error on pthread_create\n");
- return 1;
+ rc = 1;
+ pthread_exit (NULL);
}
pthread_join(posix_t, NULL);
deltas[i] = diff_timestamp (&ts2, &ts1);
}
- return 0;
+ pthread_exit (NULL);
+}
+
+void *pong (__attribute__((unused)) void *arg)
+{
+ pthread_mutex_unlock (mutex);
+ pthread_exit (NULL);
+}
+
+int finish ()
+{
+ return rc;
}
#define MAXBUF 1024
#define TIMER 1000
-pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_mutex_t *mutex = NULL;
ts_t ts1, ts2;
int rc = 0;
int localhost_ip = 0x7f000001;
printf ("Sending ping...\n");
- pthread_mutex_unlock (&mutex);
+ pthread_mutex_unlock (mutex);
usleep (TIMER);
for (int i = 0; i < nb_measurements; i++) {
char *msg = get_msg (MSGLEN);
- pthread_mutex_lock (&mutex);
- pthread_mutex_unlock (&mutex);
+ pthread_mutex_lock (mutex);
+ pthread_mutex_unlock (mutex);
sys_timestamp (&ts1);
if (sendto (sock, msg, MSGLEN, 0, (struct sockaddr *)&remote, sizeof (remote)) == -1) {
for (int i = 0; i < nb_measurements; i++) {
- pthread_mutex_unlock (&mutex);
+ pthread_mutex_unlock (mutex);
char buffer[MAXBUF] = { 0 };
if (recv (sock, buffer, MAXBUF, 0) == -1) {
sys_timestamp (&ts2);
deltas[i] = diff_timestamp (&ts2, &ts1);
- pthread_mutex_lock (&mutex);
+ pthread_mutex_lock (mutex);
usleep (TIMER);
}
pthread_exit (NULL);
}
-int test (dts_t *buffer, int nb)
+int init (dts_t *buffer, int nb, pthread_mutex_t *synchro)
{
/* set global variables */
deltas = buffer;
nb_measurements = nb;
+ mutex = synchro;
- /* udp socket test */
-
- pthread_mutex_lock (&mutex);
-
- pthread_t tid1;
- if (pthread_create (&tid1, NULL, ping, NULL) != 0) {
- fprintf (stderr, "error on pthread_create\n");
- return 1;
- }
-
- pthread_mutex_lock (&mutex);
-
- pthread_t tid2;
- if (pthread_create (&tid2, NULL, pong, NULL) != 0) {
- fprintf (stderr, "error on pthread_create\n");
- return 1;
- }
-
- pthread_join (tid1, NULL);
- pthread_join (tid2, NULL);
-
- pthread_mutex_destroy (&mutex);
+ return 0;
+}
+int finish ()
+{
return rc;
}