bertogna_fp_comp.v 29.2 KB
Newer Older
Felipe Cerqueira's avatar
Felipe Cerqueira committed
1
Require Import rt.util.all.
2
Require Import rt.analysis.global.basic.bertogna_fp_theory.
3
From mathcomp Require Import ssreflect ssrbool ssrfun eqtype ssrnat seq fintype bigop div path.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
4 5 6

Module ResponseTimeIterationFP.

7
  Import ResponseTimeAnalysisFP.
8

Felipe Cerqueira's avatar
Felipe Cerqueira committed
9 10
  (* In this section, we define the algorithm of Bertogna and Cirinei's
     response-time analysis for FP scheduling. *)
11 12 13
  Section Analysis.
    
    Context {sporadic_task: eqType}.
14 15 16
    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
17

18
    (* As input for each iteration of the algorithm, we consider pairs
Felipe Cerqueira's avatar
Felipe Cerqueira committed
19
       of tasks and computed response-time bounds. *)
20
    Let task_with_response_time := (sporadic_task * time)%type.
21
    
Felipe Cerqueira's avatar
Felipe Cerqueira committed
22
    Context {Job: eqType}.
23
    Variable job_arrival: Job -> time.
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
Felipe Cerqueira's avatar
Felipe Cerqueira committed
52
                                                R_prev t)
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
    Definition fp_claimed_bounds (ts: seq sporadic_task) :=
Felipe Cerqueira's avatar
Felipe Cerqueira committed
80
      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). *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
84
    Definition fp_schedulable (ts: seq 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
Felipe Cerqueira's avatar
Felipe Cerqueira committed
262
                     (per_task_rta tsk rt_bounds (max_steps tsk))) 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

Felipe Cerqueira's avatar
Felipe Cerqueira committed
277
      (* Assume that the task set is sorted by unique priorities, ... *)
278
      Hypothesis H_task_set_is_sorted: sorted higher_priority ts.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
279 280
      Hypothesis H_task_set_has_unique_priorities:
        FP_is_antisymmetric_over_task_set higher_priority ts.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
281

Felipe Cerqueira's avatar
Felipe Cerqueira committed
282 283
      (* ...the priority order is transitive, ...*)
      Hypothesis H_priority_transitive: FP_is_transitive higher_priority.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
284 285 286 287
      
      (* ... and that the response-time analysis succeeds. *)
      Variable hp_bounds: seq task_with_response_time.
      Variable R: time.
288 289 290 291 292
      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
293 294 295 296 297

      (* We prove that higher-priority tasks have smaller index. *)
      Lemma fp_claimed_bounds_hp_tasks_have_smaller_index :
        forall hp_idx idx,
          hp_idx < size ts ->
298
          idx < size ts ->
Felipe Cerqueira's avatar
Felipe Cerqueira committed
299 300 301
          hp_idx != idx ->
          higher_priority (TASK hp_idx) (TASK idx) ->
          hp_idx < idx.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
302
      Proof.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
303 304 305 306 307
        unfold TASK; clear TASK.
        rename ts into ts'; destruct ts' as [ts UNIQ]; simpl in *.
        intros hp_idx idx LThp LT NEQ HP.
        rewrite ltn_neqAle; apply/andP; split; first by done.
        by apply sorted_rel_implies_le_idx with (leT := higher_priority) (s := ts) (x0 := elem).
Felipe Cerqueira's avatar
Felipe Cerqueira committed
308
      Qed.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
309

Felipe Cerqueira's avatar
Felipe Cerqueira committed
310 311
    End HighPriorityTasks.

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

Felipe Cerqueira's avatar
Felipe Cerqueira committed
315 316
      (* Consider any list of higher-priority tasks. *)
      Variable ts_hp: list sporadic_task.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
317

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

