-
Notifications
You must be signed in to change notification settings - Fork 1
feat: parallelize filter evaluation #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0dab88f
chore: update packet-dissector crates to 0.3.3
higebu 8e7ec97
feat: add thread pool sizing and filter parallel-safety check
higebu 6a645a8
feat(tui): parallelize filter scan with per-worker registries
higebu dfab220
feat(read): parallelize filter evaluation for file input
higebu 4654b29
bench: add parallel filter scan benchmark
higebu ef2022d
fix(read): align progress and serialization error semantics with sequ…
higebu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,152 @@ | ||
| //! Benchmark for parallel filter evaluation (`dsct read --threads`). | ||
| //! | ||
| //! Generates a synthetic pcap with mixed UDP and TCP packets and measures | ||
| //! the throughput of the parallel read engine at threads=1 vs threads=4, | ||
| //! writing matched records to [`io::sink()`]. | ||
|
|
||
| use std::io; | ||
| use std::path::Path; | ||
|
|
||
| use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; | ||
|
|
||
| use dsct::parallel_read::{ParallelReadOptions, run}; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Pcap generation | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| /// Build a synthetic pcap with `n` packets alternating UDP and TCP. | ||
| /// | ||
| /// Even-indexed packets: Ethernet + IPv4 + UDP (42 bytes). | ||
| /// Odd-indexed packets: Ethernet + IPv4 + TCP (54 bytes). | ||
| fn build_bench_pcap(n: usize) -> Vec<u8> { | ||
| let mut buf = Vec::with_capacity(24 + n * 60); | ||
|
|
||
| // Global header | ||
| buf.extend_from_slice(&0xA1B2C3D4u32.to_le_bytes()); | ||
| buf.extend_from_slice(&2u16.to_le_bytes()); | ||
| buf.extend_from_slice(&4u16.to_le_bytes()); | ||
| buf.extend_from_slice(&0i32.to_le_bytes()); | ||
| buf.extend_from_slice(&0u32.to_le_bytes()); | ||
| buf.extend_from_slice(&65535u32.to_le_bytes()); | ||
| buf.extend_from_slice(&1u32.to_le_bytes()); // Ethernet | ||
|
|
||
| // UDP packet template (42 bytes): Eth + IPv4 (UDP, 10.0.0.1→10.0.0.2) | ||
| let udp: &[u8] = &[ | ||
| // Ethernet (14 bytes) | ||
| 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // dst mac | ||
| 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, // src mac | ||
| 0x08, 0x00, // ethertype IPv4 | ||
| // IPv4 (20 bytes) | ||
| 0x45, 0x00, 0x00, 0x1c, // version/IHL, DSCP, total length (28) | ||
| 0x00, 0x00, 0x00, 0x00, // id, flags+frag | ||
| 0x40, 0x11, 0x00, 0x00, // TTL=64, proto=UDP, checksum=0 | ||
| 0x0a, 0x00, 0x00, 0x01, // src 10.0.0.1 | ||
| 0x0a, 0x00, 0x00, 0x02, // dst 10.0.0.2 | ||
| // UDP (8 bytes) | ||
| 0x10, 0x00, // src port 4096 | ||
| 0x10, 0x01, // dst port 4097 | ||
| 0x00, 0x08, // length 8 | ||
| 0x00, 0x00, // checksum | ||
| ]; | ||
|
|
||
| // TCP SYN packet template (54 bytes): Eth + IPv4 (TCP, 10.0.0.3→10.0.0.4) | ||
| let tcp: &[u8] = &[ | ||
| // Ethernet (14 bytes) | ||
| 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x08, 0x00, | ||
| // IPv4 (20 bytes) | ||
| 0x45, 0x00, 0x00, 0x28, // total length = 40 | ||
| 0x00, 0x00, 0x00, 0x00, 0x40, 0x06, 0x00, 0x00, // TTL=64, proto=TCP | ||
| 0x0a, 0x00, 0x00, 0x03, // src 10.0.0.3 | ||
| 0x0a, 0x00, 0x00, 0x04, // dst 10.0.0.4 | ||
| // TCP (20 bytes) | ||
| 0x30, 0x39, // src port 12345 | ||
| 0x07, 0xd0, // dst port 2000 | ||
| 0x00, 0x00, 0x00, 0x01, // seq | ||
| 0x00, 0x00, 0x00, 0x00, // ack | ||
| 0x50, 0x02, // data offset=5, SYN | ||
| 0xff, 0xff, // window | ||
| 0x00, 0x00, // checksum | ||
| 0x00, 0x00, // urgent | ||
| ]; | ||
|
|
||
| for i in 0..n { | ||
| let pkt = if i % 2 == 0 { udp } else { tcp }; | ||
| let ts_sec = (i / 1_000_000) as u32; | ||
| let ts_usec = (i % 1_000_000) as u32; | ||
| buf.extend_from_slice(&ts_sec.to_le_bytes()); | ||
| buf.extend_from_slice(&ts_usec.to_le_bytes()); | ||
| buf.extend_from_slice(&(pkt.len() as u32).to_le_bytes()); | ||
| buf.extend_from_slice(&(pkt.len() as u32).to_le_bytes()); | ||
| buf.extend_from_slice(pkt); | ||
| } | ||
|
|
||
| buf | ||
| } | ||
|
|
||
| /// Write a bench pcap to `path` and return the file size in bytes. | ||
| fn write_bench_pcap(path: &Path, n: usize) -> u64 { | ||
| let data = build_bench_pcap(n); | ||
| std::fs::write(path, &data).expect("write bench pcap"); | ||
| data.len() as u64 | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Benchmark | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| fn bench_parallel_read(c: &mut Criterion) { | ||
| const N: usize = 20_000; // ~20 k packets; each is 42 or 54 bytes | ||
|
|
||
| let path = std::env::temp_dir().join(format!( | ||
| "dsct_bench_parallel_{}_{}.pcap", | ||
| N, | ||
| std::process::id() | ||
| )); | ||
| let file_size = write_bench_pcap(&path, N); | ||
|
|
||
| let mut group = c.benchmark_group("parallel_read"); | ||
| group.sample_size(10); | ||
| group.throughput(Throughput::Bytes(file_size)); | ||
|
|
||
| // Filter that is parallel-safe: match all UDP packets. | ||
| let filter = "udp"; | ||
|
|
||
| for &threads in &[1usize, 4usize] { | ||
| group.bench_with_input( | ||
| BenchmarkId::new("udp_filter", format!("threads={threads}")), | ||
| &threads, | ||
| |b, &t| { | ||
| b.iter(|| { | ||
| let mut sink = io::sink(); | ||
| run( | ||
| &ParallelReadOptions { | ||
| path: &path, | ||
| filter_str: filter, | ||
| decode_as_args: &[], | ||
| threads: t, | ||
| sample_rate: 1, | ||
| offset: 0, | ||
| count: None, | ||
| pn_filter: None, | ||
| field_config: None, // verbose mode | ||
| raw_bytes: false, | ||
| progress_interval: 0, | ||
| }, | ||
| &mut sink, | ||
| &mut |_, _| {}, | ||
| &mut |_, _| {}, | ||
| ) | ||
| .expect("parallel_read::run failed in benchmark"); | ||
| }); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| group.finish(); | ||
|
|
||
| let _ = std::fs::remove_file(&path); | ||
| } | ||
|
|
||
| criterion_group!(benches, bench_parallel_read); | ||
| criterion_main!(benches); |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FilterExpr::Whereskipsnormalize_protocol_nameunlikeFilterExpr::ProtocolThe
Protocolarm explicitly normalises the name before comparing againstSAFE_PROTOCOLS, but theWherearm comparesclause.protocoldirectly. The tests pass because the SQL parser appears to lower-case protocol names, but if the two code paths are ever decoupled (e.g.,WhereClauseis constructed directly in tests or serialised/deserialised), a protocol stored with any casing other than lowercase ("TCP","UDP") would not match the whitelist and would conservatively fall through to sequential scanning without any warning.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!