Bug 488843 - Triple buffering causing stuttering
Summary: Triple buffering causing stuttering
Status: RESOLVED FIXED
Alias: None
Product: kwin
Classification: Plasma
Component: performance (show other bugs)
Version: 6.1.0
Platform: Other Linux
: NOR normal
Target Milestone: ---
Assignee: KWin default assignee
URL:
Keywords: regression
: 488821 488860 (view as bug list)
Depends on:
Blocks:
 
Reported: 2024-06-20 22:38 UTC by Paulo Henrique
Modified: 2024-07-26 19:32 UTC (History)
10 users (show)

See Also:
Latest Commit:
Version Fixed In: 6.1.4
Sentry Crash Report:


Attachments
KWIN_Performance_Data_Triple_Buffering.zip (940.69 KB, application/zip)
2024-06-27 09:22 UTC, hbr
Details
KWIN_6_1_2_Performance_Data_Triple_Buffering.zip (608.03 KB, application/zip)
2024-07-04 19:08 UTC, hbr
Details
perf stats csv files running KWin with MR 5982 and without (1.51 MB, application/zip)
2024-07-05 16:09 UTC, Marcus
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Paulo Henrique 2024-06-20 22:38:30 UTC
After plasma 6.1.0, i noticed stutterings playing games like FFXIV and Granblue fantasy relink, since plasma 6.1 merged triple buffering i tested disabling it with KWIN_DRM_DISABLE_TRIPLE_BUFFERING=1 and i can confirm this solves the issue.

Its probably affecting more games or others 3D applications.


SOFTWARE/OS VERSIONS
Linux/KDE Plasma: Arch Linux
(available in About System)
KDE Plasma Version: 6.1.0
KDE Frameworks Version: 6.3.0
Qt Version: 6.7.1

ADDITIONAL INFORMATION
hardware spec : ryzen 5600, 32GB, RX 6600
Comment 1 Paulo Henrique 2024-06-20 22:49:09 UTC
Also, my display don't have anything fancy like VRR its just an old  fullhd 60hz display.
Comment 2 Nate Graham 2024-06-20 23:16:30 UTC
Similar thing to what was reported in Bug 488821. This one is AMD though, not NVIDIA.
Comment 3 hbr 2024-06-21 12:19:52 UTC
I can confirm this.

I have stuttery animations with Plasma 6.1 a lot more often than I did with Plasma 6 or 5 (but when they are smooth, they are REALLY smooth!).

This ist most noticable when moving windows, minimizing windows, showing the list menu in the task manager or when sub windows open in Dolphin (confirmations, renames, etc.).

Setting "KWIN_DRM_DISABLE_TRIPLE_BUFFERING=1" fixes it.

SOFTWARE/OS VERSIONS
Operating System: EndeavourOS 
KDE Plasma Version: 6.1.0
KDE Frameworks Version: 6.3.0
Qt Version: 6.7.1
Kernel Version: 6.9.5-zen1-1-zen (64-bit)
Graphics Platform: Wayland
Mesa: 24.1
Processors: AMD Ryzen 7 7700X 8-Core Processor
Memory: 31,0 GiB of RAM
Graphics Processor: AMD Radeon RX 7900 XTX
Comment 4 hbr 2024-06-21 12:52:09 UTC
Also happens on Intel hardware for me, see https://bugs.kde.org/show_bug.cgi?id=488860

Setting "KWIN_DRM_DISABLE_TRIPLE_BUFFERING=1" fixes it on both of my machines.
Comment 5 Bug Janitor Service 2024-06-24 12:15:47 UTC
A possibly relevant merge request was started @ https://invent.kde.org/plasma/kwin/-/merge_requests/5973
Comment 6 Zamundaaa 2024-06-24 13:39:44 UTC
Git commit a169114fee661e61519f06c0567f4daa486dc336 by Xaver Hugl.
Committed on 24/06/2024 at 13:26.
Pushed by zamundaaa into branch 'master'.

core/renderloop: log frame statistics into a file

