Skip to content

Commit 7f7e829

Browse files
committed
feat: moving IRQs
1 parent 3cf2794 commit 7f7e829

6 files changed

Lines changed: 79 additions & 10 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ run-debug:
7474
## run-release: 🏎️ run the app (prod)
7575
.PHONY: run-release
7676
run-release:
77-
$(call pp,starting app. dont forget to run `scripts/cpu_shield_start.sh`)
77+
$(call pp,starting app. dont forget to run `scripts/cpu_shield_start.sh` and `scripts/irqs_move.sh`)
7878
build/Release/tradercpp
7979

8080
# CONTAINERISATION RECIPES ----------------------------------------------------

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,16 @@ NB: this app uses `make` as a recipe book, but it's not essential:
156156
- ✅ FIX-thread CPU affinity
157157
- Disable hyperthreading
158158
- NIC
159+
- ✅ NIC IRQ affinity to same CPU as FIX
159160
- wired, kernel-bypass NICs
160-
- NIC IRQ affinity to same CPU as FIX
161161
- hardware queue affinity
162162
- QoS (mark packets)
163163
- AF_XDP (+ Zero-copy mode)
164164
- ~dedicated NIC + DPDK~
165165
- OS
166-
- vacate OS services / move IRQs for all system devices to the last CPU
167-
- RTOS
166+
- ✅ vacate OS services
167+
- move IRQs for all system devices to other CPUs
168+
- RTOS / PREEMPT_RT kernel
168169
- BIOS
169170
- disable hyperthreading, turbo boost
170171
- disable C-states deeper than C1 (C1E, C6, etc)

scripts/irqs_move.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
# ------------------------------------------------------------
3+
# Move all IRQs (except NICs) to non-isolated CPUs dynamically
4+
# ------------------------------------------------------------
5+
6+
# List of NIC interfaces to protect (keep IRQs on isolated cores)
7+
NIC_IFACES=("eth0" "eth1") # modify as needed
8+
9+
# Determine total CPU count
10+
CPU_COUNT=$(nproc)
11+
12+
# Set first isolated CPU index (adjust if your isolated CPUs differ)
13+
ISOLATED_END=1 # e.g., CPUs 0-1 are isolated
14+
15+
# Build CPU mask for CPUs >= ISOLATED_END + 1
16+
# Example: if CPU_COUNT=8, ISOLATED_END=1 -> mask = 11111100 = FC
17+
# The mask is binary, then converted to hex
18+
19+
CPU_MASK=$(printf "%X" $(( (1 << (CPU_COUNT)) - 1 - ((1 << (ISOLATED_END + 1)) - 1) )))
20+
21+
echo "Detected CPU mask for IRQs: $CPU_MASK (CPUs >= $((ISOLATED_END + 1)))"
22+
23+
# Helper function to check if IRQ belongs to a NIC
24+
is_nic_irq() {
25+
local irq_file="$1"
26+
for nic in "${NIC_IFACES[@]}"; do
27+
if grep -q "$nic" "$irq_file"; then
28+
return 0 # It's a NIC IRQ
29+
fi
30+
done
31+
return 1 # Not a NIC IRQ
32+
}
33+
34+
# Iterate through all IRQ directories
35+
for irq_dir in /proc/irq/*; do
36+
[ -d "$irq_dir" ] || continue
37+
irq_num=$(basename "$irq_dir")
38+
smp_file="$irq_dir/smp_affinity"
39+
40+
[ -f "$smp_file" ] || continue
41+
42+
if is_nic_irq "$irq_dir/affinity_hint"; then
43+
echo "Skipping NIC IRQ $irq_num"
44+
continue
45+
fi
46+
47+
# Move IRQ to CPUs >= ISOLATED_END + 1
48+
echo "$CPU_MASK" > "$smp_file"
49+
echo "Moved IRQ $irq_num to CPUs >= $((ISOLATED_END + 1))"
50+
done

scripts/irqs_restore.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
# ------------------------------------------------------------
3+
# Restore all IRQs to default CPU affinity (all CPUs)
4+
# ------------------------------------------------------------
5+
6+
# Determine total number of CPUs
7+
CPU_COUNT=$(nproc)
8+
9+
# Default mask: all CPUs (binary 111...1)
10+
DEFAULT_MASK=$(printf "%X" $(( (1 << CPU_COUNT) - 1 )))
11+
12+
echo "Restoring all IRQs to default mask: $DEFAULT_MASK"
13+
14+
# Iterate through all IRQ directories
15+
for irq_dir in /proc/irq/*; do
16+
[ -d "$irq_dir" ] || continue
17+
smp_file="$irq_dir/smp_affinity"
18+
19+
[ -f "$smp_file" ] || continue
20+
21+
# Restore default mask
22+
echo "$DEFAULT_MASK" > "$smp_file"
23+
echo "Restored IRQ $(basename $irq_dir) to all CPUs"
24+
done

src/utils/threading.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,6 @@ unsigned int Threading::get_cpu_count() {
147147
#endif
148148
}
149149

150-
// static function
151-
void Threading::set_thread_affinity(std::thread& t, unsigned int cpu_id) {
152-
return set_native_thread_affinity(t.native_handle(), cpu_id);
153-
}
154-
155150
// static function
156151
void Threading::set_thread_cpu(unsigned int cpu_id) {
157152
#if defined(__linux__) || defined(__APPLE__)

src/utils/threading.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ class Threading {
6161
static void set_thread_realtime();
6262

6363
private:
64-
static void set_thread_affinity(std::thread& t, unsigned int cpu_id);
6564
// platform-specific affinity for native thread handle
6665
static void set_native_thread_affinity(std::thread::native_handle_type handle,
6766
unsigned int cpu_id);

0 commit comments

Comments
 (0)