virtio: optimize stats counters performance
PATCH 1/1 virtio: optimize stats counters performance
Optimized the performance of updating the virtio statistics counters by reducing the number of branches and inlining the function.
Ordered the packet size comparisons according to the probability with typical internet traffic mix.
--- drivers/net/virtio/virtio_rxtx.c | 34 -------------------------------- drivers/net/virtio/virtio_rxtx.h | 26 ++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 36 deletions(-)
drivers/net/virtio/virtio_rxtx.c +0 -34
@@ -81,40 +81,6 @@ vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx) dp->next =VQ_RING_DESC_CHAIN_END ; } -void -virtio_update_packet_stats (struct virtnet_stats *stats,struct rte_mbuf *mbuf) -{ -uint32_t s = mbuf->pkt_len; -struct rte_ether_addr *ea; - - stats->bytes += s; - -if (s ==64 ) { - stats->size_bins[1 ]++; - }else if (s >64 && s <1024 ) { -uint32_t bin; - -/* count zeros, and offset into correct bin */ - bin = (sizeof (s) *8 ) -rte_clz32 (s) -5 ; - stats->size_bins[bin]++; - }else { -if (s <64 ) - stats->size_bins[0 ]++; -else if (s <1519 ) - stats->size_bins[6 ]++; -else - stats->size_bins[7 ]++; - } - - ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *); -if (rte_is_multicast_ether_addr (ea)) { -if (rte_is_broadcast_ether_addr (ea)) - stats->broadcast++; -else - stats->multicast++; - } -} -static inline void virtio_rx_stats_updated (struct virtnet_rx *rxvq,struct rte_mbuf *m) {
drivers/net/virtio/virtio_rxtx.h +24 -2
@@ -35,7 +35,29 @@ struct virtnet_tx { };int virtio_rxq_vec_setup (struct virtnet_rx *rxvq); -void virtio_update_packet_stats (struct virtnet_stats *stats, -struct rte_mbuf *mbuf); + +static inline void +virtio_update_packet_stats (struct virtnet_stats *const stats,const struct rte_mbuf *const mbuf) +{ +uint32_t s = mbuf->pkt_len; + const struct rte_ether_addr *const ea = rte_pktmbuf_mtod(mbuf, const struct rte_ether_addr *); + + stats->bytes += s; + +if (s >=1024 ) { + stats->size_bins[6 + (s >1518 )]++; + }else if (s <=64 ) { + stats->size_bins[s >>6 ]++; + }else { +/* count zeros, and offset into correct bin */ +uint32_t bin = (sizeof (s) *8 ) -rte_clz32 (s) -5 ; + stats->size_bins[bin]++; + } + +RTE_BUILD_BUG_ON (offsetof(struct virtnet_stats , broadcast) != + offsetof(struct virtnet_stats , multicast) +sizeof (uint64_t )); +if (rte_is_multicast_ether_addr (ea)) + (&stats->multicast)[rte_is_broadcast_ether_addr (ea)]++; +}#endif /* _VIRTIO_RXTX_H_ */
Signature · 1 line
2.43.0
PATCH v2 1/1 virtio: optimize stats counters performance
Optimized the performance of updating the virtio statistics counters by reducing the number of branches and inlining the function.
Ordered the packet size comparisons according to the probability with typical internet traffic mix.
---
v2:
* Fixed checkpatch warning about line length.
--- drivers/net/virtio/virtio_rxtx.c | 34 -------------------------------- drivers/net/virtio/virtio_rxtx.h | 26 ++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 36 deletions(-)
drivers/net/virtio/virtio_rxtx.c +0 -34
@@ -81,40 +81,6 @@ vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx) dp->next =VQ_RING_DESC_CHAIN_END ; } -void -virtio_update_packet_stats (struct virtnet_stats *stats,struct rte_mbuf *mbuf) -{ -uint32_t s = mbuf->pkt_len; -struct rte_ether_addr *ea; - - stats->bytes += s; - -if (s ==64 ) { - stats->size_bins[1 ]++; - }else if (s >64 && s <1024 ) { -uint32_t bin; - -/* count zeros, and offset into correct bin */ - bin = (sizeof (s) *8 ) -rte_clz32 (s) -5 ; - stats->size_bins[bin]++; - }else { -if (s <64 ) - stats->size_bins[0 ]++; -else if (s <1519 ) - stats->size_bins[6 ]++; -else - stats->size_bins[7 ]++; - } - - ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *); -if (rte_is_multicast_ether_addr (ea)) { -if (rte_is_broadcast_ether_addr (ea)) - stats->broadcast++; -else - stats->multicast++; - } -} -static inline void virtio_rx_stats_updated (struct virtnet_rx *rxvq,struct rte_mbuf *m) {
drivers/net/virtio/virtio_rxtx.h +24 -2
@@ -35,7 +35,29 @@ struct virtnet_tx { };int virtio_rxq_vec_setup (struct virtnet_rx *rxvq); -void virtio_update_packet_stats (struct virtnet_stats *stats, -struct rte_mbuf *mbuf); + +static inline void +virtio_update_packet_stats (struct virtnet_stats *const stats,const struct rte_mbuf *const mbuf) +{ +uint32_t s = mbuf->pkt_len; + const struct rte_ether_addr *ea = rte_pktmbuf_mtod(mbuf, const struct rte_ether_addr *); + + stats->bytes += s; + +if (s >=1024 ) { + stats->size_bins[6 + (s >1518 )]++; + }else if (s <=64 ) { + stats->size_bins[s >>6 ]++; + }else { +/* count zeros, and offset into correct bin */ +uint32_t bin = (sizeof (s) *8 ) -rte_clz32 (s) -5 ; + stats->size_bins[bin]++; + } + +RTE_BUILD_BUG_ON (offsetof(struct virtnet_stats , broadcast) != + offsetof(struct virtnet_stats , multicast) +sizeof (uint64_t )); +if (rte_is_multicast_ether_addr (ea)) + (&stats->multicast)[rte_is_broadcast_ether_addr (ea)]++; +}#endif /* _VIRTIO_RXTX_H_ */
Signature · 1 line
2.43.0
PATCH v3 1/1 virtio: optimize stats counters performance
In reply to Morten BrørupOptimized the performance of updating the virtio statistics counters by reducing the number of branches and inlining the function.
Ordered the packet size comparisons according to the probability with typical internet traffic mix.
---
v3:
* Eliminated a local variable.
* Note: Substituted sizeof(uint32_t)*4 by 32UL, using unsigned long type to keep optimal offsetting in generated assembler output.
* Removed unnecessary curly braces.
v2:
* Fixed checkpatch warning about line length.
--- drivers/net/virtio/virtio_rxtx.c | 34 -------------------------------- drivers/net/virtio/virtio_rxtx.h | 23 +++++++++++++++++++-- 2 files changed, 21 insertions(+), 36 deletions(-)
drivers/net/virtio/virtio_rxtx.c +0 -34
@@ -81,40 +81,6 @@ vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx) dp->next =VQ_RING_DESC_CHAIN_END ; } -void -virtio_update_packet_stats (struct virtnet_stats *stats,struct rte_mbuf *mbuf) -{ -uint32_t s = mbuf->pkt_len; -struct rte_ether_addr *ea; - - stats->bytes += s; - -if (s ==64 ) { - stats->size_bins[1 ]++; - }else if (s >64 && s <1024 ) { -uint32_t bin; - -/* count zeros, and offset into correct bin */ - bin = (sizeof (s) *8 ) -rte_clz32 (s) -5 ; - stats->size_bins[bin]++; - }else { -if (s <64 ) - stats->size_bins[0 ]++; -else if (s <1519 ) - stats->size_bins[6 ]++; -else - stats->size_bins[7 ]++; - } - - ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *); -if (rte_is_multicast_ether_addr (ea)) { -if (rte_is_broadcast_ether_addr (ea)) - stats->broadcast++; -else - stats->multicast++; - } -} -static inline void virtio_rx_stats_updated (struct virtnet_rx *rxvq,struct rte_mbuf *m) {
drivers/net/virtio/virtio_rxtx.h +21 -2
@@ -35,7 +35,26 @@ struct virtnet_tx { };int virtio_rxq_vec_setup (struct virtnet_rx *rxvq); -void virtio_update_packet_stats (struct virtnet_stats *stats, -struct rte_mbuf *mbuf); + +static inline void +virtio_update_packet_stats (struct virtnet_stats *const stats,const struct rte_mbuf *const mbuf) +{ +uint32_t s = mbuf->pkt_len; + const struct rte_ether_addr *ea = rte_pktmbuf_mtod(mbuf, const struct rte_ether_addr *); + + stats->bytes += s; + +if (s >=1024 ) + stats->size_bins[6 + (s >1518 )]++; +else if (s <=64 ) + stats->size_bins[s >>6 ]++; +else + stats->size_bins[32UL -rte_clz32 (s) -5 ]++; + +RTE_BUILD_BUG_ON (offsetof(struct virtnet_stats , broadcast) != + offsetof(struct virtnet_stats , multicast) +sizeof (uint64_t )); +if (rte_is_multicast_ether_addr (ea)) + (&stats->multicast)[rte_is_broadcast_ether_addr (ea)]++; +}#endif /* _VIRTIO_RXTX_H_ */
Signature · 1 line
2.43.0
PATCH v4 1/1 virtio: optimize stats counters performance
In reply to Morten BrørupOptimized the performance of updating the virtio statistics counters by reducing the number of branches and inlining the function.
Ordered the packet size comparisons according to the probability with typical internet traffic mix.
---
v4:
* Consider multicast/broadcast packets unlikely.
v3:
* Eliminated a local variable.
* Note: Substituted sizeof(uint32_t)*4 by 32UL, using unsigned long type to keep optimal offsetting in generated assembler output.
* Removed unnecessary curly braces.
v2:
* Fixed checkpatch warning about line length.
--- drivers/net/virtio/virtio_rxtx.c | 34 -------------------------------- drivers/net/virtio/virtio_rxtx.h | 23 +++++++++++++++++++-- 2 files changed, 21 insertions(+), 36 deletions(-)
drivers/net/virtio/virtio_rxtx.c +0 -34
@@ -81,40 +81,6 @@ vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx) dp->next =VQ_RING_DESC_CHAIN_END ; } -void -virtio_update_packet_stats (struct virtnet_stats *stats,struct rte_mbuf *mbuf) -{ -uint32_t s = mbuf->pkt_len; -struct rte_ether_addr *ea; - - stats->bytes += s; - -if (s ==64 ) { - stats->size_bins[1 ]++; - }else if (s >64 && s <1024 ) { -uint32_t bin; - -/* count zeros, and offset into correct bin */ - bin = (sizeof (s) *8 ) -rte_clz32 (s) -5 ; - stats->size_bins[bin]++; - }else { -if (s <64 ) - stats->size_bins[0 ]++; -else if (s <1519 ) - stats->size_bins[6 ]++; -else - stats->size_bins[7 ]++; - } - - ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *); -if (rte_is_multicast_ether_addr (ea)) { -if (rte_is_broadcast_ether_addr (ea)) - stats->broadcast++; -else - stats->multicast++; - } -} -static inline void virtio_rx_stats_updated (struct virtnet_rx *rxvq,struct rte_mbuf *m) {
drivers/net/virtio/virtio_rxtx.h +21 -2
@@ -35,7 +35,26 @@ struct virtnet_tx { };int virtio_rxq_vec_setup (struct virtnet_rx *rxvq); -void virtio_update_packet_stats (struct virtnet_stats *stats, -struct rte_mbuf *mbuf); + +static inline void +virtio_update_packet_stats (struct virtnet_stats *const stats,const struct rte_mbuf *const mbuf) +{ +uint32_t s = mbuf->pkt_len; + const struct rte_ether_addr *ea = rte_pktmbuf_mtod(mbuf, const struct rte_ether_addr *); + + stats->bytes += s; + +if (s >=1024 ) + stats->size_bins[6 + (s >1518 )]++; +else if (s <=64 ) + stats->size_bins[s >>6 ]++; +else + stats->size_bins[32UL -rte_clz32 (s) -5 ]++; + +RTE_BUILD_BUG_ON (offsetof(struct virtnet_stats , broadcast) != + offsetof(struct virtnet_stats , multicast) +sizeof (uint64_t )); +if (unlikely (rte_is_multicast_ether_addr (ea))) + (&stats->multicast)[rte_is_broadcast_ether_addr (ea)]++; +}#endif /* _VIRTIO_RXTX_H_ */
Signature · 1 line
2.43.0
Quoted 12 lines · On Wed, 31 Jul 2024 22:58:16 +0000 Morten Brørup <[email protected]>…
drivers/net/virtio/virtio_rxtx.h +11
-0+ +static inline void +virtio_update_packet_stats (struct virtnet_stats *const stats,const struct rte_mbuf *const mbuf) +{ +uint32_t s = mbuf->pkt_len; + const struct rte_ether_addr *ea = rte_pktmbuf_mtod(mbuf, const struct rte_ether_addr *); + + stats->bytes += s; + +if (s >=1024 ) + stats->size_bins[6 + (s >1518 )]++;
drivers/net/virtio/virtio_rxtx.h struct virtnet_tx { · line 58
Quoted 10 lines
drivers/net/virtio/virtio_rxtx.h +10
-0+else if (s <=64 ) + stats->size_bins[s >>6 ]++; +else + stats->size_bins[32UL -rte_clz32 (s) -5 ]++; + +RTE_BUILD_BUG_ON (offsetof(struct virtnet_stats , broadcast) != + offsetof(struct virtnet_stats , multicast) +sizeof (uint64_t )); +if (unlikely (rte_is_multicast_ether_addr (ea))) + (&stats->multicast)[rte_is_broadcast_ether_addr (ea)]++; +}
Why move it to virtio_rxtx.h it was fine where it was.
Quoted 27 lines
From: Stephen Hemminger [mailto:[email protected]]
Sent: Thursday, 1 August 2024 01.46drivers/net/virtio/virtio_rxtx.h struct virtnet_tx {
Quoted 3 lines · On Wed, 31 Jul 2024 22:58:16 +0000 Morten Brørup <[email protected]>…
drivers/net/virtio/virtio_rxtx.h +3
-0+ +static inline void +virtio_update_packet_stats(struct virtnet_stats *const stats, conststruct
rte_mbuf*const mbuf)drivers/net/virtio/virtio_rxtx.h struct virtnet_tx {
Quoted 3 lines
drivers/net/virtio/virtio_rxtx.h +3
-0+{ +uint32_t s = mbuf->pkt_len; + const struct rte_ether_addr *ea = rte_pktmbuf_mtod(mbuf, conststruct
rte_ether_addr*);drivers/net/virtio/virtio_rxtx.h struct virtnet_tx {
Quoted 12 lines
drivers/net/virtio/virtio_rxtx.h +12
-0+ + stats->bytes += s; + +if (s >=1024 ) + stats->size_bins[6 + (s >1518 )]++; +else if (s <=64 ) + stats->size_bins[s >>6 ]++; +else + stats->size_bins[32UL -rte_clz32 (s) -5 ]++; + +RTE_BUILD_BUG_ON (offsetof(struct virtnet_stats , broadcast) != + offsetof(struct virtnet_stats, multicast) +
sizeof(uint64_t));
Quoted 6 lines
drivers/net/virtio/virtio_rxtx.h struct virtnet_tx { · line 58
Quoted 4 lines
drivers/net/virtio/virtio_rxtx.h +3
-0+if (unlikely (rte_is_multicast_ether_addr (ea))) + (&stats->multicast)[rte_is_broadcast_ether_addr (ea)]++; +}Why move it to
virtio_rxtx.hit was fine where it was.
Because it is also called from the vector implementations [1], where it was not inlined before.
[1]: https://elixir.bootlin.com/dpdk/v24.07/A/ident/virtio_update_packet_stats
PATCH v5 1/1 virtio: optimize stats counters performance
In reply to Morten BrørupOptimized the performance of updating the virtio statistics counters by reducing the number of branches.
Ordered the packet size comparisons according to the probability with typical internet traffic mix.
---
v5:
* Do not inline the function. (Stephen)
v4:
* Consider multicast/broadcast packets unlikely.
v3:
* Eliminated a local variable.
* Note: Substituted sizeof(uint32_t)*4 by 32UL, using unsigned long type to keep optimal offsetting in generated assembler output.
* Removed unnecessary curly braces.
v2:
* Fixed checkpatch warning about line length.
--- drivers/net/virtio/virtio_rxtx.c | 39 ++++++++++++-------------------- drivers/net/virtio/virtio_rxtx.h | 4 ++-- 2 files changed, 16 insertions(+), 27 deletions(-)
drivers/net/virtio/virtio_rxtx.c +14 -25
@@ -82,37 +82,26 @@ vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx) }void -virtio_update_packet_stats (struct virtnet_stats *stats,struct rte_mbuf *mbuf) +virtio_update_packet_stats (struct virtnet_stats *const stats, +const struct rte_mbuf *const mbuf) {uint32_t s = mbuf->pkt_len; -struct rte_ether_addr *ea; +const struct rte_ether_addr *const ea = + rte_pktmbuf_mtod(mbuf, const struct rte_ether_addr *); stats->bytes += s; -if (s ==64 ) { - stats->size_bins[1 ]++; - }else if (s >64 && s <1024 ) { -uint32_t bin; - -/* count zeros, and offset into correct bin */ - bin = (sizeof (s) *8 ) -rte_clz32 (s) -5 ; - stats->size_bins[bin]++; - }else { -if (s <64 ) - stats->size_bins[0 ]++; -else if (s <1519 ) - stats->size_bins[6 ]++; -else - stats->size_bins[7 ]++; - } +if (s >=1024 ) + stats->size_bins[6 + (s >1518 )]++; +else if (s <=64 ) + stats->size_bins[s >>6 ]++; +else + stats->size_bins[32UL -rte_clz32 (s) -5 ]++; - ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *); -if (rte_is_multicast_ether_addr (ea)) { -if (rte_is_broadcast_ether_addr (ea)) - stats->broadcast++; -else - stats->multicast++; - } +RTE_BUILD_BUG_ON (offsetof(struct virtnet_stats , broadcast) != + offsetof(struct virtnet_stats , multicast) +sizeof (uint64_t )); +if (unlikely (rte_is_multicast_ether_addr (ea))) + (&stats->multicast)[rte_is_broadcast_ether_addr (ea)]++; } static inline void
drivers/net/virtio/virtio_rxtx.h +2 -2
@@ -35,7 +35,7 @@ struct virtnet_tx { };int virtio_rxq_vec_setup (struct virtnet_rx *rxvq); -void virtio_update_packet_stats (struct virtnet_stats *stats, -struct rte_mbuf *mbuf); +void virtio_update_packet_stats (struct virtnet_stats *const stats, +const struct rte_mbuf *const mbuf);#endif /* _VIRTIO_RXTX_H_ */
Signature · 1 line
2.43.0
Quoted 7 lines · On Thu, 1 Aug 2024 16:03:12 +0000 Morten Brørup <[email protected]>…
Optimized the performance of updating the virtio statistics counters by reducing the number of branches.
Ordered the packet size comparisons according to the probability with typical internet traffic mix.
Signed-off-by Morten Brørup <[email protected]>
LGTM
I wonder if other software drivers could use similar counters?
Quoted 13 lines
From: Stephen Hemminger [mailto:[email protected]]
Sent: Thursday, 1 August 2024 18.18Quoted 1 line · On Thu, 1 Aug 2024 16:03:12 +0000 Morten Brørup <[email protected]>…
Optimized the performance of updating the virtio statistics counters
by
Quoted 5 lines
reducing the number of branches.
Ordered the packet size comparisons according to the probability with typical internet traffic mix.
Quoted 6 lines
Quoted 1 line
Signed-off-by Morten Brørup <[email protected]>LGTM
I wonder if other software drivers could use similar counters?
While working on this, I noticed the netvsc driver and vhost lib also have size_bins [1], so they are likely candidates.
The netvsc's hn_update_packet_stats() function [2] seems like 100 % copy-paste, and should be easy to paste over with the new implementation. The vhost lib's vhost_queue_stats_update() function [3] also looks rather similar to the original variant, and could benefit too.
[1]: https://elixir.bootlin.com/dpdk/v24.07/A/ident/size_bins
[2]: https://elixir.bootlin.com/dpdk/v24.07/source/drivers/net/netvsc/hn_rxtx.c#L108
[3]: https://elixir.bootlin.com/dpdk/v24.07/source/lib/vhost/virtio_net.c#L56
I'll take a look around and add similar patches for what I find. Thanks for the reminder.
Quoted 40 lines
From: Morten Brørup [mailto:[email protected]]
Sent: Thursday, 1 August 2024 22.38Quoted 11 lines
From: Stephen Hemminger [mailto:[email protected]]
Sent: Thursday, 1 August 2024 18.18Quoted 1 line · On Thu, 1 Aug 2024 16:03:12 +0000 Morten Brørup <[email protected]>…
Optimized the performance of updating the virtio statistics counters
by
Quoted 3 lines
reducing the number of branches.
Ordered the packet size comparisons according to the probability
with
Quoted 8 lines
Quoted 3 lines
typical internet traffic mix.
Signed-off-by Morten Brørup <[email protected]>LGTM
I wonder if other software drivers could use similar counters?
While working on this, I noticed the netvsc driver and vhost lib also have
size_bins[1], so they are likely candidates.The netvsc's
hn_update_packet_stats()function [2] seems like 100 % copy-paste, and should be easy to paste over with the new implementation.
The vhost lib'svhost_queue_stats_update()function [3] also looks rather similar to the original variant, and could benefit too.[1]: https://elixir.bootlin.com/dpdk/v24.07/A/ident/size_bins
[2]:
https://elixir.bootlin.com/dpdk/v24.07/source/drivers/net/netvsc/hn_rxtx
.c#L108
[3]:
https://elixir.bootlin.com/dpdk/v24.07/source/lib/vhost/virtio_net.c#L56
Quoted 2 lines
I'll take a look around and add similar patches for what I find. Thanks for the reminder.
Separate patches for vhost-user [4] and netvsc [5] now provided.
[4]: https://inbox.dpdk.org/dev/[email protected]/T/#u
[5]: https://inbox.dpdk.org/dev/[email protected]/T/#u
Quoted 29 lines · Morten Brørup
Optimized the performance of updating the virtio statistics counters by reducing the number of branches.
Ordered the packet size comparisons according to the probability with typical internet traffic mix.
Signed-off-by Morten Brørup <[email protected]>---
v5:
* Do not inline the function. (Stephen)
v4:
* Consider multicast/broadcast packets unlikely.
v3:
* Eliminated a local variable.
* Note: Substituted sizeof(uint32_t)*4 by 32UL, using unsigned long type to keep optimal offsetting in generated assembler output.
* Removed unnecessary curly braces.
v2:
* Fixed checkpatch warning about line length.--- drivers/net/virtio/virtio_rxtx.c | 39 ++++++++++++-------------------- drivers/net/virtio/virtio_rxtx.h | 4 ++-- 2 files changed, 16 insertions(+), 27 deletions(-)drivers/net/virtio/virtio_rxtx.c +0
-0
drivers/net/virtio/virtio_rxtx.c vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx) · line 86
Quoted 6 lines
drivers/net/virtio/virtio_rxtx.c +2
-1@@ -82,37 +82,26 @@ vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx) }void -virtio_update_packet_stats (struct virtnet_stats *stats,struct rte_mbuf *mbuf) +virtio_update_packet_stats (struct virtnet_stats *const stats, +const struct rte_mbuf *const mbuf)
The two const is also for performace? Is there gain?
Quoted 32 lines
drivers/net/virtio/virtio_rxtx.c +8
-18{uint32_t s = mbuf->pkt_len; -struct rte_ether_addr *ea; +const struct rte_ether_addr *const ea = + rte_pktmbuf_mtod(mbuf, const struct rte_ether_addr *); stats->bytes += s; -if (s ==64 ) { - stats->size_bins[1 ]++; - }else if (s >64 && s <1024 ) { -uint32_t bin; - -/* count zeros, and offset into correct bin */ - bin = (sizeof (s) *8 ) -rte_clz32 (s) -5 ; - stats->size_bins[bin]++; - }else { -if (s <64 ) - stats->size_bins[0 ]++; -else if (s <1519 ) - stats->size_bins[6 ]++; -else - stats->size_bins[7 ]++; - } +if (s >=1024 ) + stats->size_bins[6 + (s >1518 )]++; +else if (s <=64 ) + stats->size_bins[s >>6 ]++; +else + stats->size_bins[32UL -rte_clz32 (s) -5 ]++; - ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *);
drivers/net/virtio/virtio_rxtx.c vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx) · line 104
Quoted 10 lines
drivers/net/virtio/virtio_rxtx.c +4
-6-if (rte_is_multicast_ether_addr (ea)) { -if (rte_is_broadcast_ether_addr (ea)) - stats->broadcast++; -else - stats->multicast++; - } +RTE_BUILD_BUG_ON (offsetof(struct virtnet_stats , broadcast) != + offsetof(struct virtnet_stats , multicast) +sizeof (uint64_t )); +if (unlikely (rte_is_multicast_ether_addr (ea))) + (&stats->multicast)[rte_is_broadcast_ether_addr (ea)]++;
The "rte_is_broadcast_ether_addr(ea) " will be calculated twice if packet is mulitcast. How about coding like:
-->is_mulitcast = rte_is_multicast_ether_addr(ea);
if (unlikely(is_mulitcast)) (&stats->multicast)[rte_is_broadcast_ether_addr(ea)]++;
Quoted 17 lines
drivers/net/virtio/virtio_rxtx.c +0
-0} static inline voiddrivers/net/virtio/virtio_rxtx.h +2
-2@@ -35,7 +35,7 @@ struct virtnet_tx { };int virtio_rxq_vec_setup (struct virtnet_rx *rxvq); -void virtio_update_packet_stats (struct virtnet_stats *stats, -struct rte_mbuf *mbuf); +void virtio_update_packet_stats (struct virtnet_stats *const stats, +const struct rte_mbuf *const mbuf);#endif /* _VIRTIO_RXTX_H_ */
Quoted 5 lines · On Fri, 2 Aug 2024 10:23:12 +0800 "lihuisong (C)" <[email protected]> wrote:
drivers/net/virtio/virtio_rxtx.c vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx) · line 86
Quoted 4 lines
void
drivers/net/virtio/virtio_rxtx.c +2
-1-virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf) +virtio_update_packet_stats(struct virtnet_stats *const stats, + const struct rte_mbuf *const mbuf)The two const is also for performace? Is there gain?
If you look at resulting code (ie godbolt.org) the resulting code never changes when const is added. The compiler already
knows what is modified. Const is only a programmer and correctness thing.
Quoted 11 lines · Stephen Hemminger
Quoted 5 lines · On Fri, 2 Aug 2024 10:23:12 +0800 "lihuisong (C)" <[email protected]> wrote:
drivers/net/virtio/virtio_rxtx.c vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx) · line 86
Quoted 4 lines
void
drivers/net/virtio/virtio_rxtx.c +2
-1-virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf) +virtio_update_packet_stats(struct virtnet_stats *const stats, + const struct rte_mbuf *const mbuf)The two const is also for performace? Is there gain?
If you look at resulting code (ie godbolt.org) the resulting code never changes when const is added. The compiler already
knows what is modified. Const is only a programmer and correctness thing.
I know this. Thanks for your clarifying and recommending the site that is good to use.😁 This patch is just for optimizing stats counters performance.
And the new added const has nothing to do with this optimization.
But it's ok for me.
Quoted 1 line
.
Quoted 10 lines
From: lihuisong (C) [mailto:[email protected]]
Sent: Friday, 2 August 2024 04.23Quoted 2 lines · Morten Brørup
void
-virtio_update_packet_stats(structvirtnet_stats*stats, struct
rte_mbuf*mbuf)drivers/net/virtio/virtio_rxtx.c vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx) · line 86
Quoted 2 lines
drivers/net/virtio/virtio_rxtx.c +2
-0+virtio_update_packet_stats(struct virtnet_stats *const stats, + const struct rte_mbuf *const mbuf)The two const is also for performace? Is there gain?
The "const struct rte_mbuf * mbuf" informs the optimizer that the function does not modify the mbuf. This is for the benefit of callers of the function; they can rely on the mbuf being unchanged when the function returns. So, if the optimizer has cached some mbuf field before calling the function, it does not need to read the mbuf field again, but can continue using the cached value after the function call.
The two "type *const ptr" probably make no performance difference with the compilers and function call conventions used by the CPU architectures supported by DPDK.
Quoted 12 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ RTE_BUILD_BUG_ON(offsetof(struct virtnet_stats, broadcast) != + offsetof(struct virtnet_stats, multicast) +
sizeof(uint64_t));drivers/net/virtio/virtio_rxtx.c vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx) · line 104
Quoted 2 lines
drivers/net/virtio/virtio_rxtx.c +2
-0+if (unlikely (rte_is_multicast_ether_addr (ea))) + (&stats->multicast)[rte_is_broadcast_ether_addr (ea)]++;The "
rte_is_broadcast_ether_addr(ea)" will be calculated twice if packet is mulitcast.
How about coding like:
-->is_mulitcast=rte_is_multicast_ether_addr(ea);
if (unlikely(is_mulitcast)) (&stats->multicast)[rte_is_broadcast_ether_addr(ea)]++;
I don't think "rte_is_broadcast_ether_addr(ea)" is calculated twice for multicast packets. My code essentially does this:
if (mc(ea)) stats[bc(ea)]++;
Changing to this shouldn't make a difference:
m = mc(ea); if (m) stats[bc(ea)]++;
Quoted 6 lines · Morten Brørup
Quoted 6 lines
From: lihuisong (C) [mailto:[email protected]]
Sent: Friday, 2 August 2024 04.23Quoted 2 lines · Morten Brørup
void
-virtio_update_packet_stats(structvirtnet_stats*stats, struct
Quoted 8 lines
Quoted 4 lines
rte_mbuf*mbuf)drivers/net/virtio/virtio_rxtx.c vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx) · line 86
Quoted 2 lines
drivers/net/virtio/virtio_rxtx.c +2
-0+virtio_update_packet_stats(struct virtnet_stats *const stats, + const struct rte_mbuf *const mbuf)The two const is also for performace? Is there gain?
The "const struct
rte_mbuf* mbuf" informs the optimizer that the function does not modify the mbuf. This is for the benefit of callers of the function; they can rely on the mbuf being unchanged when the function returns. So, if the optimizer has cached some mbuf field before calling the function, it does not need to read the mbuf field again, but can continue using the cached value after the function call.The two "type *const ptr" probably make no performance difference with the compilers and function call conventions used by the CPU architectures supported by DPDK.
ok
Quoted 15 lines
Quoted 4 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ RTE_BUILD_BUG_ON(offsetof(struct virtnet_stats, broadcast) != + offsetof(struct virtnet_stats, multicast) +
sizeof(uint64_t));Quoted 9 lines
drivers/net/virtio/virtio_rxtx.c vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx) · line 104
Quoted 2 lines
drivers/net/virtio/virtio_rxtx.c +2
-0+if (unlikely (rte_is_multicast_ether_addr (ea))) + (&stats->multicast)[rte_is_broadcast_ether_addr (ea)]++;The "
rte_is_broadcast_ether_addr(ea)" will be calculated twice if packet is mulitcast.
How about coding like:
-->is_mulitcast=rte_is_multicast_ether_addr(ea);
if (unlikely(is_mulitcast)) (&stats->multicast)[rte_is_broadcast_ether_addr(ea)]++;I don't think "
rte_is_broadcast_ether_addr(ea)" is calculated twice for multicast packets. My code essentially does this:
Quoted 7 lines
if (mc(ea)) stats[bc(ea)]++;Changing to this shouldn't make a difference:
m = mc(ea); if (m) stats[bc(ea)]++;
Yeah,you are right.
Quoted 1 line
LGTM,
Quoted 94 lines · Morten Brørup
Optimized the performance of updating the virtio statistics counters by reducing the number of branches.
Ordered the packet size comparisons according to the probability with typical internet traffic mix.
Signed-off-by Morten Brørup <[email protected]>---
v5:
* Do not inline the function. (Stephen)
v4:
* Consider multicast/broadcast packets unlikely.
v3:
* Eliminated a local variable.
* Note: Substituted sizeof(uint32_t)*4 by 32UL, using unsigned long type to keep optimal offsetting in generated assembler output.
* Removed unnecessary curly braces.
v2:
* Fixed checkpatch warning about line length.--- drivers/net/virtio/virtio_rxtx.c | 39 ++++++++++++-------------------- drivers/net/virtio/virtio_rxtx.h | 4 ++-- 2 files changed, 16 insertions(+), 27 deletions(-)drivers/net/virtio/virtio_rxtx.c +14
-25@@ -82,37 +82,26 @@ vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx) }void -virtio_update_packet_stats (struct virtnet_stats *stats,struct rte_mbuf *mbuf) +virtio_update_packet_stats (struct virtnet_stats *const stats, +const struct rte_mbuf *const mbuf) {uint32_t s = mbuf->pkt_len; -struct rte_ether_addr *ea; +const struct rte_ether_addr *const ea = + rte_pktmbuf_mtod(mbuf, const struct rte_ether_addr *); stats->bytes += s; -if (s ==64 ) { - stats->size_bins[1 ]++; - }else if (s >64 && s <1024 ) { -uint32_t bin; - -/* count zeros, and offset into correct bin */ - bin = (sizeof (s) *8 ) -rte_clz32 (s) -5 ; - stats->size_bins[bin]++; - }else { -if (s <64 ) - stats->size_bins[0 ]++; -else if (s <1519 ) - stats->size_bins[6 ]++; -else - stats->size_bins[7 ]++; - } +if (s >=1024 ) + stats->size_bins[6 + (s >1518 )]++; +else if (s <=64 ) + stats->size_bins[s >>6 ]++; +else + stats->size_bins[32UL -rte_clz32 (s) -5 ]++; - ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *); -if (rte_is_multicast_ether_addr (ea)) { -if (rte_is_broadcast_ether_addr (ea)) - stats->broadcast++; -else - stats->multicast++; - } +RTE_BUILD_BUG_ON (offsetof(struct virtnet_stats , broadcast) != + offsetof(struct virtnet_stats , multicast) +sizeof (uint64_t )); +if (unlikely (rte_is_multicast_ether_addr (ea))) + (&stats->multicast)[rte_is_broadcast_ether_addr (ea)]++; } static inline voiddrivers/net/virtio/virtio_rxtx.h +2
-2@@ -35,7 +35,7 @@ struct virtnet_tx { };int virtio_rxq_vec_setup (struct virtnet_rx *rxvq); -void virtio_update_packet_stats (struct virtnet_stats *stats, -struct rte_mbuf *mbuf); +void virtio_update_packet_stats (struct virtnet_stats *const stats, +const struct rte_mbuf *const mbuf);#endif /* _VIRTIO_RXTX_H_ */
Quoted 101 lines · Morten Brørup
External email: Use caution opening links or attachments
Optimized the performance of updating the virtio statistics counters by reducing the number of branches.
Ordered the packet size comparisons according to the probability with typical internet traffic mix.
Signed-off-by Morten Brørup <[email protected]>---
v5:
* Do not inline the function. (Stephen)
v4:
* Consider multicast/broadcast packets unlikely.
v3:
* Eliminated a local variable.
* Note: Substituted sizeof(uint32_t)*4 by 32UL, using unsigned long type to keep optimal offsetting in generated assembler output.
* Removed unnecessary curly braces.
v2:
* Fixed checkpatch warning about line length.
---drivers/net/virtio/virtio_rxtx.c| 39 ++++++++++++--------------------drivers/net/virtio/virtio_rxtx.h| 4 ++--2 files changed, 16 insertions(+), 27 deletions(-)drivers/net/virtio/virtio_rxtx.c +0
-0@@ -82,37 +82,26 @@ vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx) }void
drivers/net/virtio/virtio_rxtx.c +2
-1-virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf) +virtio_update_packet_stats(struct virtnet_stats *const stats, + const struct rte_mbuf *const mbuf){ uint32_t s = mbuf->pkt_len;drivers/net/virtio/virtio_rxtx.c +12
-24-struct rte_ether_addr *ea; +const struct rte_ether_addr *const ea = + rte_pktmbuf_mtod(mbuf, const struct rte_ether_addr *); stats->bytes += s; -if (s ==64 ) { - stats->size_bins[1 ]++; - }else if (s >64 && s <1024 ) { -uint32_t bin; - -/* count zeros, and offset into correct bin */ - bin = (sizeof (s) *8 ) -rte_clz32 (s) -5 ; - stats->size_bins[bin]++; - }else { -if (s <64 ) - stats->size_bins[0 ]++; -else if (s <1519 ) - stats->size_bins[6 ]++; -else - stats->size_bins[7 ]++; - } +if (s >=1024 ) + stats->size_bins[6 + (s >1518 )]++; +else if (s <=64 ) + stats->size_bins[s >>6 ]++; +else + stats->size_bins[32UL -rte_clz32 (s) -5 ]++; - ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *); -if (rte_is_multicast_ether_addr (ea)) { -if (rte_is_broadcast_ether_addr (ea)) - stats->broadcast++; -else - stats->multicast++; - } +RTE_BUILD_BUG_ON (offsetof(struct virtnet_stats , broadcast) != + offsetof(struct virtnet_stats , multicast) +sizeof (uint64_t )); +if (unlikely (rte_is_multicast_ether_addr (ea))) + (&stats->multicast)[rte_is_broadcast_ether_addr (ea)]++;}
static inline void
drivers/net/virtio/virtio_rxtx.h +2
-2@@ -35,7 +35,7 @@ struct virtnet_tx { };int virtio_rxq_vec_setup (struct virtnet_rx *rxvq); -void virtio_update_packet_stats (struct virtnet_stats *stats, -struct rte_mbuf *mbuf); +void virtio_update_packet_stats (struct virtnet_stats *const stats, +const struct rte_mbuf *const mbuf);#endif /* _VIRTIO_RXTX_H_ */
Quoted 3 lines
—
2.43.0
Quoted 19 lines · Morten Brørup
Optimized the performance of updating the virtio statistics counters by reducing the number of branches.
Ordered the packet size comparisons according to the probability with typical internet traffic mix.
Signed-off-by Morten Brørup <[email protected]>---
v5:
* Do not inline the function. (Stephen)
v4:
* Consider multicast/broadcast packets unlikely.
v3:
* Eliminated a local variable.
* Note: Substituted sizeof(uint32_t)*4 by 32UL, using unsigned long type to keep optimal offsetting in generated assembler output.
* Removed unnecessary curly braces.
v2:
* Fixed checkpatch warning about line length.
Quoted 5 lines
--- drivers/net/virtio/virtio_rxtx.c | 39 ++++++++++++-------------------- drivers/net/virtio/virtio_rxtx.h | 4 ++-- 2 files changed, 16 insertions(+), 27 deletions(-)
Thanks,
Maxime
Maxime Coquelin
AppliedQuoted 19 lines · Morten Brørup
Optimized the performance of updating the virtio statistics counters by reducing the number of branches.
Ordered the packet size comparisons according to the probability with typical internet traffic mix.
Signed-off-by Morten Brørup <[email protected]>---
v5:
* Do not inline the function. (Stephen)
v4:
* Consider multicast/broadcast packets unlikely.
v3:
* Eliminated a local variable.
* Note: Substituted sizeof(uint32_t)*4 by 32UL, using unsigned long type to keep optimal offsetting in generated assembler output.
* Removed unnecessary curly braces.
v2:
* Fixed checkpatch warning about line length.
Quoted 5 lines
--- drivers/net/virtio/virtio_rxtx.c | 39 ++++++++++++-------------------- drivers/net/virtio/virtio_rxtx.h | 4 ++-- 2 files changed, 16 insertions(+), 27 deletions(-)
Applied to next-virtio/for-next-net.
Thanks,
Maxime