Bug 479126 - Wayland: High CPU on mouse move
Summary: Wayland: High CPU on mouse move
Status: CLOSED FIXED
Alias: None
Product: kwin
Classification: Plasma
Component: compositing (show other bugs)
Version: 5.91.0
Platform: Arch Linux Linux
: NOR major
Target Milestone: ---
Assignee: KWin default assignee
URL:
Keywords: qt6
Depends on:
Blocks:
 
Reported: 2023-12-28 16:48 UTC by John E
Modified: 2024-01-19 04:17 UTC (History)
9 users (show)

See Also:
Latest Commit:
Version Fixed In: 6.0


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description John E 2023-12-28 16:48:23 UTC
SUMMARY
Hello,

Just moved to Plasma 6 Wayland from Plasma 5.27.10 Wayland and as is the session is unusable. I'm having 2 major issues:

- If I don't do anything it seems fine but if I move the mouse kwin_wayland uses a crazy amount of CPU, well on this very weak CPU maybe on a powerful one it wouldn't be noticeable, but on mine the desktop can freeze if I move it much. By crazy amount I mean I can see a kwin_wayland process taking up to 190% in HTOP and I believe there's more than one process, whereas in the same situation without touching the mouse it's more like 2%. Using the input from kde connect causes the same behavior, but using my keyboard doesn't seem to have any bad effect.

- With HDR on the screen keeps flickering. It could be some power/bandwidth issue related to the above, I never had that problem with gamescope/mpv.

This machine has a first gen SSD, 8GB of RAM, a sandy bridge pentium and a Radeon 580 8Gb on Arch. The mouse is a Logitech G900 if it matters. 

