From patchwork Thu May 16 17:55:05 2019 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: 53485 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 571895F2A; Thu, 16 May 2019 19:55:26 +0200 (CEST) Received: from sessmg23.ericsson.net (sessmg23.ericsson.net [193.180.251.45]) by dpdk.org (Postfix) with ESMTP id EF4365B20 for ; Thu, 16 May 2019 19:55:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1558029318; x=1560621318; h=From:Sender:Reply-To:Subject:Date:Message-ID:To:CC:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=d8IrLP7hWL5S+xfzhDw/zKwOPIKdITQcZEqq2N7axeU=; b=SJQzs8BVEX3R8pR4ysH2SVaQp01xilQ6gflxIS/D3bCxSnUGAtK5xVQFuILIx837 L6MBdQLFJMBPJmNGpcOe4/4YdU/8nAbjPhIgbWccyWLgfOzNWR/O3dzyTBAgRuI+ wz8UjGEEVkBmlYI5mWlPyecXpzXBmM2Lv/P1bbzsso8=; X-AuditID: c1b4fb2d-17dff70000001a6d-6a-5cdda4068800 Received: from ESESSMB501.ericsson.se (Unknown_Domain [153.88.183.119]) by sessmg23.ericsson.net (Symantec Mail Security) with SMTP id 78.27.06765.604ADDC5; Thu, 16 May 2019 19:55:18 +0200 (CEST) Received: from ESESSMB502.ericsson.se (153.88.183.163) by ESESSMB501.ericsson.se (153.88.183.162) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Thu, 16 May 2019 19:55:18 +0200 Received: from selio1a020.lmera.ericsson.se (153.88.183.153) by smtp.internal.ericsson.com (153.88.183.190) with Microsoft SMTP Server id 15.1.1713.5 via Frontend Transport; Thu, 16 May 2019 19:55:18 +0200 Received: from breslau.lmera.ericsson.se (breslau.lmera.ericsson.se [150.132.109.241]) by selio1a020.lmera.ericsson.se (8.15.1+Sun/8.15.1) with ESMTP id x4GHtHct023701; Thu, 16 May 2019 19:55:18 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , , =?utf-8?q?Mattias_R=C3=B6nnblom?= Date: Thu, 16 May 2019 19:55:05 +0200 Message-ID: <20190516175510.27154-2-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190516175510.27154-1-mattias.ronnblom@ericsson.com> References: <20190514092046.30808-1-mattias.ronnblom@ericsson.com> <20190516175510.27154-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrOLMWRmVeSWpSXmKPExsUyM2J7uS7bkrsxBlMuWFvcWGVv8e7TdiaL xXfkHJg9fi1YyuqxeM9LJo+ek/OYApijuGxSUnMyy1KL9O0SuDLWvZ3NXvDeo+LwApEGxu02 XYycHBICJhLzzq1k62Lk4hASOMoocW5tBxOE841RYv6Fs6wQzkVGidvzFrFAOJcZJc7Pb2QH 6WcT8JSY/K6bBcQWERCSWPrxMjtIEbNAK6PEjoeNrCAJYYEgieeP74LZLAKqEqtXr2QEsXkF nCSO9XxngzhEXmL1hgPMIDangLPE1fMHwOqFBKolfnatYYKoF5Q4OfMJ2DJmAU2J1u2/2SFs eYnmrbOZIeq1JO4v+cI8gVFoFpKWWUhaZiFpWcDIvIpRtDi1uDg33chYL7UoM7m4OD9PLy+1 ZBMjMLgPbvmtu4Nx9WvHQ4wCHIxKPLzf59yNEWJNLCuuzD3EKMHBrCTCu+H97Rgh3pTEyqrU ovz4otKc1OJDjNIcLErivNGr98QICaQnlqRmp6YWpBbBZJk4OKUaGDt6nW7MPXN3nS/blPfh PNfSEk+ePP+13EOn1mjeVo1/HxZVny9JT1UJVjrRdnbj0rVhE9uS7DO7+vouuy8TFvVimc+z w/jkh1cqEYErI/+c+JVpe/poeOhjpT4d5cZFBusezWh/YxHImpzOJOTNI/N/Sm9MplkFlyHr 7Qh9mbiT6pIs3dp3lViKMxINtZiLihMBOkCBZWoCAAA= Subject: [dpdk-dev] [PATCH v2 1/6] eal: replace libc-based random number generation with LFSR X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" This commit replaces rte_rand()'s use of lrand48() with a DPDK-native combined Linear Feedback Shift Register (LFSR) (also known as Tausworthe) pseudo-random number generator. This generator is faster and produces better-quality random numbers than the linear congruential generator (LCG) of lib's lrand48(). The implementation, as opposed to lrand48(), is multi-thread safe in regards to concurrent rte_rand() calls from different lcore threads. A LCG is still used, but only to seed the five per-lcore LFSR sequences. In addition, this patch also addresses the issue of the legacy implementation only producing 62 bits of pseudo randomness, while the API requires all 64 bits to be random. This pseudo-random number generator is not cryptographically secure - just like lrand48(). Bugzilla ID: 114 Bugzilla ID: 276 Signed-off-by: Mattias Rönnblom --- lib/librte_eal/common/include/rte_random.h | 29 ++--- lib/librte_eal/common/meson.build | 1 + lib/librte_eal/common/rte_random.c | 139 +++++++++++++++++++++ lib/librte_eal/freebsd/eal/Makefile | 1 + lib/librte_eal/freebsd/eal/eal.c | 2 - lib/librte_eal/linux/eal/Makefile | 1 + lib/librte_eal/linux/eal/eal.c | 2 - lib/librte_eal/rte_eal_version.map | 8 ++ 8 files changed, 161 insertions(+), 22 deletions(-) create mode 100644 lib/librte_eal/common/rte_random.c diff --git a/lib/librte_eal/common/include/rte_random.h b/lib/librte_eal/common/include/rte_random.h index b2ca1c209..66dfe8ae7 100644 --- a/lib/librte_eal/common/include/rte_random.h +++ b/lib/librte_eal/common/include/rte_random.h @@ -16,7 +16,6 @@ extern "C" { #endif #include -#include /** * Seed the pseudo-random generator. @@ -25,34 +24,28 @@ extern "C" { * value. It may need to be re-seeded by the user with a real random * value. * + * This function is not multi-thread safe in regards to other + * rte_srand() calls, nor is it in relation to concurrent rte_rand() + * calls. + * * @param seedval * The value of the seed. */ -static inline void -rte_srand(uint64_t seedval) -{ - srand48((long)seedval); -} +void +rte_srand(uint64_t seedval); /** * Get a pseudo-random value. * - * This function generates pseudo-random numbers using the linear - * congruential algorithm and 48-bit integer arithmetic, called twice - * to generate a 64-bit value. + * The generator is not cryptographically secure. + * + * If called from lcore threads, this function is thread-safe. * * @return * A pseudo-random value between 0 and (1<<64)-1. */ -static inline uint64_t -rte_rand(void) -{ - uint64_t val; - val = (uint64_t)lrand48(); - val <<= 32; - val += (uint64_t)lrand48(); - return val; -} +uint64_t +rte_rand(void); #ifdef __cplusplus } diff --git a/lib/librte_eal/common/meson.build b/lib/librte_eal/common/meson.build index 0670e4102..bafd23207 100644 --- a/lib/librte_eal/common/meson.build +++ b/lib/librte_eal/common/meson.build @@ -35,6 +35,7 @@ common_sources = files( 'rte_keepalive.c', 'rte_malloc.c', 'rte_option.c', + 'rte_random.c', 'rte_reciprocal.c', 'rte_service.c' ) diff --git a/lib/librte_eal/common/rte_random.c b/lib/librte_eal/common/rte_random.c new file mode 100644 index 000000000..4d3cf5226 --- /dev/null +++ b/lib/librte_eal/common/rte_random.c @@ -0,0 +1,139 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2019 Ericsson AB + */ + +#include + +#include +#include +#include +#include +#include +#include + +struct rte_rand_state { + uint64_t z1; + uint64_t z2; + uint64_t z3; + uint64_t z4; + uint64_t z5; +} __rte_cache_aligned; + +static struct rte_rand_state rand_states[RTE_MAX_LCORE]; + +static uint32_t +__rte_rand_lcg32(uint32_t *seed) +{ + *seed = 1103515245U * *seed + 12345U; + + return *seed; +} + +static uint64_t +__rte_rand_lcg64(uint32_t *seed) +{ + uint64_t low; + uint64_t high; + + /* A 64-bit LCG would have been much cleaner, but good + * multiplier/increments for such seem hard to come by. + */ + + low = __rte_rand_lcg32(seed); + high = __rte_rand_lcg32(seed); + + return low | (high << 32); +} + +static uint64_t +__rte_rand_lfsr258_gen_seed(uint32_t *seed, uint64_t min_value) +{ + uint64_t res; + + res = __rte_rand_lcg64(seed); + + if (res < min_value) + res += min_value; + + return res; +} + +static void +__rte_srand_lfsr258(uint64_t seed, struct rte_rand_state *state) +{ + uint32_t lcg_seed; + + lcg_seed = (uint32_t)(seed ^ (seed >> 32)); + + state->z1 = __rte_rand_lfsr258_gen_seed(&lcg_seed, 2UL); + state->z2 = __rte_rand_lfsr258_gen_seed(&lcg_seed, 512UL); + state->z3 = __rte_rand_lfsr258_gen_seed(&lcg_seed, 4096UL); + state->z4 = __rte_rand_lfsr258_gen_seed(&lcg_seed, 131072UL); + state->z5 = __rte_rand_lfsr258_gen_seed(&lcg_seed, 8388608UL); +} + +void +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_MAX_LCORE; lcore_id++) + __rte_srand_lfsr258(seed + lcore_id, &rand_states[lcore_id]); +} + +static __rte_always_inline uint64_t +__rte_rand_lfsr258_comp(uint64_t z, uint64_t a, uint64_t b, uint64_t c, + uint64_t d) +{ + return ((z & c) << d) ^ (((z << a) ^ z) >> b); +} + +/* Based on L’Ecuyer, P.: Tables of maximally equidistributed combined + * LFSR generators. + */ + +static __rte_always_inline uint64_t +__rte_rand_lfsr258(struct rte_rand_state *state) +{ + state->z1 = __rte_rand_lfsr258_comp(state->z1, 1UL, 53UL, + 18446744073709551614UL, 10UL); + state->z2 = __rte_rand_lfsr258_comp(state->z2, 24UL, 50UL, + 18446744073709551104UL, 5UL); + state->z3 = __rte_rand_lfsr258_comp(state->z3, 3UL, 23UL, + 18446744073709547520UL, 29UL); + state->z4 = __rte_rand_lfsr258_comp(state->z4, 5UL, 24UL, + 18446744073709420544UL, 23UL); + state->z5 = __rte_rand_lfsr258_comp(state->z5, 3UL, 33UL, + 18446744073701163008UL, 8UL); + + return state->z1 ^ state->z2 ^ state->z3 ^ state->z4 ^ state->z5; +} + +static __rte_always_inline +struct rte_rand_state *__rte_rand_get_state(void) +{ + unsigned int lcore_id; + + lcore_id = rte_lcore_id(); + + if (unlikely(lcore_id == LCORE_ID_ANY)) + lcore_id = rte_get_master_lcore(); + + return &rand_states[lcore_id]; +} + +uint64_t +rte_rand(void) +{ + struct rte_rand_state *state; + + state = __rte_rand_get_state(); + + return __rte_rand_lfsr258(state); +} + +RTE_INIT(rte_rand_init) +{ + rte_srand(rte_get_timer_cycles()); +} diff --git a/lib/librte_eal/freebsd/eal/Makefile b/lib/librte_eal/freebsd/eal/Makefile index 19854ee2c..ca616c480 100644 --- a/lib/librte_eal/freebsd/eal/Makefile +++ b/lib/librte_eal/freebsd/eal/Makefile @@ -69,6 +69,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += malloc_mp.c SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_keepalive.c SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_option.c SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_service.c +SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_random.c SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_reciprocal.c # from arch dir diff --git a/lib/librte_eal/freebsd/eal/eal.c b/lib/librte_eal/freebsd/eal/eal.c index c6ac9028f..5d43310b3 100644 --- a/lib/librte_eal/freebsd/eal/eal.c +++ b/lib/librte_eal/freebsd/eal/eal.c @@ -727,8 +727,6 @@ rte_eal_init(int argc, char **argv) #endif } - rte_srand(rte_rdtsc()); - /* in secondary processes, memory init may allocate additional fbarrays * not present in primary processes, so to avoid any potential issues, * initialize memzones first. diff --git a/lib/librte_eal/linux/eal/Makefile b/lib/librte_eal/linux/eal/Makefile index 6e5261152..729795a10 100644 --- a/lib/librte_eal/linux/eal/Makefile +++ b/lib/librte_eal/linux/eal/Makefile @@ -77,6 +77,7 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += malloc_mp.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_keepalive.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_option.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_service.c +SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_random.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_reciprocal.c # from arch dir diff --git a/lib/librte_eal/linux/eal/eal.c b/lib/librte_eal/linux/eal/eal.c index 161399619..d6bf0e89e 100644 --- a/lib/librte_eal/linux/eal/eal.c +++ b/lib/librte_eal/linux/eal/eal.c @@ -1083,8 +1083,6 @@ rte_eal_init(int argc, char **argv) #endif } - rte_srand(rte_rdtsc()); - if (rte_eal_log_init(logid, internal_config.syslog_facility) < 0) { rte_eal_init_alert("Cannot init logging."); rte_errno = ENOMEM; diff --git a/lib/librte_eal/rte_eal_version.map b/lib/librte_eal/rte_eal_version.map index 245493461..e615d7cb9 100644 --- a/lib/librte_eal/rte_eal_version.map +++ b/lib/librte_eal/rte_eal_version.map @@ -287,6 +287,14 @@ DPDK_19.05 { } DPDK_18.11; +DPDK_19.08 { + global: + + rte_rand; + rte_srand; + +} DPDK_19.05; + EXPERIMENTAL { global: From patchwork Thu May 16 17:55:06 2019 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: 53484 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 140255F1F; Thu, 16 May 2019 19:55:24 +0200 (CEST) Received: from sessmg22.ericsson.net (sessmg22.ericsson.net [193.180.251.58]) by dpdk.org (Postfix) with ESMTP id E2D7C4CAB for ; Thu, 16 May 2019 19:55:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1558029319; x=1560621319; h=From:Sender:Reply-To:Subject:Date:Message-ID:To:CC:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=sCbbIv/xOw/DWYTaCt6jmAP/dF/zn8swp9Q9wpMlpc0=; b=FRe8Sm/Biurh04AoDH5DdSAezk0EGaMBzTcW+/itY9sfnmWHT3gkONv8XuPikACA 89XcEu+vcX/6L9tSJiVPIzigCOlMo1hFQA3lNTODsG5tWi71e+pZ0Wf/sM9AL3Av O+1Jt663FUWzYZ+xV9ITnZn76vARKBDo0jk+/2AN0Qg=; X-AuditID: c1b4fb3a-6f1ff7000000189f-df-5cdda407f657 Received: from ESESBMB501.ericsson.se (Unknown_Domain [153.88.183.114]) by sessmg22.ericsson.net (Symantec Mail Security) with SMTP id 2E.CB.06303.704ADDC5; Thu, 16 May 2019 19:55:19 +0200 (CEST) Received: from ESESSMB504.ericsson.se (153.88.183.165) by ESESBMB501.ericsson.se (153.88.183.168) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Thu, 16 May 2019 19:55:19 +0200 Received: from selio1a020.lmera.ericsson.se (153.88.183.153) by smtp.internal.ericsson.com (153.88.183.192) with Microsoft SMTP Server id 15.1.1713.5 via Frontend Transport; Thu, 16 May 2019 19:55:19 +0200 Received: from breslau.lmera.ericsson.se (breslau.lmera.ericsson.se [150.132.109.241]) by selio1a020.lmera.ericsson.se (8.15.1+Sun/8.15.1) with ESMTP id x4GHtHcu023701; Thu, 16 May 2019 19:55:19 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , , =?utf-8?q?Mattias_R=C3=B6nnblom?= Date: Thu, 16 May 2019 19:55:06 +0200 Message-ID: <20190516175510.27154-3-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190516175510.27154-1-mattias.ronnblom@ericsson.com> References: <20190514092046.30808-1-mattias.ronnblom@ericsson.com> <20190516175510.27154-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrOLMWRmVeSWpSXmKPExsUyM2J7kS77krsxBssOGVncWGVv8e7TdiaL xXfkHJg9fi1YyuqxeM9LJo+ek/OYApijuGxSUnMyy1KL9O0SuDLe3vzLVvBSsOLCv4MsDYyH +boYOTgkBEwk+p6qdDFycQgJHGWUWNHxiQXC+cYocexCEyOEc5FRorPpOJRzmVFi8YE7TF2M nBxsAp4Sk991s4DYIgJCEks/XmYHKWIWaGWU2PGwkRUkISwQKPH9+D+wBhYBVYn9l7eA2bwC ThLblrxnBrElBOQlVm84AGZzCjhLXD1/AKxXSKBa4mfXGqh6QYmTM5+ALWMW0JRo3f6bHcKW l2jeOpsZol5L4v6SL8wTGIVmIWmZhaRlFpKWBYzMqxhFi1OLi3PTjYz0Uosyk4uL8/P08lJL NjECg/vglt9WOxgPPnc8xCjAwajEwxs7526MEGtiWXFl7iFGCQ5mJRHeDe9vxwjxpiRWVqUW 5ccXleakFh9ilOZgURLnjV69J0ZIID2xJDU7NbUgtQgmy8TBKdXAuPT8367vL9QZqr99Yp3a z7Z2wywxrfQQs4KnK8+qtWZ9OBLpul5/9dTkHT9D2TYrRjfXRt3X/HL4sZd16iWrMzNdc0tj NV4H/OCdri5y/ALDnxUbHPKvPZhX+DhIx+lqeuUp1t3Zrb80NO5LWOxSzK5aGqapfy465HJ+ x1W2F2/m5Jrpr3C7r8RSnJFoqMVcVJwIALrMzdRqAgAA Subject: [dpdk-dev] [PATCH v2 2/6] eal: add pseudo-random number generation performance test X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Add performance test for rte_rand(). Signed-off-by: Mattias Rönnblom --- app/test/Makefile | 1 + app/test/test_rand_perf.c | 75 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 app/test/test_rand_perf.c diff --git a/app/test/Makefile b/app/test/Makefile index 68d6b4fbc..be0f39227 100644 --- a/app/test/Makefile +++ b/app/test/Makefile @@ -73,6 +73,7 @@ SRCS-y += test_reciprocal_division.c SRCS-y += test_reciprocal_division_perf.c SRCS-y += test_fbarray.c SRCS-y += test_external_mem.c +SRCS-y += test_rand_perf.c SRCS-y += test_ring.c SRCS-y += test_ring_perf.c diff --git a/app/test/test_rand_perf.c b/app/test/test_rand_perf.c new file mode 100644 index 000000000..771713757 --- /dev/null +++ b/app/test/test_rand_perf.c @@ -0,0 +1,75 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2019 Ericsson AB + */ + +#include +#include + +#include +#include +#include + +#include "test.h" + +static volatile uint64_t vsum; + +#define ITERATIONS (100000000) + +enum rand_type { + rand_type_64 +}; + +static const char * +rand_type_desc(enum rand_type rand_type) +{ + switch (rand_type) { + case rand_type_64: + return "Full 64-bit [rte_rand()]"; + default: + return NULL; + } +} + +static __rte_always_inline void +test_rand_perf_type(enum rand_type rand_type) +{ + uint64_t start; + uint32_t i; + uint64_t end; + uint64_t sum = 0; + uint64_t op_latency; + + start = rte_rdtsc(); + + for (i = 0; i < ITERATIONS; i++) { + switch (rand_type) { + case rand_type_64: + sum += rte_rand(); + break; + } + } + + end = rte_rdtsc(); + + /* to avoid an optimizing compiler removing the whole loop */ + vsum = sum; + + op_latency = (end - start) / ITERATIONS; + + printf("%s: %"PRId64" TSC cycles/op\n", rand_type_desc(rand_type), + op_latency); +} + +static int +test_rand_perf(void) +{ + rte_srand(42); + + printf("Pseudo-random number generation latencies:\n"); + + test_rand_perf_type(rand_type_64); + + return 0; +} + +REGISTER_TEST_COMMAND(rand_perf_autotest, test_rand_perf); From patchwork Thu May 16 17:55:07 2019 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: 53486 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 23EF45F3C; Thu, 16 May 2019 19:55:29 +0200 (CEST) Received: from sessmg23.ericsson.net (sessmg23.ericsson.net [193.180.251.45]) by dpdk.org (Postfix) with ESMTP id 9534E4CAB for ; Thu, 16 May 2019 19:55:20 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1558029320; x=1560621320; h=From:Sender:Reply-To:Subject:Date:Message-ID:To:CC:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=etnIbbaJOa5w1s/djhxcAn0JCwzBlDzy37MAb8E/V24=; b=JQOn6Wi1pFraGOG/V8MkfD8OT+Bxp9NSJLtqBGV0g2ljEvbEE9FSKQ0B261z77eQ Ifhj8EXNwQrIaBzy4H0ZKtYQqlk7Yx9TbQD+lk9GX9x1mkRCCRar1oZhB/lJnyuD QTeg8H23uGWqWjvZnMtZZH6cqbiIh0OXJqULtJ3EGHg=; X-AuditID: c1b4fb2d-17dff70000001a6d-6c-5cdda408a6ae Received: from ESESSMB501.ericsson.se (Unknown_Domain [153.88.183.119]) by sessmg23.ericsson.net (Symantec Mail Security) with SMTP id 7A.27.06765.804ADDC5; Thu, 16 May 2019 19:55:20 +0200 (CEST) Received: from ESESBMB503.ericsson.se (153.88.183.170) by ESESSMB501.ericsson.se (153.88.183.162) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Thu, 16 May 2019 19:55:20 +0200 Received: from selio1a020.lmera.ericsson.se (153.88.183.153) by smtp.internal.ericsson.com (153.88.183.186) with Microsoft SMTP Server id 15.1.1713.5 via Frontend Transport; Thu, 16 May 2019 19:55:19 +0200 Received: from breslau.lmera.ericsson.se (breslau.lmera.ericsson.se [150.132.109.241]) by selio1a020.lmera.ericsson.se (8.15.1+Sun/8.15.1) with ESMTP id x4GHtHcv023701; Thu, 16 May 2019 19:55:20 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , , =?utf-8?q?Mattias_R=C3=B6nnblom?= Date: Thu, 16 May 2019 19:55:07 +0200 Message-ID: <20190516175510.27154-4-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190516175510.27154-1-mattias.ronnblom@ericsson.com> References: <20190514092046.30808-1-mattias.ronnblom@ericsson.com> <20190516175510.27154-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrOLMWRmVeSWpSXmKPExsUyM2J7uS7HkrsxBjOeylncWGVv8e7TdiaL xXfkHJg9fi1YyuqxeM9LJo+ek/OYApijuGxSUnMyy1KL9O0SuDJetb9lKlgrWtG00LaB8atg FyMnh4SAiUT/tWssXYxcHEICRxklJuw9BuV8Y5SYtGEGE4RzkVFi/8bv7BDOZUaJa19uM4L0 swl4Skx+180CYosICEks/XgZrIhZoJVRYsfDRlaQhLCAk8SiqVvAilgEVCXmHnzLBGLzAsU3 7bzCBnGIvMTqDQeYQWxOAWeJq+cPgPUKCVRL/OxaA1UvKHFy5hOwOcwCmhKt23+zQ9jyEs1b ZzND1GtJ3F/yhXkCo9AsJC2zkLTMQtKygJF5FaNocWpxcW66kbFealFmcnFxfp5eXmrJJkZg cB/c8lt3B+Pq146HGAU4GJV4eL/PuRsjxJpYVlyZe4hRgoNZSYR3w/vbMUK8KYmVValF+fFF pTmpxYcYpTlYlMR5o1fviRESSE8sSc1OTS1ILYLJMnFwSjUwFvq1RxwwuuJhcKadIWB1XXyv eJvTeo2Nzq81pW5/Zgh5l1V8Z+sj5gC+m0GtaZJCt/MMNp+flmRz4EDn1KgNbz7Xm35tF313 iyngMOf6Jyezbohv7jsxoeybdpx/5vQ/xuFlMcv6hIv/9z2vm3Z/6/uPBUZtwcYMn3aHzbro zjXDsH3u5/ZSJZbijERDLeai4kQAKAm7+WoCAAA= Subject: [dpdk-dev] [PATCH v2 3/6] eal: improve entropy for initial PRNG seed X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Replace the use of rte_get_timer_cycles() with getentropy() for seeding the pseudo-random number generator. getentropy() provides a more truly random value. getentropy() requires glibc 2.25 and Linux kernel 3.17. In case getentropy() is not found at compile time, or the relevant syscall fails in runtime, the rdseed machine instruction will be used as a fallback. rdseed is only available on x86 (Broadwell or later). In case it is not present, rte_get_timer_cycles() will be used as a second fallback. On non-Meson builds, getentropy() will not be used. Suggested-by: Bruce Richardson Suggested-by: Stephen Hemminger Signed-off-by: Mattias Rönnblom --- lib/librte_eal/common/rte_random.c | 36 +++++++++++++++++++++++++++++- lib/librte_eal/meson.build | 3 +++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/librte_eal/common/rte_random.c b/lib/librte_eal/common/rte_random.c index 4d3cf5226..e53d96d18 100644 --- a/lib/librte_eal/common/rte_random.c +++ b/lib/librte_eal/common/rte_random.c @@ -2,7 +2,11 @@ * Copyright(c) 2019 Ericsson AB */ +#ifdef RTE_MACHINE_CPUFLAG_RDSEED +#include +#endif #include +#include #include #include @@ -133,7 +137,37 @@ rte_rand(void) return __rte_rand_lfsr258(state); } +static uint64_t +__rte_random_initial_seed(void) +{ +#ifdef RTE_LIBEAL_USE_GETENTROPY + int ge_rc; + uint64_t ge_seed; + + ge_rc = getentropy(&ge_seed, sizeof(ge_seed)); + + if (ge_rc == 0) + return ge_seed; +#endif +#ifdef RTE_MACHINE_CPUFLAG_RDSEED + unsigned int rdseed_rc; + unsigned long long rdseed_seed; + + /* first fallback: rdseed instruction, if available */ + rdseed_rc = _rdseed64_step(&rdseed_seed); + + if (rdseed_rc == 1) + return (uint64_t)rdseed_seed; +#endif + /* second fallback: seed using rdtsc */ + return rte_get_timer_cycles(); +} + RTE_INIT(rte_rand_init) { - rte_srand(rte_get_timer_cycles()); + uint64_t seed; + + seed = __rte_random_initial_seed(); + + rte_srand(seed); } diff --git a/lib/librte_eal/meson.build b/lib/librte_eal/meson.build index fa36b20e0..ccd5b85b8 100644 --- a/lib/librte_eal/meson.build +++ b/lib/librte_eal/meson.build @@ -18,6 +18,9 @@ deps += 'kvargs' if dpdk_conf.has('RTE_USE_LIBBSD') ext_deps += libbsd endif +if cc.has_function('getentropy', prefix : '#include ') + cflags += '-DRTE_LIBEAL_USE_GETENTROPY' +endif sources = common_sources + env_sources objs = common_objs + env_objs headers = common_headers + env_headers From patchwork Thu May 16 17:55:08 2019 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: 53487 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 180CE7CCF; Thu, 16 May 2019 19:55:32 +0200 (CEST) Received: from sessmg23.ericsson.net (sessmg23.ericsson.net [193.180.251.45]) by dpdk.org (Postfix) with ESMTP id 6EF365F19 for ; Thu, 16 May 2019 19:55:21 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1558029321; x=1560621321; h=From:Sender:Reply-To:Subject:Date:Message-ID:To:CC:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=S/h1DoAkXCzKBmNCgdYEVo7lZSo6k8+XMVLKTo6r3vw=; b=WVmp3OLad78FjVRBvXmjIs35h9yEn4CbMiWLIQA2h15QTyR0nwHS8fW21A31VWro a8phNEW8dr9hCJGFQvFwxjEIw3WKvGXc6bRogF9Ar0UA1xfCaaidokvAriGGFGP8 KLQvkPRebAcpAyoOJJt/G27PfC/fStUCCoCzP7EDy/A=; X-AuditID: c1b4fb2d-195ff70000001a6d-71-5cdda409fade Received: from ESESBMB504.ericsson.se (Unknown_Domain [153.88.183.117]) by sessmg23.ericsson.net (Symantec Mail Security) with SMTP id 9B.27.06765.904ADDC5; Thu, 16 May 2019 19:55:21 +0200 (CEST) Received: from ESESSMR504.ericsson.se (153.88.183.126) by ESESBMB504.ericsson.se (153.88.183.171) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Thu, 16 May 2019 19:55:20 +0200 Received: from ESESSMB503.ericsson.se (153.88.183.164) by ESESSMR504.ericsson.se (153.88.183.126) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Thu, 16 May 2019 19:55:21 +0200 Received: from selio1a020.lmera.ericsson.se (153.88.183.153) by smtp.internal.ericsson.com (153.88.183.191) with Microsoft SMTP Server id 15.1.1713.5 via Frontend Transport; Thu, 16 May 2019 19:55:20 +0200 Received: from breslau.lmera.ericsson.se (breslau.lmera.ericsson.se [150.132.109.241]) by selio1a020.lmera.ericsson.se (8.15.1+Sun/8.15.1) with ESMTP id x4GHtHcw023701; Thu, 16 May 2019 19:55:20 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , , =?utf-8?q?Mattias_R=C3=B6nnblom?= Date: Thu, 16 May 2019 19:55:08 +0200 Message-ID: <20190516175510.27154-5-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190516175510.27154-1-mattias.ronnblom@ericsson.com> References: <20190514092046.30808-1-mattias.ronnblom@ericsson.com> <20190516175510.27154-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrKLMWRmVeSWpSXmKPExsUyM2J7qS7nkrsxBpN3ilncWGVv8e7TdiaL xXfkHJg9fi1YyuqxeM9LJo+ek/OYApijuGxSUnMyy1KL9O0SuDJ+T73MUnBSquLIt7OsDYyf RLsYOTkkBEwkmhdvZuli5OIQEjjKKLH3E4zzjVHiR+Nydjjn7JfvjBDORUaJFYf+Q2UuM0o8 3/GEFWQYm4CnxOR33SwgtoiAkMTSj5fBipgFWhkldjxsBCsSFgiU2L7wJpjNIqAq0bz5MFgD r4CTxNee3SwQV8lLrN5wgBnE5hRwlrh6/gBYvZBAtcTPrjVMEPWCEidnPgGrZxbQlGjd/psd wpaXaN46mxmiXkvi/pIvzBMYhWchaZmFpGUWkpYFjMyrGEWLU4uLc9ONjPVSizKTi4vz8/Ty Uks2MQID/+CW37o7GFe/djzEKMDBqMTD+33O3Rgh1sSy4srcQ4wSHMxKIrwb3t+OEeJNSays Si3Kjy8qzUktPsQozcGiJM4bvXpPjJBAemJJanZqakFqEUyWiYNTqoFR88Cq4hWX/2zt6NB1 MgyW4NsetNXVyttBp+MeY5b3vZQ7hz4pH/UQv7RJ7EqOcsbsx4lPX6xd+PJ6XIFptF9/PPsL nZPRHVX9ByecOhteEvXximrqa2FvleXznho4xqRtOi5S8+LCZVmNbjbTBzs2cxTYPX4v9vln gJ1I0qo1LWXqeR7nLhkrsRRnJBpqMRcVJwIAwbpNFngCAAA= Subject: [dpdk-dev] [PATCH v2 4/6] eal: introduce random generator function with upper bound X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Add a function rte_rand_max() which generates an uniformly distributed pseudo-random number less than a user-specified upper bound. The commonly used pattern rte_rand() % SOME_VALUE creates biased results (as in some values in the range are more frequently occurring than others) if SOME_VALUE is not a power of 2. Signed-off-by: Mattias Rönnblom --- lib/librte_eal/common/include/rte_random.h | 18 ++++++++++ lib/librte_eal/common/rte_random.c | 39 ++++++++++++++++++++++ lib/librte_eal/rte_eal_version.map | 1 + 3 files changed, 58 insertions(+) diff --git a/lib/librte_eal/common/include/rte_random.h b/lib/librte_eal/common/include/rte_random.h index 66dfe8ae7..939e6aaa9 100644 --- a/lib/librte_eal/common/include/rte_random.h +++ b/lib/librte_eal/common/include/rte_random.h @@ -17,6 +17,8 @@ extern "C" { #include +#include + /** * Seed the pseudo-random generator. * @@ -47,6 +49,22 @@ rte_srand(uint64_t seedval); uint64_t rte_rand(void); +/** + * Generates a pseudo-random number with an upper bound. + * + * 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. + * + * @param upper_bound + * The upper bound of the generated number. + * @return + * A pseudo-random value between 0 and (upper_bound-1). + */ +uint64_t __rte_experimental +rte_rand_max(uint64_t upper_bound); + #ifdef __cplusplus } #endif diff --git a/lib/librte_eal/common/rte_random.c b/lib/librte_eal/common/rte_random.c index e53d96d18..3d9b9b7d8 100644 --- a/lib/librte_eal/common/rte_random.c +++ b/lib/librte_eal/common/rte_random.c @@ -137,6 +137,45 @@ rte_rand(void) return __rte_rand_lfsr258(state); } +uint64_t __rte_experimental +rte_rand_max(uint64_t upper_bound) +{ + struct rte_rand_state *state; + uint8_t ones; + uint8_t leading_zeros; + uint64_t mask = ~((uint64_t)0); + uint64_t res; + + if (unlikely(upper_bound < 2)) + return 0; + + state = __rte_rand_get_state(); + + ones = __builtin_popcountll(upper_bound); + + /* Handle power-of-2 upper_bound as a special case, since it + * has no bias issues. + */ + if (unlikely(ones == 1)) + return __rte_rand_lfsr258(state) & (upper_bound - 1); + + /* The approach to avoiding bias is to create a mask that + * stretches beyond the request value range, and up to the + * next power-of-2. In case the masked generated random value + * is equal to or greater than the upper bound, just discard + * the value and generate a new one. + */ + + leading_zeros = __builtin_clzll(upper_bound); + mask >>= leading_zeros; + + do { + res = __rte_rand_lfsr258(state) & mask; + } while (unlikely(res >= upper_bound)); + + return res; +} + static uint64_t __rte_random_initial_seed(void) { diff --git a/lib/librte_eal/rte_eal_version.map b/lib/librte_eal/rte_eal_version.map index e615d7cb9..82483aed2 100644 --- a/lib/librte_eal/rte_eal_version.map +++ b/lib/librte_eal/rte_eal_version.map @@ -382,6 +382,7 @@ EXPERIMENTAL { rte_mp_request_async; rte_mp_sendmsg; rte_option_register; + rte_rand_max; rte_realloc_socket; rte_service_lcore_attr_get; rte_service_lcore_attr_reset_all; From patchwork Thu May 16 17:55:09 2019 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: 53488 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id E11BD7D4A; Thu, 16 May 2019 19:55:34 +0200 (CEST) Received: from sessmg22.ericsson.net (sessmg22.ericsson.net [193.180.251.58]) by dpdk.org (Postfix) with ESMTP id D64335F19 for ; Thu, 16 May 2019 19:55:21 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1558029321; x=1560621321; h=From:Sender:Reply-To:Subject:Date:Message-ID:To:CC:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=emEpPp6la2DesqpGTlZk9fmNqO+dTPyXy9NhN852tTA=; b=WR+Exi3u2IWV6NpyUWI1oHR6vjQruQTnfU4fWPtmvDHNuc4WLqLiQSvwYO2dsUiU 61N5G9h6dFmEOmaGctF8LG8E98t2HR2DsnNchb7Q/MEqIPxmbknqzT6E44VpsVqS 3eC6Tto4cm/Jak/7AEfjlZKX3bpL/XyxxiEhC2biNUw=; X-AuditID: c1b4fb3a-709ff7000000189f-e8-5cdda4092d86 Received: from ESESSMB501.ericsson.se (Unknown_Domain [153.88.183.119]) by sessmg22.ericsson.net (Symantec Mail Security) with SMTP id 41.DB.06303.904ADDC5; Thu, 16 May 2019 19:55:21 +0200 (CEST) Received: from ESESBMB501.ericsson.se (153.88.183.168) by ESESSMB501.ericsson.se (153.88.183.162) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Thu, 16 May 2019 19:55:21 +0200 Received: from selio1a020.lmera.ericsson.se (153.88.183.153) by smtp.internal.ericsson.com (153.88.183.184) with Microsoft SMTP Server id 15.1.1713.5 via Frontend Transport; Thu, 16 May 2019 19:55:21 +0200 Received: from breslau.lmera.ericsson.se (breslau.lmera.ericsson.se [150.132.109.241]) by selio1a020.lmera.ericsson.se (8.15.1+Sun/8.15.1) with ESMTP id x4GHtHcx023701; Thu, 16 May 2019 19:55:21 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , , =?utf-8?q?Mattias_R=C3=B6nnblom?= Date: Thu, 16 May 2019 19:55:09 +0200 Message-ID: <20190516175510.27154-6-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190516175510.27154-1-mattias.ronnblom@ericsson.com> References: <20190514092046.30808-1-mattias.ronnblom@ericsson.com> <20190516175510.27154-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrOLMWRmVeSWpSXmKPExsUyM2J7uS7nkrsxBp/fG1jcWGVv8e7TdiaL xXfkHJg9fi1YyuqxeM9LJo+ek/OYApijuGxSUnMyy1KL9O0SuDJ+XPvNXLCBt+LztaPsDYxd 3F2MnBwSAiYSZ99uYOli5OIQEjjKKLHr000o5xujxOHbx9khnIuMEls3r2SEcC4zSszuOscC 0s8m4Ckx+V03mC0iICSx9ONlsA5mgVZGiR0PG1m7GDk4hAXsJXr6NEBqWARUJd7PeMgOYvMK OEmsntHACnGHvMTqDQeYQWxOAWeJq+cPgMWFBKolfnatYYKoF5Q4OfMJ2C5mAU2J1u2/2SFs eYnmrbOZIeq1JO4v+cI8gVFoFpKWWUhaZiFpWcDIvIpRtDi1uDg33chIL7UoM7m4OD9PLy+1 ZBMjMLgPbvlttYPx4HPHQ4wCHIxKPLyxc+7GCLEmlhVX5h5ilOBgVhLh3fD+dowQb0piZVVq UX58UWlOavEhRmkOFiVx3ujVe2KEBNITS1KzU1MLUotgskwcnFINjDyzV0QrW5xIUUj+KCqz 8A+fgfHmFf+mPL2zmtF+xz2nVT+vVWoemzrtyWfvihdLw5qnbXMU4qgs2P27eeXKhvRVqSUH lpg+2Hh4xamaieWWjQeLWLc3z3QRjPqycv8Ehe2T54auixYxnX/g312ORe6FT+YcLZRSkDic VloscqerYFtB2ffmCSeVWIozEg21mIuKEwGuxg6jagIAAA== Subject: [dpdk-dev] [PATCH v2 5/6] eal: add bounded PRNG performance tests X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Add best- and worst-case performance tests for rte_rand_max(). Signed-off-by: Mattias Rönnblom --- app/test/test_rand_perf.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/app/test/test_rand_perf.c b/app/test/test_rand_perf.c index 771713757..fe797ebfa 100644 --- a/app/test/test_rand_perf.c +++ b/app/test/test_rand_perf.c @@ -15,8 +15,13 @@ static volatile uint64_t vsum; #define ITERATIONS (100000000) +#define BEST_CASE_BOUND (1<<16) +#define WORST_CASE_BOUND (BEST_CASE_BOUND + 1) + enum rand_type { - rand_type_64 + rand_type_64, + rand_type_bounded_best_case, + rand_type_bounded_worst_case }; static const char * @@ -25,6 +30,10 @@ rand_type_desc(enum rand_type rand_type) switch (rand_type) { case rand_type_64: return "Full 64-bit [rte_rand()]"; + case rand_type_bounded_best_case: + return "Bounded average best-case [rte_rand_max()]"; + case rand_type_bounded_worst_case: + return "Bounded average worst-case [rte_rand_max()]"; default: return NULL; } @@ -46,6 +55,12 @@ test_rand_perf_type(enum rand_type rand_type) case rand_type_64: sum += rte_rand(); break; + case rand_type_bounded_best_case: + sum += rte_rand_max(BEST_CASE_BOUND); + break; + case rand_type_bounded_worst_case: + sum += rte_rand_max(WORST_CASE_BOUND); + break; } } @@ -68,6 +83,8 @@ test_rand_perf(void) printf("Pseudo-random number generation latencies:\n"); test_rand_perf_type(rand_type_64); + test_rand_perf_type(rand_type_bounded_best_case); + test_rand_perf_type(rand_type_bounded_worst_case); return 0; } From patchwork Thu May 16 17:55:10 2019 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: 53489 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id E241DD16D; Thu, 16 May 2019 19:55:36 +0200 (CEST) Received: from sesbmg22.ericsson.net (sesbmg22.ericsson.net [193.180.251.48]) by dpdk.org (Postfix) with ESMTP id 0CE3A5F1B for ; Thu, 16 May 2019 19:55:23 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1558029322; x=1560621322; h=From:Sender:Reply-To:Subject:Date:Message-ID:To:CC:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=PD9GEtA2ka5cNOAsH3bMv4KkKfDJxToR2yVg1FN6ZU4=; b=ghDTF6J8LvHY5peUtEPFOLIJe/Ab/SlWNInIkQ/6923s6GTa/kDVj5ovKt0XwKfs d3fF7J0FExSOe9kQ9VdbB6DYk237x2dJfYX3rqwQFYvFL63t4UM9APdq14c5qhZf hwUR+JUMS23LY70kd0kUU54Gjt3mv3LbjW6fbcUgiik=; X-AuditID: c1b4fb30-6f5ff70000001814-76-5cdda40ac900 Received: from ESESBMB503.ericsson.se (Unknown_Domain [153.88.183.116]) by sesbmg22.ericsson.net (Symantec Mail Security) with SMTP id CF.B6.06164.A04ADDC5; Thu, 16 May 2019 19:55:22 +0200 (CEST) Received: from ESESBMR504.ericsson.se (153.88.183.139) by ESESBMB503.ericsson.se (153.88.183.170) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Thu, 16 May 2019 19:55:22 +0200 Received: from ESESSMB501.ericsson.se (153.88.183.162) by ESESBMR504.ericsson.se (153.88.183.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Thu, 16 May 2019 19:55:22 +0200 Received: from selio1a020.lmera.ericsson.se (153.88.183.153) by smtp.internal.ericsson.com (153.88.183.189) with Microsoft SMTP Server id 15.1.1713.5 via Frontend Transport; Thu, 16 May 2019 19:55:21 +0200 Received: from breslau.lmera.ericsson.se (breslau.lmera.ericsson.se [150.132.109.241]) by selio1a020.lmera.ericsson.se (8.15.1+Sun/8.15.1) with ESMTP id x4GHtHd0023701; Thu, 16 May 2019 19:55:22 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , , =?utf-8?q?Mattias_R=C3=B6nnblom?= Date: Thu, 16 May 2019 19:55:10 +0200 Message-ID: <20190516175510.27154-7-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190516175510.27154-1-mattias.ronnblom@ericsson.com> References: <20190514092046.30808-1-mattias.ronnblom@ericsson.com> <20190516175510.27154-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrELMWRmVeSWpSXmKPExsUyM2J7iS7XkrsxBj82KVvcWGVv8e7TdiaL xXfkHJg9fi1YyuqxeM9LJo+ek/OYApijuGxSUnMyy1KL9O0SuDIOLPnGWnCJr6Lv526WBsZZ PF2MnBwSAiYSW///ZOli5OIQEjjKKDHrwn1mkISQwDdGicNvLSASQPbuS7PYIRIXGSU+P0+F SFxmlGie/RsswSbgKTH5XTcLiC0iICSx9ONldpAiZoFWRokdDxtZQRLCAm4SJ3q3AtkcHCwC qhKnJymAhHkFnCTWNB1hgThJXmL1hgNgV3AKOEtcPX+AFWJxtcTPrjVMEPWCEidnPgGrZxbQ lGjdDnEDM1Bv89bZUB9oSdxf8oV5AqPwLCQts5C0zELSsoCReRWjaHFqcVJuupGRXmpRZnJx cX6eXl5qySZGYMgf3PLbYAfjy+eOhxgFOBiVeHhj59yNEWJNLCuuzD3EKMHBrCTCu+H97Rgh 3pTEyqrUovz4otKc1OJDjNIcLErivNGr98QICaQnlqRmp6YWpBbBZJk4OKUaGCdLncs3/7C4 aEfdfjMNlrfnlIP2SwlIb7FdN5lraqlRPa/LM9ljzcIZ26JX2os5/9WMdTt6YZOpuWdf0teH R9799r589nPCrG/K6Ve9emZZJlkrC7OulqzZtnTbsdW8zPWJk5r+XVvLVXab3zJrduq32Od5 zw1zyloVN1+yrax7Mv3AXrWHr5VYijMSDbWYi4oTAZOsdrR1AgAA Subject: [dpdk-dev] [PATCH v2 6/6] eal: add PRNG to MAINTAINERS and release notes X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Add a section on PRNG in MAINTAINERS, and information on the PRNG improvements in the 19.08 release notes. Signed-off-by: Mattias Rönnblom --- MAINTAINERS | 6 ++++++ doc/guides/rel_notes/release_19_08.rst | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 15d0829c5..d06f5da90 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -222,6 +222,12 @@ M: Joyce Kong F: lib/librte_eal/common/include/generic/rte_ticketlock.h F: app/test/test_ticketlock.c +Pseudo-random Number Generation +M: Mattias Rönnblom +F: lib/librte_eal/common/include/rte_random.h +F: lib/librte_eal/common/rte_random.c +F: app/test/test_rand_perf.c + ARM v7 M: Jan Viktorin M: Gavin Hu diff --git a/doc/guides/rel_notes/release_19_08.rst b/doc/guides/rel_notes/release_19_08.rst index b9510f93a..62a9acf89 100644 --- a/doc/guides/rel_notes/release_19_08.rst +++ b/doc/guides/rel_notes/release_19_08.rst @@ -54,6 +54,19 @@ New Features Also, make sure to start the actual text at the margin. ========================================================= +* **Updated the EAL Pseudo-random Number Generator.** + + The lrand48()-based rte_rand() function is replaced with a + DPDK-native combined Linear Feedback Shift Register (LFSR) + pseudo-random number generator (PRNG). + + This new PRNG implementation is multi-thread safe, provides + higher-quality pseudo-random numbers (including full 64 bit + support) and improved performance. + + In addition, is extended with a new function + rte_rand_max() which supplies unbiased, bounded pseudo-random + numbers. Removed Items -------------