From ceacb8de224d93ad5dc981f9766cdd3036f80eba Mon Sep 17 00:00:00 2001
From: rubenasplund <ruben.asplund@hotmail.com>
Date: Thu, 7 Jan 2021 00:36:42 +0100
Subject: [PATCH] Added functions preemtions(), wcet(), response_time() and
 print_analysis()

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

diff --git a/srp_analysis/src/main.rs b/srp_analysis/src/main.rs
index 0d4adc3..2ca26d0 100644
--- a/srp_analysis/src/main.rs
+++ b/srp_analysis/src/main.rs
@@ -74,27 +74,77 @@ fn main() {
     let (ip, tr) = pre_analysis(&tasks);
     println!("ip: {:?}", ip);
     println!("tr: {:?}", tr);
-    for t in &tasks {
-        blocking(t, &tasks);
-    }
+
+    print_analysis(&tasks, &ip, &tr);
+
+    
     
 }
 
+fn print_analysis(tasks: &Tasks, ip: &IdPrio, tr: &TaskResources) {
+    for t in tasks {
+        let b = blocking(t, &tasks, &ip, &tr);
+        let p = preemptions(t, &tasks);
+        let r = response_time(t, &tasks, &ip, &tr);
+        let c = wcet(t);
+        let tasks_analysis = vec![t.id.to_string(), r.to_string(), c.to_string(), b.to_string(), p.to_string()];
+        println!("{:?}", tasks_analysis)
+    }
+}
+
+
+fn response_time(task: &Task, tasks: &Tasks, ip: &IdPrio, tr: &TaskResources) -> u32 {
+    let blocking = blocking(task, &tasks, &ip, &tr);
+    let wcet = wcet(task);
+    let preemption = preemptions(task, &tasks);
+    return blocking + wcet + preemption;
+}
+
+fn wcet(task: &Task) -> u32 {
+    let c = task.trace.end.wrapping_sub(task.trace.start);
+    return c
+}
+
 fn load_factor(tasks: &Tasks) -> f32{
     let mut l_tot: f32 = 0.0;
     for t in tasks {
-        let c = t.trace.end.wrapping_sub(t.trace.start);
+        let c = wcet(t);
         let a = t.inter_arrival;
         l_tot = l_tot +  (c as f32)/(a as f32);
     }
     return l_tot;
 }
 
-fn blocking(task: &Task, tasks: &Tasks) -> u32 {
-    let (ip, tr) = pre_analysis(&tasks);
-    // If the Task is claming a resource
+fn preemptions(task: &Task, tasks: &Tasks) -> u32 {
+    // Find higher or same task prio
+    let mut higher_prio_tasks = HashSet::new();
+    for t in tasks {
+        if t.id == task.id {
+            continue;
+        }
+        if t.prio >= task.prio {
+            higher_prio_tasks.insert(t.id.to_string());
+        }
+    }
+    // Calculate preemptions time (I(t))
+    let mut p_time = 0;
+    for t_id in higher_prio_tasks {
+        for t in tasks {
+            if t_id == t.id {
+                let inter_arrival = t.inter_arrival;
+                let busy_period = t.deadline;
+                p_time = p_time + wcet(t)*(busy_period / inter_arrival);
+            }
+        }
+    }
+    // println!("{}", p_time);
+    return p_time;
+}
+
+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");
+        // println!("0");
         return 0;
     }
     // Find lower prio tasks
@@ -104,7 +154,6 @@ fn blocking(task: &Task, tasks: &Tasks) -> u32 {
             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) {
@@ -116,10 +165,9 @@ fn blocking(task: &Task, tasks: &Tasks) -> u32 {
     }
     // println!("{:?}", lower_prio_tasks);
     if lower_prio_tasks.len() == 0 {
-        println!("0");
+        // println!("0");
         return 0;
     }
-
     /* Finding longest blocking */
     let resources = &tr[&task.id];
     // Checking every resource it holds
@@ -127,13 +175,13 @@ fn blocking(task: &Task, tasks: &Tasks) -> u32 {
     let mut current_block = 0;
     // Iterate through current task resources
     for r1 in resources {
-        println!("Resource 1: {}, Resources 1 {:?}", r1, resources);
+        // println!("Resource 1: {}, Resources 1 {:?}", r1, 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 {
-                println!("Resource 2: {}, Resources 2 {:?}", r2, lower_prio_task_resources);
+                // println!("Resource 2: {}, Resources 2 {:?}", r2, lower_prio_task_resources);
                 // When current task use the same resource as a task
                 if r1 == r2 {
                     // Takes forward the blocking task
@@ -146,8 +194,8 @@ fn blocking(task: &Task, tasks: &Tasks) -> u32 {
                                     if max_block < current_block {
                                         max_block = current_block;
                                     }
-                                    println!("1: start:{}, end:{}", inner.start, inner.end);
-                                    println!("For id {} and resource 2 {}", t_id, r2)
+                                    // println!("1: start:{}, end:{}", inner.start, inner.end);
+                                    // println!("For id {} and resource 2 {}", t_id, r2)
                                 }
                                 // Iterate through second layer
                                 for inner2 in &inner.inner {
@@ -156,8 +204,8 @@ fn blocking(task: &Task, tasks: &Tasks) -> u32 {
                                         if max_block < current_block {
                                             max_block = current_block;
                                         }
-                                        println!("2: start:{}, end:{}", inner2.start, inner2.end);
-                                        println!("For id {} and resource 2 {}", t_id, r2)
+                                        // println!("2: start:{}, end:{}", inner2.start, inner2.end);
+                                        // println!("For id {} and resource 2 {}", t_id, r2)
                                     }
                                 }
                             }
@@ -167,6 +215,6 @@ fn blocking(task: &Task, tasks: &Tasks) -> u32 {
             }
         }
     }
-    println!("{}", max_block);
+    // println!("{}", max_block);
     return max_block;
 }
\ No newline at end of file
-- 
GitLab