-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
Statistics component causes significant slowdown #132597
Comments
Hey there @ThomDietrich, mind taking a look at this issue as it has been labeled with an integration ( Code owner commandsCode owners of
(message by CodeOwnersMention) statistics documentation |
@dominikandreas I think I have same issue. Updated to 2024.12.1 on Sunday. Then noticed, that host - Intel NUC fans kicks in regularly. Added CPU sensor in HA and can see, that CPU spikes from 2% usage to 7-8% usage every 5 minutes. P.S. my initial topic with initial troubleshooting: https://community.home-assistant.io/t/small-cpu-spikings-after-os-14-0-and-core-2024-12-01/807509/3 |
@vukisz I used the profiler integration to generate a cprof file (this gets stored in your config directory) with the profiler start action (I used 60 seconds as the duration), then copied it over to my local machine and used Btw your spikes could also be caused by recent changes of the supervisor, also causing high CPU load: |
If someone wants to finish #110438 I suspect that will solve the issue |
@bdraco I wasn't aware of this PR, thanks! To me this looks good, pretty much what I had in mind as well. Not sure if I would actually change anything, probably makes sense to just reopen the PR and maybe add some minor comments / naming changes to make @ThomDietrich happy :) |
@dominikandreas Feel free to start a fresh PR with that as a base commit and adjust as needed |
I started looking at this yesterday given this issue, so I started with #132648 to move it around a bit first. |
@gjohansson-ST thanks! |
Yeah, my PR was only a prep to move the methods outside of the class (very minor). I will look into the substitution of datetime to timestamp once that has been merged in. |
The relevant code has been merged so either if you can test your setup using dev code as a custom component or we leave the issue open until 2025.1 |
@gjohansson-ST I would really like to test it and provide profiler results. But I have never tried testing unreleased code as custom component :-) |
Simplest is to run a script which @bdraco has thankfully created. |
Thanks @gjohansson-ST |
You can check by opening inspect in your browser and looking at the websocket. You may need to reload for it to appear |
Hi. Thanks for support @bdraco I don't know what was those websocket calls. Can it be google assistant? Ran another profiler round and it did not produce those _async_websocket (http.py) commands. I can provide new profile, but I don't want to overwhelm with too much data. EDIT: just found out, that I can downgrade core. So I did downgraded new testing instance to 2024.11.3 and CPU spikes disappeared. Then tried: 2024.12.0b1 (no cpu spikes) and 2024.12.0b2 cpu started spiking: |
In your previous profile there wasn't a single call from |
No problems from my side @gjohansson-ST which way you prefer: Which core version do you prefer for that? Latest or 2024.12.0b2? If latest then with yesterday's PR applied or not? I can even do 2024.12.0b1 vs 2024.12.0b2 :-) |
Maybe to start with, including yesterday's PR applied, one with and one w/o spike. It's not written here but are these based on shelly? And are you using the native integration, MQTT or other (I believe I saw tasmota)? |
O.k. here you go @gjohansson-ST one 2024.12.2 (with new config\custom_components\statistics) one 60 sec calm period and one 60 sec period with spikes: |
I will look into it a bit later but then we can rule out |
You are right. Probably I hijacked wrong issue then :-( |
Maybe #132865 |
@dominikandreas this went a bit sideways. |
The problem
The
_stat_average_linear
function in the statistics component causes high CPU load usage when source sensors update frequently. Other functions from the component are likely to be affected as well (I only used_stat_average_linear
)What version of Home Assistant Core has the issue?
likely independent of any particular version
What was the last working version of Home Assistant Core?
No response
What type of installation are you running?
Home Assistant OS
Integration causing the issue
Statistics
Link to integration documentation on our website
https://www.home-assistant.io/integrations/statistics/
Diagnostics information
No response
Example YAML snippet
No response
Anything in the logs that might be useful for us?
No response
Additional information
I have a few sensors that update very frequently (e.g. current grid power at ~10Hz) for which I use an average_linear statistics sensor. Here's an example taken from
.storage/core.config_entries
:The issue seems to be related to these lines in the code:
https://github.com/home-assistant/core/blob/dev/homeassistant/components/statistics/sensor.py#L797-L809
When my source sensor started updating more frequently (switched from wifi to lan to connect to the source), home assistant became barely useable and it took me a long time to figure out what the issue was. I thought at first it was related to a version upgrade of home assistant which happened at at the same time, but everything runs smoothly again after disabling the statistics entities.
In particular, it seems to spend a lot of time within
total_seconds
. I wonder if a simple fix would be to keep track of timestamps and not datetime objects, but maybe also limit the updates to a certain frequency.Apart from the marked line, also notice the number of calls to
total_seconds
ofdatetime.timedelta
at the bottom. Since the profiler ran for 65 seconds, that's ~57ktotal_seconds
calls per second!The text was updated successfully, but these errors were encountered: