bertogna_fp_comp.v 30 KB
Newer Older
1
Add LoadPath "../../" as rt.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
2 3
Require Import rt.util.Vbase rt.util.lemmas rt.util.divround.
Require Import rt.analysis.basic.bertogna_fp_theory.
4
Require Import ssreflect ssrbool ssrfun eqtype ssrnat seq fintype bigop div path.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
5 6 7

Module ResponseTimeIterationFP.

8
  Import ResponseTimeAnalysisFP.
9

Felipe Cerqueira's avatar
Felipe Cerqueira committed
10 11
  (* In this section, we define the algorithm of Bertogna and Cirinei's
     response-time analysis for FP scheduling. *)
12 13 14
  Section Analysis.
    
    Context {sporadic_task: eqType}.
15 16 17
    Variable task_cost: sporadic_task -> time.
    Variable task_period: sporadic_task -> time.
    Variable task_deadline: sporadic_task -> time.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
18

19
    (* As input for each iteration of the algorithm, we consider pairs
Felipe Cerqueira's avatar
Felipe Cerqueira committed
20
       of tasks and computed response-time bounds. *)
21
    Let task_with_response_time := (sporadic_task * time)%type.
22
    
Felipe Cerqueira's avatar
Felipe Cerqueira committed
23
    Context {Job: eqType}.
24 25
    Variable job_cost: Job -> time.
    Variable job_deadline: Job -> time.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
26 27
    Variable job_task: Job -> sporadic_task.

