Skip to content

Fix binary bloat when using local static vars - #444

Closed
kolyshkin wants to merge 1 commit into
rogerclarkmelbourne:masterfrom
kolyshkin:bloat
Closed

kolyshkin wants to merge 1 commit into
rogerclarkmelbourne:masterfrom
kolyshkin:bloat

Conversation

@kolyshkin

@kolyshkin kolyshkin commented Feb 16, 2018 •

Copy link
Copy Markdown
Contributor

In case static local variables are used anywhere in the C++ code of the sketch or the libraries, gcc kicks in to protect initializing those from any potential threads being run in parallel.

The implementation of the above is to emit calls to __cxa_guard_acquire() and __cxa_guard_release(). These functions are part of libstdc++ [1], and so, once a local static variable appears, the whole libstdc++ is linked into the binary with all its bells and whistles, greatly increasing the binary size as a result.

One possible solution to that was proposed in [2], but it dealt with a single consequence of linking in libstdc++ (adding the __verbose_terminate_handler()).

This commits deals with the reason of libstdc++ linking. A gcc option -fno-threadsafe-statics is disabling the above local statics protection mechanism, as a result libstdc++ is not linked in implicitly.

Resulting binary size comparison using a sketch from FastLED examples (as reported by Arduino IDE, in bytes; the first number is "program storage space", the second is "dynamic memory"):

Before: 75160 / 6392
After:  14964 / 3152

For comparison, here are the numbers after applying the commit from [2]:

PR#28:	29592 / 6328

Finally, the resulting sketch is working just fine.

[1] https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/libsupc%2B%2B/guard.cc
[2] #28

In case static local variables are used anywhere in the C++ code of
the sketch or the libraries, gcc kicks in to protect initializing
those from any potential threads being run in parallel.

The implementation of the above is to emit calls to __cxa_guard_acquire()
and __cxa_guard_release(). These functions are part of libstdc++ [1],
and so, once a local static variable appears, the whole libstdc++ is
linked into the binary with all its bells and whistles, greatly
increasing the binary size as a result.

One possible solution to that was proposed in [2], but it dealt with
a single consequence of linking in libstdc++ (adding the
__verbose_terminate_handler()).

This commits deals with the reason of libstdc++ linking. A gcc option
-fno-threadsafe-statics is disabling the above local statics protection
mechanism, as a result libstdc++ is not linked in.

