Skip to content

Commit

Permalink
Reduce Fabrics memory consumption; provisions for UpdateNOC (project-…
Browse files Browse the repository at this point in the history
…chip#208)

* Reduce Fabrics memory consumption; provisions for UpdateNOC

* Revert a change which does not belong to this PR

* Remove a TODO which should not be there

* Restore commented out unit tests

* Clippy
  • Loading branch information
ivmarkov authored Sep 25, 2024
1 parent 93d2082 commit 470a7be
Show file tree
Hide file tree
Showing 30 changed files with 2,046 additions and 1,436 deletions.
49 changes: 42 additions & 7 deletions examples/onoff_light/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

use core::pin::pin;

use std::net::UdpSocket;

use embassy_futures::select::{select, select4};
Expand Down Expand Up @@ -78,7 +79,7 @@ fn main() -> Result<(), Error> {
// e.g., an opt-level of "0" will require a several times' larger stack.
//
// Optimizing/lowering `rs-matter` memory consumption is an ongoing topic.
.stack_size(54 * 1024)
.stack_size(45 * 1024)
.spawn(run)
.unwrap();

Expand All @@ -91,9 +92,10 @@ fn run() -> Result<(), Error> {
);

info!(
"Matter memory: Matter={}B, IM Buffers={}B",
"Matter memory: Matter (BSS)={}B, IM Buffers (BSS)={}B, Subscriptions (BSS)={}B",
core::mem::size_of::<Matter>(),
core::mem::size_of::<PooledBuffers<10, NoopRawMutex, IMBuffer>>()
core::mem::size_of::<PooledBuffers<10, NoopRawMutex, IMBuffer>>(),
core::mem::size_of::<Subscriptions<3>>()
);

let matter = MATTER.uninit().init_with(Matter::init(
Expand All @@ -115,8 +117,6 @@ fn run() -> Result<(), Error> {

info!("IM buffers initialized");

let mut mdns = pin!(run_mdns(&matter));

let on_off = cluster_on_off::OnOffCluster::new(Dataver::new_rand(matter.rand()));

let subscriptions = SUBSCRIPTIONS.uninit().init_with(Subscriptions::init());
Expand All @@ -128,14 +128,15 @@ fn run() -> Result<(), Error> {
// All other subscription requests will be turned down with "resource exhausted"
let responder = DefaultResponder::new(&matter, buffers, &subscriptions, dm_handler);
info!(
"Responder memory: Responder={}B, Runner={}B",
"Responder memory: Responder (stack)={}B, Runner fut (stack)={}B",
core::mem::size_of_val(&responder),
core::mem::size_of_val(&responder.run::<4, 4>())
);

// Run the responder with up to 4 handlers (i.e. 4 exchanges can be handled simultenously)
// Clients trying to open more exchanges than the ones currently running will get "I'm busy, please try again later"
let mut respond = pin!(responder.run::<4, 4>());
//let mut respond = responder_fut(responder);

// This is a sample code that simulates state changes triggered by the HAL
// Changes will be properly communicated to the Matter controllers and other Matter apps (i.e. Google Home, Alexa), thanks to subscriptions
Expand All @@ -156,6 +157,25 @@ fn run() -> Result<(), Error> {
let socket = async_io::Async::<UdpSocket>::bind(MATTER_SOCKET_BIND_ADDR)?;

// Run the Matter and mDNS transports
info!(
"Transport memory: Transport fut (stack)={}B, mDNS fut (stack)={}B",
core::mem::size_of_val(&matter.run(
&socket,
&socket,
Some((
CommissioningData {
// TODO: Hard-coded for now
verifier: VerifierData::new_with_pw(123456, matter.rand()),
discriminator: 250,
},
Default::default(),
)),
)),
core::mem::size_of_val(&run_mdns(&matter))
);

let mut mdns = pin!(run_mdns(&matter));

let mut transport = pin!(matter.run(
&socket,
&socket,
Expand All @@ -171,9 +191,14 @@ fn run() -> Result<(), Error> {

// NOTE:
// Replace with your own persister for e.g. `no_std` environments

let psm = PSM.uninit().init_with(Psm::init());

info!(
"Persist memory: Persist (BSS)={}B, Persist fut (stack)={}B",
core::mem::size_of::<Psm<4096>>(),
core::mem::size_of_val(&psm.run(std::env::temp_dir().join("rs-matter"), &matter))
);

let mut persist = pin!(psm.run(std::env::temp_dir().join("rs-matter"), &matter));

// Combine all async tasks in a single one
Expand All @@ -189,6 +214,15 @@ fn run() -> Result<(), Error> {
futures_lite::future::block_on(all.coalesce())
}

// #[inline(never)]
// pub fn responder_fut<const N: usize, B, T>(responder: &'static DefaultResponder<N, B, T>) -> Box<impl Future<Output = Result<(), Error>>>
// where
// B: BufferAccess<IMBuffer>,
// T: DataModelHandler,
// {
// Box::new(responder.run::<4, 4>())
// }

const NODE: Node<'static> = Node {
id: 0,
endpoints: &[
Expand Down Expand Up @@ -235,6 +269,7 @@ async fn run_mdns(matter: &Matter<'_>) -> Result<(), Error> {

// NOTE:
// Replace with your own network initialization for e.g. `no_std` environments
#[inline(never)]
fn initialize_network() -> Result<(Ipv4Addr, Ipv6Addr, u32), Error> {
use log::error;
use nix::{net::if_::InterfaceFlags, sys::socket::SockaddrIn6};
Expand Down
2 changes: 1 addition & 1 deletion rs-matter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ rust-version = "1.78"

[features]
default = ["os", "rustcrypto"]
#default = ["os", "mbedtls"]
#default = ["os", "mbedtls"] mbedtls is broken since several months - check the root cause
os = ["std", "backtrace", "critical-section/std", "embassy-sync/std", "embassy-time/std", "embassy-time/generic-queue"]
std = ["alloc", "rand"]
backtrace = []
Expand Down
Loading

0 comments on commit 470a7be

Please sign in to comment.