Skip to contentSkip to conversation

netvsc: optimize stats counters performance

PATCH Applied Morten Brørup 4 participants 7 messages Aug 2, 2024 – Sep 22, 2024 Network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netvsc: optimize stats counters performance

Optimized the performance of updating the statistics counters by reducing the number of branches.

Ordered the packet size comparisons according to the probability with typical internet traffic mix.

---
 drivers/net/netvsc/hn_rxtx.c | 32 ++++++++++----------------------
 1 file changed, 10 insertions(+), 22 deletions(-)
drivers/net/netvsc/hn_rxtx.c +10 -22
index 9bf1ec5509..b704b2c971 100644
@@ -110,30 +110,18 @@ hn_update_packet_stats(struct hn_stats *stats, const struct rte_mbuf *m)
 	uint32_t s = m->pkt_len;
 	const struct rte_ether_addr *ea;
 
-	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(m, const 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 hn_stats, broadcast) !=
+			offsetof(struct hn_stats, multicast) + sizeof(uint64_t));
+	if (unlikely(rte_is_multicast_ether_addr(ea)))
+		(&stats->multicast)[rte_is_broadcast_ether_addr(ea)]++;
 }
 
 static inline unsigned int hn_rndis_pktlen(const struct rndis_packet_msg *pkt)
Signature · 1 line
2.43.0
#1 lore raw
Quoted 34 lines

Subject: [PATCH] netvsc: optimize stats counters performance

Optimized the performance of updating the statistics counters by reducing the number of branches.

Ordered the packet size comparisons according to the probability with typical internet traffic mix.

---
 drivers/net/netvsc/hn_rxtx.c | 32 ++++++++++----------------------
 1 file changed, 10 insertions(+), 22 deletions(-)
drivers/net/netvsc/hn_rxtx.c index renamed from drivers/net/netvsc/hn_rxtx.c +0 -0

9bf1ec5509..b704b2c971 100644

drivers/net/netvsc/hn_rxtx.c +0 -12
@@ -110,30 +110,18 @@ hn_update_packet_stats(struct hn_stats *stats,
 const struct rte_mbuf *m)
 	uint32_t s = m->pkt_len;
 	const struct rte_ether_addr *ea;
 
-	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)

drivers/net/netvsc/hn_rxtx.c hn_update_packet_stats(struct hn_stats *stats, · line 119

Quoted 10 lines
drivers/net/netvsc/hn_rxtx.c +6 -4
-			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]++;

This part looks good.

drivers/net/netvsc/hn_rxtx.c hn_update_packet_stats(struct hn_stats *stats, const struct rte_mbuf *m) · line 125

Quoted 13 lines
drivers/net/netvsc/hn_rxtx.c +4 -6
 	ea = rte_pktmbuf_mtod(m, const 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 hn_stats, broadcast) !=
+			offsetof(struct hn_stats, multicast) + sizeof(uint64_t));
+	if (unlikely(rte_is_multicast_ether_addr(ea)))
+		(&stats->multicast)[rte_is_broadcast_ether_addr(ea)]++;
 }

This makes the code a little harder to read. How about just add "unlikely" to rte_is_multicast_ether_addr(ea) and keep the rest unchanged?

Thank you,

Long

#2 lore raw
Quoted 64 lines

From: Long Li [mailto:[email protected]]
Sent: Friday, 2 August 2024 18.49

Quoted 3 lines

Subject: [PATCH] netvsc: optimize stats counters performance

Optimized the performance of updating the statistics counters by

reducing the

Quoted 3 lines

number of branches.

Ordered the packet size comparisons according to the probability with

typical

Review

Quoted 8 lines

internet traffic mix.

---
 drivers/net/netvsc/hn_rxtx.c | 32 ++++++++++----------------------
 1 file changed, 10 insertions(+), 22 deletions(-)
(quoted diff) +0 -0

b/drivers/net/netvsc/hn_rxtx.c index

Quoted 20 lines

9bf1ec5509..b704b2c971 100644

drivers/net/netvsc/hn_rxtx.c +0 -12
@@ -110,30 +110,18 @@ hn_update_packet_stats(struct hn_stats *stats,
 const struct rte_mbuf *m)
 	uint32_t s = m->pkt_len;
 	const struct rte_ether_addr *ea;
 
-	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)

drivers/net/netvsc/hn_rxtx.c hn_update_packet_stats(struct hn_stats *stats, · line 119

Quoted 10 lines
drivers/net/netvsc/hn_rxtx.c +6 -4
-			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]++;

