Python3 high CPU Usage

# Example configuration.yaml entry
logger:
  default: warning
  logs:
    homeassistant.event: debug

Something like this should work ^^

I found the problem, I have sensors with templates and when removing the entity_id they always made a request for data, what I did was include the entities of each sensor in the template and thus lower the use of cpu to 6%

1 Like

I have HA running in a VirtualBox VM, image obtained from the web site 3 weeks ago and running completely upt-o-date versions of HA, OS and Supervisor etc. I can’t get py-spy installed. I’ve installed the Terminal/SSH add-on and logged into HA via the Terminal that it adds to the side bar. Then this:

apk add cargo
cargo install py-spy

It ran through the build process for about 10 minutes and failed with:

Compiling addr2line v8.11.0
error: could not find native static library 'unwind', perhaps an -L flag is missing?
error: aborting due to previous error
error: failed to compile 'py-spy v0.3.3' intermediate artifacts can be found at '/tmp/cargo-installatomdZrk'
Caused by:
  could not compile 'remoteprocess'

There are pre-built binaries here https://github.com/benfred/py-spy/releases in case one of them works for your OS

Thank you, but it’s the HASSOS running in a VM. Do you know off-hand which of those is relevant?

Depending on the VM, its probably one of:

https://github.com/benfred/py-spy/releases/download/v0.3.3/py-spy-v0.3.3-x86_64-unknown-linux-gnu.tar.gz

https://github.com/benfred/py-spy/releases/download/v0.3.3/py-spy-v0.3.3-x86_64-unknown-linux-musl.tar.gz

I’ve taken the first one and copied it to the HA config shared directory (for the sake of somewhere to put it) and logged in via the terminal but have drawn a blank on running it. There doesn’t seem to be any python processes running, and the command line doesn’t find the py-spy command to run it anyway. I really don’t knoiw what I’m doiung! It would be helpful if these essential diagnostic tools could be built into HA or isntallable by an offical add-on. I reported having trouble with high CPU and Z-Wave not responding weeks ago and have been trying ever since to get py-spy running as instructed, but I just don’t know what to do. I originally had a Supervised/docker install and was able to get py-spy on the hosted system looking at python3 processes, but then a HA upgrade told me that this install was unsupported. They want control of the OS it seems. So I went to the trouble of converting it to a supported VM install. Whereas before I could get py-spy running because I was in control of my environment, now it’s now a supported install but with an unknown OS that I know nothing about and it won’t run py-spy.

Untitled

If its running in docker, you can get into the container to use py-spy with docker exec -it homeassistant /bin/bash

OK, success I think, thank you for the help. I couldn’t execute that docker command from teh Terminal/SSH facility, the command is not recognised. However, I can get into the VM console, logging onto the HA CLI anbd then logging on as root. That docker command then works. Doing a ps -ef shows me that there is indeed a python3 process running. I tried running py-spy as above but again the command couldn’t be found. I then tried the binary from the second link you provided and the command ran and created a svg graph. So I’m just waiting now for the runaway process to happen again then I can post the results.

I can’t imagine there is any way for anyone to work out that this is how you’re supposed to get in to run the py-spy…as I said, it would be helpful if this was available as an add-on. It could even monitor the process itself and alert if it was running a high CPU level for a certain length of time.

1 Like

I’m running HA Container (not HassOS) but I’m getting the same result. Which other hoop did you need to jump thru?

Take a look two posts above that one: Python3 high CPU Usage

Let me know if you need more specific assistance, but hopefully that helps.

Thanks!

That got that going but now I’m not sure what the command is to actually run py-spy in “HA Container”.

I’ve exec’ed into the container and I found the PID using top but when I run:

py-spy record --pid 206 --output /config/www/py-spy.svg --duration 120

I get:

“Permission Denied: Try running again with elevated permissions by going ‘sudo env “PATH=$PATH” !!’”

Then when I try to run anything with sudo it says “sudo: command not found”.

Any idea what the correct command to run is inside the container to actually get py-spy to run in “HA Container” (not HassOS)

Just as a follow up in case anyone else comes here looking for the same info:

I had to first add “–cap-add SYS_PTRACE” to my docker run command to enable py-spy to run without permission errors.

Next, exec into your container by either using the console in portainer or via SSH.

docker exec -it <your_container_name> /bin/bash

Once inside the container then run the following to install py-spy:

echo 'manylinux1_compatible = True' > /usr/local/lib/python3.8/site-packages/_manylinux.py

pip install py-spy

After that run top to find the PID of HA. You should see something like the following:

the one you want is the python3 program for HA (first one in the list there).

then using the PID from above run the following (replacing my PID of 205 with your own):

py-spy record --pid 205 --output /config/www/py-spy.svg --duration 120

This will run a sample and eventually write a py-spy.svg to your www folder.

The only thing I couldn’t get to work is the recoprding didn’t stop automatically after 120 seconds. I had to stop it manually by hitting ctrl-c.

Also I kept getting a continous string of warnings that the sdampling was behind and to reduce the sampling rate. I don’t know how to do that so I just let it run. I’m not sure if it impacted the accuracy of the recording or not tho TBH.

4 Likes

For those who can’t get py-spy to work, I’ve put together a profiler integration that will generate a callgrind.xxx.out file that can be viewed in qcachegrind

4 Likes

I’ve updated the Profiler integration steps

The integration can now be configured from the UI so its a bit easier to get setup.

1 Like

For me it came back with 0.116.
Below 2 120s PySpys from 2 different processes.

https://drive.google.com/drive/folders/1tTJuvIjl7SM4LSe8br5xdelYg8gBx_Ee?usp=sharing

where/how do you want us to send you the profiler data? rather not send it all publicly :wink:

btw, is it too much to ask to have the profiler write files in its own folder, or maybe a configurable folder. It now writes to /config, which feels a bit ‘unsafe’, and messy btw.

Contact details are here: https://github.com/home-assistant/core/issues/40292#issuecomment-702744364

yeah, I was in that discussion, but it was not very public, and about py-spy.

Now that this Profiler add-on is publicly released, I could imagine Nick not wanting all profiles to be sent to his private email address…

Feel free to send it on discord or to my email nick [at] koston [dot] org. No worries on the additional email.