Commit 70739230 authored by Joanne Hugé's avatar Joanne Hugé

Finish basic version of the latency measure module

parent 7dff3f4b
......@@ -4,8 +4,6 @@ PROG = main
SRCDIR = ../src
SRCS = main.c
SRCS += getip.c
SRCS += send_packet.c
OBJS = $(SRCS:%.c=%.o)
......
......@@ -7,33 +7,57 @@
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "getip.h"
#include "send_packet.h"
#include <stdint.h>
#include <inttypes.h>
#define CLOCK_ID CLOCK_MONOTONIC
#define NSECS_PER_SECOND INT64_C(1000000000)
typedef struct thread_stat {
int nb_cycles;
int64_t diff;
} thread_stat_t;
typedef struct thread_param {
int interval;
int64_t interval;
int priority;
int max_cycles;
const char *ip_address;
thread_stat_t stats;
thread_stat_t stat;
} thread_param_t;
static inline int64_t diff_ns(struct timespec t1, struct timespec t2) {
int64_t diff;
diff = NSECS_PER_SECOND * (t1.tv_sec - t2.tv_sec);
diff += ((int64_t)t1.tv_nsec) - ((int64_t)t2.tv_nsec);
return diff;
}
static inline struct timespec add_ns(struct timespec t, int64_t ns) {
struct timespec ret;
ret.tv_nsec = t.tv_nsec + ns;
ret.tv_sec = t.tv_sec;
if(ret.tv_nsec >= NSECS_PER_SECOND) {
ret.tv_sec++;
ret.tv_nsec -= NSECS_PER_SECOND;
}
return ret;
}
// Real-time thread
// Sends packets at a regular intervall
static void *packet_sending_thread(void *p) {
static void *timerthread(void *p) {
struct timespec next;
struct timespec current;
struct sched_param priority;
thread_param_t *param = (thread_param_t *)p;
thread_param_t * param = (thread_param_t *)p;
thread_stat_t * stat = &param->stat;
priority.sched_priority = param->priority;
......@@ -42,19 +66,21 @@ static void *packet_sending_thread(void *p) {
if (err)
error(EXIT_FAILURE, errno, "Couldn't set priority");
for(param->stats.nb_cycles = 0;; param->stats.nb_cycles++) {
clock_gettime(CLOCK_ID, &next);
next = add_ns(next, param->interval);
for(stat->nb_cycles = 0;; stat->nb_cycles++) {
if(param->max_cycles)
if(param->stats.nb_cycles >= param->max_cycles)
if(stat->nb_cycles >= param->max_cycles)
break;
clock_gettime(CLOCK_ID, &next);
next.tv_sec += param->interval;
send_udp_packet(param->ip_address);
clock_nanosleep(CLOCK_ID, TIMER_ABSTIME, &next, NULL);
clock_gettime(CLOCK_ID, &current);
stat->diff = diff_ns(current, next);
next = add_ns(current, param->interval);
}
return NULL;
......@@ -84,12 +110,6 @@ static void process_options(int argc, char *argv[], thread_param_t * param) {
break;
}
}
if (argc != optind + 1) {
printf("Usage: %s server_ip\n", argv[0]);
exit(EXIT_FAILURE);
}
param->ip_address = argv[optind];
}
// Main thread, has non-real time priority
......@@ -100,25 +120,25 @@ int main(int argc, char *argv[]) {
thread_param_t param;
// Default values
param.interval = 1;
param.max_cycles = -1;
param.interval = NSECS_PER_SECOND;
param.max_cycles = 100;
param.priority = 80;
process_options(argc, argv, &param);
int err =
pthread_create(&thread, NULL, packet_sending_thread, (void *)&param);
pthread_create(&thread, NULL, timerthread, (void *)&param);
if(err)
error(EXIT_FAILURE, errno, "Couldn't create thread");
for (;;) {
usleep(100000);
printf("Nb cycles: %d\n", param.stats.nb_cycles);
printf("Diff: %" PRIi64 "us (%d)\n", (param.stat.diff / 1000), param.stat.nb_cycles);
if( param.max_cycles == param.stats.nb_cycles )
if( param.max_cycles == param.stat.nb_cycles )
break;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment