# Library prosa.analysis.facts.transform.swaps

Require Export prosa.analysis.facts.transform.replace_at.

Require Export prosa.analysis.facts.behavior.deadlines.

Require Export prosa.analysis.facts.behavior.deadlines.

In this file, we establish invariants about schedules in which two
allocations have been swapped, as for instance it is done in the
classic EDF optimality proof.

For any given type of jobs...

... any given type of processor states:

...consider any given reference schedule.

In the following, let sched' denote the schedule in which the
allocations at t1 and t2 have been swapped.

First, we note that the trivial case where t1 == t2 is not interesting
because then the two schedules are identical.

In this trivial case, the amount of service received hence
is obviously always identical.

In any case, the two schedules do not differ at non-swapped times.

By definition, if a job is scheduled at t2 in the original
schedule, then it is found at t1 in the new schedule.

Similarly, a job scheduled at t1 in the original schedule is
scheduled at t2 after the swap.

If a job is scheduled at any time not involved in the swap, then
it remains scheduled at that time in the new schedule.

Lemma swap_job_scheduled_other_times:

∀ j t,

t1 != t →

t2 != t →

scheduled_at sched' j t =

scheduled_at sched j t.

∀ j t,

t1 != t →

t2 != t →

scheduled_at sched' j t =

scheduled_at sched j t.

To make case analysis more convenient, we summarize the preceding
three lemmas as a disjunction.

Corollary swap_job_scheduled_cases:

∀ j t,

scheduled_at sched' j t →

scheduled_at sched' j t = scheduled_at sched j t

∨

t = t1 ∧ scheduled_at sched' j t = scheduled_at sched j t2

∨

t = t2 ∧ scheduled_at sched' j t = scheduled_at sched j t1.

∀ j t,

scheduled_at sched' j t →

scheduled_at sched' j t = scheduled_at sched j t

∨

t = t1 ∧ scheduled_at sched' j t = scheduled_at sched j t2

∨

t = t2 ∧ scheduled_at sched' j t = scheduled_at sched j t1.

From this, we can easily conclude that no jobs have appeared out
of thin air: if a job scheduled at some time in the new schedule,
then it was also scheduled at some time in the original
schedule.

Mirroring swap_job_scheduled_cases above, we also state a
disjunction for case analysis under the premise that a job is
scheduled in the original schedule.

Lemma swap_job_scheduled_original_cases:

∀ j t,

scheduled_at sched j t →

scheduled_at sched' j t = scheduled_at sched j t

∨

t = t1 ∧ scheduled_at sched' j t2 = scheduled_at sched j t

∨

t = t2 ∧ scheduled_at sched' j t1 = scheduled_at sched j t.

∀ j t,

scheduled_at sched j t →

scheduled_at sched' j t = scheduled_at sched j t

∨

t = t1 ∧ scheduled_at sched' j t2 = scheduled_at sched j t

∨

t = t2 ∧ scheduled_at sched' j t1 = scheduled_at sched j t.

Thus, we can conclude that no jobs are lost: if a job is
scheduled at some point in the original schedule, then it is also
scheduled at some point in the new schedule.

Corollary swap_job_scheduled_original:

∀ j t,

scheduled_at sched j t →

∃ t',

scheduled_at sched' j t'.

∀ j t,

scheduled_at sched j t →

∃ t',

scheduled_at sched' j t'.

In the following, we lift the above basic invariants to
statements about invariants about the cumulative amount of
service received.
To avoid dealing with symmetric cases, assume in the following
that t1 and t2 are ordered.

As another trivial invariant, we observe that nothing has changed
before time t1.

Similarly, nothing has changed after time t2.

Thus, we observe that, before t1, the two schedules are identical with
regard to the service received by any job because they are identical.

Corollary service_before_swap_invariant:

∀ t,

t ≤ t1 →

∀ (j : Job),

service sched j t = service sched' j t.

∀ t,

t ≤ t1 →

∀ (j : Job),

service sched j t = service sched' j t.

Likewise, we observe that, *after* t2, the swapped schedule again does not
differ with regard to the service received by any job.

Lemma service_after_swap_invariant:

∀ t,

t2 < t →

∀ (j : Job),

service sched j t = service sched' j t.

∀ t,

t2 < t →

∀ (j : Job),

service sched j t = service sched' j t.

Finally, we note that, trivially, jobs that are not involved in
the swap receive invariant service.

Lemma service_of_others_invariant:

∀ t j,

~~ scheduled_in j (sched t1) →

~~ scheduled_in j (sched t2) →

service sched j t = service sched' j t.

End SwappedFacts.

∀ t j,

~~ scheduled_in j (sched t1) →

~~ scheduled_in j (sched t2) →

