Commit 7dff3f4b authored by Joanne Hugé's avatar Joanne Hugé

Start latency measuring module

parent d738089c
ARM_CC = arm-linux-gnueabihf-gcc
ARM_PROG = main_arm
PROG = main
SRCDIR = ../src
SRCS = main.c
SRCS += getip.c
SRCS += send_packet.c
OBJS = $(SRCS:%.c=%.o)
CFLAGS = -Og -g -Wall -Werror -Wextra
CFLAGS += -MD -MP
CFLAGS += -I $(SRCDIR)
CFLAGS += -std=gnu99
LDFLAGS = -pthread
vpath %.c $(SRCDIR)
$(ARM_PROG): FORCE
make clean
make -e CC:=arm-linux-gnueabihf-gcc $(PROG)
mv $(PROG) $(ARM_PROG)
FORCE:
$(PROG): $(OBJS)
$(CC) $(LDFLAGS) $^ -o $@
-include $(subst .c,.d,$(SRCS))
run: $(PROG)
./$^
clean:
$(RM) $(OBJS) $(PROG) $(subst .c,.d,$(SRCS))
.PHONY: clean FORCE
#include <errno.h>
#include <error.h>
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "getip.h"
#include "send_packet.h"
#define CLOCK_ID CLOCK_MONOTONIC
typedef struct thread_stat {
int nb_cycles;
} thread_stat_t;
typedef struct thread_param {
int interval;
int priority;
int max_cycles;
const char *ip_address;
thread_stat_t stats;
} thread_param_t;
// Real-time thread
// Sends packets at a regular intervall
static void *packet_sending_thread(void *p) {
struct timespec next;
struct sched_param priority;
thread_param_t *param = (thread_param_t *)p;
priority.sched_priority = param->priority;
int err = sched_setscheduler(0, SCHED_FIFO, &priority);
if (err)
error(EXIT_FAILURE, errno, "Couldn't set priority");
for(param->stats.nb_cycles = 0;; param->stats.nb_cycles++) {
if(param->max_cycles)
if(param->stats.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);
}
return NULL;
}
static void process_options(int argc, char *argv[], thread_param_t * param) {
for(;;) {
int c = getopt(argc, argv, "l:p:i:");
if(c == -1)
break;
switch(c) {
case 'p':
param->priority = atoi(optarg);
break;
case 'l':
param->max_cycles = atoi(optarg);
break;
case 'i':
param->interval = atoi(optarg);
break;
default:
exit(EXIT_FAILURE);
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
// Handles the IO and creates real time threads
int main(int argc, char *argv[]) {
pthread_t thread;
thread_param_t param;
// Default values
param.interval = 1;
param.max_cycles = -1;
param.priority = 80;
process_options(argc, argv, &param);
int err =
pthread_create(&thread, NULL, packet_sending_thread, (void *)&param);
if(err)
error(EXIT_FAILURE, errno, "Couldn't create thread");
for (;;) {
usleep(100000);
printf("Nb cycles: %d\n", param.stats.nb_cycles);
if( param.max_cycles == param.stats.nb_cycles )
break;
}
exit(EXIT_SUCCESS);
}
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