public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH] numa_helper: fix compatibility with older libnuma
@ 2012-07-12 14:09 Jan Stancek
  2012-07-13  5:47 ` Caspar Zhang
  0 siblings, 1 reply; 3+ messages in thread
From: Jan Stancek @ 2012-07-12 14:09 UTC (permalink / raw)
  To: ltp-list

numa_helper relied on functions that do not exist
in old libnuma (<1), which caused that it couldn't be
compiled on old distros, for example RHEL 5.3.

Old libnuma and kernel also do not support MPOL_F_MEMS_ALLOWED,
in which case it will assume that any node with memory > 0 can
be used.

This patch adds compatibility for older versions, I tested it with:
numactl-0.9.8-7.el5
numactl-2.0.3-9.el6
numactl-2.0.7-6.el7

Signed-off-by: Jan Stancek <jstancek@redhat.com>
---
 testcases/kernel/syscalls/numa/lib/Makefile      |    2 +-
 testcases/kernel/syscalls/numa/lib/numa_helper.c |   52 +++++++++++++++++-----
 2 files changed, 41 insertions(+), 13 deletions(-)

diff --git a/testcases/kernel/syscalls/numa/lib/Makefile b/testcases/kernel/syscalls/numa/lib/Makefile
index d4a6f41..ba2fb48 100644
--- a/testcases/kernel/syscalls/numa/lib/Makefile
+++ b/testcases/kernel/syscalls/numa/lib/Makefile
@@ -20,7 +20,7 @@ top_srcdir		?= ../../../../..
 
 include $(top_srcdir)/include/mk/env_pre.mk
 
-CPPFLAGS		+= -I../../../include
+CPPFLAGS		+= -I../../../include $(NUMA_CPPFLAGS)
 LIB			:= libnuma_helper.a
 
 include $(top_srcdir)/include/mk/lib.mk
diff --git a/testcases/kernel/syscalls/numa/lib/numa_helper.c b/testcases/kernel/syscalls/numa/lib/numa_helper.c
index 855ee2c..20b06ca 100644
--- a/testcases/kernel/syscalls/numa/lib/numa_helper.c
+++ b/testcases/kernel/syscalls/numa/lib/numa_helper.c
@@ -53,46 +53,74 @@ int get_allowed_nodes_arr(int *num_allowed_nodes, int **allowed_nodes)
 {
 #if HAVE_NUMA_H
 	int i;
-	struct bitmask *allowed_nodemask = NULL;
+	nodemask_t *allowed_nodemask = NULL;
+	unsigned long max_node;
+
+#if !defined(LIBNUMA_API_VERSION) || LIBNUMA_API_VERSION < 2
+	max_node = NUMA_NUM_NODES;
+	/*
+	 * NUMA_NUM_NODES is not reliable, libnuma >=2 is looking
+	 * at /proc/self/status to figure out correct number.
+	 * If buffer is not large enough get_mempolicy will fail with EINVAL.
+	 */
+	if (max_node < 1024)
+		max_node = 1024;
+#else
+	max_node = numa_max_possible_node() + 1;
 #endif
+#endif /* HAVE_NUMA_H */
+
 	*num_allowed_nodes = 0;
 	if (allowed_nodes)
 		*allowed_nodes = NULL;
 
-#if HAVE_NUMA_H && HAVE_MPOL_CONSTANTS
-	if ((allowed_nodemask = numa_allocate_nodemask()) == NULL)
+#if HAVE_NUMA_H
+	if ((allowed_nodemask = malloc(max_node/8+1)) == NULL)
 		return -1;
+	nodemask_zero(allowed_nodemask);
 
 	if (allowed_nodes) {
-		*allowed_nodes = malloc(sizeof(int)*allowed_nodemask->size);
-		if (*allowed_nodes == NULL)
+		*allowed_nodes = malloc(sizeof(int)*max_node);
+		if (*allowed_nodes == NULL) {
+			free(allowed_nodemask);
 			return -1;
+		}
 	}
 
+#if MPOL_F_MEMS_ALLOWED
 	/*
 	 * avoid numa_get_mems_allowed(), because of bug in getpol()
 	 * utility function in older versions:
 	 * http://www.spinics.net/lists/linux-numa/msg00849.html
 	 */
-	if (syscall(__NR_get_mempolicy, NULL, allowed_nodemask->maskp,
-		allowed_nodemask->size, 0, MPOL_F_MEMS_ALLOWED) < 0) {
-		numa_bitmask_free(allowed_nodemask);
+	if (syscall(__NR_get_mempolicy, NULL, allowed_nodemask->n,
+		max_node, 0, MPOL_F_MEMS_ALLOWED) < 0) {
+		free(allowed_nodemask);
 		if (allowed_nodes) {
 			free(*allowed_nodes);
 			*allowed_nodes = NULL;
 		}
 		return -2;
 	}
+#else
+	/*
+	 * old libnuma/kernel don't have MPOL_F_MEMS_ALLOWED, so let's assume
+	 * that we can use any node with memory > 0
+	 */
+	for (i = 0; i < max_node; i++)
+		if (numa_node_size64(i, NULL) > 0)
+			nodemask_set(allowed_nodemask, i);
 
-	for (i = 0; i <	allowed_nodemask->size; i++) {
-		if (numa_bitmask_isbitset(allowed_nodemask, i)) {
+#endif /* MPOL_F_MEMS_ALLOWED */
+	for (i = 0; i < max_node; i++) {
+		if (nodemask_isset(allowed_nodemask, i)) {
 			if (allowed_nodes)
 				(*allowed_nodes)[*num_allowed_nodes] = i;
 			(*num_allowed_nodes)++;
 		}
 	}
-	numa_bitmask_free(allowed_nodemask);
-#endif
+	free(allowed_nodemask);
+#endif /* HAVE_NUMA_H */
 	return 0;
 }
 
-- 
1.7.1


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-07-13  7:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-07-12 14:09 [LTP] [PATCH] numa_helper: fix compatibility with older libnuma Jan Stancek
2012-07-13  5:47 ` Caspar Zhang
2012-07-13  7:05   ` Wanlong Gao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox