Commit 3514bb7e authored by Stefan Reif's avatar Stefan Reif

Implement thread pinning

Pin worker threads to cores.
parent 55b8dcd1
Pipeline #926 failed with stages
in 3 minutes and 52 seconds
......@@ -5,8 +5,8 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_C_FLAGS "-O2 -Wall -std=c11 -D_XOPEN_SOURCE=600" )
set(CMAKE_CXX_FLAGS "-fstack-protector -fstack-protector-all -Wall -std=c++11 -D_XOPEN_SOURCE=600" )
set(CMAKE_C_FLAGS "-O2 -Wall -std=gnu11 -D_GNU_SOURCE" )
set(CMAKE_CXX_FLAGS "-fstack-protector -fstack-protector-all -Wall -std=gnu++11 -D_GNU_SOURCE" )
set(CMAKE_CXX_FLAGS_DEBUG "-O2 -Wall -ggdb" )
set(CMAKE_CXX_FLAGS_RELEASE "-Os -Wall" )
......
#include <arpa/inet.h>
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
......@@ -75,6 +76,14 @@ PrrtSocket *PrrtSocket_create(const bool is_sender)
return NULL;
}
static void pin_thread_to_core(pthread_attr_t *ap, int core)
{
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(core, &cpuset);
pthread_attr_setaffinity_np(ap, sizeof(cpu_set_t), &cpuset);
}
bool PrrtSocket_bind(PrrtSocket *sock_ptr, const char* ipAddress, const uint16_t port) {
check(port <= 65534, "Port %d cannot be bound to.", port);
......@@ -95,13 +104,26 @@ bool PrrtSocket_bind(PrrtSocket *sock_ptr, const char* ipAddress, const uint16_t
check(bind(sock_ptr->dataSocketFd, (struct sockaddr *) address, size) == EXIT_SUCCESS,
"Cannot bind data socket.");
if(sock_ptr->isSender) {
check(pthread_create(&sock_ptr->receiveFeedbackThread, NULL, receive_feedback_loop, (void *) sock_ptr) ==
pthread_attr_t feedbackattr;
pthread_attr_t datasendattr;
pthread_attr_init(&feedbackattr);
pthread_attr_init(&datasendattr);
pin_thread_to_core(&feedbackattr, 1);
pin_thread_to_core(&datasendattr, 2);
check(pthread_create(&sock_ptr->receiveFeedbackThread, &feedbackattr, receive_feedback_loop, (void *) sock_ptr) ==
EXIT_SUCCESS, "Cannot create receive feedback thread.");
check(pthread_create(&sock_ptr->sendDataThread, NULL, send_data_loop, (void *) sock_ptr) == EXIT_SUCCESS,
check(pthread_create(&sock_ptr->sendDataThread, &datasendattr, send_data_loop, (void *) sock_ptr) == EXIT_SUCCESS,
"Cannot create send thread.");
} else {
check(pthread_create(&sock_ptr->receiveDataThread, NULL, receive_data_loop, (void *) sock_ptr) == EXIT_SUCCESS,
pthread_attr_t datarecvattr;
pthread_attr_init(&datarecvattr);
pin_thread_to_core(&datarecvattr, 3);
check(pthread_create(&sock_ptr->receiveDataThread, &datarecvattr, receive_data_loop, (void *) sock_ptr) == EXIT_SUCCESS,
"Cannot create data receiving thread.");
}
......
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