Skip to content
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

Reduce static footprint #34316

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

nk-shelly
Copy link

These changes reduce memory usage in our firmware by about 30K when matter is disabled.
This is important to us, as matter is optional and not the only stack we support, making large static and global variables too expensive.

@CLAassistant
Copy link

CLAassistant commented Jul 12, 2024

CLA assistant check
All committers have signed the CLA.

@nk-shelly nk-shelly force-pushed the reduce-static-footprint branch from 71e33da to 695b6b4 Compare July 24, 2024 15:24
Copy link
Contributor

@bzbarsky-apple bzbarsky-apple left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only looked at Global.h for now, in case changes there affect how the other bits work, but I tried this PR on Mac with #define CHIP_CONFIG_DYNAMIC_GLOBALS 1 and it fails unit tests with ASAN failures.... So that's worth looking into, perhaps. But maybe after we settle on how Global.h should work.

src/lib/core/Global.h Outdated Show resolved Hide resolved
src/lib/core/Global.h Show resolved Hide resolved
src/lib/core/Global.h Outdated Show resolved Hide resolved
src/lib/core/Global.h Outdated Show resolved Hide resolved
src/lib/core/Global.h Outdated Show resolved Hide resolved
Copy link

github-actions bot commented Jul 24, 2024

PR #34316: Size comparison from 8ebace8 to 695b6b4

Increases above 0.2%:

