From 7d19d4487fd2feb9df71c288798991482af99811 Mon Sep 17 00:00:00 2001
From: grammers <samkar-4@student.ltu.se>
Date: Fri, 17 Jan 2020 14:41:16 +0100
Subject: [PATCH] done

---
 examples/f401_minimal.rs   | 12 ++++++
 my_EXAM.md                 | 11 +++++
 runner/Cargo.lock          | 63 +++++++++++++++++++++++++++++
 runner/Cargo.toml          |  1 +
 runner/src/bin/generate.rs | 73 +++++++++++++++++++++++++++------
 runner/src/common.rs       | 83 +++++++++++++++++++++++++++++++++-----
 6 files changed, 221 insertions(+), 22 deletions(-)
 create mode 100644 my_EXAM.md

diff --git a/examples/f401_minimal.rs b/examples/f401_minimal.rs
index ee90da6..ce9569b 100644
--- a/examples/f401_minimal.rs
+++ b/examples/f401_minimal.rs
@@ -12,9 +12,21 @@ use cortex_m::asm;
 use cortex_m_rt::entry;
 use cortex_m_semihosting::hprintln;
 
+static mut arr: [usize; 10] = [0; 10];
+
 #[entry]
 fn main() -> ! {
     hprintln!("Hello, world!").unwrap();
+	
+	for i in 0 .. 10 {
+		hprintln!("{}",i).unwrap();	
+		unsafe {
+			arr[i] = i;
+		}
+	}
+	unsafe {
+		hprintln!("{:?}", arr).unwrap();
+	}
 
     loop {
         asm::nop();
diff --git a/my_EXAM.md b/my_EXAM.md
new file mode 100644
index 0000000..a31057d
--- /dev/null
+++ b/my_EXAM.md
@@ -0,0 +1,11 @@
+# Samuel Karlsson (samkar-4)
+
+To run the test analyse `cd runner` and `cargo run --bin generate`.
+
+In generate.rs is to bool defined, "debug" and "advance".
+"debug" toggle extra prints useful numbers to understand the result of the report.
+"advanced" toggle between advance and approximative method for interference calculation.
+
+If tests are desirer on different values, change and or expand the t1,t2,t3 at beginning of generate.rs.
+
+Main implementation can be found in common.rs.
diff --git a/runner/Cargo.lock b/runner/Cargo.lock
index 3c5fa29..bec7539 100644
--- a/runner/Cargo.lock
+++ b/runner/Cargo.lock
@@ -206,6 +206,11 @@ dependencies = [
  "miniz_oxide 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "fuchsia-cprng"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
 [[package]]
 name = "gimli"
 version = "0.19.0"
@@ -302,6 +307,14 @@ dependencies = [
  "take_mut 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "libmath"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "rand 0.3.23 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "libusb1-sys"
 version = "0.3.4"
@@ -464,6 +477,48 @@ dependencies = [
  "proc-macro2 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "rand"
+version = "0.3.23"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "rand"
+version = "0.4.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "rand_core 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "rdrand"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "redox_syscall"
 version = "0.1.56"
@@ -498,6 +553,7 @@ name = "runner"
 version = "0.1.0"
 dependencies = [
  "failure 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libmath 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "probe-rs 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
@@ -726,6 +782,7 @@ dependencies = [
 "checksum fallible-iterator 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"
 "checksum filetime 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "1ff6d4dab0aa0c8e6346d46052e93b13a16cf847b54ed357087c35011048cc7d"
 "checksum flate2 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)" = "6bd6d6f4752952feb71363cffc9ebac9411b75b87c6ab6058c40c8900cf43c0f"
+"checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
 "checksum gimli 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)" = "162d18ae5f2e3b90a993d202f1ba17a5633c2484426f8bcae201f86194bacd00"
 "checksum goblin 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3081214398d39e4bd7f2c1975f0488ed04614ffdd976c6fc7a0708278552c0da"
 "checksum hermit-abi 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eff2656d88f158ce120947499e971d743c05dbcbed62e5bd2f38f1698bbc3772"
@@ -738,6 +795,7 @@ dependencies = [
 "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
 "checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558"
 "checksum libflate 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)" = "d9135df43b1f5d0e333385cb6e7897ecd1a43d7d11b91ac003f4d2c2d2401fdd"
+"checksum libmath 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dfd3416934a853ae80d5c3b006f632dfcbaf320300c5167e88a469e9ac214502"
 "checksum libusb1-sys 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "32d20b6c0f54a2419b6081866f7821b89eb289ca248f92e049190a10f1fada44"
 "checksum linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ae91b68aebc4ddb91978b11a1b02ddd8602a05ec19002801c5666000e05e0f83"
 "checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
@@ -757,6 +815,11 @@ dependencies = [
 "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
 "checksum quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1"
 "checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
+"checksum rand 0.3.23 (registry+https://github.com/rust-lang/crates.io-index)" = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c"
+"checksum rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
+"checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
+"checksum rand_core 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
+"checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
 "checksum redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)" = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
 "checksum rental 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8545debe98b2b139fb04cad8618b530e9b07c152d99a5de83c860b877d67847f"
 "checksum rental-impl 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "475e68978dc5b743f2f40d8e0a8fdc83f1c5e78cbf4b8fa5e74e73beebc340de"
diff --git a/runner/Cargo.toml b/runner/Cargo.toml
index 778577e..3fa8003 100644
--- a/runner/Cargo.toml
+++ b/runner/Cargo.toml
@@ -10,6 +10,7 @@ edition = "2018"
 probe-rs = { version = "0.3.0" }
 #ktest = { version = "0.1.0" }
 failure = "0.1.6"
+libmath = "0.2.1"
 
 [lib]
 name = "runner"
diff --git a/runner/src/bin/generate.rs b/runner/src/bin/generate.rs
index ea5f1f7..8079f66 100644
--- a/runner/src/bin/generate.rs
+++ b/runner/src/bin/generate.rs
@@ -64,27 +64,74 @@ fn main() {
         },
     };
 
-    // builds a vector of tasks t1, t2, t3
-    let tasks: Tasks = vec![t1, t2, t3];
+	let debug = false;
+	let advance = false;
 
-    println!("tasks {:?}", &tasks);
-   // println!("tot_util {}", tot_util(&tasks));
+    // builds a vector of tasks t1, t2, t3
+	// in falling prio order IMPORTENT The order are used
+    let mut tasks: Tasks = vec![t1, t2, t3];
+	tasks.sort_by(|a, b| (a.prio).partial_cmp(&b.prio).unwrap());
+	tasks.reverse();
 
     let (ip, tr) = pre_analysis(&tasks);
-    println!("ip: {:?}", ip);
-    println!("tr: {:?}", tr);
-
+	let dl = dead_line(&tasks);
 	let c = c_times(&tasks);
-	println!("C(t): {:?}", c);
-
 	let (l, l_tot) = load(&tasks, &c);
-	println!("L(t): {:?}",l);
-	println!("L(T): {:?}",l_tot);
+	let b = block(&tasks, &ip, &tr);
+	let i = interferens(&tasks, &c, advance);
+	let r = response(&c, &b, &i);
+
+	if debug{
+    	println!("Debug data");
+		println!("tasks {:?}", &tasks);
+    	println!("ip: {:?}", ip);
+    	println!("tr: {:?}", tr);
+		println!("DeadLine: {:?}", dl);
+		println!("C(t): {:?}", c);
+		println!("L(t): {:?}",l);
+		println!("L(T): {:?}",l_tot);
+		println!("B(t): {:?}", b);
+		println!("I(t): {:?}", i);
+		println!("R(t): {:?}", r);
+		println!("");
+	}
+
+	analys_data(&tasks, l, &r, c, b, i, l_tot);
+	test(dl, l_tot, &r);
+}
 
-	let b = block(&tasks, ip, tr);
-	println!("B(t): {:?}", b);
+// WARNIGN assums 0 overhead
+fn test(dl: data, l: f32,  r: &data) {
+	println!("TEST of real time critarias");
+	if l <= 1.0 {
+		println!("Load test SUCSES");
+	} else {
+		println!("Load test FAILD");
+	}
+
+	for (id, time) in dl {
+		if time >= *r.get(&id).unwrap() {
+			println!("Task {} respod in time: SUCSES", &id);
+		} else {
+			println!("Task {} respod to late: FAIL", &id);
+		}
+	}
+	println!("");
 }
 
+fn analys_data(tasks: &Tasks, l: L, r: &data, c: data, b: data, i: data, load: f32){
+	println!("The data for analyse");
+	println!("Total load {}%", load);
+	println!("ID | L | R | C | B | I | PRIO ");
+	println!("------------------------------");
+	for t in tasks {
+		println!("{} | {} | {} | {} | {} | {} | {}", 
+		t.id, l.get(&t.id).unwrap(), r.get(&t.id).unwrap(), c.get(&t.id).unwrap(), b.get(&t.id).unwrap(), i.get(&t.id).unwrap(), t.prio);
+	println!("------------------------------");
+
+	}
+	println!("");
+}
 /*
 TODO
 for eatch task
diff --git a/runner/src/common.rs b/runner/src/common.rs
index 085f127..b9ce898 100644
--- a/runner/src/common.rs
+++ b/runner/src/common.rs
@@ -1,4 +1,5 @@
-use std::collections::{HashMap, HashSet};
+use std::collections::HashMap;
+use math::round::ceil;
 
 // common data structures
 
@@ -32,9 +33,8 @@ pub type IdPrio = HashMap<String, u8>;
 // id_task, {id_res, longest}
 pub type TaskResources = HashMap<String, Vec<HashMap<String, u32>>>;
 
-pub type C = HashMap<String, u32>;
+pub type data = HashMap<String, u32>;
 pub type L = HashMap<String, f32>;
-pub type B = HashMap<String, u32>;
 
 // Derives the above maps from a set of tasks
 pub fn pre_analysis(tasks: &Tasks) -> (IdPrio, TaskResources) {
@@ -69,8 +69,8 @@ fn update_tr(s: String, trace: &Trace, trmap: &mut TaskResources) {
 		let mut hm = HashMap::new();
 		let time = trace.end - trace.start;
 		let mut found = false;
-		for mut t in seen {
-			if let Some(mut exist) = t.get_mut(&trace.id) {
+		for t in seen {
+			if let Some(exist) = t.get_mut(&trace.id) {
 				if *exist < time {
 					*exist = time;
 				}
@@ -91,7 +91,7 @@ fn update_tr(s: String, trace: &Trace, trmap: &mut TaskResources) {
     }
 }
 
-pub fn c_times(tasks: &Tasks) -> C {
+pub fn c_times(tasks: &Tasks) -> data {
 	let mut c = HashMap::new();
 	for t in tasks {
 		c.insert(t.id.clone(), t.trace.end - t.trace.start);
@@ -99,7 +99,7 @@ pub fn c_times(tasks: &Tasks) -> C {
 	c
 }
 
-pub fn load(tasks: &Tasks, c: &C) -> (L, f32){
+pub fn load(tasks: &Tasks, c: &data) -> (L, f32){
 	let mut l = HashMap::new();
 	let mut tot = 0.0;
 	for t in tasks {
@@ -111,7 +111,7 @@ pub fn load(tasks: &Tasks, c: &C) -> (L, f32){
 	(l, tot)
 }
 
-pub fn block(tasks: &Tasks, ip:  IdPrio, tr: TaskResources) -> B {
+pub fn block(tasks: &Tasks, ip:  &IdPrio, tr: &TaskResources) -> data {
 	let mut b = HashMap::new();
 	for t in tasks {
 		let mut block_time = 0;
@@ -122,7 +122,7 @@ pub fn block(tasks: &Tasks, ip:  IdPrio, tr: TaskResources) -> B {
 				for (id, _) in vectorn {
 					// iter this, all res
 					
-					for (tr_id, tr_has) in &tr {
+					for (tr_id, tr_has) in tr {
 						if ip.get(tr_id).unwrap() < &t.prio {
 						
 							for bl in tr_has {
@@ -146,3 +146,68 @@ pub fn block(tasks: &Tasks, ip:  IdPrio, tr: TaskResources) -> B {
 	}
 	b
 }
+
+
+pub fn interferens(tasks: &Tasks, c: &data, advance: bool) -> data {
+	let mut i = HashMap::new();
+	let a = inter_arival(&tasks);
+	i.insert(tasks[0].id.clone(), 0);
+	if !advance {
+		for index in 1..tasks.len() {
+			let mut sum = 0;
+			for index2 in 0..index {
+				let id = &tasks[index2].id;
+				sum += block_eq(*a.get(&tasks[index].id).unwrap(), 
+					*a.get(id).unwrap(), 
+					*c.get(id).unwrap());
+			}
+			i.insert(tasks[index].id.clone(), sum);
+		} 
+	
+	} else {
+		for index in 1..tasks.len() {
+			let mut sum = 0;
+			let mut block = *c.get(&tasks[index].id).unwrap();
+			while sum < block {
+				sum = block;
+				let mut part = 0;
+				for index2 in 0..index {
+					let id = &tasks[index2].id;
+					part += block_eq(block, *a.get(id).unwrap(), *c.get(id).unwrap());
+				}
+				block = part;
+			}
+			i.insert(tasks[index].id.clone(), sum);
+		}
+	}
+	i
+}
+
+
+fn block_eq(block: u32, a: u32, c: u32) -> u32 {
+	return ceil(block as f64 / a as f64, 0) as u32 * c;	
+}
+
+fn inter_arival(tasks: &Tasks) -> data {
+	let mut a = HashMap::new();
+		for t in tasks {
+			a.insert(t.id.clone(), t.inter_arrival.clone());
+		}
+	a
+}
+
+pub fn response(c: &data, b: &data, i: &data) -> data {
+	let mut r = HashMap::new();
+	for (id, tim) in c {
+		r.insert(id.clone(), tim + b.get(id).unwrap() + i.get(id).unwrap());
+	}
+	r
+}
+
+pub fn dead_line(tasks: &Tasks) -> data {
+	let mut dl = HashMap::new();
+	for t in tasks {
+		dl.insert(t.id.clone(), t.deadline);
+	}
+	dl
+}
-- 
GitLab