DPDK patches and discussions
 help / color / mirror / Atom feed
* [PATCH v3] graph: expose node context as pointers
@ 2024-03-25 10:05 Robin Jarry
  2024-03-25 10:59 ` Jerin Jacob
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Robin Jarry @ 2024-03-25 10:05 UTC (permalink / raw)
  To: dev, Jerin Jacob, Kiran Kumar K, Nithin Dabilpuram, Zhirun Yan
  Cc: Tyler Retzlaff

In some cases, the node context data is used to store two pointers
because the data is larger than the reserved 16 bytes. Having to define
intermediate structures just to be able to cast is tedious. Add two
pointers that take the same space than ctx.

Signed-off-by: Robin Jarry <rjarry@redhat.com>
---

Notes:
    v3:
    
    * Added __extension__ to the unnamed struct inside the union.
    * Fixed C++ header checks.
    * Replaced alignas() with an explicit static_assert.
    
    v2:
    
    * Added __extension__ (not sure where it is needed, I don't have access to windows).
    * It still fails the header check for C++. It seems not possible to align an unnamed union...
      Tyler, do you have an idea about how to fix that?
    * Added static_assert to ensure the anonymous union is not larger than RTE_NODE_CTX_SZ.

 lib/graph/rte_graph_worker_common.h | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/lib/graph/rte_graph_worker_common.h b/lib/graph/rte_graph_worker_common.h
index 36d864e2c14e..722e9dac0d36 100644
--- a/lib/graph/rte_graph_worker_common.h
+++ b/lib/graph/rte_graph_worker_common.h
@@ -12,7 +12,9 @@
  * process, enqueue and move streams of objects to the next nodes.
  */
 
+#include <assert.h>
 #include <stdalign.h>
+#include <stddef.h>
 
 #include <rte_common.h>
 #include <rte_cycles.h>
@@ -112,7 +114,19 @@ struct __rte_cache_aligned rte_node {
 	};
 	/* Fast path area  */
 #define RTE_NODE_CTX_SZ 16
-	alignas(RTE_CACHE_LINE_SIZE) uint8_t ctx[RTE_NODE_CTX_SZ]; /**< Node Context. */
+	/*
+	 * alignas(RTE_CACHE_LINE_SIZE) cannot be used for ctx since it is part of an unnamed union.
+	 * The compiler shifts the next field on the next cache line which is not what we want.
+	 * The alignment is enforced via a explcicit static asserts below.
+	 */
+	union {
+		uint8_t ctx[RTE_NODE_CTX_SZ];
+		/* Convenience aliases to store pointers without complex casting. */
+		__extension__ struct {
+			void *ctx_ptr;
+			void *ctx_ptr2;
+		};
+	}; /**< Node Context. */
 	uint16_t size;		/**< Total number of objects available. */
 	uint16_t idx;		/**< Number of objects used. */
 	rte_graph_off_t off;	/**< Offset of node in the graph reel. */
@@ -130,6 +144,11 @@ struct __rte_cache_aligned rte_node {
 	alignas(RTE_CACHE_LINE_MIN_SIZE) struct rte_node *nodes[]; /**< Next nodes. */
 };
 
+static_assert(offsetof(struct rte_node, ctx) % RTE_CACHE_LINE_SIZE == 0,
+	"rte_node ctx must be aligned on a cache line");
+static_assert(offsetof(struct rte_node, size) - offsetof(struct rte_node, ctx) == RTE_NODE_CTX_SZ,
+	"rte_node context union cannot be larger than RTE_NODE_CTX_SZ");
+
 /**
  * @internal
  *
-- 
2.44.0


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

* Re: [PATCH v3] graph: expose node context as pointers
  2024-03-25 10:05 [PATCH v3] graph: expose node context as pointers Robin Jarry
@ 2024-03-25 10:59 ` Jerin Jacob
  2024-03-25 11:02   ` Robin Jarry
  2024-03-25 16:31 ` [PATCH v4] " Robin Jarry
  2024-03-27  9:14 ` [PATCH v5] " Robin Jarry
  2 siblings, 1 reply; 15+ messages in thread
From: Jerin Jacob @ 2024-03-25 10:59 UTC (permalink / raw)
  To: Robin Jarry
  Cc: dev, Jerin Jacob, Kiran Kumar K, Nithin Dabilpuram, Zhirun Yan,
	Tyler Retzlaff

On Mon, Mar 25, 2024 at 3:35 PM Robin Jarry <rjarry@redhat.com> wrote:
>
> In some cases, the node context data is used to store two pointers
> because the data is larger than the reserved 16 bytes. Having to define
> intermediate structures just to be able to cast is tedious. Add two
> pointers that take the same space than ctx.
>
> Signed-off-by: Robin Jarry <rjarry@redhat.com>
> ---
>
> Notes:
>     v3:
>
>     * Added __extension__ to the unnamed struct inside the union.
>     * Fixed C++ header checks.
>     * Replaced alignas() with an explicit static_assert.
>
>     v2:
>
>     * Added __extension__ (not sure where it is needed, I don't have access to windows).
>     * It still fails the header check for C++. It seems not possible to align an unnamed union...
>       Tyler, do you have an idea about how to fix that?
>     * Added static_assert to ensure the anonymous union is not larger than RTE_NODE_CTX_SZ.
>
>  lib/graph/rte_graph_worker_common.h | 21 ++++++++++++++++++++-
>  1 file changed, 20 insertions(+), 1 deletion(-)
>
> diff --git a/lib/graph/rte_graph_worker_common.h b/lib/graph/rte_graph_worker_common.h
> index 36d864e2c14e..722e9dac0d36 100644
> --- a/lib/graph/rte_graph_worker_common.h
> +++ b/lib/graph/rte_graph_worker_common.h
> @@ -12,7 +12,9 @@
>   * process, enqueue and move streams of objects to the next nodes.
>   */
>
> +#include <assert.h>
>  #include <stdalign.h>
> +#include <stddef.h>
>
>  #include <rte_common.h>
>  #include <rte_cycles.h>
> @@ -112,7 +114,19 @@ struct __rte_cache_aligned rte_node {
>         };
>         /* Fast path area  */
>  #define RTE_NODE_CTX_SZ 16
> -       alignas(RTE_CACHE_LINE_SIZE) uint8_t ctx[RTE_NODE_CTX_SZ]; /**< Node Context. */
> +       /*
> +        * alignas(RTE_CACHE_LINE_SIZE) cannot be used for ctx since it is part of an unnamed union.
> +        * The compiler shifts the next field on the next cache line which is not what we want.
> +        * The alignment is enforced via a explcicit static asserts below.
> +        */
> +       union {
> +               uint8_t ctx[RTE_NODE_CTX_SZ];
> +               /* Convenience aliases to store pointers without complex casting. */
> +               __extension__ struct {
> +                       void *ctx_ptr;
> +                       void *ctx_ptr2;
> +               };
> +       }; /**< Node Context. */
>         uint16_t size;          /**< Total number of objects available. */
>         uint16_t idx;           /**< Number of objects used. */
>         rte_graph_off_t off;    /**< Offset of node in the graph reel. */
> @@ -130,6 +144,11 @@ struct __rte_cache_aligned rte_node {
>         alignas(RTE_CACHE_LINE_MIN_SIZE) struct rte_node *nodes[]; /**< Next nodes. */
>  };
>
> +static_assert(offsetof(struct rte_node, ctx) % RTE_CACHE_LINE_SIZE == 0,
> +       "rte_node ctx must be aligned on a cache line");


This will fail in 32bit machine.
https://mails.dpdk.org/archives/test-report/2024-March/623806.html

I can think of following solution to add before ctx.
RTE_MARKER fastpath __rte_cache__aligned;


> +static_assert(offsetof(struct rte_node, size) - offsetof(struct rte_node, ctx) == RTE_NODE_CTX_SZ,
> +       "rte_node context union cannot be larger than RTE_NODE_CTX_SZ");
> +
>  /**
>   * @internal
>   *
> --
> 2.44.0
>

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

* Re: [PATCH v3] graph: expose node context as pointers
  2024-03-25 10:59 ` Jerin Jacob