Data like target vs. actual pageflip time, and render times is often needed for
debugging or optimizing render time predictions, so this commit makes KWin print
that information to a file in the home directory whenever KWIN_LOG_PERFORMANCE_DATA
is set.

M  +10   -5    src/core/renderbackend.cpp
M  +4    -3    src/core/renderbackend.h
M  +18   -2    src/core/renderloop.cpp
M  +5    -1    src/core/renderloop_p.h

https://invent.kde.org/plasma/kwin/-/commit/a169114fee661e61519f06c0567f4daa486dc336
Comment 7 Bug Janitor Service 2024-06-24 13:40:24 UTC
A possibly relevant merge request was started @ https://invent.kde.org/plasma/kwin/-/merge_requests/5975
Comment 8 Zamundaaa 2024-06-24 13:51:40 UTC
Can someone that's affected by this please run KWin with the linked MR applied, and upload the file it puts into the home directory? It should contain pretty much all information we need to debug this properly
Comment 9 Zamundaaa 2024-06-24 13:52:09 UTC
*** Bug 488860 has been marked as a duplicate of this bug. ***
Comment 10 Zamundaaa 2024-06-24 13:52:14 UTC
*** Bug 488821 has been marked as a duplicate of this bug. ***
Comment 11 Zamundaaa 2024-06-24 14:09:05 UTC
Git commit 060db4cc7c30451522a595d3f6ce6f320409f133 by Xaver Hugl.
Committed on 24/06/2024 at 13:40.
Pushed by zamundaaa into branch 'Plasma/6.1'.

core/renderloop: log frame statistics into a file

Data like target vs. actual pageflip time, and render times is often needed for
debugging or optimizing render time predictions, so this commit makes KWin print
that information to a file in the home directory whenever KWIN_LOG_PERFORMANCE_DATA
is set.


(cherry picked from commit a169114fee661e61519f06c0567f4daa486dc336)

Co-authored-by: Xaver Hugl <xaver.hugl@gmail.com>

M  +10   -5    src/core/renderbackend.cpp
M  +4    -3    src/core/renderbackend.h
M  +18   -2    src/core/renderloop.cpp
M  +5    -1    src/core/renderloop_p.h

https://invent.kde.org/plasma/kwin/-/commit/060db4cc7c30451522a595d3f6ce6f320409f133
Comment 12 Bug Janitor Service 2024-06-24 15:06:43 UTC
A possibly relevant merge request was started @ https://invent.kde.org/plasma/kwin/-/merge_requests/5979
Comment 13 Bug Janitor Service 2024-06-24 17:19:38 UTC
A possibly relevant merge request was started @ https://invent.kde.org/plasma/kwin/-/merge_requests/5982
Comment 14 Zamundaaa 2024-06-24 17:27:57 UTC
If anyone affected could test https://invent.kde.org/plasma/kwin/-/merge_requests/5982, that would also be good. Preferably with https://invent.kde.org/plasma/kwin/-/commit/a169114fee661e61519f06c0567f4daa486dc336 logs with and without this MR
Comment 15 Zamundaaa 2024-06-25 13:27:33 UTC
Git commit c5fb21fd8baa7033a7828805ab937146c7af2480 by Xaver Hugl.
Committed on 25/06/2024 at 13:07.
Pushed by zamundaaa into branch 'master'.

core/renderloop: assume high render times if the last frame has been a while ago

This helps avoid some frame drops after the GPU may have went into a lower power
state. While this isn't generally noticeable, avoiding this makes noticing and
debugging actually relevant frame drops easier

M  +6    -1    src/core/renderloop.cpp

https://invent.kde.org/plasma/kwin/-/commit/c5fb21fd8baa7033a7828805ab937146c7af2480
Comment 16 Bug Janitor Service 2024-06-25 15:46:56 UTC
A possibly relevant merge request was started @ https://invent.kde.org/plasma/kwin/-/merge_requests/5993
Comment 17 Zamundaaa 2024-06-25 21:39:02 UTC
Git commit d2cbdbab307957b7284141e30dd9829a37413bed by Xaver Hugl.
Committed on 25/06/2024 at 16:57.
Pushed by zamundaaa into branch 'Plasma/6.1'.

