Commit 702faf9e authored by Joanne Hugé's avatar Joanne Hugé

Add CPU affinity option to server and client

parent e01d6464
#define _GNU_SOURCE
#include <errno.h>
#include <error.h>
#include <pthread.h>
......@@ -22,6 +24,9 @@ typedef struct thread_param {
int interval;
int priority;
int max_cycles;
int enable_affinity;
const char *ip_address;
thread_stat_t stats;
} thread_param_t;
......@@ -39,13 +44,24 @@ static void *packet_sending_thread(void *p) {
struct timespec next;
struct sched_param priority;
thread_param_t *param = (thread_param_t *)p;
cpu_set_t mask;
if(param->enable_affinity) {
// Set thread CPU affinity
CPU_ZERO(&mask);
CPU_SET(1, &mask);
if (sched_setaffinity(0, sizeof(mask), &mask))
fprintf(stderr, "Could not set CPU affinity to CPU #1\n");
}
// Set thread priority
priority.sched_priority = param->priority;
if (sched_setscheduler(0, SCHED_FIFO, &priority))
error(EXIT_FAILURE, errno, "Couldn't set priority");
clock_gettime(CLOCK_ID, &next);
// Packet sending loop
for (param->stats.nb_cycles = 0;; param->stats.nb_cycles++) {
if (param->max_cycles)
if (param->stats.nb_cycles >= param->max_cycles) break;
......@@ -104,11 +120,14 @@ int main(int argc, char *argv[]) {
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:");
int c = getopt(argc, argv, "ai:l:p:r:");
if (c == -1) break;
switch (c) {
case 'a':
param->enable_affinity = 1;
break;
case 'i':
param->interval = atoi(optarg) * 1000;
break;
......
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <errno.h>
#include <error.h>
......@@ -30,6 +32,8 @@ typedef struct thread_param {
int priority;
thread_stat_t stats;
int enable_affinity;
int sockfd;
} thread_param_t;
......@@ -53,6 +57,7 @@ static void *packet_receiving_thread(void *p) {
thread_param_t *param = (thread_param_t *)p;
thread_stat_t *stats = &param->stats;
uint64_t diff = 0;
cpu_set_t mask;
char buf[BUFFER_SIZE];
int bytes_received = 0;
......@@ -64,10 +69,20 @@ static void *packet_receiving_thread(void *p) {
stats->min_interval = UINT64_MAX;
stats->max_interval = 0;
if(param->enable_affinity) {
// Set thread CPU affinity
CPU_ZERO(&mask);
CPU_SET(1, &mask);
if (sched_setaffinity(0, sizeof(mask), &mask))
fprintf(stderr, "Could not set CPU affinity to CPU #1\n");
}
// Set thread priority
priority.sched_priority = param->priority;
if (sched_setscheduler(0, SCHED_FIFO, &priority))
error(EXIT_FAILURE, errno, "Couldn't set priority");
// Packet receiving loop
for (stats->packets_received = 0;; stats->packets_received++) {
bytes_received = recvfrom(param->sockfd, buf, BUFFER_SIZE - 1, 0,
(struct sockaddr *)&client_addr, &addr_len);
......@@ -124,11 +139,14 @@ int main(int argc, char *argv[]) {
static void process_options(int argc, char *argv[], thread_param_t *param,
main_param_t *main_param) {
for (;;) {
int c = getopt(argc, argv, "p:r:");
int c = getopt(argc, argv, "ap:r:");
if (c == -1) break;
switch (c) {
case 'a':
param->enable_affinity = 1;
break;
case 'p':
param->priority = atoi(optarg);
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