Skip to content

Commit

Permalink
Try 2
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernauer committed May 30, 2024
1 parent 3e32126 commit 748e2a9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
13 changes: 12 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ log = "0.4"
memadvise = "0.1"
memchr = "2.7"
number_prefix = "0.4"
page_size = "0.6"
pixelbomber = "0.6"
prometheus_exporter = "0.8"
rstest = "0.20"
Expand Down
1 change: 1 addition & 0 deletions breakwater/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ env_logger.workspace = true
log.workspace = true
memadvise.workspace = true
number_prefix.workspace = true
page_size.workspace = true
prometheus_exporter.workspace = true
rusttype.workspace = true
serde_json.workspace = true
Expand Down
11 changes: 10 additions & 1 deletion breakwater/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::alloc;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::{cmp::min, net::IpAddr, sync::Arc, time::Duration};
Expand Down Expand Up @@ -76,6 +77,10 @@ impl Server {
let (connection_dropped_tx, mut connection_dropped_rx) =
mpsc::unbounded_channel::<IpAddr>();
let connection_dropped_tx = self.max_connections_per_ip.map(|_| connection_dropped_tx);

let page_size = page_size::get();
info!("System has a page size of {page_size} bytes");

loop {
let (mut socket, socket_addr) = self
.listener
Expand Down Expand Up @@ -119,6 +124,7 @@ impl Server {
ip,
fb_for_thread,
statistics_tx_for_thread,
page_size,
network_buffer_size,
connection_dropped_tx_clone,
)
Expand All @@ -133,6 +139,7 @@ pub async fn handle_connection(
ip: IpAddr,
fb: Arc<FrameBuffer>,
statistics_tx: mpsc::Sender<StatisticsEvent>,
page_size: usize,
network_buffer_size: usize,
connection_dropped_tx: Option<mpsc::UnboundedSender<IpAddr>>,
) -> Result<(), Error> {
Expand All @@ -143,7 +150,9 @@ pub async fn handle_connection(
.await
.context(WriteToStatisticsChannelSnafu)?;

let mut buffer = vec![0u8; network_buffer_size];
let layout = alloc::Layout::from_size_align(network_buffer_size, page_size).unwrap();
let ptr = unsafe { alloc::alloc(layout) };
let buffer = unsafe { std::slice::from_raw_parts_mut(ptr, network_buffer_size) };

if let Err(err) = memadvise::advise(buffer.as_ptr() as _, buffer.len(), Advice::Sequential) {
// [`MemAdviseError`] does not implement Debug...
Expand Down

0 comments on commit 748e2a9

Please sign in to comment.