-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Buffer 'docker stats' text to avoid terminal flickering #5586
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #5586 +/- ##
==========================================
- Coverage 59.64% 59.63% -0.02%
==========================================
Files 346 346
Lines 29207 29213 +6
==========================================
Hits 17421 17421
- Misses 10817 10823 +6
Partials 969 969 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
I checked on my system - while it didn't help much with kitty, it makes a difference on slower terminals like iTerm.
That's still an improvement over the previous behavior though!
Overall LGTM, but left some nits.
cli/command/container/stats.go
Outdated
|
||
// Buffer to store formatted stats text. | ||
// Once formatted, it will be printed in one write to avoid screen flickering. | ||
var statsTextBuffer strings.Builder |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest using bytes.Buffer
here instead, see my comment below
var statsTextBuffer strings.Builder | |
var statsTextBuffer bytes.Buffer |
Thanks for a quick review! I've replaced strings.Builder with bytes.Buffer :) And yes, the "proper" solution for this would involve keeping track of the two "frames" and only redrawing the parts that have changed. But that would be a lot more involved than what we are doing now. Curious that you are still seeing the issue with kitty, since for me kitty worked well even before this change. I'm guessing it could be due to GPU drivers, and similar low level stuff, because AFAIK kitty uses GPU for text rendering. |
I think kitty is just fast enough to output the characters on the screen as soon as they're read. This could probably be tweaked with kitty settings to decrease the output frequency. But yeah, the proper solution would be to double-buffer the output and only change the actual difference. However, this is definitely an improvement for most users :) |
Thanks! Could you also squash the commits and make sure your commit has a DCO? |
done! |
This change reduces the flickering of the terminal when running `docker stats` by buffering the formatted stats text and printing it in one write. Should also consume less CPU as we now only have to issue a single syscall to write the stats text to the terminal. Signed-off-by: Giedrius Jonikas <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, LGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thanks :')
This change reduces the flickering of the terminal when running
docker stats
by buffering the formatted stats text and printing it in one write.Should also consume less CPU as we now only have to issue a single syscall to write the stats text to the terminal.
I suspect it fixes #4348, but I can not confirm since that issue was reported on WSL.
- What I did
On all of my Linux machines (Fedora Linux + GNOME) I can see terminal flickering when running 'docker stats'.
I've tested this with gnome-terminal/xfce4-terminal/xterm and it flickers on all. The only term that does not flicker is kitty.
I've also tested this on GNOME running Wayland and X11. I can see no difference in behaviour between Wayland/X11.
I've tested this on two machines: Thinkpad T14s gen1 running AMD zen, and a desktop PC running AMD zen chip with Radeon GPU. Both behave identically.
recording of the flickering:
https://github.com/user-attachments/assets/05f8278e-05c3-411e-bb32-dad262136937
'docker stats' used to issue multiple write calls to stdout. Starting with the "clear screen" command and following multiple small write requests.
I'm now bundling all of those into a single string that is then written out to the stdout as a single request.
This fixes the flickering (as well as reduces syscalls required for each update) on both of my machines.
I've tested this on Fedora 41 GNOME with the following terminal emulators: gnome-terminal/xfce4-terminal/xterm/kitty.
I've tried things like resizing the window as 'docker stats' was running. It all works flawlessly now :)
- Description for the changelog
- A picture of a cute animal (not mandatory but encouraged)
A cat that sometimes roams outside my flat. Me and my flatmates call him Phantom 🐱