From 7d3e7f5519bef884be65fe6c9625065258a51259 Mon Sep 17 00:00:00 2001
From: rubenasplund <ruben.asplund@hotmail.com>
Date: Wed, 13 Jan 2021 18:21:01 +0100
Subject: [PATCH] fixed blocking function

---
 srp_analysis/src/analysis.rs | 57 ++++++++++++++----------------------
 srp_analysis/src/main.rs     | 29 ++++++++++++------
 2 files changed, 42 insertions(+), 44 deletions(-)

diff --git a/srp_analysis/src/analysis.rs b/srp_analysis/src/analysis.rs
index 43d79aa..0377e45 100644
--- a/srp_analysis/src/analysis.rs
+++ b/srp_analysis/src/analysis.rs
@@ -104,51 +104,38 @@ pub fn preemptions(task: &Task, tasks: &Tasks, exact: bool) -> Result<u32, Strin
 }
 
 pub fn blocking(task: &Task, tasks: &Tasks) -> u32 {
-    let (_ip, tr) = pre_analysis(&tasks);
-    // Checks if the Task is claming a resource
-    if !tr.contains_key(&task.id) {
-        return 0;
-    }
+    let (ip, tr) = pre_analysis(&tasks);
+    let mut max_block = 0;
     // Find lower prio tasks that holds a resource
-    let mut lower_prio_tasks = HashSet::new();
     for t in tasks {
-        if t.prio < task.prio && tr.contains_key(&t.id) {
-            lower_prio_tasks.insert(t.id.to_string());
-        }
-    }
-    if lower_prio_tasks.len() == 0 {
-        return 0;
-    }
-    let resources = &tr[&task.id];
-    // println!("{}", &task.id);
-    // println!("{:?}", tr);
-    // println!("{:?}", resources);
-    // Checking every resource it holds
-    let mut max_block = 0;
-    // Iterate through current task resources
-    for r1 in resources {
-        // Iterate through lower prio tasks
-        for t_id in &lower_prio_tasks {
-            let lower_prio_task_resources = &tr[t_id];
-            // Iterate through lower prio task resources
-            for r2 in lower_prio_task_resources {
-                // When current task use the same resource as a task
-                if r1 == r2 {
-                    // Takes forward the blocking task
-                    for t in tasks {
-                        if &t.id == t_id {
-                            // Finds the longest blocking time
-                            max_block = longest_blocking(&t.trace, r1);
-                            
-                        }
+        if (t.prio < task.prio) && (tr.contains_key(&t.id)) && (t.id != task.id) {
+            // All resources t are using
+            let current_task_resources = &tr[&t.id];
+
+            let mut blocking_resources = HashSet::new();
+            for r in current_task_resources {
+                if ip.contains_key(r) {
+                    let prio_ceiling = &ip[r];
+                    // Is the resource prio ceiling higher than task prio OR task is using the resource
+                    if (prio_ceiling > &task.prio) || tr[&task.id].contains(r) {
+                        blocking_resources.insert(r);
                     }
+
                 }
             }
+            for r in blocking_resources {
+                // Finds the longest blocking time with resource r
+                let block = longest_blocking(&t.trace, r);
+                if max_block < block {
+                    max_block = block;
+                } 
+            }
         }
     }
     return max_block;
 }
 
+// Finds the longest blocking time recursive
 fn longest_blocking(trace: &Trace, r: &str) -> u32 {
     let mut max_block = 0;
     if trace.id == r {
diff --git a/srp_analysis/src/main.rs b/srp_analysis/src/main.rs
index cf4fafa..302baeb 100644
--- a/srp_analysis/src/main.rs
+++ b/srp_analysis/src/main.rs
@@ -5,7 +5,11 @@ use common::*;
 
 fn main() {
 
-    let tasks = taskset_1();
+    let tasks = taskset_2();
+
+    // let (ip, tr) = pre_analysis(&tasks);
+    // println!("{:?}", ip);
+    // println!("{:?}", tr);
 
     println!("CPU load {}", load_factor(&tasks));
     let analysis = analysis(&tasks, true);
@@ -14,13 +18,11 @@ fn main() {
                 a.task.id, a.response_time, a.wcet, a.blocking_time, a.preemption_time);
     }
     if schedulable_check(analysis) {
-        println!("The taskset is schedulable!")
+        println!("OK! The taskset is schedulable!")
     } else {
-        println!("The taskset is NOT schedulable!")
+        println!("ERROR! The taskset is NOT schedulable!")
     }
     
-    
-
 }
 
 fn taskset_1() -> Tasks {
@@ -99,12 +101,21 @@ fn taskset_2() -> Tasks {
             id: "T1".to_string(),
             start: 0,
             end: 15,
-            inner: vec![Trace {
-                id: "R1".to_string(),
+            inner: vec![
+            Trace {
+                id: "R3".to_string(),
                 start: 2,
                 end: 8,
                 inner: vec![],
-            }],
+            },
+            Trace {
+                id: "R1".to_string(),
+                start: 9,
+                end: 13,
+                inner: vec![],
+            },
+            ],
+            
         },
     };
     
@@ -149,7 +160,7 @@ fn taskset_2() -> Tasks {
             start: 0,
             end: 20,
             inner: vec![Trace {
-                id: "R2".to_string(),
+                id: "R3".to_string(),
                 start: 8,
                 end: 20,
                 inner: vec![],
-- 
GitLab