From 512de3d0a9cb178cbcc1545aac077132db88e736 Mon Sep 17 00:00:00 2001
From: Moe Jette <jette1@llnl.gov>
Date: Tue, 16 Dec 2008 16:17:05 +0000
Subject: [PATCH] Eliminate alloc_lps_cnt and alloc_lps[] from the job
 credential.   generate alloc_lps value from the core_bitmap.

---
 src/api/step_ctx.c            |  7 +--
 src/common/slurm_cred.c       | 72 +--------------------------
 src/common/slurm_cred.h       |  8 ---
 src/sattach/sattach.c         |  2 -
 src/slurmctld/job_scheduler.c |  3 --
 src/slurmctld/proc_req.c      | 33 -------------
 src/slurmd/slurmd/req.c       | 92 ++++++++++++++++++++++-------------
 7 files changed, 60 insertions(+), 157 deletions(-)

diff --git a/src/api/step_ctx.c b/src/api/step_ctx.c
index 2f0cb1877f0..27b78dfd19b 100644
--- a/src/api/step_ctx.c
+++ b/src/api/step_ctx.c
@@ -1,9 +1,8 @@
 /*****************************************************************************\
  *  step_ctx.c - step_ctx task functions for use by AIX/POE
- *
- *  $Id$
  *****************************************************************************
- *  Copyright (C) 2004 The Regents of the University of California.
+ *  Copyright (C) 2004-2007 The Regents of the University of California.
+ *  Copyright (C) 2008 Lawrence Livermore National Security.
  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
  *  Written by Morris Jette <jette1@llnl.gov>.
  *  LLNL-CODE-402394.
@@ -61,8 +60,6 @@ _job_fake_cred(struct slurm_step_ctx_struct *ctx)
 	slurm_cred_arg_t arg;
 	uint32_t node_cnt = ctx->step_resp->step_layout->node_cnt;
 
-	arg.alloc_lps_cnt = 0;
-	arg.alloc_lps     = NULL;
 	arg.hostlist      = ctx->step_req->node_list;
 	arg.job_mem       = 0;
 	arg.jobid         = ctx->job_id;
diff --git a/src/common/slurm_cred.c b/src/common/slurm_cred.c
index d90eab24536..6ca6a5b2d47 100644
--- a/src/common/slurm_cred.c
+++ b/src/common/slurm_cred.c
@@ -149,8 +149,6 @@ struct slurm_job_credential {
 				 * default=0 (no limit) */
 	time_t    ctime;	/* time of credential creation		*/
 	char     *nodes;	/* hostnames for which the cred is ok	*/
-	uint32_t  alloc_lps_cnt;/* Number of hosts in the list above	*/
-	uint16_t *alloc_lps;	/* Number of tasks on each host		*/
 #ifndef HAVE_BG
 	bitstr_t *core_bitmap;
 	uint16_t  core_array_size;	/* core/socket array size */
@@ -614,14 +612,6 @@ slurm_cred_create(slurm_cred_ctx_t ctx, slurm_cred_arg_t *arg)
 	cred->uid    = arg->uid;
 	cred->job_mem = arg->job_mem;
 	cred->nodes  = xstrdup(arg->hostlist);
