ax-task 0.8.2

OS-independent IRQ-safe SMP task scheduling core
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
//! EEVDF scheduling entity calculations.

use super::hrtick::finish_hrtick_delta_ns;
use crate::{
    sched::{FairMode, Nice},
    thread::TaskError,
};

const BASE_WEIGHT: u64 = 1024;
const MAX_VIRTUAL_DELTA: u64 = i64::MAX as u64;
/// Returns the signed modular distance from `reference` to `value`.
///
/// Linux compares EEVDF virtual time as `(s64)(a - b)`. Keeping every active
/// request and lag within half of the `u64` timeline makes this ordering stable
/// across wrap without periodically rewriting every queued entity.
pub(crate) const fn virtual_delta(value: u64, reference: u64) -> i64 {
    value.wrapping_sub(reference) as i64
}

pub(crate) const fn virtual_before(value: u64, reference: u64) -> bool {
    virtual_delta(value, reference) < 0
}

pub(crate) const fn virtual_after(value: u64, reference: u64) -> bool {
    virtual_delta(value, reference) > 0
}

pub(crate) const fn virtual_min(lhs: u64, rhs: u64) -> u64 {
    if virtual_before(rhs, lhs) { rhs } else { lhs }
}

pub(crate) const fn virtual_max(lhs: u64, rhs: u64) -> u64 {
    if virtual_after(rhs, lhs) { rhs } else { lhs }
}

/// Per-thread EEVDF service and lag state.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct FairEntity {
    nice: Nice,
    mode: FairMode,
    vruntime: u64,
    service_request_ns: u64,
    virtual_deadline: u64,
    protected_until_vruntime: u64,
    placement: FairPlacement,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum FairPlacement {
    Initial,
    Active,
    Delayed {
        virtual_lag: i64,
    },
    DelayedMigrating {
        virtual_lag: i64,
        relative_deadline: u64,
    },
    Sleeping {
        virtual_lag: i64,
    },
    Migrating {
        virtual_lag: i64,
        relative_deadline: u64,
    },
}

/// Linux `set_load_weight()`: SCHED_IDLE ignores nice and uses
/// `WEIGHT_IDLEPRIO`.
const fn load_weight(nice: Nice, mode: FairMode) -> u32 {
    match mode {
        FairMode::Idle => crate::sched::SchedulePolicy::IDLE_POLICY_WEIGHT,
        FairMode::Normal | FairMode::Batch => nice.weight(),
    }
}

impl FairEntity {
    #[cfg(test)]
    pub(super) const fn test_state(
        nice: Nice,
        mode: FairMode,
        vruntime: u64,
        virtual_deadline: u64,
    ) -> Self {
        Self {
            nice,
            mode,
            vruntime,
            service_request_ns: 1,
            virtual_deadline,
            protected_until_vruntime: virtual_deadline,
            placement: FairPlacement::Active,
        }
    }

    /// Creates a fair entity at a run queue's current virtual time.
    pub fn new(nice: Nice, mode: FairMode, request_ns: u64, virtual_time: u64) -> Self {
        // Linux preserves the task's nice value while SCHED_IDLE pins its
        // effective load to WEIGHT_IDLEPRIO.
        let weighted_request = weighted_delta(request_ns, load_weight(nice, mode));
        Self {
            nice,
            mode,
            vruntime: virtual_time,
            service_request_ns: request_ns,
            virtual_deadline: virtual_time.wrapping_add(weighted_request),
            protected_until_vruntime: virtual_time,
            placement: FairPlacement::Initial,
        }
    }

    /// Charges physical execution and renews a consumed request immediately.
    ///
    /// Linux's `update_curr()` calls `update_deadline()` as soon as `vruntime`
    /// reaches the active deadline. Returning the expiry separately preserves
    /// the caller's reschedule decision while the entity already owns its next
    /// request, including when it keeps running without a schedule-out.
    pub fn charge(&mut self, runtime_ns: u64, _virtual_time: u64) -> bool {
        self.vruntime = self
            .vruntime
            .wrapping_add(weighted_delta(runtime_ns, self.load_weight()));
        let request_exhausted = self.request_exhausted();
        if request_exhausted {
            self.renew_request();
        }
        request_exhausted
    }

