From 5c77bbfe330b1420791b12bba6ef78bea5108b13 Mon Sep 17 00:00:00 2001
From: Moe Jette <jette1@llnl.gov>
Date: Wed, 21 Mar 2007 23:46:00 +0000
Subject: [PATCH] More work toward cpuset support.

---
 src/plugins/task/affinity/affinity.c      | 267 +---------------------
 src/plugins/task/affinity/affinity.h      |  16 +-
 src/plugins/task/affinity/cpuset.c        | 267 +++++++++++++++++++++-
 src/plugins/task/affinity/cpuset.h        |  41 ----
 src/plugins/task/affinity/task_affinity.c |  64 +++---
 5 files changed, 304 insertions(+), 351 deletions(-)
 delete mode 100644 src/plugins/task/affinity/cpuset.h

diff --git a/src/plugins/task/affinity/affinity.c b/src/plugins/task/affinity/affinity.c
index 03103649da3..ef6593c1fdb 100644
--- a/src/plugins/task/affinity/affinity.c
+++ b/src/plugins/task/affinity/affinity.c
@@ -32,6 +32,7 @@
  *  with SLURM; if not, write to the Free Software Foundation, Inc.,
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
 \*****************************************************************************/
+
 #include "affinity.h"
 
 #ifdef HAVE_PLPA
@@ -48,11 +49,13 @@ void slurm_chkaffinity(cpu_set_t *mask, slurmd_job_t *job, int statval)
 	int task_lid = job->envtp->localid;
 	pid_t mypid = job->envtp->task_pid;
 
-	if (!(job->cpu_bind_type & CPU_BIND_VERBOSE)) return;
+	if (!(job->cpu_bind_type & CPU_BIND_VERBOSE))
+		return;
 
 	action[0] = '\0';
 	status[0] = '\0';
-	if (statval) { strcpy(status, " FAILED"); }
+	if (statval)
+		strcpy(status, " FAILED");
 
 	if (job->cpu_bind_type & CPU_BIND_NONE) {
 		strcpy(action, "");
@@ -216,263 +219,3 @@ int slurm_getaffinity(pid_t pid, size_t size, cpu_set_t *mask)
 	debug3("sched_getaffinity(%d) = 0x%s", pid, cpuset_to_str(mask, mstr));
 	return (rval);
 }
