8000 esp32/modesp32: Implement idf_task_stats(). by DvdGiessen · Pull Request #12732 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

esp32/modesp32: Implement idf_task_stats(). #12732

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

Merged
merged 1 commit into from
May 16, 2025

Conversation

DvdGiessen
Copy link
Contributor
@DvdGiessen DvdGiessen commented Oct 18, 2023

This adds a new function, esp32.idf_task_stats(), that can be used to retrieve task statistics which is useful for diagnosing issues where some tasks are using up a lot of CPU time. For example, I originally found #8351 and #12728 using this function.

An animated GIF is worth a thousand words:

print_task_stats

(See also micropython/micropython-lib#822 which implements the Python function for this.)

By default the functionality is not enabled; it depends on a few extra option to be set. I have the following snippet in my sdkconfig.board files:

# Uncomment to enable esp32.idf_task_stats()
#CONFIG_FREERTOS_USE_TRACE_FACILITY=y
#CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID=y
#CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS=y

If the required FreeRTOS functionality is available, the idf_task_stats() function is automatically enabled.

@dpgeorge
Copy link
Member

This looks pretty neat! The zephyr port already has zephyr.thread_analyze() which I think just dumps all tasks, but what you have here is more useful.

I think this would be a good (optional) addition.

@DvdGiessen DvdGiessen force-pushed the esp32_idf_task_stats branch from 1982e18 to e2316bd Compare November 1, 2023 16:08
@DvdGiessen
Copy link
Contributor Author
DvdGiessen commented Nov 1, 2023

Thank you for the feedback! I've updated the branch to address them.

One thing I'm not sure about is the rather large code snippet (compared to snippets elsewhere in the documentation) that implements the print_task_stats() function. I suppose is a useful example, but not sure this is the right place for it. Though it is easily discoverable this way.

@DvdGiessen DvdGiessen marked this pull request as ready for review November 1, 2023 19:44
@dpgeorge
Copy link
Member
dpgeorge commented Nov 2, 2023

One thing I'm not sure about is the rather large code snippet (compared to snippets elsewhere in the documentation) that implements the print_task_stats() function. I suppose is a useful example, but not sure this is the right place for it

It could be added to examples/... in this repo. But maybe it could make sense to put it in micropython-lib as an esp32 tool? Easy to install it then on a board.

@DvdGiessen
Copy link
Contributor Author

Updated to move the code snippet from the documentation to a separate function in micropython-lib: micropython/micropython-lib#822

@DvdGiessen DvdGiessen force-pushed the esp32_idf_task_stats branch from 8ddc267 to 5842015 Compare August 31, 2024 23:03
@DvdGiessen DvdGiessen force-pushed the esp32_idf_task_stats branch from 5842015 to 65310c7 Compare October 24, 2024 20:11
@DvdGiessen DvdGiessen force-pushed the esp32_idf_task_stats branch from 65310c7 to fa7c95b Compare April 16, 2025 14:14
@DvdGiessen
Copy link
Contributor Author

Note: The build failure is due to the linked PR for micropython-lib not being merged, and thus the docs are referring to a file that does not yet exist on the micropython-lib master branch.

@DvdGiessen DvdGiessen force-pushed the esp32_idf_task_stats branch from fa7c95b to 61cd01d Compare April 24, 2025 10:26
@DvdGiessen DvdGiessen force-pushed the esp32_idf_task_stats branch from 61cd01d to aad79b4 Compare May 14, 2025 13:19
#if CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID
MP_OBJ_NEW_SMALL_INT(task_array[i].xCoreID),
#else
mp_const_none,
Copy link
Member

Choose a reason for hiding this comment

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

The utop module expects this entry to be an integer, so if CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID is disabled then utop crashes.

Maybe -1 can be used here instead of None, so that it's uniformly an integer?

(I did see that FreeRTOS returns -1 for some tasks' xCoreID, such as "Tmr Svc", so maybe a different sentinel integer like 255, or -255?)

Copy link
Contributor Author
@DvdGiessen DvdGiessen May 15, 2025

Choose a reason for hiding this comment

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

Note: -1 is the constant tskNO_AFFINITY, meaning the task is not pinned to one specific core. Mapping it in utop (perhaps listing it as any instead of -1?) could make that clearer for the casual user.

Partly because there's already different integer values to interpret I think explicitly using None makes it clearer that the value is not available at all. I've now explicitly documented that behaviour and fixed utop to correctly handle that. If you're getting an integer value, that's a valid value returned by the IDF, and when you get None you have not build with the required configuration options. Let me know what you think!

Copy link
Member

Choose a reason for hiding this comment

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

OK, I think that's fine to keep it as None when the feature is not supported.

< 8000 !-- '"` -->

@DvdGiessen DvdGiessen force-pushed the esp32_idf_task_stats branch from aad79b4 to 0b1d2b0 Compare May 15, 2025 10:40
@dpgeorge dpgeorge force-pushed the esp32_idf_task_stats branch 2 times, most recently from e91357e to 045d785 Compare May 16, 2025 06:38
This adds a new function, `esp32.idf_task_info()`, that can be used to
retrieve task statistics which is useful for diagnosing issues where some
tasks are using up a lot of CPU time.

It's best used in conjunction with the `utop` module from micropython-lib.

Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
@dpgeorge dpgeorge force-pushed the esp32_idf_task_stats branch from 045d785 to 06d8c08 Compare May 16, 2025 06:50
@dpgeorge dpgeorge merged commit 06d8c08 into micropython:master May 16, 2025
9 checks passed
8000
@dpgeorge
Copy link
Member

This works really well, thank you!

FWIW, enabling it costs around 1k of flash.

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.

2 participants
0