From 6c4af25e715a352ecf07a6127f35b0718712b7ef Mon Sep 17 00:00:00 2001
From: Moe Jette <jette1@llnl.gov>
Date: Tue, 20 Apr 2010 15:46:05 +0000
Subject: [PATCH] add ability for squeue to view job's gres value

---
 doc/man/man1/squeue.1 |  4 ++++
 src/squeue/opts.c     | 15 +++++++++------
 src/squeue/print.c    | 16 +++++++++++++---
 src/squeue/print.h    |  6 +++++-
 src/squeue/sort.c     | 25 +++++++++++++++++++++++--
 5 files changed, 54 insertions(+), 12 deletions(-)

diff --git a/doc/man/man1/squeue.1 b/doc/man/man1/squeue.1
index d528284d20a..6c7e9848ef4 100644
--- a/doc/man/man1/squeue.1
+++ b/doc/man/man1/squeue.1
@@ -108,6 +108,10 @@ Number of tasks created by a job step.
 This reports the value of the \fBsrun \-\-ntasks\fR option.
 (Valid for job steps only)
 .TP
+\fB%b\fR
+Generic resources (gres) required by the job or step.
+(Valid for jobs and job steps)
+.TP
 \fB%c\fR
 Minimum number of CPUs (processors) per node requested by the job.
 This reports the value of the \fBsrun \-\-mincpus\fR option with a
diff --git a/src/squeue/opts.c b/src/squeue/opts.c
index 660be3012b3..dfbb6e69754 100644
--- a/src/squeue/opts.c
+++ b/src/squeue/opts.c
@@ -4,7 +4,7 @@
  *  $Id$
  *****************************************************************************
  *  Copyright (C) 2002-2007 The Regents of the University of California.
- *  Copyright (C) 2008-2009 Lawrence Livermore National Security.
+ *  Copyright (C) 2008-2010 Lawrence Livermore National Security.
  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
  *  Written by Joey Ekstrom <ekstrom1@llnl.gov>, Morris Jette <jette1@llnl.gov>
  *  CODE-OCEC-09-009. All rights reserved.
@@ -500,6 +500,10 @@ extern int parse_format( char* format )
 							field_size,
 							right_justify,
 							suffix  );
+			else if (field[0] == 'b')
+				job_format_add_gres( params.format_list,
+						     field_size, right_justify,
+						     suffix );
 			else if (field[0] == 'c')
 				job_format_add_min_cpus( params.format_list,
 							 field_size,
@@ -536,16 +540,15 @@ extern int parse_format( char* format )
 							 field_size,
 							 right_justify,
 							 suffix );
-			else if (field[0] == 'G')
-				job_format_add_group_id( params.format_list,
-							 field_size,
-							 right_justify,
-							 suffix );
 			else if (field[0] == 'g')
 				job_format_add_group_name( params.format_list,
 							   field_size,
 							   right_justify,
 							   suffix );
+			else if (field[0] == 'G')
+				job_format_add_gres( params.format_list,
+						     field_size, right_justify,
+						     suffix );
 			else if (field[0] == 'h')
 				job_format_add_shared( params.format_list,
 						       field_size,
diff --git a/src/squeue/print.c b/src/squeue/print.c
index 54d4468dfd9..7e31854b14c 100644
--- a/src/squeue/print.c
+++ b/src/squeue/print.c
@@ -2,10 +2,9 @@
  *  print.c - squeue print job functions
  *****************************************************************************
  *  Copyright (C) 2002-2007 The Regents of the University of California.
- *  Copyright (C) 2008-2009 Lawrence Livermore National Security.
+ *  Copyright (C) 2008-2010 Lawrence Livermore National Security.
  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
- *  Written by Joey Ekstrom <ekstrom1@llnl.gov>,
- *             Morris Jette <jette1@llnl.gov>, et. al.
+ *  Written by Joey Ekstrom <ekstrom1@llnl.gov>, et. al.
  *  CODE-OCEC-09-009. All rights reserved.
  *
  *  This file is part of SLURM, a resource management program.
@@ -394,6 +393,17 @@ int _print_job_user_name(job_info_t * job, int width, bool right, char* suffix)
 	return SLURM_SUCCESS;
 }
 