Felipe Cerqueira's avatar
Felipe Cerqueira committed
28
    (* Consider a platform with num_cpus processors, ... *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
29
    Variable num_cpus: nat.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
30 31

    (* ..., and priorities based on an FP policy. *)
32
    Variable higher_priority: FP_policy sporadic_task.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
33 34

    (* Next we define the fixed-point iteration for computing
Felipe Cerqueira's avatar
Felipe Cerqueira committed
35
       Bertogna's response-time bound of a task set. *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
36
    
Felipe Cerqueira's avatar
Felipe Cerqueira committed
37
    (* First, given a sequence of pairs R_prev = <..., (tsk_hp, R_hp)> of
Felipe Cerqueira's avatar
Felipe Cerqueira committed
38
       response-time bounds for the higher-priority tasks, we define an
Felipe Cerqueira's avatar
Felipe Cerqueira committed
39
       iteration that computes the response-time bound of the current task:
Felipe Cerqueira's avatar
Felipe Cerqueira committed
40 41 42

           R_tsk (0) = task_cost tsk
           R_tsk (step + 1) =  f (R step),
Felipe Cerqueira's avatar
Felipe Cerqueira committed
43

Felipe Cerqueira's avatar
Felipe Cerqueira committed
44 45
       where f is the response-time recurrence, step is the number of iterations,
       and R_tsk (0) is the initial state. *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
46
    Definition per_task_rta (tsk: sporadic_task)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
47
                            (R_prev: seq task_with_response_time) (step: nat) :=
Felipe Cerqueira's avatar
Felipe Cerqueira committed
48 49 50
      iter step
        (fun t => task_cost tsk +
                  div_floor
51
                    (total_interference_bound_fp task_cost task_period tsk
52
                                                R_prev t higher_priority)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
53 54 55 56
                    num_cpus)
        (task_cost tsk).

    (* To ensure that the iteration converges, we will apply per_task_rta
57
       a "sufficient" number of times: task_deadline tsk - task_cost tsk + 1.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
58
       This corresponds to the time complexity of the iteration. *)
59
    Definition max_steps (tsk: sporadic_task) := task_deadline tsk - task_cost tsk + 1.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
60
    
Felipe Cerqueira's avatar
Felipe Cerqueira committed
61 62 63 64
    (* Next we compute the response-time bounds for the entire task set.
       Since high-priority tasks may not be schedulable, we allow the
       computation to fail.
       Thus, given the response-time bound of previous tasks, we either
Felipe Cerqueira's avatar
Felipe Cerqueira committed
65
       (a) append the computed response-time bound (tsk, R) of the current task
Felipe Cerqueira's avatar
Felipe Cerqueira committed
66 67
           to the list of pairs, or,
       (b) return None if the response-time analysis failed. *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
68
    Definition fp_bound_of_task hp_pairs tsk :=
Felipe Cerqueira's avatar
Felipe Cerqueira committed
69 70 71 72 73 74 75 76 77 78
      if hp_pairs is Some rt_bounds then
        let R := per_task_rta tsk rt_bounds (max_steps tsk) in
          if R <= task_deadline tsk then
            Some (rcons rt_bounds (tsk, R))
          else None
      else None.

    (* The response-time analysis for a given task set is defined
       as a left-fold (reduce) based on the function above.
       This either returns a list of task and response-time bounds, or None. *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
79 80
    Definition fp_claimed_bounds (ts: taskset_of sporadic_task) :=
      foldl fp_bound_of_task (Some [::]) ts.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
81

Felipe Cerqueira's avatar
Felipe Cerqueira committed
82 83
    (* The schedulability test simply checks if we got a list of
       response-time bounds (i.e., if the computation did not fail). *)
84
    Definition fp_schedulable (ts: taskset_of sporadic_task) :=
Felipe Cerqueira's avatar
Felipe Cerqueira committed
85
      fp_claimed_bounds ts != None.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
86
    
Felipe Cerqueira's avatar
Felipe Cerqueira committed
87 88
    (* In the following section, we prove several helper lemmas about the
       list of response-time bounds. The results seem trivial, but must be proven
Felipe Cerqueira's avatar
Felipe Cerqueira committed
89 90
       nonetheless since the list of response-time bounds is computed with
       a specific algorithm and there are no lemmas in the library for that. *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
91
    Section SimpleLemmas.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
92

93 94 95 96 97
      (* First, we show that the first component of the computed list is the set of tasks. *)
      Lemma fp_claimed_bounds_unzip :
        forall ts hp_bounds, 
          fp_claimed_bounds ts = Some hp_bounds ->
          unzip1 hp_bounds = ts.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
98
      Proof.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
        unfold fp_claimed_bounds in *; intros ts.
        induction ts using last_ind; first by destruct hp_bounds.
        {
          intros hp_bounds SOME.
          destruct (lastP hp_bounds) as [| hp_bounds'].
          {
            rewrite -cats1 foldl_cat /= in SOME.
            unfold fp_bound_of_task at 1 in SOME; simpl in *; desf.
            by destruct l.
          }
          rewrite -cats1 foldl_cat /= in SOME.
          unfold fp_bound_of_task at 1 in SOME; simpl in *; desf.
          move: H0 => /eqP EQSEQ.
          rewrite eqseq_rcons in EQSEQ.
          move: EQSEQ => /andP [/eqP SUBST /eqP EQSEQ]; subst.
          unfold unzip1; rewrite map_rcons; f_equal.
          by apply IHts.
        }
      Qed.
      
      (* Next, we show that some properties of the analysis are preserved for the
         prefixes of the list: (a) the tasks do not change, (b) R <= deadline,
         (c) R is computed using the response-time equation, ... *) 
      Lemma fp_claimed_bounds_rcons :
Felipe Cerqueira's avatar
Felipe Cerqueira committed
123
        forall ts' hp_bounds tsk1 tsk2 R,
124 125 126 127 128
          (fp_claimed_bounds (rcons ts' tsk1) = Some (rcons hp_bounds (tsk2, R)) ->
           (fp_claimed_bounds ts' = Some hp_bounds /\
            tsk1 = tsk2 /\
            R = per_task_rta tsk1 hp_bounds (max_steps tsk1) /\
            R <= task_deadline tsk1)).
Felipe Cerqueira's avatar
Felipe Cerqueira committed
129
      Proof.
130 131
        intros ts hp_bounds tsk tsk' R.
        rewrite -cats1.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
132
        unfold fp_claimed_bounds in *.
133 134 135
        rewrite foldl_cat /=.
        unfold fp_bound_of_task at 1; simpl; desf.
        intros EQ; inversion EQ; move: EQ H0 => _ /eqP EQ.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
136
        rewrite eqseq_rcons in EQ.
137 138
        move: EQ => /andP [/eqP EQ /eqP RESP].
        by inversion RESP; repeat split; subst.
139
      Qed.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
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
      (* ..., which implies that any prefix of the computation is the computation
         of the prefix. *)
      Lemma fp_claimed_bounds_take :
        forall ts hp_bounds i,
          fp_claimed_bounds ts = Some hp_bounds ->
          i <= size hp_bounds ->
          fp_claimed_bounds (take i ts) = Some (take i hp_bounds).
      Proof.                                                        
        intros ts hp_bounds i SOME LTi.
        have UNZIP := fp_claimed_bounds_unzip ts hp_bounds SOME.
        rewrite <- UNZIP in *.
        rewrite -[hp_bounds]take_size /unzip1 map_take in SOME.
        fold (unzip1 hp_bounds) in *; clear UNZIP.
        rewrite leq_eqVlt in LTi.
        move: LTi => /orP [/eqP EQ | LTi]; first by subst.
        remember (size hp_bounds) as len; apply eq_leq in Heqlen.
        induction len; first by rewrite ltn0 in LTi.
        {
          assert (TAKElen: fp_claimed_bounds (take len (unzip1 (hp_bounds))) =
                             Some (take len (hp_bounds))).
          {
            assert (exists p, p \in hp_bounds).
            {
              destruct hp_bounds; first by rewrite ltn0 in Heqlen.
              by exists t; rewrite in_cons eq_refl orTb.
            } destruct H as [[tsk R] _].
             rewrite (take_nth tsk) in SOME; last by rewrite size_map.
            rewrite (take_nth (tsk,R)) in SOME; last by done.
            destruct (nth (tsk, R) hp_bounds len) as [tsk_len R_len].
            by apply fp_claimed_bounds_rcons in SOME; des.
          }
          rewrite ltnS leq_eqVlt in LTi.
          move: LTi => /orP [/eqP EQ | LESS]; first by subst.
          apply ltnW in Heqlen.
          by specialize (IHlen Heqlen TAKElen LESS).
        }
Felipe Cerqueira's avatar
Felipe Cerqueira committed
177
      Qed.
178
      
Felipe Cerqueira's avatar
Felipe Cerqueira committed
179
      (* If the analysis suceeds, the computed response-time bounds are no larger
180
         than the deadlines ... *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
181
      Lemma fp_claimed_bounds_le_deadline :
Felipe Cerqueira's avatar
Felipe Cerqueira committed
182
        forall ts' rt_bounds tsk R,
Felipe Cerqueira's avatar
Felipe Cerqueira committed
183
          fp_claimed_bounds ts' = Some rt_bounds ->
Felipe Cerqueira's avatar
Felipe Cerqueira committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
          (tsk, R) \in rt_bounds ->
          R <= task_deadline tsk.
      Proof.
        intros ts; induction ts as [| ts' tsk_lst] using last_ind.
        {
          intros rt_bounds tsk R SOME IN.
          by inversion SOME; subst; rewrite in_nil in IN.
        }
        {
          intros rt_bounds tsk_i R SOME IN.
          destruct (lastP rt_bounds) as [|rt_bounds (tsk_lst', R_lst)];
            first by rewrite in_nil in IN.
          rewrite mem_rcons in_cons in IN; move: IN => /orP IN.
          destruct IN as [LAST | FRONT].
          {
            move: LAST => /eqP LAST.
            rewrite -cats1 in SOME.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
201 202 203
            unfold fp_claimed_bounds in *.
            rewrite foldl_cat /= in SOME.
            unfold fp_bound_of_task in SOME.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
204 205 206 207 208 209 210 211 212
            desf; rename H0 into EQ.
            move: EQ => /eqP EQ.
            rewrite eqseq_rcons in EQ.
            move: EQ => /andP [_ /eqP EQ].
            inversion EQ; subst.
            by apply Heq0.
          }
          {
            apply IHts with (rt_bounds := rt_bounds); last by ins.
213
            by apply fp_claimed_bounds_rcons in SOME; des.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
214 215 216
          }
        }
      Qed.
217
      
218
      (* ... and no smaller than the task costs. *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
219
      Lemma fp_claimed_bounds_ge_cost :
Felipe Cerqueira's avatar
Felipe Cerqueira committed
220
        forall ts' rt_bounds tsk R,
Felipe Cerqueira's avatar
Felipe Cerqueira committed
221
          fp_claimed_bounds ts' = Some rt_bounds ->
Felipe Cerqueira's avatar
Felipe Cerqueira committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
          (tsk, R) \in rt_bounds ->
          R >= task_cost tsk.
      Proof.
        intros ts; induction ts as [| ts' tsk_lst] using last_ind.
        {
          intros rt_bounds tsk R SOME IN.
          by inversion SOME; subst; rewrite in_nil in IN.
        }
        {
          intros rt_bounds tsk_i R SOME IN.
          destruct (lastP rt_bounds) as [|rt_bounds (tsk_lst', R_lst)];
            first by rewrite in_nil in IN.
          rewrite mem_rcons in_cons in IN; move: IN => /orP IN.
          destruct IN as [LAST | FRONT].
          {
            move: LAST => /eqP LAST.
            rewrite -cats1 in SOME.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
239 240 241
            unfold fp_claimed_bounds in *.
            rewrite foldl_cat /= in SOME.
            unfold fp_bound_of_task in SOME.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
242 243 244 245 246 247 248 249 250 251
            desf; rename H0 into EQ.
            move: EQ => /eqP EQ.
            rewrite eqseq_rcons in EQ.
            move: EQ => /andP [_ /eqP EQ].
            inversion EQ; subst.
            by destruct (max_steps tsk_lst');
              [by apply leqnn | by apply leq_addr].
          }
          {
            apply IHts with (rt_bounds := rt_bounds); last by ins.
252
            by apply fp_claimed_bounds_rcons in SOME; des.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
253 254 255 256
          }
        }
      Qed.

Felipe Cerqueira's avatar
Felipe Cerqueira committed
257
      (* Short lemma about unfolding the iteration one step. *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
258 259 260
      Lemma per_task_rta_fold :
        forall tsk rt_bounds,
          task_cost tsk +
261
           div_floor (total_interference_bound_fp task_cost task_period tsk rt_bounds
262
                     (per_task_rta tsk rt_bounds (max_steps tsk)) higher_priority) num_cpus
Felipe Cerqueira's avatar
Felipe Cerqueira committed
263 264
          = per_task_rta tsk rt_bounds (max_steps tsk).+1.
      Proof.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
265
          by done.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
266
      Qed.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
267

Felipe Cerqueira's avatar
Felipe Cerqueira committed
268 269 270
    End SimpleLemmas.

    (* In this section, we prove that if the task set is sorted by priority,
Felipe Cerqueira's avatar
Felipe Cerqueira committed
271
       the tasks in fp_claimed_bounds are interfering tasks.  *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
272 273 274
    Section HighPriorityTasks.

      (* Consider a list of previous tasks and a task tsk to be analyzed. *)
275
      Variable ts: taskset_of sporadic_task.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
276 277

      (* Assume that the task set doesn't contain duplicates and is sorted by priority, ... *)
278 279
      Hypothesis H_task_set_is_a_set: uniq ts.
      Hypothesis H_task_set_is_sorted: sorted higher_priority ts.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
280

281 282
      (* ...the priority order is strict (<), ...*)
      Hypothesis H_priority_irreflexive: irreflexive higher_priority.
283
      Hypothesis H_priority_transitive: transitive higher_priority.
284
      Hypothesis H_priority_antissymetric: antisymmetric higher_priority.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
285 286 287 288
      
      (* ... and that the response-time analysis succeeds. *)
      Variable hp_bounds: seq task_with_response_time.
      Variable R: time.
289 290 291 292 293 294
      Hypothesis H_analysis_succeeds: fp_claimed_bounds ts = Some hp_bounds.

      (* Let's refer to tasks by index. *)
      Variable elem: sporadic_task.
      Let TASK := nth elem ts.
                    
Felipe Cerqueira's avatar
Felipe Cerqueira committed
295
      (* Then, the tasks in the prefix of fp_claimed_bounds are exactly interfering tasks
Felipe Cerqueira's avatar
Felipe Cerqueira committed
296
         under FP scheduling.*)
297 298 299 300
      Lemma fp_claimed_bounds_interf:
        forall idx,
          idx < size ts ->
          [seq tsk_hp <- ts | fp_can_interfere_with higher_priority (TASK idx) tsk_hp] = take idx ts.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
301
      Proof.
302
        rename H_task_set_is_sorted into SORT,
Felipe Cerqueira's avatar
Felipe Cerqueira committed
303
               H_task_set_is_a_set into UNIQ,
304 305 306
               H_priority_antissymetric into ANTI,
               H_priority_irreflexive into IRR.
        induction idx.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
307
        {
308 309 310 311 312 313 314 315 316 317 318 319
          intros LT.
          destruct ts as [| tsk0 ts']; [by done | simpl in SORT].
          unfold fp_can_interfere_with; rewrite /= eq_refl andbF.
          apply eq_trans with (y := filter pred0 ts');
            last by apply filter_pred0.
          apply eq_in_filter; red; intros x INx; rewrite /TASK /=.
          destruct (x != tsk0) eqn:SAME; rewrite ?andbT ?andbF //.
          apply negbTE; apply/negP; unfold not; intro HP.
          move: SAME => /eqP SAME; apply SAME; clear SAME.
          apply ANTI; apply/andP; split; first by done.
          apply order_path_min in SORT; last by done.
          by move: SORT => /allP SORT; apply SORT.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
320 321
        }
        {
322 323 324 325 326 327 328 329 330 331
          intros LT.
          generalize LT; intro LT'; apply ltSnm in LT.
          feed IHidx; first by done.
          rewrite -filter_idx_le_takeS //.
          apply eq_in_filter; red; intros x INx.
          unfold fp_can_interfere_with.
          generalize INx; intro SUBST; apply nth_index with (x0 := elem) in SUBST.
          rewrite -SUBST; clear SUBST.
          rewrite index_uniq; [ | by rewrite index_mem | by done].
          apply/idP/idP.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
332
          {
333 334 335 336 337 338
            move => /andP [HP DIFF].
            unfold TASK in *.
            apply sorted_uniq_rel_implies_le_idx in HP; try (by done);
              last by rewrite index_mem.
            by rewrite leq_eqVlt in HP; move: HP => /orP [/eqP SAME | LESS];
                first by rewrite SAME eq_refl in DIFF.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
339 340
          }
          {
341 342 343 344 345 346 347 348
            intros LEidx; apply/andP; split;
              first by apply sorted_lt_idx_implies_rel.
            apply/eqP; red; intro BUG.
            eapply f_equal with (f := fun x => index x ts) in BUG.
            rewrite nth_index in BUG; last by done.
            rewrite BUG in LEidx.
            by rewrite index_uniq // ltnn in LEidx.
          }
Felipe Cerqueira's avatar
Felipe Cerqueira committed
349 350
        }
      Qed.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
351
      
Felipe Cerqueira's avatar
Felipe Cerqueira committed
352 353
    End HighPriorityTasks.

Felipe Cerqueira's avatar
Felipe Cerqueira committed
354
    (* In this section, we show that the fixed-point iteration converges. *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
355 356
    Section Convergence.

357
      (* Consider any set of higher-priority tasks. *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
358 359
      Variable ts_hp: taskset_of sporadic_task.

360
      (* Assume that the response-time analysis succeeds for the higher-priority tasks. *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
361
      Variable rt_bounds: seq task_with_response_time.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
362
      Hypothesis H_test_succeeds: fp_claimed_bounds ts_hp = Some rt_bounds.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
363

364
      (* Consider any task tsk to be analyzed, ... *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
365 366
      Variable tsk: sporadic_task.

367 368 369 370
      (* ... and assume all tasks have valid parameters. *)
      Hypothesis H_valid_task_parameters:
        valid_sporadic_taskset task_cost task_period task_deadline (rcons ts_hp tsk).

Felipe Cerqueira's avatar
Felipe Cerqueira committed
371 372 373
      (* To simplify, let f denote the fixed-point iteration. *)
      Let f := per_task_rta tsk rt_bounds.

Felipe Cerqueira's avatar
Felipe Cerqueira committed
374
      (* Assume that f (max_steps tsk) is no larger than the deadline. *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
375 376 377 378 379 380 381 382
      Hypothesis H_no_larger_than_deadline: f (max_steps tsk) <= task_deadline tsk.

      (* First, we show that f is monotonically increasing. *)
      Lemma bertogna_fp_comp_f_monotonic :
        forall x1 x2, x1 <= x2 -> f x1 <= f x2.
      Proof.
        unfold valid_sporadic_taskset, is_valid_sporadic_task in *.
        rename H_test_succeeds into SOME,
383
               H_valid_task_parameters into VALID.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
384 385 386 387 388 389 390 391 392 393
        intros x1 x2 LEx; unfold f, per_task_rta.
        apply fun_mon_iter_mon; [by ins | by ins; apply leq_addr |].
        clear LEx x1 x2; intros x1 x2 LEx.
        unfold div_floor, total_interference_bound_fp.
        rewrite big_seq_cond [\sum_(i <- _ | let '(tsk_other, _) := i in
                                 _ && (tsk_other != tsk))_]big_seq_cond.
        rewrite leq_add2l leq_div2r // leq_sum //.

        intros i; destruct (i \in rt_bounds) eqn:HP; last by rewrite andFb.
        destruct i as [i R]; intros _.
394 395 396 397 398 399 400
        have GE_COST := fp_claimed_bounds_ge_cost ts_hp rt_bounds i R SOME.
        have UNZIP := fp_claimed_bounds_unzip ts_hp rt_bounds SOME.
        assert (IN: i \in ts_hp).
        {
          by rewrite -UNZIP; apply/mapP; exists (i,R).
        }
        unfold interference_bound_generic; simpl.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
401 402 403
        rewrite leq_min; apply/andP; split.
        {
          apply leq_trans with (n := W task_cost task_period i R x1); first by apply geq_minl.
404
            exploit (VALID i); [by rewrite mem_rcons in_cons IN orbT | ins; des].
Felipe Cerqueira's avatar
Felipe Cerqueira committed
405
            by apply W_monotonic; try by ins; apply GE_COST.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
406 407 408 409 410 411 412
        }
        {
          apply leq_trans with (n := x1 - task_cost tsk + 1); first by apply geq_minr.
          by rewrite leq_add2r leq_sub2r //.
        }
      Qed.

413
      (* If the iteration converged at an earlier step, it remains as a fixed point. *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
414
      Lemma bertogna_fp_comp_f_converges_early :
415
        (exists k, k <= max_steps tsk /\ f k = f k.+1) ->
Felipe Cerqueira's avatar
Felipe Cerqueira committed
416 417
        f (max_steps tsk) = f (max_steps tsk).+1.
      Proof.
418
        by intros EX; des; apply iter_fix with (k := k).
Felipe Cerqueira's avatar
Felipe Cerqueira committed
419 420 421 422 423 424 425 426
      Qed.

      (* Else, we derive a contradiction. *)
      Section DerivingContradiction.

        (* Assume instead that the iteration continued to diverge. *)
        Hypothesis H_keeps_diverging:
          forall k,
427
            k <= max_steps tsk -> f k != f k.+1.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
428 429 430 431

        (* By monotonicity, it follows that the value always increases. *)
        Lemma bertogna_fp_comp_f_increases :
          forall k,
432
            k <= max_steps tsk ->
Felipe Cerqueira's avatar
Felipe Cerqueira committed
433 434 435 436 437 438 439 440
            f k < f k.+1.
        Proof.
          intros k LT.
          rewrite ltn_neqAle; apply/andP; split.
            by apply H_keeps_diverging.
            by apply bertogna_fp_comp_f_monotonic, leqnSn.
        Qed.

Felipe Cerqueira's avatar
Felipe Cerqueira committed
441
        (* In the end, the response-time bound must exceed the deadline. Contradiction! *)
442 443 444 445
        Lemma bertogna_fp_comp_rt_grows_too_much :
          forall k,
            k <= max_steps tsk ->
            f k > k + task_cost tsk - 1.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
446
        Proof.
447 448 449 450 451
          rename H_valid_task_parameters into TASK_PARAMS.
          unfold valid_sporadic_taskset, is_valid_sporadic_task in *; des.
          exploit (TASK_PARAMS tsk);
            [by rewrite mem_rcons in_cons eq_refl orTb | intro PARAMS; des].
          induction k.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
452
          {
453 454 455
            intros _; rewrite add0n -addn1 subh1;
              first by rewrite -addnBA // subnn addn0 /= leqnn.
            by apply PARAMS.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
456
          }
457 458 459 460 461 462 463 464 465 466 467 468 469 470
          {
            intros LT.
            specialize (IHk (ltnW LT)).
            apply leq_ltn_trans with (n := f k);
              last by apply bertogna_fp_comp_f_increases, ltnW.
            rewrite -addn1 -addnA [1 + _]addnC addnA -addnBA // subnn addn0.
            rewrite -(ltn_add2r 1) in IHk.
            rewrite subh1 in IHk; last first.
            {
              apply leq_trans with (n := task_cost tsk); last by apply leq_addl.
              by apply PARAMS.
            }
            by rewrite -addnBA // subnn addn0 addn1 ltnS in IHk.
          }  
Felipe Cerqueira's avatar
Felipe Cerqueira committed
471 472 473 474 475 476 477 478
        Qed.

      End DerivingContradiction.
      
      (* Using the lemmas above, we prove the convergence of the iteration after max_steps. *)
      Lemma per_task_rta_converges:
        f (max_steps tsk) = f (max_steps tsk).+1.
      Proof.
479 480 481 482
        rename H_no_larger_than_deadline into LE,
               H_valid_task_parameters into TASK_PARAMS.
        unfold valid_sporadic_taskset, is_valid_sporadic_task in *; des.
       
Felipe Cerqueira's avatar
Felipe Cerqueira committed
483
        (* Either f converges by the deadline or not. *)
484
        destruct ([exists k in 'I_(max_steps tsk).+1, f k == f k.+1]) eqn:EX.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
485 486 487
        {
          move: EX => /exists_inP EX; destruct EX as [k _ ITERk].
          apply bertogna_fp_comp_f_converges_early.
488
          by exists k; split; [by rewrite -ltnS; apply ltn_ord | by apply/eqP].
Felipe Cerqueira's avatar
Felipe Cerqueira committed
489 490 491 492 493 494 495
        }

        (* If not, then we reach a contradiction *)
        apply negbT in EX; rewrite negb_exists_in in EX.
        move: EX => /forall_inP EX.
        rewrite leqNgt in LE; move: LE => /negP LE.
        exfalso; apply LE.
496 497 498 499 500 501 502 503 504 505 506 507
        have TOOMUCH := bertogna_fp_comp_rt_grows_too_much _ (max_steps tsk).
        exploit TOOMUCH; [| by apply leqnn |].
        {
          intros k LEk; rewrite -ltnS in LEk.
          by exploit (EX (Ordinal LEk)); [by done | intro DIFF].
        }
        unfold max_steps at 1.
        exploit (TASK_PARAMS tsk);
          [by rewrite mem_rcons in_cons eq_refl orTb | intro PARAMS; des].
        rewrite -addnA [1 + _]addnC addnA -addnBA // subnn addn0.
        rewrite subh1; last by apply PARAMS2.
        by rewrite -addnBA // subnn addn0.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
508 509 510
      Qed.
      
    End Convergence.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
511
    
Felipe Cerqueira's avatar
Felipe Cerqueira committed
512
    Section MainProof.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
513

Felipe Cerqueira's avatar
Felipe Cerqueira committed
514
      (* Consider a task set ts. *)
515
      Variable ts: taskset_of sporadic_task.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
516
      
517
      (* Assume that higher_priority is a total strict order (<).
518 519 520
         TODO: it doesn't have to be total over the universe of tasks,
         but only within the task set. However, to weaken this hypothesis
         we need to re-prove some lemmas from ssreflect. *)
521
      Hypothesis H_irreflexive: irreflexive higher_priority.
522 523 524
      Hypothesis H_transitive: transitive higher_priority.
      Hypothesis H_unique_priorities: antisymmetric higher_priority.
      Hypothesis H_total: total higher_priority.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
525 526 527 528 529

      (* Assume the task set has no duplicates, ... *)
      Hypothesis H_ts_is_a_set: uniq ts.

      (* ...all tasks have valid parameters, ... *)
530 531
      Hypothesis H_valid_task_parameters:
        valid_sporadic_taskset task_cost task_period task_deadline ts.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
532 533 534 535 536 537

      (* ...restricted deadlines, ...*)
      Hypothesis H_restricted_deadlines:
        forall tsk, tsk \in ts -> task_deadline tsk <= task_period tsk.

      (* ...and tasks are ordered by increasing priorities. *)
538
      Hypothesis H_sorted_ts: sorted higher_priority ts.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
539 540 541 542 543 544 545 546

      (* Next, consider any arrival sequence such that...*)
      Context {arr_seq: arrival_sequence Job}.

     (* ...all jobs come from task set ts, ...*)
      Hypothesis H_all_jobs_from_taskset:
        forall (j: JobIn arr_seq), job_task j \in ts.
      
547
      (* ...jobs have valid parameters,...*)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
548 549
      Hypothesis H_valid_job_parameters:
        forall (j: JobIn arr_seq),
550
          valid_sporadic_job task_cost task_deadline job_cost job_deadline job_task j.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
551 552
      
      (* ... and satisfy the sporadic task model.*)
553 554
      Hypothesis H_sporadic_tasks:
        sporadic_task_model task_period arr_seq job_task.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
555
      
556
      (* Then, consider any platform with at least one CPU such that...*)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
557 558 559 560 561 562 563 564 565
      Variable sched: schedule num_cpus arr_seq.
      Hypothesis H_at_least_one_cpu :
        num_cpus > 0.

      (* ...jobs only execute after they arrived and no longer
         than their execution costs,... *)
      Hypothesis H_jobs_must_arrive_to_execute:
        jobs_must_arrive_to_execute sched.
      Hypothesis H_completed_jobs_dont_execute:
566
        completed_jobs_dont_execute job_cost sched.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
567

Felipe Cerqueira's avatar
Felipe Cerqueira committed
568
      (* ...and do not execute in parallel (required by the workload bound). *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
569 570 571
      Hypothesis H_no_parallelism:
        jobs_dont_execute_in_parallel sched.

572 573 574 575
      (* Assume that the scheduler is work-conserving and enforces the FP policy. *)
      Hypothesis H_work_conserving: work_conserving job_cost sched.
      Hypothesis H_enforces_FP_policy:
        enforces_FP_policy job_cost job_task sched higher_priority.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
576

577
      Let no_deadline_missed_by_task (tsk: sporadic_task) :=
578
        task_misses_no_deadline job_cost job_deadline job_task sched tsk.
579
      Let no_deadline_missed_by_job :=
580
        job_misses_no_deadline job_cost job_deadline sched.
581
      Let response_time_bounded_by (tsk: sporadic_task) :=
582
        is_response_time_bound_of_task job_cost job_task tsk sched.
583
          
Felipe Cerqueira's avatar
Felipe Cerqueira committed
584 585
      (* In the following theorem, we prove that any response-time bound contained
         in fp_claimed_bounds is safe. The proof follows by induction on the task set:
Felipe Cerqueira's avatar
Felipe Cerqueira committed
586

587 588
           Induction hypothesis: assume all higher-priority tasks have safe response-time bounds.
           Inductive step: we prove that the response-time bound of the current task is safe.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
589 590

         Note that the inductive step is a direct application of the main Theorem from
Felipe Cerqueira's avatar
Felipe Cerqueira committed
591 592 593 594
         bertogna_fp_theory.v. *)
      Theorem fp_analysis_yields_response_time_bounds :
        forall tsk R,
          (tsk, R) \In fp_claimed_bounds ts ->
595
          response_time_bounded_by tsk R.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
596
      Proof.
597 598
        rename H_valid_job_parameters into JOBPARAMS, H_valid_task_parameters into TASKPARAMS.
        unfold valid_sporadic_taskset in *.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
599
        intros tsk R MATCH.
600 601
        assert (SOME: exists hp_bounds, fp_claimed_bounds ts = Some hp_bounds /\
                                        (tsk, R) \in hp_bounds).
Felipe Cerqueira's avatar
Felipe Cerqueira committed
602 603 604
        {
          destruct (fp_claimed_bounds ts); last by done.
          by exists l; split.
605 606 607 608 609 610 611 612 613 614 615 616 617 618
        } clear MATCH; des; rename SOME0 into IN.

        have UNZIP := fp_claimed_bounds_unzip ts hp_bounds SOME.
        
        set elem := (tsk,R).
        move: IN => /(nthP elem) [idx LTidx EQ].
        set NTH := fun k => nth elem hp_bounds k.
        set TASK := fun k => (NTH k).1.
        set RESP := fun k => (NTH k).2.
        cut (response_time_bounded_by (TASK idx) (RESP idx));
          first by unfold TASK, RESP, NTH; rewrite EQ.
        clear EQ.

        assert (PAIR: forall idx, (TASK idx, RESP idx) = NTH idx).
Felipe Cerqueira's avatar
Felipe Cerqueira committed
619
        {
620
          by intros i; unfold TASK, RESP; destruct (NTH i).
Felipe Cerqueira's avatar
Felipe Cerqueira committed
621
        }
622 623

        assert (SUBST: forall i, i < size hp_bounds -> TASK i = nth tsk ts i).
Felipe Cerqueira's avatar
Felipe Cerqueira committed
624
        {
625 626

          by intros i LTi; rewrite /TASK /NTH -UNZIP (nth_map elem) //.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
627
        }
Felipe Cerqueira's avatar
Felipe Cerqueira committed
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 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
        assert (SIZE: size hp_bounds = size ts).
        {
          by rewrite -UNZIP size_map.
        }

        induction idx as [idx IH'] using strong_ind.

        assert (IH: forall tsk_hp R_hp, (tsk_hp, R_hp) \in take idx hp_bounds -> response_time_bounded_by tsk_hp R_hp).
        {
          intros tsk_hp R_hp INhp.
          move: INhp => /(nthP elem) [k LTk EQ].
          rewrite size_take LTidx in LTk.
          rewrite nth_take in EQ; last by done.
          cut (response_time_bounded_by (TASK k) (RESP k));
            first by unfold TASK, RESP, NTH; rewrite EQ.
          by apply IH'; try (by done); apply (ltn_trans LTk).
        } clear IH'.

        unfold response_time_bounded_by in *.

        exploit (fp_claimed_bounds_rcons (take idx ts) (take idx hp_bounds) (TASK idx) (TASK idx) (RESP idx)).
        {
          by rewrite PAIR SUBST // -2?take_nth -?SIZE // (fp_claimed_bounds_take _ hp_bounds).
        }
        intros [_ [_ [REC DL]]].

        apply bertogna_cirinei_response_time_bound_fp with
              (task_cost0 := task_cost) (task_period0 := task_period)
              (task_deadline0 := task_deadline) (job_deadline0 := job_deadline) (tsk0 := (TASK idx))
              (job_task0 := job_task) (ts0 := ts) (hp_bounds0 := take idx hp_bounds)
              (higher_eq_priority := higher_priority); try (by done).
        {
          cut (NTH idx \in hp_bounds); [intros IN | by apply mem_nth].
          by rewrite -UNZIP; apply/mapP; exists (TASK idx, RESP idx); rewrite PAIR.
        }
        {
          unfold unzip1 in *; rewrite map_take UNZIP SUBST //.
          by apply fp_claimed_bounds_interf with (hp_bounds := hp_bounds); rewrite -?SIZE.
        }
        {
          intros hp_tsk R_hp IN; apply mem_take in IN.
          by apply fp_claimed_bounds_ge_cost with (ts' := ts) (rt_bounds := hp_bounds).
        }
        {
          intros hp_tsk R_hp IN; apply mem_take in IN.
          by apply fp_claimed_bounds_le_deadline with (ts' := ts) (rt_bounds := hp_bounds).
        }
        {
          rewrite REC per_task_rta_fold.
          apply per_task_rta_converges with (ts_hp := take idx ts);
            [by apply fp_claimed_bounds_take; try (by apply ltnW) | | by rewrite -REC ].
          rewrite SUBST // -take_nth -?SIZE //.
          by intros i IN; eapply TASKPARAMS, mem_take, IN.
        }
      Qed.
      
Felipe Cerqueira's avatar
Felipe Cerqueira committed
685
      (* Therefore, if the schedulability test suceeds, ...*)
Felipe Cerqueira's avatar
Cleanup  
Felipe Cerqueira committed
686
      Hypothesis H_test_succeeds: fp_schedulable ts.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
687
      
Felipe Cerqueira's avatar
Felipe Cerqueira committed
688
      (*..., no task misses its deadline. *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
689
      Theorem taskset_schedulable_by_fp_rta :
Felipe Cerqueira's avatar
Cleanup  
Felipe Cerqueira committed
690
        forall tsk, tsk \in ts -> no_deadline_missed_by_task tsk.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
691
      Proof.
Felipe Cerqueira's avatar
Cleanup  
Felipe Cerqueira committed
692
        unfold no_deadline_missed_by_task, task_misses_no_deadline,
Felipe Cerqueira's avatar
Felipe Cerqueira committed
693
               job_misses_no_deadline, completed,
694 695
               fp_schedulable, valid_sporadic_job in *.
        rename H_valid_job_parameters into JOBPARAMS.
696
        move => tsk INtsk j JOBtsk.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
697
        have RLIST := (fp_analysis_yields_response_time_bounds).
698
        have UNZIP := (fp_claimed_bounds_unzip ts).
Felipe Cerqueira's avatar
Felipe Cerqueira committed
699
        have DL := (fp_claimed_bounds_le_deadline ts).
Felipe Cerqueira's avatar
Felipe Cerqueira committed
700

Felipe Cerqueira's avatar
Felipe Cerqueira committed
701
        destruct (fp_claimed_bounds ts) as [rt_bounds |]; last by ins.
702 703 704 705 706 707
        feed (UNZIP rt_bounds); first by done.
        assert (EX: exists R, (tsk, R) \in rt_bounds).
        {
          rewrite -UNZIP in INtsk; move: INtsk => /mapP EX.
          by destruct EX as [p]; destruct p as [tsk' R]; simpl in *; subst tsk'; exists R.
        } des.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
708
        exploit (RLIST tsk R); [by ins | by apply JOBtsk | intro COMPLETED].
Felipe Cerqueira's avatar
Felipe Cerqueira committed
709
        exploit (DL rt_bounds tsk R); [by ins | by ins | clear DL; intro DL].
710
        rewrite eqn_leq; apply/andP; split; first by apply cumulative_service_le_job_cost.
711
        apply leq_trans with (n := service sched j (job_arrival j + R)); last first.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
712
        {
713
          unfold valid_sporadic_taskset, is_valid_sporadic_task in *.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
714
          apply extend_sum; rewrite // leq_add2l.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
715 716 717 718 719 720 721
          specialize (JOBPARAMS j); des; rewrite JOBPARAMS1.
          by rewrite JOBtsk.
        }
        rewrite leq_eqVlt; apply/orP; left; rewrite eq_sym.
        by apply COMPLETED.
      Qed.

Felipe Cerqueira's avatar
Cleanup  
Felipe Cerqueira committed
722
      (* For completeness, since all jobs of the arrival sequence
Felipe Cerqueira's avatar
Felipe Cerqueira committed
723 724
         are spawned by the task set, we also conclude that no job in
         the schedule misses its deadline. *)
Felipe Cerqueira's avatar
Cleanup  
Felipe Cerqueira committed
725 726 727 728 729 730
      Theorem jobs_schedulable_by_fp_rta :
        forall (j: JobIn arr_seq), no_deadline_missed_by_job j.
      Proof.
        intros j.
        have SCHED := taskset_schedulable_by_fp_rta.
        unfold no_deadline_missed_by_task, task_misses_no_deadline in *.
731
        apply SCHED with (tsk := job_task j); last by done.
Felipe Cerqueira's avatar
Cleanup  
Felipe Cerqueira committed
732 733 734
        by apply H_all_jobs_from_taskset.
      Qed.
      
Felipe Cerqueira's avatar
Felipe Cerqueira committed
735
    End MainProof.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
736

737 738 739
  End Analysis.

End ResponseTimeIterationFP.