From c6ded6c52504a3b6d03bb5fd343611bd9b053d84 Mon Sep 17 00:00:00 2001
From: Florian Fischer <florian.fischer@muhq.space>
Date: Mon, 2 Aug 2021 14:00:40 +0200
Subject: [PATCH] pin the thread to CPU 0

---
 Makefile |  2 +-
 bench.c  | 13 +++++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index 21b4705..9768467 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ OBJ := $(addprefix bench-,$(SYSCALLS))
 
 LDFLAGS := -luring -pthread -lrt
 
-CFLAGS := -Werror -Wall -g -O3
+CFLAGS := -Werror -Wall -g -O3 -D_GNU_SOURCE
 # CFLAGS := -Werror -Wall -g -O0
 
 .PHONY: all clean eval docker-eval check
diff --git a/bench.c b/bench.c
index b7439a6..4199b1e 100644
--- a/bench.c
+++ b/bench.c
@@ -1,9 +1,11 @@
 #include <err.h>
 #include <fcntl.h>
+#include <sched.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/eventfd.h>
+#include <unistd.h>
 
 #include "stopwatch.h"
 
@@ -23,11 +25,22 @@ static int create_eventfd() {
 	return fd;
 }
 
+static void set_cpu_affinity(int cpu) {
+	cpu_set_t set;
+	// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
+	CPU_ZERO(&set);
+	CPU_SET(cpu, &set);
+	if (sched_setaffinity(getpid(), sizeof(set), &set) == -1)
+		err(EXIT_FAILURE, "sched_setaffinity failed");
+}
+
 int main() {
 	uint64_t cycles_sum = 0;
 	uint64_t nanos_sum = 0;
 	uint64_t write_buf = 1;
 
+	set_cpu_affinity(0);
+
 	int fd = create_eventfd();
 
 	init(fd);
-- 
GitLab