diff mbox series

[3/4] eal: allow checking CPU flags by name

Message ID 20190529154132.49955-4-bruce.richardson@intel.com (mailing list archive)
State Rejected, archived
Delegated to: Thomas Monjalon
Headers show
Series Enhance CPU flag support | expand

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation fail Compilation issues

Commit Message

Bruce Richardson May 29, 2019, 3:41 p.m. UTC
Rather than using enum values for CPU flags, which means the symbols don't
exist on other architectures, provide a flag lookup by name, allowing us to
unconditionally check for a CPU flag.

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
 lib/librte_eal/common/eal_common_cpuflags.c   | 41 +++++++++++++++++
 .../common/include/generic/rte_cpuflags.h     | 44 ++++++++++++++++++-
 lib/librte_eal/rte_eal_version.map            |  3 ++
 3 files changed, 87 insertions(+), 1 deletion(-)

Comments

David Marchand June 27, 2019, 1:22 p.m. UTC | #1
On Wed, May 29, 2019 at 5:42 PM Bruce Richardson <bruce.richardson@intel.com>
wrote:

> Rather than using enum values for CPU flags, which means the symbols don't
> exist on other architectures, provide a flag lookup by name, allowing us to
> unconditionally check for a CPU flag.
>

Did you consider passing a string for the CPU architecture rather than an
enum?
It would have to be compared to RTE_ARCH in rte_cpu_get_flagname_enabled.
Or to accomodate with x86_64/i686, this could be a cpu arch family.

This avoids adding a new C type that seems quite limited wrt its uses.
Bruce Richardson June 28, 2019, 12:40 p.m. UTC | #2
On Thu, Jun 27, 2019 at 03:22:14PM +0200, David Marchand wrote:
>    On Wed, May 29, 2019 at 5:42 PM Bruce Richardson
>    <[1]bruce.richardson@intel.com> wrote:
> 
>      Rather than using enum values for CPU flags, which means the symbols
>      don't
>      exist on other architectures, provide a flag lookup by name,
>      allowing us to
>      unconditionally check for a CPU flag.
> 
>    Did you consider passing a string for the CPU architecture rather than
>    an enum?
>    It would have to be compared to RTE_ARCH in
>    rte_cpu_get_flagname_enabled.
>    Or to accomodate with x86_64/i686, this could be a cpu arch family.
>    This avoids adding a new C type that seems quite limited wrt its uses.
>    --
>    David Marchand
>

I'm not sure I really see the value in having string names for the
architecture values, I think it would be a lot more clunky to manage rather
than having an enum value. The key difference vs the flags is that the
flags are only valid per-architecture while the architecture defines can be
globally valid, and secondly there is a finite, and small, number of
architectures compared to the number of flags supported.

If you feel strongly about it I can investigate it, but I'm not sure I see
the value in doing so right now if the only benefit is avoiding the enum.

/Bruce
David Marchand June 28, 2019, 1:34 p.m. UTC | #3
On Fri, Jun 28, 2019 at 2:40 PM Bruce Richardson <bruce.richardson@intel.com>
wrote:

> On Thu, Jun 27, 2019 at 03:22:14PM +0200, David Marchand wrote:
> >    On Wed, May 29, 2019 at 5:42 PM Bruce Richardson
> >    <[1]bruce.richardson@intel.com> wrote:
> >
> >      Rather than using enum values for CPU flags, which means the symbols
> >      don't
> >      exist on other architectures, provide a flag lookup by name,
> >      allowing us to
> >      unconditionally check for a CPU flag.
> >
> >    Did you consider passing a string for the CPU architecture rather than
> >    an enum?
> >    It would have to be compared to RTE_ARCH in
> >    rte_cpu_get_flagname_enabled.
> >    Or to accomodate with x86_64/i686, this could be a cpu arch family.
> >    This avoids adding a new C type that seems quite limited wrt its uses.
> >    --
> >    David Marchand
> >
>
> I'm not sure I really see the value in having string names for the
> architecture values, I think it would be a lot more clunky to manage rather
> than having an enum value. The key difference vs the flags is that the
> flags are only valid per-architecture while the architecture defines can be
> globally valid, and secondly there is a finite, and small, number of
> architectures compared to the number of flags supported.
>
> If you feel strongly about it I can investigate it, but I'm not sure I see
> the value in doing so right now if the only benefit is avoiding the enum.
>

I suppose we won't have too much trouble handling ABI breakage (thinking
about when we will remove x86 support).
Ok, let's go with this.
diff mbox series

Patch

diff --git a/lib/librte_eal/common/eal_common_cpuflags.c b/lib/librte_eal/common/eal_common_cpuflags.c
index 3a055f7c7..5084a3e7a 100644
--- a/lib/librte_eal/common/eal_common_cpuflags.c
+++ b/lib/librte_eal/common/eal_common_cpuflags.c
@@ -3,6 +3,7 @@ 
  */
 
 #include <stdio.h>
