[PATCH 1/4] gputop: print percentage number

Lucas De Marchi lucas.demarchi at intel.com
Wed May 1 17:33:00 UTC 2024


Besides printing the bar, also print the raw number for easy
visualization of small quantities. While at it, make sure gputop still
works with small console widths.

v2: Use %5.1f instead of %.1f so it also aligns the non-decimal part

Signed-off-by: Lucas De Marchi <lucas.demarchi at intel.com>
---
 tools/gputop.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/tools/gputop.c b/tools/gputop.c
index 8cec951b4..7fd9e9790 100644
--- a/tools/gputop.c
+++ b/tools/gputop.c
@@ -42,17 +42,19 @@ static void n_spaces(const unsigned int n)
 
 static void print_percentage_bar(double percent, int max_len)
 {
-	int bar_len, i, len = max_len - 2;
+	int bar_len, i, len = max_len - 1;
 	const int w = 8;
 
-	assert(max_len > 0);
+	len -= printf("|%5.1f%% ", percent);
+
+	/* no space left for bars, do what we can */
+	if (len < 0)
+		len = 0;
 
 	bar_len = ceil(w * percent * len / 100.0);
 	if (bar_len > w * len)
 		bar_len = w * len;
 
-	putchar('|');
-
 	for (i = bar_len; i >= w; i -= w)
 		printf("%s", bars[w]);
 	if (i)
-- 
2.45.0



More information about the igt-dev mailing list