diff mbox

ndctl: add unit test for presence of PCOMMIT

Message ID 1441927523-6736-1-git-send-email-ross.zwisler@linux.intel.com (mailing list archive)
State Superseded
Headers show

Commit Message

Ross Zwisler Sept. 10, 2015, 11:25 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 | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 test-pcommit.h     |  4 +++
 4 files changed, 94 insertions(+), 3 deletions(-)
 create mode 100644 lib/test-pcommit.c
 create mode 100644 test-pcommit.h

Comments

Dan Williams Sept. 11, 2015, 12:23 a.m. UTC | #1
On Thu, Sep 10, 2015 at 4:25 PM, Ross Zwisler
<ross.zwisler@linux.intel.com> wrote:
> 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 | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  test-pcommit.h     |  4 +++
>  4 files changed, 94 insertions(+), 3 deletions(-)
>  create mode 100644 lib/test-pcommit.c
>  create mode 100644 test-pcommit.h
>
[..]
> diff --git a/lib/test-pcommit.c b/lib/test-pcommit.c
> new file mode 100644
> index 0000000..be45ca2
> --- /dev/null
> +++ b/lib/test-pcommit.c
> @@ -0,0 +1,77 @@
> +/*
> + * 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 <fcntl.h>
> +#include <linux/fs.h>
> +#include <linux/ndctl.h>

Hmm we don't need ndctl.h for this test, and including it like this
causes compile errors in pre-4.2 environments (as I recently found
trying to get our unit tests ready to run in the 0day environment).

In general use the following pattern when you need to include it.

#ifdef HAVE_NDCTL_H
#include <linux/ndctl.h>
#else
#include <ndctl.h>
#endif

> +#include <ndctl/libndctl.h>

This can go to, and probably a few more, but the ndctl.h one above is
the only problematic one.

> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <sys/ioctl.h>
> +#include <syslog.h>
> +#include <sys/stat.h>
> +#include <sys/types.h>
> +#include <unistd.h>
> +#include <uuid/uuid.h>
> +#include <test-pcommit.h>
> +
> +#define err(msg)\
> +       fprintf(stderr, "%s:%d: %s (%s)\n", __func__, __LINE__, msg, strerror(errno))
> +
> +static const char *comm = "test-pcommit";
> +
> +int test_pcommit(int log_level)
> +{
> +       const char *pcommit = "pcommit";
> +       const char *flags = "flags";
> +       const int buffer_size = 1024;
> +
> +       char buffer[buffer_size];
> +       FILE *cpuinfo;
> +       char *token;
> +

You might add a call to ndctl_test_attempt() here to only attempt this
on 4.2 and later environments.

        if (!ndctl_test_attempt(test, KERNEL_VERSION(4, 2, 0)))
                return 77;
Ross Zwisler Sept. 11, 2015, 1:01 a.m. UTC | #2
On Thu, Sep 10, 2015 at 05:23:54PM -0700, Dan Williams wrote:
> On Thu, Sep 10, 2015 at 4:25 PM, Ross Zwisler
> <ross.zwisler@linux.intel.com> wrote:
> > 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>
<>
> > +int test_pcommit(int log_level)
> > +{
> > +       const char *pcommit = "pcommit";
> > +       const char *flags = "flags";
> > +       const int buffer_size = 1024;
> > +
> > +       char buffer[buffer_size];
> > +       FILE *cpuinfo;
> > +       char *token;
> > +
> 
> You might add a call to ndctl_test_attempt() here to only attempt this
> on 4.2 and later environments.
> 
>         if (!ndctl_test_attempt(test, KERNEL_VERSION(4, 2, 0)))
>                 return 77;

Is there a reason to add this restriction?  This test doesn't rely on anything
in ndctl (I'll remove the extra includes), should work with any kernel version
that supports the "pcommit" string in /proc/cpuid.
Dan Williams Sept. 11, 2015, 1:12 a.m. UTC | #3
On Thu, Sep 10, 2015 at 6:01 PM, Ross Zwisler
<ross.zwisler@linux.intel.com> wrote:
> On Thu, Sep 10, 2015 at 05:23:54PM -0700, Dan Williams wrote:
>> On Thu, Sep 10, 2015 at 4:25 PM, Ross Zwisler
>> <ross.zwisler@linux.intel.com> wrote:
>> > 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>
> <>
>> > +int test_pcommit(int log_level)
>> > +{
>> > +       const char *pcommit = "pcommit";
>> > +       const char *flags = "flags";
>> > +       const int buffer_size = 1024;
>> > +
>> > +       char buffer[buffer_size];
>> > +       FILE *cpuinfo;
>> > +       char *token;
>> > +
>>
>> You might add a call to ndctl_test_attempt() here to only attempt this
>> on 4.2 and later environments.
>>
>>         if (!ndctl_test_attempt(test, KERNEL_VERSION(4, 2, 0)))
>>                 return 77;
>
> Is there a reason to add this restriction?  This test doesn't rely on anything
> in ndctl (I'll remove the extra includes), should work with any kernel version
> that supports the "pcommit" string in /proc/cpuid.

Not really, just for consistency with the other tests since the cpu
supporting pcommit on pre-4.2 kernels isn't all that useful.  But I'm
fine if you leave it in after fixing up the includes.
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..88ea0b5 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(loglevel);
+	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..be45ca2
--- /dev/null
+++ b/lib/test-pcommit.c
@@ -0,0 +1,77 @@ 
+/*
+ * 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 <fcntl.h>
+#include <linux/fs.h>
+#include <linux/ndctl.h>
+#include <ndctl/libndctl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <syslog.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <uuid/uuid.h>
+#include <test-pcommit.h>
+
+#define err(msg)\
+	fprintf(stderr, "%s:%d: %s (%s)\n", __func__, __LINE__, msg, strerror(errno))
+
+static const char *comm = "test-pcommit";
+
+int test_pcommit(int log_level)
+{
+	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[])
+{
+	comm = argv[0];
+	return test_pcommit(LOG_DEBUG);
+}
diff --git a/test-pcommit.h b/test-pcommit.h
new file mode 100644
index 0000000..9cf6bc9
--- /dev/null
+++ b/test-pcommit.h
@@ -0,0 +1,4 @@ 
+#ifndef __TEST_PCOMMIT__
+#define __TEST_PCOMMIT__
+int test_pcommit(int loglevel);
+#endif