From 93abc9976c3c74b9e02a4dbb91387f92099e0089 Mon Sep 17 00:00:00 2001 From: Aditya Kumar Date: Fri, 10 Jan 2025 15:05:56 +0000 Subject: [PATCH] Add more helpful error message if section size is larger than slab size --- core/cu29_derive/src/lib.rs | 14 ++++-- ...id_file_path.rs => invalid_config_path.rs} | 2 +- .../copper_runtime/invalid_config_path.stderr | 7 +++ .../copper_runtime/invalid_file_path.stderr | 7 --- .../copper_runtime/invalid_logging_config.rs | 6 +++ .../invalid_logging_config.stderr | 8 ++++ .../compile_fail/cu_msg/invalid_file_path.rs | 4 +- .../cu_msg/invalid_file_path.stderr | 13 +++--- .../cu_msg/invalid_logging_config.rs | 5 +++ .../cu_msg/invalid_logging_config.stderr | 8 ++++ .../tests/config/invalid_logging_config.ron | 26 +++++++++++ core/cu29_runtime/src/config.rs | 44 ++++++++++++++++++- 12 files changed, 122 insertions(+), 22 deletions(-) rename core/cu29_derive/tests/compile_fail/copper_runtime/{invalid_file_path.rs => invalid_config_path.rs} (60%) create mode 100644 core/cu29_derive/tests/compile_fail/copper_runtime/invalid_config_path.stderr delete mode 100644 core/cu29_derive/tests/compile_fail/copper_runtime/invalid_file_path.stderr create mode 100644 core/cu29_derive/tests/compile_fail/copper_runtime/invalid_logging_config.rs create mode 100644 core/cu29_derive/tests/compile_fail/copper_runtime/invalid_logging_config.stderr create mode 100644 core/cu29_derive/tests/compile_fail/cu_msg/invalid_logging_config.rs create mode 100644 core/cu29_derive/tests/compile_fail/cu_msg/invalid_logging_config.stderr create mode 100644 core/cu29_derive/tests/config/invalid_logging_config.ron diff --git a/core/cu29_derive/src/lib.rs b/core/cu29_derive/src/lib.rs index 0180b0295..bb30e5eb3 100644 --- a/core/cu29_derive/src/lib.rs +++ b/core/cu29_derive/src/lib.rs @@ -54,8 +54,9 @@ pub fn gen_cumsgs(config_path_lit: TokenStream) -> TokenStream { } #[cfg(feature = "macro_debug")] eprintln!("[gen culist support with {:?}]", config); - let Ok(cuconfig) = read_config(&config) else { - return return_error(format!("Failed to read the configuration file: {}", config)); + let cuconfig = match read_config(&config) { + Ok(cuconfig) => cuconfig, + Err(e) => return return_error(e.to_string()), }; let runtime_plan: CuExecutionLoop = match compute_runtime_plan(&cuconfig) { Ok(plan) => plan, @@ -271,11 +272,16 @@ pub fn copper_runtime(args: TokenStream, input: TokenStream) -> TokenStream { } }; - let Ok(copper_config) = read_config(&config_file) else { + if !std::path::Path::new(&config_full_path(&config_file)).exists() { return return_error(format!( - "Failed to read the configuration file: {}", + "The configuration file `{}` does not exist. Please provide a valid path.", config_file )); + } + + let copper_config = match read_config(&config_file) { + Ok(cuconfig) => cuconfig, + Err(e) => return return_error(e.to_string()), }; let copper_config_content = match read_to_string(config_full_path(config_file.as_str())) { Ok(ok) => ok, diff --git a/core/cu29_derive/tests/compile_fail/copper_runtime/invalid_file_path.rs b/core/cu29_derive/tests/compile_fail/copper_runtime/invalid_config_path.rs similarity index 60% rename from core/cu29_derive/tests/compile_fail/copper_runtime/invalid_file_path.rs rename to core/cu29_derive/tests/compile_fail/copper_runtime/invalid_config_path.rs index 9b5c46901..aef6b481b 100644 --- a/core/cu29_derive/tests/compile_fail/copper_runtime/invalid_file_path.rs +++ b/core/cu29_derive/tests/compile_fail/copper_runtime/invalid_config_path.rs @@ -1,6 +1,6 @@ use cu29_derive::copper_runtime; -#[copper_runtime(config = "/path/to/config.ron")] +#[copper_runtime(config = "path/to/config.ron")] struct MyApplicationStruct; fn main() {} \ No newline at end of file diff --git a/core/cu29_derive/tests/compile_fail/copper_runtime/invalid_config_path.stderr b/core/cu29_derive/tests/compile_fail/copper_runtime/invalid_config_path.stderr new file mode 100644 index 000000000..e998ef093 --- /dev/null +++ b/core/cu29_derive/tests/compile_fail/copper_runtime/invalid_config_path.stderr @@ -0,0 +1,7 @@ +error: The configuration file `path/to/config.ron` does not exist. Please provide a valid path. + --> tests/compile_fail/copper_runtime/invalid_config_path.rs:3:1 + | +3 | #[copper_runtime(config = "path/to/config.ron")] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the attribute macro `copper_runtime` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/core/cu29_derive/tests/compile_fail/copper_runtime/invalid_file_path.stderr b/core/cu29_derive/tests/compile_fail/copper_runtime/invalid_file_path.stderr deleted file mode 100644 index 16ddbdf7f..000000000 --- a/core/cu29_derive/tests/compile_fail/copper_runtime/invalid_file_path.stderr +++ /dev/null @@ -1,7 +0,0 @@ -error: Failed to read the configuration file: /path/to/config.ron - --> tests/compile_fail/copper_runtime/invalid_file_path.rs:3:1 - | -3 | #[copper_runtime(config = "/path/to/config.ron")] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = note: this error originates in the attribute macro `copper_runtime` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/core/cu29_derive/tests/compile_fail/copper_runtime/invalid_logging_config.rs b/core/cu29_derive/tests/compile_fail/copper_runtime/invalid_logging_config.rs new file mode 100644 index 000000000..7ab6522d1 --- /dev/null +++ b/core/cu29_derive/tests/compile_fail/copper_runtime/invalid_logging_config.rs @@ -0,0 +1,6 @@ +use cu29_derive::copper_runtime; + +#[copper_runtime(config = "config/invalid_logging_config.ron")] +struct MyApplicationStruct; + +fn main() {} \ No newline at end of file diff --git a/core/cu29_derive/tests/compile_fail/copper_runtime/invalid_logging_config.stderr b/core/cu29_derive/tests/compile_fail/copper_runtime/invalid_logging_config.stderr new file mode 100644 index 000000000..09f7de5cf --- /dev/null +++ b/core/cu29_derive/tests/compile_fail/copper_runtime/invalid_logging_config.stderr @@ -0,0 +1,8 @@ +error: Section size (2 MiB) cannot be larger than slab size (1 MiB). Adjust the parameters accordingly. + context:None + --> tests/compile_fail/copper_runtime/invalid_logging_config.rs:3:1 + | +3 | #[copper_runtime(config = "config/invalid_logging_config.ron")] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the attribute macro `copper_runtime` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/core/cu29_derive/tests/compile_fail/cu_msg/invalid_file_path.rs b/core/cu29_derive/tests/compile_fail/cu_msg/invalid_file_path.rs index 6b9bf294a..fffadaf6b 100644 --- a/core/cu29_derive/tests/compile_fail/cu_msg/invalid_file_path.rs +++ b/core/cu29_derive/tests/compile_fail/cu_msg/invalid_file_path.rs @@ -1,5 +1,5 @@ -use cu29_derive::gen_cumsg; +use cu29_derive::gen_cumsgs; -gen_cumsg!("invalid/path/to/config.ron"); +gen_cumsgs!("invalid/path/to/config.ron"); fn main() {} \ No newline at end of file diff --git a/core/cu29_derive/tests/compile_fail/cu_msg/invalid_file_path.stderr b/core/cu29_derive/tests/compile_fail/cu_msg/invalid_file_path.stderr index 8ea5391a1..d0302b34f 100644 --- a/core/cu29_derive/tests/compile_fail/cu_msg/invalid_file_path.stderr +++ b/core/cu29_derive/tests/compile_fail/cu_msg/invalid_file_path.stderr @@ -1,8 +1,7 @@ -error[E0432]: unresolved import `cu29_derive::gen_cumsg` - --> tests/compile_fail/cu_msg/invalid_file_path.rs:1:5 +error: The configuration file `invalid/path/to/config.ron` does not exist. Please provide a valid path. + --> tests/compile_fail/cu_msg/invalid_file_path.rs:3:1 | -1 | use cu29_derive::gen_cumsg; - | ^^^^^^^^^^^^^--------- - | | | - | | help: a similar name exists in the module: `gen_cumsgs` - | no `gen_cumsg` in the root +3 | gen_cumsgs!("invalid/path/to/config.ron"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `gen_cumsgs` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/core/cu29_derive/tests/compile_fail/cu_msg/invalid_logging_config.rs b/core/cu29_derive/tests/compile_fail/cu_msg/invalid_logging_config.rs new file mode 100644 index 000000000..634c3f824 --- /dev/null +++ b/core/cu29_derive/tests/compile_fail/cu_msg/invalid_logging_config.rs @@ -0,0 +1,5 @@ +use cu29_derive::gen_cumsgs; + +gen_cumsgs!("config/invalid_logging_config.ron"); + +fn main() {} \ No newline at end of file diff --git a/core/cu29_derive/tests/compile_fail/cu_msg/invalid_logging_config.stderr b/core/cu29_derive/tests/compile_fail/cu_msg/invalid_logging_config.stderr new file mode 100644 index 000000000..7a3b30114 --- /dev/null +++ b/core/cu29_derive/tests/compile_fail/cu_msg/invalid_logging_config.stderr @@ -0,0 +1,8 @@ +error: Section size (2 MiB) cannot be larger than slab size (1 MiB). Adjust the parameters accordingly. + context:None + --> tests/compile_fail/cu_msg/invalid_logging_config.rs:3:1 + | +3 | gen_cumsgs!("config/invalid_logging_config.ron"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `gen_cumsgs` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/core/cu29_derive/tests/config/invalid_logging_config.ron b/core/cu29_derive/tests/config/invalid_logging_config.ron new file mode 100644 index 000000000..acd6d960f --- /dev/null +++ b/core/cu29_derive/tests/config/invalid_logging_config.ron @@ -0,0 +1,26 @@ +( + tasks: [ + ( + id: "task0", + type: "tasks::ExampleSrc", + ), + ( + id: "task1", + type: "tasks::ExampleTask", + ), + ( + id: "task2", + type: "tasks::ExampleSink", + ), + ], + cnx: [ + (src: "task0", dst: "task1", msg: "i32"), + (src: "task1", dst: "task2", msg: "i32"), + ], + monitor: (type: "ExampleMonitor"), + logging: ( + section_size_mib: 2, + slab_size_mib: 1, + enable_task_logging: true + ) +) diff --git a/core/cu29_runtime/src/config.rs b/core/cu29_runtime/src/config.rs index df58edae5..16d7f6666 100644 --- a/core/cu29_runtime/src/config.rs +++ b/core/cu29_runtime/src/config.rs @@ -643,6 +643,30 @@ impl CuConfig { pub fn get_monitor_config(&self) -> Option<&MonitorConfig> { self.monitor.as_ref() } + + /// Validate the logging configuration to ensure section pre-allocation sizes do not exceed slab sizes. + /// This method is wrapper around [LoggingConfig::validate] + pub fn validate_logging_config(&self) -> CuResult<()> { + if let Some(logging) = &self.logging { + return logging.validate(); + } + Ok(()) + } +} + +impl LoggingConfig { + /// Validate the logging configuration to ensure section pre-allocation sizes do not exceed slab sizes. + pub fn validate(&self) -> CuResult<()> { + if let Some(section_size_mib) = self.section_size_mib { + if let Some(slab_size_mib) = self.slab_size_mib { + if section_size_mib > slab_size_mib { + return Err(CuError::from(format!("Section size ({} MiB) cannot be larger than slab size ({} MiB). Adjust the parameters accordingly.", section_size_mib, slab_size_mib))); + } + } + } + + Ok(()) + } } /// Read a copper configuration from a file. @@ -659,7 +683,10 @@ pub fn read_configuration(config_filename: &str) -> CuResult { /// Read a copper configuration from a String. pub fn read_configuration_str(config_content: String) -> CuResult { - Ok(CuConfig::deserialize_ron(&config_content)) + let cuconfig = CuConfig::deserialize_ron(&config_content); + cuconfig.validate_logging_config()?; + + Ok(cuconfig) } // tests @@ -742,4 +769,19 @@ mod tests { assert_eq!(logging_config.section_size_mib.unwrap(), 100); assert!(logging_config.enable_task_logging); } + + #[test] + fn test_validate_logging_config() { + // Test with valid logging configuration + let txt = + r#"( tasks: [], cnx: [], logging: ( slab_size_mib: 1024, section_size_mib: 100 ) )"#; + let config = CuConfig::deserialize_ron(txt); + assert!(config.validate_logging_config().is_ok()); + + // Test with invalid logging configuration + let txt = + r#"( tasks: [], cnx: [], logging: ( slab_size_mib: 100, section_size_mib: 1024 ) )"#; + let config = CuConfig::deserialize_ron(txt); + assert!(config.validate_logging_config().is_err()); + } }