From patchwork Mon Dec 5 10:03:34 2022 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: 120453 X-Patchwork-Delegate: david.marchand@redhat.com 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 D4D09A0542; Mon, 5 Dec 2022 11:08:17 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 8006540F18; Mon, 5 Dec 2022 11:08:17 +0100 (CET) Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-db3eur04on2040.outbound.protection.outlook.com [40.107.6.40]) by mails.dpdk.org (Postfix) with ESMTP id 72C8C40A87 for ; Mon, 5 Dec 2022 11:08:15 +0100 (CET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MmtbbUSRerbI7q5ZExrnRpJ905ogI/f764Ss4PkSpji1IgMzGlftBQ7Sox7PtNgU+Uy55CwqZCv0+9kyHrVVZOTAh93XTgqepPWGoVJMqi3MUWJj8grE77ghFLaCbImWZD7OfE75a4j9Iom5Zg2QBVJ0FLRVHVMmaikY20NFYs72KEtI4cMTXYuXd/uru0FmQur/cuJTABLd8adQxDapZOlC4mW2tCh2doSLta92LjOhIRD5bffuRE/rPQgcxpG0snn/qajQZMfDdoF/LsnE1+Iwhad1N2Ef4Fa8xXFSdIBgR335k+W5okmQ93PBgppw7LeRmlL/ERtufqkhbJMIwg== 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=Sb9cDatYZKeo9qk9xD9zh3yS6bO1DaSubhio6Fsq2QU=; b=WfDph7V+pD2CMSeGmVz+vf/woQnhg6CeluzaSOdVDSJEHvBKMOoQON/Raq3kpCADMM0P2Vv697oszwJaWK7ouJPaA2bnP5Sjn4h40F8/cqIlagNGzbLYkEY/oNSnM2peJN5nSQkXorwxkbvtX6VMDYRVKbCOY0/bavdXI5NCC0JSEatquNO7vO3MaHDnahIXDOzf7g7H+ZvCAsD9mRlEczlPgQM4uI4Tb/0CLLhUoJ4d7FxaJoMMbWEVRvnGkt0CoPiiTuRNIxScFxQxTVzLmKfTt/t/RM9+j2nu61X9cTLipVN+mo2FCC30jB+ZwO/t2/9yGmc1+o0Xjr3z6iHYbg== 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 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=Sb9cDatYZKeo9qk9xD9zh3yS6bO1DaSubhio6Fsq2QU=; b=oab5A9MgpcFGbFs3jc4iJc+wQwvPhx3SgsSXuc/9Mm7a540liaogLc8Tm4l1qxzHC5+PYc+gZJjpuQuJvkmbB0WqmwxrPLLUPZmyjrXeQ893HCUHNjaY57wZ650i7rQCypPPX2FzzwOMZ5oVKutnSQdp72Au9/X3vqzyN0oQciE= Received: from AS9PR06CA0159.eurprd06.prod.outlook.com (2603:10a6:20b:45c::26) by AS8PR07MB9378.eurprd07.prod.outlook.com (2603:10a6:20b:61a::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.13; Mon, 5 Dec 2022 10:08:14 +0000 Received: from AM0EUR02FT029.eop-EUR02.prod.protection.outlook.com (2603:10a6:20b:45c:cafe::a) by AS9PR06CA0159.outlook.office365.com (2603:10a6:20b:45c::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.13 via Frontend Transport; Mon, 5 Dec 2022 10:08:14 +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 AM0EUR02FT029.mail.protection.outlook.com (10.13.54.155) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id 15.20.5880.14 via Frontend Transport; Mon, 5 Dec 2022 10:08:14 +0000 Received: from ESESSMB501.ericsson.se (153.88.183.162) by ESESSMR505.ericsson.se (153.88.183.127) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2375.34; Mon, 5 Dec 2022 11:08:13 +0100 Received: from seliicinfr00049.seli.gic.ericsson.se (153.88.183.153) by smtp.internal.ericsson.com (153.88.183.189) with Microsoft SMTP Server id 15.1.2375.34 via Frontend Transport; Mon, 5 Dec 2022 11:08:13 +0100 Received: from localhost.localdomain (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00049.seli.gic.ericsson.se (Postfix) with ESMTP id BB0C6380061; Mon, 5 Dec 2022 11:08:13 +0100 (CET) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: Thomas Monjalon , David Marchand CC: , =?utf-8?q?Mattias_R=C3=B6nnblom?= Subject: [PATCH] eal: have unregistered non-EAL threads use dedicated PRNG Date: Mon, 5 Dec 2022 11:03:34 +0100 Message-ID: <20221205100334.3500-1-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM0EUR02FT029:EE_|AS8PR07MB9378:EE_ X-MS-Office365-Filtering-Correlation-Id: 1e1f9575-0df0-479b-368b-08dad6a89f4c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: DhUghQQKH2fnJ/U09v3wEo8CamfjpD/UKQ516AM4kVB82cIvnbRss47G+OWMeB0nSN+wJfKfxBNlZMwodjHm/prHIGhTvUdoBsGpgw0cdy9qUVzFpMsn9D7F3hrVI6mkzcdinwjFrM85GEUxckkNzCGQabFicP0sdhxRbBndtDH0/33kWbV4JI8doidf+d+5LHeW51rc2AP+sP+SPSNLIvO24vDm5ggZbqSuc3EromLOqiaeDPULp2mPLRLqQ29rwCEBTIPxLvTM7mLHH0M3GtvDxDbnBbbRfV34VRYSoCESe76fI1llZa0cC10jrunlXmt8qITBQakk4IIFSy166jma0+f/VynihEToWNFhSqBA3upK46JNfMEWOvXb2AAcSNX1puylmWqM8BII2zBDuvcLl/HZwWo3XKNEGcYBXNUUANKaTJ7q9I0IfbegLVm/fYIT7jR+EpOIvDdEMipqCGvRWhDK4zb2nEOLIUeVrAVPPx/4xmZ/57P6EGJlVXMk4ACPWE3fGPrN1vqpKPHDGajBhI9cXqETR8LLDLmjDCvWhiHm6gmiknajo6ZCE8YXTHPlGX9y3BWt5wuhzncFUbVU7z1OBAgAtjqVkxFibwp1w+/33saUHOGfMMQFFeTGOD0tQueZNwwvPoMUUvbOBBU64mxBrKxXkDaJbQClx7rBqHITwmB/6NzBNtn2GIogGOn1drT++7yp1zQeuy1aYgGJCns797UjykzvzPZA5uOobiQt5Q568aASWkBaaEaO 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:(13230022)(4636009)(396003)(376002)(136003)(39860400002)(346002)(451199015)(46966006)(40470700004)(36840700001)(2906002)(86362001)(47076005)(336012)(83380400001)(186003)(316002)(110136005)(6266002)(54906003)(1076003)(26005)(107886003)(6666004)(41300700001)(40460700003)(2616005)(478600001)(7636003)(356005)(82740400003)(82960400001)(8936002)(5660300002)(4326008)(36860700001)(70206006)(8676002)(70586007)(82310400005)(40480700001)(36756003); DIR:OUT; SFP:1101; X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Dec 2022 10:08:14.1299 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1e1f9575-0df0-479b-368b-08dad6a89f4c 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: AM0EUR02FT029.eop-EUR02.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR07MB9378 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 Prior to this change, unregistered non-EAL threads shared a PRNG instance with the main lcore. The main lcore may well be used for fast path processing, potentially making rte_rand() calls in the process. It should not need to synchronize with control threads. With this change, all unregistered non-EAL threads share one dedicated PRNG instance. The API documentation is updated to use the proper terminology when referring to threads equipped with an lcore id. Signed-off-by: Mattias Rönnblom Acked-by: Morten Brørup Acked-by: Morten Brørup --- lib/eal/common/rte_random.c | 17 +++++++++++------ lib/eal/include/rte_random.h | 10 +++++++--- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/eal/common/rte_random.c b/lib/eal/common/rte_random.c index 166b0d8921..565f2401ce 100644 --- a/lib/eal/common/rte_random.c +++ b/lib/eal/common/rte_random.c @@ -20,7 +20,11 @@ struct rte_rand_state { uint64_t z5; } __rte_cache_aligned; -static struct rte_rand_state rand_states[RTE_MAX_LCORE]; +/* 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]; static uint32_t __rte_rand_lcg32(uint32_t *seed) @@ -114,14 +118,15 @@ __rte_rand_lfsr258(struct rte_rand_state *state) static __rte_always_inline struct rte_rand_state *__rte_rand_get_state(void) { - unsigned int lcore_id; + unsigned int idx; - lcore_id = rte_lcore_id(); + idx = rte_lcore_id(); - if (unlikely(lcore_id == LCORE_ID_ANY)) - lcore_id = rte_get_main_lcore(); + /* last instance reserved for unregistered non-EAL threads */ + if (unlikely(idx == LCORE_ID_ANY)) + idx = RTE_MAX_LCORE; - return &rand_states[lcore_id]; + return &rand_states[idx]; } uint64_t diff --git a/lib/eal/include/rte_random.h b/lib/eal/include/rte_random.h index d90e4d2192..2edf5d210b 100644 --- a/lib/eal/include/rte_random.h +++ b/lib/eal/include/rte_random.h @@ -41,7 +41,8 @@ rte_srand(uint64_t seedval); * * The generator is not cryptographically secure. * - * If called from lcore threads, this function is thread-safe. + * If called from EAL threads or registered non-EAL threads, this function + * is thread-safe. * * @return * A pseudo-random value between 0 and (1<<64)-1. @@ -55,7 +56,8 @@ rte_rand(void); * This function returns an uniformly distributed (unbiased) random * number less than a user-specified maximum value. * - * If called from lcore threads, this function is thread-safe. + * If called from EAL threads or registered non-EAL threads, this function + * is thread-safe. * * @param upper_bound * The upper bound of the generated number. @@ -75,7 +77,9 @@ rte_rand_max(uint64_t upper_bound); * number uniformly distributed over the interval [0.0, 1.0). * * The generator is not cryptographically secure. - * If called from lcore threads, this function is thread-safe. + * + * If called from EAL threads or registered non-EAL threads, this function + * is thread-safe. * * @return * A pseudo-random value between 0 and 1.0.