This part looks good.

drivers/net/netvsc/hn_rxtx.c hn_update_packet_stats(struct hn_stats *stats, const struct rte_mbuf *m)

Quoted 10 lines
drivers/net/netvsc/hn_rxtx.c +2 -6
 	ea = rte_pktmbuf_mtod(m, const 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 hn_stats, broadcast) !=
+			offsetof(struct hn_stats, multicast) +

sizeof(uint64_t));

Quoted 5 lines

drivers/net/netvsc/hn_rxtx.c hn_update_packet_stats(struct hn_stats *stats, const struct rte_mbuf *m) · line 125

Quoted 3 lines
drivers/net/netvsc/hn_rxtx.c +2 -0
+	if (unlikely(rte_is_multicast_ether_addr(ea)))
+		(&stats->multicast)[rte_is_broadcast_ether_addr(ea)]++;
 }

This makes the code a little harder to read.

I agree it is somewhat convoluted.
It's a tradeoff... I preferred performance at the cost of making the code somewhat harder to read. The RTE_BUILD_BUG_ON() also helps showing what is going on with the weird indexing.

Quoted 3 lines

How about just add
"unlikely" to rte_is_multicast_ether_addr(ea) and keep the rest unchanged?

Then I could also add "unlikely" to is_broadcast().
In theory, there should be minimal broadcast traffic in any normal network. (Except when experiencing broadcast storms due to network loops or other network problems.) But in reality, I think most real life networks see less multicast (non-broadcast) than broadcast traffic.

I don' think the following alternative makes the code significantly more readable than the method in this patch, but I'll mention it for the sake of discussion:

I could modify the hn_stats type like this:

struct hn_stats {
other fields...
+ union {

+		struct {
			uint64_t	multicast;
			uint64_t	broadcast;

+ };
+ uint64_t multicast_broadcast[2];
+ };
other fields...
};

So the code would become:

+ if (unlikely(rte_is_multicast_ether_addr(ea)))
+ stats->multicast_broadcast[rte_is_broadcast_ether_addr(ea)]++;

Whatever we decide, we should use the same method in all three patches (netvsc, virtio, vhost-user). The choices are:
1. Stick with the original code (with two branches), and add unlikely().
2. Use the method provided in this patch (with only one branch).
3. Introduce a multicast_broadcast[2] to the stats structure as described above (also with only one branch).

@Long, @Wei, @Maxime, @Chenbo, @Stephen and anyone else interested, please cast your votes. Comments are also welcome! :-)

I'm in favor of #2.

#3 lore raw
Quoted 16 lines · On Fri, 2 Aug 2024 19:28:26 +0200 Morten Brørup <[email protected]>…
Quoted 10 lines

drivers/net/netvsc/hn_rxtx.c hn_update_packet_stats(struct hn_stats *stats, const struct rte_mbuf *m)

Quoted 9 lines
drivers/net/netvsc/hn_rxtx.c +2 -6
 	ea = rte_pktmbuf_mtod(m, const 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 hn_stats, broadcast) !=
+			offsetof(struct hn_stats, multicast) +  

sizeof(uint64_t));

Quoted 5 lines

drivers/net/netvsc/hn_rxtx.c hn_update_packet_stats(struct hn_stats *stats, const struct rte_mbuf *m) · line 125

Quoted 3 lines
drivers/net/netvsc/hn_rxtx.c +2 -0
+	if (unlikely(rte_is_multicast_ether_addr(ea)))
+		(&stats->multicast)[rte_is_broadcast_ether_addr(ea)]++;
 }  

This makes the code a little harder to read.

Quoted 3 lines

I agree it is somewhat convoluted.
It's a tradeoff... I preferred performance at the cost of making the code somewhat harder to read. The RTE_BUILD_BUG_ON() also helps showing what is going on with the weird indexing.

Optimizing for multicast packets is not worth bothering. Keep the original code it is simpler.

#4 lore raw
Quoted 22 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Friday, 2 August 2024 19.33

Quoted 16 lines · On Fri, 2 Aug 2024 19:28:26 +0200 Morten Brørup <[email protected]>…
Quoted 15 lines

drivers/net/netvsc/hn_rxtx.c hn_update_packet_stats(struct hn_stats *stats, const struct rte_mbuf *m)

Quoted 9 lines
drivers/net/netvsc/hn_rxtx.c +2 -6
 	ea = rte_pktmbuf_mtod(m, const 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 hn_stats, broadcast) !=
