summaryrefslogtreecommitdiff
path: root/src/ipcpd/ipcp.c
diff options
context:
space:
mode:
authorDimitri Staessens <dimitri.staessens@ugent.be>2018-04-02 08:19:54 +0200
committerSander Vrijders <sander.vrijders@ugent.be>2018-04-05 10:31:52 +0200
commit3b03c4b2bcbcf5a913a3127d2b45283540c505c3 (patch)
treefc18ab41a741f7d6ce3b05145558a1cf83c422f8 /src/ipcpd/ipcp.c
parentb81d5a327d272db9b511217665b5febceccb725d (diff)
downloadouroboros-3b03c4b2bcbcf5a913a3127d2b45283540c505c3.tar.gz
ouroboros-3b03c4b2bcbcf5a913a3127d2b45283540c505c3.zip
ipcpd: Enable locking threads to a CPU core
This adds a function that locks a thread to a random core. This greatly improves performance on multi-cpu systems. There is no portable way to do this, this only implements it for GNU/Linux. Signed-off-by: Dimitri Staessens <dimitri.staessens@ugent.be> Signed-off-by: Sander Vrijders <sander.vrijders@ugent.be>
Diffstat (limited to 'src/ipcpd/ipcp.c')
-rw-r--r--src/ipcpd/ipcp.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/ipcpd/ipcp.c b/src/ipcpd/ipcp.c
index d834997f..2321e2a8 100644
--- a/src/ipcpd/ipcp.c
+++ b/src/ipcpd/ipcp.c
@@ -20,6 +20,11 @@
* Foundation, Inc., http://www.fsf.org/about/contact/.
*/
+#if defined(__linux__) && !defined(DISABLE_CORE_LOCK)
+#define _GNU_SOURCE
+#define NPROC (sysconf(_SC_NPROCESSORS_ONLN))
+#endif
+
#define _POSIX_C_SOURCE 200112L
#define __XSI_VISIBLE 500
@@ -42,6 +47,9 @@
#include <string.h>
#include <sys/socket.h>
#include <stdlib.h>
+#if defined(__linux__) && !defined(DISABLE_CORE_LOCK)
+#include <unistd.h>
+#endif
struct cmd {
struct list_head next;
@@ -779,3 +787,24 @@ int ipcp_wait_state(enum ipcp_state state,
return ret;
}
+
+void ipcp_lock_to_core(void)
+{
+#if defined(__linux__) && !defined(DISABLE_CORE_LOCK)
+ cpu_set_t cpus;
+ size_t cpu;
+
+ /* Choose a random core. */
+ cpu = rand() % NPROC;
+
+ CPU_ZERO(&cpus);
+ CPU_SET(cpu, &cpus);
+
+ if (pthread_setaffinity_np(pthread_self(), sizeof(cpus), &cpus))
+ log_warn("Failed to lock thread %lu to CPU %lu/%lu.",
+ pthread_self(), cpu, NPROC);
+ else
+ log_dbg("Locked thread %lu to CPU %lu/%lu.",
+ pthread_self(), cpu, NPROC);
+#endif
+}