#include <stdlib.h>
#include <string.h>
-//#include <pthread.h>
-
#include <fcntl.h> /* For O_* constants */
#include <sys/stat.h> /* For mode constants */
#include <mqueue.h>
{
printf ("Waiting for data...\n");
- nstime_t ts = 0, ts_old;
for (int i = 0; i < nb_measurements; i++) {
- ts_old = ts;
- ts = sys_timestamp_ns();
-
+ mtime_t ts1;
+ sys_timestamp (&ts1);
+
char buffer[MAXBUF] = { 0 };
int m = mq_receive (mq, buffer, sizeof(buffer), NULL);
if (m == -1) {
}
//printf ("receive '%s'\n", buffer);
- deltas[i] = ts - ts_old;
+ mtime_t ts2;
+ sys_timestamp (&ts2);
+ deltas[i] = diff_timestamp (&ts2, &ts1);
}
- return usec_e;
+ return nsec_e;
}
sec_t sender (void)
/* mutex test */
-/*
- pthread_attr_t th_attr;
- struct sched_param param;
-
- pthread_attr_init (&th_attr);
- pthread_attr_setschedpolicy (&th_attr, SCHED_FIFO);
- param.sched_priority = sched_get_priority_max (SCHED_FIFO);
- pthread_attr_setschedparam (&th_attr, ¶m);
- pthread_attr_setinheritsched (&th_attr, PTHREAD_EXPLICIT_SCHED);
-*/
-
- struct mq_attr mq_attr;
- mq_attr.mq_flags = 0;
- mq_attr.mq_maxmsg = 5;
- mq_attr.mq_msgsize = MAXBUF;
- mq_attr.mq_curmsgs = 0;
+ struct mq_attr attr;
+ attr.mq_flags = 0;
+ attr.mq_maxmsg = 5;
+ attr.mq_msgsize = MAXBUF;
+ attr.mq_curmsgs = 0;
- mq = mq_open ("/test_queue", O_CREAT | O_RDWR, 0644, &mq_attr);
+ mq = mq_open ("/test_queue", O_CREAT | O_RDWR, 0644, &attr);
if (mq == -1) {
fprintf (stderr, "error: mq_open\n");
return none_e;
#include "mtime.h"
-mstime_t sys_timestamp_ms (void)
+void sys_timestamp (mtime_t *ts)
{
- struct timespec ts;
- clock_gettime (CLOCK_REALTIME, &ts);
- return ts.tv_sec * 1000 + ts.tv_nsec / 10000000 ;
+ clock_gettime (CLOCK_REALTIME, ts);
}
-ustime_t sys_timestamp_us (void)
+unsigned int diff_timestamp (mtime_t *ts1, mtime_t *ts2)
{
- struct timespec ts;
- clock_gettime (CLOCK_REALTIME, &ts);
- return ts.tv_sec * 1000000 + ts.tv_nsec / 1000 ;
+ return (ts1->tv_sec - ts2->tv_sec) * 1000000000 +
+ ts1->tv_nsec - ts2->tv_nsec;
}
-nstime_t sys_timestamp_ns (void)
+void set_timestamp (mtime_t *ts1, mtime_t *ts2)
{
- struct timespec ts;
- clock_gettime (CLOCK_REALTIME, &ts);
- return ts.tv_sec * 1000000000 + ts.tv_nsec;
+ ts1->tv_sec = ts2->tv_sec;
+ ts1->tv_nsec = ts2->tv_nsec;
}
#include "inttypes.h"
-typedef int64_t mstime_t;
+typedef unsigned int mstime_t;
-mstime_t sys_timestamp_ms (void);
+typedef unsigned int ustime_t;
-typedef int64_t ustime_t;
+typedef unsigned int nstime_t;
-ustime_t sys_timestamp_us (void);
+typedef struct timespec mtime_t;
-typedef int64_t nstime_t;
+void sys_timestamp (mtime_t *ts);
-nstime_t sys_timestamp_ns (void);
+unsigned int diff_timestamp (mtime_t *ts1, mtime_t *ts2);
+
+void set_timestamp (mtime_t *ts1, mtime_t *ts2);
#endif /* __MTIME_H__ */
/* depend: */
/* cflags: */
-/* linker: mtime.o test.o stat.o -lm -lpthread */
+/* linker: mtime.o test.o stat.o -lm -lpthread -lrt */
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int shared_flag = 0;
-nstime_t timestamp1, timestamp2;
+mtime_t timestamp1, timestamp2;
int try = 0;
void *mutex_giver_task (__attribute__((unused)) void *arg)
while (try < nb_measurements) {
pthread_mutex_lock (&mutex);
shared_flag = 1;
- timestamp1 = sys_timestamp_ns ();
+ sys_timestamp (×tamp1);
pthread_mutex_unlock (&mutex);
}
pthread_exit (NULL);
while (try < nb_measurements) {
pthread_mutex_lock (&mutex);
if (shared_flag) {
- timestamp2 = sys_timestamp_ns ();
- deltas[try++] = timestamp2 - timestamp1;
+ sys_timestamp (×tamp2);
+ deltas[try++] = diff_timestamp (×tamp2, ×tamp1);
shared_flag = 0;
}
pthread_mutex_unlock (&mutex);
/* mutex test */
- pthread_t giver_thread, taker_thread;
- 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);
-
shared_flag = 1;
-
- if (pthread_create (&giver_thread, &attr, mutex_giver_task, NULL) != 0) {
+
+ 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 none_e;
}
- if (pthread_create (&taker_thread, &attr, mutex_taker_task, NULL) != 0) {
+ 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 none_e;
}
/* depend: */
/* cflags: */
-/* linker: mtime.o test.o stat.o -lm */
+/* linker: mtime.o test.o stat.o -lm -lrt */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-//#include <pthread.h>
-
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
{
printf ("Waiting for data...\n");
- nstime_t ts = 0, ts_old;
for (int i = 0; i < nb_measurements; i++) {
- ts_old = ts;
- ts = sys_timestamp_ns();
-
+ mtime_t ts1;
+ sys_timestamp (&ts1);
+
char buffer[MAXBUF] = { 0 };
if (read (fd, buffer, sizeof(buffer)) == -1) {
fprintf (stderr, "error: read (%d)\n", i);
}
//printf ("receive '%s'\n", buffer);
- deltas[i] = ts - ts_old;
+ mtime_t ts2;
+ sys_timestamp (&ts2);
+ deltas[i] = diff_timestamp (&ts2, &ts1);
}
return usec_e;
deltas = buffer;
nb_measurements = nb;
- /* mutex test */
-
-/*
- pthread_attr_t th_attr;
- struct sched_param param;
-
- pthread_attr_init (&th_attr);
- pthread_attr_setschedpolicy (&th_attr, SCHED_FIFO);
- param.sched_priority = sched_get_priority_max (SCHED_FIFO);
- pthread_attr_setschedparam (&th_attr, ¶m);
- pthread_attr_setinheritsched (&th_attr, PTHREAD_EXPLICIT_SCHED);
-*/
+ /* pipe test */
int pipefd[2] = { 0 };
/* depend: */
/* cflags: */
-/* linker: mtime.o test.o stat.o -lm -lpthread */
+/* linker: mtime.o test.o stat.o -lm -lpthread -lrt */
#include <pthread.h>
#include <semaphore.h>
/* global variables */
-ustime_t *measure_tab = NULL;
+mstime_t *measure_tab = NULL;
char *message = "semaphore";
int counter = 0;
int try = 0;
sem_t sem;
-ustime_t ts1, ts2;
+mtime_t ts1, ts2;
void* trythis (__attribute__((unused)) void *arg)
{
/* thread 1 took the lock -> timestamp 1 */
if (counter % 2 != 0) {
- ts1 = sys_timestamp_us ();
+ sys_timestamp (&ts1);
}
-
+
/* thread 2 took the lock -> timestamp 2 */
else {
- ts2 = sys_timestamp_us ();
- measure_tab[try++] = ts2 - ts1;
+ sys_timestamp (&ts2);
+ measure_tab[try++] = diff_timestamp (&ts2, &ts1);
}
sem_post (&sem);
sem_init (&sem, 0, 1);
for (int i = 0; i < nb; i++) {
- pthread_t tid1, tid2;
+
+ pthread_t tid1;
if (pthread_create (&tid1, NULL, trythis, NULL) != 0) {
fprintf (stderr, "error on pthread_create\n");
return none_e;
}
-
+
+ pthread_t tid2;
if (pthread_create (&tid2, NULL, trythis, NULL) != 0) {
fprintf (stderr, "error on pthread_create\n");
return none_e;
sem_destroy (&sem);
- return usec_e;
+ return msec_e;
}
#include <assert.h>
#include <inttypes.h>
+#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stat.h"
#include "test.h"
+/* constants */
+
+#define RT_PRIO 80
+
/* static variables */
char *progname = NULL;
}
}
+ /* 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 = RT_PRIO;
+ int rc = sched_setscheduler (0, SCHED_FIFO, ¶m); /* non-preemptive */
+ // int rc = sched_setscheduler (0, SCHED_RR, ¶m); /* preemptive */
+ if (rc != 0) {
+ fprintf (stderr, "error: sched_setscheduler\n");
+ return 1;
+ }
+
/* main process */
int64_t *buffer = (int64_t *) calloc (nb, sizeof (int64_t));
if (sec_unit == none_e) {
printf ("\033[1;31mKO\033[0;0m\n");
- exit (1);
+ return 1;
} else {
printf ("\033[1;32mOK\033[0;0m\n");
}
/* depend: */
/* cflags: */
-/* linker: mtime.o test.o stat.o -lm -lpthread */
+/* linker: mtime.o test.o stat.o -lm -lpthread -lrt */
#include <pthread.h>
#include <stdio.h>
/* global variables */
-ustime_t *deltas = NULL;
+nstime_t *deltas = NULL;
int nb_measurements = 0;
char *message = "thread";
/* thread test */
- 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++) {
- ustime_t timestamp1 = sys_timestamp_us ();
+ mtime_t ts1;
+ sys_timestamp (&ts1);
- if (pthread_create(&posix_t, &attr, dummy_thread, NULL) != 0) {
+ pthread_t posix_t;
+ if (pthread_create(&posix_t, NULL, dummy_thread, NULL) != 0) {
fprintf (stderr, "error on pthread_create\n");
return none_e;
}
pthread_join(posix_t, NULL);
- ustime_t timestamp2 = sys_timestamp_us ();
+ mtime_t ts2;
+ sys_timestamp (&ts2);
- deltas[i++] = timestamp2 - timestamp1;
+ deltas[i++] = diff_timestamp (&ts2, &ts1);
}
- pthread_attr_destroy(&attr);
-
- return usec_e;
+ return nsec_e;
}