service sched j t = service sched' j t.

End SwappedFacts.

In the following section, we observe that, if the original
schedule satisfies certain properties, then so does the new
scheduled obtained by swapping the allocations at times t1 and
t2.

For any given type of jobs...

... any given type of processor states:

...consider any given reference schedule.

First, we observe that if jobs never accumulate more service than
required, then that's still the case after the swap.

Lemma swapped_service_bound:

(∀ j t, service sched j t ≤ job_cost j) →

(∀ j t, service sched' j t ≤ job_cost j).

(∀ j t, service sched j t ≤ job_cost j) →

(∀ j t, service sched' j t ≤ job_cost j).

From the above service bound, we conclude that, if if completed jobs don't
execute in the original schedule, then that's still the case after the
swap, assuming an ideal unit-service model (i.e., scheduled jobs receive
exactly one unit of service).

Lemma swapped_completed_jobs_dont_execute:

unit_service_proc_model PState →

ideal_progress_proc_model PState →

completed_jobs_dont_execute sched →

completed_jobs_dont_execute sched'.

unit_service_proc_model PState →

ideal_progress_proc_model PState →

completed_jobs_dont_execute sched →

completed_jobs_dont_execute sched'.

Suppose all jobs in the original schedule come from some arrival sequence...

Variable arr_seq: arrival_sequence Job.

Hypothesis H_from_arr_seq: jobs_come_from_arrival_sequence sched arr_seq.

Hypothesis H_from_arr_seq: jobs_come_from_arrival_sequence sched arr_seq.

...then that's still the case in the new schedule.

Lemma swapped_jobs_come_from_arrival_sequence:

jobs_come_from_arrival_sequence sched' arr_seq.

End SwappedScheduleProperties.

jobs_come_from_arrival_sequence sched' arr_seq.

End SwappedScheduleProperties.

In the next section, we consider a special case of swaps: namely,
when the job moved earlier has an earlier deadline than the job
being moved to a later allocation, assuming that the former has
not yet missed its deadline, which is the core transformation of
the classic EDF optimality proof.

For any given type of jobs with costs and deadlines...

... any given type of processor states...

...consider a given reference schedule...

...in which complete jobs don't execute...

...and scheduled jobs always receive service.

...that are ordered.

Further, assume that, if there are jobs scheduled at times t1 and t2, then
they either have the same deadline or violate EDF, ...

Hypothesis H_not_EDF:

∀ j1 j2,

scheduled_at sched j1 t1 →

scheduled_at sched j2 t2 →

job_deadline j1 ≥ job_deadline j2.

∀ j1 j2,

scheduled_at sched j1 t1 →

scheduled_at sched j2 t2 →

job_deadline j1 ≥ job_deadline j2.

...and that we don't move idle times or deadline misses earlier,
i.e., if t1 is not an idle time, then neither is t2 and whatever
job is scheduled at time t2 has not yet missed its deadline.

Hypothesis H_no_idle_time_at_t2:

∀ j1,

scheduled_at sched j1 t1 →

∃ j2, scheduled_at sched j2 t2 ∧ job_deadline j2 > t2.

∀ j1,

scheduled_at sched j1 t1 →

∃ j2, scheduled_at sched j2 t2 ∧ job_deadline j2 > t2.

Consider the schedule obtained from swapping the allocations at times t1 and t2.

The key property of this transformation is that, for any job that
meets its deadline in the original schedule, we have not
introduced any deadline misses, which we establish by considering
a number of different cases.

Consider any job...

... that meets its deadline in the original schedule.

First we observe that jobs that are not involved in the swap
still meet their deadlines.

Lemma uninvolved_implies_deadline_met:

~~ scheduled_at sched j t1 →

~~ scheduled_at sched j t2 →

job_meets_deadline sched' j.

~~ scheduled_at sched j t1 →

~~ scheduled_at sched j t2 →

job_meets_deadline sched' j.

Next, we observe that a swap is unproblematic for the job scheduled at
time t2.

Finally, we observe is also unproblematic for the job that is
moved to a later allocation.

Lemma moved_later_implies_deadline_met:

scheduled_at sched j t1 →

job_meets_deadline sched' j.

End NoNewDeadlineMissesCases.

scheduled_at sched j t1 →

job_meets_deadline sched' j.

End NoNewDeadlineMissesCases.

From the above case analysis, we conclude that no deadline misses
are introduced in the schedule obtained from swapping the
allocations at times t1 and t2.

Theorem edf_swap_no_deadline_misses_introduced:

∀ j, job_meets_deadline sched j → job_meets_deadline sched' j.

End EDFSwap.

∀ j, job_meets_deadline sched j → job_meets_deadline sched' j.

End EDFSwap.