core/renderloop: assume high render times if the last frame has been a while ago

This helps avoid some frame drops after the GPU may have went into a lower power
state. While this isn't generally noticeable, avoiding this makes noticing and
debugging actually relevant frame drops easier


(cherry picked from commit c5fb21fd8baa7033a7828805ab937146c7af2480)

Co-authored-by: Xaver Hugl <xaver.hugl@gmail.com>

M  +6    -1    src/core/renderloop.cpp

https://invent.kde.org/plasma/kwin/-/commit/d2cbdbab307957b7284141e30dd9829a37413bed
Comment 18 pyro_kde_bugs 2024-06-26 18:41:10 UTC
I've been doing some testing with the FPS counter on my screen.  I know this isn't meant to be 100% accurate but in this case the user experience is reflected within the desktop effect.

I've done quite a bit of testing and think it may have something to do with the desktop blur effect, or at least the issue seems to be exaggerated when a window that uses blur is in view. Launching konsole with a blurred background tanks my FPS to 30-40fps while no blurred windows are visible I sit about 100-160fps. I'll include my system details just in case.

Operating System: EndeavourOS 
KDE Plasma Version: 6.1.0
KDE Frameworks Version: 6.3.0
Qt Version: 6.7.2
Kernel Version: 6.9.6-arch1-1 (64-bit)
Graphics Platform: Wayland
Processors: 20 × 13th Gen Intel® Core™ i7-1370P
Memory: 62.5 GiB of RAM
Graphics Processor: Mesa Intel® Graphics
Manufacturer: Framework
Product Name: Laptop (13th Gen Intel Core)
System Version: A7

I haven't tried to run the mentioned merge requests to gather debug data because I'm not actually 100% sure how to run it on my system.
Comment 19 hbr 2024-06-27 09:22:27 UTC
Created attachment 171074 [details]
KWIN_Performance_Data_Triple_Buffering.zip

Updated to Plasma 6.1.1 today and recorded the performance data for both my AMD desktop and Intel notebook.

Updated to Plasma 6.1.1
Enabled performance logging
Cleared Plasma related caches
Rebooted

For the Intel notebook I recorded with 3 different kernels (Arch normal, Arch LTS and Arch Zen) with triple buffering enabled and one time with it disabled.

For the AMD desktop I recorded with 2 different power profiles for the GPU (3D Fullscreen and Compute), both being more aggressive in raising the clocks than the default profile and Compute being more aggressive than 3D Fullscreen. 2 times with triple buffering enabled and one time with triple buffering disabled.

Plasma 6.1.1 already shows improvements with triple buffering enabled on both my desktop and notebook. But stutters can still be noticed on both, especially when maximizing / minimizing windows (often when done in quick succession) or when the notebook has been idle for a bit.