@ 2024-03-25 11:02   ` Robin Jarry
  2024-03-25 11:08     ` Jerin Jacob
  0 siblings, 1 reply; 15+ messages in thread
From: Robin Jarry @ 2024-03-25 11:02 UTC (permalink / raw)
  To: Jerin Jacob
  Cc: dev, Jerin Jacob, Kiran Kumar K, Nithin Dabilpuram, Zhirun Yan,
	Tyler Retzlaff

Jerin Jacob, Mar 25, 2024 at 11:59:
> > +static_assert(offsetof(struct rte_node, ctx) % RTE_CACHE_LINE_SIZE == 0,
> > +       "rte_node ctx must be aligned on a cache line");
>
>
> This will fail in 32bit machine.
> https://mails.dpdk.org/archives/test-report/2024-March/623806.html

Hi Jerin, yes I saw that :(

> I can think of following solution to add before ctx.
> RTE_MARKER fastpath __rte_cache__aligned;

It will not be taken into account for MSVC. Is that OK?


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

* Re: [PATCH v3] graph: expose node context as pointers
  2024-03-25 11:02   ` Robin Jarry
@ 2024-03-25 11:08     ` Jerin Jacob
  2024-03-25 11:15       ` Robin Jarry
  0 siblings, 1 reply; 15+ messages in thread
From: Jerin Jacob @ 2024-03-25 11:08 UTC (permalink / raw)
  To: Robin Jarry
  Cc: dev, Jerin Jacob, Kiran Kumar K, Nithin Dabilpuram, Zhirun Yan,
	Tyler Retzlaff

On Mon, Mar 25, 2024 at 4:32 PM Robin Jarry <rjarry@redhat.com> wrote:
>
> Jerin Jacob, Mar 25, 2024 at 11:59:
> > > +static_assert(offsetof(struct rte_node, ctx) % RTE_CACHE_LINE_SIZE == 0,
> > > +       "rte_node ctx must be aligned on a cache line");
> >
> >
> > This will fail in 32bit machine.
> > https://mails.dpdk.org/archives/test-report/2024-March/623806.html
>
> Hi Jerin, yes I saw that :(
>
> > I can think of following solution to add before ctx.
> > RTE_MARKER fastpath __rte_cache__aligned;
>
> It will not be taken into account for MSVC. Is that OK?

Why?. rte_mbuf has a similar scheme.
RTE_MARKER cacheline1 __rte_cache_min_aligned;

>

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

* Re: [PATCH v3] graph: expose node context as pointers
  2024-03-25 11:08     ` Jerin Jacob
@ 2024-03-25 11:15       ` Robin Jarry
  2024-03-25 11:35         ` Jerin Jacob
  0 siblings, 1 reply; 15+ messages in thread
From: Robin Jarry @ 2024-03-25 11:15 UTC (permalink / raw)
  To: Jerin Jacob
  Cc: dev, Jerin Jacob, Kiran Kumar K, Nithin Dabilpuram, Zhirun Yan,
	Tyler Retzlaff

Jerin Jacob, Mar 25, 2024 at 12:08:
> > It will not be taken into account for MSVC. Is that OK?
>
> Why?. rte_mbuf has a similar scheme.
> RTE_MARKER cacheline1 __rte_cache_min_aligned;

RTE_MARKER* types seem not defined for the MSVC toolchain.

https://github.com/DPDK/dpdk/blob/v24.03-rc4/lib/eal/include/rte_common.h#L589-L602

Maybe I am missing something.


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

* Re: [PATCH v3] graph: expose node context as pointers
  2024-03-25 11:15       ` Robin Jarry
@ 2024-03-25 11:35         ` Jerin Jacob
  2024-03-25 12:07           ` Bruce Richardson
                             ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Jerin Jacob @ 2024-03-25 11:35 UTC (permalink / raw)
  To: Robin Jarry
  Cc: dev, Jerin Jacob, Kiran Kumar K, Nithin Dabilpuram, Zhirun Yan,
	Tyler Retzlaff

On Mon, Mar 25, 2024 at 4:45 PM Robin Jarry <rjarry@redhat.com> wrote:
>
> Jerin Jacob, Mar 25, 2024 at 12:08:
> > > It will not be taken into account for MSVC. Is that OK?
> >
> > Why?. rte_mbuf has a similar scheme.
> > RTE_MARKER cacheline1 __rte_cache_min_aligned;
>
> RTE_MARKER* types seem not defined for the MSVC toolchain.
>
> https://github.com/DPDK/dpdk/blob/v24.03-rc4/lib/eal/include/rte_common.h#L589-L602

Hmm. Not sure, how mbuf is building for MSCV tool chain then.

Another option could be to have a helper inline function/macro to take
care of casting to make app code clean of casting.

>
> Maybe I am missing something.

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

* Re: [PATCH v3] graph: expose node context as pointers
  2024-03-25 11:35         ` Jerin Jacob
@ 2024-03-25 12:07           ` Bruce Richardson
  2024-03-25 12:08           ` David Marchand
  2024-03-25 15:20           ` Robin Jarry
  2 siblings, 0 replies; 15+ messages in thread
From: Bruce Richardson @ 2024-03-25 12:07 UTC (permalink / raw)
  To: Jerin Jacob
  Cc: Robin Jarry, dev, Jerin Jacob, Kiran Kumar K, Nithin Dabilpuram,
	Zhirun Yan, Tyler Retzlaff

On Mon, Mar 25, 2024 at 05:05:12PM +0530, Jerin Jacob wrote:
> On Mon, Mar 25, 2024 at 4:45 PM Robin Jarry <rjarry@redhat.com> wrote:
> >
> > Jerin Jacob, Mar 25, 2024 at 12:08:
> > > > It will not be taken into account for MSVC. Is that OK?
> > >
> > > Why?. rte_mbuf has a similar scheme.
> > > RTE_MARKER cacheline1 __rte_cache_min_aligned;
> >
> > RTE_MARKER* types seem not defined for the MSVC toolchain.
> >
> > https://github.com/DPDK/dpdk/blob/v24.03-rc4/lib/eal/include/rte_common.h#L589-L602
> 
> Hmm. Not sure, how mbuf is building for MSCV tool chain then.
> 
> Another option could be to have a helper inline function/macro to take
> care of casting to make app code clean of casting.

The markers are being removed from DPDK and being replaced by more
portable, and more standards-conforming constructs. We should not be adding
more markers to existing structures. See [1]

/Bruce

[1] https://patches.dpdk.org/project/dpdk/list/?series=31579

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

* Re: [PATCH v3] graph: expose node context as pointers
  2024-03-25 11:35         ` Jerin Jacob
  2024-03-25 12:07           ` Bruce Richardson
@ 2024-03-25 12:08           ` David Marchand
  2024-03-25 15:20           ` Robin Jarry
  2 siblings, 0 replies; 15+ messages in thread
From: David Marchand @ 2024-03-25 12:08 UTC (permalink / raw)
  To: Jerin Jacob
  Cc: Robin Jarry, dev, Jerin Jacob, Kiran Kumar K, Nithin Dabilpuram,
	Zhirun Yan, Tyler Retzlaff

On Mon, Mar 25, 2024 at 12:35 PM Jerin Jacob <jerinjacobk@gmail.com> wrote:
>
> On Mon, Mar 25, 2024 at 4:45 PM Robin Jarry <rjarry@redhat.com> wrote:
> >
> > Jerin Jacob, Mar 25, 2024 at 12:08:
> > > > It will not be taken into account for MSVC. Is that OK?
> > >
> > > Why?. rte_mbuf has a similar scheme.
> > > RTE_MARKER cacheline1 __rte_cache_min_aligned;
> >
> > RTE_MARKER* types seem not defined for the MSVC toolchain.

There is some work in progress to stop using those markers.
https://patchwork.dpdk.org/project/dpdk/list/?series=31579&state=*


> >
> > https://github.com/DPDK/dpdk/blob/v24.03-rc4/lib/eal/include/rte_common.h#L589-L602
>
> Hmm. Not sure, how mbuf is building for MSCV tool chain then.

Atm, MSVC builds a really small list of libraries.

http://git.dpdk.org/dpdk/tree/lib/meson.build#n71?id=v24.03-rc4

if is_ms_compiler
    libraries = [
            'log',
            'kvargs',
            'telemetry',
            'eal',
            'ring',
    ]
endif


>
> Another option could be to have a helper inline function/macro to take
> care of casting to make app code clean of casting.

That could be an option.


-- 
David Marchand


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

* Re: [PATCH v3] graph: expose node context as pointers
  2024-03-25 11:35         ` Jerin Jacob
  2024-03-25 12:07           ` Bruce Richardson
  2024-03-25 12:08           ` David Marchand
@ 2024-03-25 15:20           ` Robin Jarry
  2024-03-25 15:47             ` Jerin Jacob
  2 siblings, 1 reply; 15+ messages in thread
From: Robin Jarry @ 2024-03-25 15:20 UTC (permalink / raw)
  To: Jerin Jacob
  Cc: dev, Jerin Jacob, Kiran Kumar K, Nithin Dabilpuram, Zhirun Yan,
	Tyler Retzlaff

Jerin Jacob, Mar 25, 2024 at 12:35:
> Another option could be to have a helper inline function/macro to take
> care of casting to make app code clean of casting.

Would something like this be suitable?

#define RTE_NODE_CTX_PTR1(n) ((void **)(n)->ctx)[0]
#define RTE_NODE_CTX_PTR2(n) ((void **)(n)->ctx)[1]


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

* Re: [PATCH v3] graph: expose node context as pointers
  2024-03-25 15:20           ` Robin Jarry
@ 2024-03-25 15:47             ` Jerin Jacob
  2024-03-25 15:51               ` Robin Jarry
  0 siblings, 1 reply; 15+ messages in thread
From: Jerin Jacob @ 2024-03-25 15:47 UTC (permalink / raw)
  To: Robin Jarry
  Cc: dev, Jerin Jacob, Kiran Kumar K, Nithin Dabilpuram, Zhirun Yan,
	Tyler Retzlaff

On Mon, Mar 25, 2024 at 8:50 PM Robin Jarry <rjarry@redhat.com> wrote:
>
> Jerin Jacob, Mar 25, 2024 at 12:35:
> > Another option could be to have a helper inline function/macro to take
> > care of casting to make app code clean of casting.
>
> Would something like this be suitable?
>
> #define RTE_NODE_CTX_PTR1(n) ((void **)(n)->ctx)[0]
> #define RTE_NODE_CTX_PTR2(n) ((void **)(n)->ctx)[1]

Works for me. No strong opinion about the name, RTE_NODE_CTX_AS_PTR1
may be more reflecting the intent.

>

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

* Re: [PATCH v3] graph: expose node context as pointers
  2024-03-25 15:47             ` Jerin Jacob
@ 2024-03-25 15:51               ` Robin Jarry
  2024-03-25 15:56                 ` Jerin Jacob
  0 siblings, 1 reply; 15+ messages in thread
From: Robin Jarry @ 2024-03-25 15:51 UTC (permalink / raw)
  To: Jerin Jacob
  Cc: dev, Jerin Jacob, Kiran Kumar K, Nithin Dabilpuram, Zhirun Yan,
	Tyler Retzlaff

Jerin Jacob, Mar 25, 2024 at 16:47:
> > #define RTE_NODE_CTX_PTR1(n) ((void **)(n)->ctx)[0]
> > #define RTE_NODE_CTX_PTR2(n) ((void **)(n)->ctx)[1]
>
> Works for me. No strong opinion about the name, RTE_NODE_CTX_AS_PTR1
> may be more reflecting the intent.

I also thought about adding inline getter/setter functions but that's 
more code. It may be cleaner:

 static inline void *rte_node_ctx_ptr1_get(struct rte_node *n) {
     return ((void **)node->ctx)[0];
 }
 static inline void *rte_node_ctx_ptr2_get(struct rte_node *n) {
     return ((void **)node->ctx)[1];
 }
 static inline void rte_node_ctx_ptr1_set(struct rte_node *n, void *p) {
     ((void **)node->ctx)[0] = p;
 }
 static inline void rte_node_ctx_ptr2_set(struct rte_node *n, void *p) {
     ((void **)node->ctx)[1] = p;
 }

I don't have a strong opinion. I'll go either way.


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

* Re: [PATCH v3] graph: expose node context as pointers
  2024-03-25 15:51               ` Robin Jarry
@ 2024-03-25 15:56                 ` Jerin Jacob
  0 siblings, 0 replies; 15+ messages in thread
From: Jerin Jacob @ 2024-03-25 15:56 UTC (permalink / raw)
  To: Robin Jarry
  Cc: dev, Jerin Jacob, Kiran Kumar K, Nithin Dabilpuram, Zhirun Yan,
	Tyler Retzlaff

On Mon, Mar 25, 2024 at 9:21 PM Robin Jarry <rjarry@redhat.com> wrote:
>
> Jerin Jacob, Mar 25, 2024 at 16:47:
> > > #define RTE_NODE_CTX_PTR1(n) ((void **)(n)->ctx)[0]
> > > #define RTE_NODE_CTX_PTR2(n) ((void **)(n)->ctx)[1]
> >
> > Works for me. No strong opinion about the name, RTE_NODE_CTX_AS_PTR1
> > may be more reflecting the intent.
>
> I also thought about adding inline getter/setter functions but that's
> more code. It may be cleaner:
>
>  static inline void *rte_node_ctx_ptr1_get(struct rte_node *n) {
>      return ((void **)node->ctx)[0];
>  }
>  static inline void *rte_node_ctx_ptr2_get(struct rte_node *n) {
>      return ((void **)node->ctx)[1];
>  }
>  static inline void rte_node_ctx_ptr1_set(struct rte_node *n, void *p) {
>      ((void **)node->ctx)[0] = p;
>  }
>  static inline void rte_node_ctx_ptr2_set(struct rte_node *n, void *p) {
>      ((void **)node->ctx)[1] = p;
>  }
>
> I don't have a strong opinion. I'll go either way.

Inline is better.

>

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

* [PATCH v4] graph: expose node context as pointers
  2024-03-25 10:05 [PATCH v3] graph: expose node context as pointers Robin Jarry
  2024-03-25 10:59 ` Jerin Jacob
@ 2024-03-25 16:31 ` Robin Jarry
  2024-03-25 16:50   ` Jerin Jacob
  2024-03-27  9:14 ` [PATCH v5] " Robin Jarry
  2 siblings, 1 reply; 15+ messages in thread
From: Robin Jarry @ 2024-03-25 16:31 UTC (permalink / raw)
  To: dev, Jerin Jacob, Kiran Kumar K, Nithin Dabilpuram, Zhirun Yan
  Cc: Tyler Retzlaff

In some cases, the node context data is used to store two pointers
because the data is larger than the reserved 16 bytes. Having to define
intermediate structures just to be able to cast is tedious. Add helper
inline functions to cast the node context to opaque pointers.

Signed-off-by: Robin Jarry <rjarry@redhat.com>
---

Notes:
    v4:
    
    * Replaced the unnamed union with helper inline functions.
    
    v3:
    
    * Added __extension__ to the unnamed struct inside the union.
    * Fixed C++ header checks.
    * Replaced alignas() with an explicit static_assert.

 lib/graph/rte_graph_worker_common.h | 54 +++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/lib/graph/rte_graph_worker_common.h b/lib/graph/rte_graph_worker_common.h
index 36d864e2c14e..f54f65598193 100644
--- a/lib/graph/rte_graph_worker_common.h
+++ b/lib/graph/rte_graph_worker_common.h
@@ -130,6 +130,60 @@ struct __rte_cache_aligned rte_node {
 	alignas(RTE_CACHE_LINE_MIN_SIZE) struct rte_node *nodes[]; /**< Next nodes. */
 };
 
+/**
+ * Cast the first 8 bytes of node context as an opaque pointer.
+ *
+ * @param node
+ *   Pointer to the node object.
+ *
+ * @return
+ *   The opaque pointer value.
+ */
+static inline void *rte_node_ctx_ptr1_get(struct rte_node *node)
+{
+	return ((void **)node->ctx)[0];
+}
+
+/**
+ * Cast the last 8 bytes of node context as an opaque pointer.
+ *
+ * @param node
+ *   Pointer to the node object.
+ *
+ * @return
+ *   The opaque pointer value.
+ */
+static inline void *rte_node_ctx_ptr2_get(struct rte_node *node)
+{
+	return ((void **)node->ctx)[1];
+}
+
+/**
+ * Set the first 8 bytes of node context to an opaque pointer value.
+ *
+ * @param node
+ *   Pointer to the node object.
+ * @param ptr
+ *   The opaque pointer value.
+ */
+static inline void rte_node_ctx_ptr1_set(struct rte_node *node, void *ptr)
+{
+	((void **)node->ctx)[0] = ptr;
+}
+
+/**
+ * Set the last 8 bytes of node context to an opaque pointer value.
+ *
+ * @param node
+ *   Pointer to the node object.
+ * @param ptr
+ *   The opaque pointer value.
+ */
+static inline void rte_node_ctx_ptr2_set(struct rte_node *node, void *ptr)
+{
+	((void **)node->ctx)[1] = ptr;
+}
+
 /**
  * @internal
  *
-- 
2.44.0


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

* Re: [PATCH v4] graph: expose node context as pointers
  2024-03-25 16:31 ` [PATCH v4] " Robin Jarry
@ 2024-03-25 16:50   ` Jerin Jacob
  0 siblings, 0 replies; 15+ messages in thread
From: Jerin Jacob @ 2024-03-25 16:50 UTC (permalink / raw)
  To: Robin Jarry
  Cc: dev, Jerin Jacob, Kiran Kumar K, Nithin Dabilpuram, Zhirun Yan,
	Tyler Retzlaff

On Mon, Mar 25, 2024 at 10:11 PM Robin Jarry <rjarry@redhat.com> wrote:
>
> In some cases, the node context data is used to store two pointers
> because the data is larger than the reserved 16 bytes. Having to define
> intermediate structures just to be able to cast is tedious. Add helper
> inline functions to cast the node context to opaque pointers.
>
> Signed-off-by: Robin Jarry <rjarry@redhat.com>
> ---
>
> Notes:
>     v4:
>
>     * Replaced the unnamed union with helper inline functions.
>
>     v3:
>
>     * Added __extension__ to the unnamed struct inside the union.
>     * Fixed C++ header checks.
>     * Replaced alignas() with an explicit static_assert.
>
>  lib/graph/rte_graph_worker_common.h | 54 +++++++++++++++++++++++++++++
>  1 file changed, 54 insertions(+)
>
> diff --git a/lib/graph/rte_graph_worker_common.h b/lib/graph/rte_graph_worker_common.h
> index 36d864e2c14e..f54f65598193 100644
> --- a/lib/graph/rte_graph_worker_common.h
> +++ b/lib/graph/rte_graph_worker_common.h
> @@ -130,6 +130,60 @@ struct __rte_cache_aligned rte_node {
>         alignas(RTE_CACHE_LINE_MIN_SIZE) struct rte_node *nodes[]; /**< Next nodes. */
>  };
>
> +/**
> + * Cast the first 8 bytes of node context as an opaque pointer.
> + *
> + * @param node
> + *   Pointer to the node object.
> + *
> + * @return
> + *   The opaque pointer value.
> + */
> +static inline void *rte_node_ctx_ptr1_get(struct rte_node *node)

