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 24DD4459AD; Mon, 16 Sep 2024 13:01:41 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 46E0440E1F; Mon, 16 Sep 2024 13:01:35 +0200 (CEST) Received: from EUR03-VI1-obe.outbound.protection.outlook.com (mail-vi1eur03on2075.outbound.protection.outlook.com [40.107.103.75]) by mails.dpdk.org (Postfix) with ESMTP id 2FF9F40041 for ; Mon, 16 Sep 2024 13:01:33 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=nPnEy+W//xkZZ0X9z8ITDFCbTag34Mu9q70taXlF/jpDuUdxfVJNX6SKfq6GBwb/VlbmgKxFPd1aJIN9ofeBsXc2KGqqB2SWcqwmJgxLQ303GrN7H6DfhwC6OCKBg0Y88yKHf1PAJjS5feavYPgFuWPZhDxa3xHODtuebaFntonwcXwADlE4aU9vCfg56EBujNaHptecMKaHhmndclUncxV3k3OJXyieNcfFxtxiWdZYgP7y2s7h9TVqBbJ82rNTxEYb8OfUyOvWHmrxXyP6a2Q5NvJx4r/LRmK6oroMMSovZLfyK4JrnBGtY2C2mdA2MY62O37jxtiP7BXsqis6cw== 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=Zy8ak8h4HO6nmUF4zttubY4GfLkhEODMu2Ics9aInIg=; b=Th9dC4ZYHFTGmVRynlixfFIeXYlcupMu1ahmYDpS4pPx5ohcl5rls2VTRazeL7w1heO6JeMgFVtBANyt3jEFOI776IZOGXyyuWIyIkNHqisEUM64R7fj/J7Mx1e3fQRvmru+FAuOoDLnSRKXJ3iq2Duf/lheS3JCSi3SChb7QIcm701+YB7BZSAwOJJPm/x7rCnhdR2ULw4gbzS/zBan6vzkn6YsUhU2WtIFFo/MbGrXhzmZBYydpIXBatZA0OoLuT+JapSAGvmzsh4W+yxGmpKz+xHDth9JKxpnwpfRS8egju2rwNlf/7Vwj+5IzsmGhnYF6YSEz7UppuaBxWkiKg== 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=Zy8ak8h4HO6nmUF4zttubY4GfLkhEODMu2Ics9aInIg=; b=k3q+cAnNLFDsUw/7MkYH5ju/Thm6mOpDxyFFhxvxwpuRa0gs+cdRf1HKcpUW0OOltGE9FrFFsKr5aX5EuSB30aFH0KeG8EJzPgAcp05pTQ/g52el/rbBxoLQ9PL4XFir7P5wngBvz3vlfLMaqYqQbHWz0yVWhTCqyk0OkW7k08musg8tCnD1u6eikaJyfaXBeHsuoA/serisJkl71TZT611YB78/lGNpvXT4tcYGVBlNxo7DnOyRoKUPI4mpFg2uaF7KlkvXMMNln1KDH+BAisyKHV+WX9PIRe6U+oKKFSc/p9fbZwDDLWEKJZ0PmvivJPM5U+h+wRaRD8jiG3tSeA== Received: from DUZPR01CA0247.eurprd01.prod.exchangelabs.com (2603:10a6:10:4b5::17) by VI1PR07MB6223.eurprd07.prod.outlook.com (2603:10a6:800:137::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.24; Mon, 16 Sep 2024 11:01:31 +0000 Received: from DB5PEPF00014B92.eurprd02.prod.outlook.com (2603:10a6:10:4b5:cafe::c0) by DUZPR01CA0247.outlook.office365.com (2603:10a6:10:4b5::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.30 via Frontend Transport; Mon, 16 Sep 2024 11:01:31 +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 DB5PEPF00014B92.mail.protection.outlook.com (10.167.8.230) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.13 via Frontend Transport; Mon, 16 Sep 2024 11:01:30 +0000 Received: from seliicinfr00049.seli.gic.ericsson.se (153.88.142.248) by smtp-central.internal.ericsson.com (100.87.178.65) with Microsoft SMTP Server id 15.2.1544.11; Mon, 16 Sep 2024 13:01:28 +0200 Received: from breslau.. (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00049.seli.gic.ericsson.se (Postfix) with ESMTP id 6E069380061; Mon, 16 Sep 2024 13:01:28 +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 v4 3/7] eal: add lcore variable performance test Date: Mon, 16 Sep 2024 12:52:06 +0200 Message-ID: <20240916105210.721315-4-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240916105210.721315-1-mattias.ronnblom@ericsson.com> References: <20240912084429.703405-2-mattias.ronnblom@ericsson.com> <20240916105210.721315-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: DB5PEPF00014B92:EE_|VI1PR07MB6223:EE_ X-MS-Office365-Filtering-Correlation-Id: 181abd92-2981-4c21-c72c-08dcd63eeb81 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|1800799024|376014|82310400026|36860700013; X-Microsoft-Antispam-Message-Info: =?utf-8?B?NmI4Ymt3dDFkNHN2a1diMnRMZ2t0bmpFL2ZGMUp0YkJSS2h5Wjhsby93aWtX?= =?utf-8?B?RkZLVDFOMnVXc0NySnhNMmJhbHgrU3lPeTVIKzQ1MzNMUVZoTVlhbUlDeWdH?= =?utf-8?B?SWprZGhYcW14WlpGOXBobDJZcEp1V0FzMlV5MllWRFdYb0pTWUtLQ3dLdGlS?= =?utf-8?B?UmdwOTdvanpjZEtlM1hxaSt2MEVHMExsRVZXMUJ6QTZrVWpyYzM1UzJMMmsw?= =?utf-8?B?Q2VHemNHeFcrQjNmRjd6NDhydGJRdlNvNW5sK045N0FZZnBpUHk4TGYvUVRG?= =?utf-8?B?MWtmUG0vaXp5SFM0cFFFRWNFTVdqcXV2eDlSVC9lMUpjS3c1Y2tnWDcwV3V3?= =?utf-8?B?NEJENHNDTksyNndvZE45Y2FVclpndWdibEF4eDFwY0xTcVlmNmszWjcrTEFU?= =?utf-8?B?VGQ2TktKUzBiaE5CenBnbmxQQ1Y0VlRjTWVHTTRpT2tpc0c3azI0cTNZSmxm?= =?utf-8?B?dW0vUURRR09oVEJkNmxhMmY3Rm8zVFNIaTE2eVovNy9KLzJBL0xqUVlIQmVI?= =?utf-8?B?eHhhSm8rcjgreFhuZEpNSWk1TmE5akpsdndmZ09EOGhmejl3dlNUWHFLQk1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ittdnk2bUNDTFdQSnBzZmo3?= =?utf-8?B?T0lnR244YkYvb1MyUXB4V0gyQmxaaVlpa3piNWNlbXlCcWQrQVVQUlpUNisz?= =?utf-8?B?NkNaNWZwYkNIQmF5STlPV2JTeEZ3dnMzRzhkYjhsOEdxVGFSVkl4ejV3Y1F0?= =?utf-8?B?OEJCRjNid1hEalZOVkVESkRoL3RLTE1MRkozMFNhZVVCWDZaNU5zRzhMNmV4?= =?utf-8?B?RTJmeDJ6NWJVcmYvN2VXRCtDcXlNaytqMDcrNnV5Q0x0Nm5DbmZRZHErc3ZZ?= =?utf-8?B?WWV6Z0pPc09OVFBJZjNOVUN6ZWx5VHpEMmFBOWdaZFFRWmZ0a2FxR1o2UVBj?= =?utf-8?B?ZUE2cWdkb1ozUHlicm5nR2UrdU1YbDJrRWFjNnQxSDJnWXM1YkVCbFEvVmZF?= =?utf-8?Q?9eG/nvdkJb27E?= 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)(1800799024)(376014)(82310400026)(36860700013); DIR:OUT; SFP:1101; X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Sep 2024 11:01:30.6453 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 181abd92-2981-4c21-c72c-08dcd63eeb81 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: DB5PEPF00014B92.eurprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR07MB6223 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 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 | 244 +++++++++++++++++++++++++++++++++ 2 files changed, 245 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..8b0abc771c --- /dev/null +++ b/app/test/test_lcore_var_perf.c @@ -0,0 +1,244 @@ +/* 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_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); +} + +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) / ITERATIONS; + + return latency; +} + +static void +test_lcore_var_access_n(unsigned int num_mods) +{ + double sarray_latency; + double 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); + + lvar_latency = + benchmark_access(mods, num_mods, lvar_init, lvar_update); + + printf("%17u %13.1f %13.1f %16.1f\n", num_mods, sarray_latency, + 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("Modules/Variables Static array Thread-local Storage " + "Lcore 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