Disabling triple buffering again improves things on the notebook and completely removes stutters on the desktop.
Comment 20 pyro_kde_bugs 2024-06-27 15:34:20 UTC
(In reply to pyro_kde_bugs from comment #18)
> I've been doing some testing with the FPS counter on my screen.  I know this
> isn't meant to be 100% accurate but in this case the user experience is
> reflected within the desktop effect.
> 
> I've done quite a bit of testing and think it may have something to do with
> the desktop blur effect, or at least the issue seems to be exaggerated when
> a window that uses blur is in view. Launching konsole with a blurred
> background tanks my FPS to 30-40fps while no blurred windows are visible I
> sit about 100-160fps. I'll include my system details just in case.
> 
> Operating System: EndeavourOS 
> KDE Plasma Version: 6.1.0
> KDE Frameworks Version: 6.3.0
> Qt Version: 6.7.2
> Kernel Version: 6.9.6-arch1-1 (64-bit)
> Graphics Platform: Wayland
> Processors: 20 × 13th Gen Intel® Core™ i7-1370P
> Memory: 62.5 GiB of RAM
> Graphics Processor: Mesa Intel® Graphics
> Manufacturer: Framework
> Product Name: Laptop (13th Gen Intel Core)
> System Version: A7
> 
> I haven't tried to run the mentioned merge requests to gather debug data
> because I'm not actually 100% sure how to run it on my system.

Ignore this one, I figured out my issue was not related to this bug at all, it turns out my RAM got slightly de-seated and was causing my CPU to run at its minimum idle frequency all the time (400mhz). After I re-seated the RAM my low fps issues went away completely.  Sorry for adding confusion into the mix, disregard the comment this is in reply to.
Comment 21 hbr 2024-07-04 19:07:55 UTC
Updated to Plasma 6.1.2 today and recorded performance data again for the notebook and desktop.

On the notebook I can't really see a difference anymore between triple buffering enabled or disabled. The severe stutters are gone.

On the desktop the severe stutters also seem to be gone. But animations still seem to be a bit smoother with dynamic triple buffering disabled.
Comment 22 hbr 2024-07-04 19:08:48 UTC
Created attachment 171380 [details]
KWIN_6_1_2_Performance_Data_Triple_Buffering.zip

Performance data from Plasma 6.1.2 attached.
Comment 23 Marcus 2024-07-05 16:09:31 UTC
Created attachment 171409 [details]
perf stats csv files running KWin with MR 5982 and without

I built KWin with the hysteresis MR (https://invent.kde.org/plasma/kwin/-/merge_requests/5982) applied and ran it with logging.
I also ran stock 6.1.2 KWin with logging. This was done on a notebook with intel UHD 630 integrated gpu in the 1080@60Hz mode.

I didn't notice improvements in either cases on my end, framerate still gets halved until certain types of large screen updates happen (pulling the overview, alt-tabbing, switching vdesktops, etc), at which point it goes back to 60fps for a short time. just like the others, disabling triple-buffering fixes it. This is much less of an issue in 144Hz mode.
Comment 24 Zamundaaa 2024-07-16 14:44:48 UTC
Git commit 0672313c209232341581459c656b8e8fa1cf8158 by Xaver Hugl.
Committed on 16/07/2024 at 14:21.
Pushed by zamundaaa into branch 'master'.

core/renderloop: add some hysteresis to triple buffering

Switching to triple buffering requires dropping a frame, so if we constantly
switch back and forth between double and triple buffering, that can cause
very visible performance issues

M  +31   -7    src/core/renderloop.cpp
M  +2    -0    src/core/renderloop_p.h

https://invent.kde.org/plasma/kwin/-/commit/0672313c209232341581459c656b8e8fa1cf8158
Comment 25 Bug Janitor Service 2024-07-16 15:54:02 UTC
A possibly relevant merge request was started @ https://invent.kde.org/plasma/kwin/-/merge_requests/6114
Comment 26 Zamundaaa 2024-07-16 16:01:18 UTC
Thanks for the measurements. Seems like the MR doesn't make much of a difference except (like expected) keeping triple buffering enabled more often, so your problem is elsewhere.

My analysis tool says that 0.45% of frames are dropped, and half of those are from late commits. For those I suspect they happen when scheduling goes really bad in combination with more commits needing to be processed in the commit thread. If you increase the 1500us safety margin at https://invent.kde.org/plasma/kwin/-/blob/0672313c209232341581459c656b8e8fa1cf8158/src/backends/drm/drm_commit_thread.cpp#L296, does that reduce the stutter?
Comment 27 Zamundaaa 2024-07-16 16:11:35 UTC
Git commit ce1ba4252552c7f314149e8be87d5d63fca0210c by Xaver Hugl.
Committed on 16/07/2024 at 15:53.
Pushed by zamundaaa into branch 'Plasma/6.1'.

core/renderloop: add some hysteresis to triple buffering

Switching to triple buffering requires dropping a frame, so if we constantly
switch back and forth between double and triple buffering, that can cause
very visible performance issues


(cherry picked from commit 0672313c209232341581459c656b8e8fa1cf8158)

Co-authored-by: Xaver Hugl <xaver.hugl@gmail.com>

M  +31   -7    src/core/renderloop.cpp
M  +2    -0    src/core/renderloop_p.h

https://invent.kde.org/plasma/kwin/-/commit/ce1ba4252552c7f314149e8be87d5d63fca0210c
Comment 28 Marcus 2024-07-19 14:40:24 UTC
Thanks for the response!

So I tested different values for the extra safety margin on a Plasma/6.1 build, and increasing it does help reduce dropped frames.
I need to make it pretty high, closing in on the frame time itself (not counting vblank), to have a noticeable effect though.
Anything lower than ~10ms still makes the framerate eventually tank, but above ~14ms the 60fps target is consistently maintained.

I also took the liberty of running the analysis tool myself:
- 6 000 us
> - total of 46008 frames
> - dropped 40 frames (0.087%)
>    - 11 because of too late commits (0.024%)
>    - 16 because of late rendering start (0.035%)
>    - 13 because of render times (0.028%)
> - 2257us average render time
> - 2000us median render time
> - 363s total 'unnecessary' latency
> - would've estimated render times too low in 59 frames (0.13%)
> - would've estimated 50+% too much render time in 40951 frames (89%)
- 13 000 us
> - total of 48274 frames
> - dropped 24 frames (0.05%)
>    - 8 because of too late commits (0.017%)
>    - 9 because of late rendering start (0.019%)
>    - 7 because of render times (0.015%)
> - 2224us average render time
> - 2000us median render time
> - 259s total 'unnecessary' latency
> - would've estimated render times too low in 88 frames (0.18%)
> - would've estimated 50+% too much render time in 43972 frames (91%)
- 15 000 us
> - total of 50195 frames
> - dropped 23 frames (0.046%)
>    - 2 because of too late commits (0.004%)
>    - 14 because of late rendering start (0.028%)
>    - 7 because of render times (0.014%)
> - 2081us average render time
> - 2000us median render time
> - 183s total 'unnecessary' latency
> - would've estimated render times too low in 71 frames (0.14%)
> - would've estimated 50+% too much render time in 21862 frames (44%)
Comment 29 Zamundaaa 2024-07-19 14:57:47 UTC
hmm yeah I was more thinking about increasing it by 0.5ms, making it that high is not an option as it increases latency by a whole frame too. It not making much of a difference also means that the commit thread isn't the problem.

If you increase the "1ms" in https://invent.kde.org/plasma/kwin/-/blob/2340470a9a298e1bdb157a3cd4cc948727d3f657/src/core/renderloop.cpp#L55 a bit instead, does that help as well?

Though I see you're already at less than 0.1% of frames being dropped; a few dropped frames is always expected because that just happens when CPU or GPU load suddenly and unexpectedly increase a bunch. So idk if we can improve things a lot for you specifically.
Comment 30 Marcus 2024-07-19 21:58:42 UTC
Increasing the 1ms extra is the same deal; it helps but I need to make it very high.
so it doesn't seem to be related to dropped frames.
> - total of 42978 frames
> - dropped 105 frames (0.24%)
>     - 87 because of too late commits (0.2%)
>     - 12 because of late rendering start (0.028%)
>     - 6 because of render times (0.014%)
But my issue isn't stuttering due to dropped frames per se; it's that the frame rate drops to 30fps for long periods of time, like a starved double-buffered vsync, unless I (apparently) add myself a frame of latency or turn off triple-buffering altogether, and only at 60Hz. and somehow this doesn't show in the perf stats.

if there's no obvious solution to this I can just disable tb indefinitely, but it's still a weird issue.
Comment 31 Marcus 2024-07-19 23:55:18 UTC
looking at RenderLoopPrivate::scheduleRepaint more closely it seems that if I make the safety margin
Comment 32 Marcus 2024-07-19 23:57:42 UTC
Sorry, ignore that.
Comment 33 Zamundaaa 2024-07-24 16:36:26 UTC
*** Bug 490358 has been marked as a duplicate of this bug. ***
Comment 34 Bug Janitor Service 2024-07-24 20:27:37 UTC
A possibly relevant merge request was started @ https://invent.kde.org/plasma/kwin/-/merge_requests/6168
Comment 35 Zamundaaa 2024-07-24 20:28:38 UTC
Does the halved refresh rate issue go away with that MR?
Comment 36 Zamundaaa 2024-07-25 14:07:17 UTC
I think I found the core of the issue, and I added a commit to the linked MR that I'm relatively confident should finally actually fix this bug. Please test it!
Comment 37 Michael Marley 2024-07-25 17:15:02 UTC
Testing with the new patches I no longer see the drops to 30fps that I saw previously, so that part seems fixed to me.  I'm still getting some stuttering that I wasn't previously with Plasma 5.x though, so I'm going to re-open https://bugs.kde.org/show_bug.cgi?id=490358.
Comment 38 Zamundaaa 2024-07-26 13:08:54 UTC
Git commit 457b3a47ffe91b335ab18170cfb830d35217b9a8 by Xaver Hugl.
Committed on 26/07/2024 at 12:59.
Pushed by zamundaaa into branch 'master'.

core/renderloop: don't move the target presentation timestamp back when rescheduling

Re-doing the frame scheduling for an already scheduled composite cycle was meant to
adjust to small timing changes in the presentation timestamp, but if the expected
compositing time was close to vblank, it could happen that this would instead move
the target presentation timestamp and effectively drop a frame.

To fix that, this commit makes the presentation timestamp be adjusted only to be more
accurate, but still target the same vblank interval.

M  +11   -1    src/core/renderloop.cpp

https://invent.kde.org/plasma/kwin/-/commit/457b3a47ffe91b335ab18170cfb830d35217b9a8
Comment 39 Zamundaaa 2024-07-26 13:09:02 UTC
Git commit d0f0481860ca0d8ebb88121c61544fbdc7a04d2f by Xaver Hugl.
Committed on 26/07/2024 at 12:59.
Pushed by zamundaaa into branch 'master'.

core/renderloop: fix triple buffering hysteresis

The way it was implemented it only changed the target pageflip, but not the time at which
KWin would start compositing, which could make it skip scheduling a frame for each second
vblank and drop the refresh rate to half of what it should be that way

M  +2    -0    src/core/renderloop.cpp

https://invent.kde.org/plasma/kwin/-/commit/d0f0481860ca0d8ebb88121c61544fbdc7a04d2f
Comment 40 Zamundaaa 2024-07-26 13:27:06 UTC
Git commit 7b8f24698f797143531f0d5723cc6c263b9175e8 by Xaver Hugl.
Committed on 26/07/2024 at 13:10.
Pushed by zamundaaa into branch 'Plasma/6.1'.

core/renderloop: don't move the target presentation timestamp back when rescheduling

Re-doing the frame scheduling for an already scheduled composite cycle was meant to
adjust to small timing changes in the presentation timestamp, but if the expected
compositing time was close to vblank, it could happen that this would instead move
the target presentation timestamp and effectively drop a frame.

To fix that, this commit makes the presentation timestamp be adjusted only to be more
accurate, but still target the same vblank interval.

M  +11   -1    src/core/renderloop.cpp

https://invent.kde.org/plasma/kwin/-/commit/7b8f24698f797143531f0d5723cc6c263b9175e8
Comment 41 Zamundaaa 2024-07-26 13:27:14 UTC
Git commit aad82e9c772d52e853e7fa36a97620e41172df95 by Xaver Hugl.
Committed on 26/07/2024 at 13:10.
Pushed by zamundaaa into branch 'Plasma/6.1'.

core/renderloop: fix triple buffering hysteresis

The way it was implemented it only changed the target pageflip, but not the time at which
KWin would start compositing, which could make it skip scheduling a frame for each second
vblank and drop the refresh rate to half of what it should be that way

M  +2    -0    src/core/renderloop.cpp

https://invent.kde.org/plasma/kwin/-/commit/aad82e9c772d52e853e7fa36a97620e41172df95