Blame SOURCES/0001-tests-Add-utility-to-check-for-a-minimum-number-of-o.patch

fc9b0e
From d42f467a923dfc09309acb7a83b42e3285fbd8f4 Mon Sep 17 00:00:00 2001
fc9b0e
Message-Id: <d42f467a923dfc09309acb7a83b42e3285fbd8f4.1566225007.git.aquini@redhat.com>
fc9b0e
From: Sandipan Das <sandipan@linux.ibm.com>
fc9b0e
Date: Thu, 15 Aug 2019 13:08:28 +0530
fc9b0e
Subject: [RHEL7 PATCH 01/31] tests: Add utility to check for a minimum number
fc9b0e
 of online cpus
fc9b0e
fc9b0e
This adds a test utility to check if a minimum number (N)
fc9b0e
of online cpus are available. If available, this will also
fc9b0e
provide a list of the first N online cpus.
fc9b0e
fc9b0e
Signed-off-by: Sandipan Das <sandipan@linux.ibm.com>
fc9b0e
Signed-off-by: Eric B Munson <emunson@mgebm.net>
fc9b0e
Signed-off-by: Rafael Aquini <aquini@redhat.com>
fc9b0e
---
fc9b0e
 tests/hugetests.h |  1 +
fc9b0e
 tests/testutils.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
fc9b0e
 2 files changed, 49 insertions(+)
fc9b0e
fc9b0e
diff --git a/tests/hugetests.h b/tests/hugetests.h
fc9b0e
index 8b1d8d9..e3179e6 100644
fc9b0e
--- a/tests/hugetests.h
fc9b0e
+++ b/tests/hugetests.h
fc9b0e
@@ -43,6 +43,7 @@ extern char *test_name;
fc9b0e
 void check_free_huge_pages(int nr_pages_needed);
fc9b0e
 void check_must_be_root(void);
fc9b0e
 void check_hugetlb_shm_group(void);
fc9b0e
+void check_online_cpus(int[], int);
fc9b0e
 void test_init(int argc, char *argv[]);
fc9b0e
 int test_addr_huge(void *p);
fc9b0e
 unsigned long long get_mapping_page_size(void *p);
fc9b0e
diff --git a/tests/testutils.c b/tests/testutils.c
fc9b0e
index 6298370..2b47547 100644
fc9b0e
--- a/tests/testutils.c
fc9b0e
+++ b/tests/testutils.c
fc9b0e
@@ -33,6 +33,8 @@
fc9b0e
 #include <sys/ipc.h>
fc9b0e
 #include <sys/shm.h>
fc9b0e
 #include <sys/stat.h>
fc9b0e
+#include <sys/sysinfo.h>
fc9b0e
+#include <sys/types.h>
fc9b0e
 #include <sys/mman.h>
fc9b0e
 #include <fcntl.h>
fc9b0e
 
fc9b0e
@@ -80,6 +82,52 @@ void check_hugetlb_shm_group(void)
fc9b0e
 		CONFIG("Do not have permission to use SHM_HUGETLB");
fc9b0e
 }
fc9b0e
 
fc9b0e
+#define SYSFS_CPU_ONLINE_FMT	"/sys/devices/system/cpu/cpu%d/online"
fc9b0e
+
fc9b0e
+void check_online_cpus(int online_cpus[], int nr_cpus_needed)
fc9b0e
+{
fc9b0e
+	char cpu_state, path_buf[64];
fc9b0e
+	int total_cpus, cpu_idx, fd, ret, i;
fc9b0e
+
fc9b0e
+	total_cpus = get_nprocs_conf();
fc9b0e
+	cpu_idx = 0;
fc9b0e
+
fc9b0e
+	if (get_nprocs() < nr_cpus_needed)
fc9b0e
+		CONFIG("Atleast online %d cpus are required", nr_cpus_needed);
fc9b0e
+
fc9b0e
+	for (i = 0; i < total_cpus && cpu_idx < nr_cpus_needed; i++) {
fc9b0e
+		errno = 0;
fc9b0e
+		sprintf(path_buf, SYSFS_CPU_ONLINE_FMT, i);
fc9b0e
+		fd = open(path_buf, O_RDONLY);
fc9b0e
+		if (fd < 0) {
fc9b0e
+			/* If 'online' is absent, the cpu cannot be offlined */
fc9b0e
+			if (errno == ENOENT) {
fc9b0e
+				online_cpus[cpu_idx] = i;
fc9b0e
+				cpu_idx++;
fc9b0e
+				continue;
fc9b0e
+			} else {
fc9b0e
+				FAIL("Unable to open %s: %s", path_buf,
fc9b0e
+				     strerror(errno));
fc9b0e
+			}
fc9b0e
+		}
fc9b0e
+
fc9b0e
+		ret = read(fd, &cpu_state, 1);
fc9b0e
+		if (ret < 1)
fc9b0e
+			FAIL("Unable to read %s: %s", path_buf,
fc9b0e
+			     strerror(errno));
fc9b0e
+
fc9b0e
+		if (cpu_state == '1') {
fc9b0e
+			online_cpus[cpu_idx] = i;
fc9b0e
+			cpu_idx++;
fc9b0e
+		}
fc9b0e
+
fc9b0e
+		close(fd);
fc9b0e
+	}
fc9b0e
+
fc9b0e
+	if (cpu_idx < nr_cpus_needed)
fc9b0e
+		CONFIG("Atleast %d online cpus were not found", nr_cpus_needed);
fc9b0e
+}
fc9b0e
+
fc9b0e
 void  __attribute__((weak)) cleanup(void)
fc9b0e
 {
fc9b0e
 }
fc9b0e
-- 
fc9b0e
1.8.3.1
fc9b0e