    /// Clamps placement to the current runqueue virtual time.
    pub(crate) fn place_at_least(&mut self, virtual_time: u64) {
        if !virtual_before(self.vruntime, virtual_time) {
            return;
        }
        let shift = virtual_time.wrapping_sub(self.vruntime);
        self.vruntime = virtual_time;
        self.virtual_deadline = self.virtual_deadline.wrapping_add(shift);
        if self.slice_is_protected() {
            self.protected_until_vruntime = self.protected_until_vruntime.wrapping_add(shift);
        } else {
            self.protected_until_vruntime = self.vruntime;
        }
    }

    /// Saves the bounded virtual lag at the point this entity stops competing.
    ///
    /// Linux records `se->vlag` against `avg_vruntime()` before dequeue. The
    /// sleeping task must retain that service credit while it is absent from
    /// the weighted average; deriving lag at wake time would instead reward
    /// arbitrary sleep duration.
    pub(crate) fn capture_sleep_lag(
        &mut self,
        virtual_time: u64,
        rq_max_slice_ns: u64,
        timing_granularity_ns: u64,
    ) {
        let virtual_lag =
            self.bounded_virtual_lag(virtual_time, rq_max_slice_ns, timing_granularity_ns);
        #[cfg(feature = "qperf-metrics")]
        crate::diagnostics::counters::record_fair_sleep_lag(virtual_lag);
        self.placement = FairPlacement::Sleeping { virtual_lag };
    }

    /// Saves lag and the active request deadline before changing runqueues.
    pub(crate) fn capture_migration(
        &mut self,
        virtual_time: u64,
        rq_max_slice_ns: u64,
        timing_granularity_ns: u64,
    ) {
        let relative_deadline = self.virtual_deadline.wrapping_sub(self.vruntime);
        self.placement = match self.placement {
            FairPlacement::Delayed { virtual_lag } => FairPlacement::DelayedMigrating {
                virtual_lag: self.refreshed_delayed_lag(
                    virtual_time,
                    rq_max_slice_ns,
                    timing_granularity_ns,
                    virtual_lag,
                ),
                relative_deadline,
            },
            FairPlacement::Migrating { .. } | FairPlacement::DelayedMigrating { .. } => return,
            FairPlacement::Initial | FairPlacement::Active | FairPlacement::Sleeping { .. } => {
                FairPlacement::Migrating {
                    virtual_lag: self.bounded_virtual_lag(
                        virtual_time,
                        rq_max_slice_ns,
                        timing_granularity_ns,
                    ),
                    relative_deadline,
                }
            }
        };
    }

    /// Places a newly runnable entity into the runqueue competition.
    ///
    /// Linux gives an initial entity half a service request so it joins peers
    /// near their average progress. A sleeping entity instead restores its
    /// saved lag and starts a full request. Adding either entity changes the
    /// weighted average, so inflate saved lag by `(W + w) / W` before insert.
    pub(crate) fn place_after_activation(
        &mut self,
        virtual_time: u64,
        runnable_weight: u64,
    ) -> Result<(), TaskError> {
        #[cfg(feature = "qperf-metrics")]
        let sleeping = matches!(self.placement, FairPlacement::Sleeping { .. });
        let (saved_lag, request_ns) = match self.placement {
            FairPlacement::Initial => (0, self.service_request_ns / 2),
            FairPlacement::Sleeping { virtual_lag } => (virtual_lag, self.service_request_ns),
            FairPlacement::Active
            | FairPlacement::Delayed { .. }
            | FairPlacement::DelayedMigrating { .. }
            | FairPlacement::Migrating { .. } => {
                return Err(TaskError::InvalidConfiguration);
            }
        };
        #[cfg(feature = "qperf-metrics")]
        if sleeping {
            crate::diagnostics::counters::record_fair_sleep_wake_lag(saved_lag);
        }
        let placement_lag = self.inflated_placement_lag(saved_lag, runnable_weight);
        self.vruntime = virtual_time.wrapping_sub(placement_lag as u64);
        self.virtual_deadline = self
            .vruntime
            .wrapping_add(weighted_delta(request_ns, self.load_weight()));
        self.protected_until_vruntime = self.vruntime;
        self.placement = FairPlacement::Active;
        Ok(())
    }

    /// Restores state after an owner-to-owner transfer.
    ///
    /// A wake forwarded through another CPU retains sleep placement, while an
    /// already-runnable migration retains its active request. The entity state,
    /// rather than the transport message, owns this semantic distinction.
    pub(crate) fn place_after_transfer(
        &mut self,
        virtual_time: u64,
        runnable_weight: u64,
    ) -> Result<(), TaskError> {
        match self.placement {
            FairPlacement::Initial | FairPlacement::Sleeping { .. } => {
                self.place_after_activation(virtual_time, runnable_weight)
            }
            FairPlacement::Migrating {
                virtual_lag,
                relative_deadline,
            } => {
                let placement_lag = self.inflated_placement_lag(virtual_lag, runnable_weight);
                self.vruntime = virtual_time.wrapping_sub(placement_lag as u64);
                self.virtual_deadline = self.vruntime.wrapping_add(relative_deadline);
                self.protected_until_vruntime = self.vruntime;
                self.placement = FairPlacement::Active;
                Ok(())
            }
            FairPlacement::Delayed { .. } | FairPlacement::DelayedMigrating { .. } => {
                Err(TaskError::InvalidConfiguration)
            }
            FairPlacement::Active => {
                self.place_at_least(virtual_time);
                self.cancel_slice_protection();
                Ok(())
            }
        }
    }

    fn bounded_virtual_lag(
        self,
        virtual_time: u64,
        rq_max_slice_ns: u64,
        timing_granularity_ns: u64,
    ) -> i64 {
        let limit = weighted_delta(
            rq_max_slice_ns.saturating_add(timing_granularity_ns),
            self.load_weight(),
        )
        .min(i64::MAX as u64) as i64;
        virtual_delta(virtual_time, self.vruntime).clamp(-limit, limit)
    }

    /// Marks Linux's `on_rq && sched_delayed` state on an ineligible sleeper.
    pub(crate) fn begin_delayed_dequeue(
        &mut self,
        virtual_time: u64,
        rq_max_slice_ns: u64,
        timing_granularity_ns: u64,
    ) {
        let virtual_lag = self
            .bounded_virtual_lag(virtual_time, rq_max_slice_ns, timing_granularity_ns)
            .min(0);
        #[cfg(feature = "qperf-metrics")]
        crate::diagnostics::counters::record_fair_delayed_begin(virtual_lag);
        self.placement = FairPlacement::Delayed { virtual_lag };
    }

    pub(crate) const fn is_delayed(self) -> bool {
        matches!(self.placement, FairPlacement::Delayed { .. })
    }

    pub(crate) const fn is_delayed_migrating(self) -> bool {
        matches!(self.placement, FairPlacement::DelayedMigrating { .. })
    }

    fn refreshed_delayed_lag(
        self,
        virtual_time: u64,
        rq_max_slice_ns: u64,
        timing_granularity_ns: u64,
        saved_lag: i64,
    ) -> i64 {
        // Linux DELAY_ZERO is enabled in v7.1: delayed service debt may
        // improve toward zero, but it may neither grow more negative nor turn
        // into positive credit while the task sleeps on-rq.
        self.bounded_virtual_lag(virtual_time, rq_max_slice_ns, timing_granularity_ns)
            .max(saved_lag)
            .min(0)
    }

    /// Returns the lag that requires Linux `requeue_delayed_entity()` to
    /// dequeue, place, and reinsert this entity.
    pub(crate) fn delayed_requeue_lag(
        self,
        virtual_time: u64,
        rq_max_slice_ns: u64,
        timing_granularity_ns: u64,
    ) -> Result<Option<i64>, TaskError> {
        let FairPlacement::Delayed {
            virtual_lag: saved_lag,
        } = self.placement
        else {
            return Err(TaskError::InvalidConfiguration);
        };
        #[cfg(feature = "qperf-metrics")]
        crate::diagnostics::counters::record_fair_delayed_wake_refresh(
            saved_lag,
            self.bounded_virtual_lag(virtual_time, rq_max_slice_ns, timing_granularity_ns),
        );
        let lag = self.refreshed_delayed_lag(
            virtual_time,
            rq_max_slice_ns,
            timing_granularity_ns,
            saved_lag,
        );
        #[cfg(feature = "qperf-metrics")]
        crate::diagnostics::counters::record_fair_delayed_wake_lag(lag);
        let placed_vruntime = virtual_time.wrapping_sub(lag as u64);
        Ok((placed_vruntime != self.vruntime).then_some(lag))
    }

