[PATCH] tools/gputop: Fix zero output when stdout is not a terminal

Vidya Srinivas
2026-04-06T06:07:58Z

When gputop's output is redirected to a file or run via adb shell
(e.g., on Android), update_console_size() calls ioctl(0, TIOCGWINSZ)
on stdin which may not be a terminal. If stdin is a pipe or
redirected, the ioctl returns -1 and con_w/con_h remain at their
initial value of -1.

This causes the main display loop to immediately break since
'lines >= con_h' evaluates to true (0 >= -1), resulting in zero
output being produced.

Fix this by trying ioctl on stdout first, then stdin, then stderr
before falling back to a default of 80x50. This ensures the real
terminal size is obtained when any of the standard file descriptors
is connected to a terminal, and provides a sensible default when
none are (e.g., full redirection or adb shell).

This affects anyone using gputop in automation, CI pipelines, or any
non-interactive context on Linux and Android where output is
redirected or piped.

v2: Try stdout, stdin, and stderr for terminal size before falling
back to defaults (Soham Purkait)

Signed-off-by: Vidya Srinivas <vidya.srinivas@intel.com>
Reviewed-by: Kamil Konieczny <kamil.konieczny@linux.intel.com>
---
tools/gputop.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/tools/gputop.c b/tools/gputop.c
index 9b2e8cb6f..3b5e3c0cf 100644
--- a/tools/gputop.c
+++ b/tools/gputop.c
@@ -505,8 +505,13 @@ static void update_console_size(int *w, int *h)
{
struct winsize ws = {};

- if (ioctl(0, TIOCGWINSZ, &ws) == -1)
+ if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
+ ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1 &&
+ ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1) {
+ *w = 80;
+ *h = 50;
return;
+ }

*w = ws.ws_col;
*h = ws.ws_row;
--
2.45.2