platform target config section 8ebace8 695b6b4 change % change
linux air-purifier-app debug unknown 4632 4648 16 0.3
FLASH 2705679 2717215 11536 0.4
all-clusters-app debug FLASH 5834028 5851090 17062 0.3
all-clusters-minimal-app debug FLASH 5293826 5309994 16168 0.3
bridge-app debug FLASH 4694610 4706978 12368 0.3
chip-tool-ipv6only arm64 unknown 20128 20192 64 0.3
fabric-bridge-app debug FLASH 4448844 4460384 11540 0.3
lighting-app debug+rpc+ui FLASH 5618689 5633569 14880 0.3
lock-app debug FLASH 4753062 4767064 14002 0.3
ota-provider-app debug FLASH 4397556 4411522 13966 0.3
ota-requestor-app debug FLASH 4535338 4549408 14070 0.3
shell debug unknown 4168 4184 16 0.4
FLASH 2978349 2994445 16096 0.5
thermostat-no-ble arm64 unknown 9208 9480 272 3.0
tv-app debug FLASH 5970301 5987085 16784 0.3
Full report (82 builds for bl602, bl702, bl702l, cc13x4_26x4, cc32xx, cyw30739, efr32, esp32, linux, mbed, nxp, psoc6, qpg, stm32, telink, tizen)
platform target config section 8ebace8 695b6b4 change % change
bl602 lighting-app bl602 FLASH 1271952 1272632 680 0.1
RAM 95384 95384 0 0.0
bl602+mfd FLASH 1286210 1286890 680 0.1
RAM 95528 95528 0 0.0
bl602+rpc FLASH 1310920 1311600 680 0.1
RAM 103808 103808 0 0.0
bl702 lighting-app bl702 FLASH 1092846 1093522 676 0.1
RAM 15245 15245 0 0.0
bl702+mfd FLASH 1103540 1104216 676 0.1
RAM 15397 15397 0 0.0
bl702+rpc FLASH 1182912 1183588 676 0.1
RAM 24245 24245 0 0.0
bl706-eth FLASH 876146 876822 676 0.1
RAM 27348 27348 0 0.0
bl706-wifi FLASH 1128328 1129086 758 0.1
RAM 14681 14681 0 0.0
bl702l lighting-app bl702l FLASH 1079744 1080680 936 0.1
RAM 21800 21800 0 0.0
bl702l+mfd FLASH 1091006 1091686 680 0.1
RAM 21960 21960 0 0.0
cc13x4_26x4 lighting-app LP_EM_CC1354P10_6 FLASH 798900 799064 164 0.0
RAM 109236 109236 0 0.0
lock-ftd LP_EM_CC1354P10_6 FLASH 816660 816832 172 0.0
RAM 117020 117020 0 0.0
lock-mtd LP_EM_CC1354P10_6 FLASH 808492 808672 180 0.0
RAM 111308 111308 0 0.0
pump-app LP_EM_CC1354P10_6 FLASH 761108 761272 164 0.0
RAM 105408 105408 0 0.0
pump-controller-app LP_EM_CC1354P10_6 FLASH 746892 747048 156 0.0
RAM 105632 105632 0 0.0
cc32xx air-purifier CC3235SF_LAUNCHXL FLASH 606478 606754 276 0.0
RAM 204564 204564 0 0.0
lock CC3235SF_LAUNCHXL FLASH 652018 652290 272 0.0
RAM 204836 204836 0 0.0
cyw30739 light CYW30739B2-P5-EVK-01 unknown 2040 2040 0 0.0
FLASH 668009 668121 112 0.0
RAM 77700 77700 0 0.0
CYW30739B2-P5-EVK-02 unknown 2040 2040 0 0.0
FLASH 687861 687973 112 0.0
RAM 80340 80340 0 0.0
CYW30739B2-P5-EVK-03 unknown 2040 2040 0 0.0
FLASH 687861 687973 112 0.0
RAM 80340 80340 0 0.0
CYW930739M2EVB-02 unknown 2040 2040 0 0.0
FLASH 644805 644909 104 0.0
RAM 72768 72768 0 0.0
light-switch CYW30739B2-P5-EVK-01 unknown 2040 2040 0 0.0
FLASH 609561 609681 120 0.0
RAM 70884 70884 0 0.0
CYW30739B2-P5-EVK-02 unknown 2040 2040 0 0.0
FLASH 629189 629317 128 0.0
RAM 73428 73428 0 0.0
CYW30739B2-P5-EVK-03 unknown 2040 2040 0 0.0
FLASH 629189 629317 128 0.0
RAM 73428 73428 0 0.0
lock CYW30739B2-P5-EVK-01 unknown 2040 2040 0 0.0
FLASH 625257 625385 128 0.0
RAM 73900 73900 0 0.0
CYW30739B2-P5-EVK-02 unknown 2040 2040 0 0.0
FLASH 644965 645093 128 0.0
RAM 76444 76444 0 0.0
CYW30739B2-P5-EVK-03 unknown 2040 2040 0 0.0
FLASH 644965 645093 128 0.0
RAM 76444 76444 0 0.0
thermostat CYW30739B2-P5-EVK-01 unknown 2040 2040 0 0.0
FLASH 593477 593597 120 0.0
RAM 67852 67852 0 0.0
CYW30739B2-P5-EVK-02 unknown 2040 2040 0 0.0
FLASH 613329 613449 120 0.0
RAM 70492 70492 0 0.0
CYW30739B2-P5-EVK-03 unknown 2040 2040 0 0.0
FLASH 613329 613449 120 0.0
RAM 70492 70492 0 0.0
efr32 lighting-app BRD4187C FLASH 926120 926240 120 0.0
RAM 134528 134528 0 0.0
lock-app BRD4338a FLASH 734508 734892 384 0.1
RAM 208004 208004 0 0.0
window-app BRD4187C FLASH 1014572 1014956 384 0.0
RAM 126648 126648 0 0.0
esp32 all-clusters-app c3devkit DRAM 90924 90924 0 0.0
FLASH 1470970 1471016 46 0.0
IRAM 75570 75570 0 0.0
m5stack DRAM 117404 117404 0 0.0
FLASH 1540095 1540147 52 0.0
IRAM 125403 125403 0 0.0
linux air-purifier-app debug unknown 4632 4648 16 0.3
FLASH 2705679 2717215 11536 0.4
RAM 125200 96936 -28264 -22.6
all-clusters-app debug unknown 5400 5400 0 0.0
FLASH 5834028 5851090 17062 0.3
RAM 494560 466488 -28072 -5.7
all-clusters-minimal-app debug unknown 5312 5312 0 0.0
FLASH 5293826 5309994 16168 0.3
RAM 235728 207592 -28136 -11.9
bridge-app debug unknown 5296 5296 0 0.0
FLASH 4694610 4706978 12368 0.3
RAM 212992 184832 -28160 -13.2
chip-tool debug unknown 5784 5784 0 0.0
FLASH 12274738 12276096 1358 0.0
RAM 548050 534538 -13512 -2.5
chip-tool-ipv6only arm64 unknown 20128 20192 64 0.3
FLASH 10983084 10984268 1184 0.0
RAM 597656 584272 -13384 -2.2
fabric-admin debug unknown 5672 5672 0 0.0
FLASH 11295111 1129646 1356 0.0
RAM 544890 531442 -13448 -2.5
fabric-bridge-app debug unknown 4568 4568 0 0.0
FLASH 4448844 4460384 11540 0.3
RAM 199024 170912 -28112 -14.1
lighting-app debug+rpc+ui unknown 5968 5968 0 0.0
FLASH 5618689 5633569 14880 0.3
RAM 224272 196072 -28200 -12.6
lock-app debug unknown 5232 5232 0 0.0
FLASH 4753062 4767064 14002 0.3
RAM 200704 172520 -28184 -14.0
ota-provider-app debug unknown 4608 4608 0 0.0
FLASH 4397556 4411522 13966 0.3
RAM 194880 166776 -28104 -14.4
ota-requestor-app debug unknown 4544 4544 0 0.0
FLASH 4535338 4549408 14070 0.3
RAM 199416 171312 -28104 -14.1
shell debug unknown 4168 4184 16 0.4
FLASH 2978349 2994445 16096 0.5
RAM 153704 125752 -27952 -18.2
thermostat-no-ble arm64 unknown 9208 9480 272 3.0
FLASH 4255716 4260348 4632 0.1
RAM 236336 208632 -27704 -11.7
tv-app debug unknown 5504 5504 0 0.0
FLASH 5970301 5987085 16784 0.3
RAM 573184 545176 -28008 -4.9
tv-casting-app debug unknown 5168 5168 0 0.0
FLASH 1048639 10505677 19280 0.2
RAM 633352 603960 -29392 -4.6
mbed lock-app-release cy8cproto_062_4343w FLASH 1503276 1503364 88 0.0
RAM 226720 226720 0 0.0
nxp contact k32w0+release FLASH 576100 576228 128 0.0
RAM 70104 70104 0 0.0
k32w1+release FLASH 591656 591784 128 0.0
RAM 74144 74144 0 0.0
light k32w0+release FLASH 610400 610568 168 0.0
RAM 69564 69564 0 0.0
k32w1+release FLASH 675192 675288 96 0.0
RAM 82872 82872 0 0.0
psoc6 all-clusters cy8ckit_062s2_43012 FLASH 1611460 1611756 296 0.0
RAM 209728 209728 0 0.0
all-clusters-minimal cy8ckit_062s2_43012 FLASH 1531812 1532092 280 0.0
RAM 206544 206544 0 0.0
light cy8ckit_062s2_43012 FLASH 1458844 1459132 288 0.0
RAM 199808 199808 0 0.0
lock cy8ckit_062s2_43012 FLASH 1459484 1459772 288 0.0
RAM 224328 224328 0 0.0
qpg lighting-app qpg6105+debug FLASH 651724 651852 128 0.0
RAM 104636 104636 0 0.0
lock-app qpg6105+debug FLASH 612120 612248 128 0.0
RAM 99320 99320 0 0.0
stm32 light STM32WB5MM-DK FLASH 474120 474216 96 0.0
RAM 144260 144260 0 0.0
telink air-quality-sensor-app tlsr9528a_retention FLASH 633086 633190 104 0.0
RAM 50576 50576 0 0.0
all-clusters-app tlsr9118bdk40d FLASH 658760 658872 112 0.0
RAM 148480 148480 0 0.0
all-clusters-minimal-app tlsr9528a FLASH 778986 779098 112 0.0
RAM 113260 113260 0 0.0
bridge-app tlsr9258a FLASH 676006 676118 112 0.0
RAM 95344 95344 0 0.0
contact-sensor-app tlsr9528a_retention FLASH 634670 634774 104 0.0
RAM 50620 50620 0 0.0
light-switch-app-ota-shell-factory-data tlsr9528a FLASH 720542 720654 112 0.0
RAM 77196 77196 0 0.0
lighting-app-ota-factory-data tlsr9118bdk40d FLASH 614040 614152 112 0.0
RAM 144684 144684 0 0.0
lighting-app-ota-rpc-factory-data-4mb tlsr9518adk80d FLASH 801844 801956 112 0.0
RAM 103088 103088 0 0.0
lock-app-dfu tlsr9528a FLASH 666498 666610 112 0.0
RAM 69900 69900 0 0.0
ota-requestor-app tlsr9258a FLASH 695368 695480 112 0.0
RAM 95068 95068 0 0.0
pump-app tlsr9518adk80d FLASH 616934 617046 112 0.0
RAM 57000 57000 0 0.0
pump-controller-app tlsr9518adk80d FLASH 607344 607456 112 0.0
RAM 56800 56800 0 0.0
shell tlsr9518adk80d FLASH 466520 466524 4 0.0
RAM 72488 72488 0 0.0
smoke_co_alarm-app tlsr9528a_retention FLASH 641288 641392 104 0.0
RAM 52248 52248 0 0.0
temperature-measurement-app-mars-ota tlsr9518adk80d FLASH 651166 651278 112 0.0
RAM 60436 60436 0 0.0
thermostat tlsr9518adk80d FLASH 626052 626164 112 0.0
RAM 57124 57124 0 0.0
window-covering tlsr9118bdk40d FLASH 519436 519548 112 0.0
RAM 97856 97856 0 0.0
tizen all-clusters-app arm unknown 1588 1588 0 0.0
FLASH 1640500 1642212 1712 0.1
RAM 48708 48708 0 0.0
chip-tool-ubsan arm unknown 2388 2388 0 0.0
FLASH 16290646 16291426 780 0.0
RAM 7153088 7153548 460 0.0

