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 7EA05459C6; Wed, 18 Sep 2024 10:11:28 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id DFAFB42F41; Wed, 18 Sep 2024 10:10:50 +0200 (CEST) Received: from EUR05-VI1-obe.outbound.protection.outlook.com (mail-vi1eur05on2088.outbound.protection.outlook.com [40.107.21.88]) by mails.dpdk.org (Postfix) with ESMTP id 66A8B42E89 for ; Wed, 18 Sep 2024 10:10:41 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=wkme5AcfAiHlXsDi5b6A9oTJ+P+YtYgwrJIisLY0C5R0tvLdUJ6AiDAaeV9sWPsCjIE80cz4ntNPIDhYgaO74xr9TsHQUvwWgc/GxncHfUFbW/IcXl0/SivoIrDwz5eIx/YlmEzKN9/dTdDjaLTXLB6XcuiYBt2IjNrvBuaCpkv0vWjgagPQVq8RCsOrXTx92F8JpM2lmq4RAVqPFZF6qI8nqQAb/9HSAPTklFVd7vB0CbrbsLH9MTzqkmWg03URdWiFH23vSQ505WTh7tAENcwYpwy8Esr+PlX23QZfV5sL3EBGxRBGims002QHcsNq924G8fih0TPxtkh9KF2HWw== 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=Ate4uRWMkvddxCjyZ+f9KFEjsQMoM5Kyj5mukgASMmU=; b=LllEkY/p/V6xvhr2xpCcW6RFs5LysN5RaOyX/AjeG2PROgSfTxqYYv6q66erdTQ2SmQJRDP8QZvIVm1suGEITxCEdxrHSKDIme1eR+wO4w0GtgY0s7ODE4wE0R1zgPXzc/I5FZXrqH53Cevn1SrmLddCJuyyUc45sP70dKiGM8B92h3YVmTIL1swS3lKbesOjLRzPEY/6iilfIzkiGCzlUMtLOkhYrDtBKe96fiKIY8nEUoLIEejml6XVyDcJTVo1HWCGtaqVXkiqOlfmFxffHGpOoZul7rboB8IqR7Xa3FvHzo3g+PwC0Dcc0p4R2CxhxvQcqnLCPRGbmMVsKpqSw== 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=Ate4uRWMkvddxCjyZ+f9KFEjsQMoM5Kyj5mukgASMmU=; b=cwSKw4G4R8CFceu7/bhkcaREJHhjUoxvKj5jfSAQO3HvWCED/V7Hfb0XKUl0dFAqXenm9zICQiVMsidgfUzJPR6iJoESN5v/o/o403hvLsu73arpgrG8a8aJwWzXZP214opg7rJNTg4jxnnLbXolSQ+n/JJrQYEHUtOD7iwO4VspRJBiVT2omYF+H+KJIUmIRVkspgyT8K0gwu5b1kvPQk9yEV03hUi6m9c5hVCAKAxtmgjspOkQc4zPsnRVu8vyHl36loDHt2gINUrUPgqXRCg9/krg9S+CmNDL7EFwuRh8hfZfJ7RE4fOHx6KhOq2QFqO/8G7ko5R0JzJ9HWXLzw== Received: from AM8P191CA0011.EURP191.PROD.OUTLOOK.COM (2603:10a6:20b:21a::16) by PR3PR07MB8209.eurprd07.prod.outlook.com (2603:10a6:102:174::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7962.24; Wed, 18 Sep 2024 08:10:36 +0000 Received: from AM4PEPF00027A66.eurprd04.prod.outlook.com (2603:10a6:20b:21a:cafe::c1) by AM8P191CA0011.outlook.office365.com (2603:10a6:20b:21a::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.30 via Frontend Transport; Wed, 18 Sep 2024 08:10:36 +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 AM4PEPF00027A66.mail.protection.outlook.com (10.167.16.91) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.13 via Frontend Transport; Wed, 18 Sep 2024 08:10:36 +0000 Received: from seliicinfr00050.seli.gic.ericsson.se (153.88.142.248) by smtp-central.internal.ericsson.com (100.87.178.62) with Microsoft SMTP Server id 15.2.1544.11; Wed, 18 Sep 2024 10:10:36 +0200 Received: from breslau.. (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00050.seli.gic.ericsson.se (Postfix) with ESMTP id 285441C006A; Wed, 18 Sep 2024 10:10:36 +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 , =?UTF-8?q?Mattias=20R=C3=B6nnblom?= Subject: [PATCH v6 3/7] eal: add lcore variable performance test Date: Wed, 18 Sep 2024 10:00:50 +0200 Message-ID: <20240918080054.725164-4-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240918080054.725164-1-mattias.ronnblom@ericsson.com> References: <20240917143239.724069-2-mattias.ronnblom@ericsson.com> <20240918080054.725164-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: AM4PEPF00027A66:EE_|PR3PR07MB8209:EE_ X-MS-Office365-Filtering-Correlation-Id: 224e4d81-719d-4751-8941-08dcd7b96096 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|82310400026|1800799024|376014|36860700013; X-Microsoft-Antispam-Message-Info: =?utf-8?B?UDV1QzUwbFJuYzJvYnBmLy9kM3lUSmdhS2RPRGhGT1ZRL1orbmZvY3dUR05M?= =?utf-8?B?YUR6dkV6WEFtdUtkNm04VWpzVjZaYXpvdzFQVi9IUVlRTXc4OVNhM2hnUWZr?= =?utf-8?B?Nm1zSkVGaEIvb2VEMGE5cWdXK3c5QXQ1ejd5MWdXTjNNZGlVaEcvQmZ6dzdO?= =?utf-8?B?MXcvN0J2bU0wZEZnaCt3MFZxblNFejllUWo3RzA3Um5ieU9MbFBJc1dCVU9v?= =?utf-8?B?SENRclJRcUNMQm55dlZYMVo4UEZRbDlYVFZORjFERGlLNGJiNVFzamcyQ255?= =?utf-8?B?SzJ4WjBVMG12b0g5Y1dJaGVzMVk0dHV5RGwxWGp3MkdCaUlkYVpRcDhnK1pG?= =?utf-8?B?bmpSNWk2SVVoNUg2eGdENmxIWjNMZVE5Q042Ni9tWlNteXZ1Q3VQZUt2WFVZ?= =?utf-8?B?QUNXMXpmR05RbGpVMEZvcVRUdU1kcjdESzZhd0srSFdEalBqcDZ3Nno0Y1ZI?= =?utf-8?B?NC9HNnkyYWNKenE4RWRjanA2cGp1bUhTR0pUdGdRek5sRGJRUi9obVRoTEdl?= =?utf-8?B?QkxLZUFIVWtMV29TZmtZT1Z3RlZhcjA1cm13S0tnUkluaG5Qcnh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jlRVjlHU1hYbjZmL1hi?= =?utf-8?B?bXcxVjdMdllQbUhhYTNEWDFVUlBCakZsVWVYeXMzN3hUdDhBMVhsa2dFM0Ja?= =?utf-8?B?MnRDOEttNll4NS9zbWgwaSt5RjR4RFJJeTQ3OHhWQ21kTmV5d2dhOHZkbmc4?= =?utf-8?B?MUNSMXBvMzkwdElDY21rT3B3eE95cFJoZGp0dXd4bi93UGg5V3V0SDI2WEdv?= =?utf-8?B?VE9oZFZSbXltOHB1RXZ0Y0dKUXBPYnJ1NVVBUnMzTUcvZU43K05IV2M2ZlpG?= =?utf-8?B?eFdkTjBkMkRHeU56NmRGUDMvV2RIY1UvcmlKUGpxVHlmeVZ5bHZ3QVpEUmNh?= =?utf-8?B?NEU4MTVhNy9DTGdUZmNKVS82LzhnYUZTY2lkbW81OXdSUm9YSGdXVTk2V3Fz?= =?utf-8?Q?snEGREs4qHheY?= 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)(1800799024)(376014)(36860700013); DIR:OUT; SFP:1101; X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Sep 2024 08:10:36.8542 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 224e4d81-719d-4751-8941-08dcd7b96096 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: AM4PEPF00027A66.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PR3PR07MB8209 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 -- 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..2680bfb6f7 --- /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 are 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 wiht a small, per-lcore state. Note however that + * these tests has 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.34.1