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 3D39044052; Fri, 17 May 2024 19:40:55 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 31E0C4064C; Fri, 17 May 2024 19:40:51 +0200 (CEST) Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by mails.dpdk.org (Postfix) with ESMTP id 7A8A040268 for ; Fri, 17 May 2024 19:40:48 +0200 (CEST) Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1ed012c1afbso7156715ad.1 for ; Fri, 17 May 2024 10:40:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1715967647; x=1716572447; 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=ALuFLJILP6C+V/9ZzQqYB1D5U2Yok5jTHH8UBj4O/kg=; b=z8Q83rp8Vei7ZHTBTOFYKkepcFxemu6KiNjGTlonvCPzkJkGOHjRjRteIvlOqMeLWt Pt9xpaZav3P+ADePb3hjvjbIxekg09hbMutmSnGPTVjmJopdskUyjzn9XvvwBlq+OEHo 9BnOW3aMMohBfdVVYH5ND1W5SHu4RO8HKqqqszuYKA1SG3rGIBb4gxahu24dVbJI++OO V8w2fnEy/YAlwZKPRNqWH9bYNdIT4T5cONp26JtF8qiHB4cH6GJb9al80/G6arJee/de AfjlyHzolhwSu0eNFcsMN+Y/1GL4suxcFD0SS+oGbtVW4q/LWLwYDNRIHtkiP5J3g1DJ SviQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715967647; x=1716572447; 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=ALuFLJILP6C+V/9ZzQqYB1D5U2Yok5jTHH8UBj4O/kg=; b=vQoZwbL32biSyRWofj5U3NScsSoTZQJ+Ct16d+6pZqcZjlJSd4LqLffKZ2njeOJ3M/ LQiwW5eNUvIOCVPW8gP3Au+Hl45qTbtbmKjxrN28XshmkrbhDUxk2ep3mwhQxS+WlCY7 Ty6jOXH7UuaThNncW5FkhpxpdBTiceiqfWWJ+Wste38Rk3954OQCJplGDegD4R10XJvq gwe13SpJ7uSzb4gx+KAh03wWpI+qL5m8FbpB6Elshzv0JtVmSGTVoFfAz4SGT9Evg5gb QyDL3pBFVgoRsIW9f2eu9bUGUW/t0czUatB4X0L7HNaMsOvYwIg2lIabotWoxu3E1jXo RoNg== X-Gm-Message-State: AOJu0YwC4gD/ChH+R51qb/H9ZvNWO6wy9dIOHEm9SdFlaEhWf9BQTY4Q YX0hWmPlJjzH0jPP3zulmD+mNgc/zNHBjsx0nRv2JS0ujcEi2PAT6QcdSInY8k9Vl7d4GXGjiTH BMDM= X-Google-Smtp-Source: AGHT+IGif2we2gSsQJ0+0rfsL4n8sVVXQgN/CxEn+6TtrDT70d5ouX632On6SMjHyO6CTsVtVNPV6g== X-Received: by 2002:a17:902:d507:b0:1ec:c6ba:f2c3 with SMTP id d9443c01a7336-1ef42d69b5amr362755805ad.2.1715967647453; Fri, 17 May 2024 10:40:47 -0700 (PDT) Received: from hermes.lan (204-195-96-226.wavecable.com. [204.195.96.226]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0bf31032sm158830485ad.131.2024.05.17.10.40.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 May 2024 10:40:46 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , =?UTF-8?q?Morten=20Br=C3=B8rup?= , Tyler Retzlaff Subject: [PATCH v7 1/9] eal: generic 64 bit counter Date: Fri, 17 May 2024 10:35:08 -0700 Message-ID: <20240517174044.90952-2-stephen@networkplumber.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240517174044.90952-1-stephen@networkplumber.org> References: <20240510050507.14381-1-stephen@networkplumber.org> <20240517174044.90952-1-stephen@networkplumber.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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 This header implements 64 bit counters using atomic operations but with a weak memory ordering so that they are safe against load/store splits on 32 bit platforms. Signed-off-by: Stephen Hemminger Acked-by: Morten Brørup --- lib/eal/include/meson.build | 1 + lib/eal/include/rte_counter.h | 116 ++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 lib/eal/include/rte_counter.h diff --git a/lib/eal/include/meson.build b/lib/eal/include/meson.build index e94b056d46..c070dd0079 100644 --- a/lib/eal/include/meson.build +++ b/lib/eal/include/meson.build @@ -12,6 +12,7 @@ headers += files( 'rte_class.h', 'rte_common.h', 'rte_compat.h', + 'rte_counter.h', 'rte_debug.h', 'rte_dev.h', 'rte_devargs.h', diff --git a/lib/eal/include/rte_counter.h b/lib/eal/include/rte_counter.h new file mode 100644 index 0000000000..cdaa426e12 --- /dev/null +++ b/lib/eal/include/rte_counter.h @@ -0,0 +1,116 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (c) Stephen Hemminger + */ + +#ifndef _RTE_COUNTER_H_ +#define _RTE_COUNTER_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +/** + * @file + * RTE Counter + * + * A counter is 64 bit value that is safe from split read/write. + * It assumes that only one cpu at a time will update the counter, + * and another CPU may want to read it. + * + * This is a weaker subset of full atomic variables. + * + * The counters are subject to the restrictions of atomic variables + * in packed structures or unaligned. + */ + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * The RTE counter type. + */ +typedef RTE_ATOMIC(uint64_t) rte_counter64_t; + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Add value to counter. + * Assumes this operation is only done by one thread on the object. + * + * @param counter + * A pointer to the counter. + * @param val + * The value to add to the counter. + */ +__rte_experimental +static inline void +rte_counter64_add(rte_counter64_t *counter, uint32_t val) +{ + rte_atomic_fetch_add_explicit(counter, val, rte_memory_order_relaxed); +} + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Read a counter. + * This operation can be done by any thread. + * + * @param counter + * A pointer to the counter. + * @return + * The current value of the counter. + */ +__rte_experimental +static inline uint64_t +rte_counter64_fetch(const rte_counter64_t *counter) +{ + return rte_atomic_load_explicit(counter, rte_memory_order_consume); +} + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Set a counter. + * This operation can be done by any thread. + * + * @param counter + * A pointer to the counter. + * @param val + * Value to set counter to. + */ +__rte_experimental +static inline void +rte_counter64_set(rte_counter64_t *counter, uint64_t val) +{ + rte_atomic_store_explicit(counter, val, rte_memory_order_release); +} + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Reset a counter to zero. + * This operation can be done by any thread. + * + * @param counter + * A pointer to the counter. + */ +__rte_experimental +static inline void +rte_counter64_reset(rte_counter64_t *counter) +{ + rte_counter64_set(counter, 0); +} + +#ifdef __cplusplus +} +#endif + +#endif /* _RTE_COUNTER_H_ */ -- 2.43.0