raudette / meshtastic_discord_bridge

A Discord bot which bridges discussions between a Discord channel and a Meshtastic mesh through a locally connected radio
MIT License
10 stars 5 forks source link

High cpu usage? #3

Open limes-github opened 1 month ago

limes-github commented 1 month ago

Hello,

I just noticed one weird thing.

root 3142348 87.0 0.6 497528 49492 pts/1 Sl+ 23:03 37:23 python3 meshtastic_discord_bridge.py

CPU usage reaches 100% for single core (i5 3470T).

raudette commented 1 month ago

The PC I'm running it on is an i5-7500t, I'm showing 0% CPU, it's probably been running for about a day since I restarted it last.

(base) raudette@6502:~$ ps -p 2690193 -o %cpu,%mem,cmd %CPU %MEM CMD 0.0 0.2 python meshtastic_discord_bridge.py

I can get it to spike to 0.7% when I'm interacting with it in Discord.

I'm connected via USB. I'm currently connected to a Heltec V3 - I just checked, it's running ancient firmware - v2.3.10

limes-github commented 1 month ago

Hello,

This is after around 1 minute: %CPU %MEM CMD 25.7 0.5 python3 meshtastic_discord_bridge.py

This is after around 2 minutes: %CPU %MEM CMD 40.6 0.5 python3 meshtastic_discord_bridge.py

raudette commented 1 month ago

I haven't been able to reproduce this issue - I've upgraded the firmware on my device to 2.5.4, I'm connecting by IP, and after 12 hours, I'm still showing 0%, going up to 0.7% when there is activity.

I can't think of any other potential meaningful differences that would account for such a large difference in CPU usage, it's a fairly simple script connecting the Discord and Meshtastic libraries. I can't see this making a difference, but I'm running Python 3.11.5 on Ubuntu 24.04.

Perhaps if you have another PC available, try running it on another PC, and see if you can reproduce the issue.

limes-github commented 1 month ago

I will run it on OpenSUSE which I have available. On my NAS I have Python 3.9.2 on openmediavault 6.9.16.

I will let you know what the test results are.

atimias commented 1 week ago

writing from different account. Cannot reproduce it god dammit.

On this platform it gets messy:

omv:[~/meshtastic_discord_bridge]#python -V
Python 3.11.2
omv:[~/meshtastic_discord_bridge]#cat /etc/os-release 
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
raudette commented 1 week ago

I just took a look at the other major packages - pip show indicates: discord v 2.3.2 pypubsub 4.0.3 meshtastic 2.3.8

I can't see the python version being a big deal, but perhaps try an alternate python installation with conda or something similar?

atimias commented 1 week ago

I recreated the same environment.

On my server (high cpu peaks) discord 2.2.2 pypubsub 4.0.3 meshtastic 2.5.4

On my Raspberry (no issues at all) discord 2.2.2 pypubsub 4.0.3 meshtastic 2.5.4

That's interesting.

Edit: it's not a critical issue. In my case I moved it to the RPI. Debugging it could be time consuming, so please don't inspect too deeply and waste your time on this :)

atimias commented 1 week ago

Well.. that's interesting. After couple hours on Raspberry Pi.

root 248811 89.3 1.3 505288 51368 pts/3 Sl+ 08:30 214:26 python3 meshtastic_discord_bridge.py

CPU again in peak (1 core)

raudette commented 1 week ago

As best as I can tell, I started the app last on Oct 19th, and it is still sub-1%

I have no experience profiling apps - I just read this article: https://medium.com/@maximsmirnov/profiling-asynchronous-python-576568f6f2c0

It suggests this tool: https://github.com/plasma-umass/scalene

It's a pip package. After installing, you run it as follows: scalene meshtastic_discord_bridge.py

After you notice the issue, hit CTRL-C

Scalene then dumps output to the screen, indicating where resources are being spent. Might be worth checking out.

atimias commented 1 week ago

169 │ 17% │ │ 72% │ 56% │ 3M │▁ 13% │ 5 │client.run(token)

This one seems to have biggest values. It reached 100% on core.

atimias commented 1 week ago

Here is a result of attaching py-spy to the PID of the process. It was 100% at the time.

py-spy

I don't know if this helps, but when the CPU spikes high the network traffic to my Meshtastic node stops permanently (observed by tcpdump).