-
-#ifdef HAVE_CPUSETS_EXP
-int get_cpuset_mask(cs_cpumask_t *mask, slurmd_job_t *job)
-{
-	int nbr_task_cpus;
-	int nummasks, i, j;
-	char *curstr, *selstr;
-	char mstr[1 + CPU_SETSIZE / 4];
-	int local_id = job->envtp->localid;
-
-	debug2("get_cpuset_mask bind_type = %d, bind_list = %s\n", 
-		job->cpu_bind_type, job->cpu_bind);
-
-	if ((!job->cpu_bind_type) || (job->cpu_bind_type & CPU_BIND_NONE)) {
-		return false;
-	}
-
-	nbr_task_cpus = job->cpus / job->ntasks;
-
-	if (job->cpu_bind_type & CPU_BIND_RANK) {
-		for (i=0;i<nbr_task_cpus;i++){
-			cs_cpumask_add(mask, local_id * nbr_task_cpus + i);
-		}
-		return true;
-	}
-
-	nummasks = 0;
-	selstr = NULL;
-
-	if (job->cpu_bind_type & CPU_BIND_MAPCPU) {
-		unsigned int mycpu = 0;
-		/* find first entry for this task */
-		curstr = job->cpu_bind;
-		while (*curstr) {
-			if (nummasks == local_id*nbr_task_cpus) {
-				selstr = curstr;
-				break;
-			}
-			if (*curstr == ',')
-				nummasks++;
-			curstr++;
-		}
-		/* check if we found a cpu entry */
-		if (!selstr){
-			error("not enough entries in map_cpu:<list>");
-			return false;
-		}
-		/* add cpus to mask from list for "nbr_task_cpus" */
-		for (i=0;i<nbr_task_cpus;i++){
-			if (!*selstr){
-				error("not enough entries in map_cpu:<list>");
-				return false;
-			}
-			/* extract the selected mask from the list */
-			j = 0;
-			curstr = mstr;
-			while ((*selstr && *selstr != ',') && (j++ < (CPU_SETSIZE/4)))
-				*curstr++ = *selstr++;
-			*curstr = '\0';
-			if (*selstr == ',')
-				selstr++;
-			if (strncmp(mstr, "0x", 2) == 0) {
-				mycpu = strtoul (&(mstr[2]), NULL, 16);
-			} else {
-				mycpu = strtoul (mstr, NULL, 10);
-			}
-			cs_cpumask_add(mask, mycpu);
-		}
-		return true;
-	}
-
-	if (job->cpu_bind_type & CPU_BIND_MASKCPU) {
-		/* find mask entry for this task */
-		curstr = job->cpu_bind;
-		while (*curstr) {
-			if (nummasks == local_id) {
-				selstr = curstr;
-				break;
-			}
-			if (*curstr == ',')
-			        nummasks++;
-			curstr++;
-		}
-		/* check if we found a cpu entry */
-		if (!selstr){
-			error("not enough entries in mask_cpu:<list>");
-			return false;
-		}
-		/* extract the selected mask from the list */
-		j = 0;
-		curstr = mstr;
-		while ((*selstr && *selstr != ',') && (j++ < (CPU_SETSIZE/4)))
-			*curstr++ = *selstr++;
-		*curstr = '\0';
-		/* convert mask string into cpu_set_t mask */
-		if (str_to_cpuset( (cpu_set_t *) mask, mstr) < 0) {
-			error("str_to_cpuset %s", mstr);
-			return false;
-		}
-		return true;
-	}
-
-	return false;
-}
-
-int get_memset_mask(cs_memmask_t *mem_mask, cs_cpumask_t *cpu_mask, slurmd_job_t *job)
-{
-	cs_cpumask_t cur_mask, tst_mask;
-	int nbr_nodes, nummasks, i, j;
-	char *curstr, *selstr;
-	char mstr[1 + CPU_SETSIZE / 4];
-	int local_id = job->envtp->localid;
-
-	debug2("get_memset_mask bind_type = %d, bind_list = %s\n", 
-		job->mem_bind_type, job->mem_bind);
-
-	/* If "not specified" or "None" or "Rank", do not set a new memory mask in the CPUset  */
-	if ((!job->mem_bind_type) || (job->mem_bind_type & (MEM_BIND_NONE | MEM_BIND_RANK))) 
-		return false;
-
-	/* For now, make LOCAL and MAP_CPU the same */
-	if (job->mem_bind_type & (MEM_BIND_LOCAL | MEM_BIND_MAPCPU)) {
-		nbr_nodes = cs_nr_nodes();
-		for (i=0; i<nbr_nodes; i++) {
-			cs_get_node_cpus(i, &cur_mask);
-			cs_cpumask_and(&tst_mask, &cur_mask, cpu_mask);
-			if (!cs_cpumask_empty(&tst_mask)) {
-				cs_memmask_add(mem_mask, i);
-				debug2("added node = %d to mem mask %08x \n",i,*mem_mask);
-			}
-		}
-		
-		return true;
-	}
-	    
-	/* allow user to set specific memory masks */
-	if (job->mem_bind_type & MEM_BIND_MASKCPU) {
-		/* find mask entry for this task */
-		nummasks = 0;
-		selstr = NULL;
-
-		curstr = job->mem_bind;
-		while (*curstr) {
-			if (nummasks == local_id) {
-				selstr = curstr;
-				break;
-			}
-			if (*curstr == ',')
-			        nummasks++;
-			curstr++;
-		}
-		/* check if we found a mem entry */
-		if (!selstr){
-			error("not enough entries in mask_mem:<list>");
-			return false;
-		}
-		/* extract the selected mask from the list */
-		j = 0;
-		curstr = mstr;
-		while ((*selstr && *selstr != ',') && (j++ < (CPU_SETSIZE/4)))
-			*curstr++ = *selstr++;
-		*curstr = '\0';
-		/* convert mask string into cpu_set_t mask */
-		if (str_to_cpuset( (cpu_set_t *) mem_mask, mstr) < 0) {
-			error("str_to_cpuset %s", mstr);
-			return false;
-		}
-		return true;
-	}
-	return false;
-}
-
-int make_task_cpuset(slurmd_job_t *job, cs_cpumask_t *cpu_mask, cs_memmask_t *mem_mask){
-
-	char path[PATH_MAX];
-	char *current_cs = NULL;
-	int retval = 0;
-	int success = 0;
-
-	info("cpuset - cs_init called");
-	cs_init();
-
-	current_cs = cs_get_current();
-	if (!current_cs)
-		return -ENOMEM;
-
-	int l = snprintf(path, PATH_MAX, "%sslurm%u_%d", current_cs, job->jobid, 
-			 job->envtp->localid);
-	if (l > PATH_MAX) {
-		retval = -ENAMETOOLONG;
-		goto out;
-	}
-
-	debug("cpuset path = %s",path);
-
-	retval = cs_create(path);
-	if (retval < 0)
-		goto out;
-	retval = cs_set_autoclean(path, CS_AUTOCLEAN);
-	if (retval < 0)
-		goto out;
-
-	cs_lock_libcpuset();
-	retval = cs_set_cpus(path, *cpu_mask);
-	if (retval < 0)
-		debug3("cpuset - error on cs_set_cpus = %d %s",retval,cs_strerror(retval));
-	success = 1;
-
-	if (cs_supports_mem()) {
-
-		/* Check for mem_bind options */
-		if (get_memset_mask(mem_mask, cpu_mask, job)) {
-			debug("cpuset - mem_mask = %d (decimal) and %08x (hex)",
-				*mem_mask, *mem_mask);
-			retval = cs_set_mems(path, *mem_mask);
-			if (retval < 0) {
-				debug3("cpuset - error on cs_set_mems = %d %s",
-					retval,cs_strerror(retval));
-				success = 0;
-				goto out_created;
-			}
-		} else {
-			/* Copy parent of new cpuset (i.e current) mems mask */
-			retval = cs_get_mems(current_cs, mem_mask);
-			if (retval < 0) {
-				debug3("cpuset - error on cs_get_mems = %d %s",
-					retval,cs_strerror(retval));
-				success = 0;
-				goto out_created;
-			}
-			retval = cs_set_mems(path, *mem_mask);
-			if (retval < 0) {
-				debug3("cpuset - error on cs_set_mems = %d %s",
-					retval,cs_strerror(retval));
-				success = 0;
-				goto out_created;
-			}
-		}
-	}
-
-	retval = cs_add_task(path, job->envtp->task_pid);
-	if (retval < 0) {
-		debug3("cpuset - error on cs_add_task = %d %s",retval,cs_strerror(retval));
-	}
-
- out_created:
-	if (!success)
-		cs_destroy(path);
-	cs_unlock_libcpuset();
-
- out:
-	free(current_cs);
-
-	current_cs = cs_get_current();
-	debug("cpuset - exit make_task_cpuset retval = %d cpuset = %s",retval,current_cs);
-	free(current_cs);
-
-	return retval;
-}
-#endif
diff --git a/src/plugins/task/affinity/affinity.h b/src/plugins/task/affinity/affinity.h
index cad2f1cca5c..e46fc8fb27f 100644
--- a/src/plugins/task/affinity/affinity.h
+++ b/src/plugins/task/affinity/affinity.h
@@ -52,12 +52,14 @@
 #include <sys/stat.h>
 #include <sys/param.h>
 #include <sys/poll.h>