    /// Clears delayed state when Linux keeps the existing tree position.
    pub(crate) fn clear_delayed(&mut self) -> Result<(), TaskError> {
        if !matches!(self.placement, FairPlacement::Delayed { .. }) {
            return Err(TaskError::InvalidConfiguration);
        }
        self.placement = FairPlacement::Active;
        Ok(())
    }

    /// Places a delayed entity against the post-dequeue weighted average.
    pub(crate) fn place_reactivated_delayed(
        &mut self,
        virtual_time: u64,
        runnable_weight: u64,
        saved_lag: i64,
    ) -> Result<(), TaskError> {
        if !matches!(self.placement, FairPlacement::Delayed { .. }) {
            return Err(TaskError::InvalidConfiguration);
        }
        let placement_lag = self.inflated_placement_lag(saved_lag, runnable_weight);
        self.vruntime = virtual_time.wrapping_sub(placement_lag as u64);
        self.virtual_deadline = self
            .vruntime
            .wrapping_add(weighted_delta(self.service_request_ns, self.load_weight()));
        self.protected_until_vruntime = self.vruntime;
        self.placement = FairPlacement::Active;
        Ok(())
    }

    /// Linux delayed `dequeue_entity(..., DEQUEUE_DELAYED)`.
    pub(crate) fn finish_delayed_dequeue(
        &mut self,
        virtual_time: u64,
        rq_max_slice_ns: u64,
        timing_granularity_ns: u64,
    ) -> Result<(), TaskError> {
        let FairPlacement::Delayed {
            virtual_lag: saved_lag,
        } = self.placement
        else {
            return Err(TaskError::InvalidConfiguration);
        };
        let virtual_lag = self.refreshed_delayed_lag(
            virtual_time,
            rq_max_slice_ns,
            timing_granularity_ns,
            saved_lag,
        );
        self.placement = FairPlacement::Sleeping { virtual_lag };
        Ok(())
    }

    /// Rebases an on-rq delayed entity after `TASK_ON_RQ_MIGRATING` transfer.
    pub(crate) fn place_delayed_after_transfer(
        &mut self,
        virtual_time: u64,
        runnable_weight: u64,
    ) -> Result<(), TaskError> {
        let FairPlacement::DelayedMigrating {
            virtual_lag,
            relative_deadline,
        } = self.placement
        else {
            return Err(TaskError::InvalidConfiguration);
        };
        let placement_lag = self.inflated_placement_lag(virtual_lag, runnable_weight);
        self.vruntime = virtual_time.wrapping_sub(placement_lag as u64);
        self.virtual_deadline = self.vruntime.wrapping_add(relative_deadline);
        self.protected_until_vruntime = self.vruntime;
        self.placement = FairPlacement::Delayed { virtual_lag };
        Ok(())
    }

    fn inflated_placement_lag(self, saved_lag: i64, runnable_weight: u64) -> i64 {
        if saved_lag == 0 || runnable_weight == 0 {
            return 0;
        }
        let weight = u64::from(self.load_weight());
        (i128::from(saved_lag).saturating_mul(i128::from(runnable_weight.saturating_add(weight)))
            / i128::from(runnable_weight))
        .clamp(i128::from(i64::MIN), i128::from(i64::MAX)) as i64
    }

    /// Starts a new request after expiry or explicit yield.
    pub(crate) fn renew_request(&mut self) {
        self.virtual_deadline = self
            .vruntime
            .wrapping_add(weighted_delta(self.service_request_ns, self.load_weight()));
        self.protected_until_vruntime = self.vruntime;
    }

    /// Forfeits an eligible request when the thread explicitly yields.
    ///
    /// Moving to the active virtual deadline lets positive-lag peers advance
    /// the runqueue virtual time instead of allowing the yielding thread to
    /// anchor eligibility at its old vruntime. An already ineligible entity
    /// keeps its request because it has already yielded its execution position.
    pub(crate) fn yield_request(&mut self, virtual_time: u64) {
        let eligible = self.is_eligible(virtual_time);
        #[cfg(feature = "qperf-metrics")]
        crate::diagnostics::counters::record_fair_yield(
            eligible,
            if eligible {
                virtual_delta(self.virtual_deadline, self.vruntime).max(0) as u64
            } else {
                0
            },
            if eligible {
                0
            } else {
                virtual_delta(self.vruntime, virtual_time).max(0) as u64
            },
        );
        if eligible {
            self.vruntime = virtual_max(self.vruntime, self.virtual_deadline);
            self.renew_request();
        } else if self.request_exhausted() {
            self.renew_request();
        }
    }

