# Library prosa.analysis.facts.priority.inversion

Require Export prosa.analysis.definitions.priority_inversion.
Require Export prosa.analysis.facts.model.uniprocessor.

# Lemmas about Priority Inversion

This file collects basic facts about the notion of priority inversion in the general context of arbitrary processor models.
Section PI.
Consider any type of tasks ...
Context {Task : TaskType}.
Context `{TaskCost Task}.
... and any type of jobs associated with these tasks.
Context {Job : JobType}.
Context `{JobTask Job Task}.
Context `{JobArrival Job}.
Context `{JobCost Job}.
Next, consider any kind of processor state model, ...
Context {PState : ProcessorState Job}.
... any valid arrival sequence, ...
Variable arr_seq : arrival_sequence Job.
Hypothesis H_valid_arrival_sequence : valid_arrival_sequence arr_seq.
... and any valid schedule.
Variable sched : schedule PState.
Hypothesis H_jobs_come_from_arrival_sequence :
jobs_come_from_arrival_sequence sched arr_seq.
Hypothesis H_jobs_must_arrive_to_execute : jobs_must_arrive_to_execute sched.
Assume a given reflexive JLFP policy...
Context {JLFP : JLFP_policy Job}.
Hypothesis H_priority_is_reflexive : reflexive_job_priorities JLFP.
... and consider an arbitrary job.
Variable j : Job.

## Occurrence of Priority Inversion

First, we observe conditions under which priority inversions does, or does not, arise.
Assume that j is scheduled at time t, then there is no priority inversion at time t.
Lemma sched_itself_implies_no_priority_inversion :
∀ t,
scheduled_at sched j t →
~~ priority_inversion arr_seq sched j t.
Conversely, if job j experiences a priority inversion at time t, then some (other) job is scheduled at the time, ...
Lemma priority_inversion_scheduled_at :
∀ t,
priority_inversion arr_seq sched j t →
∃ j', scheduled_at sched j' t.
... and thus there is no priority inversion at idle instants.
Lemma no_priority_inversion_when_idle :
∀ t,
is_idle arr_seq sched t →
~~ priority_inversion arr_seq sched j t.
Section Uniprocessors.

## Occurrence of Priority Inversion on Uniprocessors

Stronger conditions hold on uniprocessors since only one job can be scheduled at any time.
Hypothesis H_uni : uniprocessor_model PState.
On a uniprocessor, the job scheduled when j incurs priority inversion necessarily has lower priority than j.
Lemma priority_inversion_hep_job :
∀ t j',
scheduled_at sched j' t →
priority_inversion arr_seq sched j t = ~~ hep_job j' j.
Conversely, if a higher-priority job is scheduled on a uniprocessor, then j does not incur priority inversion.
Corollary no_priority_inversion_when_hep_job_scheduled :
∀ t j',
scheduled_at sched j' t →
hep_job j' j →
~~ priority_inversion arr_seq sched j t.
From the above lemmas, we obtain a simplified definition of priority_inversion that holds on uniprocessors.
Lemma uni_priority_inversion_P :
∀ t,
reflect (exists2 j', scheduled_at sched j' t & ~~ hep_job j' j)
(priority_inversion arr_seq sched j t).
End Uniprocessors.

## Cumulative Priority Inversion

We observe that the cumulative priority inversion (CPI) that job j incurs in an interval `[t1, t2)` can be split arbitrarily: is equal to the sum of CPI in an interval `[t1, t_mid)` and CPI in an interval `[t_mid, t2)`.
Lemma cumulative_priority_inversion_cat :
∀ (t_mid t1 t2 : instant),
t1 ≤ t_mid →
t_mid ≤ t2 →
cumulative_priority_inversion arr_seq sched j t1 t2
= cumulative_priority_inversion arr_seq sched j t1 t_mid
+ cumulative_priority_inversion arr_seq sched j t_mid t2.
End PI.