[v4] virtio: optimize stats counters performance

Message ID 20240731225816.39567-1-mb@smartsharesystems.com (mailing list archive)
State Superseded, archived
Delegated to: Maxime Coquelin
Headers
Series [v4] virtio: optimize stats counters performance |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/intel-Functional success Functional PASS
ci/github-robot: build success github build: passed
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-marvell-Functional success Functional Testing PASS
ci/iol-abi-testing success Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-broadcom-Functional success Functional Testing PASS
ci/iol-unit-arm64-testing pending Testing pending
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-compile-amd64-testing success Testing PASS
ci/iol-compile-arm64-testing pending Testing pending
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-sample-apps-testing success Testing PASS

Commit Message

Morten Brørup July 31, 2024, 10:58 p.m. UTC
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.

Signed-off-by: Morten Brørup <mb@smartsharesystems.com>
---
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(-)
  

Comments

Stephen Hemminger July 31, 2024, 11:45 p.m. UTC | #1
On Wed, 31 Jul 2024 22:58:16 +0000
Morten Brørup <mb@smartsharesystems.com> wrote:

> +
> +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)]++;
> +}
>  

Why move it to virtio_rxtx.h it was fine where it was.
  
Morten Brørup July 31, 2024, 11:51 p.m. UTC | #2
> From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> Sent: Thursday, 1 August 2024 01.46
> 
> On Wed, 31 Jul 2024 22:58:16 +0000
> Morten Brørup <mb@smartsharesystems.com> wrote:
> 
> > +
> > +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)]++;
> > +}
> >
> 
> Why move it to virtio_rxtx.h it 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

diff --git a/drivers/net/virtio/virtio_rxtx.c b/drivers/net/virtio/virtio_rxtx.c
index f69b9453a2..bb04fd7d43 100644
--- a/drivers/net/virtio/virtio_rxtx.c
+++ b/drivers/net/virtio/virtio_rxtx.c
@@ -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)
 {
diff --git a/drivers/net/virtio/virtio_rxtx.h b/drivers/net/virtio/virtio_rxtx.h
index afc4b74534..0f938ab145 100644
--- a/drivers/net/virtio/virtio_rxtx.h
+++ b/drivers/net/virtio/virtio_rxtx.h
@@ -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_ */