From a7c29e04932f0ac3f0912ade4cdeb40a9a515ac4 Mon Sep 17 00:00:00 2001 From: li041 Date: Mon, 26 Jan 2026 20:58:19 +0800 Subject: [PATCH 1/2] Bind `epoll_loop` to last online CPU --- tools/event_monitor.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tools/event_monitor.c b/tools/event_monitor.c index 7461420..239c55d 100644 --- a/tools/event_monitor.c +++ b/tools/event_monitor.c @@ -8,8 +8,10 @@  * Authors:  *      Guowei Li <2401213322@stu.pku.edu.cn>  */ +#define _GNU_SOURCE #include #include +#include #include #include @@ -81,6 +83,32 @@ int initialize_event_monitor() { epoll_fd = epoll_create1(0); log_debug("create epoll_fd %d", epoll_fd); pthread_create(&emonitor_tid, NULL, epoll_loop, NULL); + cpu_set_t cpuset; + if (sched_getaffinity(0, sizeof(cpu_set_t), &cpuset) == 0) { + int last_cpu = -1; + for (int i = CPU_SETSIZE - 1; i >= 1; i++) { + if (CPU_ISSET(i, &cpuset)) { + last_cpu = i; + break; + } + } + + if (last_cpu != -1) { + cpu_set_t set; + CPU_ZERO(&set); + CPU_SET(last_cpu, &set); + if (pthread_setaffinity_np(emonitor_tid, sizeof(cpu_set_t), &set) != + 0) { + log_warn("failed to set epoll_loop thread to cpu %d", last_cpu); + } else { + log_info("epoll_loop thread set to cpu %d", last_cpu); + } + } else { + log_warn("No available CPU other than CPU0"); + } + } else { + log_warn("failed to get cpu affinity: %d", errno); + } if (epoll_fd >= 0) return 0; else { From a1f00a4499210126f96590ef28af52b7a89fb52a Mon Sep 17 00:00:00 2001 From: li041 Date: Wed, 28 Jan 2026 20:45:03 +0800 Subject: [PATCH 2/2] need to be squashed into prev commit --- tools/event_monitor.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/event_monitor.c b/tools/event_monitor.c index 239c55d..bd9f807 100644 --- a/tools/event_monitor.c +++ b/tools/event_monitor.c @@ -84,9 +84,10 @@ int initialize_event_monitor() { log_debug("create epoll_fd %d", epoll_fd); pthread_create(&emonitor_tid, NULL, epoll_loop, NULL); cpu_set_t cpuset; + CPU_ZERO(&cpuset); if (sched_getaffinity(0, sizeof(cpu_set_t), &cpuset) == 0) { int last_cpu = -1; - for (int i = CPU_SETSIZE - 1; i >= 1; i++) { + for (int i = CPU_SETSIZE - 1; i >= 1; i--) { if (CPU_ISSET(i, &cpuset)) { last_cpu = i; break;