From patchwork Tue Feb 20 08:49:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Mattias_R=C3=B6nnblom?= X-Patchwork-Id: 136892 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id C7C7C43B51; Tue, 20 Feb 2024 09:57:19 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id AF70440A6E; Tue, 20 Feb 2024 09:57:11 +0100 (CET) Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05on2059.outbound.protection.outlook.com [40.107.20.59]) by mails.dpdk.org (Postfix) with ESMTP id 956D34064A for ; Tue, 20 Feb 2024 09:57:08 +0100 (CET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aAaQf1FL6ZVPKr0+3U5EJLKhQWGLMCS8cFoh7LtWbIqmwjydF6+pAu9c+iKxkP7LKgEkU4PraP20ReWRX/8mv08B3FphpaCSshkAl1Q1VfKecTi0u1iTU2BmkGa97DFiNW29Mdy34j838v1CFaJRNUrQtPLJBr9rWlvao2VwlvjgmIDe2kxn4xNoSOiYyEyJW/Fcf5rqXdIsS1BQiB4OZBUVtS5sfQB+hQrtjOJul145axVylSk8VuaOF5MDW9WOoe/HZf9QdU88QWbU8yjciz8CFnGBF6sSBedy7eHBz8r5YiTq9GiAhCFYlEbPTILAEIjvqxjuSj8ci7HKqU1GUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=8lBGZUWMvJ+xi/hRt3dkkJhhf0E7azD832S+1BFYhIY=; b=WkR0uFpC5cVpJtITwUzZKrMU5ZZwQ4/YZdAOQWgPYzKEGJpjb82jElnJCEZDKektlJGwuuUN02tOehbIg6NP7hejLi5h7zVzjzRtg89KHXe/86e32Dw6lbcpLmnvE4ZeUcwcqVPKmhKqbmI0NA69Zs7yZ5Y5yn9ErjUd5BtsXqUKxIWR07EaMAlkRHveDRQdrqqoaqCU3ZGdEM7+cf8+fC/rBg8YLX7FiwNSna3FqKejTqAwc5E7dei68QIcRTBrzfd7EkDjs3k/NjqqrjF4BLjnbeX1jIRG8a8HbwupbN44q7NUH4NQS0e2VayFJqpxLKrxx+bNFn8NlhVB1t9Pqg== 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=8lBGZUWMvJ+xi/hRt3dkkJhhf0E7azD832S+1BFYhIY=; b=wxBG5wp2kgQJAh5MOjlDx11oexSU9KF+KnOscBpJrCV3XnNWOoB3Bxijv6ws4qQd/f0yf382GwOFSQgxEnbXXdAl12BFBSRr6LcrM+IX17/qJSFXqLiBaWhoxzA0GuKA/hOrQK59U5Ot4MeN5WyHqBZYAYrPRF74KSwEtbWNY4Sdfu7AhpkE5J3HmlA3uCb7jfsc0vDlVJgFhwOl1+TZ6/Ax4Wzbfk3ZGecdqBZR+Mb1cgDNgoSM05KKjX0mWihnkIJSQmIi9MA5u4MLwcaF6UX9ZVBqy8RASL2BhgJa4JfH1EEIBu51Atqr2fgcLUL1QVo1Jx8AoAfePoq+dRI2Kw== Received: from AS4P192CA0044.EURP192.PROD.OUTLOOK.COM (2603:10a6:20b:658::8) by AS2PR07MB9025.eurprd07.prod.outlook.com (2603:10a6:20b:555::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7292.39; Tue, 20 Feb 2024 08:57:07 +0000 Received: from AM4PEPF00027A63.eurprd04.prod.outlook.com (2603:10a6:20b:658:cafe::91) by AS4P192CA0044.outlook.office365.com (2603:10a6:20b:658::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7292.39 via Frontend Transport; Tue, 20 Feb 2024 08:57:07 +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 AM4PEPF00027A63.mail.protection.outlook.com (10.167.16.73) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7292.25 via Frontend Transport; Tue, 20 Feb 2024 08:57:06 +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.1258.12; Tue, 20 Feb 2024 09:57:05 +0100 Received: from breslau.. (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00050.seli.gic.ericsson.se (Postfix) with ESMTP id C5F851C006A; Tue, 20 Feb 2024 09:57:05 +0100 (CET) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , =?utf-8?q?Morten_Br=C3=B8rup?= , Stephen Hemminger , =?utf-8?q?Mattias_R=C3=B6nn?= =?utf-8?q?blom?= Subject: [RFC v3 3/6] random: keep PRNG state in lcore variable Date: Tue, 20 Feb 2024 09:49:05 +0100 Message-ID: <20240220084908.488252-4-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240220084908.488252-1-mattias.ronnblom@ericsson.com> References: <20240219094036.485727-2-mattias.ronnblom@ericsson.com> <20240220084908.488252-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM4PEPF00027A63:EE_|AS2PR07MB9025:EE_ X-MS-Office365-Filtering-Correlation-Id: 706e2e13-9a46-48a4-fce4-08dc31f1ea31 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 7tkW+qejfA/4XC8YHiYIsHxJjX/429go+bhaJxkBpKBTO1BCIfkmhAvu4FQm6Ea2H9iash90zpEyxY+TH3SFF4urUGZrRI/jr0nzoq8dxCWDrMZEa/T2JUjJEYIrKHDLVKqPS0dMHtAfSFbWeYSl+8/FdGehDICaNQLomcLVI5AKVjk//9bOOimIhJHUR4gTP7FCR467jsl6dcHPgdRLygdz9kK/CjUlYl1zcqrr8xXd5a/aAne9AsBdSGDumOaSo/KSpFruVyyuVIDMfr31infB/NuOdbsFpx0U6HP7Q7XuvHfDffAQJyP12jzuZAY66XDt+vlDM0XhRtQw2SCgcTMw7ZyQsnmO87NDx2rvWkOUTkrqVAIXAep2kkMsNzCfhN2Xnzy73qckHadAMQH2Hm7NBL6/LKAje5qLfejTwr/g94ZTYp4x6xFtFKDvhHwYDL7qMZ4PaDLbEdD7P6bCv6lTOiEjrQB+ow5POi2WaB1rZJUo1IzRS5Y3PoPWah9yMbfVFI5bPgGwsmwbqYhRFjAs3+EIgLF8QBicsh0V6DUM7/3ZfGys9I+MJmh5c5lqs+O/vx/HgQ9LnEBG0ndY4hmF4n3zfaShT9aY1lwiaLpx+TmIgb4Ii8wOkVt5EHTdNkd81fw1BG5TGBqgjefe1w== 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:(13230031)(36860700004)(40470700004)(46966006); DIR:OUT; SFP:1101; X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2024 08:57:06.5166 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 706e2e13-9a46-48a4-fce4-08dc31f1ea31 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: AM4PEPF00027A63.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS2PR07MB9025 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 Replace keeping PRNG state in a RTE_MAX_LCORE-sized static array of cache-aligned and RTE_CACHE_GUARDed struct instances with keeping the same state in a more cache-friendly lcore variable. RFC v3: * Remove cache alignment on unregistered threads' rte_rand_state. (Morten Brørup) Signed-off-by: Mattias Rönnblom Acked-by: Morten Brørup --- lib/eal/common/rte_random.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/eal/common/rte_random.c b/lib/eal/common/rte_random.c index 7709b8f2c6..adbbf13f0e 100644 --- a/lib/eal/common/rte_random.c +++ b/lib/eal/common/rte_random.c @@ -11,6 +11,7 @@ #include #include #include +#include #include struct rte_rand_state { @@ -19,14 +20,12 @@ struct rte_rand_state { uint64_t z3; uint64_t z4; uint64_t z5; - RTE_CACHE_GUARD; -} __rte_cache_aligned; +}; -/* One instance each for every lcore id-equipped thread, and one - * additional instance to be shared by all others threads (i.e., all - * unregistered non-EAL threads). - */ -static struct rte_rand_state rand_states[RTE_MAX_LCORE + 1]; +RTE_LCORE_VAR_HANDLE(struct rte_rand_state, rand_state); + +/* instance to be shared by all unregistered non-EAL threads */ +static struct rte_rand_state unregistered_rand_state; static uint32_t __rte_rand_lcg32(uint32_t *seed) @@ -85,8 +84,14 @@ rte_srand(uint64_t seed) unsigned int lcore_id; /* add lcore_id to seed to avoid having the same sequence */ - for (lcore_id = 0; lcore_id < RTE_DIM(rand_states); lcore_id++) - __rte_srand_lfsr258(seed + lcore_id, &rand_states[lcore_id]); + for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { + struct rte_rand_state *lcore_state = + RTE_LCORE_VAR_LCORE_PTR(lcore_id, rand_state); + + __rte_srand_lfsr258(seed + lcore_id, lcore_state); + } + + __rte_srand_lfsr258(seed + lcore_id, &unregistered_rand_state); } static __rte_always_inline uint64_t @@ -124,11 +129,10 @@ struct rte_rand_state *__rte_rand_get_state(void) idx = rte_lcore_id(); - /* last instance reserved for unregistered non-EAL threads */ if (unlikely(idx == LCORE_ID_ANY)) - idx = RTE_MAX_LCORE; + return &unregistered_rand_state; - return &rand_states[idx]; + return RTE_LCORE_VAR_PTR(rand_state); } uint64_t @@ -228,6 +232,8 @@ RTE_INIT(rte_rand_init) { uint64_t seed; + RTE_LCORE_VAR_ALLOC(rand_state); + seed = __rte_random_initial_seed(); rte_srand(seed);