@nk-shelly nk-shelly force-pushed the reduce-static-footprint branch from 847d093 to 7a71686 Compare July 25, 2024 08:22
Copy link
Contributor

@Damian-Nordic Damian-Nordic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking the Global<> path :)

src/platform/Linux/CHIPPlatformConfig.h Outdated Show resolved Hide resolved
Copy link

github-actions bot commented Jul 25, 2024

PR #34316: Size comparison from 34462f1 to 7a71686

Increases above 0.2%:

platform target config section 34462f1 7a71686 change % change
linux air-purifier-app debug unknown 4632 4656 24 0.5
FLASH 2710279 2720081 9802 0.4
all-clusters-app debug FLASH 5838962 5854244 15282 0.3
all-clusters-minimal-app debug FLASH 5298760 5313148 14388 0.3
bridge-app debug FLASH 4699178 4709844 10666 0.2
chip-tool-ipv6only arm64 unknown 20128 20192 64 0.3
fabric-bridge-app debug FLASH 4453464 4463304 9840 0.2
lighting-app debug+rpc+ui FLASH 5623585 5636769 13184 0.2
lock-app debug FLASH 4757630 4769930 12300 0.3
ota-provider-app debug FLASH 4402124 4414390 12266 0.3
ota-requestor-app debug FLASH 4540272 4552610 12338 0.3
shell debug unknown 4168 4192 24 0.6
FLASH 2983165 2997517 14352 0.5
thermostat-no-ble arm64 unknown 9208 9480 272 3.0
tv-app debug FLASH 5974781 5989757 14976 0.3
Full report (82 builds for bl602, bl702, bl702l, cc13x4_26x4, cc32xx, cyw30739, efr32, esp32, linux, mbed, nxp, psoc6, qpg, stm32, telink, tizen)
platform target config section 34462f1 7a71686 change % change
bl602 lighting-app bl602 FLASH 1271964 1272644 680 0.1
RAM 95384 95384 0 0.0
bl602+mfd FLASH 1286222 1286902 680 0.1
RAM 95528 95528 0 0.0
bl602+rpc FLASH 1311188 1311868 680 0.1
RAM 103808 103808 0 0.0
bl702 lighting-app bl702 FLASH 1092858 1093534 676 0.1
RAM 15245 15245 0 0.0
bl702+mfd FLASH 1103808 1104484 676 0.1
RAM 15397 15397 0 0.0
bl702+rpc FLASH 1182924 1183600 676 0.1
RAM 24245 24245 0 0.0
bl706-eth FLASH 876158 876834 676 0.1
RAM 27348 27348 0 0.0
bl706-wifi FLASH 1128598 1129356 758 0.1
RAM 14681 14681 0 0.0
bl702l lighting-app bl702l FLASH 1080012 1080692 680 0.1
RAM 21800 21800 0 0.0
bl702l+mfd FLASH 1091274 1091954 680 0.1
RAM 21960 21960 0 0.0
cc13x4_26x4 lighting-app LP_EM_CC1354P10_6 FLASH 798972 799120 148 0.0
RAM 109236 109236 0 0.0
lock-ftd LP_EM_CC1354P10_6 FLASH 816684 816856 172 0.0
RAM 117020 117020 0 0.0
lock-mtd LP_EM_CC1354P10_6 FLASH 808516 808696 180 0.0
RAM 111308 111308 0 0.0
pump-app LP_EM_CC1354P10_6 FLASH 761132 761296 164 0.0
RAM 105408 105408 0 0.0
pump-controller-app LP_EM_CC1354P10_6 FLASH 746916 747072 156 0.0
RAM 105632 105632 0 0.0
cc32xx air-purifier CC3235SF_LAUNCHXL FLASH 606494 606770 276 0.0
RAM 204564 204564 0 0.0
lock CC3235SF_LAUNCHXL FLASH 652026 652298 272 0.0
RAM 204836 204836 0 0.0
cyw30739 light CYW30739B2-P5-EVK-01 unknown 2040 2040 0 0.0
FLASH 668081 668185 104 0.0
RAM 77700 77700 0 0.0
CYW30739B2-P5-EVK-02 unknown 2040 2040 0 0.0
FLASH 687933 688037 104 0.0
RAM 80340 80340 0 0.0
CYW30739B2-P5-EVK-03 unknown 2040 2040 0 0.0
FLASH 687933 688037 104 0.0
RAM 80340 80340 0 0.0
CYW930739M2EVB-02 unknown 2040 2040 0 0.0
FLASH 644869 644981 112 0.0
RAM 72768 72768 0 0.0
light-switch CYW30739B2-P5-EVK-01 unknown 2040 2040 0 0.0
FLASH 609585 609713 128 0.0
RAM 70884 70884 0 0.0
CYW30739B2-P5-EVK-02 unknown 2040 2040 0 0.0
FLASH 629221 629341 120 0.0
RAM 73428 73428 0 0.0
CYW30739B2-P5-EVK-03 unknown 2040 2040 0 0.0
FLASH 629221 629341 120 0.0
RAM 73428 73428 0 0.0
lock CYW30739B2-P5-EVK-01 unknown 2040 2040 0 0.0
FLASH 625289 625409 120 0.0
RAM 73900 73900 0 0.0
CYW30739B2-P5-EVK-02 unknown 2040 2040 0 0.0
FLASH 644997 645117 120 0.0
RAM 76444 76444 0 0.0
CYW30739B2-P5-EVK-03 unknown 2040 2040 0 0.0
FLASH 644997 645117 120 0.0
RAM 76444 76444 0 0.0
thermostat CYW30739B2-P5-EVK-01 unknown 2040 2040 0 0.0
FLASH 593517 593637 120 0.0
RAM 67852 67852 0 0.0
CYW30739B2-P5-EVK-02 unknown 2040 2040 0 0.0
FLASH 613369 613489 120 0.0
RAM 70492 70492 0 0.0
CYW30739B2-P5-EVK-03 unknown 2040 2040 0 0.0
FLASH 613369 613489 120 0.0
RAM 70492 70492 0 0.0
efr32 lighting-app BRD4187C FLASH 926152 926272 120 0.0
RAM 134528 134528 0 0.0
lock-app BRD4338a FLASH 734524 734908 384 0.1
RAM 208004 208004 0 0.0
window-app BRD4187C FLASH 1014604 1014988 384 0.0
RAM 126648 126648 0 0.0
esp32 all-clusters-app c3devkit DRAM 90948 90948 0 0.0
FLASH 1471128 1471174 46 0.0
IRAM 75570 75570 0 0.0
m5stack DRAM 117428 117428 0 0.0
FLASH 1540255 1540307 52 0.0
IRAM 125403 125403 0 0.0
linux air-purifier-app debug unknown 4632 4656 24 0.5
FLASH 2710279 2720081 9802 0.4
RAM 125200 96936 -28264 -22.6
all-clusters-app debug unknown 5400 5408 8 0.1
FLASH 5838962 5854244 15282 0.3
RAM 494560 466488 -28072 -5.7
all-clusters-minimal-app debug unknown 5312 5320 8 0.2
FLASH 5298760 5313148 14388 0.3
RAM 235728 207592 -28136 -11.9
bridge-app debug unknown 5296 5304 8 0.2
FLASH 4699178 4709844 10666 0.2
RAM 212992 184832 -28160 -13.2
chip-tool debug unknown 5784 5784 0 0.0
FLASH 12279384 12280126 742 0.0
RAM 548050 534538 -13512 -2.5
chip-tool-ipv6only arm64 unknown 20128 20192 64 0.3
FLASH 10987004 10987724 720 0.0
RAM 597680 584296 -13384 -2.2
fabric-admin debug unknown 5672 5672 0 0.0
FLASH 11299943 11300651 708 0.0
RAM 544890 531442 -13448 -2.5
fabric-bridge-app debug unknown 4568 4576 8 0.2
FLASH 4453464 4463304 9840 0.2
RAM 199024 170912 -28112 -14.1
lighting-app debug+rpc+ui unknown 5968 5976 8 0.1
FLASH 5623585 5636769 13184 0.2
RAM 224272 196072 -28200 -12.6
lock-app debug unknown 5232 5240 8 0.2
FLASH 4757630 4769930 12300 0.3
RAM 200704 172520 -28184 -14.0
ota-provider-app debug unknown 4608 4616 8 0.2
FLASH 4402124 4414390 12266 0.3
RAM 194880 166776 -28104 -14.4
ota-requestor-app debug unknown 4544 4552 8 0.2
FLASH 4540272 4552610 12338 0.3
RAM 199416 171312 -28104 -14.1
shell debug unknown 4168 4192 24 0.6
FLASH 2983165 2997517 14352 0.5
RAM 153704 125752 -27952 -18.2
thermostat-no-ble arm64 unknown 9208 9480 272 3.0
FLASH 4259572 4262860 3288 0.1
RAM 236360 208656 -27704 -11.7
tv-app debug unknown 5504 5512 8 0.1
FLASH 5974781 5989757 14976 0.3
RAM 573184 545176 -28008 -4.9
tv-casting-app debug unknown 5168 5168 0 0.0
FLASH 10490877 10508349 17472 0.2
RAM 633352 603960 -29392 -4.6
mbed lock-app-release cy8cproto_062_4343w FLASH 1503276 1503364 88 0.0
RAM 226720 226720 0 0.0
nxp contact k32w0+release FLASH 576100 576244 144 0.0
RAM 70104 70104 0 0.0
k32w1+release FLASH 591672 591800 128 0.0
RAM 74144 74144 0 0.0
light k32w0+release FLASH 610464 610632 168 0.0
RAM 69564 69564 0 0.0
k32w1+release FLASH 675248 675360 112 0.0
RAM 82872 82872 0 0.0
psoc6 all-clusters cy8ckit_062s2_43012 FLASH 1611588 1611868 280 0.0
RAM 209728 209728 0 0.0
all-clusters-minimal cy8ckit_062s2_43012 FLASH 1531940 1532220 280 0.0
RAM 206544 206544 0 0.0
light cy8ckit_062s2_43012 FLASH 1458956 1459244 288 0.0
RAM 199808 199808 0 0.0
lock cy8ckit_062s2_43012 FLASH 1459500 1459788 288 0.0
RAM 224328 224328 0 0.0
qpg lighting-app qpg6105+debug FLASH 651780 651908 128 0.0
RAM 104636 104636 0 0.0
lock-app qpg6105+debug FLASH 612136 612264 128 0.0
RAM 99320 99320 0 0.0
stm32 light STM32WB5MM-DK FLASH 474176 474272 96 0.0
RAM 144260 144260 0 0.0
telink air-quality-sensor-app tlsr9528a_retention FLASH 633112 633216 104 0.0
RAM 50576 50576 0 0.0
all-clusters-app tlsr9118bdk40d FLASH 658826 658938 112 0.0
RAM 148480 148480 0 0.0
all-clusters-minimal-app tlsr9528a FLASH 779052 779164 112 0.0
RAM 113260 113260 0 0.0
bridge-app tlsr9258a FLASH 676032 676144 112 0.0
RAM 95344 95344 0 0.0
contact-sensor-app tlsr9528a_retention FLASH 634696 634800 104 0.0
RAM 50620 50620 0 0.0
light-switch-app-ota-shell-factory-data tlsr9528a FLASH 720568 720680 112 0.0
RAM 77196 77196 0 0.0
lighting-app-ota-factory-data tlsr9118bdk40d FLASH 614100 614212 112 0.0
RAM 144684 144684 0 0.0
lighting-app-ota-rpc-factory-data-4mb tlsr9518adk80d FLASH 801904 802016 112 0.0
RAM 103088 103088 0 0.0
lock-app-dfu tlsr9528a FLASH 666524 666636 112 0.0
RAM 69900 69900 0 0.0
ota-requestor-app tlsr9258a FLASH 695428 695540 112 0.0
RAM 95068 95068 0 0.0
pump-app tlsr9518adk80d FLASH 616960 617072 112 0.0
RAM 57000 57000 0 0.0
pump-controller-app tlsr9518adk80d FLASH 607370 607482 112 0.0
RAM 56800 56800 0 0.0
shell tlsr9518adk80d FLASH 466520 466524 4 0.0
RAM 72488 72488 0 0.0
smoke_co_alarm-app tlsr9528a_retention FLASH 641314 641418 104 0.0
RAM 52248 52248 0 0.0
temperature-measurement-app-mars-ota tlsr9518adk80d FLASH 651192 651304 112 0.0
RAM 60436 60436 0 0.0
thermostat tlsr9518adk80d FLASH 626084 626196 112 0.0
RAM 57124 57124 0 0.0
window-covering tlsr9118bdk40d FLASH 519462 519574 112 0.0
RAM 97856 97856 0 0.0
tizen all-clusters-app arm unknown 1588 1588 0 0.0
FLASH 1640836 1642536 1700 0.1
RAM 48708 48708 0 0.0
chip-tool-ubsan arm unknown 2388 2388 0 0.0
FLASH 16291318 16292098 780 0.0
RAM 7153452 7153912 460 0.0