+			offsetof(struct hn_stats, multicast) +

sizeof(uint64_t));

drivers/net/netvsc/hn_rxtx.c hn_update_packet_stats(struct hn_stats *stats, const struct rte_mbuf *m) · line 125

Quoted 3 lines
drivers/net/netvsc/hn_rxtx.c +2 -0
+	if (unlikely(rte_is_multicast_ether_addr(ea)))
+		(&stats->multicast)[rte_is_broadcast_ether_addr(ea)]++;
 }

This makes the code a little harder to read.

Quoted 5 lines
Quoted 2 lines

I agree it is somewhat convoluted.
It's a tradeoff... I preferred performance at the cost of making the code

somewhat harder to read.

Quoted 1 line

The RTE_BUILD_BUG_ON() also helps showing what is going on with the weird

indexing.

Similar patches have been accepted by other drivers:
[virtio]: https://patchwork.dpdk.org/project/dpdk/patch/[email protected]/
[vhost-user]: https://patchwork.dpdk.org/project/dpdk/patch/[email protected]/

Quoted 2 lines

Optimizing for multicast packets is not worth bothering.

Optimizing for multicast/broadcast comes into play in multicast environments, and during network broadcast storms. Although I don't know if any of those two scenarios are relevant for this specific driver.

Quoted 1 line

Keep the original code it is simpler.

Let's keep similar code similar across drivers.

@Long, @Wei, please Review/Ack, so the patch can be applied.

Reviewed-by
#5 lore raw
Quoted 60 lines

Subject: RE: [PATCH] netvsc: optimize stats counters performance

Quoted 19 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Friday, 2 August 2024 19.33

Quoted 10 lines · On Fri, 2 Aug 2024 19:28:26 +0200 Morten Brørup <[email protected]>…
Quoted 10 lines

drivers/net/netvsc/hn_rxtx.c hn_update_packet_stats(struct hn_stats *stats, const struct rte_mbuf *m)

Quoted 9 lines
drivers/net/netvsc/hn_rxtx.c +2 -6
 	ea = rte_pktmbuf_mtod(m, const 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 hn_stats, broadcast) !=
+			offsetof(struct hn_stats, multicast) +

sizeof(uint64_t));

Quoted 2 lines
Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (unlikely(rte_is_multicast_ether_addr(ea)))
+		(&stats-

multicast)[rte_is_broadcast_ether_addr(ea)]++;

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

}

This makes the code a little harder to read.

I agree it is somewhat convoluted.
It's a tradeoff... I preferred performance at the cost of making the code

somewhat harder to read.

Quoted 1 line

The RTE_BUILD_BUG_ON() also helps showing what is going on with the

weird

Quoted 1 line

indexing.

Similar patches have been accepted by other drivers:
[virtio]:
https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpatchwor
k.dpdk.org%2Fproject%2Fdpdk%2Fpatch%2F20240801160312.205281-1- mb%40smartsharesystems.com%2F&data=05%7C02%7Clongli%40microsoft.com %7C014c845ad8ec4ea25fb508dcd8b22474%7C72f988bf86f141af91ab2d7cd011 db47%7C1%7C0%7C638623506854135960%7CUnknown%7CTWFpbGZsb3d8eyJ WIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0 %7C%7C%7C&sdata=DrVP6TuTruREAxAoSEBED11TOapSqDN5stsDJJTuux0%3D&r eserved=0
[vhost-user]:
https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpatchwor
k.dpdk.org%2Fproject%2Fdpdk%2Fpatch%2F20240802143259.269827-1- mb%40smartsharesystems.com%2F&data=05%7C02%7Clongli%40microsoft.com %7C014c845ad8ec4ea25fb508dcd8b22474%7C72f988bf86f141af91ab2d7cd011 db47%7C1%7C0%7C638623506854150865%7CUnknown%7CTWFpbGZsb3d8eyJ WIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0 %7C%7C%7C&sdata=85T9qnC4nbAwU53sxnfQ2lMayp5soX8BMEJsbB58ZBw%3D &reserved=0

Quoted 2 lines

Optimizing for multicast packets is not worth bothering.

Optimizing for multicast/broadcast comes into play in multicast environments, and during network broadcast storms.
Although I don't know if any of those two scenarios are relevant for this specific driver.

Quoted 5 lines
Quoted 1 line

Keep the original code it is simpler.

Let's keep similar code similar across drivers.

@Long, @Wei, please Review/Ack, so the patch can be applied.