From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 8F7774571C; Fri, 2 Aug 2024 16:33:02 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 8324342F9B; Fri, 2 Aug 2024 16:33:02 +0200 (CEST) Received: from dkmailrelay1.smartsharesystems.com (smartserver.smartsharesystems.com [77.243.40.215]) by mails.dpdk.org (Postfix) with ESMTP id 6961240E20 for ; Fri, 2 Aug 2024 16:33:01 +0200 (CEST) Received: from smartserver.smartsharesystems.com (smartserver.smartsharesys.local [192.168.4.10]) by dkmailrelay1.smartsharesystems.com (Postfix) with ESMTP id 44F0F2099B; Fri, 2 Aug 2024 16:33:01 +0200 (CEST) Received: from dkrd4.smartsharesys.local ([192.168.4.26]) by smartserver.smartsharesystems.com with Microsoft SMTPSVC(6.0.3790.4675); Fri, 2 Aug 2024 16:33:00 +0200 From: =?UTF-8?q?Morten=20Br=C3=B8rup?= To: maxime.coquelin@redhat.com, chenbox@nvidia.com Cc: dev@dpdk.org, =?UTF-8?q?Morten=20Br=C3=B8rup?= Subject: [PATCH] vhost-user: optimize stats counters performance Date: Fri, 2 Aug 2024 14:32:59 +0000 Message-ID: <20240802143259.269827-1-mb@smartsharesystems.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-OriginalArrivalTime: 02 Aug 2024 14:33:00.0582 (UTC) FILETIME=[E032C460:01DAE4E8] X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org 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. Signed-off-by: Morten Brørup --- lib/vhost/virtio_net.c | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/lib/vhost/virtio_net.c b/lib/vhost/virtio_net.c index 370402d849..25a495df56 100644 --- a/lib/vhost/virtio_net.c +++ b/lib/vhost/virtio_net.c @@ -53,7 +53,7 @@ is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring) } static inline void -vhost_queue_stats_update(struct virtio_net *dev, struct vhost_virtqueue *vq, +vhost_queue_stats_update(const struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mbuf **pkts, uint16_t count) __rte_shared_locks_required(&vq->access_lock) { @@ -64,37 +64,25 @@ vhost_queue_stats_update(struct virtio_net *dev, struct vhost_virtqueue *vq, return; for (i = 0; i < count; i++) { - struct rte_ether_addr *ea; - struct rte_mbuf *pkt = pkts[i]; + const struct rte_ether_addr *ea; + const struct rte_mbuf *pkt = pkts[i]; uint32_t pkt_len = rte_pktmbuf_pkt_len(pkt); stats->packets++; stats->bytes += pkt_len; - if (pkt_len == 64) { - stats->size_bins[1]++; - } else if (pkt_len > 64 && pkt_len < 1024) { - uint32_t bin; - - /* count zeros, and offset into correct bin */ - bin = (sizeof(pkt_len) * 8) - rte_clz32(pkt_len) - 5; - stats->size_bins[bin]++; - } else { - if (pkt_len < 64) - stats->size_bins[0]++; - else if (pkt_len < 1519) - stats->size_bins[6]++; - else - stats->size_bins[7]++; - } + if (pkt_len >= 1024) + stats->size_bins[6 + (pkt_len > 1518)]++; + else if (pkt_len <= 64) + stats->size_bins[pkt_len >> 6]++; + else + stats->size_bins[32UL - rte_clz32(pkt_len) - 5]++; - ea = rte_pktmbuf_mtod(pkt, struct rte_ether_addr *); - if (rte_is_multicast_ether_addr(ea)) { - if (rte_is_broadcast_ether_addr(ea)) - stats->broadcast++; - else - stats->multicast++; - } + ea = rte_pktmbuf_mtod(pkt, const struct rte_ether_addr *); + RTE_BUILD_BUG_ON(offsetof(struct virtqueue_stats, broadcast) != + offsetof(struct virtqueue_stats, multicast) + sizeof(uint64_t)); + if (unlikely(rte_is_multicast_ether_addr(ea))) + (&stats->multicast)[rte_is_broadcast_ether_addr(ea)]++; } } -- 2.43.0