From 16aca1194328eb54ce9d5f75c807fad8997067c1 Mon Sep 17 00:00:00 2001
From: Moe Jette <jette1@llnl.gov>
Date: Mon, 12 Dec 2005 20:15:43 +0000
Subject: [PATCH] Modify tests to work with sched_setaffinity functions having
 2 or 3 args.

---
 testsuite/expect/test1.89        |  1 +
 testsuite/expect/test1.89.prog.c | 55 +++++++++++++++++++++++++++-----
 2 files changed, 48 insertions(+), 8 deletions(-)

diff --git a/testsuite/expect/test1.89 b/testsuite/expect/test1.89
index 3e3003f29fd..310ff704666 100755
--- a/testsuite/expect/test1.89
+++ b/testsuite/expect/test1.89
@@ -77,6 +77,7 @@ set job_id $expect_out(1,string)
 # Run a job step to get allocated processor count and affinity
 #
 expect -re $prompt
+set mask 0
 set task_cnt 0
 send "$srun -c1 $file_prog\n"
 expect {
diff --git a/testsuite/expect/test1.89.prog.c b/testsuite/expect/test1.89.prog.c
index 70ad0230bfd..20e4fac18c6 100644
--- a/testsuite/expect/test1.89.prog.c
+++ b/testsuite/expect/test1.89.prog.c
@@ -1,26 +1,65 @@
+#define _GNU_SOURCE
 #include <errno.h>
 #include <sched.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <gnu/libc-version.h>
 
-main (int argc, char **argv)
+static void _load_mask(cpu_set_t *mask)
 {
-	char *task_str;
-	unsigned long mask;
-	int task_id;
+	int rc, affinity_args = 3;
+	int (*fptr_sched_getaffinity)() = sched_getaffinity;
 
-	if (sched_getaffinity((pid_t) 0, (unsigned int) sizeof(mask), &mask)
-			!= 0) {
-		fprintf(stderr, "ERROR: sched_getaffinity: %s\n", 
+#if defined __GLIBC__
+	const char *glibc_vers = gnu_get_libc_version();
+	if (glibc_vers != NULL) {
+		int scnt = 0, major = 0, minor = 0, point = 0;
+		scnt = sscanf (glibc_vers, "%d.%d.%d", &major,
+			&minor, &point);
+		if (scnt == 3) {
+			if ((major <= 2) && (minor <= 3) && (point <= 2)) {
+				affinity_args = 2;
+			}
+		}
+	}
+#endif
+	if (affinity_args == 3) {
+		rc = (*fptr_sched_getaffinity)((pid_t) 0, 
+			(unsigned int) sizeof(cpu_set_t), mask);
+	} else {
+		rc = (*fptr_sched_getaffinity)((pid_t) 0, mask);
+	}
+	if (rc != 0) {
+		fprintf(stderr, "ERROR: sched_getaffinity: %s\n",
 			strerror(errno));
 		exit(1);
 	}
+}
+
+static int _mask_to_int(cpu_set_t *mask)
+{
+	int i, rc = 0;
+	for (i=0; i<CPU_SETSIZE; i++) {
+		if (CPU_ISSET(i, mask))
+			rc += (1 << i);
+	}
+	return rc;
+}
+	
+	
+main (int argc, char **argv)
+{
+	char *task_str;
+	cpu_set_t mask;
+	int task_id;
+
+	_load_mask(&mask);
 	if ((task_str = getenv("SLURM_PROCID")) == NULL) {
 		fprintf(stderr, "ERROR: getenv(SLURM_TASKID) failed\n");
 		exit(1);
 	}
 	task_id = atoi(task_str);
-	printf("TASK_ID:%d,MASK:%lu\n", task_id, mask);
+	printf("TASK_ID:%d,MASK:%u\n", task_id, _mask_to_int(&mask));
 	exit(0);
 }
-- 
GitLab