[PATCH v3] tools/xentop: add option to display dom0 first

zithro / Cyril Rébert posted 1 patch 8 months ago
Patches applied successfully (tree, apply log)
git fetch https://gitlab.com/xen-project/patchew/xen tags/patchew/92ef4d230e05970e1d685b03125fce44adc55010.1707331801.git.slack@rabbit.lu
docs/man/xentop.1.pod |  6 +++++-
tools/xentop/xentop.c | 30 +++++++++++++++++++++++++++---
2 files changed, 32 insertions(+), 4 deletions(-)
[PATCH v3] tools/xentop: add option to display dom0 first
Posted by zithro / Cyril Rébert 8 months ago
From: Cyril Rébert <slack@rabbit.lu>

Add a command line option to xentop to be able to display dom0 first, on top of the list.
This is unconditional, so sorting domains with the S option will also ignore dom0.

Signed-off-by: Cyril Rébert (zithro) <slack@rabbit.lu>
---
Changes in v3:
(none, just reformatting patch correctly ... hopefully ?!)
---
Changes in v2:
- bug fix
- add documentation
---
 docs/man/xentop.1.pod |  6 +++++-
 tools/xentop/xentop.c | 30 +++++++++++++++++++++++++++---
 2 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/docs/man/xentop.1.pod b/docs/man/xentop.1.pod
index 126f43d2f3..593a484ce7 100644
--- a/docs/man/xentop.1.pod
+++ b/docs/man/xentop.1.pod
@@ -5,7 +5,7 @@ xentop - displays real-time information about a Xen system and domains
 =head1 SYNOPSIS
 
 B<xentop> [B<-h>] [B<-V>] [B<-d>SECONDS] [B<-n>] [B<-r>] [B<-v>] [B<-f>]
-[B<-b>] [B<-i>ITERATIONS]
+[B<-b>] [B<-i>ITERATIONS] [B<-z>]
 
 =head1 DESCRIPTION
 
@@ -57,6 +57,10 @@ output data in batch mode (to stdout)
 
 maximum number of iterations xentop should produce before ending
 
+=item B<-z>, B<--dom0-first>
+
+display dom0 first, ignoring interactive sorting
+
 =back
 
 =head1 INTERACTIVE COMMANDS
diff --git a/tools/xentop/xentop.c b/tools/xentop/xentop.c
index 545bd5e96d..0a2fab7f15 100644
--- a/tools/xentop/xentop.c
+++ b/tools/xentop/xentop.c
@@ -211,6 +211,7 @@ int show_networks = 0;
 int show_vbds = 0;
 int repeat_header = 0;
 int show_full_name = 0;
+int dom0_first = 0;
 #define PROMPT_VAL_LEN 80
 const char *prompt = NULL;
 char prompt_val[PROMPT_VAL_LEN];
@@ -240,6 +241,7 @@ static void usage(const char *program)
 	       "-b, --batch	     output in batch mode, no user input accepted\n"
 	       "-i, --iterations     number of iterations before exiting\n"
 	       "-f, --full-name      output the full domain name (not truncated)\n"
+	       "-z, --dom0-first     display dom0 first (ignore sorting)\n"
 	       "\n" XENTOP_BUGSTO,
 	       program);
 	return;
@@ -1163,6 +1165,8 @@ static void top(void)
 {
 	xenstat_domain **domains;
 	unsigned int i, num_domains = 0;
+	int dom0_index = -1;
+	int sort_start = 0, sort_count = 0;
 
 	/* Now get the node information */
 	if (prev_node != NULL)
@@ -1183,11 +1187,27 @@ static void top(void)
 	if(domains == NULL)
 		fail("Failed to allocate memory\n");
 
-	for (i=0; i < num_domains; i++)
+	for (i=0; i < num_domains; i++) {
 		domains[i] = xenstat_node_domain_by_index(cur_node, i);
+		if ( strcmp(xenstat_domain_name(domains[i]), "Domain-0") == 0 )
+			dom0_index = i;
+	}
+
+	/* Handle dom0 position, not for dom0-less */
+	if ( dom0_first == 1 && dom0_index != -1 ){
+		/* if dom0 is not first in domains, swap it there */
+		if ( dom0_index != 0 ){
+			xenstat_domain *tmp;
+			tmp = domains[0];
+			domains[0] = domains[dom0_index];
+			domains[dom0_index] = tmp;
+		}
+		sort_start = 1;
+		sort_count = 1;
+	}
 
 	/* Sort */
-	qsort(domains, num_domains, sizeof(xenstat_domain *),
+	qsort((domains+sort_start), (num_domains-sort_count), sizeof(xenstat_domain *),
 	      (int(*)(const void *, const void *))compare_domains);
 
 	if(first_domain_index >= num_domains)
@@ -1242,9 +1262,10 @@ int main(int argc, char **argv)
 		{ "batch",	   no_argument,	      NULL, 'b' },
 		{ "iterations",	   required_argument, NULL, 'i' },
 		{ "full-name",     no_argument,       NULL, 'f' },
+		{ "dom0-first",    no_argument,       NULL, 'z' },
 		{ 0, 0, 0, 0 },
 	};
-	const char *sopts = "hVnxrvd:bi:f";
+	const char *sopts = "hVnxrvd:bi:fz";
 
 	if (atexit(cleanup) != 0)
 		fail("Failed to install cleanup handler.\n");
@@ -1286,6 +1307,9 @@ int main(int argc, char **argv)
 		case 'f':
 			show_full_name = 1;
 			break;
+		case 'z':
+			dom0_first = 1;
+			break;
 		}
 	}
 
-- 
2.39.2


Re: [PATCH v3] tools/xentop: add option to display dom0 first
Posted by Anthony PERARD 8 months ago
On Wed, Feb 07, 2024 at 08:02:00PM +0100, zithro / Cyril Rébert wrote:
> From: Cyril Rébert <slack@rabbit.lu>
> 
> Add a command line option to xentop to be able to display dom0 first, on top of the list.
> This is unconditional, so sorting domains with the S option will also ignore dom0.
> 
> Signed-off-by: Cyril Rébert (zithro) <slack@rabbit.lu>

Reviewed-by: Anthony PERARD <anthony.perard@citrix.com>

Thanks,

-- 
Anthony PERARD