It seems dropping the polling rate in piper for my mouse and disabling screen tearing in KCM drastically reduces the issue. Now kwin_wayland uses around 30% on mouse moves, interestingly systemsettings uses about the same (that's the main window currently) on mouse moves.

STEPS TO REPRODUCE
1. start HTOP
2. move the mouse

OBSERVED RESULT
CPU used is super high.

EXPECTED RESULT
CPU used barely changes.

SOFTWARE/OS VERSIONS
Linux: 6.6.8
KDE Plasma Version: 5.91
KDE Frameworks Version: I'm not on that machine right now but through SSH I see this frameworkintegration 5.247.0 and plasma-framework5 5.113.0 (that one shouldn't matter I'm thinking)
Qt Version: 6.7.0beta1

ADDITIONAL INFORMATION
I only upgraded my system to latest, maybe I need to install some kind of meta package for 6 that would help?
Comment 1 Andrej Halveland 2023-12-30 16:18:35 UTC
Just for comparison.

While looking at htop, without moving my mouse the kwin_wayland process jumps between 0 and 1% CPU usage and when I move my mouse (on the desktop) it jumps to about 12-15% CPU usage.

This is on a Zephyrus G15 GA502IV laptop with Ryzen 9 4900HS and Vega 8.
Comment 2 Andrej Halveland 2023-12-30 16:30:06 UTC
Here is a more extensive software/hardware info:

Plasma 6 Beta 2 (installed through Arch Linux's kde-unstable repo)

SOFTWARE/OS VERSIONS
Operating System: Arch Linux
KDE Plasma Version: 5.91.0
KDE Frameworks Version: 5.247.0
Qt Version: 6.7 (beta1)
Kernel Version: 6.7.0-rc6
Graphics Platform: Wayland
Graphics Drivers: mesa 23.3.1-2
Processors: AMD Ryzen 9 4900HS
Memory:  24GB of RAM
Graphics Processor: Integrated: Vega 8
Manufacturer: ASUS (Zephyrus G15 GA502IV)
Comment 3 John E 2023-12-31 00:52:21 UTC
(In reply to Andrej Halveland from comment #1)

Hey,
it seems you're seeing the same behavior but with a better CPU so it's not as bad an experience.
Since we're both on Arch I'd assume it's related, maybe to Qt 6.7?

Does my workaround help you as well?

Thanks for adding to this report!
Comment 4 David Edmundson 2023-12-31 14:17:03 UTC
This is not reproducible here. Can you try to get a log in perf/hotspot?
Comment 5 John E 2024-01-01 01:15:23 UTC
(In reply to David Edmundson from comment #4)
> This is not reproducible here. Can you try to get a log in perf/hotspot?

Sure, I've uploaded it to mega as it's bigger than 4Mb:
https://mega.nz/file/hxIVXazR#LvzLng3je5F2YwnroKrNCY9YxlAWtBbGKUzYSE2sBfc

I didn't know if I should attach to the user process of kwin or the root one so I attached to both I hope that's ok.
Comment 6 John E 2024-01-02 17:55:43 UTC
Oh I should add that I have 2 displays connected, a 4K TV and a 1080P receiver, they're mirrored, maybe that has something to do?
Comment 7 Pawel 2024-01-03 11:55:58 UTC
I can reproduce the high cpu usage on mouse move. I have a 12 core/24 thread 24 × AMD Ryzen 9 5900X 12-Core Processor and kwin_wayland uses 30% of a single thread when moving the mouse.
Comment 8 John E 2024-01-09 07:26:30 UTC
I don't know how much that relates, but in case it does:

I currently see kwin_wayland using 1747Mb of VRAM in amdgpu_top (I do not know how accurate this is though).
When I start the machine it usually is around 200Mb or so.
Right now in terms of GUI apart from Plasma stuff there is only pavucontrol and konsole that are open; previously mpv played some videos and it's not impossible that firefox was open for a few minutes on restart not sure anymore. That doesn't seem like anything heavy.

If it does not, I can create another BR or probably join another one.
Comment 9 Akseli Lahtinen 2024-01-12 15:00:24 UTC
I can see minimal spike kwin_wayland process when moving mouse around fast (about 3-4%).

I have mouse with 1000hz polling rate and 165hz monitor.
Comment 10 Bug Janitor Service 2024-01-12 16:03:17 UTC
A possibly relevant merge request was started @ https://invent.kde.org/plasma/kwin/-/merge_requests/4901
Comment 11 Zamundaaa 2024-01-12 20:39:47 UTC
Git commit d4a3d6689ea2cd75427b3c3efedd654c6e38d30d by Xaver Hugl.
Committed on 12/01/2024 at 19:15.
Pushed by zamundaaa into branch 'master'.

backends/drm: wait for the pageflip to be done with the condition variable

...instead of busy looping, which is causing several percent CPU usage in some cases

M  +1    -1    src/backends/drm/drm_commit_thread.cpp

https://invent.kde.org/plasma/kwin/-/commit/d4a3d6689ea2cd75427b3c3efedd654c6e38d30d
Comment 12 Zamundaaa 2024-01-12 21:09:22 UTC
Git commit 4b4852463f7b37deda98efb5aefbb13984db9845 by Xaver Hugl.
Committed on 12/01/2024 at 21:56.
Pushed by zamundaaa into branch 'Plasma/6.0'.

backends/drm: wait for the pageflip to be done with the condition variable

...instead of busy looping, which is causing several percent CPU usage in some cases


(cherry picked from commit d4a3d6689ea2cd75427b3c3efedd654c6e38d30d)

M  +1    -1    src/backends/drm/drm_commit_thread.cpp

https://invent.kde.org/plasma/kwin/-/commit/4b4852463f7b37deda98efb5aefbb13984db9845
Comment 13 John E 2024-01-16 12:56:34 UTC
I have not yet been able to test the fix, but I was able to try again on RC1.

It actually works ok as long as my 2nd screen does not mirror the first.
If I have no 2nd screen it works, if I have a 2nd screen on the side it works, but it eats crazy CPU resources when it's mirrored.
When I say it works ok, the foreground process does use a bit more CPU when the mouse is moved but it's only a few percent so no big deal. Yet based on the description of the fix, I fear it would fix that and not the actual annoying case but we'll see. I'll try to start a build with the fix now and I'll report later.
Comment 14 Nate Graham 2024-01-16 20:30:18 UTC
Thanks, the testing is appreciated.
Comment 15 Pawel 2024-01-16 20:56:07 UTC
I applied this patch on top of arch kwin RC1 and it does resolve the issue for me. Kwin's CPU usage is no longer spiking when moving the mouse cursor, at leas that's what htop says.
Comment 16 John E 2024-01-17 10:27:10 UTC
I was able to install the patched kwin and run it for a bit.

The extra +~2% CPU usage when using the mouse without mirroring is still there so the patch didn't change that, which is fine.

Alas I was unable to drag my second screen onto my first one in system settings so I could not mirror them, not sure why since I had no issue doing yesterday so but I'm doing it from kdeconnect so it's not necessarily super easy; hence I haven't tested the bad scenario yet.

Is meta+p still working on RC1? It shows nothing.

I'll try again today or tomorrow.

Thanks for reading Nate and Pawel for testing!
Comment 17 Nate Graham 2024-01-18 00:30:25 UTC
Meta+P is broken right now, yeah. You can mirror using the Display Configuration widget in the system tray if dragging one screen on top of another one isn't working either. Note that this is fiddly; the drag area is fairly small.
Comment 18 John E 2024-01-19 04:17:29 UTC
I wished I had seen your message earlier Nate but after enough fiddling in the system settings UI I got it to mirror (well I think, it's only an audio output so it's hard to say for sure) and I could not reproduce the issue so it seems the fix was fine!

Thank you!