tdesktop icon indicating copy to clipboard operation
tdesktop copied to clipboard

Reconnect spinner burns CPU

Open benzea opened this issue 4 years ago • 40 comments

Steps to reproduce

  1. Disconnect network
  2. Look at telegram or look at top/strace

Or, said differently, try to work somewhere without internet connection (and power supply).

Expected behaviour

Telegram should not burn CPU. In particular when it is not even visible (e.g. different workspace). Which it probably only can do because it is using X11 (Xwayland) rather than wayland (GNOME shell).

Easiest fix would likely be to just do a countdown that is only updated e.g. every 5 seconds instead of showing a spinner. At least when the window is not focused.

Configuration

Operating system: F32

Version of Telegram Desktop: 2.1.11

Installation source (Linux Only) - flatpak

Used theme: default

benzea avatar Jun 13 '20 19:06 benzea

Reproducable on Gentoo GNU/Linux, Telegram 2.1.7, installation source - Gentoo package, DE - Plasma Wayland.

p4vook avatar Jun 18 '20 07:06 p4vook

I am also seeing a lot of wakeups while just running and connected. This could be related if QT has a similar behaviour to GTK where spinners may cause wakeups (but no rendering) if they are not stopped in certain use cases.

benzea avatar Jun 18 '20 09:06 benzea

Hey there!

This issue will be automatically closed in 7 days if there would be no activity. We therefore assume that the user has lost interest or resolved the problem on their own.

Don't worry though; if this is an error, let us know with a comment and we'll be happy to reopen the issue.

Thanks!

stale[bot] avatar Oct 23 '20 14:10 stale[bot]

Behaviour is still there.

benzea avatar Oct 23 '20 15:10 benzea

Still present on Ubuntu 20.04 when network connection goes down. Telegram uses 6-7 % CPU. Oftentimes something "snaps" all Telegram starts using 100% CPU. Even when I go back online, the CPU usage stays at 100%. The only remedy is to restart Telegram.

themighty1 avatar Feb 09 '21 18:02 themighty1

I literally woke up last night to the sound of my cpu because my internet went offline. it also uses ALL cores to make sure it is annoying enough. thanks telegram.

sh4dowb avatar Apr 16 '21 01:04 sh4dowb

Hey there!

This issue was inactive for a long time and will be automatically closed in 30 days if there isn't any further activity. We therefore assume that the user has lost interest or resolved the problem on their own.

Don't worry though; if this is an error, let us know with a comment and we'll be happy to reopen the issue.

Thanks!

stale[bot] avatar Oct 13 '21 03:10 stale[bot]

Still the same.

benzea avatar Oct 18 '21 14:10 benzea

Hey there!

This issue was inactive for a long time and will be automatically closed in 30 days if there isn't any further activity. We therefore assume that the user has lost interest or resolved the problem on their own.

Don't worry though; if this is an error, let us know with a comment and we'll be happy to reopen the issue.

Thanks!

stale[bot] avatar Apr 17 '22 06:04 stale[bot]

Still the same.

benzea avatar Apr 17 '22 14:04 benzea

Hey there!

This issue was inactive for a long time and will be automatically closed in 30 days if there isn't any further activity. We therefore assume that the user has lost interest or resolved the problem on their own.

Don't worry though; if this is an error, let us know with a comment and we'll be happy to reopen the issue.

Thanks!

github-actions[bot] avatar Oct 15 '22 02:10 github-actions[bot]

still a problem

benzea avatar Oct 24 '22 21:10 benzea

Also a problem for me on macOS 12

pjrobertson avatar Nov 19 '22 07:11 pjrobertson

I just updated Telegram Desktop to v4.6.10b, and the problem persists.

VahidSh69 avatar Mar 14 '23 12:03 VahidSh69

Still the same behavior, also it some times cause my laptop to overheat and freeze, so I have to close tg when I am away from laptop.

shtirlic avatar May 17 '23 05:05 shtirlic

@shtirlic , does this behaviour happen when your network connection goes down?

themighty1 avatar May 17 '23 05:05 themighty1

@themighty1 actually the debug process is quite hard for this issue, my scenario is like this: I lock my laptop with tg running in the background and in the morning I got frozen and hot laptop with all fans on max, in journalctl I do not see any suspicious errors or something bad around the projected freeze time, but for sure I saw iwd reconnection messages some time before. It might be that lost connection is not instantly spinning CPU to orbital speed just in time of disconnection, but somehow it triggers some deadlock in tg code a little bit later. Reconnection/Lost connection high CPU issues I have seen for years before, I saw this on Windows and macOS too. Currently my setup is: Arch Linux with KDE(x11). I will try to gather more info using audit logs and logging system usage/thermal/network activity.

shtirlic avatar May 17 '23 06:05 shtirlic

Thanks for the description. I know it's a tall call but someone motivated enough could compile a debug version of tgdesktop and run it under a CPU profiler to see which function is responsible for the CPU loop.

