Skip to content

Commit 30b6b9d

Browse files
committed
[pixie-uefi] Compute total memory available and optimize buffer size.
1 parent 3f8a4c7 commit 30b6b9d

4 files changed

Lines changed: 35 additions & 11 deletions

File tree

pixie-uefi/src/flash.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
use crate::os::{
2-
error::{Error, Result},
3-
mpsc, TcpStream, UefiOS, PACKET_SIZE,
1+
use crate::{
2+
os::{
3+
error::{Error, Result},
4+
mpsc, TcpStream, UefiOS, PACKET_SIZE,
5+
},
6+
MIN_MEMORY,
47
};
58
use alloc::{boxed::Box, collections::BTreeMap, rc::Rc, string::ToString, vec::Vec};
69
use core::{cell::RefCell, mem, net::SocketAddrV4};
710
use futures::future::{select, Either};
811
use log::info;
912
use lz4_flex::decompress;
10-
use pixie_shared::{ChunkHash, Image, TcpRequest, UdpRequest, BODY_LEN, CHUNKS_PORT, HEADER_LEN};
13+
use pixie_shared::{
14+
ChunkHash, Image, TcpRequest, UdpRequest, BODY_LEN, CHUNKS_PORT, HEADER_LEN, MAX_CHUNK_SIZE,
15+
};
1116
use uefi::proto::console::text::Color;
1217

1318
struct PartialChunk {
@@ -232,6 +237,9 @@ pub async fn flash(os: UefiOS, server_addr: SocketAddrV4) -> Result<()> {
232237
let task1 = async {
233238
let mut tx = tx;
234239
let mut last_seen = Vec::new();
240+
let total_mem = os.get_total_mem();
241+
let max_chunks = (total_mem.saturating_sub(MIN_MEMORY) as usize / MAX_CHUNK_SIZE).max(128);
242+
log::debug!("Total memory: {total_mem}. Max chunks in memory: {max_chunks}");
235243
while !chunks_info.is_empty() {
236244
let recv = Box::pin(socket.recv(&mut buf));
237245
let sleep = Box::pin(os.sleep_us(100_000));
@@ -247,7 +255,7 @@ pub async fn flash(os: UefiOS, server_addr: SocketAddrV4) -> Result<()> {
247255
}
248256

249257
assert_eq!(last_seen.len(), received.len());
250-
if last_seen.len() > 128 {
258+
if last_seen.len() > max_chunks {
251259
let hash = last_seen.remove(0);
252260
received
253261
.remove(&hash)

pixie-uefi/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ mod reboot_to_os;
2828
mod register;
2929
mod store;
3030

31+
const MIN_MEMORY: u64 = 500 << 20;
32+
3133
async fn server_discover(os: UefiOS) -> Result<SocketAddrV4> {
3234
let socket = os.udp_bind(None).await?;
3335

pixie-uefi/src/os/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ use core::{
2525
task::{Context, Poll},
2626
};
2727
use uefi::{
28-
boot::{EventType, ScopedProtocol, TimerTrigger, Tpl},
28+
boot::{EventType, MemoryType, ScopedProtocol, TimerTrigger, Tpl},
29+
mem::memory_map::MemoryMap,
2930
proto::{
3031
console::{
3132
serial::Serial,
@@ -571,6 +572,14 @@ impl UefiOS {
571572
pub fn shutdown(&self) -> ! {
572573
uefi::runtime::reset(uefi::runtime::ResetType::SHUTDOWN, Status::SUCCESS, None)
573574
}
575+
576+
pub fn get_total_mem(&self) -> u64 {
577+
uefi::boot::memory_map(MemoryType::LOADER_DATA)
578+
.expect("Failed to get memory map")
579+
.entries()
580+
.map(|entry| entry.page_count * 4096)
581+
.sum()
582+
}
574583
}
575584

576585
impl log::Log for UefiOS {

pixie-uefi/src/store.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
error::{Error, Result},
44
mpsc, TcpStream, UefiOS,
55
},
6-
parse_disk,
6+
parse_disk, MIN_MEMORY,
77
};
88
use alloc::{rc::Rc, vec::Vec};
99
use core::{cell::RefCell, net::SocketAddrV4};
@@ -102,10 +102,15 @@ pub async fn store(os: UefiOS, server_address: SocketAddrV4) -> Result<()> {
102102
let mut total_size = 0;
103103
let mut total_csize = 0;
104104

105-
let (tx1, mut rx1) = mpsc::channel(32);
106-
let (tx2, mut rx2) = mpsc::channel(32);
107-
let (tx3, mut rx3) = mpsc::channel(32);
108-
let (tx4, mut rx4) = mpsc::channel(32);
105+
let total_mem = os.get_total_mem();
106+
let channel_size =
107+
(total_mem.saturating_sub(MIN_MEMORY) as usize / (4 * MAX_CHUNK_SIZE)).max(32);
108+
log::debug!("Total memory: {total_mem}. Channel size: {channel_size}");
109+
110+
let (tx1, mut rx1) = mpsc::channel(channel_size);
111+
let (tx2, mut rx2) = mpsc::channel(channel_size);
112+
let (tx3, mut rx3) = mpsc::channel(channel_size);
113+
let (tx4, mut rx4) = mpsc::channel(channel_size);
109114

110115
let task1 = async {
111116
let mut tx1 = tx1;

0 commit comments

Comments
 (0)