Skip to content
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

Merged
merged 1 commit into from
Oct 31, 2024

Conversation

Giedriusj1
Copy link
Contributor

@Giedriusj1 Giedriusj1 commented Oct 30, 2024

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

`docker stats` output is now buffered to reduce flickering issues 

- 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 🐱

cat_phantom

@codecov-commenter
Copy link

codecov-commenter commented Oct 30, 2024

Codecov Report

Attention: Patch coverage is 0% with 12 lines in your changes missing coverage. Please review.

Project coverage is 59.63%. Comparing base (aa331e9) to head (0b16070).
Report is 9 commits behind head on master.

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              

Copy link
Collaborator

@vvoland vvoland left a 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.


// Buffer to store formatted stats text.
// Once formatted, it will be printed in one write to avoid screen flickering.
var statsTextBuffer strings.Builder
Copy link
Collaborator

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

Suggested change
var statsTextBuffer strings.Builder
var statsTextBuffer bytes.Buffer

cli/command/container/stats.go Show resolved Hide resolved
@Giedriusj1
Copy link
Contributor Author

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.

@vvoland
Copy link
Collaborator

vvoland commented Oct 31, 2024

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 :)

@vvoland
Copy link
Collaborator

vvoland commented Oct 31, 2024

Thanks! Could you also squash the commits and make sure your commit has a DCO?

@Giedriusj1
Copy link
Contributor Author

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]>
Copy link
Collaborator

@vvoland vvoland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM!

Copy link
Member

@Benehiko Benehiko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@laurazard laurazard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks :')

@laurazard laurazard merged commit 2995631 into docker:master Oct 31, 2024
92 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

docker stats flickers
5 participants