Skip to content

Commit 7502006

Browse files
authored
Reduce responder tasks size; fix transport scheduling (#333)
This PR is fixing the following TBD left unsolved in #332 " Fix the large "responder" future by splitting it into multiple tasks in an embassy-executor task pool. This should reduce these 20KB down to ~ 10KB on x64 and even less for the other targets " Additionally, it fixes a bug in the scheduling of the transport tasks, because `matter.run_transport` needs to be called only **once**, and if there are multiple transports (as in UDP and BTP in our case) they need to be chained together.
1 parent 7f5884f commit 7502006

2 files changed

Lines changed: 98 additions & 53 deletions

File tree

.github/workflows/size-check.yml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,9 @@ jobs:
5050
run: cd bloat-check; cargo build --release
5151

5252
- name: Clippy - bloat-check
53-
if: matrix.features == '' && matrix.crypto-backend == 'rustcrypto'
5453
run: cd bloat-check; cargo clippy --no-deps -- -Dwarnings
5554

5655
- name: Clippy - bloat-check - riscv32imac
57-
if: matrix.features == '' && matrix.crypto-backend == 'rustcrypto'
5856
run: cd bloat-check; cargo clippy --no-deps --target riscv32imac-unknown-none-elf -- -Dwarnings
5957

6058
- name: Build | Thumbv6m binary for size report
@@ -75,35 +73,35 @@ jobs:
7573
- name: Prepare bloat report from the previous builds
7674
run: |
7775
python scripts/memory/gh_sizes.py \
78-
rs-matter-core x86_64-unknown-linux-gnu infologs-optz-ltofat \
76+
"(core)" infologs-optz-ltofat x86_64-unknown-linux-gnu \
7977
bloat-check/target/release/bloat-check \
8078
/tmp/bloat_reports/
8179
python scripts/memory/gh_sizes.py \
82-
rs-matter-core thumbv6m-none-eabi infodefmt-optz-ltofat \
80+
"(core)" infodefmt-optz-ltofat thumbv6m-none-eabi \
8381
bloat-check/target/thumbv6m-none-eabi/release/bloat-check \
8482
/tmp/bloat_reports/
8583
python scripts/memory/gh_sizes.py \
86-
rs-matter-core thumbv7em-none-eabi infodefmt-optz-ltofat \
84+
"(core)" infodefmt-optz-ltofat thumbv7em-none-eabi \
8785
bloat-check/target/thumbv7em-none-eabi/release/bloat-check \
8886
/tmp/bloat_reports/
8987
python scripts/memory/gh_sizes.py \
90-
rs-matter-core riscv32imac-unknown-none-elf infodefmt-optz-ltofat \
88+
"(core)" infodefmt-optz-ltofat riscv32imac-unknown-none-elf \
9189
bloat-check/target/riscv32imac-unknown-none-elf/release/bloat-check \
9290
/tmp/bloat_reports/
9391
python scripts/memory/gh_sizes.py \
94-
onoff-light x86_64-unknown-linux-gnu infologs-optz-ltofat \
92+
onoff-light infologs-optz-ltofat x86_64-unknown-linux-gnu \
9593
target/release/onoff_light \
9694
/tmp/bloat_reports/
9795
python scripts/memory/gh_sizes.py \
98-
onoff-light-bt x86_64-unknown-linux-gnu infologs-optz-ltofat \
96+
onoff-light-bt infologs-optz-ltofat x86_64-unknown-linux-gnu \
9997
target/release/onoff_light_bt \
10098
/tmp/bloat_reports/
10199
python scripts/memory/gh_sizes.py \
102-
dimmable-light x86_64-unknown-linux-gnu infologs-optz-ltofat \
100+
dimmable-light infologs-optz-ltofat x86_64-unknown-linux-gnu \
103101
target/release/dimmable_light \
104102
/tmp/bloat_reports/
105103
python scripts/memory/gh_sizes.py \
106-
speaker x86_64-unknown-linux-gnu infologs-optz-ltofat \
104+
speaker infologs-optz-ltofat x86_64-unknown-linux-gnu \
107105
target/release/speaker \
108106
/tmp/bloat_reports/
109107

bloat-check/src/bin/bloat-check.rs

Lines changed: 90 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ use rs_matter::transport::network::btp::{
8787
};
8888
use rs_matter::transport::network::mdns::builtin::{BuiltinMdnsResponder, Host};
8989
use rs_matter::transport::network::{
90-
Address, BtAddr, Ipv4Addr, Ipv6Addr, NetworkReceive, NetworkSend,
90+
Address, BtAddr, ChainedNetwork, Ipv4Addr, Ipv6Addr, NetworkReceive, NetworkSend,
9191
};
9292
use rs_matter::utils::epoch::dummy_epoch;
9393
use rs_matter::utils::init::{init, Init, InitMaybeUninit};
@@ -195,6 +195,7 @@ type AppNetCtl<'a> = NetCtlWithStatusImpl<'a, NoopRawMutex, FakeWifi>;
195195
type AppWirelessMgr<'a> = WirelessMgr<'a, &'a WifiNetworks<3, NoopRawMutex>, &'a AppNetCtl<'a>>;
196196
type AppBtp<'a> =
197197
Btp<&'a BtpContext<CriticalSectionRawMutex>, CriticalSectionRawMutex, FakeGattPeripheral>;
198+
type AppTransport<'a> = ChainedNetwork<FakeUdp, &'a AppBtp<'a>, fn(&Address) -> bool>;
198199
type AppHandler<'a> = handler_chain_type!(
199200
EpClMatcher => on_off::HandlerAsyncAdaptor<on_off::OnOffHandler<'a, TestOnOffDeviceLogic, NoLevelControl>>,
200201
EpClMatcher => Async<desc::HandlerAdaptor<DescHandler<'a>>>
@@ -326,6 +327,20 @@ fn main() -> ! {
326327
BuiltinMdnsResponder::new(&stack.matter)
327328
);
328329

330+
let transport_send = mk_static!(
331+
AppTransport<'static>,
332+
ChainedNetwork::new(Address::is_udp, FakeUdp, &*btp)
333+
);
334+
335+
report_size("Transport send", size_of_val(&*mdns), &mut aux_total);
336+
337+
let transport_recv = mk_static!(
338+
AppTransport<'static>,
339+
ChainedNetwork::new(Address::is_udp, FakeUdp, &*btp)
340+
);
341+
342+
report_size("Transport receive", size_of_val(&*mdns), &mut aux_total);
343+
329344
report_size("mDNS responder", size_of_val(&*mdns), &mut aux_total);
330345

331346
// A Wireless handler with a sample app cluster (on-off)
@@ -377,26 +392,34 @@ fn main() -> ! {
377392
let mut fut_total = 0;
378393

379394
report_size(
380-
"Respond task",
381-
size_of_val(&respond_task0(responder)),
395+
"Respond tasks",
396+
size_of_val(&respond_task_fut(responder, 0)) * 4,
382397
&mut fut_total,
383398
);
384-
report_size("DM task", size_of_val(&dm_task0(dm)), &mut fut_total);
385-
report_size("mDNS task", size_of_val(&mdns_task0(mdns)), &mut fut_total);
386-
report_size("BTP task", size_of_val(&btp_task0(btp)), &mut fut_total);
387399
report_size(
388-
"Wifi task",
389-
size_of_val(&wifi_task0(wifi_mgr)),
400+
"Respond busy tasks",
401+
size_of_val(&respond_busy_task_fut(responder, 0)) * 2,
390402
&mut fut_total,
391403
);
404+
report_size("DM task", size_of_val(&dm_task_fut(dm)), &mut fut_total);
392405
report_size(
393-
"BTP transport task",
394-
size_of_val(&btp_transport_task0(&stack.matter, btp)),
406+
"mDNS task",
407+
size_of_val(&mdns_task_fut(mdns)),
395408
&mut fut_total,
396409
);
410+
report_size("BTP task", size_of_val(&btp_task_fut(btp)), &mut fut_total);
397411
report_size(
398-
"UDP transport task",
399-
size_of_val(&udp_transport_task0(&stack.matter)),
412+
"Wifi task",
413+
size_of_val(&wifi_task_fut(wifi_mgr)),
414+
&mut fut_total,
415+
);
416+
report_size(
417+
"Transport task",
418+
size_of_val(&transport_task_fut(
419+
&stack.matter,
420+
transport_send,
421+
transport_recv,
422+
)),
400423
&mut fut_total,
401424
);
402425

@@ -423,40 +446,65 @@ fn main() -> ! {
423446
}
424447

425448
executor.run(|spawner| {
426-
unwrap!(spawner.spawn(respond_task(responder)));
449+
unwrap!(spawner.spawn(respond_busy_task(responder, 1)));
450+
unwrap!(spawner.spawn(respond_busy_task(responder, 0)));
451+
unwrap!(spawner.spawn(respond_task(responder, 3)));
452+
unwrap!(spawner.spawn(respond_task(responder, 2)));
453+
unwrap!(spawner.spawn(respond_task(responder, 1)));
454+
unwrap!(spawner.spawn(respond_task(responder, 0)));
427455
unwrap!(spawner.spawn(dm_task(dm)));
428456
unwrap!(spawner.spawn(mdns_task(mdns)));
429457
unwrap!(spawner.spawn(btp_task(btp)));
430458
unwrap!(spawner.spawn(wifi_task(wifi_mgr)));
431-
unwrap!(spawner.spawn(btp_transport_task(&stack.matter, btp)));
432-
unwrap!(spawner.spawn(udp_transport_task(&stack.matter)));
459+
unwrap!(spawner.spawn(transport_task(
460+
&stack.matter,
461+
transport_send,
462+
transport_recv
463+
)));
433464
});
434465
}
435466

436467
#[inline(always)]
437-
fn respond_task0<'d, 'a>(
468+
fn respond_task_fut<'d, 'a>(
438469
responder: &'a AppResponder<'d, 'a>,
470+
handler_id: u8,
439471
) -> impl Future<Output = Result<(), Error>> + 'a {
440-
responder.run::<4, 4>()
472+
responder.responder().handle(handler_id)
441473
}
442474

443-
#[embassy_executor::task]
444-
async fn respond_task(responder: &'static AppResponder<'static, 'static>) {
445-
unwrap!(respond_task0(responder).await);
475+
#[embassy_executor::task(pool_size = 4)]
476+
async fn respond_task(responder: &'static AppResponder<'static, 'static>, handler_id: u8) {
477+
info!("Starting responder task {}...", handler_id);
478+
unwrap!(respond_task_fut(responder, handler_id).await);
446479
}
447480

448481
#[inline(always)]
449-
fn dm_task0<'a>(dm: &'a AppDataModel<'a>) -> impl Future<Output = Result<(), Error>> + 'a {
482+
fn respond_busy_task_fut<'d, 'a>(
483+
responder: &'a AppResponder<'d, 'a>,
484+
handler_id: u8,
485+
) -> impl Future<Output = Result<(), Error>> + 'a {
486+
responder.busy_responder().handle(handler_id)
487+
}
488+
489+
#[embassy_executor::task(pool_size = 2)]
490+
async fn respond_busy_task(responder: &'static AppResponder<'static, 'static>, handler_id: u8) {
491+
info!("Starting busy responder task {}...", handler_id);
492+
unwrap!(respond_busy_task_fut(responder, handler_id).await);
493+
}
494+
495+
#[inline(always)]
496+
fn dm_task_fut<'a>(dm: &'a AppDataModel<'a>) -> impl Future<Output = Result<(), Error>> + 'a {
450497
dm.run()
451498
}
452499

453500
#[embassy_executor::task]
454501
async fn dm_task(dm: &'static AppDataModel<'static>) {
455-
unwrap!(dm_task0(dm).await);
502+
info!("Starting DM task...");
503+
unwrap!(dm_task_fut(dm).await);
456504
}
457505

458506
#[inline(always)]
459-
fn mdns_task0<'a>(
507+
fn mdns_task_fut<'a>(
460508
mdns: &'a mut BuiltinMdnsResponder<'static>,
461509
) -> impl Future<Output = Result<(), Error>> + 'a {
462510
mdns.run(
@@ -475,52 +523,51 @@ fn mdns_task0<'a>(
475523

476524
#[embassy_executor::task]
477525
async fn mdns_task(mdns: &'static mut BuiltinMdnsResponder<'static>) {
478-
unwrap!(mdns_task0(mdns).await);
526+
info!("Starting mDNS task...");
527+
unwrap!(mdns_task_fut(mdns).await);
479528
}
480529

481530
#[inline(always)]
482-
fn btp_task0<'a>(btp: &'a AppBtp<'static>) -> impl Future<Output = Result<(), Error>> + 'a {
531+
fn btp_task_fut<'a>(btp: &'a AppBtp<'static>) -> impl Future<Output = Result<(), Error>> + 'a {
483532
btp.run("MT", &TEST_DEV_DET, TEST_DEV_COMM.discriminator)
484533
}
485534

486535
#[embassy_executor::task]
487536
async fn btp_task(btp: &'static AppBtp<'static>) {
488-
unwrap!(btp_task0(btp).await);
537+
info!("Starting BTP task...");
538+
unwrap!(btp_task_fut(btp).await);
489539
}
490540

491541
#[inline(always)]
492-
fn wifi_task0<'a>(
542+
fn wifi_task_fut<'a>(
493543
wifi_mgr: &'a mut AppWirelessMgr<'static>,
494544
) -> impl Future<Output = Result<(), Error>> + 'a {
495545
wifi_mgr.run()
496546
}
497547

498548
#[embassy_executor::task]
499549
async fn wifi_task(wifi_mgr: &'static mut AppWirelessMgr<'static>) {
500-
unwrap!(wifi_task0(wifi_mgr).await);
550+
info!("Starting Wifi task...");
551+
unwrap!(wifi_task_fut(wifi_mgr).await);
501552
}
502553

503554
#[inline(always)]
504-
fn btp_transport_task0<'a>(
555+
fn transport_task_fut<'a>(
505556
matter: &'a Matter<'a>,
506-
btp: &'a AppBtp<'static>,
557+
transport_send: &'a mut AppTransport<'static>,
558+
transport_recv: &'a mut AppTransport<'static>,
507559
) -> impl Future<Output = Result<(), Error>> + 'a {
508-
matter.run_transport(btp, btp)
509-
}
510-
511-
#[embassy_executor::task]
512-
async fn btp_transport_task(matter: &'static Matter<'static>, btp: &'static AppBtp<'static>) {
513-
unwrap!(btp_transport_task0(matter, btp).await);
514-
}
515-
516-
#[inline(always)]
517-
fn udp_transport_task0<'a>(matter: &'a Matter<'a>) -> impl Future<Output = Result<(), Error>> + 'a {
518-
matter.run_transport(FakeUdp, FakeUdp)
560+
matter.run_transport(transport_send, transport_recv)
519561
}
520562

521563
#[embassy_executor::task]
522-
async fn udp_transport_task(matter: &'static Matter<'static>) {
523-
unwrap!(udp_transport_task0(matter).await);
564+
async fn transport_task(
565+
matter: &'static Matter<'static>,
566+
transport_send: &'static mut AppTransport<'static>,
567+
transport_recv: &'static mut AppTransport<'static>,
568+
) {
569+
info!("Starting transport task...");
570+
unwrap!(transport_task_fut(matter, transport_send, transport_recv).await);
524571
}
525572

526573
/// Report the size of an item and accumulate it into `total`

0 commit comments

Comments
 (0)