-#include <unistd.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #include <pwd.h>
 #include <grp.h>
 #include <stdio.h>
 #include <string.h>
 #include <sys/utsname.h>
+#include <unistd.h>
 
 #define _GNU_SOURCE
 #define __USE_GNU
@@ -67,10 +69,6 @@
 #  include <stdlib.h>
 #endif
 
-#ifdef HAVE_CPUSETS_EXP
-#  include <cpuset.h>
-#endif
-
 #include <slurm/slurm_errno.h>
 #include "src/common/slurm_xlator.h"
 #include "src/slurmd/slurmd/slurmd.h"
@@ -95,11 +93,9 @@ void	slurm_chkaffinity(cpu_set_t *mask, slurmd_job_t *job, int statval);
 int	get_cpuset(cpu_set_t *mask, slurmd_job_t *job);
 int	slurm_setaffinity(pid_t pid, size_t size, const cpu_set_t *mask);
 int	slurm_getaffinity(pid_t pid, size_t size, cpu_set_t *mask);
-#ifdef HAVE_CPUSETS_EXP
-int	get_cpuset_mask(cs_cpumask_t *mask, slurmd_job_t *job);
-int	get_memset_mask(cs_memmask_t *mem_mask, cs_cpumask_t *cpu_mask, slurmd_job_t *job);
-int	make_task_cpuset(slurmd_job_t *job, cs_cpumask_t *cpu_mask, cs_memmask_t *mem_mask);
-#endif
+int	slurm_set_cpuset(char *path, pid_t pid, size_t size, 
+		const cpu_set_t *mask);
+int	slurm_get_cpuset(char *path, pid_t pid, size_t size, cpu_set_t *mask);
 
 /*** from numa.c ***/
 #ifdef HAVE_NUMA
diff --git a/src/plugins/task/affinity/cpuset.c b/src/plugins/task/affinity/cpuset.c
index f98ebe77f89..97decac63fd 100644
--- a/src/plugins/task/affinity/cpuset.c
+++ b/src/plugins/task/affinity/cpuset.c
@@ -1,9 +1,10 @@
 /*****************************************************************************\
  *  cpuset.c - Library for interacting with /dev/cpuset file system
  *****************************************************************************
+ *  Copyright (C) 2007 Bull
  *  Copyright (C) 2007 The Regents of the University of California.
- *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
- *  Written by Morris Jette <jette1@llnl.gov>
+ *  Written by Don Albert <Don.Albert@Bull.com> and 
+ *             Morris Jette <jette1@llnl.gov>
  *  UCRL-CODE-226842.
  *  
  *  This file is part of SLURM, a resource management program.
@@ -35,7 +36,263 @@
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
 \*****************************************************************************/
 
-#if     HAVE_CONFIG_H
-#  include "config.h"
-#endif
+#include "affinity.h"
+
+int	slurm_set_cpuset(char *path, pid_t pid, size_t size, 
+		const cpu_set_t *mask)
+{
+	int fd, rc;
+	char file_path[PATH_MAX];
+	char mstr[1 + CPU_SETSIZE / 4];
+
+	if (mkdir(path, 0700) && (errno != EEXIST)) {
+		error("mkdir(%s): %m", path);
+		return -1;
+	}
+
+	snprintf(file_path, sizeof(file_path), "%s/notify_on_release", path);
+	fd = open(file_path, O_CREAT | O_WRONLY);
+	if (fd < 0) {
+		error("open(%s): %m", file_path);
+		return -1;
+	}
+	rc = write(fd, "1", 2);		/* not sure if this is right */
+	close(fd);
+
+	snprintf(file_path, sizeof(file_path), "%s/cpus", path);
+	cpuset_to_str(mask, mstr);
+	fd = open(file_path, O_CREAT | O_WRONLY);
+	if (fd < 0) {
+		error("open(%s): %m", file_path);
+		return -1;
+	}
+	rc = write(fd, mstr, strlen(mstr));
+	close(fd);
+	if (rc < 1) {
+		error("write(%s): %m", file_path);
+		return -1;
+	}
+
+	snprintf(file_path, sizeof(file_path), "%s/tasks", path);
+	snprintf(mstr, sizeof(mstr), "%d", pid);
+	fd = open(file_path, O_CREAT);
+	if (fd < 0) {
+		error("open(%s): %m", file_path);
+		return -1;
+	}
+	rc = write(fd, mstr, strlen(mstr));
+	close(fd);
+	if (rc < 1) {
+		error("write(%s): %m", file_path);
+		return -1;
+	}
+
+	return 0;
+}
+
+int	slurm_get_cpuset(char *path, pid_t pid, size_t size, cpu_set_t *mask)
+{
+	int fd, rc;
+	char file_path[PATH_MAX];
+	char mstr[1 + CPU_SETSIZE / 4];
+
+	snprintf(file_path, sizeof(file_path), "%s/cpus", path);
+	fd = open(file_path, O_RDONLY);
+	if (fd < 0) {
+		error("open(%s): %m", file_path);
+		return -1;
+	}
+	rc = read(fd, mstr, sizeof(mstr));
+	close(fd);
+	if (rc < 1) {
+		error("read(%s): %m", file_path);
+		return -1;
+	}
+	str_to_cpuset(mask, mstr);
+
+	snprintf(file_path, sizeof(file_path), "%s/tasks", path);
+	fd = open(file_path, O_RDONLY);
+	if (fd < 0) {
+		error("open(%s): %m", file_path);
+		return -1;
+	}
+	rc = read(fd, mstr, sizeof(mstr));
+	close(fd);
+	if (rc < 1) {
+		error("read(%s): %m", file_path);
+		return -1;
+	}
+	/* verify that pid is in mstr */
+
+	return 0;
+}
+
+#if 0
+int get_memset_mask(slurm_memmask_t *mem_mask, slurm_cpumask_t *cpu_mask, 
+		slurmd_job_t *job)
+{
+	slurm_cpumask_t cur_mask, tst_mask;
+	int nbr_nodes, nummasks, i, j;
+	char *curstr, *selstr;
+	char mstr[1 + CPU_SETSIZE / 4];
+	int local_id = job->envtp->localid;
+
+	debug2("get_memset_mask bind_type = %d, bind_list = %s\n", 
+		job->mem_bind_type, job->mem_bind);
+
+	/* If "not specified" or "None" or "Rank", 
+	 * do not set a new memory mask in the CPUset  */
+	if ((!job->mem_bind_type)
+	|| (job->mem_bind_type & (MEM_BIND_NONE | MEM_BIND_RANK))) 
+		return false;
+
+	/* For now, make LOCAL and MAP_CPU the same */
+	if (job->mem_bind_type & (MEM_BIND_LOCAL | MEM_BIND_MAP)) {
+		nbr_nodes = cs_nr_nodes();
+		for (i=0; i<nbr_nodes; i++) {
+			cs_get_node_cpus(i, &cur_mask);
+			cs_cpumask_and(&tst_mask, &cur_mask, cpu_mask);
+			if (!cs_cpumask_empty(&tst_mask)) {
+				cs_memmask_add(mem_mask, i);
+				debug2("added node = %d to mem mask %08x \n",
+					i,*mem_mask);
+			}
+		}
+		
+		return true;
+	}
+	    
+	/* allow user to set specific memory masks */
+	if (job->mem_bind_type & MEM_BIND_MASK) {
+		/* find mask entry for this task */
+		nummasks = 0;
+		selstr = NULL;
+
+		curstr = job->mem_bind;
+		while (*curstr) {
+			if (nummasks == local_id) {
+				selstr = curstr;
+				break;
+			}
+			if (*curstr == ',')
+			        nummasks++;
+			curstr++;
+		}
+		/* check if we found a mem entry */
+		if (!selstr){
+			error("not enough entries in mask_mem:<list>");
+			return false;
+		}
+		/* extract the selected mask from the list */
+		j = 0;
+		curstr = mstr;
+		while ((*selstr && *selstr != ',') && 
+				(j++ < (CPU_SETSIZE/4))) {
+			*curstr++ = *selstr++;
+		}
+		*curstr = '\0';
+		/* convert mask string into cpu_set_t mask */
+		if (str_to_cpuset( (cpu_set_t *) mem_mask, mstr) < 0) {
+			error("str_to_cpuset %s", mstr);
+			return false;
+		}
+		return true;
+	}
+	return false;
+}
 
