Files
RedBear-OS/local/patches/base/P53-e1000d-itr-coalescing.patch
vasilito 2e477bbc90 Fix P45 and P53 patch line numbers and missing dependencies
P53: Change itr_tracker insertion point from line 46 to 47
so it applies after NetworkScheme::new() closing, not inside it.

P45: Add log.workspace = true to ixgbed Cargo.toml since
P45 adds log::error! usage to ixgbed main.rs.
2026-05-20 23:56:25 +03:00

119 lines
4.3 KiB
Diff

diff --git a/drivers/net/e1000d/src/device.rs b/drivers/net/e1000d/src/device.rs
index 4c518f30..7ba100dd 100644
--- a/drivers/net/e1000d/src/device.rs
+++ b/drivers/net/e1000d/src/device.rs
@@ -26,0 +27 @@ const ICR: u32 = 0xC0;
+const ITR: u32 = 0xC4;
@@ -241,0 +243,6 @@ impl Intel8254x {
+ /// Set the Interrupt Throttling Rate (ITR) register.
+ /// `interval` is in 256-ns increments. 0 disables throttling.
+ pub unsafe fn set_itr(&self, interval: u16) {
+ self.write_reg(ITR, interval as u32);
+ }
+
diff --git a/drivers/net/e1000d/src/itr.rs b/drivers/net/e1000d/src/itr.rs
new file mode 100644
index 00000000..aa85a6f2
--- /dev/null
+++ b/drivers/net/e1000d/src/itr.rs
@@ -0,0 +1,81 @@
+/// Interrupt Throttling Rate (ITR) tracker for e1000d.
+///
+/// Dynamically adjusts the interrupt coalescing interval based on packet rate
+/// to balance latency and CPU overhead.
+#[derive(Debug)]
+pub struct ItrTracker {
+ last_irq_count: u64,
+ current_itr: u16,
+ consecutive_low: u32,
+ consecutive_high: u32,
+}
+
+impl ItrTracker {
+ /// Target ~8000 interrupts/sec max for low latency.
+ const LOW_LATENCY_THRESHOLD: u64 = 8000;
+ /// Target ~2000 interrupts/sec min for CPU efficiency.
+ const HIGH_THROUGHPUT_THRESHOLD: u64 = 2000;
+ /// Minimum ITR interval in 256-ns units (~50 µs).
+ const MIN_ITR: u16 = 200;
+ /// Default ITR interval in 256-ns units (~256 µs).
+ const DEFAULT_ITR: u16 = 1000;
+ /// Maximum ITR interval in 256-ns units (~2 ms).
+ const MAX_ITR: u16 = 8000;
+ /// Number of consecutive measurements before adjusting.
+ const HYSTERESIS: u32 = 3;
+
+ pub fn new() -> Self {
+ Self {
+ last_irq_count: 0,
+ current_itr: Self::DEFAULT_ITR,
+ consecutive_low: 0,
+ consecutive_high: 0,
+ }
+ }
+
+ /// Call once per IRQ to update the tracker and return the new ITR value.
+ /// Returns `None` if no change is needed.
+ pub fn update(&mut self, irq_count: u64) -> Option<u16> {
+ let delta = irq_count.saturating_sub(self.last_irq_count);
+ self.last_irq_count = irq_count;
+
+ if delta > Self::LOW_LATENCY_THRESHOLD {
+ self.consecutive_high += 1;
+ self.consecutive_low = 0;
+ if self.consecutive_high >= Self::HYSTERESIS {
+ self.consecutive_high = 0;
+ let new_itr = self.current_itr.saturating_mul(2).min(Self::MAX_ITR);
+ if new_itr != self.current_itr {
+ self.current_itr = new_itr;
+ return Some(self.current_itr);
+ }
+ }
+ } else if delta < Self::HIGH_THROUGHPUT_THRESHOLD {
+ self.consecutive_low += 1;
+ self.consecutive_high = 0;
+ if self.consecutive_low >= Self::HYSTERESIS {
+ self.consecutive_low = 0;
+ let new_itr = self.current_itr.saturating_div(2).max(Self::MIN_ITR);
+ if new_itr != self.current_itr {
+ self.current_itr = new_itr;
+ return Some(self.current_itr);
+ }
+ }
+ } else {
+ self.consecutive_low = 0;
+ self.consecutive_high = 0;
+ }
+
+ None
+ }
+
+ pub fn current_itr(&self) -> u16 {
+ self.current_itr
+ }
+}
+
+impl Default for ItrTracker {
+ fn default() -> Self {
+ Self::new()
+ }
+}
diff --git a/drivers/net/e1000d/src/main.rs b/drivers/net/e1000d/src/main.rs
index 373ea9b3..1a4b0667 100644
--- a/drivers/net/e1000d/src/main.rs
+++ b/drivers/net/e1000d/src/main.rs
@@ -8,0 +9 @@ pub mod device;
+pub mod itr;
@@ -47,0 +49,2 @@ fn daemon(daemon: daemon::Daemon, mut pcid_handle: PciFunctionHandle) -> ! {
+ let mut itr_tracker = itr::ItrTracker::new();
+
@@ -74,0 +78 @@ fn daemon(daemon: daemon::Daemon, mut pcid_handle: PciFunctionHandle) -> ! {
+ let mut irq_count: u64 = 0;
@@ -77,0 +82 @@ fn daemon(daemon: daemon::Daemon, mut pcid_handle: PciFunctionHandle) -> ! {
+ irq_count += 1;
@@ -82,0 +88,4 @@ fn daemon(daemon: daemon::Daemon, mut pcid_handle: PciFunctionHandle) -> ! {
+ if let Some(new_itr) = itr_tracker.update(irq_count) {
+ unsafe { scheme.adapter().set_itr(new_itr) };
+ }
+