New line after void*. Same for all new functions.

With that change:

Acked-by: Jerin Jacob <jerinj@marvell.com>



> +{
> +       return ((void **)node->ctx)[0];
> +}
> +
> +/**
> + * Cast the last 8 bytes of node context as an opaque pointer.
> + *
> + * @param node
> + *   Pointer to the node object.
> + *
> + * @return
> + *   The opaque pointer value.
> + */
> +static inline void *rte_node_ctx_ptr2_get(struct rte_node *node)
> +{
> +       return ((void **)node->ctx)[1];
> +}
> +
> +/**
> + * Set the first 8 bytes of node context to an opaque pointer value.
> + *
> + * @param node
> + *   Pointer to the node object.
> + * @param ptr
> + *   The opaque pointer value.
> + */
> +static inline void rte_node_ctx_ptr1_set(struct rte_node *node, void *ptr)
> +{
> +       ((void **)node->ctx)[0] = ptr;
> +}
> +
> +/**
> + * Set the last 8 bytes of node context to an opaque pointer value.
> + *
> + * @param node
> + *   Pointer to the node object.
> + * @param ptr
> + *   The opaque pointer value.
> + */
> +static inline void rte_node_ctx_ptr2_set(struct rte_node *node, void *ptr)
> +{
> +       ((void **)node->ctx)[1] = ptr;
> +}
> +
>  /**
>   * @internal
>   *
> --
> 2.44.0
>

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

* [PATCH v5] graph: expose node context as pointers
  2024-03-25 10:05 [PATCH v3] graph: expose node context as pointers Robin Jarry
  2024-03-25 10:59 ` Jerin Jacob
  2024-03-25 16:31 ` [PATCH v4] " Robin Jarry
@ 2024-03-27  9:14 ` Robin Jarry
  2 siblings, 0 replies; 15+ messages in thread
From: Robin Jarry @ 2024-03-27  9:14 UTC (permalink / raw)
  To: dev, Jerin Jacob, Kiran Kumar K, Nithin Dabilpuram, Zhirun Yan
  Cc: Tyler Retzlaff

In some cases, the node context data is used to store two pointers
because the data is larger than the reserved 16 bytes. Having to define
intermediate structures just to be able to cast is tedious. And without
intermediate structures, casting to opaque pointers is hard without
violating strict aliasing rules.

Add an unnamed union to allow storing opaque pointers in the node
context. Unfortunately, aligning an unnamed union that contains an array
produces inconsistent results between C and C++. To preserve ABI/API
compatibility in both C and C++, move all fast-path area fields into an
unnamed struct which is cache aligned. Use __rte_cache_min_aligned to
preserve existing alignment on architectures where cache lines are 128
bytes.

Add a static assert to ensure that the unnamed union is not larger than
the context array (RTE_NODE_CTX_SZ).

Signed-off-by: Robin Jarry <rjarry@redhat.com>
---

Notes:
    v5:
    
    * Helper functions to hide casting proved to be harder than expected.
      Naive casting may even be impossible without breaking strict aliasing
      rules. The only other option would be to use explicit memcpy calls.
    * Unnamed union tentative again. As suggested by Tyler (thank you!),
      using an intermediate unnamed struct to carry the alignment produces
      consistent ABI in C and C++.
    * Also, Tyler (thank you!) suggested that the fast path area alignment
      size may be incorrect for architectures where the cache line is not 64
      bytes. There will be a 64 bytes hole in the structure at the end of
      the unnamed struct before the zero length next nodes array. Use
      __rte_cache_min_aligned to preserve existing alignment.
    
    v4:
    
    * Replaced the unnamed union with helper inline functions.
    
    v3:
    
    * Added __extension__ to the unnamed struct inside the union.
    * Fixed C++ header checks.
    * Replaced alignas() with an explicit static_assert.

 lib/graph/rte_graph_worker_common.h | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/lib/graph/rte_graph_worker_common.h b/lib/graph/rte_graph_worker_common.h
index 36d864e2c14e..84d4997bbbf6 100644
--- a/lib/graph/rte_graph_worker_common.h
+++ b/lib/graph/rte_graph_worker_common.h
@@ -12,7 +12,9 @@
  * process, enqueue and move streams of objects to the next nodes.
  */
 