+int make_task_cpuset(slurmd_job_t *job, slurm_cpumask_t *cpu_mask, 
+		slurm_memmask_t *mem_mask)
+{
+
+	char path[PATH_MAX];
+	char *current_cs = NULL;
+	int retval = 0;
+	int success = 0;
+
+	info("cpuset - cs_init called");
+	cs_init();
+
+	current_cs = cs_get_current();
+	if (!current_cs)
+		return -ENOMEM;
+
+	int l = snprintf(path, PATH_MAX, "%sslurm%u_%d", current_cs, job->jobid, 
+			 job->envtp->localid);
+	if (l > PATH_MAX) {
+		retval = -ENAMETOOLONG;
+		goto out;
+	}
+
+	debug("cpuset path = %s",path);
+
+	retval = cs_create(path);
+	if (retval < 0)
+		goto out;
+	retval = cs_set_autoclean(path, CS_AUTOCLEAN);
+	if (retval < 0)
+		goto out;
+
+	cs_lock_libcpuset();
+	retval = cs_set_cpus(path, *cpu_mask);
+	if (retval < 0) {
+		debug3("cpuset - error on cs_set_cpus = %d %s",
+			retval, cs_strerror(retval));
+	}
+	success = 1;
+
+	if (cs_supports_mem()) {
+
+		/* Check for mem_bind options */
+		if (get_memset_mask(mem_mask, cpu_mask, job)) {
+			debug("cpuset - mem_mask = %d (decimal) and %08x (hex)",
+				*mem_mask, *mem_mask);
+			retval = cs_set_mems(path, *mem_mask);
+			if (retval < 0) {
+				debug3("cpuset - error on cs_set_mems = %d %s",
+					retval,cs_strerror(retval));
+				success = 0;
+				goto out_created;
+			}
+		} else {
+			/* Copy parent of new cpuset (i.e current) mems mask */
+			retval = cs_get_mems(current_cs, mem_mask);
+			if (retval < 0) {
+				debug3("cpuset - error on cs_get_mems = %d %s",
+					retval,cs_strerror(retval));
+				success = 0;
+				goto out_created;
+			}
+			retval = cs_set_mems(path, *mem_mask);
+			if (retval < 0) {
+				debug3("cpuset - error on cs_set_mems = %d %s",
+					retval, cs_strerror(retval));
+				success = 0;
+				goto out_created;
+			}
+		}
+	}
+
+	retval = cs_add_task(path, job->envtp->task_pid);
+	if (retval < 0) {
+		debug3("cpuset - error on cs_add_task = %d %s",
+			retval, cs_strerror(retval));
+	}
+
+ out_created:
+	if (!success)
+		cs_destroy(path);
+	cs_unlock_libcpuset();
+
+ out:
+	free(current_cs);
+
+	current_cs = cs_get_current();
+	debug("cpuset - exit make_task_cpuset retval = %d cpuset = %s",
+		retval, current_cs);
+	free(current_cs);
+
+	return retval;
+}
+
+#endif
diff --git a/src/plugins/task/affinity/cpuset.h b/src/plugins/task/affinity/cpuset.h
deleted file mode 100644
index 72826e50dfe..00000000000
--- a/src/plugins/task/affinity/cpuset.h
+++ /dev/null
@@ -1,41 +0,0 @@
-/*****************************************************************************\
- *  cpuset.h - Functions for interacting with /dev/cpuset file system
- *****************************************************************************
- *  Copyright (C) 2007 The Regents of the University of California.
- *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
- *  Written by Morris Jette <jette1@llnl.gov>
- *  UCRL-CODE-226842.
- *  
- *  This file is part of SLURM, a resource management program.
- *  For details, see <http://www.llnl.gov/linux/slurm/>.
- *  
- *  SLURM is free software; you can redistribute it and/or modify it under
- *  the terms of the GNU General Public License as published by the Free
- *  Software Foundation; either version 2 of the License, or (at your option)
- *  any later version.
- *
- *  In addition, as a special exception, the copyright holders give permission 
- *  to link the code of portions of this program with the OpenSSL library under 
- *  certain conditions as described in each individual source file, and 
- *  distribute linked combinations including the two. You must obey the GNU 
- *  General Public License in all respects for all of the code used other than 
- *  OpenSSL. If you modify file(s) with this exception, you may extend this 
- *  exception to your version of the file(s), but you are not obligated to do 
- *  so. If you do not wish to do so, delete this exception statement from your
- *  version.  If you delete this exception statement from all source files in 
- *  the program, then also delete it here.
- *  
- *  SLURM is distributed in the hope that it will be useful, but WITHOUT ANY
- *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- *  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
- *  details.
- *  
- *  You should have received a copy of the GNU General Public License along
- *  with SLURM; if not, write to the Free Software Foundation, Inc.,
- *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
-\*****************************************************************************/
-
-#ifndef _HAVE_CPUSET_H
-#define _HAVE_CPUSET_H
-
-#endif
diff --git a/src/plugins/task/affinity/task_affinity.c b/src/plugins/task/affinity/task_affinity.c
index f0bcfda663b..0a27e81ef33 100644
--- a/src/plugins/task/affinity/task_affinity.c
+++ b/src/plugins/task/affinity/task_affinity.c
@@ -48,6 +48,8 @@
 #include "affinity.h"
 #include "dist_tasks.h"
 