Resulting binary size comparison using a sketch from FastLED examples
(as reported by Arduino IDE, in bytes; the first number is "program
storage space", the second is "dynamic memory"):

Before: 75160 / 6392
After:  14964 / 3152

For comparison, here are the numbers after applying the commit from [2]:

PR#28:	29592 / 6328

Finally, the resulting sketch is working just fine.

[1] https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/libsupc%2B%2B/guard.cc
[2] rogerclarkmelbourne#28

Signed-off-by: Kir Kolyshkin <[email protected]>
@RickKimball

RickKimball commented Feb 16, 2018 •

Copy link
Copy Markdown
Contributor

This already got rejected because some users like FreeRTOS

#296 (comment)
#296 (comment)

@kolyshkin

Copy link
Copy Markdown
Contributor Author

This already got rejected because some users like FreeRTOS

Thanks for letting me know @RickKimball, I'll discuss it there.

@rogerclarkmelbourne

Copy link
Copy Markdown
Owner

I will close this as it has compatibility issues.

Please open a new PR if this can be done and still support RTOS

@kolyshkin

Copy link
Copy Markdown
Contributor Author

There was an extensive discussion at #296, which I just re-read, and my overall understanding is there is a claim that

The solution could harm if using an RTOS

with no substantial proof to it. I tried very hard to find it, but I'm not an RTOS user :(

So, can we maybe revisit this PR and, unless proven that it breaks RTOS, consider merging it?

@focalintent

Copy link
Copy Markdown

Make it a menu option in boards.txt - for example, to tack it onto the optimizations (it's the first 3 lines below):

#-- Optimizations
maple.menu.opt.osnothread=Smallest (no thread)
maple.menu.opt.osnothread.build.flags.optimize=-Os -fno-threadsafe-statics
maple.menu.opt.osnothread.build.flags.ldspecs=
maple.menu.opt.osstd=Smallest (default)
maple.menu.opt.osstd.build.flags.optimize=-Os
maple.menu.opt.osstd.build.flags.ldspecs=
maple.menu.opt.oslto=Smallest Code with LTO
maple.menu.opt.oslto.build.flags.optimize=-Os -flto
maple.menu.opt.oslto.build.flags.ldspecs=-flto
maple.menu.opt.o1std=Fast (-O1)
maple.menu.opt.o1std.build.flags.optimize=-O1
maple.menu.opt.o1std.build.flags.ldspecs=
maple.menu.opt.o1lto=Fast (-O1) with LTO
maple.menu.opt.o1lto.build.flags.optimize=-O1 -flto
maple.menu.opt.o1lto.build.flags.ldspecs=-flto
maple.menu.opt.o2std=Faster (-O2)
maple.menu.opt.o2std.build.flags.optimize=-O2
maple.menu.opt.o2std.build.flags.ldspecs=
maple.menu.opt.o2lto=Faster (-O2) with LTO
maple.menu.opt.o2lto.build.flags.optimize=-O2 -flto
maple.menu.opt.o2lto.build.flags.ldspecs=-flto
maple.menu.opt.o3std=Fastest (-O3)
maple.menu.opt.o3std.build.flags.optimize=-O3
maple.menu.opt.o3std.build.flags.ldspecs=
maple.menu.opt.o3lto=Fastest (-O3) with LTO
maple.menu.opt.o3lto.build.flags.optimize=-O3 -flto
maple.menu.opt.o3lto.build.flags.ldspecs=-flto
maple.menu.opt.ogstd=Debug (-g)
maple.menu.opt.ogstd.build.flags.optimize=-Og
maple.menu.opt.ogstd.build.flags.ldspecs=

Or you can add a new menu where folks can select between things - but it doesn't have to be an all or nothing option.

@rogerclarkmelbourne

Copy link
Copy Markdown
Owner

Honestly I think this sill confuse most people.

Also it will probably default to that option, hence anyone using RTOS could theoretically have problems next time they compile and run their code.

@kolyshkin

Copy link
Copy Markdown
Contributor Author

I suggest to have discussions at a single place -- so far the most of it is at #296

@AndKe

AndKe commented Feb 22, 2019

Copy link
Copy Markdown

@kolyshkin - I get that I need to add -fno-threadsafe-statics , but where and how ? - can it be done in a sketch ?

@RickKimball

Copy link
Copy Markdown
Contributor

@AndKe you need to modify the STM32F1/platform.txt file.

@AndKe

AndKe commented Feb 24, 2019

Copy link
Copy Markdown

@RickKimball yes, the thing is, I have no platform.txt inside a STM32F1 directory:

andre@loke:~/Arduino$ find . -name "platform.txt"
./arduino-1.8.8/hardware/arduino/avr/platform.txt
./arduino-1.8.8/hardware/platform.txt

@RickKimball

RickKimball commented Feb 25, 2019 •

Copy link
Copy Markdown
Contributor

Where is your core installed? If you turn on verbose compiler output the console window shows where your core is located. In my case:

Using board 'genericSTM32F103C' from platform in folder: /home/user/arduino-1.8.8/hardware/Arduino_STM32/STM32F1

so my platform.txt is found in:
/home/user/arduino-1.8.8/hardware/Arduino_STM32/STM32F1/platform.txt

@AndKe

AndKe commented Feb 27, 2019

Copy link
Copy Markdown

Thanks mine is:

Using board 'genericSTM32F103C' from platform in folder: /home/andre/.arduino15/packages/stm32duino/hardware/STM32F1/2019.2.4

Surprised to see ."arduino15" directory ... need to find out what it is good for :)

@RickKimball

Copy link
Copy Markdown
Contributor

You must have used Dan Drown's json file to install. If you had installed using github, you would have known where you put it. When you use the json file. You may or maynot be using what is in github.

@AndKe

AndKe commented Feb 27, 2019

Copy link
Copy Markdown

@RickKimball I've modified ~/.arduino15/packages/stm32duino/hardware/STM32F1/2019.2.4/platform.txt as needed, but compiler does not seem to get it:



/home/andre/Arduino/arduino-1.8.8/arduino-builder -dump-prefs -logger=machine -hardware /home/andre/Arduino/arduino-1.8.8/hardware -hardware /home/andre/.arduino15/packages -hardware /home/andre/Arduino/projects/hardware -tools /home/andre/Arduino/arduino-1.8.8/tools-builder -tools /home/andre/Arduino/arduino-1.8.8/hardware/tools/avr -tools /home/andre/.arduino15/packages -built-in-libraries /home/andre/Arduino/arduino-1.8.8/libraries -libraries /home/andre/Arduino/projects/libraries -fqbn=stm32duino:STM32F1:genericSTM32F103C:device_variant=STM32F103CB,upload_method=DFUUploadMethod,cpu_speed=speed_72mhz,opt=osstd -ide-version=10808 -build-path /tmp/arduino -warnings=default -build-cache /tmp/arduino_cache_97868 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arm-none-eabi-gcc.path=/home/andre/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1 -prefs=runtime.tools.arm-none-eabi-gcc-4.8.3-2014q1.path=/home/andre/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1 -prefs=runtime.tools.stm32tools.path=/home/andre/.arduino15/packages/stm32duino/tools/stm32tools/2019.2.4 -prefs=runtime.tools.stm32tools-2019.2.4.path=/home/andre/.arduino15/packages/stm32duino/tools/stm32tools/2019.2.4 -verbose /tmp/arduino_modified_sketch_487667/ColorPalette.ino
/home/andre/Arduino/arduino-1.8.8/arduino-builder -compile -logger=machine -hardware /home/andre/Arduino/arduino-1.8.8/hardware -hardware /home/andre/.arduino15/packages -hardware /home/andre/Arduino/projects/hardware -tools /home/andre/Arduino/arduino-1.8.8/tools-builder -tools /home/andre/Arduino/arduino-1.8.8/hardware/tools/avr -tools /home/andre/.arduino15/packages -built-in-libraries /home/andre/Arduino/arduino-1.8.8/libraries -libraries /home/andre/Arduino/projects/libraries -fqbn=stm32duino:STM32F1:genericSTM32F103C:device_variant=STM32F103CB,upload_method=DFUUploadMethod,cpu_speed=speed_72mhz,opt=osstd -ide-version=10808 -build-path /tmp/arduino -warnings=default -build-cache /tmp/arduino_cache_97868 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arm-none-eabi-gcc.path=/home/andre/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1 -prefs=runtime.tools.arm-none-eabi-gcc-4.8.3-2014q1.path=/home/andre/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1 -prefs=runtime.tools.stm32tools.path=/home/andre/.arduino15/packages/stm32duino/tools/stm32tools/2019.2.4 -prefs=runtime.tools.stm32tools-2019.2.4.path=/home/andre/.arduino15/packages/stm32duino/tools/stm32tools/2019.2.4 -verbose /tmp/arduino_modified_sketch_487667/ColorPalette.ino
Using board 'genericSTM32F103C' from platform in folder: /home/andre/.arduino15/packages/stm32duino/hardware/STM32F1/2019.2.4
Using core 'maple' from platform in folder: /home/andre/.arduino15/packages/stm32duino/hardware/STM32F1/2019.2.4
Detecting libraries used...
/home/andre/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/arm-none-eabi-g++ -c -g -Os -w -DDEBUG_LEVEL=DEBUG_NONE -std=gnu++11 -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -DBOARD_generic_stm32f103c -DVECT_TAB_ADDR=0x8002000 -DERROR_LED_PORT=GPIOC -DERROR_LED_PIN=13 -w -x c++ -E -CC -mcpu=cortex-m3 -DF_CPU=72000000L -DARDUINO=10808 -DARDUINO_GENERIC_STM32F103C -DARDUINO_ARCH_STM32F1 -DSERIAL_USB -DGENERIC_BOOTLOADER -DMCU_STM32F103CB -mthumb -march=armv7-m -D__STM32F1__ -DARDUINO_ARCH_STM32 -DMCU_STM32F103CB -mthumb -march=armv7-m -D__STM32F1__ -DARDUINO_ARCH_STM32 -I/home/andre/.arduino15/packages/stm32duino/hardware/STM32F1/2019.2.4/system/libmaple -I/home/andre/.arduino15/packages/stm32duino/hardware/STM32F1/2019.2.4/system/libmaple/include -I/home/andre/.arduino15/packages/stm32duino/hardware/STM32F1/2019.2.4/system/libmaple/stm32f1/include -I/home/andre/.arduino15/packages/stm32duino/hardware/STM32F1/2019.2.4/system/libmaple/usb/stm32f1 -I/home/andre/.arduino15/packages/stm32duino/hardware/STM32F1/2019.2.4/system/libmaple/usb/usb_lib -I/home/andre/.arduino15/packages/stm32duino/hardware/STM32F1/2019.2.4/cores/maple -I/home/andre/.arduino15/packages/stm32duino/hardware/STM32F1/2019.2.4/variants/generic_stm32f103c /tmp/arduino/sketch/ColorPalette.ino.cpp -o /dev/null
/home/andre/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/arm-none-eabi-g++ -c -g -Os -w -DDEBUG_LEVEL=DEBUG_NONE -std=gnu++11 -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -DBOARD_generic_stm32f103c -DVECT_TAB_ADDR=0x8002000 -DERROR_LED_PORT=GPIOC -DERROR_LED_PIN=13 -w -x c++ -E -CC -mcpu=cortex-m3 -DF_CPU=72000000L -DARDUINO=10808 -DARDUINO_GENERIC_STM32F103C -DARDUINO_ARCH_STM32F1 -DSERIAL_USB -DGENERIC_BOOTLOADER -DMCU_STM32F103CB -mthumb -march=armv7-m -D__STM32F1__ -DARDUINO_ARCH_STM32 -DMCU_STM32F103CB -mthumb -march=armv7-m -D__STM32F1__ -DARDUINO_ARCH_STM32 -I/home/andre/.arduino15/packages/stm32duino/hardware/STM32F1/2019.2.4/system/libmaple -I/home/andre/.arduino15/packages/stm32duino/hardware/STM32F1/2019.2.4/system/libmaple/include -I/home/andre/.arduino15/packages/stm32duino/hardware/STM32F1/2019.2.4/system/libmaple/stm32f1/include -I/home/andre/.arduino15/packages/stm32duino/hardware/STM32F1/2019.2.4/system/libmaple/usb/stm32f1 -I/home/andre/.arduino15/packages/stm32duino/hardware/STM32F1/2019.2.4/system/libmaple/usb/usb_lib -I/home/andre/.arduino15/packages/stm32duino/hardware/STM32F1/2019.2.4/cores/maple -I/home/andre/.arduino15/packages/stm32duino/hardware/STM32F1/2019.2.4/variants/generic_stm32f103c -I/home/andre/Arduino/projects/libraries/FastLED /tmp/arduino/sketch/ColorPalette.ino.cpp -o /dev/null
Using cached library dependencies for file: /home/andre/Arduino/projects/libraries/FastLED/FastLED.cpp
Using cached library dependencies for file: /home/andre/Arduino/projects/libraries/FastLED/bitswap.cpp

is there some sort of caching that survives Arduino application restart ?

@RickKimball

RickKimball commented Feb 27, 2019 •

Copy link
Copy Markdown
Contributor

If you quit the arduino ide and restart it will use the latest platform.txt. What version of the Arduino IDE are you using?

Never mind, I see you are using 1.8.8 that should be fine.

@AndKe

AndKe commented Feb 27, 2019

Copy link
Copy Markdown

v1.8.8

@AndKe

AndKe commented Feb 27, 2019

Copy link
Copy Markdown

@RickKimball Thank you.
I updated the library from the usual source, and now the edited platform.txt works as expected.

now I can fully enjoy trying to figure out why FastLED does not work at all with Blue Pill. :)

@rogerclarkmelbourne

Copy link
Copy Markdown
Owner

The issue with thread safe static is being discussed in several issues at the moment.

e.g.

#513

I suspect the final solution will be to default to no-treadsafe-statics, and have a build option for thread safe statics, in the optimisation settings

e.g. -Os -no-threadsafe-statics will be the default, and there will be a separate option for -Os on its own, which RTOS users need to select
All higher levels of optimisation will also be set as no-threadsafe-statics, hence RTOS users who want higher levels of optimisation will need to manually modify their platform.txt files, as its not possible to cater for 100% of the users, without a unusably large and complex number of build setting menus

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants