From 6eafcf10e944fb5875c086631dde7fad6f0a7b3b Mon Sep 17 00:00:00 2001 From: Per Date: Wed, 4 Mar 2020 15:06:03 +0100 Subject: task_local and lock_free analysis (take 1) --- macros/Cargo.toml | 7 +- macros/src/custom_local.rs | 178 +++++++++++++++++++++++++++++++++++++++++++++ macros/src/lib.rs | 15 ++++ 3 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 macros/src/custom_local.rs (limited to 'macros') diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 610890b..74f5443 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -21,5 +21,10 @@ proc-macro = true proc-macro2 = "1" quote = "1" syn = "1" -rtic-syntax = { git = "https://github.com/rtic-rs/rtic-syntax", branch = "master", version = "0.4.0" } +#rtic-syntax = { git = "https://github.com/rtic-rs/rtic-syntax", branch = "master", version = "0.4.0" } + +[dependencies.rtic-syntax] +git = "https://github.com/rtic-rs/rtic-syntax.git" +branch = "task_local_experiment" +version = "0.4.1" diff --git a/macros/src/custom_local.rs b/macros/src/custom_local.rs new file mode 100644 index 0000000..3381790 --- /dev/null +++ b/macros/src/custom_local.rs @@ -0,0 +1,178 @@ +use syn::parse; +//use syn::Ident; +//use proc_macro2::{Ident, Span}; +use proc_macro2::Ident; +use rtfm_syntax::{ + analyze::{Analysis, Ownership}, + ast::App, +}; +use syn::Error; +// ast::{App, CustomArg}, + +type Idents<'a> = Vec<&'a Ident>; + +// Assign an `extern` interrupt to each priority level +pub fn app(app: &App, _analysis: &Analysis) -> parse::Result<()> { + // collect task local resources + let task_local: Idents = app + .resources + .iter() + .filter(|(_, r)| r.properties.task_local) + .map(|(i, _)| i) + .chain( + app.late_resources + .iter() + .filter(|(_, r)| r.properties.task_local) + .map(|(i, _)| i), + ) + .collect(); + + let lock_free: Idents = app + .resources + .iter() + .filter(|(_, r)| r.properties.lock_free) + .map(|(i, _)| i) + .chain( + app.late_resources + .iter() + .filter(|(_, r)| r.properties.lock_free) + .map(|(i, _)| i), + ) + .collect(); + + // collect all tasks into a vector + type Task = String; + + let all_tasks: Vec<(Task, Idents)> = app + .idles + .iter() + .map(|(core, ht)| { + ( + format!("Idle (core {})", core), + ht.args.resources.iter().map(|(v, _)| v).collect::>(), + ) + }) + .chain(app.software_tasks.iter().map(|(name, ht)| { + ( + name.to_string(), + ht.args.resources.iter().map(|(v, _)| v).collect::>(), + ) + })) + .chain(app.hardware_tasks.iter().map(|(name, ht)| { + ( + name.to_string(), + ht.args.resources.iter().map(|(v, _)| v).collect::>(), + ) + })) + .collect(); + + // check that task_local resources is only used once + let mut error = vec![]; + for task_local_id in task_local.iter() { + let mut used = vec![]; + for (task, tr) in all_tasks.iter() { + for r in tr { + if task_local_id == r { + used.push((task, r)); + } + } + } + if used.len() > 1 { + error.push(Error::new( + task_local_id.span(), + format!( + "task local resource {:?} is used by multiple tasks", + task_local_id.to_string() + ), + )); + + used.iter().for_each(|(task, resource)| { + error.push(Error::new( + resource.span(), + format!( + "task local resource {:?} is used by task {:?}", + resource.to_string(), + task + ), + )) + }); + } + } + + // filter out contended resources + let contended: Vec<(&Ident, &Ownership)> = _analysis + .ownerships + .iter() + .filter(|(_id, own)| match own { + Ownership::Contended { .. } => true, + _ => false, + }) + .collect(); + + // filter out lock_free contended resources + let lock_free_violation: Vec<&(&Ident, &Ownership)> = contended + .iter() + .filter(|(cont_id, _)| lock_free.iter().any(|lf_id| cont_id == lf_id)) + .collect(); + + // report contention error + lock_free_violation.iter().for_each(|(lf_err_id, _)| { + error.push(Error::new( + lf_err_id.span(), + format!( + "lock_free resource {:?} is contended by higher priority task", + lf_err_id.to_string() + ), + )) + }); + + // collect errors + if error.is_empty() { + Ok(()) + } else { + let mut err = error.iter().next().unwrap().clone(); + error.iter().for_each(|e| err.combine(e.clone())); + Err(err) + } + + // for tl in task_local { + // println!("tl {:?}", tl); + // // let mut first_use = None; + // for i in _analysis.ownerships.iter() { + // println!("\nown: {:?}", i); + // } + + // // println!("analysis {:?}", _analysis.locations); + + // for i in _analysis.locations.iter() { + // println!("\nloc: {:?}", i); + // } + + // ErrorMessage { + // // Span is implemented as an index into a thread-local interner to keep the + // // size small. It is not safe to access from a different thread. We want + // // errors to be Send and Sync to play nicely with the Failure crate, so pin + // // the span we're given to its original thread and assume it is + // // Span::call_site if accessed from any other thread. + // start_span: ThreadBound, + // end_span: ThreadBound, + // message: String, + // } + // (app.name, "here"); + // let span = app.name.span(); + // let start = span.start(); + // Err(vec![ErrorMessage { start_span: app.name.}]); + + // let mut task_locals = Vec::new(); + // println!("-- app:late_resources"); + + // println!( + // "task_locals {:?}", + // app.late_resources.filter(|r| r.task_local) + // ); + + // println!("-- resources"); + // for i in app.resources.iter() { + // println!("res: {:?}", i); + // } +} diff --git a/macros/src/lib.rs b/macros/src/lib.rs index e659559..2c81ede 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -10,6 +10,7 @@ use rtic_syntax::Settings; mod analyze; mod check; mod codegen; +mod custom_local; #[cfg(test)] mod tests; @@ -214,13 +215,27 @@ pub fn app(args: TokenStream, input: TokenStream) -> TokenStream { Ok(x) => x, }; + match custom_local::app(&app, &analysis) { + Err(e) => return e.to_compile_error().into(), + Ok(_) => {} + } + let extra = match check::app(&app, &analysis) { Err(e) => return e.to_compile_error().into(), Ok(x) => x, }; + // println!("extra {:?}", extra); + let analysis = analyze::app(analysis, &app); + // println!("after analysis, extra {:?}", extra); + + // match custom_local::app(&app, &analysis) { + // Err(e) => return e.to_compile_error().into(), + // Ok(_) => {} + // } + let ts = codegen::app(&app, &analysis, &extra); // Try to write the expanded code to disk -- cgit v1.2.3 From d4439fe73be1467e8881f7d11941b2768c37fc21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Tj=C3=A4der?= Date: Sat, 7 Mar 2020 23:39:20 +0100 Subject: Print module name and priority --- macros/src/custom_local.rs | 135 ++++++++++++++++++++------------------------- 1 file changed, 61 insertions(+), 74 deletions(-) (limited to 'macros') diff --git a/macros/src/custom_local.rs b/macros/src/custom_local.rs index 3381790..f220c3d 100644 --- a/macros/src/custom_local.rs +++ b/macros/src/custom_local.rs @@ -1,13 +1,11 @@ use syn::parse; -//use syn::Ident; -//use proc_macro2::{Ident, Span}; +use std::collections::HashMap; use proc_macro2::Ident; use rtfm_syntax::{ - analyze::{Analysis, Ownership}, + analyze::Analysis, ast::App, }; use syn::Error; -// ast::{App, CustomArg}, type Idents<'a> = Vec<&'a Ident>; @@ -42,26 +40,31 @@ pub fn app(app: &App, _analysis: &Analysis) -> parse::Result<()> { // collect all tasks into a vector type Task = String; + type Priority = u8; - let all_tasks: Vec<(Task, Idents)> = app + let all_tasks: Vec<(Task, Idents, Priority)> = app .idles .iter() .map(|(core, ht)| { ( format!("Idle (core {})", core), ht.args.resources.iter().map(|(v, _)| v).collect::>(), + 0 + ) }) .chain(app.software_tasks.iter().map(|(name, ht)| { ( name.to_string(), ht.args.resources.iter().map(|(v, _)| v).collect::>(), + ht.args.priority ) })) .chain(app.hardware_tasks.iter().map(|(name, ht)| { ( name.to_string(), ht.args.resources.iter().map(|(v, _)| v).collect::>(), + ht.args.priority ) })) .collect(); @@ -70,10 +73,10 @@ pub fn app(app: &App, _analysis: &Analysis) -> parse::Result<()> { let mut error = vec![]; for task_local_id in task_local.iter() { let mut used = vec![]; - for (task, tr) in all_tasks.iter() { + for (task, tr, priority) in all_tasks.iter() { for r in tr { if task_local_id == r { - used.push((task, r)); + used.push((task, r, priority)); } } } @@ -86,46 +89,71 @@ pub fn app(app: &App, _analysis: &Analysis) -> parse::Result<()> { ), )); - used.iter().for_each(|(task, resource)| { + used.iter().for_each(|(task, resource, priority)| { error.push(Error::new( resource.span(), format!( - "task local resource {:?} is used by task {:?}", + "task local resource {:?} is used by task {:?} with priority {:?}", resource.to_string(), - task + task, + priority ), )) }); } } - // filter out contended resources - let contended: Vec<(&Ident, &Ownership)> = _analysis - .ownerships - .iter() - .filter(|(_id, own)| match own { - Ownership::Contended { .. } => true, - _ => false, - }) - .collect(); + let mut lf_res_with_error = vec![]; + let mut lf_hash = HashMap::new(); - // filter out lock_free contended resources - let lock_free_violation: Vec<&(&Ident, &Ownership)> = contended - .iter() - .filter(|(cont_id, _)| lock_free.iter().any(|lf_id| cont_id == lf_id)) - .collect(); + for lf_res in lock_free.iter() { + for (task, tr, priority) in all_tasks.iter() { + for r in tr { + // Get all uses of resources annotated lock_free + if lf_res == r { + // HashMap returns the previous existing object if old.key == new.key + if let Some(lf_res) = lf_hash.insert(r.to_string(), (task, r, priority)) { + // Check if priority differ, if it does, append to + // list of resources which will be annotated with errors + if priority != lf_res.2 { + lf_res_with_error.push(lf_res.1); + lf_res_with_error.push(r); + } + // If the resource already violates lock free properties + if lf_res_with_error.contains(&r) { + lf_res_with_error.push(lf_res.1); + lf_res_with_error.push(r); + } + } + } + } + } + } - // report contention error - lock_free_violation.iter().for_each(|(lf_err_id, _)| { + // Add error message in the resource struct + for r in lock_free { + if lf_res_with_error.contains(&&r) { + error.push(Error::new( + r.span(), + format!( + "Lock free resource {:?} is used by tasks at different priorities", + r.to_string(), + ), + )); + } + } + + // Add error message for each use of the resource + for resource in lf_res_with_error.clone() { error.push(Error::new( - lf_err_id.span(), + resource.span(), format!( - "lock_free resource {:?} is contended by higher priority task", - lf_err_id.to_string() + "Resource {:?} is declared lock free but used by tasks at different priorities", + resource.to_string(), ), - )) - }); - + )); + } + // collect errors if error.is_empty() { Ok(()) @@ -134,45 +162,4 @@ pub fn app(app: &App, _analysis: &Analysis) -> parse::Result<()> { error.iter().for_each(|e| err.combine(e.clone())); Err(err) } - - // for tl in task_local { - // println!("tl {:?}", tl); - // // let mut first_use = None; - // for i in _analysis.ownerships.iter() { - // println!("\nown: {:?}", i); - // } - - // // println!("analysis {:?}", _analysis.locations); - - // for i in _analysis.locations.iter() { - // println!("\nloc: {:?}", i); - // } - - // ErrorMessage { - // // Span is implemented as an index into a thread-local interner to keep the - // // size small. It is not safe to access from a different thread. We want - // // errors to be Send and Sync to play nicely with the Failure crate, so pin - // // the span we're given to its original thread and assume it is - // // Span::call_site if accessed from any other thread. - // start_span: ThreadBound, - // end_span: ThreadBound, - // message: String, - // } - // (app.name, "here"); - // let span = app.name.span(); - // let start = span.start(); - // Err(vec![ErrorMessage { start_span: app.name.}]); - - // let mut task_locals = Vec::new(); - // println!("-- app:late_resources"); - - // println!( - // "task_locals {:?}", - // app.late_resources.filter(|r| r.task_local) - // ); - - // println!("-- resources"); - // for i in app.resources.iter() { - // println!("res: {:?}", i); - // } -} +} \ No newline at end of file -- cgit v1.2.3 From e2364aae3eebf3326534bd4818d0312a03817538 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Tj=C3=A4der?= Date: Tue, 8 Sep 2020 17:51:10 +0000 Subject: Updated examples and rtic-name --- macros/Cargo.toml | 8 +-- macros/src/custom_local.rs | 165 --------------------------------------------- macros/src/lib.rs | 15 ----- 3 files changed, 2 insertions(+), 186 deletions(-) delete mode 100644 macros/src/custom_local.rs (limited to 'macros') diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 74f5443..4fac48f 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -21,10 +21,6 @@ proc-macro = true proc-macro2 = "1" quote = "1" syn = "1" -#rtic-syntax = { git = "https://github.com/rtic-rs/rtic-syntax", branch = "master", version = "0.4.0" } - -[dependencies.rtic-syntax] -git = "https://github.com/rtic-rs/rtic-syntax.git" -branch = "task_local_experiment" -version = "0.4.1" +#rtic-syntax = { git = "https://github.com/rtic-rs/rtic-syntax", branch = "task_local_experiment", version = "0.4.1" } +rtic-syntax = { git = "https://github.com/AfoHT/rtic-syntax", branch = "task_local_experiment", version = "0.4.1" } diff --git a/macros/src/custom_local.rs b/macros/src/custom_local.rs deleted file mode 100644 index f220c3d..0000000 --- a/macros/src/custom_local.rs +++ /dev/null @@ -1,165 +0,0 @@ -use syn::parse; -use std::collections::HashMap; -use proc_macro2::Ident; -use rtfm_syntax::{ - analyze::Analysis, - ast::App, -}; -use syn::Error; - -type Idents<'a> = Vec<&'a Ident>; - -// Assign an `extern` interrupt to each priority level -pub fn app(app: &App, _analysis: &Analysis) -> parse::Result<()> { - // collect task local resources - let task_local: Idents = app - .resources - .iter() - .filter(|(_, r)| r.properties.task_local) - .map(|(i, _)| i) - .chain( - app.late_resources - .iter() - .filter(|(_, r)| r.properties.task_local) - .map(|(i, _)| i), - ) - .collect(); - - let lock_free: Idents = app - .resources - .iter() - .filter(|(_, r)| r.properties.lock_free) - .map(|(i, _)| i) - .chain( - app.late_resources - .iter() - .filter(|(_, r)| r.properties.lock_free) - .map(|(i, _)| i), - ) - .collect(); - - // collect all tasks into a vector - type Task = String; - type Priority = u8; - - let all_tasks: Vec<(Task, Idents, Priority)> = app - .idles - .iter() - .map(|(core, ht)| { - ( - format!("Idle (core {})", core), - ht.args.resources.iter().map(|(v, _)| v).collect::>(), - 0 - - ) - }) - .chain(app.software_tasks.iter().map(|(name, ht)| { - ( - name.to_string(), - ht.args.resources.iter().map(|(v, _)| v).collect::>(), - ht.args.priority - ) - })) - .chain(app.hardware_tasks.iter().map(|(name, ht)| { - ( - name.to_string(), - ht.args.resources.iter().map(|(v, _)| v).collect::>(), - ht.args.priority - ) - })) - .collect(); - - // check that task_local resources is only used once - let mut error = vec![]; - for task_local_id in task_local.iter() { - let mut used = vec![]; - for (task, tr, priority) in all_tasks.iter() { - for r in tr { - if task_local_id == r { - used.push((task, r, priority)); - } - } - } - if used.len() > 1 { - error.push(Error::new( - task_local_id.span(), - format!( - "task local resource {:?} is used by multiple tasks", - task_local_id.to_string() - ), - )); - - used.iter().for_each(|(task, resource, priority)| { - error.push(Error::new( - resource.span(), - format!( - "task local resource {:?} is used by task {:?} with priority {:?}", - resource.to_string(), - task, - priority - ), - )) - }); - } - } - - let mut lf_res_with_error = vec![]; - let mut lf_hash = HashMap::new(); - - for lf_res in lock_free.iter() { - for (task, tr, priority) in all_tasks.iter() { - for r in tr { - // Get all uses of resources annotated lock_free - if lf_res == r { - // HashMap returns the previous existing object if old.key == new.key - if let Some(lf_res) = lf_hash.insert(r.to_string(), (task, r, priority)) { - // Check if priority differ, if it does, append to - // list of resources which will be annotated with errors - if priority != lf_res.2 { - lf_res_with_error.push(lf_res.1); - lf_res_with_error.push(r); - } - // If the resource already violates lock free properties - if lf_res_with_error.contains(&r) { - lf_res_with_error.push(lf_res.1); - lf_res_with_error.push(r); - } - } - } - } - } - } - - // Add error message in the resource struct - for r in lock_free { - if lf_res_with_error.contains(&&r) { - error.push(Error::new( - r.span(), - format!( - "Lock free resource {:?} is used by tasks at different priorities", - r.to_string(), - ), - )); - } - } - - // Add error message for each use of the resource - for resource in lf_res_with_error.clone() { - error.push(Error::new( - resource.span(), - format!( - "Resource {:?} is declared lock free but used by tasks at different priorities", - resource.to_string(), - ), - )); - } - - // collect errors - if error.is_empty() { - Ok(()) - } else { - let mut err = error.iter().next().unwrap().clone(); - error.iter().for_each(|e| err.combine(e.clone())); - Err(err) - } -} \ No newline at end of file diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 2c81ede..e659559 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -10,7 +10,6 @@ use rtic_syntax::Settings; mod analyze; mod check; mod codegen; -mod custom_local; #[cfg(test)] mod tests; @@ -215,27 +214,13 @@ pub fn app(args: TokenStream, input: TokenStream) -> TokenStream { Ok(x) => x, }; - match custom_local::app(&app, &analysis) { - Err(e) => return e.to_compile_error().into(), - Ok(_) => {} - } - let extra = match check::app(&app, &analysis) { Err(e) => return e.to_compile_error().into(), Ok(x) => x, }; - // println!("extra {:?}", extra); - let analysis = analyze::app(analysis, &app); - // println!("after analysis, extra {:?}", extra); - - // match custom_local::app(&app, &analysis) { - // Err(e) => return e.to_compile_error().into(), - // Ok(_) => {} - // } - let ts = codegen::app(&app, &analysis, &extra); // Try to write the expanded code to disk -- cgit v1.2.3 From 6c1f4a7b5d30502e7d7d66e4a9235c7933cf825d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Tj=C3=A4der?= Date: Thu, 1 Oct 2020 17:15:51 +0000 Subject: Changed branch for rtic-syntax --- macros/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'macros') diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 4fac48f..a9f268f 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -19,8 +19,8 @@ proc-macro = true [dependencies] proc-macro2 = "1" +proc-macro-error = "1" quote = "1" syn = "1" -#rtic-syntax = { git = "https://github.com/rtic-rs/rtic-syntax", branch = "task_local_experiment", version = "0.4.1" } -rtic-syntax = { git = "https://github.com/AfoHT/rtic-syntax", branch = "task_local_experiment", version = "0.4.1" } +rtic-syntax = { git = "https://github.com/rtic-rs/rtic-syntax", branch = "master", version = "0.5.0-alpha.0" } -- cgit v1.2.3 From 37ee3a47afbbbf57751243d6d32aaac78073780c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Tj=C3=A4der?= Date: Wed, 14 Oct 2020 10:15:35 +0000 Subject: Create Enum containing all tasks --- macros/src/codegen.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'macros') diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs index f230d39..a44266a 100644 --- a/macros/src/codegen.rs +++ b/macros/src/codegen.rs @@ -126,6 +126,20 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { let user_code = app.user_code.clone(); let name = &app.name; let device = extra.device; + + // Get the list of all tasks + // Currently unused, might be useful + let task_list = analysis.tasks.clone(); + + let mut tasks = vec![]; + if !task_list.is_empty() { + tasks.push(quote!( + enum Tasks { + #(#task_list),* + } + )); + } + quote!( #(#user)* @@ -141,6 +155,9 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { #(#root_software_tasks)* + /// Unused + #(#tasks)* + /// Implementation details mod #name { /// Always include the device crate which contains the vector table -- cgit v1.2.3