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

Add refresh rate as an option on the client

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