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 6503445705; Fri, 2 Aug 2024 04:23:17 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 3842E4029A; Fri, 2 Aug 2024 04:23:17 +0200 (CEST) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by mails.dpdk.org (Postfix) with ESMTP id 0EDFA400D6 for ; Fri, 2 Aug 2024 04:23:15 +0200 (CEST) Received: from mail.maildlp.com (unknown [172.19.162.254]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4WZqPX2Hh9zyPXw; Fri, 2 Aug 2024 10:23:12 +0800 (CST) Received: from kwepemm600004.china.huawei.com (unknown [7.193.23.242]) by mail.maildlp.com (Postfix) with ESMTPS id A339E1800CD; Fri, 2 Aug 2024 10:23:13 +0800 (CST) Received: from [10.67.121.59] (10.67.121.59) by kwepemm600004.china.huawei.com (7.193.23.242) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Fri, 2 Aug 2024 10:23:13 +0800 Message-ID: <677a47da-c6ba-57a9-92c7-a98df739d216@huawei.com> Date: Fri, 2 Aug 2024 10:23:12 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.2.0 Subject: Re: [PATCH v5] virtio: optimize stats counters performance To: =?UTF-8?Q?Morten_Br=c3=b8rup?= , , CC: , References: <20240731131744.36448-1-mb@smartsharesystems.com> <20240801160312.205281-1-mb@smartsharesystems.com> From: "lihuisong (C)" In-Reply-To: <20240801160312.205281-1-mb@smartsharesystems.com> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.67.121.59] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To kwepemm600004.china.huawei.com (7.193.23.242) 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 在 2024/8/2 0:03, 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 > --- > 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(-) > > diff --git a/drivers/net/virtio/virtio_rxtx.c b/drivers/net/virtio/virtio_rxtx.c > index f69b9453a2..b67f063b31 100644 > --- a/drivers/net/virtio/virtio_rxtx.c > +++ b/drivers/net/virtio/virtio_rxtx.c > @@ -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? > { > 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)]++; 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)]++; > } > > static inline void > diff --git a/drivers/net/virtio/virtio_rxtx.h b/drivers/net/virtio/virtio_rxtx.h > index afc4b74534..68034c914b 100644 > --- a/drivers/net/virtio/virtio_rxtx.h > +++ b/drivers/net/virtio/virtio_rxtx.h > @@ -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_ */