diff mbox

[v2] ndctl: add unit test for presence of PCOMMIT

Message ID 1441985979-2963-1-git-send-email-ross.zwisler@linux.intel.com (mailing list archive)
State Accepted
Commit 202653080f52
Headers show

Commit Message

Ross Zwisler Sept. 11, 2015, 3:39 p.m. UTC
Fail our BAT test if run on systems without proper PCOMMIT support.
PCOMMIT is required on non-ADR x86 systems that want to use the PMEM
API.

Signed-off-by: Ross Zwisler <ross.zwisler@linux.intel.com>
---
 Makefile.am        | 10 ++++++---
 builtin-bat.c      |  6 ++++++
 lib/test-pcommit.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 test-pcommit.h     |  4 ++++
 4 files changed, 80 insertions(+), 3 deletions(-)
 create mode 100644 lib/test-pcommit.c
 create mode 100644 test-pcommit.h
diff mbox

Patch

diff --git a/Makefile.am b/Makefile.am
index 8b07408..e5b4b49 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -77,7 +77,8 @@  endif
 
 if ENABLE_DESTRUCTIVE
 ndctl_SOURCES += lib/blk_namespaces.c \
-		 lib/pmem_namespaces.c
+		 lib/pmem_namespaces.c \
+		 lib/test-pcommit.c
 ndctl_SOURCES += builtin-bat.c
 endif
 
@@ -115,13 +116,16 @@  TESTS = lib/test-libndctl lib/test-dpa-alloc lib/test-parent-uuid
 check_PROGRAMS = lib/test-libndctl lib/test-dpa-alloc lib/test-parent-uuid
 
 if ENABLE_DESTRUCTIVE
-TESTS += lib/test-blk-ns lib/test-pmem-ns
-check_PROGRAMS += lib/test-blk-ns lib/test-pmem-ns
+TESTS += lib/test-blk-ns lib/test-pmem-ns lib/test-pcommit
+check_PROGRAMS += lib/test-blk-ns lib/test-pmem-ns lib/test-pcommit
 endif
 
 lib_test_libndctl_SOURCES = lib/test-libndctl.c lib/test-core.c
 lib_test_libndctl_LDADD = lib/libndctl.la $(UUID_LIBS) $(KMOD_LIBS)
 
+lib_test_pcommit_SOURCES = lib/test-pcommit.c
+lib_test_pcommit_LDADD = lib/libndctl.la $(KMOD_LIBS)
+
 lib_test_blk_ns_SOURCES = lib/blk_namespaces.c
 lib_test_blk_ns_LDADD = lib/libndctl.la $(KMOD_LIBS)
 
diff --git a/builtin-bat.c b/builtin-bat.c
index 4c6ee64..e79a55d 100644
--- a/builtin-bat.c
+++ b/builtin-bat.c
@@ -1,5 +1,6 @@ 
 #include <stdio.h>
 #include <syslog.h>
+#include <test-pcommit.h>
 #include <test-blk-namespaces.h>
 #include <test-pmem-namespaces.h>
 #include <util/parse-options.h>
@@ -25,6 +26,11 @@  int cmd_bat(int argc, const char **argv)
 	if (argc)
 		usage_with_options(u, options);
 
+	rc = test_pcommit();
+	fprintf(stderr, "test_pcommit: %s\n", rc ? "FAIL" : "PASS");
+	if (rc)
+		return rc;
+
 	rc = test_blk_namespaces(loglevel);
 	fprintf(stderr, "test_blk_namespaces: %s\n", rc ? "FAIL" : "PASS");
 	if (rc)
diff --git a/lib/test-pcommit.c b/lib/test-pcommit.c
new file mode 100644
index 0000000..f40052f
--- /dev/null
+++ b/lib/test-pcommit.c
@@ -0,0 +1,63 @@ 
+/*
+ * test-pcommit: Make sure PCOMMIT is supported by the platform.
+ *
+ * Copyright (c) 2015, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
+ * more details.
+ */
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <test-pcommit.h>
+
+#define err(msg)\
+	fprintf(stderr, "%s:%d: %s (%s)\n", __func__, __LINE__, msg, strerror(errno))
+
+int test_pcommit(void)
+{
+	const char *pcommit = "pcommit";
+	const char *flags = "flags";
+	const int buffer_size = 1024;
+
+	char buffer[buffer_size];
+	FILE *cpuinfo;
+	char *token;
+
+	cpuinfo = fopen("/proc/cpuinfo", "r");
+	if (!cpuinfo) {
+		err("open");
+		return EBADF;
+	}
+
+        while (fgets(buffer, buffer_size, cpuinfo)) {
+		token = strtok(buffer, " :");
+
+		if (token &&
+		    strncmp(token, flags, strlen(flags)) != 0)
+			continue;
+
+		while (token != NULL) {
+			token = strtok(NULL, " ");
+			if (token &&
+			    strncmp(token, pcommit, strlen(pcommit)) == 0) {
+				fclose(cpuinfo);
+				return 0;
+			}
+		}
+        }
+
+	fclose(cpuinfo);
+	return ENOTSUP;
+}
+
+int __attribute__((weak)) main(int argc, char *argv[])
+{
+	return test_pcommit();
+}
diff --git a/test-pcommit.h b/test-pcommit.h
new file mode 100644
index 0000000..27e788d
--- /dev/null
+++ b/test-pcommit.h
@@ -0,0 +1,4 @@ 
+#ifndef __TEST_PCOMMIT__
+#define __TEST_PCOMMIT__
+int test_pcommit(void);
+#endif