+#define CPUSET_DIR "/dev/cpuset"
+
 /*
  * These variables are required by the generic plugin interface.  If they
  * are not found in the plugin, the plugin loader will ignore it.
@@ -156,10 +158,11 @@ int task_pre_launch ( slurmd_job_t *job )
 	debug("affinity task_pre_launch: %u.%u, task %d", 
 		job->jobid, job->stepid, job->envtp->procid);
 
-#ifdef HAVE_CPUSETS_EXP
-	if (!conf->use_cpusets) {
-	  info("Using sched_affinity for tasks");
-#endif
+	if (conf->use_cpusets)
+		info("Using cpuset affinity for tasks");
+	else
+		info("Using sched_affinity for tasks");
+
 	/*** CPU binding support ***/
 	if (job->cpu_bind_type) {	
 		cpu_set_t new_mask, cur_mask;
@@ -168,15 +171,34 @@ int task_pre_launch ( slurmd_job_t *job )
 		int setval = 0;
 		slurm_getaffinity(mypid, sizeof(cur_mask), &cur_mask);
 
-		if (get_cpuset(&new_mask, job)) {
-			if (!(job->cpu_bind_type & CPU_BIND_NONE)) {
+		if (get_cpuset(&new_mask, job)
+		&&  (!(job->cpu_bind_type & CPU_BIND_NONE))) {
+			if (conf->use_cpusets) {
+				char path[PATH_MAX];
+				if (snprintf(path, PATH_MAX, "%sslurm%u_%d",
+						CPUSET_DIR, job->jobid,
+						job->envtp->localid) > 
+						PATH_MAX) {
+					error("cpuset path too long");
+					return SLURM_ERROR;
+				}
+				setval = slurm_set_cpuset(path, mypid,
+						sizeof(new_mask), 
+						&new_mask);
+				slurm_get_cpuset(path, mypid,
+						sizeof(cur_mask), 
+						&cur_mask);
+			} else {
 				setval = slurm_setaffinity(mypid,
-						sizeof(new_mask), &new_mask);
+						sizeof(new_mask), 
+						&new_mask);
 				slurm_getaffinity(mypid,
-						sizeof(cur_mask), &cur_mask);
+						sizeof(cur_mask), 
+						&cur_mask);
 			}
 		}
-		slurm_chkaffinity(setval ? &new_mask : &cur_mask, job, setval);
+		slurm_chkaffinity(setval ? &new_mask : &cur_mask, 
+					job, setval);
 	}
 
 #ifdef HAVE_NUMA
