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

Pthread uses RAM and flash even not used (IDFGH-13283) #14213

Closed
3 tasks done
jrahlf opened this issue Jul 18, 2024 · 3 comments
Closed
3 tasks done

Pthread uses RAM and flash even not used (IDFGH-13283) #14213

jrahlf opened this issue Jul 18, 2024 · 3 comments
Assignees
Labels
Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally Type: Bug bugs in IDF

Comments

@jrahlf
Copy link

jrahlf commented Jul 18, 2024

Answers checklist.

  • I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
  • I have updated my IDF branch (master or release) to the latest version and checked that the issue is present there.
  • I have searched the issue tracker for a similar issue and not found a similar issue.

IDF version.

v5.1.1

Operating System used.

Windows

How did you build your project?

Command line with Make

If you are using Windows, please specify command line type.

None

What is the expected behavior?

  • I expected pthread library not to use any RAM and flash when the feature is not used.
  • There should be a KConfig option to disable pthreads when not needed
  • Unused pthread symbols should be stripped

What is the actual behavior?

  • startup.c calls esp_pthread_init() even when pthread is not used.
  • pthread unused functions do not get stripped

Steps to reproduce.

  • Delete all code in pthread/pthread.c, newlib/pthread.c, pthread/pthread_rwlock.c, pthread/local_storage.c, pthread/pthread_cond_var.c, and remove call to esp_pthread_init() in startup.c and compare binary size with regular build.
  • Check the map file for pthread symbols.

Build or installation Logs.

python -m esp_idf_size --diff .\no-pthread.map .\with-pthread.map
<CURRENT> MAP file: .\with-pthread.map
<REFERENCE> MAP file: .\no-pthread.map
Difference is counted as <CURRENT> - <REFERENCE>,           i.e. a positive number means that <CURRENT> is larger.
Total sizes of <CURRENT>:                                       <REFERENCE>     Difference
Used static IRAM:  105042 bytes ( 257198 remain, 29.0% used)         104666           +376 (   -376 remain,      +0 total)
      .text size:  104015 bytes                                      103639           +376
   .vectors size:    1027 bytes                                        1027
Used stat D/IRAM:  177832 bytes ( 168024 remain, 51.4% used)         177808            +24 (    -24 remain,      +0 total)
      .data size:   17216 bytes                                       17200            +16
      .bss  size:  160616 bytes                                      160608             +8
Used Flash size : 1078247 bytes                                     1077335           +912
           .text:  706411 bytes                                      705979           +432
         .rodata:  371580 bytes                                      371100           +480
Total image size: 1200505 bytes (.bin may be padded larger)         1199201          +1304

There will also be savings in heap space which are not shown here.

More Information.

Even with pthread linked in, but completely unused, the functions remain in the final binary. This can be seen via the map file:

...
pthread_create                                    esp-idf/pthread/libpthread.a(pthread.c.obj)
pthread_detach                                    esp-idf/pthread/libpthread.a(pthread.c.obj)

This shows that pthread_create is not used anywhere outside its own compilation unit, but it does not get stripped. Why not?

@jrahlf jrahlf added the Type: Bug bugs in IDF label Jul 18, 2024
@github-actions github-actions bot changed the title Pthread uses RAM and flash even not used Pthread uses RAM and flash even not used (IDFGH-13283) Jul 18, 2024
@espressif-bot espressif-bot added the Status: Opened Issue is new label Jul 18, 2024
@stkw0
Copy link
Contributor

stkw0 commented Jul 25, 2024

There should be a KConfig option to disable pthreads when not needed

In case it helps, you can define your COMPONENTS and not include pthread component. This way you can avoid using pthreads.

@jrahlf
Copy link
Author

jrahlf commented Aug 1, 2024

Not depending on pthread cannot eliminate the esp_pthread_init() call in startup.c.
Maybe it helps in stripping the pthread_create function symbol from the binary.

The above diff shows that 380Bytes IRAM can be saved by eliminating the unused pthread stuff, so I hope this will get fixed in the future.

@ESP-Marius
Copy link
Collaborator

We'll improve this. The situation is better on master (only pulls in 300 bytes of extra flash code), but I think we can make it even better than that, even without adding a kconfig option.

@espressif-bot espressif-bot added Status: In Progress Work is in progress and removed Status: Opened Issue is new labels Aug 8, 2024
@espressif-bot espressif-bot added Status: Reviewing Issue is being reviewed and removed Status: In Progress Work is in progress labels Aug 15, 2024
@espressif-bot espressif-bot added Status: Done Issue is done internally Resolution: NA Issue resolution is unavailable and removed Status: Reviewing Issue is being reviewed labels Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally Type: Bug bugs in IDF
Projects
None yet
Development

No branches or pull requests

4 participants