From 04f32dd54b98374da0df5e77bd20ec70cc28eb82 Mon Sep 17 00:00:00 2001
From: rubenasplund <ruben.asplund@hotmail.com>
Date: Tue, 12 Jan 2021 18:21:08 +0100
Subject: [PATCH] Added a recursive function to blocking

---
 srp_analysis/src/main.rs | 80 +++++++++++++++++++++++++++++++++-------
 1 file changed, 66 insertions(+), 14 deletions(-)

diff --git a/srp_analysis/src/main.rs b/srp_analysis/src/main.rs
index ee2e2a9..ff78ef1 100644
--- a/srp_analysis/src/main.rs
+++ b/srp_analysis/src/main.rs
@@ -77,8 +77,6 @@ fn main() {
 
     print_analysis(&tasks, &ip, &tr);
 
-    let test = (10.0 as f32/30.0 as f32).ceil();
-    println!("Test {}", test);
 }
 // Prints out vector with [task id, response time, wcet time, blocking time, preemption time]
 fn print_analysis(tasks: &Tasks, ip: &IdPrio, tr: &TaskResources) {
@@ -139,7 +137,7 @@ fn preemptions_exact(task: &Task, tasks: &Tasks, ip: &IdPrio, tr: &TaskResources
             new_busy_period = new_busy_period + wcet(t)*(((busy_period as f32) / (t.inter_arrival as f32)).ceil() as u32);
         }
     }
-    println!("nb {}, b {}", new_busy_period, busy_period);
+    // println!("nb {}, b {}", new_busy_period, busy_period);
     if new_busy_period.wrapping_sub(base_case) == 0 {
         return Ok(0);
     } else if new_busy_period > task.deadline { 
@@ -162,30 +160,83 @@ fn preemptions(task: &Task, tasks: &Tasks, ip: &IdPrio, tr: &TaskResources, exac
 fn blocking(task: &Task, tasks: &Tasks, ip: &IdPrio, tr: &TaskResources) -> u32 {
     // Checks if the Task is claming a resource
     if !tr.contains_key(&task.id) {
-        // println!("0");
         return 0;
     }
-    // Find lower prio tasks
+    // Find lower prio tasks that holds a resource
     let mut lower_prio_tasks = HashSet::new();
     for t in tasks {
-        if t.prio < task.prio {
+        if t.prio < task.prio && tr.contains_key(&t.id) {
             lower_prio_tasks.insert(t.id.to_string());
         }
     }
-    // Do these task hold a resource, if not delete from list
-    for t in tasks {
-        if !lower_prio_tasks.contains(&t.id) {
-            continue;
+    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;
+    let mut current_block = 0;
+    // Iterate through current task resources
+    for r1 in resources {
+        // Iterate through lower prio tasks
+        for t_id in &lower_prio_tasks {
+            let mut 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 {
+                            max_block = longest_blocking(&t.trace, r1);
+                            
+                        }
+                    }
+                }
+            }
         }
-        if !tr.contains_key(&t.id) {
-            lower_prio_tasks.remove(&t.id);
+    }
+    return max_block;
+}
+
+fn longest_blocking(trace: &Trace, r: &str) -> u32 {
+    let mut max_block = 0;
+    if trace.id == r {
+        max_block = trace.end.wrapping_sub(trace.start);
+    }
+    if trace.inner.len() != 0 {
+        for inner in &trace.inner {
+            let block = longest_blocking(&inner, r);
+            if max_block < block {
+                max_block = block;
+            }
+        }
+    }
+    return max_block;
+}
+
+/*
+fn blocking(task: &Task, tasks: &Tasks, ip: &IdPrio, tr: &TaskResources) -> u32 {
+    // Checks if the Task is claming a resource
+    if !tr.contains_key(&task.id) {
+        return 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;
     }
-    /* Finding longest blocking */
     let resources = &tr[&task.id];
+    println!("{:?}", tr);
+    println!("{:?}", resources);
     // Checking every resource it holds
     let mut max_block = 0;
     let mut current_block = 0;
@@ -233,4 +284,5 @@ fn blocking(task: &Task, tasks: &Tasks, ip: &IdPrio, tr: &TaskResources) -> u32
         }
     }
     return max_block;
-}
\ No newline at end of file
+}
+*/
\ No newline at end of file
-- 
GitLab