322
      (* Consider any task tsk to be analyzed, ... *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
323 324
      Variable tsk: sporadic_task.

325 326 327 328
      (* ... 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
329 330 331
      (* To simplify, let f denote the fixed-point iteration. *)
      Let f := per_task_rta tsk rt_bounds.

Felipe Cerqueira's avatar
Felipe Cerqueira committed
332
      (* Assume that f (max_steps tsk) is no larger than the deadline. *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
333 334 335 336 337 338 339 340
      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,
341
               H_valid_task_parameters into VALID.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
342 343 344 345
        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.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
346 347 348 349 350 351
        rewrite big_seq_cond.
        rewrite [\sum_(_ <- _ | true) _]big_seq_cond.
        rewrite leq_add2l leq_div2r //.
        apply leq_sum; move => i /andP [IN _].
        destruct i as [i R].
        have GE_COST := fp_claimed_bounds_ge_cost ts_hp rt_bounds i R SOME IN.
352
        have UNZIP := fp_claimed_bounds_unzip ts_hp rt_bounds SOME.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
353
        assert (IN': i \in ts_hp).
354 355 356 357
        {
          by rewrite -UNZIP; apply/mapP; exists (i,R).
        }
        unfold interference_bound_generic; simpl.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
358 359 360
        rewrite leq_min; apply/andP; split.
        {
          apply leq_trans with (n := W task_cost task_period i R x1); first by apply geq_minl.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
361 362
          exploit (VALID i); [by rewrite mem_rcons in_cons IN' orbT | ins; des].
          by apply W_monotonic; try (by done); apply GE_COST.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
363 364 365 366 367 368 369
        }
        {
          apply leq_trans with (n := x1 - task_cost tsk + 1); first by apply geq_minr.
          by rewrite leq_add2r leq_sub2r //.
        }
      Qed.

370
      (* If the iteration converged at an earlier step, it remains as a fixed point. *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
371
      Lemma bertogna_fp_comp_f_converges_early :
372
        (exists k, k <= max_steps tsk /\ f k = f k.+1) ->
Felipe Cerqueira's avatar
Felipe Cerqueira committed
373 374
        f (max_steps tsk) = f (max_steps tsk).+1.
      Proof.
375
        by intros EX; des; apply iter_fix with (k := k).
Felipe Cerqueira's avatar
Felipe Cerqueira committed
376 377 378 379 380 381 382 383
      Qed.

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

        (* Assume instead that the iteration continued to diverge. *)
        Hypothesis H_keeps_diverging:
          forall k,
384
            k <= max_steps tsk -> f k != f k.+1.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
385 386 387 388

        (* By monotonicity, it follows that the value always increases. *)
        Lemma bertogna_fp_comp_f_increases :
          forall k,
389
            k <= max_steps tsk ->
Felipe Cerqueira's avatar
Felipe Cerqueira committed
390 391 392 393 394 395 396 397
            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
398
        (* In the end, the response-time bound must exceed the deadline. Contradiction! *)
399 400 401 402
        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
403
        Proof.
404
          have INC := bertogna_fp_comp_f_increases.
405 406 407 408 409
          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
410
          {
411 412 413
            intros _; rewrite add0n -addn1 subh1;
              first by rewrite -addnBA // subnn addn0 /= leqnn.
            by apply PARAMS.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
414
          }
415 416 417
          {
            intros LT.
            specialize (IHk (ltnW LT)).
418
            apply leq_ltn_trans with (n := f k); last by apply INC, ltnW.
419 420
            rewrite -addn1 -addnA [1 + _]addnC addnA -addnBA // subnn addn0.
            rewrite -(ltn_add2r 1) in IHk.
421 422 423
            rewrite subh1 in IHk;
              last by apply leq_trans with (n := task_cost tsk);
                [by apply PARAMS | by apply leq_addl].
424 425
            by rewrite -addnBA // subnn addn0 addn1 ltnS in IHk.
          }  
Felipe Cerqueira's avatar
Felipe Cerqueira committed
426 427 428 429 430 431 432 433
        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.
434 435
        have TOOMUCH := bertogna_fp_comp_rt_grows_too_much.
        have INC := bertogna_fp_comp_f_increases.
436 437 438 439
        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
440
        (* Either f converges by the deadline or not. *)
441
        destruct ([exists k in 'I_(max_steps tsk).+1, f k == f k.+1]) eqn:EX.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
442 443 444
        {
          move: EX => /exists_inP EX; destruct EX as [k _ ITERk].
          apply bertogna_fp_comp_f_converges_early.
445
          by exists k; split; [by rewrite -ltnS; apply ltn_ord | by apply/eqP].
Felipe Cerqueira's avatar
Felipe Cerqueira committed
446 447 448 449 450 451 452
        }

        (* 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.
453 454

        assert (DIFF: forall k : nat, k <= max_steps tsk -> f k != f k.+1).
455 456
        {
          intros k LEk; rewrite -ltnS in LEk.
457 458 459
          by exploit (EX (Ordinal LEk)); [by done | intro DIFF; apply DIFF].
        }          
        exploit TOOMUCH; [by apply DIFF | by apply leq_addr |].
460 461 462
        exploit (TASK_PARAMS tsk);
          [by rewrite mem_rcons in_cons eq_refl orTb | intro PARAMS; des].
        rewrite subh1; last by apply PARAMS2.
463 464 465
        rewrite -addnBA // subnn addn0 subn1 prednK //.
        intros LT; apply (leq_ltn_trans LT).
        by rewrite /max_steps [_ - _ + 1]addn1; apply INC, leq_addr.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
466 467 468
      Qed.
      
    End Convergence.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
469
    
Felipe Cerqueira's avatar
Felipe Cerqueira committed
470
    Section MainProof.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
471

Felipe Cerqueira's avatar
Felipe Cerqueira committed
472
      (* Consider a task set ts. *)
473
      Variable ts: taskset_of sporadic_task.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
474
      
Felipe Cerqueira's avatar
Felipe Cerqueira committed
475
      (* Assume that all tasks have valid parameters, ... *)
476 477
      Hypothesis H_valid_task_parameters:
        valid_sporadic_taskset task_cost task_period task_deadline ts.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
478

Felipe Cerqueira's avatar
Felipe Cerqueira committed
479
      (* ...constrained deadlines.*)
480
      Hypothesis H_constrained_deadlines:
Felipe Cerqueira's avatar
Felipe Cerqueira committed
481 482
        forall tsk, tsk \in ts -> task_deadline tsk <= task_period tsk.

Felipe Cerqueira's avatar
Felipe Cerqueira committed
483 484 485 486 487 488 489 490
      (* Assume that the task set is totally ordered by unique priorities,
         and that the priority order is transitive. *)
      Hypothesis H_task_set_is_sorted: sorted higher_priority ts.
      Hypothesis H_task_set_has_unique_priorities:
        FP_is_antisymmetric_over_task_set higher_priority ts.
      Hypothesis H_priority_is_total:
        FP_is_total_over_task_set higher_priority ts.
      Hypothesis H_priority_transitive: FP_is_transitive higher_priority.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
491 492 493 494 495 496

      (* 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:
497
        forall j, arrives_in arr_seq j -> job_task j \in ts.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
498
      
499
      (* ...jobs have valid parameters,...*)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
500
      Hypothesis H_valid_job_parameters:
501 502
        forall j,
          arrives_in arr_seq j ->
503
          valid_sporadic_job task_cost task_deadline job_cost job_deadline job_task j.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
504 505
      
      (* ... and satisfy the sporadic task model.*)
506
      Hypothesis H_sporadic_tasks:
507
        sporadic_task_model task_period job_arrival job_task arr_seq.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
508

509 510
      (* Then, consider any schedule of this arrival sequence such that... *)
      Variable sched: schedule Job num_cpus.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
511
      Hypothesis H_at_least_one_cpu: num_cpus > 0.
512 513 514
      Hypothesis H_jobs_come_from_arrival_sequence:
        jobs_come_from_arrival_sequence sched arr_seq.
      
Felipe Cerqueira's avatar
Felipe Cerqueira committed
515
      (* ...jobs only execute after they arrived and no longer
516
         than their execution costs. *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
517
      Hypothesis H_jobs_must_arrive_to_execute:
518
        jobs_must_arrive_to_execute job_arrival sched.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
519
      Hypothesis H_completed_jobs_dont_execute:
520
        completed_jobs_dont_execute job_cost sched.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
521

522 523
      (* Also assume that jobs are sequential (as required by the workload bound). *)
      Hypothesis H_sequential_jobs: sequential_jobs sched.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
524

525
      (* Assume that the scheduler is work-conserving and respects the FP policy. *)
526
      Hypothesis H_work_conserving: work_conserving job_arrival job_cost arr_seq sched.
527
      Hypothesis H_respects_FP_policy:
528
        respects_FP_policy job_arrival job_cost job_task arr_seq sched higher_priority.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
529

530
      Let no_deadline_missed_by_task (tsk: sporadic_task) :=
531
        task_misses_no_deadline job_arrival job_cost job_deadline job_task arr_seq sched tsk.
532
      Let no_deadline_missed_by_job :=
533
        job_misses_no_deadline job_arrival job_cost job_deadline sched.
534
      Let response_time_bounded_by (tsk: sporadic_task) :=
535
        is_response_time_bound_of_task job_arrival job_cost job_task arr_seq sched tsk.
536
          
Felipe Cerqueira's avatar
Felipe Cerqueira committed
537 538
      (* 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
539

540 541
           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
542 543

         Note that the inductive step is a direct application of the main Theorem from
Felipe Cerqueira's avatar
Felipe Cerqueira committed
544 545 546 547
         bertogna_fp_theory.v. *)
      Theorem fp_analysis_yields_response_time_bounds :
        forall tsk R,
          (tsk, R) \In fp_claimed_bounds ts ->
548
          response_time_bounded_by tsk R.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
549
      Proof.
550 551
        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
552
        intros tsk R MATCH.
553 554
        assert (SOME: exists hp_bounds, fp_claimed_bounds ts = Some hp_bounds /\
                                        (tsk, R) \in hp_bounds).
Felipe Cerqueira's avatar
Felipe Cerqueira committed
555 556 557
        {
          destruct (fp_claimed_bounds ts); last by done.
          by exists l; split.
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
        } 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).
          by intros i; unfold TASK, RESP; destruct (NTH i).

        assert (SUBST: forall i, i < size hp_bounds -> TASK i = nth tsk ts i).
          by intros i LTi; rewrite /TASK /NTH -UNZIP (nth_map elem) //.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
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
        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).
        {
607
          cut (NTH idx \in hp_bounds = true); [intros IN | by apply mem_nth].
Felipe Cerqueira's avatar
Felipe Cerqueira committed
608
          by rewrite set_mem -UNZIP; apply/mapP; exists (TASK idx, RESP idx); rewrite PAIR.
609 610
        }
        {
Felipe Cerqueira's avatar
Felipe Cerqueira committed
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
          intros hp_tsk IN INTERF.
          exists (RESP (index hp_tsk ts)).
          move: (IN) => INDEX; apply nth_index with (x0 := tsk) in INDEX.
          rewrite -{1}[hp_tsk]INDEX -SUBST; last by rewrite SIZE index_mem.
          assert (UNIQ: uniq hp_bounds).
          {
            apply map_uniq with (f := fst); unfold unzip1 in *; rewrite UNZIP.
            by destruct ts.
          }
          rewrite -filter_idx_lt_take //.
          {
            rewrite PAIR mem_filter; apply/andP; split;
              last by apply mem_nth; rewrite SIZE index_mem.
            {
              rewrite /NTH index_uniq; [| by rewrite SIZE index_mem | by done ].
              {
                move: INTERF => /andP [HP NEQ].
                apply fp_claimed_bounds_hp_tasks_have_smaller_index with
                  (ts := ts) (elem := tsk) (hp_bounds := hp_bounds);
                  try (by done);
                  [by rewrite index_mem | by rewrite -SIZE | | by rewrite INDEX -SUBST].
                apply/eqP; intro BUG; subst idx.
                rewrite SUBST -{1}INDEX in NEQ;
                  first by rewrite eq_refl in NEQ.
                by rewrite SIZE index_mem INDEX.
              }
            }
          }
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
        }
        {
          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
657
      (* Therefore, if the schedulability test suceeds, ...*)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
658
      Hypothesis H_test_succeeds: fp_schedulable ts.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
659
      
Felipe Cerqueira's avatar
Felipe Cerqueira committed
660
      (*..., no task misses its deadline. *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
661
      Theorem taskset_schedulable_by_fp_rta :
Felipe Cerqueira's avatar
Felipe Cerqueira committed
662
        forall tsk, tsk \in ts -> no_deadline_missed_by_task tsk.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
663
      Proof.
664 665 666 667
        have RLIST := (fp_analysis_yields_response_time_bounds).
        have UNZIP := (fp_claimed_bounds_unzip ts).
        have DL := (fp_claimed_bounds_le_deadline ts).

Felipe Cerqueira's avatar
Felipe Cerqueira committed
668
        unfold no_deadline_missed_by_task, task_misses_no_deadline,
Felipe Cerqueira's avatar
Felipe Cerqueira committed
669
               job_misses_no_deadline, completed,
670 671
               fp_schedulable, valid_sporadic_job in *.
        rename H_valid_job_parameters into JOBPARAMS.
672
        move => tsk INtsk j ARRj JOBtsk.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
673
        destruct (fp_claimed_bounds ts) as [rt_bounds |]; last by ins.
674 675 676
        feed (UNZIP rt_bounds); first by done.
        assert (EX: exists R, (tsk, R) \in rt_bounds).
        {
Felipe Cerqueira's avatar
Felipe Cerqueira committed
677
          rewrite set_mem -UNZIP in INtsk; move: INtsk => /mapP EX.
678 679
          by destruct EX as [p]; destruct p as [tsk' R]; simpl in *; subst tsk'; exists R.
        } des.
680
        exploit (RLIST tsk R EX j ARRj); [by done | intro COMPLETED].
Felipe Cerqueira's avatar
Felipe Cerqueira committed
681
        exploit (DL rt_bounds tsk R); [by ins | by ins | clear DL; intro DL].
682
        rewrite eqn_leq; apply/andP; split; first by apply cumulative_service_le_job_cost.
683
        apply leq_trans with (n := service sched j (job_arrival j + R)); last first.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
684
        {
685
          unfold valid_sporadic_taskset, is_valid_sporadic_task in *.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
686
          apply extend_sum; rewrite // leq_add2l.
687
          specialize (JOBPARAMS j ARRj); des; rewrite JOBPARAMS1.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
688 689 690 691 692 693
          by rewrite JOBtsk.
        }
        rewrite leq_eqVlt; apply/orP; left; rewrite eq_sym.
        by apply COMPLETED.
      Qed.

Felipe Cerqueira's avatar
Felipe Cerqueira committed
694
      (* For completeness, since all jobs of the arrival sequence
Felipe Cerqueira's avatar
Felipe Cerqueira committed
695 696
         are spawned by the task set, we also conclude that no job in
         the schedule misses its deadline. *)
Felipe Cerqueira's avatar
Felipe Cerqueira committed
697
      Theorem jobs_schedulable_by_fp_rta :
698
        forall j, arrives_in arr_seq j -> no_deadline_missed_by_job j.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
699
      Proof.
700
        intros j ARRj.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
701 702
        have SCHED := taskset_schedulable_by_fp_rta.
        unfold no_deadline_missed_by_task, task_misses_no_deadline in *.
703
        apply SCHED with (tsk := job_task j); try (by done).
Felipe Cerqueira's avatar
Felipe Cerqueira committed
704 705 706
        by apply H_all_jobs_from_taskset.
      Qed.
      
Felipe Cerqueira's avatar
Felipe Cerqueira committed
707
    End MainProof.
Felipe Cerqueira's avatar
Felipe Cerqueira committed
708

709 710 711
  End Analysis.

End ResponseTimeIterationFP.