-        cred->alloc_lps_cnt = arg->alloc_lps_cnt;
-        cred->alloc_lps  = NULL;
-        if (cred->alloc_lps_cnt > 0) {
-                cred->alloc_lps =  xmalloc(cred->alloc_lps_cnt * 
-					   sizeof(uint16_t));
-                memcpy(cred->alloc_lps, arg->alloc_lps, 
-		       cred->alloc_lps_cnt * sizeof(uint16_t));
-        }
 #ifndef HAVE_BG
 {
 	int i, sock_recs = 0;
@@ -686,14 +676,6 @@ slurm_cred_copy(slurm_cred_t cred)
 	rcred->uid    = cred->uid;
 	rcred->job_mem = cred->job_mem;
 	rcred->nodes  = xstrdup(cred->nodes);
-	rcred->alloc_lps_cnt = cred->alloc_lps_cnt;
-	rcred->alloc_lps  = NULL;
-	if (rcred->alloc_lps_cnt > 0) {
-		rcred->alloc_lps =  xmalloc(rcred->alloc_lps_cnt * 
-					    sizeof(uint16_t));
-		memcpy(rcred->alloc_lps, cred->alloc_lps, 
-		       rcred->alloc_lps_cnt * sizeof(uint16_t));
-	}
 #ifndef HAVE_BG
 	rcred->core_bitmap = bit_copy(cred->core_bitmap);
 	rcred->core_array_size = cred->core_array_size;
@@ -741,14 +723,6 @@ slurm_cred_faker(slurm_cred_arg_t *arg)
 	cred->uid      = arg->uid;
 	cred->job_mem  = arg->job_mem;
 	cred->nodes    = xstrdup(arg->hostlist);
-	cred->alloc_lps_cnt = arg->alloc_lps_cnt;
-	cred->alloc_lps  = NULL;
-	if (cred->alloc_lps_cnt > 0) {
-		cred->alloc_lps =  xmalloc(cred->alloc_lps_cnt * 
-					   sizeof(uint16_t));
-		memcpy(cred->alloc_lps, arg->alloc_lps, 
-		       cred->alloc_lps_cnt * sizeof(uint16_t));
-	}
 #ifndef HAVE_BG
 {
 	int i, sock_recs = 0;
@@ -800,7 +774,6 @@ slurm_cred_faker(slurm_cred_arg_t *arg)
 
 void slurm_cred_free_args(slurm_cred_arg_t *arg)
 {
-	xfree(arg->alloc_lps);
 	if (arg->core_bitmap) {
 		bit_free(arg->core_bitmap);
 		arg->core_bitmap = NULL;
@@ -810,7 +783,6 @@ void slurm_cred_free_args(slurm_cred_arg_t *arg)
 	xfree(arg->job_hostlist);
 	xfree(arg->sock_core_rep_count);
 	xfree(arg->sockets_per_node);
-	arg->alloc_lps_cnt = 0;
 }
 
 int
@@ -828,13 +800,6 @@ slurm_cred_get_args(slurm_cred_t cred, slurm_cred_arg_t *arg)
 	arg->uid      = cred->uid;
 	arg->job_mem  = cred->job_mem;
 	arg->hostlist = xstrdup(cred->nodes);
-	arg->alloc_lps_cnt = cred->alloc_lps_cnt;
-	if (arg->alloc_lps_cnt > 0) {
-		arg->alloc_lps = xmalloc(arg->alloc_lps_cnt * sizeof(uint16_t));
-		memcpy(arg->alloc_lps, cred->alloc_lps, 
-		       arg->alloc_lps_cnt * sizeof(uint16_t));
-	} else
-		arg->alloc_lps = NULL;
 #ifdef HAVE_BG
 	arg->core_bitmap = NULL;
 	arg->cores_per_socket = NULL;
@@ -916,13 +881,6 @@ slurm_cred_verify(slurm_cred_ctx_t ctx, slurm_cred_t cred,
 	arg->uid      = cred->uid;
 	arg->job_mem  = cred->job_mem;
 	arg->hostlist = xstrdup(cred->nodes);
-	arg->alloc_lps_cnt = cred->alloc_lps_cnt;
-	if (arg->alloc_lps_cnt > 0) {
-		arg->alloc_lps = xmalloc(arg->alloc_lps_cnt * sizeof(uint16_t));
-		memcpy(arg->alloc_lps, cred->alloc_lps, 
-		       arg->alloc_lps_cnt * sizeof(uint16_t));
-	} else
-		arg->alloc_lps = NULL;
 
 #ifdef HAVE_BG
 	arg->core_bitmap = NULL;
@@ -970,7 +928,6 @@ slurm_cred_destroy(slurm_cred_t cred)
 	xassert(cred->magic == CRED_MAGIC);
 
 	slurm_mutex_lock(&cred->mutex);
-	xfree(cred->alloc_lps);
 #ifndef HAVE_BG
 	if (cred->core_bitmap) {
 		bit_free(cred->core_bitmap);
@@ -1181,12 +1138,6 @@ slurm_cred_unpack(Buf buffer)
 	cred->uid = cred_uid;
 	safe_unpack32(          &cred->job_mem,       buffer);
 	safe_unpackstr_xmalloc( &cred->nodes, &len,   buffer);
-	safe_unpack32(          &cred->alloc_lps_cnt, buffer);
-        if (cred->alloc_lps_cnt > 0) {
-		safe_unpack16_array(&cred->alloc_lps, &len,  buffer);
-		if (len != cred->alloc_lps_cnt)
-			goto unpack_error;
-	}
 	safe_unpack_time(       &cred->ctime,         buffer);
 #ifndef HAVE_BG
 {
@@ -1265,8 +1216,6 @@ slurm_cred_ctx_unpack(slurm_cred_ctx_t ctx, Buf buffer)
 void
 slurm_cred_print(slurm_cred_t cred)
 {
-        int i;
-
 	if (cred == NULL)
 		return;
 
@@ -1279,14 +1228,11 @@ slurm_cred_print(slurm_cred_t cred)
 	info("Cred: UID           %u",  (uint32_t) cred->uid);
 	info("Cred: job_mem       %u",  cred->job_mem       );
 	info("Cred: Nodes         %s",  cred->nodes         );
-	info("Cred: alloc_lps_cnt %u",  cred->alloc_lps_cnt );
-	info("Cred: alloc_lps: ");                            
-	for (i=0; i<cred->alloc_lps_cnt; i++)                 
-		info("      alloc_lps[%d] %u ", i, cred->alloc_lps[i]);
 	info("Cred: ctime         %s",  ctime(&cred->ctime) );
 	info("Cred: siglen        %u",  cred->siglen        );
 #ifndef HAVE_BG
 {
+	int i;
 	char str[128];
 	info("Cred: core_bitmap   %s", 
 	     bit_fmt(str, sizeof(str), cred->core_bitmap));
@@ -1305,19 +1251,6 @@ slurm_cred_print(slurm_cred_t cred)
 
 }
 
-int slurm_cred_get_alloc_lps(slurm_cred_t cred, char **nodes,
-			     uint32_t *alloc_lps_cnt, uint16_t **alloc_lps)
-{
-	if ((cred == NULL) || (nodes == NULL) ||
-	    (alloc_lps_cnt == NULL) || (alloc_lps == NULL))
-		return EINVAL;
-
-	*nodes         = cred->nodes;
-	*alloc_lps_cnt = cred->alloc_lps_cnt;
-	*alloc_lps     = cred->alloc_lps;
-	return SLURM_SUCCESS;
-}
-
 static void 
 _verifier_ctx_init(slurm_cred_ctx_t ctx)
 {
@@ -1514,9 +1447,6 @@ _pack_cred(slurm_cred_t cred, Buf buffer)
 	pack32(cred_uid,       buffer);
 	pack32(cred->job_mem,  buffer);
 	packstr(cred->nodes,   buffer);
-	pack32(cred->alloc_lps_cnt, buffer);
-	if (cred->alloc_lps_cnt > 0)
-		pack16_array(cred->alloc_lps, cred->alloc_lps_cnt, buffer);
 	pack_time(cred->ctime, buffer);
 #ifndef HAVE_BG
 {
diff --git a/src/common/slurm_cred.h b/src/common/slurm_cred.h
index 0341ffefc98..38518a6013a 100644
--- a/src/common/slurm_cred.h
+++ b/src/common/slurm_cred.h
@@ -139,8 +139,6 @@ typedef struct {
 				 * default=0 (no limit) */
 	uid_t    uid;
 	char    *hostlist;
-	uint32_t alloc_lps_cnt;
-	uint16_t *alloc_lps;
 
 	bitstr_t *core_bitmap;
 	uint16_t *cores_per_socket;
@@ -281,17 +279,11 @@ slurm_cred_t slurm_cred_unpack(Buf buffer);
  */
 int slurm_cred_get_signature(slurm_cred_t cred, char **datap, int *len);
 
-
 /*
  * Print a slurm job credential using the info() call
  */
 void slurm_cred_print(slurm_cred_t cred);
 
-/*
- * Get count of allocated LPS (processors) by node
- */
-int slurm_cred_get_alloc_lps(slurm_cred_t cred, char **nodes, 
-			     uint32_t *alloc_lps_cnt, uint16_t **alloc_lps);
 #ifdef DISABLE_LOCALTIME
 extern char * timestr (const time_t *tp, char *buf, size_t n);
 #endif
diff --git a/src/sattach/sattach.c b/src/sattach/sattach.c
index d761fdb9e83..36472b2b4e7 100644
--- a/src/sattach/sattach.c
+++ b/src/sattach/sattach.c
@@ -228,8 +228,6 @@ static slurm_cred_t _generate_fake_cred(uint32_t jobid, uint32_t stepid,
 	arg.stepid   = stepid;
 	arg.uid      = uid;
 	arg.hostlist = nodelist;
-	arg.alloc_lps_cnt = 0;    
-	arg.alloc_lps =  NULL;
 
 	arg.core_bitmap   = bit_alloc(node_cnt);
 	bit_nset(arg.core_bitmap, 0, node_cnt-1);
diff --git a/src/slurmctld/job_scheduler.c b/src/slurmctld/job_scheduler.c
index 94323548019..c55b70a5785 100644
--- a/src/slurmctld/job_scheduler.c
+++ b/src/slurmctld/job_scheduler.c
@@ -626,9 +626,6 @@ extern int make_batch_job_cred(batch_job_launch_msg_t *launch_msg_ptr,
 	} else
 		cred_arg.job_mem = job_ptr->details->job_min_memory;
 
-	cred_arg.alloc_lps_cnt = 0;
-	cred_arg.alloc_lps = NULL;
-
 	/* Identify the cores allocated to this job. */
 	xassert(job_ptr->select_job);
 	select_ptr = job_ptr->select_job;
diff --git a/src/slurmctld/proc_req.c b/src/slurmctld/proc_req.c
index 91958f0b1de..16e4a037c62 100644
--- a/src/slurmctld/proc_req.c
+++ b/src/slurmctld/proc_req.c
@@ -556,38 +556,6 @@ static int _make_step_cred(struct step_record *step_ptr,
 #else
 	cred_arg.hostlist = step_ptr->step_layout->node_list;
 #endif
-	cred_arg.alloc_lps_cnt = select_ptr->nhosts;
-	if ((cred_arg.alloc_lps_cnt > 0) &&
-	    bit_equal(job_ptr->node_bitmap, step_ptr->step_node_bitmap)) {
-		cred_arg.alloc_lps = xmalloc(cred_arg.alloc_lps_cnt *
-				sizeof(uint16_t));
-		memcpy(cred_arg.alloc_lps, select_ptr->cpus,
-		       cred_arg.alloc_lps_cnt*sizeof(uint16_t));
-        } else if (cred_arg.alloc_lps_cnt > 0) {
-		/* Construct an array of allocated CPUs per node.
-		 * Translate from array based upon job's allocation
-		 * to array based upon nodes allocated to the step. */
-		int i, job_inx = -1, step_inx = -1;
-		int job_inx_target = job_ptr->node_cnt;
-		cred_arg.alloc_lps = xmalloc(cred_arg.alloc_lps_cnt *
-					     sizeof(uint16_t));
-		for (i=0; i<node_record_count; i++) {
-			if (!bit_test(select_ptr->node_bitmap, i))
-				continue;
-			job_inx++;
-			if (!bit_test(step_ptr->step_node_bitmap, i))
-				continue;
-			step_inx++;
-			cred_arg.alloc_lps[step_inx] = select_ptr->
-						       cpus[job_inx];
-			if (job_inx == job_inx_target)
-				break;
-		}
-		cred_arg.alloc_lps_cnt = step_inx + 1;
-        } else {
-		error("No resources allocated to job %u", job_ptr->job_id);
-		cred_arg.alloc_lps = NULL;
-	}
 
 	/* Identify the cores allocated to this job step
 	 * The core_bitmap is based upon the nodes allocated to the _job_.
@@ -601,7 +569,6 @@ static int _make_step_cred(struct step_record *step_ptr,
 	cred_arg.job_hostlist        = job_ptr->nodes;
 
 	*slurm_cred = slurm_cred_create(slurmctld_config.cred_ctx, &cred_arg);
-	xfree(cred_arg.alloc_lps);
 	if (*slurm_cred == NULL) {
 		error("slurm_cred_create error");
 		return ESLURM_INVALID_JOB_CREDENTIAL;
diff --git a/src/slurmd/slurmd/req.c b/src/slurmd/slurmd/req.c
index a1c5b0afab8..bce6e31756f 100644
--- a/src/slurmd/slurmd/req.c
+++ b/src/slurmd/slurmd/req.c
@@ -630,6 +630,8 @@ _check_job_credential(launch_tasks_request_msg_t *req, uid_t uid,
 	uint32_t         jobid = req->job_id;
 	uint32_t         stepid = req->job_step_id;
 	int              tasks_to_launch = req->tasks_to_launch[node_id];
+	uint32_t         alloc_lps = 0;
+
 	/*
 	 * First call slurm_cred_verify() so that all valid
 	 * credentials are checked
@@ -683,55 +685,62 @@ _check_job_credential(launch_tasks_request_msg_t *req, uid_t uid,
 		goto fail;
 	}
 
-        if ((arg.alloc_lps_cnt > 0) && (tasks_to_launch > 0)) {
-                host_index = hostset_find(hset, conf->node_name);
-
-#if(0)
-		/* Left for debugging purposes */
-                if (host_index >= 0)
-                  info(" cons_res %u alloc_lps_cnt %u "
-			"task[%d] = %u = task_to_launch %d host %s ", 
-			arg.jobid, arg.alloc_lps_cnt, host_index, 
-			arg.alloc_lps[host_index], 
-			tasks_to_launch, conf->node_name);
-#endif
-
-                if (host_index < 0) { 
+        if ((arg.job_nhosts > 0) && (tasks_to_launch > 0)) {
+		uint32_t i, i_first_bit=0, i_last_bit=0;
+		host_index = hostset_find(hset, conf->node_name);
+		if ((host_index < 0) || (host_index >= arg.job_nhosts)) { 
                         error("job cr credential invalid host_index %d for "
 			      "job %u", host_index, arg.jobid);
                         goto fail; 
                 }
-		if (host_index > arg.alloc_lps_cnt)
-			error("host_index > alloc_lps_cnt in credential");
-                else if (arg.alloc_lps[host_index] == 0)
+		host_index++;	/* change from 0-origin to 1-origin */
+		for (i=0; host_index; i++) {
+			if (host_index > arg.sock_core_rep_count[i]) {
+				i_first_bit += arg.sockets_per_node[i] *
+					       arg.cores_per_socket[i] *
+					       arg.sock_core_rep_count[i];
+				host_index -= arg.sock_core_rep_count[i];
+			} else {
+				i_first_bit += arg.sockets_per_node[i] *
+					       arg.cores_per_socket[i] *
+					       (host_index - 1);
+				i_last_bit = i_first_bit +
+					     arg.sockets_per_node[i] *
+					     arg.cores_per_socket[i];
+				break;
+			}
+		}
+		/* Now count the allocated processors */
+		for (i = i_first_bit; i < i_last_bit; i++) {
+			if (bit_test(arg.core_bitmap, i))
+				alloc_lps++;
+		}
+                if (alloc_lps == 0) {
 			error("cons_res: zero processors allocated to step");
-                if (tasks_to_launch > arg.alloc_lps[host_index]) {
+			alloc_lps = 1;
+		}
+		if (tasks_to_launch > alloc_lps) {
 			/* This is expected with the --overcommit option */
-			verbose("cons_res: More than one tasks per logical "
-				"processor (%d > %u) on host [%u.%u %ld %s] ",
-				tasks_to_launch, arg.alloc_lps[host_index], 
-				arg.jobid, arg.stepid, (long) arg.uid, 
-				arg.hostlist);
-			verbose("cons_res: Use task/affinity plug-in to bind "
-				"the tasks to the allocated resources");
+			debug("cons_res: More than one tasks per logical "
+			      "processor (%d > %u) on host [%u.%u %ld %s] ",
+			      tasks_to_launch, alloc_lps, arg.jobid,
+			      arg.stepid, (long) arg.uid, arg.hostlist);
 		}
-        }
+        } else
+		alloc_lps = 1;
 
 	/* Overwrite any memory limits in the RPC with contents of the 
 	 * memory limit within the credential. 
 	 * Reset the CPU count on this node to correct value. */
 	if (arg.job_mem & MEM_PER_CPU) {
 		req->job_mem = arg.job_mem & (~MEM_PER_CPU);
-		if ((host_index >= 0) && (host_index < arg.alloc_lps_cnt) &&
-		    (arg.alloc_lps[host_index] > 0))
-			req->job_mem *= arg.alloc_lps[host_index];
+		req->job_mem *= alloc_lps;
 	} else
 		req->job_mem = arg.job_mem;
-	if ((host_index >= 0) && (host_index < arg.alloc_lps_cnt))
-		req->cpus_allocated[node_id] = arg.alloc_lps[host_index];
+	req->cpus_allocated[node_id] = alloc_lps;
 #if 0
 	info("mem orig:%u cpus:%u limit:%u", 
-	     arg.job_mem, arg.alloc_lps[host_index], req->job_mem);
+	     arg.job_mem, alloc_lps, req->job_mem);
 #endif
 
 	*step_hset = hset;
@@ -947,11 +956,24 @@ _set_batch_job_limits(slurm_msg_t *msg)
 
 	if (slurm_cred_get_args(req->cred, &arg) != SLURM_SUCCESS)
 		return;
-
+		
 	if (arg.job_mem & MEM_PER_CPU) {
+		int i;
+		uint32_t alloc_lps = 0, last_bit;
+		if (arg.job_nhosts > 0) {
+			last_bit = arg.sockets_per_node[0] * 
+				   arg.cores_per_socket[0];
+			for (i=0; i<last_bit; i++) {
+				if (bit_test(arg.core_bitmap, i))
+					alloc_lps++;
+			}
+		}
+		if (alloc_lps == 0) {
+			error("_set_batch_job_limit: alloc_lps is zero");
+			alloc_lps = 1;
+		}
 		req->job_mem = arg.job_mem & (~MEM_PER_CPU);
-		if (arg.alloc_lps_cnt > 1)
-			req->job_mem *= arg.alloc_lps_cnt;
+		req->job_mem *= alloc_lps;
 	} else
 		req->job_mem = arg.job_mem;
 
-- 
GitLab