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 2555145B40; Tue, 15 Oct 2024 09:05:09 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id D99FF406BC; Tue, 15 Oct 2024 09:04:34 +0200 (CEST) Received: from EUR03-AM7-obe.outbound.protection.outlook.com (mail-am7eur03on2062.outbound.protection.outlook.com [40.107.105.62]) by mails.dpdk.org (Postfix) with ESMTP id 4C6D8402E3 for ; Tue, 15 Oct 2024 09:04:19 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=qjEPtMH5mQEtmLFm+MJsXLUnZ1ltDF7YI8X/RMpHnhVG821xudltoR4ie+8wylFn643PvWQ7uKWnwG/Q02W+C7UYE1B7EYlu9G7VdiQydt9go05OMXKTyklD+qV8zWCPCdm6CjXtgb4jOBgwJR4G5HE5rH29tOmoCyHGu83uJejzoeq22ceT62MLKuxHjB49uAxUZ9Cdl8l3suA4KYV9WvKu9Tef+Y+tukLpZF+lfwxe1vebf09wXmYrTkqvQ8vllecjXRbH0dGjJeC/ZdXI56OEjEqHmNV9XpsydrXRRdsCq8rTEjlBg3AD8Abt9Z8lTeVTQkwOSOWGI8MFTc966A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=3SE7ek+/krDICvlTVpg7t/7m7wAOxBjgLDNI2xEB+jk=; b=GpFriM5G8jHdbUrUx48P1XRuf0SrrmJPHI/pBtiXtxb+k77gxdZD1Yi9L9u9K9L7WRJATPqiwBi5xXZ3eeTX3APDMkwOTHIao7KdseYEwQ2cx9P++pl2sdN5E1OlsuS0m+WQz3OGDgMx/mmPKvzE+EePY1rWw+Ir/7gYuQtNKw8LgJijcmJ5FBcD1cWjdg00P6c951QhKztMoBdZQMUcUFkHndzhGIyYpDYit4rG980o8pPvxSdPghMlRRtI4JNK0z7QT1N4x4ZpsiDjQF6+oNMjGH0CZoyGsP8YAXFu2RqrTED3q48QeZ718xi0MlU0j3GLr/kfpzItVvjRdJU7Jw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 192.176.1.74) smtp.rcpttodomain=dpdk.org smtp.mailfrom=ericsson.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=ericsson.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ericsson.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3SE7ek+/krDICvlTVpg7t/7m7wAOxBjgLDNI2xEB+jk=; b=kL3J2I/kM8nNwHIsbs27fIYiKOWqrCqqokpiCDoIt0wBYKgQvLppesZbfMx+cTSUQFQocqwRdnKQvtERFbecKpsYZkid2unfFHbpBxillSKvC4jz9/zhwRRrCFVkLDsxbX9feDZkgXPd4RpFtK1Z0sbf5GqofIGP+xl47XhLvXy/eovTyuQvz8CE8ihEg3kv8uLK+8C63OT7TH36Uue2WvW+uOYiHOwwND7RamXz4tCfDp9+I6VsBx5/KztatJF10Cp5dcSMZ2AHQ85M/UO7Py5+Znypl6w/+WCm5Uqg+KKrAv1Jl/pIuGedUWKdPzlXKf4JI9KifBgqp9pPG2HMDA== Received: from DUZPR01CA0201.eurprd01.prod.exchangelabs.com (2603:10a6:10:4b6::12) by GV1PR07MB8998.eurprd07.prod.outlook.com (2603:10a6:150:a3::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.26; Tue, 15 Oct 2024 07:04:12 +0000 Received: from DB1PEPF000509EC.eurprd03.prod.outlook.com (2603:10a6:10:4b6::4) by DUZPR01CA0201.outlook.office365.com (2603:10a6:10:4b6::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.27 via Frontend Transport; Tue, 15 Oct 2024 07:04:12 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 192.176.1.74) smtp.mailfrom=ericsson.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=ericsson.com; Received-SPF: Pass (protection.outlook.com: domain of ericsson.com designates 192.176.1.74 as permitted sender) receiver=protection.outlook.com; client-ip=192.176.1.74; helo=oa.msg.ericsson.com; pr=C Received: from oa.msg.ericsson.com (192.176.1.74) by DB1PEPF000509EC.mail.protection.outlook.com (10.167.242.70) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.17 via Frontend Transport; Tue, 15 Oct 2024 07:04:12 +0000 Received: from seliicinfr00050.seli.gic.ericsson.se (153.88.142.248) by smtp-central.internal.ericsson.com (100.87.178.60) with Microsoft SMTP Server id 15.2.1544.11; Tue, 15 Oct 2024 09:04:11 +0200 Received: from breslau.. (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00050.seli.gic.ericsson.se (Postfix) with ESMTP id 677471C006B; Tue, 15 Oct 2024 09:04:11 +0200 (CEST) From: =?UTF-8?q?Mattias=20R=C3=B6nnblom?= To: CC: , =?UTF-8?q?Morten=20Br=C3=B8rup?= , Stephen Hemminger , Konstantin Ananyev , David Marchand , Jerin Jacob , Luka Jankovic , =?UTF-8?q?Mattias=20R=C3=B6nnblom?= , Chengwen Feng Subject: [PATCH v12 3/7] eal: add lcore variable performance test Date: Tue, 15 Oct 2024 08:55:01 +0200 Message-ID: <20241015065505.823840-4-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241015065505.823840-1-mattias.ronnblom@ericsson.com> References: <20241014074348.821962-2-mattias.ronnblom@ericsson.com> <20241015065505.823840-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DB1PEPF000509EC:EE_|GV1PR07MB8998:EE_ X-MS-Office365-Filtering-Correlation-Id: fa2f0933-f19d-4ce0-29b0-08dcece792d0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|82310400026|36860700013|376014|1800799024; X-Microsoft-Antispam-Message-Info: =?utf-8?B?aGFnLzUxMVdvL3A4eUkwcXN4Wnhvb2xhUWF1M3Rhb0dOdy93clFhdUdqRDVO?= =?utf-8?B?ODl3Tm1YOVhSRUpvYmppVWFzZkliTVZoNmZ2MWl3WFphemRhN3RCM20xck5a?= =?utf-8?B?V2xOeFRaN1lOS1lUZlpMQXhUeVVqRXFVVFJidGROa0xUVHAvdTZKTWo2Z1Fa?= =?utf-8?B?SEk2clFCSkhoWG05Y1Z2bFRud3AvSCtFZ0d2NVAwZ1diemxDcy9PYVpUS3dk?= =?utf-8?B?aUZPRlU0NGE2NzNGdXJteksycEFNZGFUVGM4bGF1QThuS1d5c1oyam5yaFhZ?= =?utf-8?B?NFh4dWZNMWtBVFJYWXVqYWQ1VHlJNmpJbFV5Q0Y2MTdBOXp0akRzU3VFMWVs?= =?utf-8?B?Q2pDaVVvV1RuOWo3SWFFQldVdnArNUY5Y1EyVzNBYXQ0T2s0K21KVXhBT3Uy?= =?utf-8?B?NzdCWFZVdDBlSURBWW9Xb0NnNndSSmFHMCtkZmZDM1VSUm1IK3Ztd2NqZkdV?= =?utf-8?B?Y1VnbjlORnMwaTY1MGFFdkhPUGpnVUYvbGkzcDkzTDZVV3hiRGtDUXZreGpq?= =?utf-8?B?dUZaN1dsYTNXSjZPRzd6M3hnZ20w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Nka0Y2MndVeVVMd3hqdXlJMjNwakV4WFNhREpwMXpj?= =?utf-8?B?UUUrVHFvNDc2ZmhLOUI2bWE0NkhxT0RJVkFVYWZwbm80L01yQ0V6dG81bHpU?= =?utf-8?B?dEZaNVdGcUdNOHBoRy9mRUlJcTBrTzA1b1NoNHFaTHFESUNmY3F2eFQyL0gv?= =?utf-8?B?VFI1QmJyblBGaUNnUDBUZGZwMkJtVFU0VkNZVUdTcVlSamJ0dXFDaTlZUzdB?= =?utf-8?B?MGl1cG12Nk1Ea0RFYlpKTXM2cEFxS3lPdUtQTDZhS2Y4bHFSRzQya3gvRjIv?= =?utf-8?B?NkdXUFl6MHN4a2Fveml2UXgyc2p1R00wWjR5MW9EZGNyNDRnc0lpMkx5Nnp6?= =?utf-8?B?d2QzZFRFNVc3bi9LOFZNdi9zUDZRckJ4OHZRZ0ZKMTZuOUwreUtYUFovVDJ0?= =?utf-8?Q?AWdWKq00/rH2K?= X-Forefront-Antispam-Report: CIP:192.176.1.74; CTRY:SE; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:oa.msg.ericsson.com; PTR:office365.se.ericsson.net; CAT:NONE; SFS:(13230040)(82310400026)(36860700013)(376014)(1800799024); DIR:OUT; SFP:1101; X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Oct 2024 07:04:12.3546 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fa2f0933-f19d-4ce0-29b0-08dcece792d0 X-MS-Exchange-CrossTenant-Id: 92e84ceb-fbfd-47ab-be52-080c6b87953f X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=92e84ceb-fbfd-47ab-be52-080c6b87953f; Ip=[192.176.1.74]; Helo=[oa.msg.ericsson.com] X-MS-Exchange-CrossTenant-AuthSource: DB1PEPF000509EC.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: GV1PR07MB8998 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 Add basic micro benchmark for lcore variables, in an attempt to assure that the overhead isn't significantly greater than alternative approaches, in scenarios where the benefits aren't expected to show up (i.e., when plenty of cache is available compared to the working set size of the per-lcore data). Signed-off-by: Mattias Rönnblom Acked-by: Chengwen Feng Acked-by: Stephen Hemminger Acked-by: Morten Brørup -- PATCH v8: * Fix spelling. (Morten Brørup) PATCH v6: * Use floating point math when calculating per-update latency. (Morten Brørup) PATCH v5: * Add variant of thread-local storage with initialization performed at the time of thread creation to the benchmark scenarios. (Morten Brørup) PATCH v4: * Rework the tests to be a little less unrealistic. Instead of a single dummy module using a single variable, use a number of variables/modules. In this way, differences in cache effects may show up. * Add RTE_CACHE_GUARD to better mimic that static array pattern. (Morten Brørup) * Show latencies as TSC cycles. (Morten Brørup) --- app/test/meson.build | 1 + app/test/test_lcore_var_perf.c | 257 +++++++++++++++++++++++++++++++++ 2 files changed, 258 insertions(+) create mode 100644 app/test/test_lcore_var_perf.c diff --git a/app/test/meson.build b/app/test/meson.build index 48279522f0..d4e0c59900 100644 --- a/app/test/meson.build +++ b/app/test/meson.build @@ -104,6 +104,7 @@ source_file_deps = { 'test_kvargs.c': ['kvargs'], 'test_latencystats.c': ['ethdev', 'latencystats', 'metrics'] + sample_packet_forward_deps, 'test_lcore_var.c': [], + 'test_lcore_var_perf.c': [], 'test_lcores.c': [], 'test_link_bonding.c': ['ethdev', 'net_bond', 'net'] + packet_burst_generator_deps + virtual_pmd_deps, diff --git a/app/test/test_lcore_var_perf.c b/app/test/test_lcore_var_perf.c new file mode 100644 index 0000000000..2efb8342d1 --- /dev/null +++ b/app/test/test_lcore_var_perf.c @@ -0,0 +1,257 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2024 Ericsson AB + */ + +#define MAX_MODS 1024 + +#include + +#include +#include +#include +#include +#include + +#include "test.h" + +struct mod_lcore_state { + uint64_t a; + uint64_t b; + uint64_t sum; +}; + +static void +mod_init(struct mod_lcore_state *state) +{ + state->a = rte_rand(); + state->b = rte_rand(); + state->sum = 0; +} + +static __rte_always_inline void +mod_update(volatile struct mod_lcore_state *state) +{ + state->sum += state->a * state->b; +} + +struct __rte_cache_aligned mod_lcore_state_aligned { + struct mod_lcore_state mod_state; + + RTE_CACHE_GUARD; +}; + +static struct mod_lcore_state_aligned +sarray_lcore_state[MAX_MODS][RTE_MAX_LCORE]; + +static void +sarray_init(void) +{ + unsigned int lcore_id = rte_lcore_id(); + int mod; + + for (mod = 0; mod < MAX_MODS; mod++) { + struct mod_lcore_state *mod_state = + &sarray_lcore_state[mod][lcore_id].mod_state; + + mod_init(mod_state); + } +} + +static __rte_noinline void +sarray_update(unsigned int mod) +{ + unsigned int lcore_id = rte_lcore_id(); + struct mod_lcore_state *mod_state = + &sarray_lcore_state[mod][lcore_id].mod_state; + + mod_update(mod_state); +} + +struct mod_lcore_state_lazy { + struct mod_lcore_state mod_state; + bool initialized; +}; + +/* + * Note: it's usually a bad idea have this much thread-local storage + * allocated in a real application, since it will incur a cost on + * thread creation and non-lcore thread memory usage. + */ +static RTE_DEFINE_PER_LCORE(struct mod_lcore_state_lazy, + tls_lcore_state)[MAX_MODS]; + +static inline void +tls_init(struct mod_lcore_state_lazy *state) +{ + mod_init(&state->mod_state); + + state->initialized = true; +} + +static __rte_noinline void +tls_lazy_update(unsigned int mod) +{ + struct mod_lcore_state_lazy *state = + &RTE_PER_LCORE(tls_lcore_state[mod]); + + /* With thread-local storage, initialization must usually be lazy */ + if (!state->initialized) + tls_init(state); + + mod_update(&state->mod_state); +} + +static __rte_noinline void +tls_update(unsigned int mod) +{ + struct mod_lcore_state_lazy *state = + &RTE_PER_LCORE(tls_lcore_state[mod]); + + mod_update(&state->mod_state); +} + +RTE_LCORE_VAR_HANDLE(struct mod_lcore_state, lvar_lcore_state)[MAX_MODS]; + +static void +lvar_init(void) +{ + unsigned int mod; + + for (mod = 0; mod < MAX_MODS; mod++) { + RTE_LCORE_VAR_ALLOC(lvar_lcore_state[mod]); + + struct mod_lcore_state *state = + RTE_LCORE_VAR_VALUE(lvar_lcore_state[mod]); + + mod_init(state); + } +} + +static __rte_noinline void +lvar_update(unsigned int mod) +{ + struct mod_lcore_state *state = + RTE_LCORE_VAR_VALUE(lvar_lcore_state[mod]); + + mod_update(state); +} + +static void +shuffle(unsigned int *elems, size_t len) +{ + size_t i; + + for (i = len - 1; i > 0; i--) { + unsigned int other = rte_rand_max(i + 1); + + unsigned int tmp = elems[other]; + elems[other] = elems[i]; + elems[i] = tmp; + } +} + +#define ITERATIONS UINT64_C(10000000) + +static inline double +benchmark_access(const unsigned int *mods, unsigned int num_mods, + void (*init_fun)(void), void (*update_fun)(unsigned int)) +{ + unsigned int i; + double start; + double end; + double latency; + unsigned int num_mods_mask = num_mods - 1; + + RTE_VERIFY(rte_is_power_of_2(num_mods)); + + if (init_fun != NULL) + init_fun(); + + /* Warm up cache and make sure TLS variables are initialized */ + for (i = 0; i < num_mods; i++) + update_fun(i); + + start = rte_rdtsc(); + + for (i = 0; i < ITERATIONS; i++) + update_fun(mods[i & num_mods_mask]); + + end = rte_rdtsc(); + + latency = (end - start) / (double)ITERATIONS; + + return latency; +} + +static void +test_lcore_var_access_n(unsigned int num_mods) +{ + double sarray_latency; + double tls_latency; + double lazy_tls_latency; + double lvar_latency; + unsigned int mods[num_mods]; + unsigned int i; + + for (i = 0; i < num_mods; i++) + mods[i] = i; + + shuffle(mods, num_mods); + + sarray_latency = + benchmark_access(mods, num_mods, sarray_init, sarray_update); + + tls_latency = + benchmark_access(mods, num_mods, NULL, tls_update); + + lazy_tls_latency = + benchmark_access(mods, num_mods, NULL, tls_lazy_update); + + lvar_latency = + benchmark_access(mods, num_mods, lvar_init, lvar_update); + + printf("%17u %8.1f %14.1f %15.1f %10.1f\n", num_mods, sarray_latency, + tls_latency, lazy_tls_latency, lvar_latency); +} + +/* + * The potential performance benefit of lcore variables compared to + * the use of statically sized, lcore id-indexed arrays is not + * shorter latencies in a scenario with low cache pressure, but rather + * fewer cache misses in a real-world scenario, with extensive cache + * usage. These tests are a crude simulation of such, using dummy + * modules, each with a small, per-lcore state. Note however that + * these tests have very little non-lcore/thread local state, which is + * unrealistic. + */ + +static int +test_lcore_var_access(void) +{ + unsigned int num_mods = 1; + + printf("- Latencies [TSC cycles/update] -\n"); + printf("Number of Static Thread-local Thread-local Lcore\n"); + printf("Modules/Variables Array Storage Storage (Lazy) Variables\n"); + + for (num_mods = 1; num_mods <= MAX_MODS; num_mods *= 2) + test_lcore_var_access_n(num_mods); + + return TEST_SUCCESS; +} + +static struct unit_test_suite lcore_var_testsuite = { + .suite_name = "lcore variable perf autotest", + .unit_test_cases = { + TEST_CASE(test_lcore_var_access), + TEST_CASES_END() + }, +}; + +static int +test_lcore_var_perf(void) +{ + return unit_test_suite_runner(&lcore_var_testsuite); +} + +REGISTER_PERF_TEST(lcore_var_perf_autotest, test_lcore_var_perf); -- 2.43.0