    /// Reweights and rebases one active request without discarding its lag.
    pub(crate) fn reconfigure(
        mut self,
        nice: Nice,
        mode: FairMode,
        source_virtual_time: u64,
        destination_virtual_time: u64,
    ) -> Self {
        let old_weight = self.load_weight();
        let new_weight = load_weight(nice, mode);
        let reweight_lag = |lag: i64| {
            (i128::from(lag) * i128::from(old_weight) / i128::from(new_weight))
                .clamp(-i128::from(i64::MAX), i128::from(i64::MAX)) as i64
        };
        let protected = self.slice_is_protected();
        let relative_protection = i128::from(virtual_delta(
            self.protected_until_vruntime,
            source_virtual_time,
        ));
        let relative_deadline =
            i128::from(virtual_delta(self.virtual_deadline, source_virtual_time));
        let lag = i128::from(virtual_delta(source_virtual_time, self.vruntime));
        let reweighted_lag = reweight_lag(lag as i64);
        let reweighted_deadline =
            (relative_deadline * i128::from(old_weight) / i128::from(new_weight))
                .clamp(-i128::from(i64::MAX), i128::from(i64::MAX)) as i64;
        self.nice = nice;
        self.mode = mode;
        self.vruntime = destination_virtual_time.wrapping_sub(reweighted_lag as u64);
        self.virtual_deadline = destination_virtual_time.wrapping_add(reweighted_deadline as u64);
        let active_relative_deadline = self.virtual_deadline.wrapping_sub(self.vruntime);
        self.protected_until_vruntime = if protected {
            let reweighted_protection =
                (relative_protection * i128::from(old_weight) / i128::from(new_weight))
                    .clamp(-i128::from(i64::MAX), i128::from(i64::MAX)) as i64;
            destination_virtual_time.wrapping_add(reweighted_protection as u64)
        } else {
            self.vruntime
        };
        self.placement = match self.placement {
            FairPlacement::Sleeping { virtual_lag } => FairPlacement::Sleeping {
                virtual_lag: reweight_lag(virtual_lag),
            },
            FairPlacement::Delayed { virtual_lag } => FairPlacement::Delayed {
                virtual_lag: reweight_lag(virtual_lag),
            },
            FairPlacement::Migrating { virtual_lag, .. } => FairPlacement::Migrating {
                virtual_lag: reweight_lag(virtual_lag),
                relative_deadline: active_relative_deadline,
            },
            FairPlacement::DelayedMigrating { virtual_lag, .. } => {
                FairPlacement::DelayedMigrating {
                    virtual_lag: reweight_lag(virtual_lag),
                    relative_deadline: active_relative_deadline,
                }
            }
            FairPlacement::Initial | FairPlacement::Active => self.placement,
        };
        self
    }

    /// Protects a newly selected request through the shortest competing slice.
    ///
    /// Linux v7.1 `RUN_TO_PARITY` uses `min(current.slice,
    /// cfs_rq_min_slice())`; the active request deadline remains the upper
    /// bound when no shorter entity is queued.
    pub(crate) fn set_slice_protection(&mut self, shortest_competing_slice_ns: Option<u64>) {
        let protected_slice_ns = shortest_competing_slice_ns
            .unwrap_or(self.service_request_ns)
            .min(self.service_request_ns);
        self.protected_until_vruntime = if protected_slice_ns == self.service_request_ns {
            self.virtual_deadline
        } else {
            virtual_min(
                self.virtual_deadline,
                self.vruntime
                    .wrapping_add(weighted_delta(protected_slice_ns, self.load_weight())),
            )
        };
    }

    /// Tightens a running request after another Fair entity joins its queue.
    pub(crate) fn update_slice_protection(&mut self, shortest_queued_slice_ns: u64) {
        let queued_boundary = self
            .vruntime
            .wrapping_add(weighted_delta(shortest_queued_slice_ns, self.load_weight()));
        self.protected_until_vruntime = virtual_min(self.protected_until_vruntime, queued_boundary);
    }

