Enhance CPU flag support
cover Enhance CPU flag support
Currently in DPDK, any checks for the presence or absense of specific CPU flags has to be enclosed in #ifdef blocks, since the flags are conditionally defined per architecture as enums. This set attempts to improve this situation by allowing the flags to be queried as strings, with a CPU architecture type also specified. One new public API is provided for this. [Other APIs used by that are internal for now, but have an rte_ prefix in case we want to expose them publically later]
To test out this new functionality the crc code in the net library is updated to use this, demonstrating how the code can be simplified and the use of #ifdef's reduced.
Bruce Richardson (4):
build: fix quoting on RTE_ARCH string value config/arm: fix missing define for arm platforms
eal: allow checking CPU flags by name
net: replace ifdefs with runtime branches
config/arm/meson.build | 2 + config/ppc_64/meson.build | 2 +- config/x86/meson.build | 4 +- lib/librte_eal/common/eal_common_cpuflags.c | 41 +++++++++++++++++ .../common/include/generic/rte_cpuflags.h | 44 ++++++++++++++++++- lib/librte_eal/rte_eal_version.map | 3 ++ lib/librte_net/rte_net_crc.c | 38 ++++++++++------ 7 files changed, 117 insertions(+), 17 deletions(-)
Signature · 1 line
2.21.0
PATCH 1/4 build: fix quoting on RTE_ARCH string value
The value for RTE_ARCH needs to be quoted when output to the rte_build_config.h file, so use "set_quoted" rather than "set" when assigning it.
--- config/ppc_64/meson.build | 2 +- config/x86/meson.build | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
config/ppc_64/meson.build +1 -1
@@ -4,7 +4,7 @@ if not dpdk_conf.get('RTE_ARCH_64') error('Only 64-bit compiles are supported for this platform type') endif -dpdk_conf.set('RTE_ARCH', 'ppc_64') +dpdk_conf.set_quoted('RTE_ARCH', 'ppc_64') dpdk_conf.set('RTE_ARCH_PPC_64', 1) # overrides specific to ppc64
config/x86/meson.build +2 -2
@@ -31,10 +31,10 @@ endforeach dpdk_conf.set('RTE_ARCH_X86', 1) if dpdk_conf.get('RTE_ARCH_64') dpdk_conf.set('RTE_ARCH_X86_64', 1) - dpdk_conf.set('RTE_ARCH', 'x86_64') + dpdk_conf.set_quoted('RTE_ARCH', 'x86_64') else dpdk_conf.set('RTE_ARCH_I686', 1) - dpdk_conf.set('RTE_ARCH', 'i686') + dpdk_conf.set_quoted('RTE_ARCH', 'i686') endif if cc.get_define('__AES__', args: machine_args) != ''
Signature · 1 line
2.21.0
Re: [dpdk-dev] [PATCH 1/4] build: fix quoting on RTE_ARCH string value
Quoted 14 lines · Bruce Richardson
The value for
RTE_ARCHneeds to be quoted when output to therte_build_config.hfile, so use "set_quoted" rather than "set" when assigning it.Fixes a25a650be5f0 ("build: add infrastructure for meson and ninjabuilds")
Fixes 54d609a13876 ("build: add ppc64 meson build")
Quoted 7 lines
Signed-off-by: Bruce Richardson <
[email protected]Quoted 1 line
--- config/ppc_64/meson.build | 2 +- config/x86/meson.build | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
Signature · 2 lines
Kind regards, Luca Boccassi
PATCH 2/4 config/arm: fix missing define for arm platforms
In reply to Bruce RichardsonThe RTE_ARCH macro was defined for all platforms when using make, but was only defined for x86 and ppc platforms when using meson. Fix this omission.
--- config/arm/meson.build | 2 ++ 1 file changed, 2 insertions(+)
config/arm/meson.build +2 -0
@@ -113,6 +113,7 @@ dpdk_conf.set('RTE_FORCE_INTRINSICS', 1) if not dpdk_conf.get('RTE_ARCH_64') dpdk_conf.set('RTE_CACHE_LINE_SIZE', 64) + dpdk_conf.set_quoted('RTE_ARCH', 'arm') dpdk_conf.set('RTE_ARCH_ARM', 1) dpdk_conf.set('RTE_ARCH_ARMv7', 1) # the minimum architecture supported, armv7-a, needs the following,
@@ -120,6 +121,7 @@ if not dpdk_conf.get('RTE_ARCH_64') machine_args += '-mfpu=neon' else dpdk_conf.set('RTE_CACHE_LINE_SIZE', 128) + dpdk_conf.set_quoted('RTE_ARCH', 'arm64') dpdk_conf.set('RTE_ARCH_ARM64', 1) machine = []
Signature · 1 line
2.21.0
PATCH 3/4 eal: allow checking CPU flags by name
In reply to Bruce RichardsonRather 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.
--- 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(-)
lib/librte_eal/common/eal_common_cpuflags.c +41 -0
@@ -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 ; +}
lib/librte_eal/common/include/generic/rte_cpuflags.h +43 -1
56 lines not on this page · raw message
lib/librte_eal/rte_eal_version.map +3 -0
@@ -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; };
Signature · 1 line
2.21.0
Re: [dpdk-dev] [PATCH 3/4] eal: allow checking CPU flags by name
Quoted 4 lines · Bruce Richardson
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.
Signature · 1 line
David Marchand
Re: [dpdk-dev] [PATCH 3/4] eal: allow checking CPU flags by name
Quoted 9 lines · David Marchand
On Wed, May 29, 2019 at 5:42 PM Bruce Richardson <[1][email protected]> 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.
Quoted 9 lines
Did you consider passing a string for the CPU architecture rather than an enum?
It would have to be compared toRTE_ARCHinrte_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
Re: [dpdk-dev] [PATCH 3/4] eal: allow checking CPU flags by name
Quoted 27 lines · Bruce Richardson
Quoted 9 lines · David Marchand
On Wed, May 29, 2019 at 5:42 PM Bruce Richardson <[1][email protected]> 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.Quoted 9 lines
Did you consider passing a string for the CPU architecture rather than an enum?
It would have to be compared toRTE_ARCHinrte_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 MarchandI'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.
Quoted 3 lines
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.
Signature · 1 line
David Marchand
PATCH 4/4 net: replace ifdefs with runtime branches
In reply to Bruce RichardsonUse the flag checking functions and a couple of empty stubs to remove the ifdefs from the middle of the C code, and replace them with more readable regular if statements. Other ifdefs at the top of the file are kept, but are not mixed with C code, so there is a clean separation.
--- lib/librte_net/rte_net_crc.c | 38 ++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 13 deletions(-)
lib/librte_net/rte_net_crc.c +25 -13
@@ -18,8 +18,17 @@#ifdef X86_64_SSE42_PCLMULQDQ #include <net_crc_sse.h> -#elif definedARM64_NEON_PMULL +#else +/* define stubs for the SSE functions to avoid compiler errors */ +#define handlers_sse42 handlers_scalar +#define rte_net_crc_sse42_init () do { } while(0) +#endif + +#ifdef ARM64_NEON_PMULL #include <net_crc_neon.h> +#else +#define handlers_neon handlers_scalar +#define rte_net_crc_neon_init () do { } while(0)#endif /* crc tables */
@@ -140,18 +149,19 @@ void rte_net_crc_set_alg(enum rte_net_crc_alg alg) {switch (alg) { -#ifdef X86_64_SSE42_PCLMULQDQ case RTE_NET_CRC_SSE42 : - handlers = handlers_sse42; -break ; -#elif definedARM64_NEON_PMULL +if (rte_cpu_get_flagname_enabled (rte_cpu_arch_x86, +"RTE_CPUFLAG_SSE4_2" )) { + handlers = handlers_sse42; +break ; + }/* fall-through */ case RTE_NET_CRC_NEON : -if (rte_cpu_get_flag_enabled (RTE_CPUFLAG_PMULL )) { +if (rte_cpu_get_flagname_enabled (rte_cpu_arch_arm, +"RTE_CPUFLAG_PMULL" )) { handlers = handlers_neon;break ; } -#endif /* fall-through */ case RTE_NET_CRC_SCALAR :/* fall-through */
@@ -182,15 +192,17 @@ RTE_INIT(rte_net_crc_init)rte_net_crc_scalar_init (); -#ifdef X86_64_SSE42_PCLMULQDQ - alg =RTE_NET_CRC_SSE42 ; -rte_net_crc_sse42_init (); -#elif definedARM64_NEON_PMULL -if (rte_cpu_get_flag_enabled (RTE_CPUFLAG_PMULL )) { +if (rte_cpu_get_flagname_enabled (rte_cpu_arch_x86, +"RTE_CPUFLAG_SSE4_2" )) { + alg =RTE_NET_CRC_SSE42 ; +rte_net_crc_sse42_init (); + } + +if (rte_cpu_get_flagname_enabled (rte_cpu_arch_arm, +"RTE_CPUFLAG_PMULL" )) { alg =RTE_NET_CRC_NEON ;rte_net_crc_neon_init (); } -#endif rte_net_crc_set_alg (alg); }
Signature · 1 line
2.21.0
Re: [dpdk-dev] [PATCH 4/4] net: replace ifdefs with runtime branches
29/05/2019 17:41, Bruce Richardson:
Quoted 9 lines
Use the flag checking functions and a couple of empty stubs to remove the ifdefs from the middle of the C code, and replace them with more readable regular if statements. Other ifdefs at the top of the file are kept, but are not mixed with C code, so there is a clean separation.
Signed-off-by Bruce Richardson <[email protected]>--- lib/librte_net/rte_net_crc.c | 38 ++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 13 deletions(-)
The result is more lines of code :)
Quoted 9 lines
lib/librte_net/rte_net_crc.c +2
-1@@ -18,8 +18,17 @@#ifdef X86_64_SSE42_PCLMULQDQ #include <net_crc_sse.h> -#elif definedARM64_NEON_PMULL +#else +/* define stubs for the SSE functions to avoid compiler errors */
lib/librte_net/rte_net_crc.c line 32
Quoted 10 lines
lib/librte_net/rte_net_crc.c +8
-0+#define handlers_sse42 handlers_scalar +#define rte_net_crc_sse42_init () do { } while(0) +#endif + +#ifdef ARM64_NEON_PMULL #include <net_crc_neon.h> +#else +#define handlers_neon handlers_scalar +#define rte_net_crc_neon_init () do { } while(0)#endif
Looking at the need for stubs, I don't see the benefit.
lib/librte_net/rte_net_crc.c void · line 154
Quoted 10 lines
lib/librte_net/rte_net_crc.c +0
-0rte_net_crc_set_alg(enum rte_net_crc_alg alg) {lib/librte_net/rte_net_crc.c +2
-4switch (alg) { -#ifdef X86_64_SSE42_PCLMULQDQ case RTE_NET_CRC_SSE42 : - handlers = handlers_sse42; -break ; -#elif definedARM64_NEON_PMULL +if (rte_cpu_get_flagname_enabled (rte_cpu_arch_x86, +"RTE_CPUFLAG_SSE4_2" )) {
Why the CPU flags strings are prefixed with RTE_CPUFLAG_?
Re: [dpdk-dev] [PATCH 4/4] net: replace ifdefs with runtime branches
Quoted 26 lines · Thomas Monjalon
29/05/2019 17:41, Bruce Richardson:
Quoted 9 lines
Use the flag checking functions and a couple of empty stubs to remove the ifdefs from the middle of the C code, and replace them with more readable regular if statements. Other ifdefs at the top of the file are kept, but are not mixed with C code, so there is a clean separation.
Signed-off-by Bruce Richardson <[email protected]>--- lib/librte_net/rte_net_crc.c | 38 ++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 13 deletions(-)The result is more lines of code :)
Quoted 13 lines
lib/librte_net/rte_net_crc.c +6
-1@@ -18,8 +18,17 @@#ifdef X86_64_SSE42_PCLMULQDQ #include <net_crc_sse.h> -#elif definedARM64_NEON_PMULL +#else +/* define stubs for the SSE functions to avoid compiler errors */ +#define handlers_sse42 handlers_scalar +#define rte_net_crc_sse42_init () do { } while(0) +#endif +
Quoted 9 lines
lib/librte_net/rte_net_crc.c line 32
Quoted 6 lines
lib/librte_net/rte_net_crc.c +4
-0+#ifdef ARM64_NEON_PMULL #include <net_crc_neon.h> +#else +#define handlers_neon handlers_scalar +#define rte_net_crc_neon_init () do { } while(0)#endif Looking at the need for stubs, I don't see the benefit.
Yes, one needs stubs, but those are placed in a single place, and the main C-code itself is free of ifdefs running through it. I'd view this as a good thing in limiting the scope of any ifdef-ery, since it annoys me looking at #ifdefs in the middle of functions (since it messes up the indentation flow of the code if nothing else!).
If you don't see this as a big benefit, then there is not a lot else to commend this set for you, sadly. It just seemed a nice improvement to me - irrespective of net lines of code.
/Bruce
Re: [dpdk-dev] [PATCH 4/4] net: replace ifdefs with runtime branches
01/07/2019 22:41, Bruce Richardson:
Quoted 42 lines · Thomas Monjalon
Quoted 26 lines
29/05/2019 17:41, Bruce Richardson:
Quoted 9 lines
Use the flag checking functions and a couple of empty stubs to remove the ifdefs from the middle of the C code, and replace them with more readable regular if statements. Other ifdefs at the top of the file are kept, but are not mixed with C code, so there is a clean separation.
Signed-off-by Bruce Richardson <[email protected]>--- lib/librte_net/rte_net_crc.c | 38 ++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 13 deletions(-)The result is more lines of code :)
Quoted 13 lines
lib/librte_net/rte_net_crc.c +6
-1@@ -18,8 +18,17 @@#ifdef X86_64_SSE42_PCLMULQDQ #include <net_crc_sse.h> -#elif definedARM64_NEON_PMULL +#else +/* define stubs for the SSE functions to avoid compiler errors */ +#define handlers_sse42 handlers_scalar +#define rte_net_crc_sse42_init () do { } while(0) +#endif +Quoted 9 lines
lib/librte_net/rte_net_crc.c line 32
Quoted 6 lines
lib/librte_net/rte_net_crc.c +4
-0+#ifdef ARM64_NEON_PMULL #include <net_crc_neon.h> +#else +#define handlers_neon handlers_scalar +#define rte_net_crc_neon_init () do { } while(0)#endif Looking at the need for stubs, I don't see the benefit.
Yes, one needs stubs, but those are placed in a single place, and the main C-code itself is free of ifdefs running through it. I'd view this as a good thing in limiting the scope of any ifdef-ery, since it annoys me looking at #ifdefs in the middle of functions (since it messes up the indentation flow of the code if nothing else!).
Quoted 3 lines
If you don't see this as a big benefit, then there is not a lot else to commend this set for you, sadly. It just seemed a nice improvement to me - irrespective of net lines of code.
Any other opinion?
Re: [dpdk-dev] [PATCH 4/4] net: replace ifdefs with runtime branches
Quoted 44 lines
01/07/2019 22:41, Bruce Richardson:
Quoted 42 lines · Thomas Monjalon
Quoted 26 lines
29/05/2019 17:41, Bruce Richardson:
Quoted 9 lines
Use the flag checking functions and a couple of empty stubs to remove the ifdefs from the middle of the C code, and replace them with more readable regular if statements. Other ifdefs at the top of the file are kept, but are not mixed with C code, so there is a clean separation.
Signed-off-by Bruce Richardson <[email protected]>--- lib/librte_net/rte_net_crc.c | 38 ++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 13 deletions(-)The result is more lines of code :)
Quoted 13 lines
lib/librte_net/rte_net_crc.c +6
-1@@ -18,8 +18,17 @@#ifdef X86_64_SSE42_PCLMULQDQ #include <net_crc_sse.h> -#elif definedARM64_NEON_PMULL +#else +/* define stubs for the SSE functions to avoid compiler errors */ +#define handlers_sse42 handlers_scalar +#define rte_net_crc_sse42_init () do { } while(0) +#endif +Quoted 9 lines
lib/librte_net/rte_net_crc.c line 32
Quoted 6 lines
lib/librte_net/rte_net_crc.c +4
-0+#ifdef ARM64_NEON_PMULL #include <net_crc_neon.h> +#else +#define handlers_neon handlers_scalar +#define rte_net_crc_neon_init () do { } while(0)#endif Looking at the need for stubs, I don't see the benefit.
Yes, one needs stubs, but those are placed in a single place, and the main C-code itself is free of ifdefs running through it. I'd view this as a good thing in limiting the scope of any ifdef-ery, since it annoys me looking at #ifdefs in the middle of functions (since it messes up the indentation flow of the code if nothing else!).
Quoted 5 lines
Quoted 3 lines
If you don't see this as a big benefit, then there is not a lot else to commend this set for you, sadly. It just seemed a nice improvement to me - irrespective of net lines of code.
Any other opinion?
I support adding a few lines of code in the slow path to provide cleaner separation of architecture specific code, though the existing IFDEF code doesn't appear very intrusive in this case. My preference would be architecture specific header files and strong/weak linking to pull in the appropriate code.
Dave
Quoted 36 lines
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Bruce Richardson
Sent: Wednesday, May 29, 2019 4:41 PM
To: [email protected]
Cc: Richardson, Bruce <[email protected]>
Subject: [dpdk-dev] [PATCH 0/4] Enhance CPU flag supportCurrently in DPDK, any checks for the presence or absense of specific CPU flags has to be enclosed in #ifdef blocks, since the flags are conditionally defined per architecture as enums. This set attempts to improve this situation by allowing the flags to be queried as strings, with a CPU architecture type also specified. One new public API is provided for this. [Other APIs used by that are internal for now, but have an
rte_prefix in case we want to expose them publically later]To test out this new functionality the crc code in the net library is updated to use this, demonstrating how the code can be simplified and the use of #ifdef's reduced.
Bruce Richardson (4):
build: fix quoting onRTE_ARCHstring value config/arm: fix missing define for arm platforms
eal: allow checking CPU flags by name
net: replace ifdefs with runtime branchesconfig/arm/meson.build | 2 + config/ppc_64/meson.build | 2 +- config/x86/meson.build | 4 +- lib/librte_eal/common/eal_common_cpuflags.c | 41 +++++++++++++++++ .../common/include/generic/rte_cpuflags.h | 44 ++++++++++++++++++- lib/librte_eal/rte_eal_version.map | 3 ++ lib/librte_net/rte_net_crc.c | 38 ++++++++++------ 7 files changed, 117 insertions(+), 17 deletions(-)
Quoted 1 line
--
Series Acked-by: Konstantin Ananyev <[email protected]>
Quoted 1 line
2.21.0