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 20AC04403F; Thu, 16 May 2024 01:43:00 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id C5C1140649; Thu, 16 May 2024 01:42:49 +0200 (CEST) Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) by mails.dpdk.org (Postfix) with ESMTP id CD6F3402E1 for ; Thu, 16 May 2024 01:42:46 +0200 (CEST) Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-6570bd6c3d7so652747a12.0 for ; Wed, 15 May 2024 16:42:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1715816566; x=1716421366; darn=dpdk.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Z8eKqf5lNQlmO6QqgxdqNWvOCC2KYo8qD/rGb5b3ZPM=; b=zjbDODa/9uqmb7NICMmQkNQBjm8mPfKiP5j+Dkoudr85ppAYJuxfleDcGz9s3v3KTd k6X2ccbuDfz+2+qKkaNqJX+4UVm0JaM5/1jjzRzefmIy0Pq6fO8pn8D0xyY8MIoRcoZL fMPmLhY7l941SArLCR7UbtsjzJUCWJHbaectAi3Bn94CtqpTMMMHyWSVfExwLDWF7qoz 1sfLBgvEa4AVDD7B1wCKYtbi6qtPd5Ty1Q0Vxv0pkqY57ilyC3GrdnMH1TodJdSZjj5P Uf33gS6oX0z9xbfKHTv6UFDWMrZPxn9FA4aBZIr0lXNMzDWNTDkXTrjeYJ49YcSRQmV9 acWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715816566; x=1716421366; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Z8eKqf5lNQlmO6QqgxdqNWvOCC2KYo8qD/rGb5b3ZPM=; b=KXC+m0sJhehvNtI8Dziy8T/ISL3TWFBY8gXjqI9lnBN0zl2nDe6Z92K+ze3Njuo0IM AMxtdSCIl4WGWdZyC5cDEOCeefQaCWQYaKh+N8cIWQzLIHNpnK/Lr/GJvs2jyAx4Blsk dA/DQz1LMe0Lrk2WTjlriFYqYqJKWLyO0yelRn6TWlXQ5ydUmdqoromxemvkjXTsLOad 7dLTPagXrt6EFmDQB5PiJ3S7cRL9kp+vKxdtuQ89e7D2ha237XQIgCsU31wrDZI7KhLQ NYQ0SIceASALs/21lbwLjGNtjHYg3ka4QOkOso6oSLP+nkMRmYMQoE0sH6+Qqd0DbIRk TYhQ== X-Gm-Message-State: AOJu0Yzz3fBmn2iXiy4tb5VgJcoDvWJ5cNIV3OIYL0+Oexe2/BoGIl3t SM701Oz2Qi8JJ8PGph9u0qzvMxWASEhAIn1glHeoNY+ZH6tx6UrXiKzvF9qAam6mwgsOE7CUmtY BvN8= X-Google-Smtp-Source: AGHT+IEWRTGOGR0FoQ4zYFU2owOYyKMXphce6BKWY4qGwkiDuSBXRCukBvTosTAOwn1MnxWFiIR9KQ== X-Received: by 2002:a05:6a20:e607:b0:1af:fe9e:5e43 with SMTP id adf61e73a8af0-1affe9e608fmr7759407637.11.1715816565868; Wed, 15 May 2024 16:42:45 -0700 (PDT) Received: from hermes.local (204-195-96-226.wavecable.com. [204.195.96.226]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f67b0cd494sm958479b3a.161.2024.05.15.16.42.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 16:42:45 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Thomas Monjalon , Ferruh Yigit , Andrew Rybchenko Subject: [PATCH v4 2/8] ethdev: add common counters for statistics Date: Wed, 15 May 2024 16:40:54 -0700 Message-ID: <20240515234234.5015-3-stephen@networkplumber.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240515234234.5015-1-stephen@networkplumber.org> References: <20240510050507.14381-1-stephen@networkplumber.org> <20240515234234.5015-1-stephen@networkplumber.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Introduce common helper routines for keeping track of per-queue statistics in SW PMD's. The code in several drivers had copy/pasted the same code for this, but had common issues with 64 bit counters on 32 bit platforms. Signed-off-by: Stephen Hemminger --- lib/ethdev/ethdev_swstats.c | 109 +++++++++++++++++++++++++++++++ lib/ethdev/ethdev_swstats.h | 124 ++++++++++++++++++++++++++++++++++++ lib/ethdev/meson.build | 2 + lib/ethdev/version.map | 3 + 4 files changed, 238 insertions(+) create mode 100644 lib/ethdev/ethdev_swstats.c create mode 100644 lib/ethdev/ethdev_swstats.h diff --git a/lib/ethdev/ethdev_swstats.c b/lib/ethdev/ethdev_swstats.c new file mode 100644 index 0000000000..7892b2180b --- /dev/null +++ b/lib/ethdev/ethdev_swstats.c @@ -0,0 +1,109 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (c) Stephen Hemminger + */ + + +#include +#include +#include + +#include "rte_ethdev.h" +#include "ethdev_driver.h" +#include "ethdev_swstats.h" + +int +rte_eth_counters_stats_get(const struct rte_eth_dev *dev, + size_t tx_offset, size_t rx_offset, + struct rte_eth_stats *stats) +{ + uint64_t packets, bytes, errors; + unsigned int i; + + for (i = 0; i < dev->data->nb_tx_queues; i++) { + const void *txq = dev->data->tx_queues[i]; + const struct rte_eth_counters *counters; + + if (txq == NULL) + continue; + + counters = (const struct rte_eth_counters *)((const char *)txq + tx_offset); + packets = rte_counter64_fetch(&counters->packets); + bytes = rte_counter64_fetch(&counters->bytes); + errors = rte_counter64_fetch(&counters->errors); + + /* + * Prevent compiler from fetching values twice which would + * cause per-queue and global statistics to not match. + */ + rte_compiler_barrier(); + + stats->opackets += packets; + stats->obytes += bytes; + stats->oerrors += errors; + + if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) { + stats->q_opackets[i] = packets; + stats->q_obytes[i] = bytes; + } + } + + for (i = 0; i < dev->data->nb_rx_queues; i++) { + const void *rxq = dev->data->rx_queues[i]; + const struct rte_eth_counters *counters; + + if (rxq == NULL) + continue; + + counters = (const struct rte_eth_counters *)((const char *)rxq + rx_offset); + packets = rte_counter64_fetch(&counters->packets); + bytes = rte_counter64_fetch(&counters->bytes); + errors = rte_counter64_fetch(&counters->errors); + + rte_compiler_barrier(); + + stats->ipackets += packets; + stats->ibytes += bytes; + stats->ierrors += errors; + + if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) { + stats->q_ipackets[i] = packets; + stats->q_ibytes[i] = bytes; + } + } + + stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed; + return 0; +} + +int +rte_eth_counters_reset(struct rte_eth_dev *dev, size_t tx_offset, size_t rx_offset) +{ + struct rte_eth_counters *counters; + unsigned int i; + + for (i = 0; i < dev->data->nb_tx_queues; i++) { + void *txq = dev->data->tx_queues[i]; + + if (txq == NULL) + continue; + + counters = (struct rte_eth_counters *)((char *)txq + tx_offset); + rte_counter64_reset(&counters->packets); + rte_counter64_reset(&counters->bytes); + rte_counter64_reset(&counters->errors); + } + + for (i = 0; i < dev->data->nb_rx_queues; i++) { + void *rxq = dev->data->rx_queues[i]; + + if (rxq == NULL) + continue; + + counters = (struct rte_eth_counters *)((char *)rxq + rx_offset); + rte_counter64_reset(&counters->packets); + rte_counter64_reset(&counters->bytes); + rte_counter64_reset(&counters->errors); + } + + return 0; +} diff --git a/lib/ethdev/ethdev_swstats.h b/lib/ethdev/ethdev_swstats.h new file mode 100644 index 0000000000..808c540640 --- /dev/null +++ b/lib/ethdev/ethdev_swstats.h @@ -0,0 +1,124 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (c) Stephen Hemminger + */ + +#ifndef _RTE_ETHDEV_SWSTATS_H_ +#define _RTE_ETHDEV_SWSTATS_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @file + * + * Internal statistics counters for software based devices. + * Hardware PMD's should use the hardware counters instead. + * + * This provides a library for PMD's to keep track of packets and bytes. + * It is assumed that this will be used per queue and queues are not + * shared by lcores. + */ + +#include + +/** + * A structure to be embedded in the device driver per-queue data. + */ +struct rte_eth_counters { + rte_counter64_t packets; /**< Total number of packets. */ + rte_counter64_t bytes; /**< Total number of bytes. */ + rte_counter64_t errors; /**< Total number of packets with errors. */ +}; + +/** + * @internal + * Increment counters for a single packet. + * + * @param counters + * Pointer to queue structure containing counters. + * @param sz + * Size of the packet in bytes. + */ +__rte_internal +static inline void +rte_eth_count_packet(struct rte_eth_counters *counters, uint32_t sz) +{ + rte_counter64_add(&counters->packets, 1); + rte_counter64_add(&counters->bytes, sz); +} + +/** + * @internal + * Increment counters based on mbuf. + * + * @param counters + * Pointer to queue structure containing counters. + * @param mbuf + * Received or transmitted mbuf. + */ +__rte_internal +static inline void +rte_eth_count_mbuf(struct rte_eth_counters *counters, const struct rte_mbuf *mbuf) +{ + rte_eth_count_packet(counters, rte_pktmbuf_pkt_len(mbuf)); +} + +/** + * @internal + * Increment error counter. + * + * @param counters + * Pointer to queue structure containing counters. + */ +__rte_internal +static inline void +rte_eth_count_error(struct rte_eth_counters *counters) +{ + rte_counter64_add(&counters->errors, 1); +} + +/** + * @internal + * Retrieve the general statistics for all queues. + * @see rte_eth_stats_get. + * + * @param dev + * Pointer to the Ethernet device structure. + * @param tx_offset + * Offset from the tx_queue structure where stats are located. + * @param rx_offset + * Offset from the rx_queue structure where stats are located. + * @param stats + * A pointer to a structure of type *rte_eth_stats* to be filled + * @return + * Zero if successful. Non-zero otherwise. + */ +__rte_internal +int rte_eth_counters_stats_get(const struct rte_eth_dev *dev, + size_t tx_offset, size_t rx_offset, + struct rte_eth_stats *stats); + +/** + * @internal + * Reset the statistics for all queues. + * @see rte_eth_stats_reset. + * + * @param dev + * Pointer to the Ethernet device structure. + * @param tx_offset + * Offset from the tx_queue structure where stats are located. + * @param rx_offset + * Offset from the rx_queue structure where stats are located. + * @return + * Zero if successful. Non-zero otherwise. + */ +__rte_internal +int rte_eth_counters_reset(struct rte_eth_dev *dev, + size_t tx_offset, size_t rx_offset); + +#ifdef __cplusplus +} +#endif + +#endif /* _RTE_ETHDEV_SWSTATS_H_ */ diff --git a/lib/ethdev/meson.build b/lib/ethdev/meson.build index f1d2586591..7ce29a46d4 100644 --- a/lib/ethdev/meson.build +++ b/lib/ethdev/meson.build @@ -3,6 +3,7 @@ sources = files( 'ethdev_driver.c', + 'ethdev_swstats.c', 'ethdev_private.c', 'ethdev_profile.c', 'ethdev_trace_points.c', @@ -42,6 +43,7 @@ driver_sdk_headers += files( 'ethdev_driver.h', 'ethdev_pci.h', 'ethdev_vdev.h', + 'ethdev_swstats.h', ) if is_linux diff --git a/lib/ethdev/version.map b/lib/ethdev/version.map index 79f6f5293b..fc595be278 100644 --- a/lib/ethdev/version.map +++ b/lib/ethdev/version.map @@ -358,4 +358,7 @@ INTERNAL { rte_eth_switch_domain_alloc; rte_eth_switch_domain_free; rte_flow_fp_default_ops; + + rte_eth_counters_reset; + rte_eth_counters_stats_get; }; -- 2.43.0