Skip to content

Commit

Permalink
Add more helpful error message if section size is larger than slab size
Browse files Browse the repository at this point in the history
  • Loading branch information
AS1100K committed Jan 12, 2025
1 parent 5df7784 commit 93abc99
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 22 deletions.
14 changes: 10 additions & 4 deletions core/cu29_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {}
Original file line number Diff line number Diff line change
@@ -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)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use cu29_derive::copper_runtime;

#[copper_runtime(config = "config/invalid_logging_config.ron")]
struct MyApplicationStruct;

fn main() {}
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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() {}
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use cu29_derive::gen_cumsgs;

gen_cumsgs!("config/invalid_logging_config.ron");

fn main() {}
Original file line number Diff line number Diff line change
@@ -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)
26 changes: 26 additions & 0 deletions core/cu29_derive/tests/config/invalid_logging_config.ron
Original file line number Diff line number Diff line change
@@ -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
)
)
44 changes: 43 additions & 1 deletion core/cu29_runtime/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -659,7 +683,10 @@ pub fn read_configuration(config_filename: &str) -> CuResult<CuConfig> {

/// Read a copper configuration from a String.
pub fn read_configuration_str(config_content: String) -> CuResult<CuConfig> {
Ok(CuConfig::deserialize_ron(&config_content))
let cuconfig = CuConfig::deserialize_ron(&config_content);
cuconfig.validate_logging_config()?;

Ok(cuconfig)
}

// tests
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 93abc99

Please sign in to comment.