Bug 469777 - KWin since 5.27.5 uses 1 CPU core at 100% whenever something is using PipeWire to capture the screen
Summary: KWin since 5.27.5 uses 1 CPU core at 100% whenever something is using PipeWir...
Status: RESOLVED FIXED
Alias: None
Product: kwin
Classification: Plasma
Component: wayland-generic (show other bugs)
Version: 5.27.5
Platform: Arch Linux Linux
: NOR normal
Target Milestone: ---
Assignee: elitedev
URL:
Keywords: regression, wayland
: 446115 471364 472407 (view as bug list)
Depends on:
Blocks:
 
Reported: 2023-05-15 02:16 UTC by Prajna Sariputra
Modified: 2024-02-19 13:27 UTC (History)
11 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 Prajna Sariputra 2023-05-15 02:16:17 UTC
SUMMARY
After updating to 5.27.5 whenever something uses PipeWire to capture the screen (I tried OBS, Krfb and Firefox) KWin starts using 100% of one CPU core, even when the app isn't doing anything with the screen capture (like having OBS open without it streaming/recording or Krfb with no active connections). On my system with 8 cores and 16 threads that translates to about 6-7% overall CPU usage, but it also causes the CPU to use its turbo state constantly to keep up as a result.

Reverting to KWin 5.27.4.1 makes the CPU usage go back down to more normal levels, and building KWin 5.27.4.1 with this MR (https://invent.kde.org/plasma/kwin/-/merge_requests/3970, screencast: Offer the real framerate range we have available) applied to it causes the issue to come back.

I guess that MR might just be exacerbating an existing issue though, I have been getting `kwin_screencast: Dropping a screencast frame because the compositor is slow` messages appear repeatedly in journalctl anytime a screencast session is active, but with 5.27.5 as well as with 5.27.4.1 with that MR applied I get way more of those (from like 20 per second to well over 1000 per second).

With Krfb in particular the issue is particularly terrible, even after closing it the CPU usage remains at the same high level.


STEPS TO REPRODUCE
1. Update to KWin 5.27.5 or build KWin 5.27.4.1 with the aforementioned MR applied
2. Start a screencast session (either from OBS, Firefox or Krfb)

OBSERVED RESULT
KWin's CPU usage is ridiculously high.

EXPECTED RESULT
KWin should not consume so much CPU.

SOFTWARE/OS VERSIONS
Operating System: Arch Linux 
KDE Plasma Version: 5.27.5
KDE Frameworks Version: 5.105.0
Qt Version: 5.15.9
Kernel Version: 6.3.2-arch1-1 (64-bit)
Graphics Platform: Wayland
Processors: 16 × AMD Ryzen 7 6800H with Radeon Graphics
Memory: 14.9 GiB of RAM
Graphics Processor: AMD Radeon Graphics
Manufacturer: HP
Product Name: OMEN by HP Gaming Laptop 16-n0xxx
Comment 1 Prajna Sariputra 2023-05-18 04:04:29 UTC
> I guess that MR might just be exacerbating an existing issue though, I have been getting `kwin_screencast: Dropping a screencast frame because the compositor is slow` messages appear repeatedly in journalctl anytime a screencast session is active, but with 5.27.5 as well as with 5.27.4.1 with that MR applied I get way more of those (from like 20 per second to well over 1000 per second).

Correction: The warning messages I was getting prior to 5.27.5 is actually `Dropping a screencast cursor update because the compositor is slow`, so that part isn't as much "more warning messages than before" as it is "new warning messages". Looking through old logs I did still see the screencast frame drop message, but it was only occasionally occurring back then.

Also, out of the 3 commits introduced in the problematic MR I found that the issue is specifically caused by commit 226d8c0a3b464f8870c45bfe5079f042a3cd5d67 (screencast: Ensure we respect the negotiated framerate), reverting it on top of 5.27.5 drops KWin's CPU usage back to what it was before.
Comment 2 Prajna Sariputra 2023-11-03 17:04:51 UTC
The situation seems to have slightly improved now with KWin 5.27.9 (KWin from git at commit abf642d42e8189deaabf1999661e83aab25b99fa appears to be the same), screen sharing with Firefox now results in a much more reasonable CPU usage from KWin, however both OBS and Krfb still cause the issue.

Something I just noticed though is that the order I start the apps to test with can change things, for example if I try OBS first, close it and then Firefox the issue will persist with OBS even if I close Firefox and open OBS again, but after a reboot if I try Firefox first, close it and then try OBS the issue doesn't appear with both Firefox and OBS. So, I guess Firefox is now doing something nice that happens to make KWin behave properly?

For now I'm still reverting commit 226d8c0a3b464f8870c45bfe5079f042a3cd5d67 on top of KWin 5.27.9 to get around this problem fully.
Comment 3 toby 2023-12-01 10:20:18 UTC
I've also been having this issue and it's been causing my screen to lag every time I share my screen.
Comment 4 toby 2023-12-19 08:26:35 UTC
(In reply to toby from comment #3)
> I've also been having this issue and it's been causing my screen to lag
> every time I share my screen.

I've now confirm this is the case by reverting commit 226d8c0a3b464f8870c45bfe5079f042a3cd5d67 as I do not experience any stutters. I also do not get 100% CPU usage on on core.

Here is the bug report to the stuttering issue https://bugs.kde.org/show_bug.cgi?id=471364.
Comment 5 Nate Graham 2023-12-20 19:34:43 UTC
*** Bug 471364 has been marked as a duplicate of this bug. ***
Comment 6 Nate Graham 2023-12-20 19:34:50 UTC
*** Bug 446115 has been marked as a duplicate of this bug. ***
Comment 7 Nate Graham 2023-12-20 19:36:00 UTC
commit 226d8c0a3b464f8870c45bfe5079f042a3cd5d67
Author: Aleix Pol <aleixpol@kde.org>
Date:   Thu Apr 6 20:28:40 2023 +0200

    screencast: Ensure we respect the negotiated framerate
    
    Discards frames sent under the timeframe that was negotiated with the
    client. This allows clients to just request the frames that they should
    be able to compute.


Aleix, would you be able to investigate?
Comment 8 Nate Graham 2024-01-17 23:07:24 UTC
Is anyone able to test again with Plasma 6 to see if it's already fixed there? A number of changes have gone in for PipeWire and related code in KWin, so there's a chance.
Comment 9 Prajna Sariputra 2024-01-18 06:25:36 UTC
Unfortunately the issue still persists with KWin at commit 9f10d5d79fe21c63989e41c1184f895c5464ab2b from git master for me with OBS Studio, although interestingly now Krfb (23.08.4 from the Arch repos) no longer causes the problem, unlike in Plasma 5.27.10, so the situation has improved but the issue is not quite fixed yet.
Comment 10 Prajna Sariputra 2024-01-18 06:43:14 UTC
Spoke too soon, forgot about the fact that the order the screen sharing apps are started can change things (as I noted back in comment 2, oops), and after starting OBS, closing it and then starting Krfb the issue cropped up with Krfb again.

Firefox meanwhile still seems to manage to avoid the issue somehow, in fact even after I managed to reproduce the issue with OBS and Krfb, starting a screen sharing session in Firefox and then stopping it seems to make KWin behave sanely with both OBS and Krfb again, this time in the same session, without reboots.
Comment 11 Adam 2024-01-24 19:17:17 UTC
This exact issue has been driving me nuts for the last few hours. I can confirm this is still happening with my setup (versions & specs listed below) with OBS Studio 30.0.2.

Operating System: Arch Linux 
KDE Plasma Version: 5.27.10
KDE Frameworks Version: 5.114.0
Qt Version: 5.15.12
Kernel Version: 6.7.1-zen1-1-zen (64-bit)
Graphics Platform: Wayland
Processors: 16 × AMD Ryzen 7 5800X 8-Core Processor
Memory: 31.3 GiB of RAM
Graphics Processor: AMD Radeon RX 6700 XT
Comment 12 elitedev 2024-01-29 00:08:03 UTC
I'm also affected by this bug and I'm able to consistently reproduce it.

Adding a 'Screen Capture (PipeWire)' source in OBS is enough to trigger it -- it doesn't even need to be streaming. As soon as the source is added, a tray icon call 'Screen casting' is created and the bug starts to occur. It appears that it drops frames every second.

I'm able to work around it, by just changing the monitor source over and over in OBS while watching testufo pattern. It randomly doesn't occur sometimes and once it's good, it stays good with no frame drops. 

I'm on a fully updated Arch machine running KDE/Wayland -- let me know if there's anything else I can add.
Comment 13 elitedev 2024-01-31 04:26:19 UTC
I've just finished testing a fix for this issue and have created a MR.

https://invent.kde.org/plasma/kwin/-/merge_requests/5078
Comment 14 Julien Delquié 2024-01-31 07:37:42 UTC
Just tested the fix, and it works for me. :)

---

Operating System: Gentoo Linux 2.14
KDE Plasma Version: 5.27.10
KDE Frameworks Version: 5.114.0
Qt Version: 5.15.12
Kernel Version: 6.7.2-gentoo-r1-x86_64 (64-bit)
Graphics Platform: Wayland
Processors: 8 × Intel® Core™ i7-9700K CPU @ 3.60GHz
Memory: 31.3 Gio of RAM
Graphics Processor: AMD Radeon RX 7800 XT
Comment 15 Vlad Zahorodnii 2024-02-01 08:28:26 UTC
Git commit 5ae170f1d6e4c1252c98318f9939ccca3baa235f by Vlad Zahorodnii, on behalf of Jay Paul.
Committed on 01/02/2024 at 08:27.
Pushed by vladz into branch 'master'.

plugins/screencast: set frame timer to one shot

M  +1    -0    src/plugins/screencast/screencaststream.cpp

https://invent.kde.org/plasma/kwin/-/commit/5ae170f1d6e4c1252c98318f9939ccca3baa235f
Comment 16 Vlad Zahorodnii 2024-02-01 08:28:51 UTC
Git commit 416ed6479d38cbc21da1278db3e52f3c4e67ed90 by Vlad Zahorodnii, on behalf of Jay Paul.
Committed on 01/02/2024 at 08:28.
Pushed by vladz into branch 'Plasma/6.0'.

plugins/screencast: set frame timer to one shot


(cherry picked from commit 5ae170f1d6e4c1252c98318f9939ccca3baa235f)

M  +1    -0    src/plugins/screencast/screencaststream.cpp

https://invent.kde.org/plasma/kwin/-/commit/416ed6479d38cbc21da1278db3e52f3c4e67ed90
Comment 17 toby 2024-02-01 08:56:19 UTC
Just tested the fix on Plasma 6.0 RC2 which works for me. 

I had put up a bug bounty for this on KDE Discuss (https://discuss.kde.org/t/bug-fix-screen-freezing-lags-every-second-when-screen-sharing-wayland/8768)

elitedev could you please email me or message me on KDE Discuss to discuss payment.
Comment 18 Zamundaaa 2024-02-12 15:19:02 UTC
*** Bug 472407 has been marked as a duplicate of this bug. ***
Comment 19 Vlad Zahorodnii 2024-02-19 13:27:47 UTC
Git commit 3544df1abaf8c5b1d34158890f3122071c7b01b1 by Vlad Zahorodnii, on behalf of Jay Paul.
Committed on 19/02/2024 at 13:08.
Pushed by vladz into branch 'Plasma/5.27'.

plugins/screencast: set frame timer to one shot


(cherry picked from commit 5ae170f1d6e4c1252c98318f9939ccca3baa235f)

(cherry picked from commit 416ed6479d38cbc21da1278db3e52f3c4e67ed90)

M  +1    -0    src/plugins/screencast/screencaststream.cpp

https://invent.kde.org/plasma/kwin/-/commit/3544df1abaf8c5b1d34158890f3122071c7b01b1