themighty1 avatar May 17 '23 06:05 themighty1

I know it's a tall call but someone motivated enough could compile a debug version of tgdesktop and run it under a CPU profiler to see which function is responsible for the CPU loop.

I just looked in to doing this, but the build instructions don't work for Mac. For other devs, this is incredibly easy to reproduce for me on macOS:

  1. Open Telegram
  2. Go to System Preferences > Network > Advanced ... and set up a SOCKS proxy for an IP/port that hangs.
  3. See CPU spikes

pjrobertson avatar May 17 '23 06:05 pjrobertson

Original reporter here. As I implied in my original report, the problem appears to be excessive redrawing. Just plain redrawing of a spinner ...

i.e. powertop says:

                Usage       Events/s    Category       Description
              4.6 ms/s      72.9        Process        [PID 967096] telegram-desktop --

and well, yeah, redrawing with 60Hz would be consistent with that. sysprof shows the CPU time is pretty much all spend drawing too: Screenshot from 2023-05-17 13-35-21

So, really, just replace that reconnection spinner with something that is only updated once every few seconds at max.

And yes ... it could be that the desktop environment or QT is partially to blame here (e.g. window shouldn't actually get redrawn if it is completely obscured). But, maybe it would still be nice to solve it here in some way.

benzea avatar May 17 '23 11:05 benzea

Sorry, better screenshot of the sysprof data: image

benzea avatar May 17 '23 11:05 benzea

@benzea, Thanks for pointing out the animation. The app does some work even when interface animation is disabled in settings. Here is the capture of sysprof.

2023-06-21_capture.zip

Screenshot from 2023-06-21 19-28-37

yanovich avatar Jun 21 '23 16:06 yanovich

The problem is CPU usage offline is higher than online. CPU usage is 0% of a CPU thread online, 25% offline w/ animation on, 4% w/ animation off for i5-11600KF. and 0/50/5 for i5-8265U. 5% CPU usages is a big load on laptop battery. It consumes as much energy as watching a movie, but doing nothing.

yanovich avatar Jun 21 '23 17:06 yanovich

@yanovich oh, true. And in that case it seems to be mostly polling and killing QT timers. So, I guess stopping/starting some timer somewhere ... outch

benzea avatar Jun 22 '23 08:06 benzea

Hey there!

This issue was inactive for a long time and will be automatically closed in 30 days if there isn't any further activity. We therefore assume that the user has lost interest or resolved the problem on their own.

Don't worry though; if this is an error, let us know with a comment and we'll be happy to reopen the issue.

Thanks!

github-actions[bot] avatar Dec 20 '23 01:12 github-actions[bot]

still an issue on the latest tg desktop I still see some ~5-10% CPU usage like I reported 2 years ago.

themighty1 avatar Dec 20 '23 05:12 themighty1

you're lucky, it uses %1600 CPU on my system. fans still wake me up when there's a connection issue..

Sent from Proton Mail for iOS

On Wed, Dec 20, 2023 at 08:21, dan @.***(mailto:On Wed, Dec 20, 2023 at 08:21, dan < wrote:

still an issue on the latest tg desktop I still see some ~5-10% CPU usage like I reported 2 years ago.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

sh4dowb avatar Dec 20 '23 09:12 sh4dowb

There's a bug in the Qt 6 RHI abstraction that it mis-uses QImage's CoW feature and causes a detach (copy) of entire window buffer on every repaint so the animation is perhaps consuming way more CPU on Wayland than on Windows/macOS/X11. I've patched Qt for official builds in 4.14.4 so it should consume not more than official builds for other systems. Can't help distro builds though.

ilya-fedin avatar Jan 12 '24 00:01 ilya-fedin

Can't help distro builds though.

Can you post your qt patch? That would help...

joecool1029 avatar Jan 23 '24 18:01 joecool1029

Currently it consists of 4 patches: https://github.com/desktop-app/patches/blob/master/qtbase_6.6.1/0029-fix-backing-store-rhi-unneeded-copy.patch https://github.com/desktop-app/patches/blob/master/qtbase_6.6.1/0030-fix-backing-store-rhi-xcb-unneeded-copy.patch https://github.com/desktop-app/patches/blob/master/qtbase_6.6.1/0031-fix-backing-store-opengl-subimage-unneeded-copy.patch https://github.com/desktop-app/patches/blob/master/qtwayland_6.6.1/0009-fix-backing-store-rhi-unneeded-copy.patch But I think it could be made down to 2 patches: the fix-backing-store-opengl-subimage-unneeded-copy one + just remove the image.detach() from QBackingStoreDefaultCompositor::toTexture. This means less changes but fixing for all platforms at once (all Qt platforms affected but the current variant fixes only X11 and Wayland).

ilya-fedin avatar Jan 23 '24 18:01 ilya-fedin