+int _print_job_gres(job_info_t * job, int width, bool right, char* suffix)
+{
+	if (job == NULL)	/* Print the Header instead */
+		_print_str("GRES", width, right, true);
+	else
+		_print_str(job->gres, width, right, true);
+	if (suffix)
+		printf("%s", suffix);
+	return SLURM_SUCCESS;
+}
+
 int _print_job_group_id(job_info_t * job, int width, bool right, char* suffix)
 {
 	if (job == NULL)	/* Print the Header instead */
diff --git a/src/squeue/print.h b/src/squeue/print.h
index 5a06e2b72b3..c7abfbf370c 100644
--- a/src/squeue/print.h
+++ b/src/squeue/print.h
@@ -2,7 +2,7 @@
  *  print.h - squeue print job definitions
  *****************************************************************************
  *  Copyright (C) 2002-2007 The Regents of the University of California.
- *  Copyright (C) 2008-2009 Lawrence Livermore National Security
+ *  Copyright (C) 2008-2010 Lawrence Livermore National Security
  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
  *  Written by Joey Ekstrom <ekstrom1@llnl.gov>
  *  CODE-OCEC-09-009. All rights reserved.
@@ -99,6 +99,8 @@ int job_format_add_function(List list, int width, bool right_justify,
 	job_format_add_function(list,wid,right,suffix,_print_job_user_name)
 #define job_format_add_user_id(list,wid,right,suffix) \
 	job_format_add_function(list,wid,right,suffix,_print_job_user_id)
+#define job_format_add_gres(list,wid,right,suffix) \
+        job_format_add_function(list,wid,right,suffix,_print_job_gres)
 #define job_format_add_group_name(list,wid,right,suffix) \
         job_format_add_function(list,wid,right,suffix,_print_job_group_name)
 #define job_format_add_group_id(list,wid,right,suffix) \
@@ -194,6 +196,8 @@ int _print_job_user_id(job_info_t * job, int width, bool right_justify,
 			char* suffix);
 int _print_job_user_name(job_info_t * job, int width, bool right_justify,
 			char* suffix);
+int _print_job_gres(job_info_t * job, int width, bool right_justify,
+			char* suffix);
 int _print_job_group_id(job_info_t * job, int width, bool right_justify,
 			char* suffix);
 int _print_job_group_name(job_info_t * job, int width, bool right_justify,
diff --git a/src/squeue/sort.c b/src/squeue/sort.c
index cd16b053a1f..117a3f05f89 100644
--- a/src/squeue/sort.c
+++ b/src/squeue/sort.c
@@ -2,7 +2,7 @@
  *  sort.c - squeue sorting functions
  *****************************************************************************
  *  Copyright (C) 2002-2007 The Regents of the University of California.
- *  Copyright (C) 2008-2009 Lawrence Livermore National Security.
+ *  Copyright (C) 2008-2010 Lawrence Livermore National Security.
  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
  *  Written by Morris Jette <jette1@llnl.gov>, et. al.
  *  CODE-OCEC-09-009. All rights reserved.
@@ -51,6 +51,7 @@
 
 static bool reverse_order;
 
+static int _sort_job_by_gres(void *void1, void *void2);
 static int _sort_job_by_group_id(void *void1, void *void2);
 static int _sort_job_by_group_name(void *void1, void *void2);
 static int _sort_job_by_id(void *void1, void *void2);
@@ -105,7 +106,9 @@ void sort_job_list(List job_list)
 		if ((i > 0) && (params.sort[i-1] == '-'))
 			reverse_order = true;
 
-		if      (params.sort[i] == 'c')
+		if      (params.sort[i] == 'b')
+			list_sort(job_list, _sort_job_by_gres);
+		else if (params.sort[i] == 'c')
 			;	/* sort_job_by_min_cpus_per_node */
 		else if (params.sort[i] == 'C')
 			list_sort(job_list, _sort_job_by_num_cpus);
@@ -216,6 +219,24 @@ void sort_step_list(List step_list)
 /*****************************************************************************
  * Local Job Sort Functions
  *****************************************************************************/
+static int _sort_job_by_gres(void *void1, void *void2)
+{
+	int diff;
+	job_info_t *job1 = (job_info_t *) void1;
+	job_info_t *job2 = (job_info_t *) void2;
+	char *val1 = "", *val2 = "";
+
+	if (job1->gres)
+		val1 = job1->gres;
+	if (job2->gres)
+		val2 = job2->gres;
+	diff = strcmp(val1, val2);
+
+	if (reverse_order)
+		diff = -diff;
+	return diff;
+}
+
 static int _sort_job_by_group_id(void *void1, void *void2)
 {
 	int diff;
-- 
GitLab