@nk-shelly nk-shelly force-pushed the reduce-static-footprint branch from 7a71686 to 5bb635a Compare July 25, 2024 11:38
@nk-shelly
Copy link
Author

I only looked at Global.h for now, in case changes there affect how the other bits work, but I tried this PR on Mac with #define CHIP_CONFIG_DYNAMIC_GLOBALS 1 and it fails unit tests with ASAN failures.... So that's worth looking into, perhaps. But maybe after we settle on how Global.h should work.

How can I reproduce this? ninja -C out/host check seems to be passing on my Mac. Is ASAN enabled by default and how should I run the tests with it if not?

@nk-shelly nk-shelly force-pushed the reduce-static-footprint branch from 5bb635a to a405bfd Compare July 26, 2024 07:06
@nk-shelly nk-shelly requested a review from bzbarsky-apple July 26, 2024 07:07
@nk-shelly nk-shelly force-pushed the reduce-static-footprint branch 2 times, most recently from dd9b1f9 to 384e040 Compare July 28, 2024 09:25
@nk-shelly nk-shelly force-pushed the reduce-static-footprint branch from 384e040 to b314b5c Compare July 29, 2024 11:44
{ &sDebugEventBuffer[0], sizeof(sDebugEventBuffer), ::chip::app::PriorityLevel::Debug },
{ &sInfoEventBuffer[0], sizeof(sInfoEventBuffer), ::chip::app::PriorityLevel::Info },
{ &sCritEventBuffer[0], sizeof(sCritEventBuffer), ::chip::app::PriorityLevel::Critical }
{ sDebugEventBuffer.get(), CHIP_DEVICE_CONFIG_EVENT_LOGGING_DEBUG_BUFFER_SIZE, ::chip::app::PriorityLevel::Debug },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can easily get out of sync with the definition of sDebugEventBuffer (and similar in MessageDefHelper). The size() bit on Global was a good idea; it was just operator[] that I didn't think we needed...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having the arrays inside a struct also means sizeof continues to work on the member as expected.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still getting ASAN failures on Mac with this PR if I set

