diff --git a/srp_analysis/src/main.rs b/srp_analysis/src/main.rs index 4e528df95e3ba4b7e3378c6e435079df9f2feda8..0d4adc3e2b9dd24246a993320283db5ef000c02a 100644 --- a/srp_analysis/src/main.rs +++ b/srp_analysis/src/main.rs @@ -1,5 +1,6 @@ mod common; use common::*; +use std::collections::HashSet; fn main() { let t1 = Task { @@ -67,10 +68,105 @@ fn main() { // builds a vector of tasks t1, t2, t3 let tasks: Tasks = vec![t1, t2, t3]; - println!("tasks {:?}", &tasks); - // println!("tot_util {}", tot_util(&tasks)); + // println!("tasks {:?}", &tasks); + println!("tot_util {}", load_factor(&tasks)); let (ip, tr) = pre_analysis(&tasks); println!("ip: {:?}", ip); println!("tr: {:?}", tr); + for t in &tasks { + blocking(t, &tasks); + } + } + +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 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 + if !tr.contains_key(&task.id) { + println!("0"); + return 0; + } + // Find lower prio tasks + let mut lower_prio_tasks = HashSet::new(); + for t in tasks { + if t.prio < task.prio { + 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 !tr.contains_key(&t.id) { + lower_prio_tasks.remove(&t.id); + } + } + // println!("{:?}", lower_prio_tasks); + if lower_prio_tasks.len() == 0 { + println!("0"); + return 0; + } + + /* Finding longest blocking */ + let resources = &tr[&task.id]; + // Checking every resource it holds + let mut max_block = 0; + let mut current_block = 0; + // Iterate through current task resources + for r1 in 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); + // 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 { + // Iterate through first layer + for inner in &t.trace.inner { + if &inner.id == r2 { + current_block = inner.end.wrapping_sub(inner.start); + 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) + } + // Iterate through second layer + for inner2 in &inner.inner { + if &inner2.id == r2 { + current_block = inner2.end.wrapping_sub(inner2.start); + 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!("{}", max_block); + return max_block; +} \ No newline at end of file