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 03C914403F; Thu, 16 May 2024 01:42:53 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 6F29F402F2; Thu, 16 May 2024 01:42:48 +0200 (CEST) Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) by mails.dpdk.org (Postfix) with ESMTP id F0A4C402E1 for ; Thu, 16 May 2024 01:42:45 +0200 (CEST) Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-6f467fb2e66so6681657b3a.1 for ; Wed, 15 May 2024 16:42:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1715816565; x=1716421365; 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=3/mQJ/gJ76xp3pkyhb95yzNS3j0G5rXIsxM40qM+R8s=; b=aGC6ZEF55XWyQN0NYZwOchhJ5DU4HyxyHqhwAgjRBiE4bREQUYaOroRMOpLGciy4qU Fr6YlCRnGSB1BT1jdJmwb/5SUweW/0X94K1V3gzAl/Nd/GtOm55fy+Hj0CCVR+w8rFW+ OMdduoF4v7OXjaVOd8Xg7MfmPDLyBNE/qJ2xAv9/IfPc9ogl0d1x0nkjv4jgTnwGK81F Wce2E1c4If/M0zFBEQZeS3Tr0jo6ceIoaTp+GX5HsHnQbKWdaBHw5xoiblSnNUnaZ7dP O5RA+n3Qn9ZQmUneskQ/cxOvdKX0avRTrg+jTXnCZf1FTIjTLMLe4NMRRA2Jbzk1hXgP uUYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715816565; x=1716421365; 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=3/mQJ/gJ76xp3pkyhb95yzNS3j0G5rXIsxM40qM+R8s=; b=WRdAovWkufz8YYNi4kLpH9/3NnfB68G97rDDU/LaZUCDFSDBBtbRZp41Ep0EDrq8Vy Dz/bvxyOjUOTf6KgTUzanOOgzSe4xx7cLQj0jPMMCM3uY2xrDDiDWXb7Un0qyu6/7NnK 9+wfcALjsSEdpZuXGLiiLH3jdxFfNMngPbNJoxGk8m38nZlXt2dmvidL33FUqdQTw9eH 7NNkvx4OXH6fCRXzfQKEr2LOIghV54jBeOZFWjcA7JV0EXzzUtHf6Da9yeTUww8ZT+R1 alHy9U4+3XOpGbOQg94s/ZZgihHqcsXAyDk4jRhGx2ZMI9MXcb/amlC4BXmiDJNhP+ce Rd3A== X-Gm-Message-State: AOJu0YzA7swIL/gfDcoFFCRox+WdcxZmUCQr5AOEkVeRfA+PPTqggBl0 UHF7klqX3a264Eh4XQ54q0IgRa+Z8il+VMn+89Zg/52SFIvhr+uY1og73wPpC3uXQiH2Alzx9Oz sYr8= X-Google-Smtp-Source: AGHT+IE65TsMl2AkQOM6ypF+YIRV5Yf6l1QPoBmHLHDLg91SccE6/YKfSckY+7TQaRHkl8eL7CqhMg== X-Received: by 2002:a05:6a00:8a14:b0:6f6:802d:db82 with SMTP id d2e1a72fcca58-6f6802ddf11mr680825b3a.23.1715816564969; Wed, 15 May 2024 16:42:44 -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.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 16:42:44 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , =?UTF-8?q?Morten=20Br=C3=B8rup?= Subject: [PATCH v4 1/8] eal: generic 64 bit counter Date: Wed, 15 May 2024 16:40:53 -0700 Message-ID: <20240515234234.5015-2-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-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 | 91 +++++++++++++++++++++++++++++++++++ 2 files changed, 92 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..82e558bd6f --- /dev/null +++ b/lib/eal/include/rte_counter.h @@ -0,0 +1,91 @@ +/* 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. + */ + +#include + +#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_reset(rte_counter64_t *counter) +{ + *counter = 0; +} + +#else +/* + * 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_relaxed); +} + +__rte_experimental +static inline void +rte_counter64_reset(rte_counter64_t *counter) +{ + rte_atomic_store_explicit(counter, 0, rte_memory_order_relaxed); +} +#endif + + +#ifdef __cplusplus +} +#endif + +#endif /* _RTE_COUNTER_H_ */ -- 2.43.0