-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for reading DLT messages.
This change includes: - new API to read DLT messages from a source - new API to read DLT messages from a stream (#34) - new API to collect generic DLT statistics (#31) - removing of any dependency to the buf_redux crate - consolidation of the crate's feature names - increase of the crate's version to 0.20.0
- Loading branch information
Showing
17 changed files
with
865 additions
and
540 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use dlt_core::{ | ||
parse::DltParseError, | ||
read::DltMessageReader, | ||
statistics::{collect_statistics, Statistic, StatisticCollector}, | ||
}; | ||
use std::{env, fs, fs::File, path::PathBuf, time::Instant}; | ||
|
||
pub struct MessageCounter { | ||
count: usize, | ||
} | ||
|
||
impl StatisticCollector for MessageCounter { | ||
fn collect_statistic(&mut self, _statistic: Statistic) -> Result<(), DltParseError> { | ||
self.count += 1; | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn main() { | ||
// collect input file details | ||
let dlt_file_path = PathBuf::from(&env::args().nth(1).expect("no filename given")); | ||
let dlt_file = File::open(&dlt_file_path).expect("open input file"); | ||
let dlt_file_size = fs::metadata(&dlt_file_path).expect("file size error").len(); | ||
// now scan all file content | ||
let mut dlt_reader = DltMessageReader::new(dlt_file, true); | ||
let mut dlt_collector = MessageCounter { count: 0 }; | ||
let start = Instant::now(); | ||
collect_statistics(&mut dlt_reader, &mut dlt_collector).expect("collect dlt statistics"); | ||
// print some stats | ||
let duration_in_s = start.elapsed().as_millis() as f64 / 1000.0; | ||
let file_size_in_mb = dlt_file_size as f64 / 1024.0 / 1024.0; | ||
let amount_per_second: f64 = file_size_in_mb / duration_in_s; | ||
println!( | ||
"parsing {} messages took {:.3}s! ({:.3} MB/s)", | ||
dlt_collector.count, duration_in_s, amount_per_second | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,31 @@ | ||
use buf_redux::{policy::MinBuffered, BufReader}; | ||
use dlt_core::parse::{dlt_message, DltParseError}; | ||
use std::{env, fs, fs::File, io::BufRead, path::PathBuf, time::Instant}; | ||
|
||
const BIN_READER_CAPACITY: usize = 10 * 1024 * 1024; | ||
const BIN_MIN_BUFFER_SPACE: usize = 10 * 1024; | ||
use dlt_core::read::{read_message, DltMessageReader}; | ||
use std::{env, fs, fs::File, path::PathBuf, time::Instant}; | ||
|
||
fn main() { | ||
// collect input file details | ||
let dlt_file_path = PathBuf::from(&env::args().nth(1).expect("No filename given")); | ||
let dlt_file = File::open(&dlt_file_path).expect("could not open file"); | ||
let source_file_size = fs::metadata(&dlt_file_path).expect("file size error").len(); | ||
// create a reader that maintains a minimum amount of bytes in it's buffer | ||
let mut reader = BufReader::with_capacity(BIN_READER_CAPACITY, dlt_file) | ||
.set_policy(MinBuffered(BIN_MIN_BUFFER_SPACE)); | ||
let dlt_file_path = PathBuf::from(&env::args().nth(1).expect("no filename given")); | ||
let dlt_file = File::open(&dlt_file_path).expect("open input file"); | ||
let dlt_file_size = fs::metadata(&dlt_file_path).expect("file size error").len(); | ||
// now parse all file content | ||
let mut parsed = 0usize; | ||
let mut dlt_reader = DltMessageReader::new(dlt_file, true); | ||
let mut message_count = 0usize; | ||
let start = Instant::now(); | ||
loop { | ||
let consumed: usize = match reader.fill_buf() { | ||
Ok(content) => { | ||
if content.is_empty() { | ||
println!("empty content after {} parsed messages", parsed); | ||
break; | ||
} | ||
let available = content.len(); | ||
|
||
match dlt_message(content, None, true) { | ||
Ok((rest, _maybe_msg)) => { | ||
let consumed = available - rest.len(); | ||
parsed += 1; | ||
consumed | ||
} | ||
Err(DltParseError::IncompleteParse { needed }) => { | ||
println!("parse incomplete, needed: {:?}", needed); | ||
return; | ||
} | ||
Err(DltParseError::ParsingHickup(reason)) => { | ||
println!("parse error: {}", reason); | ||
4 //skip 4 bytes | ||
} | ||
Err(DltParseError::Unrecoverable(cause)) => { | ||
println!("unrecoverable parse failure: {}", cause); | ||
return; | ||
} | ||
} | ||
match read_message(&mut dlt_reader, None).expect("read dlt message") { | ||
Some(_message) => { | ||
message_count += 1; | ||
} | ||
Err(e) => { | ||
println!("Error reading: {}", e); | ||
return; | ||
None => { | ||
break; | ||
} | ||
}; | ||
reader.consume(consumed); | ||
} | ||
|
||
// print some stats | ||
let duration_in_s = start.elapsed().as_millis() as f64 / 1000.0; | ||
let file_size_in_mb = source_file_size as f64 / 1024.0 / 1024.0; | ||
let file_size_in_mb = dlt_file_size as f64 / 1024.0 / 1024.0; | ||
let amount_per_second: f64 = file_size_in_mb / duration_in_s; | ||
println!( | ||
"parsing {} messages took {:.3}s! ({:.3} MB/s)", | ||
parsed, duration_in_s, amount_per_second | ||
message_count, duration_in_s, amount_per_second | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use dlt_core::stream::{read_message, DltStreamReader}; | ||
use std::{env, fs, path::PathBuf, time::Instant}; | ||
use tokio::fs::File; | ||
use tokio_util::compat::TokioAsyncReadCompatExt; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// collect input file details | ||
let dlt_file_path = PathBuf::from(&env::args().nth(1).expect("no filename given")); | ||
let dlt_file = File::open(&dlt_file_path).await.expect("open input file"); | ||
let dlt_file_size = fs::metadata(&dlt_file_path).expect("file size error").len(); | ||
// now parse all file content | ||
let mut dlt_reader = DltStreamReader::new(dlt_file.compat(), true); | ||
let mut message_count = 0usize; | ||
let start = Instant::now(); | ||
loop { | ||
match read_message(&mut dlt_reader, None) | ||
.await | ||
.expect("read dlt message") | ||
{ | ||
Some(_message) => { | ||
message_count += 1; | ||
} | ||
None => { | ||
break; | ||
} | ||
}; | ||
} | ||
// print some stats | ||
let duration_in_s = start.elapsed().as_millis() as f64 / 1000.0; | ||
let file_size_in_mb = dlt_file_size as f64 / 1024.0 / 1024.0; | ||
let amount_per_second: f64 = file_size_in_mb / duration_in_s; | ||
println!( | ||
"parsing {} messages took {:.3}s! ({:.3} MB/s)", | ||
message_count, duration_in_s, amount_per_second | ||
); | ||
} |
Oops, something went wrong.