    /// Cancels protection when Linux `PREEMPT_SHORT` selects the shorter wakee.
    pub(crate) fn cancel_slice_protection(&mut self) {
        self.protected_until_vruntime = self.vruntime;
    }

    /// Returns whether `RUN_TO_PARITY` still protects this active request.
    pub(crate) const fn slice_is_protected(self) -> bool {
        virtual_before(self.vruntime, self.protected_until_vruntime)
    }

    /// Reports whether this entity has a shorter physical service request.
    pub(crate) const fn has_shorter_slice_than(self, current: Self) -> bool {
        self.service_request_ns < current.service_request_ns
    }

    /// Returns the Linux hrtick boundary for the active EEVDF request.
    ///
    /// `vprot` only protects run-to-parity selection. Linux programs hrtick at
    /// `deadline - vruntime`; shortening protection after an enqueue must not
    /// create an earlier physical timer deadline. The returned value is the
    /// unscaled physical service represented by that virtual deadline. The
    /// final physical delay retains Linux `hrtick_start()`'s 10 us floor.
    pub(crate) fn runtime_deadline_delta_ns(self) -> u64 {
        let virtual_delta = self.virtual_deadline.wrapping_sub(self.vruntime);
        ((u128::from(self.load_weight()) * u128::from(virtual_delta)) / u128::from(BASE_WEIGHT))
            .min(u128::from(u64::MAX)) as u64
    }

    pub(crate) fn finish_runtime_deadline_delta_ns(self, irq_util_avg: u32) -> u64 {
        finish_hrtick_delta_ns(self.runtime_deadline_delta_ns(), irq_util_avg)
    }
}

impl FairEntity {
    /// Reports whether the active request has no service left.
    pub(crate) const fn request_exhausted(self) -> bool {
        !virtual_before(self.vruntime, self.virtual_deadline)
    }

    /// Returns whether non-negative lag makes this entity eligible.
    pub const fn is_eligible(self, virtual_time: u64) -> bool {
        !virtual_after(self.vruntime, virtual_time)
    }

    /// Returns normal, batch, or idle fair semantics.
    pub const fn mode(self) -> FairMode {
        self.mode
    }

    /// Returns accumulated weighted virtual runtime.
    pub const fn vruntime(self) -> u64 {
        self.vruntime
    }

    /// Returns the Linux-compatible load weight used for lag accounting.
    pub(crate) const fn weight(self) -> u32 {
        self.load_weight()
    }

    /// Returns `WEIGHT_IDLEPRIO` for idle policy and the nice weight otherwise,
    /// exactly like Linux `set_load_weight()`.
    const fn load_weight(self) -> u32 {
        load_weight(self.nice, self.mode)
    }

    /// Returns the EEVDF virtual deadline.
    pub const fn virtual_deadline(self) -> u64 {
        self.virtual_deadline
    }

    /// Returns whether this entity owns the earlier EEVDF deadline.
    ///
    /// Linux v7.1 compares virtual deadlines on the modular virtual-time
    /// timeline and lets EEVDF eligibility and slice protection decide wakeup
    /// preemption. A legacy physical-time wakeup granularity must not be added
    /// to these weighted virtual deadlines.
    pub(crate) const fn deadline_precedes(self, current: Self) -> bool {
        virtual_before(self.virtual_deadline, current.virtual_deadline)
    }

    /// Returns the physical service request used for this EEVDF slice.
    pub(crate) const fn service_request_ns(self) -> u64 {
        self.service_request_ns
    }
}

const fn weighted_delta_needs_scaling(weight: u32) -> bool {
    weight != BASE_WEIGHT as u32
}

fn weighted_delta(runtime_ns: u64, weight: u32) -> u64 {
    // Linux `calc_delta_fair()` returns the execution delta unchanged for
    // NICE_0_LOAD. Besides preserving the exact value, this keeps the default
    // fair path out of the software 128-bit division helper.
    if !weighted_delta_needs_scaling(weight) {
        return runtime_ns.min(MAX_VIRTUAL_DELTA);
    }
    ((runtime_ns as u128).saturating_mul(BASE_WEIGHT as u128) / weight as u128)
        .min(MAX_VIRTUAL_DELTA as u128) as u64
}

#[cfg(test)]
mod tests;