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 D9DDF44044; Fri, 17 May 2024 02:13:21 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 666B0402ED; Fri, 17 May 2024 02:13:17 +0200 (CEST) Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by mails.dpdk.org (Postfix) with ESMTP id 26E774027F for ; Fri, 17 May 2024 02:13:14 +0200 (CEST) Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-6f43ee95078so780572b3a.1 for ; Thu, 16 May 2024 17:13:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1715904793; x=1716509593; 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=it8vQ7zCnyKycuWg3oTjgKRI/75M1tfp8aWkSfvtLSE=; b=yBPOnqyb13zIcquimSHGi3wR/zPQ9wYMunweozaWhmwq6wn9zhkQoJAWmtrsWcCXtM r1akPdJNU34kJm5aD5XCKrhWWrEgP/3PFh76fkWIAuRBV2s6B2AVnue5t07a9pKbK8y2 McrIfSyeYPHDwTY3viAy5cVmcRaI4TsWvqbgfnoFov4+j7EhGzhteNCGiUCQ2Ia3h+42 Y5t7XBtonHw6AhaYogk8PvGC8jbZtv11l3FSFDRahYjkdz84sBHa0ltK3Xcydfd4IiSC 0AnLWJ/rdk8cW8e0Bj5g/fnTBBQ/fW3nsIzXK7Vh4kDVHXcXsgVC3kFe8pjSYQzmnomw 7oIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715904793; x=1716509593; 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=it8vQ7zCnyKycuWg3oTjgKRI/75M1tfp8aWkSfvtLSE=; b=Q2Qbqf/BnZKKeaR3GDOcNFAoqrNiZmadfdcCMQ5ydhTS8IqkdOoqWQPqDSuSy3Pw9n etl6jGDCdeguvR1VhG7Te6osa/uagjJ71a2wZQmzb03ZepDUWp9MX8iSUdjnL9a4oJzo uF+E9c9B6BEUA1LC9XWiAW/Av538GHL3K6dFZ6epbdPBSLFwaQ6j9LqpIZ7fVd2nl2+K i8aEsU/ytmxDpPid1lBzBrwo2hbJiDFGRlhb7KZMagG0Zvpdbx+4ca1944FrQNAy3CED bHS3/DrJMllkMmHqdqCcaLBNpFD+vTR9miZE1Zje9uZRNtq49FZ5Q2y/q2qgv1SzE0Lo pL4g== X-Gm-Message-State: AOJu0YzRJfwlMGb9cZDH7vjcMJy9rFv8gJkWmiV0i8jXiXxwiWUb5OA0 KJQGXd8myUDvMfnNR3lK5uHALacPAswCQ8PcnszHxAPufQZpGpmGANhKyalOMFNicZoQcGN6n0X FGrs= X-Google-Smtp-Source: AGHT+IEQ+34crnoyxX8RxHJm3dmVZQQi4uAY20f/wc6/KBwGbWc5ydCkeDbtxDYkp9OSybrVasZ2oQ== X-Received: by 2002:a05:6a20:12d5:b0:1af:66e6:b1b2 with SMTP id adf61e73a8af0-1afde0b7152mr23824295637.1.1715904793104; Thu, 16 May 2024 17:13:13 -0700 (PDT) Received: from hermes.local (204-195-96-226.wavecable.com. [204.195.96.226]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2b9f5820ce9sm3337495a91.56.2024.05.16.17.13.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 17:13:12 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , =?UTF-8?q?Morten=20Br=C3=B8rup?= Subject: [PATCH v6 1/9] eal: generic 64 bit counter Date: Thu, 16 May 2024 17:12:01 -0700 Message-ID: <20240517001302.65514-2-stephen@networkplumber.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240517001302.65514-1-stephen@networkplumber.org> References: <20240510050507.14381-1-stephen@networkplumber.org> <20240517001302.65514-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 that are NOT atomic but 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 | 98 +++++++++++++++++++++++++++++++++++ 2 files changed, 99 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..d623195d63 --- /dev/null +++ b/lib/eal/include/rte_counter.h @@ -0,0 +1,98 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (c) Stephen Hemminger + */ + +#ifndef _RTE_COUNTER_H_ +#define _RTE_COUNTER_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @file + * RTE Counter + * + * A counter is 64 bit value that is safe from split read/write + * on 32 bit platforms. It assumes that only one cpu at a time + * will update the counter, and another CPU may want to read it. + * + * This is a much weaker guarantee than full atomic variables + * but is faster since no locked operations are required for update. + */ + +#ifdef RTE_ARCH_64 +/* + * On a platform that can support native 64 bit type, no special handling. + * These are just wrapper around 64 bit value. + */ +typedef uint64_t rte_counter64_t; + +/** + * Add value to counter. + */ +__rte_experimental +static inline void +rte_counter64_add(rte_counter64_t *counter, uint32_t val) +{ + *counter += val; +} + +__rte_experimental +static inline uint64_t +rte_counter64_fetch(const rte_counter64_t *counter) +{ + return *counter; +} + +__rte_experimental +static inline void +rte_counter64_set(rte_counter64_t *counter, uint64_t val) +{ + *counter = val; +} + +#else + +#include + +/* + * On a 32 bit platform need to use atomic to force the compler to not + * split 64 bit read/write. + */ +typedef RTE_ATOMIC(uint64_t) rte_counter64_t; + +__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); +} + +__rte_experimental +static inline uint64_t +rte_counter64_fetch(const rte_counter64_t *counter) +{ + return rte_atomic_load_explicit(counter, rte_memory_order_consume); +} + +__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); +} +#endif + +__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