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 E78F445B34; Mon, 14 Oct 2024 09:53:44 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id DB8EB4065B; Mon, 14 Oct 2024 09:53:09 +0200 (CEST) Received: from EUR02-DB5-obe.outbound.protection.outlook.com (mail-db5eur02on2087.outbound.protection.outlook.com [40.107.249.87]) by mails.dpdk.org (Postfix) with ESMTP id 51B5B402DE for ; Mon, 14 Oct 2024 09:53:00 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=uQq0znL9wpEbUEfQKd2ZJssy6aQ+TgbffR9VU/4icN1JoOhAgYMEyRhfcATcISOv70i5V9RNdntyhig2cauWM576Dj7A7FkJXzQRJF6NmdLdvyvV6PBSZR+xUORMz5NIK98T3DgmI0ZgsnkJf+Htvdf+KbM0HtojRx4AsnaU13ckFQ8wZZgtFadt3HpiLSXWWW+o9PajthioW4bvVuo+Hd9w+I4GvRmydTpTu/Tj2KbBDkS3p2WeFQqGUaijuolECZnMY+0XfQCWmyS2dbOcNmn9sfTNOjvojIyLvlvefU+b0D07aNEWD3WvZSrKba5owK0VCS4UUNgKGxYf0nQ7LQ== 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=zWzJLQ8WLVkGkxLv3qbkWA2jUDCHy4PMeUCGLkVdl7Ww5hdAWO0rIPspRrDoW812eTS20kKAsV3ceTNQT98+Q9Udnqv8b5mXXODkAmGQU+iY01duWbbqOSJSrBWc5bHlIZ29Mc026jBhS68KHbEJK4vH8ZpBW/n3c/0Y+49XMUp+D71IonZh0PMg2pjeD6YVm4od1R7zT7JxSqwDU8jsGLLvpTjpGGn4aFf3uNHPL8crwvD684PdeYONbM5dH1fu4qa5xkKT+SLK20I6Y+YtxxfB/fvWt8aZAF3cTzSYz6K0JpnTqpKbthEpy7jU77/8YERqdgJHjzaDHZ70xC8fbQ== 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=WVmw9WxWaICx4Uu9BRDVh0eaTPyf69voYvTAniFBAZy1YIsm6DgSM8085gOWSf0oaICXK2jIweHNAuIspxF7myyohnb9YjbUQ93oQSb9gNmm3OE7MqCTLan3XCcRwHx7xlAmxhT21yb2vxygL/R1HvPe9A29Qcybsx9K/QN6ogkl59F8tqAmB8baEwDoolgTJQWmGI4vPiRjBTupFADH9mNce6i7KwPq4QDPD63nN1R0yE81PyM7zA1u4Ms+exDADFcZ16N1clMaDYLlutUMcSHTnilw8r+ST3hJm+dypbQz3i83sgy9pNpbo34FRTUxVSEZYnYhlVOoiWRWYLv1cg== Received: from DU2PR04CA0155.eurprd04.prod.outlook.com (2603:10a6:10:2b0::10) by PR3PR07MB6970.eurprd07.prod.outlook.com (2603:10a6:102:5f::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.25; Mon, 14 Oct 2024 07:52:55 +0000 Received: from DB1PEPF000509E6.eurprd03.prod.outlook.com (2603:10a6:10:2b0:cafe::55) by DU2PR04CA0155.outlook.office365.com (2603:10a6:10:2b0::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.24 via Frontend Transport; Mon, 14 Oct 2024 07:52:55 +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 DB1PEPF000509E6.mail.protection.outlook.com (10.167.242.56) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.13 via Frontend Transport; Mon, 14 Oct 2024 07:52:55 +0000 Received: from seliicinfr00050.seli.gic.ericsson.se (153.88.142.248) by smtp-central.internal.ericsson.com (100.87.178.67) with Microsoft SMTP Server id 15.2.1544.11; Mon, 14 Oct 2024 09:52:54 +0200 Received: from breslau.. (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00050.seli.gic.ericsson.se (Postfix) with ESMTP id 635BB1C006B; Mon, 14 Oct 2024 09:52:54 +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 v11 3/7] eal: add lcore variable performance test Date: Mon, 14 Oct 2024 09:43:44 +0200 Message-ID: <20241014074348.821962-4-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241014074348.821962-1-mattias.ronnblom@ericsson.com> References: <20241011081901.816211-2-mattias.ronnblom@ericsson.com> <20241014074348.821962-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: DB1PEPF000509E6:EE_|PR3PR07MB6970:EE_ X-MS-Office365-Filtering-Correlation-Id: 794b7dee-a739-4a60-a85b-08dcec2536d1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|376014|36860700013|1800799024|82310400026; X-Microsoft-Antispam-Message-Info: =?utf-8?B?aFdNamFURzR0bCtsQ1owYVNCS1laTkUxUExnY2VmNzZYTDZjWGVtbzBlUkRn?= =?utf-8?B?QnRIUTFmWW5Ta3N5TWE5NHRGdllFR3R6VDIwQjlDa2xNZnRMWGZDcEVPMWV2?= =?utf-8?B?R0NwbkYvZHhJck1pUnVZRSsyeUJPcENWcEJWYjBEWWNiSEM4ODdPM0EvYkFk?= =?utf-8?B?WEtSL2x3Q1RlVnhHcUtuUGJvSFZ6UnRSZ3BmYXg0UXQwekRqaE5yRWZSclAr?= =?utf-8?B?bEFjaGtRZGwveEY2THFOMCtlc3Z2QzFPNEptUmVOcUNXRm9DL3A4eVprV0lP?= =?utf-8?B?OUpLR0VaQ3FGMEpiNlZsVXI5TzVyMDhMTVl5VkNQS2pVdDg4VjRzQmtNWUJ4?= =?utf-8?B?emhEdGZHSk8xQ1d2VkpMSHJhZUZuL3o0SERTdXowL1BrTytrZW5SU3VYVU8y?= =?utf-8?B?SGt3cUM5V1diaFVadEhhK3B2RmFKYS9JdlNqOUp2dlF6REFFQUFyUzlEQmZo?= =?utf-8?B?Q1Myc0NkZnlsZ21ray96b2lhSnpIMGpjaHRpVU1PMUpUVWUrOTB5eGt4cEI5?= =?utf-8?B?K05CT0dqSDVtNHhRS2UzWk1SRU56Nnk0NnNadHhpVWZ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GNmbEhKZVFHVUN0dzF0Z1U2WisydUZo?= =?utf-8?B?NXV2N0lic0VLU2R6a2Y4dXZSMFMvZDRrZy9ZajlxNHNETk9ZcUtSVlZMY3pn?= =?utf-8?B?c2JleUM2SEFlaUFvK1l4cG9hZ0FHbUhpbjlTa2lVUVZ5TmJkeEVnVElNcFlN?= =?utf-8?B?VEt0OXppdEZjNTBDRU5qVS9jQmU5SFczOHFGRGhhd0Y3SEZFTTNuMFNRYzhY?= =?utf-8?B?L0lIOFlBcG5VVGFFK2JPVDl4bVVpenFpaHVScDZVQ3BOdU45Tk4wN0JLN3pM?= =?utf-8?B?aGxXNVNrZmNBSG01dGcwWjRIT1hFZWx2aGt1bEpGdnhEbFVCVXl3MWVUaUc0?= =?utf-8?B?My9hS2FMemo2VDJXTmxpVjk5UDZFUzROTEMyVlZXaTJkTXNFUGdSdEJBNGsv?= =?utf-8?Q?DRxB6iaGgYL+k?= 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)(376014)(36860700013)(1800799024)(82310400026); DIR:OUT; SFP:1101; X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Oct 2024 07:52:55.6641 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 794b7dee-a739-4a60-a85b-08dcec2536d1 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: DB1PEPF000509E6.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PR3PR07MB6970 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