@@ -192,30 +214,6 @@ int task_pre_launch ( slurmd_job_t *job )
 		}
 		slurm_chk_memset(&cur_mask, job);
 	}
-#endif
-#ifdef HAVE_CPUSETS_EXP
-	}
-	else {
-		cs_cpumask_t cpu_mask;
-		cs_memmask_t mem_mask;
-
-		info("Using cpuset affinity for tasks");
-		debug("from job structure - nprocs=%u, ntasks=%u, cpus=%u",
-			job->nprocs, job->ntasks, job->cpus);
-		debug("from job->envtp - procid=%d, localid=%d, pid=%d",
-			job->envtp->procid, job->envtp->localid, job->envtp->task_pid);
-
-		cs_cpumask_clear(&cpu_mask);
-		cs_memmask_clear(&mem_mask);
-		if (get_cpuset_mask(&cpu_mask, job)) {
-			debug2("cpu_mask = %d (decimal) and %08x (hex)", cpu_mask, cpu_mask);
-			make_task_cpuset(job, &cpu_mask, &mem_mask);
-		}
-		slurm_chkaffinity(&cpu_mask, job, 0);
-#ifdef HAVE_NUMA
-		slurm_chk_memset(&mem_mask, job);
-#endif
-	}
 #endif
 	return SLURM_SUCCESS;
 }
-- 
GitLab