All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tvrtko Ursulin <tvrtko.ursulin@linux.intel.com>
To: igt-dev@lists.freedesktop.org, Intel-gfx@lists.freedesktop.org
Subject: [Intel-gfx] [PATCH i-g-t 1/3] intel_gpu_top: Display large pids nicely in interactive mode
Date: Tue, 28 Mar 2023 13:54:27 +0100	[thread overview]
Message-ID: <20230328125429.1970202-2-tvrtko.ursulin@linux.intel.com> (raw)
In-Reply-To: <20230328125429.1970202-1-tvrtko.ursulin@linux.intel.com>

From: Tvrtko Ursulin <tvrtko.ursulin@intel.com>

So far the width of the PID column was hardcoded to six characters which
is not enough on systems with high uptime, where PID numbers can grow
large, and results in broken line formatting.

Fix it by tracking the largest width for both the pid and name fields and
use them dynamically.

Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
---
 tools/intel_gpu_top.c | 66 +++++++++++++++++++++++++++++++++++++------
 1 file changed, 58 insertions(+), 8 deletions(-)

diff --git a/tools/intel_gpu_top.c b/tools/intel_gpu_top.c
index a4302aa389b4..39be916297e4 100644
--- a/tools/intel_gpu_top.c
+++ b/tools/intel_gpu_top.c
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2007-2021 Intel Corporation
+ * Copyright © 2007-2023 Intel Corporation
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -693,6 +693,7 @@ struct client {
 	enum client_status status;
 	unsigned int id;
 	unsigned int pid;
+	char pid_str[10];
 	char name[24];
 	char print_name[24];
 	unsigned int samples;
@@ -709,6 +710,9 @@ struct clients {
 	unsigned int num_classes;
 	struct engine_class *class;
 
+	int max_pid_len;
+	int max_name_len;
+
 	char pci_slot[64];
 
 	struct client *client;
@@ -758,9 +762,14 @@ update_client(struct client *c, unsigned int pid, char *name,
 	      const struct drm_client_fdinfo *info)
 {
 	unsigned int i;
+	int len;
 
-	if (c->pid != pid)
+	if (c->pid != pid) {
 		c->pid = pid;
+		len = snprintf(c->pid_str, sizeof(c->pid_str) - 1, "%u", pid);
+		if (len > c->clients->max_pid_len)
+			c->clients->max_pid_len = len;
+	}
 
 	if (strcmp(c->name, name)) {
 		char *p;
@@ -774,6 +783,10 @@ update_client(struct client *c, unsigned int pid, char *name,
 				*p = '*';
 			p++;
 		}
+
+		len = strlen(c->print_name);
+		if (len > c->clients->max_name_len)
+			c->clients->max_name_len = len;
 	}
 
 	c->last_runtime = 0;
@@ -990,6 +1003,7 @@ static struct clients *display_clients(struct clients *clients)
 			ac->id = -c->pid;
 			ac->pid = c->pid;
 			strcpy(ac->name, c->name);
+			strcpy(ac->pid_str, c->pid_str);
 			strcpy(ac->print_name, c->print_name);
 			ac->val = calloc(clients->num_classes,
 					 sizeof(ac->val[0]));
@@ -1013,6 +1027,9 @@ static struct clients *display_clients(struct clients *clients)
 	aggregated->num_clients = num;
 	aggregated->active_clients = num;
 
+	aggregated->max_pid_len = clients->max_pid_len;
+	aggregated->max_name_len = clients->max_name_len;
+
 	clients = aggregated;
 
 out:
@@ -1104,9 +1121,34 @@ static size_t readat2buf(int at, const char *name, char *buf, const size_t sz)
 	}
 }
 
+static void clients_update_max_lengths(struct clients *clients)
+{
+	struct client *c;
+	int tmp;
+
+	clients->max_name_len = 0;
+	clients->max_pid_len = 0;
+
+	for_each_client(clients, c, tmp) {
+		int len;
+
+		if (c->status != ALIVE)
+			continue; /* Array not yet sorted by the caller. */
+
+		len = strlen(c->print_name);
+		if (len > clients->max_name_len)
+			clients->max_name_len = len;
+
+		len = strlen(c->pid_str);
+		if (len > clients->max_pid_len)
+			clients->max_pid_len = len;
+	}
+}
+
 static struct clients *scan_clients(struct clients *clients, bool display)
 {
 	struct dirent *proc_dent;
+	bool freed = false;
 	struct client *c;
 	DIR *proc_dir;
 	int tmp;
@@ -1208,12 +1250,17 @@ next:
 	closedir(proc_dir);
 
 	for_each_client(clients, c, tmp) {
-		if (c->status == PROBE)
+		if (c->status == PROBE) {
 			free_client(c);
-		else if (c->status == FREE)
+			freed = true;
+		} else if (c->status == FREE) {
 			break;
+		}
 	}
 
+	if (freed)
+		clients_update_max_lengths(clients);
+
 	return display ? display_clients(clients) : clients;
 }
 
@@ -2172,15 +2219,16 @@ print_clients_header(struct clients *clients, int lines,
 		     int con_w, int con_h, int *class_w)
 {
 	if (output_mode == INTERACTIVE) {
-		const char *pidname = "   PID              NAME ";
 		unsigned int num_active = 0;
-		int len = strlen(pidname);
+		int len;
 
 		if (lines++ >= con_h)
 			return lines;
 
 		printf("\033[7m");
-		printf("%s", pidname);
+		len = printf("%*s %*s ",
+			     clients->max_pid_len, "PID",
+			     clients->max_name_len, "NAME");
 
 		if (lines++ >= con_h || len >= con_w)
 			return lines;
@@ -2241,7 +2289,9 @@ print_client(struct client *c, struct engines *engines, double t, int lines,
 
 		lines++;
 
-		printf("%6u %17s ", c->pid, c->print_name);
+		printf("%*s %*s ",
+		       clients->max_pid_len, c->pid_str,
+		       clients->max_name_len, c->print_name);
 
 		for (i = 0; c->samples > 1 && i < clients->num_classes; i++) {
 			double pct;
-- 
2.37.2


WARNING: multiple messages have this Message-ID (diff)
From: Tvrtko Ursulin <tvrtko.ursulin@linux.intel.com>
To: igt-dev@lists.freedesktop.org, Intel-gfx@lists.freedesktop.org
Cc: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Subject: [igt-dev] [PATCH i-g-t 1/3] intel_gpu_top: Display large pids nicely in interactive mode
Date: Tue, 28 Mar 2023 13:54:27 +0100	[thread overview]
Message-ID: <20230328125429.1970202-2-tvrtko.ursulin@linux.intel.com> (raw)
In-Reply-To: <20230328125429.1970202-1-tvrtko.ursulin@linux.intel.com>

From: Tvrtko Ursulin <tvrtko.ursulin@intel.com>

So far the width of the PID column was hardcoded to six characters which
is not enough on systems with high uptime, where PID numbers can grow
large, and results in broken line formatting.

Fix it by tracking the largest width for both the pid and name fields and
use them dynamically.

Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
---
 tools/intel_gpu_top.c | 66 +++++++++++++++++++++++++++++++++++++------
 1 file changed, 58 insertions(+), 8 deletions(-)

diff --git a/tools/intel_gpu_top.c b/tools/intel_gpu_top.c
index a4302aa389b4..39be916297e4 100644
--- a/tools/intel_gpu_top.c
+++ b/tools/intel_gpu_top.c
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2007-2021 Intel Corporation
+ * Copyright © 2007-2023 Intel Corporation
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -693,6 +693,7 @@ struct client {
 	enum client_status status;
 	unsigned int id;
 	unsigned int pid;
+	char pid_str[10];
 	char name[24];
 	char print_name[24];
 	unsigned int samples;
@@ -709,6 +710,9 @@ struct clients {
 	unsigned int num_classes;
 	struct engine_class *class;
 
+	int max_pid_len;
+	int max_name_len;
+
 	char pci_slot[64];
 
 	struct client *client;
@@ -758,9 +762,14 @@ update_client(struct client *c, unsigned int pid, char *name,
 	      const struct drm_client_fdinfo *info)
 {
 	unsigned int i;
+	int len;
 
-	if (c->pid != pid)
+	if (c->pid != pid) {
 		c->pid = pid;
+		len = snprintf(c->pid_str, sizeof(c->pid_str) - 1, "%u", pid);
+		if (len > c->clients->max_pid_len)
+			c->clients->max_pid_len = len;
+	}
 
 	if (strcmp(c->name, name)) {
 		char *p;
@@ -774,6 +783,10 @@ update_client(struct client *c, unsigned int pid, char *name,
 				*p = '*';
 			p++;
 		}
+
+		len = strlen(c->print_name);
+		if (len > c->clients->max_name_len)
+			c->clients->max_name_len = len;
 	}
 
 	c->last_runtime = 0;
@@ -990,6 +1003,7 @@ static struct clients *display_clients(struct clients *clients)
 			ac->id = -c->pid;
 			ac->pid = c->pid;
 			strcpy(ac->name, c->name);
+			strcpy(ac->pid_str, c->pid_str);
 			strcpy(ac->print_name, c->print_name);
 			ac->val = calloc(clients->num_classes,
 					 sizeof(ac->val[0]));
@@ -1013,6 +1027,9 @@ static struct clients *display_clients(struct clients *clients)
 	aggregated->num_clients = num;
 	aggregated->active_clients = num;
 
+	aggregated->max_pid_len = clients->max_pid_len;
+	aggregated->max_name_len = clients->max_name_len;
+
 	clients = aggregated;
 
 out:
@@ -1104,9 +1121,34 @@ static size_t readat2buf(int at, const char *name, char *buf, const size_t sz)
 	}
 }
 
+static void clients_update_max_lengths(struct clients *clients)
+{
+	struct client *c;
+	int tmp;
+
+	clients->max_name_len = 0;
+	clients->max_pid_len = 0;
+
+	for_each_client(clients, c, tmp) {
+		int len;
+
+		if (c->status != ALIVE)
+			continue; /* Array not yet sorted by the caller. */
+
+		len = strlen(c->print_name);
+		if (len > clients->max_name_len)
+			clients->max_name_len = len;
+
+		len = strlen(c->pid_str);
+		if (len > clients->max_pid_len)
+			clients->max_pid_len = len;
+	}
+}
+
 static struct clients *scan_clients(struct clients *clients, bool display)
 {
 	struct dirent *proc_dent;
+	bool freed = false;
 	struct client *c;
 	DIR *proc_dir;
 	int tmp;
@@ -1208,12 +1250,17 @@ next:
 	closedir(proc_dir);
 
 	for_each_client(clients, c, tmp) {
-		if (c->status == PROBE)
+		if (c->status == PROBE) {
 			free_client(c);
-		else if (c->status == FREE)
+			freed = true;
+		} else if (c->status == FREE) {
 			break;
+		}
 	}
 
+	if (freed)
+		clients_update_max_lengths(clients);
+
 	return display ? display_clients(clients) : clients;
 }
 
@@ -2172,15 +2219,16 @@ print_clients_header(struct clients *clients, int lines,
 		     int con_w, int con_h, int *class_w)
 {
 	if (output_mode == INTERACTIVE) {
-		const char *pidname = "   PID              NAME ";
 		unsigned int num_active = 0;
-		int len = strlen(pidname);
+		int len;
 
 		if (lines++ >= con_h)
 			return lines;
 
 		printf("\033[7m");
-		printf("%s", pidname);
+		len = printf("%*s %*s ",
+			     clients->max_pid_len, "PID",
+			     clients->max_name_len, "NAME");
 
 		if (lines++ >= con_h || len >= con_w)
 			return lines;
@@ -2241,7 +2289,9 @@ print_client(struct client *c, struct engines *engines, double t, int lines,
 
 		lines++;
 
-		printf("%6u %17s ", c->pid, c->print_name);
+		printf("%*s %*s ",
+		       clients->max_pid_len, c->pid_str,
+		       clients->max_name_len, c->print_name);
 
 		for (i = 0; c->samples > 1 && i < clients->num_classes; i++) {
 			double pct;
-- 
2.37.2

  reply	other threads:[~2023-03-28 12:54 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-28 12:54 [Intel-gfx] [PATCH i-g-t 0/3] Assorted intel_gpu_top tweaks Tvrtko Ursulin
2023-03-28 12:54 ` [igt-dev] " Tvrtko Ursulin
2023-03-28 12:54 ` Tvrtko Ursulin [this message]
2023-03-28 12:54   ` [igt-dev] [PATCH i-g-t 1/3] intel_gpu_top: Display large pids nicely in interactive mode Tvrtko Ursulin
2023-04-14 15:24   ` [Intel-gfx] " Kamil Konieczny
2023-04-14 15:24     ` [igt-dev] " Kamil Konieczny
2023-03-28 12:54 ` [Intel-gfx] [PATCH i-g-t 2/3] intel_gpu_top: Use full console width for global metrics Tvrtko Ursulin
2023-03-28 12:54   ` [igt-dev] " Tvrtko Ursulin
2023-04-14 15:26   ` [Intel-gfx] " Kamil Konieczny
2023-04-14 15:26     ` Kamil Konieczny
2023-03-28 12:54 ` [Intel-gfx] [PATCH i-g-t 3/3] intel_gpu_top: Show non-normalized client usage in numeric mode Tvrtko Ursulin
2023-03-28 12:54   ` [igt-dev] " Tvrtko Ursulin
2023-04-14 15:28   ` [Intel-gfx] " Kamil Konieczny
2023-04-14 15:28     ` [igt-dev] " Kamil Konieczny
2023-04-17 10:58     ` Tvrtko Ursulin
2023-04-17 10:58       ` [igt-dev] " Tvrtko Ursulin
2023-03-28 14:45 ` [igt-dev] ✗ Fi.CI.BAT: failure for Assorted intel_gpu_top tweaks Patchwork
2023-04-06 16:22 ` [igt-dev] ✓ Fi.CI.BAT: success for Assorted intel_gpu_top tweaks (rev2) Patchwork
2023-04-07  6:30 ` [igt-dev] ✓ Fi.CI.IGT: " Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230328125429.1970202-2-tvrtko.ursulin@linux.intel.com \
    --to=tvrtko.ursulin@linux.intel.com \
    --cc=Intel-gfx@lists.freedesktop.org \
    --cc=igt-dev@lists.freedesktop.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.