+#include <assert.h>
 #include <stdalign.h>
+#include <stddef.h>
 
 #include <rte_common.h>
 #include <rte_cycles.h>
@@ -111,14 +113,21 @@ struct __rte_cache_aligned rte_node {
 		} dispatch;
 	};
 	/* Fast path area  */
+	__extension__ struct __rte_cache_min_aligned {
 #define RTE_NODE_CTX_SZ 16
-	alignas(RTE_CACHE_LINE_SIZE) uint8_t ctx[RTE_NODE_CTX_SZ]; /**< Node Context. */
-	uint16_t size;		/**< Total number of objects available. */
-	uint16_t idx;		/**< Number of objects used. */
-	rte_graph_off_t off;	/**< Offset of node in the graph reel. */
-	uint64_t total_cycles;	/**< Cycles spent in this node. */
-	uint64_t total_calls;	/**< Calls done to this node. */
-	uint64_t total_objs;	/**< Objects processed by this node. */
+		union {
+			uint8_t ctx[RTE_NODE_CTX_SZ];
+			__extension__ struct {
+				void *ctx_ptr;
+				void *ctx_ptr2;
+			};
+		}; /**< Node Context. */
+		uint16_t size;		/**< Total number of objects available. */
+		uint16_t idx;		/**< Number of objects used. */
+		rte_graph_off_t off;	/**< Offset of node in the graph reel. */
+		uint64_t total_cycles;	/**< Cycles spent in this node. */
+		uint64_t total_calls;	/**< Calls done to this node. */
+		uint64_t total_objs;	/**< Objects processed by this node. */
 		union {
 			void **objs;	   /**< Array of object pointers. */
 			uint64_t objs_u64;
@@ -127,9 +136,13 @@ struct __rte_cache_aligned rte_node {
 			rte_node_process_t process; /**< Process function. */
 			uint64_t process_u64;
 		};
+	};
 	alignas(RTE_CACHE_LINE_MIN_SIZE) struct rte_node *nodes[]; /**< Next nodes. */
 };
 
+static_assert(offsetof(struct rte_node, size) - offsetof(struct rte_node, ctx) == RTE_NODE_CTX_SZ,
+	"rte_node context must be RTE_NODE_CTX_SZ bytes exactly");
+
 /**
  * @internal
  *
-- 
2.44.0


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

end of thread, other threads:[~2024-03-27  9:17 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-25 10:05 [PATCH v3] graph: expose node context as pointers Robin Jarry
2024-03-25 10:59 ` Jerin Jacob
2024-03-25 11:02   ` Robin Jarry
2024-03-25 11:08     ` Jerin Jacob
2024-03-25 11:15       ` Robin Jarry
2024-03-25 11:35         ` Jerin Jacob
2024-03-25 12:07           ` Bruce Richardson
2024-03-25 12:08           ` David Marchand
2024-03-25 15:20           ` Robin Jarry
2024-03-25 15:47             ` Jerin Jacob
2024-03-25 15:51               ` Robin Jarry
2024-03-25 15:56                 ` Jerin Jacob
2024-03-25 16:31 ` [PATCH v4] " Robin Jarry
2024-03-25 16:50   ` Jerin Jacob
2024-03-27  9:14 ` [PATCH v5] " Robin Jarry

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).