+#include <string.h>
 
 #include <rte_common.h>
 #include <rte_cpuflags.h>
@@ -48,3 +49,43 @@  rte_cpu_is_supported(void)
 
 	return 1;
 }
+
+static enum rte_cpu_flag_t
+rte_cpu_get_flag(const char *flagname)
+{
+	int i;
+
+	if (flagname == NULL)
+		return RTE_CPUFLAG_NUMFLAGS;
+
+	for (i = 0; i < RTE_CPUFLAG_NUMFLAGS; i++)
+		if (strcmp(flagname, rte_cpu_get_flag_name(i)) == 0)
+			break;
+	return i;
+}
+
+static int
+rte_cpu_is_architecture(enum rte_cpu_arch arch)
+{
+	switch (arch) {
+	case rte_cpu_arch_arm:
+		return strcmp(RTE_ARCH, "arm") == 0 ||
+				strcmp(RTE_ARCH, "arm64") == 0;
+	case rte_cpu_arch_ppc:
+		return strcmp(RTE_ARCH, "ppc_64") == 0;
+	case rte_cpu_arch_x86:
+		return strcmp(RTE_ARCH, "x86_64") == 0 ||
+				strcmp(RTE_ARCH, "i686") == 0;
+	default:
+		return -EINVAL;
+	}
+}
+
+int
+rte_cpu_get_flagname_enabled(enum rte_cpu_arch arch, const char *flagname)
+{
+	if (!rte_cpu_is_architecture(arch))
+		return 0;
+
+	return rte_cpu_get_flag_enabled(rte_cpu_get_flag(flagname)) == 1;
+}
diff --git a/lib/librte_eal/common/include/generic/rte_cpuflags.h b/lib/librte_eal/common/include/generic/rte_cpuflags.h
index 156ea0029..a53551eba 100644
--- a/lib/librte_eal/common/include/generic/rte_cpuflags.h
+++ b/lib/librte_eal/common/include/generic/rte_cpuflags.h
@@ -10,7 +10,8 @@ 
  * Architecture specific API to determine available CPU features at runtime.
  */
 
-#include "rte_common.h"
+#include <rte_common.h>
+#include <rte_compat.h>
 #include <errno.h>
 
 /**
@@ -46,6 +47,47 @@  __extension__
 int
 rte_cpu_get_flag_enabled(enum rte_cpu_flag_t feature);
 
+/**
+ * Enumeration of the various CPU architectures supported by DPDK.
+ *
+ * When checking for CPU flags by name, it's possible that multiple
+ * architectures have flags with the same name e.g. AES is defined in
+ * both arm and x86 feature lists. Therefore we need to pass in at runtime
+ * the architecture we are checking for as well as the CPU flag. This enum
+ * defines the various supported architectures to be used for that checking.
+ */
+enum rte_cpu_arch {
+	rte_cpu_arch_arm = 0,
+	rte_cpu_arch_ppc,
+	rte_cpu_arch_x86,
+
+	rte_cpu_num_arch /* must always be the last */
+};
+
+/**
+ * Function for checking if a named CPU flag is enabled
+ *
+ * Wrapper around the rte_cpu_get_flag() and rte_cpu_get_flag_enabled()
+ * calls, which is safe to use even if the flag doesn't exist on target
+ * architecture. The function also verifies the target architecture so that
+ * we can distinguish e.g. AES support for arm vs x86 platforms.
+ *
+ * Note: This function uses multiple string compares in its operation and
+ * so is not recommended for data-path use. It should be called once, and
+ * the return value cached for later use.
+ *
+ * @param arch
+ *   The architecture on which we need to check the flag, since multiple
+ *   architectures could have flags with the same name.
+ * @param flagname
+ *   The name of the flag to query
+ * @return
+ *   1 if flag is available
+ *   0 if flag is not unavailable or invalid
+ */
+__rte_experimental int
+rte_cpu_get_flagname_enabled(enum rte_cpu_arch arch, const char *flagname);
+
 /**
  * This function checks that the currently used CPU supports the CPU features
  * that were specified at compile time. It is called automatically within the
diff --git a/lib/librte_eal/rte_eal_version.map b/lib/librte_eal/rte_eal_version.map
index 245493461..5ca5584a5 100644
--- a/lib/librte_eal/rte_eal_version.map
+++ b/lib/librte_eal/rte_eal_version.map
@@ -378,4 +378,7 @@  EXPERIMENTAL {
 	rte_service_lcore_attr_get;
 	rte_service_lcore_attr_reset_all;
 	rte_service_may_be_active;
+
+	# added in 19.08
+	rte_cpu_get_flagname_enabled;
 };