#define CHIP_CONFIG_DYNAMICALLY_ALLOCATED_GLOBALS 1

and then do:

./gn_build.sh is_asan=true enable_host_clang_build=false enable_host_gcc_mbedtls_crypto_tests=false enable_host_clang_boringssl_crypto_tests=false chip_enable_dnssd_tests=false

I prodded at this, and the issue is that at least PlatformManager has the following places where it does not properly initialize members, but that was being covered up by 0-init of static things:

  1. In the Darwin PlatformManagerImpl, mRunLoopSem should be getting initialized to nullptr and isn't. This was the proximate cause of the test failure.
  2. In GenericPlatformManagerImpl, mAppEventHandlerList should be getting initialized to nullptr and isn't. I'm surprised anything real actually passed with that problem.... mMsgLayerWasActive is also not initialized in the constructor, but looks like it gets initialized in _InitChipStack, so that seems OK.

The question is: do any of the other things that this behavior change applies to have these sorts of problems? Could probably use an audit.

@@ -62,7 +62,8 @@ class ReportSchedulerImpl : public ReportScheduler
public:
using Timeout = System::Clock::Timeout;

ReportSchedulerImpl(TimerDelegate * aTimerDelegate);
ReportSchedulerImpl() = default;
ReportSchedulerImpl(TimerDelegate * aTimerDelegate) : ReportScheduler(aTimerDelegate) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove the nullptr check for this constructor?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a default constructor now and an Init method to set the timer delegate ptr. This constructor is probably only used in some tests. The idea that once on object is constructed there will be a non-null pointer to a timer delegate in it is not valid anymore, so why keep that check?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How and where would the Init method get called is the constructor using a TimerDelegate is used?

So far, it doesn't seem it happens. Also, if the constructor accepts a TimerDelegate, how is the Init method supposed to be used?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Init() method is only used right above your other comment (#34316) and only for the sReportScheduler in CommonCaseDeviceServerInitParams, which is always default constructed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which is why the nullptr check should remain in this constructor right? Otherwise the other places in the code using something else than the default constructor just lost their nullptr check.

}

// Injection of report scheduler WILL lead to two schedulers being allocated. As recommended above, this should only be used
// for IN-TREE examples. If a default scheduler is desired, the basic ServerInitParams should be used by the application and
// CommonCaseDeviceServerInitParams should not be allocated.
if (this->reportScheduler == nullptr)
{
reportScheduler = &sReportScheduler;
sReportScheduler->Init(&sTimerDelegate.get());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not get called on injection of a report scheduler, we can either keep the nullptr check in the constructor that takes a TimerDelegate as a param, or have an EnsureInit that takes no parameter and that gets called in an else condition here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I get your point. Why initialise that global at all, especially when it's not going to be used? The whole point of this PR is to allow us to not even allocate the memory for it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this->reportScheduler == nullptr only checks if there were no report Scheduler injected. If this check fails, that means the reportScheduler was injected somewhere else, so why not check that it is initialized?

something like:
reportScheduler.EnsureInit() to make sure that whatever is injected has been properly generated?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't the same logic apply to all other things that can be injected? My impression was you need to Init() them before injecting (in this case you can alternatively construct it with a pointer). I don't see EnsureInit() used for any of the other ones...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As noted above, I think we should avoid changing how ReportScheduler is initialized.

ReportScheduler(TimerDelegate * aTimerDelegate) : mTimerDelegate(aTimerDelegate) {}

virtual ~ReportScheduler() = default;

void Init(TimerDelegate * aTimerDelegate)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this method supposed to be used with this constructor:
ReportScheduler(TimerDelegate * aTimerDelegate) : mTimerDelegate(aTimerDelegate) {}?

It would overwrite the TimerDelegate previously used, which defeats the purpose of having a constructor that accepts a TimerDelegate.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, right, Global doesn't do anything except default constructors, so I added this Init method instead. I didn't delete the constructor with the pointer as I suspect it's already used by people. The intention was you can use the default constructor and then Init() later, or you can use the constructor with a pointer, in which case you don't need to Init().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the better pattern is to have the Global be of a sub-class or wrapper struct with the default constructor we want.

Copy link
Contributor

@ksperling-apple ksperling-apple left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think overall this is looking really good, just a few comments.

Comment on lines 40 to 42
uint32_t gPrettyPrintingDepthLevel = 0;
char gLineBuffer[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
Global<char[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE]> gLineBuffer;
size_t gCurLineBufferSize = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nicer to do something like

struct PrettyPrintState {
    uint32_t mDepthLevel = 0;
    char mLineBuffer[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
    size_t mCurLineBufferSize = 0;
};
Global<PrettyPrintState> gPrettyPrint;

We can probably avoid adding support for arrays to Global that way too.

Copy link
Contributor

@ksperling-apple ksperling-apple Jul 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern also avoids the overhead for the extra Globals in the lazy / dynamic cases.

static uint8_t sCritEventBuffer[CHIP_DEVICE_CONFIG_EVENT_LOGGING_CRIT_BUFFER_SIZE];
static ::chip::PersistedCounter<chip::EventNumber> sGlobalEventIdCounter;
static ::chip::app::CircularEventBuffer sLoggingBuffer[CHIP_NUM_EVENT_LOGGING_BUFFERS];
static Global<uint8_t[CHIP_DEVICE_CONFIG_EVENT_LOGGING_INFO_BUFFER_SIZE]> sInfoEventBuffer;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, maybe these should be grouped into a struct constructed by a single Global ?

{ &sDebugEventBuffer[0], sizeof(sDebugEventBuffer), ::chip::app::PriorityLevel::Debug },
{ &sInfoEventBuffer[0], sizeof(sInfoEventBuffer), ::chip::app::PriorityLevel::Info },
{ &sCritEventBuffer[0], sizeof(sCritEventBuffer), ::chip::app::PriorityLevel::Critical }
{ sDebugEventBuffer.get(), CHIP_DEVICE_CONFIG_EVENT_LOGGING_DEBUG_BUFFER_SIZE, ::chip::app::PriorityLevel::Debug },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having the arrays inside a struct also means sizeof continues to work on the member as expected.

app::DefaultTimerDelegate CommonCaseDeviceServerInitParams::sTimerDelegate;
app::reporting::ReportSchedulerImpl
CommonCaseDeviceServerInitParams::sReportScheduler(&CommonCaseDeviceServerInitParams::sTimerDelegate);
Global<KvsPersistentStorageDelegate> CommonCaseDeviceServerInitParams::sKvsPersistenStorageDelegate;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of all these separate globals, you can create a DefaultCommonCaseDeviceServerInitParams class inside Server.cpp (could be a sub-class of CommonCaseDeviceServerInitParams I think) with an appropriate default constructor, and a single Global constructing that. Then reference that global in InitializeStaticResourcesBeforeServerInit (which should probably move into the .cpp file) instead of those individual globals.

This also avoids having to add an Init method to ReportScheduler.

(A simpler existing example of this pattern is struct ControllerAccessControl.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about this more, it may be worthwhile to look at some of those individual default implementation objects and see how large they are, to determine if it makes sense to keep them as separate Globals. However the pattern of wrapping or sub-classing the ReportScheduler should still be used to avoid having to change the initialization.

}

// Injection of report scheduler WILL lead to two schedulers being allocated. As recommended above, this should only be used
// for IN-TREE examples. If a default scheduler is desired, the basic ServerInitParams should be used by the application and
// CommonCaseDeviceServerInitParams should not be allocated.
if (this->reportScheduler == nullptr)
{
reportScheduler = &sReportScheduler;
sReportScheduler->Init(&sTimerDelegate.get());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As noted above, I think we should avoid changing how ReportScheduler is initialized.

@@ -33,8 +34,8 @@ namespace Internal {
template <class ImplClass>
chip::Inet::EndPointManager<Inet::UDPEndPoint> & GenericConnectivityManagerImpl_UDP<ImplClass>::_UDPEndPointManager()
{
static chip::Inet::UDPEndPointManagerImpl sUDPEndPointManagerImpl;
return sUDPEndPointManagerImpl;
static Global<chip::Inet::UDPEndPointManagerImpl> sUDPEndPointManagerImpl;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move the Global out of the method, otherwise we end up with the compiler-generated lazy init + whatever Global does.

*
* The default is to use static allocation.
*/
#ifndef CHIP_CONFIG_DYNAMICALLY_ALLOCATED_GLOBALS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we call rename this to CHIP_CONFIG_GLOBALS_DYNAMIC, or something that starts with CHIP_CONFIG_GLOBALS_ anyway?

This also semantically implies CHIP_CONFIG_GLOBALS_LAZY_INIT and/or conflicts with that setting if we take CHIP_CONFIG_GLOBALS_LAZY_INIT to mean "static but lazy". Maybe the simplest thing is to just check if CHIP_CONFIG_GLOBALS_DYNAMIC and CHIP_CONFIG_GLOBALS_LAZY_INIT are both 1 and #error if so.

@@ -97,25 +97,70 @@ class Global
~Global() = default;

private:
#if CHIP_CONFIG_DYNAMICALLY_ALLOCATED_GLOBALS
std::remove_extent_t<T> * mStorage = nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There don't seem to be any strong use cases for Global with arrays, so I would prefer to just static_assert here that T isn't an array and have the code be simpler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants