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

Add refresh rate as an option on the client

parent 50a950fd
*.o
*.d
simple-server/build/main
simple-client/build/main
latency-measure/build/main
latency-measure/build/main
clockres/build/clockres
clockres/build/clockres_arm
packet-exchange/build/server
packet-exchange/build/client
......@@ -2,6 +2,7 @@
#include <error.h>
#include <pthread.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
......@@ -11,6 +12,7 @@
#include "send_packet.h"
#define CLOCK_ID CLOCK_MONOTONIC
#define NSEC_PER_SEC UINT64_C(1000000000)
typedef struct thread_stat {
int nb_cycles;
......@@ -24,13 +26,16 @@ typedef struct thread_param {
thread_stat_t stats;
} thread_param_t;
static void process_options(int argc, char *argv[], thread_param_t * param);
typedef struct main_param {
int refresh_rate;
} main_param_t;
static void process_options(int argc, char *argv[], thread_param_t *param,
main_param_t *main_param);
// Real-time thread
// Sends packets at a regular intervall
static void *packet_sending_thread(void *p) {
struct timespec next;
struct sched_param priority;
......@@ -40,22 +45,24 @@ static void *packet_sending_thread(void *p) {
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 (err) error(EXIT_FAILURE, errno, "Couldn't set priority");
if(param->max_cycles)
if(param->stats.nb_cycles >= param->max_cycles)
break;
clock_gettime(CLOCK_ID, &next);
clock_gettime(CLOCK_ID, &next);
next.tv_sec += param->interval;
for (param->stats.nb_cycles = 0;; param->stats.nb_cycles++) {
if (param->max_cycles)
if (param->stats.nb_cycles >= param->max_cycles) break;
send_udp_packet(param->ip_address);
clock_nanosleep(CLOCK_ID, TIMER_ABSTIME, &next, NULL);
next.tv_nsec += param->interval;
if ( (unsigned int)next.tv_nsec >= NSEC_PER_SEC) {
next.tv_sec += 1;
next.tv_nsec -= NSEC_PER_SEC;
}
clock_nanosleep(CLOCK_ID, TIMER_ABSTIME, &next, NULL);
}
return NULL;
......@@ -64,54 +71,59 @@ static void *packet_sending_thread(void *p) {
// 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;
main_param_t main_param;
// Default values
param.interval = 1;
param.max_cycles = -1;
param.priority = 99;
process_options(argc, argv, &param);
main_param.refresh_rate = 50000;
// Process bash options
process_options(argc, argv, &param, &main_param);
if(pthread_create(&thread, NULL, packet_sending_thread, (void *)&param))
usleep(10000);
if (pthread_create(&thread, NULL, packet_sending_thread, (void *)&param))
error(EXIT_FAILURE, errno, "Couldn't create thread");
for (;;) {
usleep(100000);
usleep(main_param.refresh_rate);
printf("Nb cycles: %d\n", param.stats.nb_cycles);
if( param.max_cycles == param.stats.nb_cycles )
break;
if (param.max_cycles == param.stats.nb_cycles) break;
}
exit(EXIT_SUCCESS);
}
static void process_options(int argc, char *argv[], thread_param_t * param) {
for(;;) {
int c = getopt(argc, argv, "l:p:i:");
static void process_options(int argc, char *argv[], thread_param_t *param,
main_param_t *main_param) {
for (;;) {
int c = getopt(argc, argv, "i:l:p:r:");
if(c == -1)
break;
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;
switch (c) {
case 'i':
param->interval = atoi(optarg) * 1000;
break;
case 'l':
param->max_cycles = atoi(optarg);
break;
case 'p':
param->priority = atoi(optarg);
break;
case 'r':
main_param->refresh_rate = atoi(optarg);
break;
default:
exit(EXIT_FAILURE);
break;
}
}
......
......@@ -57,7 +57,10 @@ int send_udp_packet(const char * server_ip) {
freeaddrinfo(servinfo);
printf("Sent %d bytes to %s\n", bytes_sent, server_ip);
#ifdef DEBUG_ENABLED
printf("Sent %d bytes to %s\n", bytes_sent, server_ip);
#endif
close(sockfd);
return 0;
}
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