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

WIP: rewrite and clean up code

parent da459532
......@@ -14,6 +14,7 @@
#define MAX_KERNEL_LATENCY 1000
#define MAX_RTT_LATENCY 1000
#define MAX_JITTER 1000
#define MAX_BUFFER_SIZE 1024
......
......@@ -34,6 +34,10 @@ static int sock_fd;
static int so_timestamping_flags =
SOF_TIMESTAMPING_RX_SOFTWARE | SOF_TIMESTAMPING_SOFTWARE;
static uint64_t timestamps_buffer[64];
static int ts_buf_read_index = 0;
static int ts_buf_write_index = 0;
// Sets the interface
static int set_if() {
struct ifreq ifreq;
......@@ -49,6 +53,7 @@ static int set_if() {
void init_udp_recv(struct ingress_param * _params,
struct thread_param * _thread_params,
int use_histogram,
uint64_t * _kernel_latency_hist) {
int getaddrinfo_err;
int set_if_err;
......@@ -56,6 +61,7 @@ void init_udp_recv(struct ingress_param * _params,
params = _params;
thread_params = _thread_params;
use_histogram = _use_histogram;
kernel_latency_hist = _kernel_latency_hist;
memset(&hints, 0, sizeof hints);
......@@ -99,13 +105,12 @@ void init_udp_recv(struct ingress_param * _params,
sizeof(so_timestamping_flags)))
error(EXIT_FAILURE, errno, "setsockopt SO_TIMESTAMPING failed\n");
}
}
/*
* Receives udp packets
*/
packet_info_t recv_udp_packet(int use_timestamps, int use_histograms, int64_t histograms[NB_HISTOGRAMS][MAX_HIST_VAL]) {
void recv_udp_packet() {
struct cmsghdr *cmsg;
struct msghdr msg; // Message hardware, sent to the socket
......@@ -118,9 +123,13 @@ packet_info_t recv_udp_packet(int use_timestamps, int use_histograms, int64_t hi
int recvmsgerr;
packet_info_t packet_info;
struct timespec ts;
if (use_timestamps) {
clock_gettime(CLOCK_REALTIME, &ts);
timestamps_buffer[ts_buf_write_index++] = ts_to_uint(ts);
}
iov.iov_base = &rx_buffer;
iov.iov_len = MAX_BUFFER_SIZE - 1;
......@@ -132,11 +141,6 @@ packet_info_t recv_udp_packet(int use_timestamps, int use_histograms, int64_t hi
msg.msg_control = &control;
msg.msg_controllen = sizeof(control);
if (use_timestamps) {
clock_gettime(CLOCK_REALTIME, &ts);
packet_info.userspace_exit_ts = ts_to_uint(ts);
}
recvmsgerr = recvmsg(sock_fd, &msg, 0);
if (recvmsgerr < 0)
error(EXIT_FAILURE, errno, "recvmsg failed, ret value: %d\n", recvmsgerr);
......
......@@ -3,7 +3,11 @@
#include "utilities.h"
int init_udp_recv(int use_timestamps, char *network_if);
void init_udp_recv(struct ingress_param * _params,
struct thread_param * _thread_params,
int use_histogram,
uint64_t * _kernel_latency_hist);
packet_info_t recv_udp_packet(int use_timestamps, int use_histograms, int64_t histograms[NB_HISTOGRAMS][MAX_HIST_VAL]);
void init_udp_recv(ingress_param_t * params,
......
......@@ -3,9 +3,10 @@
#include "common.h"
void init_udp_send(egress_param_t * params,
thread_param_t * thread_params,
uint64_t * kernel_latency_hist);
void init_udp_send(egress_param_t * _params,
thread_param_t * _thread_params,
int _use_histogram,
uint64_t * _kernel_latency_hist);
void send_udp_packet(char *data, uint64_t txtime, int nb_cycles);
typedef struct egress_param {
......
......@@ -34,7 +34,6 @@ typedef struct thread_stat {
uint64_t min_interval;
uint64_t max_interval;
int packets_received;
packet_info_t packet_info;
int lost_packets;
} thread_stat_t;
......@@ -42,17 +41,9 @@ typedef struct thread_param {
int interval;
int priority;
thread_stat_t stats;
} thread_param_t;
typedef struct network_config {
size_t tx_buffer_len;
char ip_address[256];
char network_if[256];
int packet_priority;
} network_config_t;
typedef struct main_param {
typedef struct main_params {
int refresh_rate;
int verbose;
size_t tx_buffer_len;
......@@ -64,18 +55,19 @@ static void sighand(int sig_num);
// Static variables
static int64_t histograms[NB_HISTOGRAMS][MAX_HIST_VAL];
static int64_t kernel_latency_hist[MAX_KERNEL_LATENCY];
static int64_t jitter_hist[MAX_JITTER];
static main_param_t main_param;
static thread_param_t *param;
static network_config_t network_config;
static main_param_t main_params;
static thread_param_t thread_params;
static ingress_stat_t *ingress_stats;
static ingress_param_t *ingress_params;
static int enable_histograms;
static int enable_affinity;
static int enable_timestamps;
enum TSNTask { RECV_PACKET_TASK,
RTT_TASK };
enum TSNTask { RECV_PACKET_TASK, RTT_TASK };
static enum TSNTask tsn_task;
struct timespec measures_start;
......@@ -102,8 +94,6 @@ static void help(char *argv[]) {
static void *packet_receiving_thread(void *p) {
struct timespec current, previous;
struct sched_param priority;
thread_param_t *param = (thread_param_t *)p;
thread_stat_t *stats = &param->stats;
uint64_t diff = 0;
cpu_set_t mask;
int64_t dist_to_interval;
......@@ -121,7 +111,7 @@ static void *packet_receiving_thread(void *p) {
}
// Set thread priority
priority.sched_priority = param->priority;
priority.sched_priority = thread_params.priority;
if (sched_setscheduler(0, SCHED_FIFO, &priority))
error(EXIT_FAILURE, errno, "Couldn't set priority");
......@@ -132,17 +122,17 @@ static void *packet_receiving_thread(void *p) {
if (tsn_task == RTT_TASK) {
recv_udp_packet(0, 0, NULL);
send_udp_packet(0, 0, "", 0, network_config.ip_address, NULL);
send_udp_packet("", 0);
} else if (tsn_task == RECV_PACKET_TASK) {
int current_packet_id;
param->stats.packet_info = recv_udp_packet(enable_timestamps, enable_histograms, histograms);
thread_params.stats.packet_info = recv_udp_packet(enable_timestamps, enable_histograms, histograms);
clock_gettime(CLOCK_MONOTONIC, &current);
current_packet_id = atoi(param->stats.packet_info.data);
current_packet_id = atoi(thread_params.stats.packet_info.data);
// If this is not the first received packet
if (stats->packets_received) {
......@@ -152,10 +142,10 @@ static void *packet_receiving_thread(void *p) {
stats->max_interval = diff > stats->max_interval ? diff : stats->max_interval;
// Check if packets were lost
param->stats.lost_packets += (current_packet_id - prev_packet_id - 1) % 1000;
thread_params.stats.lost_packets += (current_packet_id - prev_packet_id - 1) % 1000;
if (enable_histograms) {
dist_to_interval = (((int64_t)diff) - param->interval) / 1000;
dist_to_interval = (((int64_t)diff) - thread_params.interval) / 1000;
dist_to_interval += MAX_HIST_VAL / 2;
if (dist_to_interval > ((int)MAX_HIST_VAL) || dist_to_interval < 0)
......@@ -177,14 +167,10 @@ static void *packet_receiving_thread(void *p) {
// Handles the IO and creates real time threads
int main(int argc, char *argv[]) {
pthread_t thread;
thread_stat_t *stats;
param = malloc(sizeof(thread_param_t));
stats = &param->stats;
// Default configuration values
param->interval = 100000 * 1000;
param->priority = 99;
thread_params.interval = 100000 * 1000;
thread_params.priority = 99;
enable_affinity = 0;
enable_timestamps = 0;
......@@ -193,8 +179,8 @@ int main(int argc, char *argv[]) {
network_config.tx_buffer_len = 1024;
main_param.refresh_rate = 50000;
main_param.verbose = 0;
main_params.refresh_rate = 50000;
main_params.verbose = 0;
// Process bash options
process_options(argc, argv);
......@@ -215,14 +201,14 @@ int main(int argc, char *argv[]) {
init_udp_send(0, 0, 1, network_config.network_if, network_config.tx_buffer_len);
// Create the real time thread
if (pthread_create(&thread, NULL, packet_receiving_thread, (void *)param))
if (pthread_create(&thread, NULL, packet_receiving_thread, NULL))
error(EXIT_FAILURE, errno, "Couldn't create thread");
// Verbose loop
for (;;) {
usleep(main_param.refresh_rate);
usleep(main_params.refresh_rate);
if (main_param.verbose) {
if (main_params.verbose) {
if (tsn_task == RECV_PACKET_TASK) {
......@@ -266,7 +252,7 @@ static void print_histograms() {
duration_hour = duration / NSEC_PER_SEC / 3600;
duration_minutes = duration / NSEC_PER_SEC / 60 - duration_hour * 60;
interval = param->interval / 1000;
interval = thread_params.interval / 1000;
if (enable_timestamps) {
printf("{\"measure_sets\": [{"
......@@ -333,8 +319,8 @@ static void sighand(int sig_num) {
print_histograms();
if (param->stats.lost_packets)
fprintf(stderr, "%d packets were lost\n", param->stats.lost_packets);
if (thread_params.stats.lost_packets)
fprintf(stderr, "%d packets were lost\n", thread_params.stats.lost_packets);
exit(EXIT_SUCCESS);
}
......@@ -374,22 +360,22 @@ static void process_options(int argc, char *argv[]) {
exit(EXIT_SUCCESS);
break;
case 'i':
param->interval = atoi(optarg) * 1000;
thread_params.interval = atoi(optarg) * 1000;
break;
case 'g':
enable_histograms = 1;
break;
case 'p':
param->priority = atoi(optarg);
thread_params.priority = atoi(optarg);
break;
case 'r':
main_param.refresh_rate = atoi(optarg);
main_params.refresh_rate = atoi(optarg);
break;
case 't':
enable_timestamps = 1;
break;
case 'v':
main_param.verbose = 1;
main_params.verbose = 1;
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