+++ /dev/null
-#include <time.h>
-
-#include "mstime.h"
-
-mstime_t timespec_as_milliseconds (struct timespec ts)
-{
- return ts.tv_sec * 1000 + ts.tv_nsec / 10000000 ;
-}
-
-mstime_t sys_timestamp (void)
-{
- struct timespec ts;
- clock_gettime (CLOCK_REALTIME, &ts);
- return timespec_as_milliseconds (ts);
-}
+++ /dev/null
-/* millisecond time module */
-
-#ifndef __MSTIME_H__
-#define __MSTIME_H__
-
-#include "inttypes.h"
-
-typedef int64_t mstime_t;
-
-mstime_t sys_timestamp (void);
-
-#endif /* __MSTIME_H__ */
--- /dev/null
+#include <time.h>
+
+#include "mtime.h"
+
+mstime_t sys_timestamp_ms (void)
+{
+ struct timespec ts;
+ clock_gettime (CLOCK_REALTIME, &ts);
+ return ts.tv_sec * 1000 + ts.tv_nsec / 10000000 ;
+}
+
+ustime_t sys_timestamp_us (void)
+{
+ struct timespec ts;
+ clock_gettime (CLOCK_REALTIME, &ts);
+ return ts.tv_sec * 1000000 + ts.tv_nsec / 1000 ;
+}
+
+nstime_t sys_timestamp_ns (void)
+{
+ struct timespec ts;
+ clock_gettime (CLOCK_REALTIME, &ts);
+ return ts.tv_sec * 1000000000 + ts.tv_nsec;
+}
--- /dev/null
+/* machine time module */
+
+#ifndef __MTIME_H__
+#define __MTIME_H__
+
+#include "inttypes.h"
+
+typedef int64_t mstime_t;
+
+mstime_t sys_timestamp_ms (void);
+
+typedef int64_t ustime_t;
+
+ustime_t sys_timestamp_us (void);
+
+typedef int64_t nstime_t;
+
+nstime_t sys_timestamp_ns (void);
+
+#endif /* __MTIME_H__ */
+++ /dev/null
-#include <time.h>
-
-#include "mustime.h"
-
-mustime_t timespec_as_microseconds (struct timespec ts)
-{
- return ts.tv_sec * 1000000 + ts.tv_nsec / 1000 ;
-}
-
-mustime_t sys_timestamp (void)
-{
- struct timespec ts;
- clock_gettime (CLOCK_REALTIME, &ts);
- return timespec_as_microseconds (ts);
-}
+++ /dev/null
-/* microsecond time module */
-
-#ifndef __MUSTIME_H__
-#define __MUSTIME_H__
-
-#include "inttypes.h"
-
-typedef int64_t mustime_t;
-
-mustime_t sys_timestamp (void);
-
-#endif /* __MUSTIME_H__ */
/* depend: */
/* cflags: */
-/* linker: nstime.o test.o stat.o -lm -lpthread */
+/* linker: mtime.o test.o stat.o -lm -lpthread */
#include <pthread.h>
#include <stdio.h>
-#include "nstime.h"
-#include "stat.h"
+#include "mtime.h"
#include "test.h"
/* global variables */
nstime_t *deltas = NULL;
int nb_measurements = 0;
+char *message = "mutex";
+
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int shared_flag = 0;
nstime_t timestamp1, timestamp2;
while (try < nb_measurements) {
pthread_mutex_lock (&mutex);
shared_flag = 1;
- timestamp1 = sys_timestamp ();
+ timestamp1 = sys_timestamp_ns ();
pthread_mutex_unlock (&mutex);
}
pthread_exit (NULL);
while (try < nb_measurements) {
pthread_mutex_lock (&mutex);
if (shared_flag) {
- timestamp2 = sys_timestamp ();
+ timestamp2 = sys_timestamp_ns ();
deltas[try++] = timestamp2 - timestamp1;
shared_flag = 0;
}
deltas = buffer;
nb_measurements = nb;
- /* main process */
+ /* mutex test */
pthread_t giver_thread, taker_thread;
pthread_attr_t attr;
if (pthread_create (&giver_thread, &attr, mutex_giver_task, NULL) != 0) {
fprintf (stderr, "error on pthread_create for giver_task\n");
- return 1;
+ return none_e;
}
if (pthread_create (&taker_thread, &attr, mutex_taker_task, NULL) != 0) {
fprintf (stderr, "error on pthread_create for taker_task\n");
- return 1;
+ return none_e;
}
pthread_join (giver_thread, NULL);
+++ /dev/null
-#include <time.h>
-
-#include "nstime.h"
-
-nstime_t timespec_as_nanoseconds (struct timespec ts)
-{
- return ts.tv_sec * 1000000000 + ts.tv_nsec;
-}
-
-nstime_t sys_timestamp (void)
-{
- struct timespec ts;
- clock_gettime (CLOCK_REALTIME, &ts);
- return timespec_as_nanoseconds (ts);
-}
+++ /dev/null
-/* nanosecond time module */
-
-#ifndef __NSTIME_H__
-#define __NSTIME_H__
-
-#include "inttypes.h"
-
-typedef int64_t nstime_t;
-
-nstime_t sys_timestamp (void);
-
-#endif /* __NSTIME_H__ */
/* depend: */
/* cflags: */
-/* linker: mustime.o test.o stat.o -lm -lpthread */
+/* linker: mtime.o test.o stat.o -lm -lpthread */
#include <pthread.h>
#include <semaphore.h>
+#include <stdio.h>
-#include "mustime.h"
-#include "stat.h"
+#include "mtime.h"
#include "test.h"
#define NT 2
-mustime_t *measure_tab;
+/* global variables */
+
+ustime_t *measure_tab = NULL;
+
+char *message = "semaphore";
int counter = 0;
int try = 0;
sem_t sem;
-mustime_t ts1, ts2;
+ustime_t ts1, ts2;
void* trythis (__attribute__((unused)) void *arg)
{
/* thread 1 took the lock -> timestamp 1 */
if (counter % 2 != 0) {
- ts1 = sys_timestamp ();
+ ts1 = sys_timestamp_us ();
}
/* thread 2 took the lock -> timestamp 2 */
else {
- ts2 = sys_timestamp ();
+ ts2 = sys_timestamp_us ();
measure_tab[try++] = ts2 - ts1;
}
measure_tab = buffer;
- /* main process */
+ /* semaphore test */
+
sem_init (&sem, 0, 1);
for (int i = 0; i < nb; i++) {
pthread_t tid1, tid2;
- pthread_create (&tid1, NULL, trythis, NULL);
- pthread_create (&tid2, NULL, trythis, NULL);
+ if (pthread_create (&tid1, NULL, trythis, NULL) != 0) {
+ fprintf (stderr, "error on pthread_create\n");
+ return none_e;
+ }
+
+ if (pthread_create (&tid2, NULL, trythis, NULL) != 0) {
+ fprintf (stderr, "error on pthread_create\n");
+ return none_e;
+ }
pthread_join (tid1, NULL);
pthread_join (tid2, NULL);
sem_destroy (&sem);
- return musec_e;
+ return usec_e;
}
int hist_bin = 10;
char *output = NULL;
+extern char *message;
+
/* usage function */
int usage (int ret)
int64_t *buffer = (int64_t *) calloc (nb, sizeof (int64_t));
assert (buffer);
+ printf ("Test: %s\n", (message) ? message : "unknown");
+
sec_t sec_unit = test (buffer, nb);
+ if (sec_unit == none_e) {
+ printf ("\033[1;31mKO\033[0;0m\n");
+ exit (1);
+ } else {
+ printf ("\033[1;32mOK\033[0;0m\n");
+ }
+
if (output) {
FILE *fd = fopen (output, "w");
assert (fd);
switch (sec_unit) {
case sec_e : strcpy (unit, "second"); break;
case msec_e : strcpy (unit, "millisecond"); break;
- case musec_e : strcpy (unit, "microsecond"); break;
+ case usec_e : strcpy (unit, "microsecond"); break;
case nsec_e : strcpy (unit, "nanosecond"); break;
default: strcpy (unit, "???"); break;
}
#include <inttypes.h>
typedef enum {
- sec_e = 0,
+ none_e = 0,
+ sec_e,
msec_e,
- musec_e,
+ usec_e,
nsec_e
} sec_t;
/* depend: */
/* cflags: */
-/* linker: mustime.o test.o stat.o -lm -lpthread */
+/* linker: mtime.o test.o stat.o -lm -lpthread */
#include <pthread.h>
#include <stdio.h>
-#include "mustime.h"
-#include "stat.h"
+#include "mtime.h"
#include "test.h"
/* global variables */
-mustime_t *deltas = NULL;
+ustime_t *deltas = NULL;
int nb_measurements = 0;
+char *message = "thread";
+
void* dummy_thread(void *arg) {
(void)arg;
return NULL;
deltas = buffer;
nb_measurements = nb;
+ /* 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);
+ 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 ();
+ ustime_t timestamp1 = sys_timestamp_us ();
+
+ if (pthread_create(&posix_t, &attr, dummy_thread, NULL) != 0) {
+ fprintf (stderr, "error on pthread_create\n");
+ return none_e;
+ }
- pthread_create(&posix_t, &attr, dummy_thread, NULL);
pthread_join(posix_t, NULL);
- mustime_t timestamp2 = sys_timestamp ();
+ ustime_t timestamp2 = sys_timestamp_us ();
deltas[i++] = timestamp2 - timestamp1;
}
pthread_attr_destroy(&attr);
- return musec_e;
+ return usec_e;
}