From patchwork Wed Jun 5 10:43:55 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: 54412 X-Patchwork-Delegate: thomas@monjalon.net 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 C1C411BABD; Wed, 5 Jun 2019 12:44:39 +0200 (CEST) Received: from sessmg22.ericsson.net (sessmg22.ericsson.net [193.180.251.58]) by dpdk.org (Postfix) with ESMTP id 43BF01BA85 for ; Wed, 5 Jun 2019 12:44:36 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1559731476; x=1562323476; 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=WBL9R248asGlQ4P1zIGbaAS1mMbt45m7vc7GQMANGcM=; b=b7qXIwQyXW5eihox7bFdurum12EA6qeonT/5PMvIi4BbtrzSO2Er92wIfvYi4WOU Vg+nncc7COBBTccqKRMBnzlk8UUgRXqhX/NbWCE1bTEvbVjBwZ24TKi5Qm0gtqjT /sT4Iu+yS9B+3QsEEAJ63QsKwGaHFN3pdJo2JrtfMZY=; X-AuditID: c1b4fb3a-709ff7000000189f-45-5cf79d144e4f Received: from ESESBMB503.ericsson.se (Unknown_Domain [153.88.183.116]) by sessmg22.ericsson.net (Symantec Mail Security) with SMTP id F6.FD.06303.41D97FC5; Wed, 5 Jun 2019 12:44:36 +0200 (CEST) Received: from ESESSMB504.ericsson.se (153.88.183.165) 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; Wed, 5 Jun 2019 12:44:26 +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; Wed, 5 Jun 2019 12:44:26 +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 x55AiQRB025408; Wed, 5 Jun 2019 12:44:26 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , , , , =?utf-8?q?Mattias_R=C3=B6nnblom?= Date: Wed, 5 Jun 2019 12:43:55 +0200 Message-ID: <20190605104400.24484-2-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190605104400.24484-1-mattias.ronnblom@ericsson.com> References: <20190516203529.GA642@bricha3-MOBL.ger.corp.intel.com> <20190605104400.24484-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrDLMWRmVeSWpSXmKPExsUyM2J7ia7I3O8xBjt+s1ncWGVvsX1FF5vF u0/bmSxuNZ9ks1h8R86B1ePXgqWsHov3vGTy6Dk5j8nj/b6rbB5Xvq9mDGCN4rJJSc3JLEst 0rdL4Mp40PCVraDNs2L/wQPsDYw3bLoYOTkkBEwk9i+bzdrFyMUhJHCUUeLpxFNMEM5XRolb p3czQjgXGCUer1vNDuFcYpToXzSVEaSfTcBTYvK7bhYQW0RASGLpx8tgRcwCexklXq2bB5YQ FgiSeHX+GRuIzSKgIrHz4G2wOK+Ak8Tun3sZIQ6Rl1i94QAziM0p4CzxePp+JhBbSKBaYsLb ZiaIekGJkzOfgPUyC2hKtG7/zQ5hy0s0b53NDFGvJXF/yRfmCYxCs5C0zELSMgtJywJG5lWM osWpxcW56UZGeqlFmcnFxfl5enmpJZsYgXFwcMtvqx2MB587HmIU4GBU4uG91vs9Rog1say4 MvcQowQHs5IIb+LtLzFCvCmJlVWpRfnxRaU5qcWHGKU5WJTEeaNX74kREkhPLEnNTk0tSC2C yTJxcEo1MC64uU2/YyYnp2qPR3BKz/MJcuKbF4i6cU5t+bIhgfvAQcsZ89q8Hc4LRv4WTdvx PnEKu3w65y//MKutE5R2ff/kPyktXoRzx+2mmgmzLlxl+6Q753ar99/9UwTqt7gbWpTkv1i/ kC9gs9rCD+kKlTsVGr2vL15j88brVIwgN1v8ukj/6pk3FyixFGckGmoxFxUnAgD5N4/lfwIA AA== Subject: [dpdk-dev] [PATCH v3 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 Acked-by: Bruce Richardson --- 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 | 2 + 8 files changed, 155 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 824edf0ff..20c1a9018 100644 --- a/lib/librte_eal/rte_eal_version.map +++ b/lib/librte_eal/rte_eal_version.map @@ -292,6 +292,8 @@ DPDK_19.08 { rte_lcore_index; rte_lcore_to_socket_id; + rte_rand; + rte_srand; } DPDK_19.05; From patchwork Wed Jun 5 10:43:56 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: 54409 X-Patchwork-Delegate: thomas@monjalon.net 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 38E341B9F0; Wed, 5 Jun 2019 12:44:31 +0200 (CEST) Received: from sesbmg23.ericsson.net (sesbmg23.ericsson.net [193.180.251.37]) by dpdk.org (Postfix) with ESMTP id C01641B9B2 for ; Wed, 5 Jun 2019 12:44:27 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1559731467; x=1562323467; 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=RAcu0KejPxm9PsQwRrT1ncrArElQbJjmyoF3GQyOlYE=; b=UaZQMVX++ZTqNPcevUAJD7oD0+tvB4YiCWvXOMHFKviYmVr+NpIhK+1E1rUseFex m2g3DAo8GNeQ+HgTkeDg+zaPKxvKwqTHMk+TwpQZHkkhQ8QOaHtG1CBkLbVWx4X0 QI/cSuDZpYK34u+S6APsdSG146mjsmhQ7yIpyOE3LjM=; X-AuditID: c1b4fb25-399ff700000029f0-88-5cf79d0b6d67 Received: from ESESSMB504.ericsson.se (Unknown_Domain [153.88.183.122]) by sesbmg23.ericsson.net (Symantec Mail Security) with SMTP id 5C.3A.10736.B0D97FC5; Wed, 5 Jun 2019 12:44:27 +0200 (CEST) Received: from ESESSMB504.ericsson.se (153.88.183.165) by ESESSMB504.ericsson.se (153.88.183.165) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Wed, 5 Jun 2019 12:44:26 +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; Wed, 5 Jun 2019 12:44:26 +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 x55AiQRC025408; Wed, 5 Jun 2019 12:44:27 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , , , , =?utf-8?q?Mattias_R=C3=B6nnblom?= Date: Wed, 5 Jun 2019 12:43:56 +0200 Message-ID: <20190605104400.24484-3-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190605104400.24484-1-mattias.ronnblom@ericsson.com> References: <20190516203529.GA642@bricha3-MOBL.ger.corp.intel.com> <20190605104400.24484-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrNLMWRmVeSWpSXmKPExsUyM2J7lS733O8xBtfOiFncWGVvsX1FF5vF u0/bmSxuNZ9ks1h8R86B1ePXgqWsHov3vGTy6Dk5j8nj/b6rbB5Xvq9mDGCN4rJJSc3JLEst 0rdL4Mq4sfwzY8EioYp7r3kaGL/zdTFyckgImEjMvrOSpYuRi0NI4CijRPfE/WwQzldGid+9 L6EyFxglHq9bzQ7hXGKUeLC1hRmkn03AU2Lyu24WEFtEQEhi6cfLYEXMAnsZJV6tmweWEBYI lFjcMosdxGYRUJE4uWczE4jNK+AkMbdpPTPEIfISqzccALM5BZwlHk/fD1YjJFAtMeFtM1S9 oMTJmU/AZjILaEq0bv/NDmHLSzRvnc0MUa8lcX/JF+YJjEKzkLTMQtIyC0nLAkbmVYyixanF SbnpRsZ6qUWZycXF+Xl6eaklmxiBUXBwy2/VHYyX3zgeYhTgYFTi4X3R/T1GiDWxrLgy9xCj BAezkghv4u0vMUK8KYmVValF+fFFpTmpxYcYpTlYlMR5o1fviRESSE8sSc1OTS1ILYLJMnFw SjUwFku/PPNny4ob2SePaDzZdn1NctnvM0lCl28mP5/zOHZe5tcbU2xe7ezl3qS1ynNqTc+x xe+yLxZUffQNMGYsmRtwz9Kf6W9r4GvB9kMmXlwWHUYyFyf7nvJ2Uzve568w9ekfl2SnG9NW /vq88tOJbTlrrtstXrmjMkKo63NrQf6Gb7UF3R+b9ZRYijMSDbWYi4oTAX/gIrR+AgAA Subject: [dpdk-dev] [PATCH v3 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 Acked-by: Bruce Richardson --- 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 Wed Jun 5 10:43:57 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: 54413 X-Patchwork-Delegate: thomas@monjalon.net 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 885361BAE5; Wed, 5 Jun 2019 12:44:41 +0200 (CEST) Received: from sessmg22.ericsson.net (sessmg22.ericsson.net [193.180.251.58]) by dpdk.org (Postfix) with ESMTP id 82A981BA8F for ; Wed, 5 Jun 2019 12:44:36 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1559731476; x=1562323476; 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=oriMSYzD69gLHrQpWqsvfZuMqM7qy5GGOQ+W7kCH8aU=; b=gMaWQ6JYI8hxP2SaHcXcVVHKvl9SYrXf9SEHy5UNcXMqxpT6f52F97J7hGGHU+ar 1vxv9heOdMqsGZSnf/nRY2QPfcurkkyWtDsWgI9WJ3+hYXlgje70QHl17hOWN5nd vmbt5dWJYM4Z217rBAt4jxJsKeh/TjRMqfdBee/+lZ8=; X-AuditID: c1b4fb3a-b6bb79e00000189f-51-5cf79d147f24 Received: from ESESBMB501.ericsson.se (Unknown_Domain [153.88.183.114]) by sessmg22.ericsson.net (Symantec Mail Security) with SMTP id E9.FD.06303.41D97FC5; Wed, 5 Jun 2019 12:44:36 +0200 (CEST) Received: from ESESBMR503.ericsson.se (153.88.183.135) 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; Wed, 5 Jun 2019 12:44:35 +0200 Received: from ESESBMB501.ericsson.se (153.88.183.168) by ESESBMR503.ericsson.se (153.88.183.135) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Wed, 5 Jun 2019 12:44:27 +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; Wed, 5 Jun 2019 12:44:26 +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 x55AiQRD025408; Wed, 5 Jun 2019 12:44:27 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , , , , =?utf-8?q?Mattias_R=C3=B6nnblom?= Date: Wed, 5 Jun 2019 12:43:57 +0200 Message-ID: <20190605104400.24484-4-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190605104400.24484-1-mattias.ronnblom@ericsson.com> References: <20190516203529.GA642@bricha3-MOBL.ger.corp.intel.com> <20190605104400.24484-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrJLMWRmVeSWpSXmKPExsUyM2J7ka7I3O8xBj/3SFvcWGVvsX1FF5vF u0/bmSxuNZ9ks1h8R86B1ePXgqWsHov3vGTy6Dk5j8nj/b6rbB5Xvq9mDGCN4rJJSc3JLEst 0rdL4MqYNbWNueCZaMXyruksDYwrhboYOTkkBEwkFs78xtzFyMUhJHCUUWLzyl9MEM5XRom9 c5exwjmNRw9DZS4wSjyZ/YUFwrnEKPHu7WxGkGFsAp4Sk991s4DYIgJCEks/XmYHKWIW2Mso 8WrdPLCEsICTxImL05hBbBYBFYm2DZ1gcV6geNOexWwQV8lLrN5wAKyGU8BZ4vH0/UwgtpBA tcSEt81MEPWCEidnPgHrZRbQlGjd/psdwpaXaN46mxmiXkvi/pIvzBMYhWchaZmFpGUWkpYF jMyrGEWLU4uLc9ONjPRSizKTi4vz8/TyUks2MQIj5OCW31Y7GA8+dzzEKMDBqMTDe633e4wQ a2JZcWXuIUYJDmYlEd7E219ihHhTEiurUovy44tKc1KLDzFKc7AoifNGr94TIySQnliSmp2a WpBaBJNl4uCUamB0tdybOOs7W2pNfqH9o9+Cs5UnGfXe2bfS2f/BxJqiCIZNe7hEbmrpOtl7 5u3Yffqh/NICzrmlX46Xh/676sUeGhUk4GDIXu/xcZbEivg582LdaqXiTieqbuO9+2fed6v/ L/vZX1+W4d6zf96m+e9fBF32ZGl9E3tr07MfLBP2Ml9PCG5Tv/FaiaU4I9FQi7moOBEAerZT xYwCAAA= Subject: [dpdk-dev] [PATCH v3 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 Acked-by: Bruce Richardson --- 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 Wed Jun 5 10:43:58 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: 54414 X-Patchwork-Delegate: thomas@monjalon.net 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 5BB721BB22; Wed, 5 Jun 2019 12:44:43 +0200 (CEST) Received: from sessmg23.ericsson.net (sessmg23.ericsson.net [193.180.251.45]) by dpdk.org (Postfix) with ESMTP id 8C2E91BA8F for ; Wed, 5 Jun 2019 12:44:37 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1559731476; x=1562323476; 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=uZiIz4oSZOfhKYpEU373SIY5pIImkCt1mT2IwfQZKBU=; b=Z+GcW31ZnCtL82R0sntUHSeVouDh2XM5s5zCu2u/jWKlp95nR8PhYRXOk9i4TfaZ GGPdrT9JIouhcpUmyOa9Y9Xat0Hk5hAlZQMSZDnrsF5JdSBv9YfT8x52BQqNoOTs QkUzGVKodKwhtpYf/rp/QCzi/aW3AhW7cJiWNbBg49c=; X-AuditID: c1b4fb2d-195ff70000001a6d-41-5cf79d1480d1 Received: from ESESBMB501.ericsson.se (Unknown_Domain [153.88.183.114]) by sessmg23.ericsson.net (Symantec Mail Security) with SMTP id 70.58.06765.41D97FC5; Wed, 5 Jun 2019 12:44:36 +0200 (CEST) Received: from ESESBMB504.ericsson.se (153.88.183.171) 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; Wed, 5 Jun 2019 12:44:26 +0200 Received: from selio1a020.lmera.ericsson.se (153.88.183.153) by smtp.internal.ericsson.com (153.88.183.187) with Microsoft SMTP Server id 15.1.1713.5 via Frontend Transport; Wed, 5 Jun 2019 12:44:26 +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 x55AiQRE025408; Wed, 5 Jun 2019 12:44:27 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , , , , =?utf-8?q?Mattias_R=C3=B6nnblom?= Date: Wed, 5 Jun 2019 12:43:58 +0200 Message-ID: <20190605104400.24484-5-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190605104400.24484-1-mattias.ronnblom@ericsson.com> References: <20190516203529.GA642@bricha3-MOBL.ger.corp.intel.com> <20190605104400.24484-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrDLMWRmVeSWpSXmKPExsUyM2J7ka7I3O8xBouPsVncWGVvsX1FF5vF u0/bmSxuNZ9ks1h8R86B1ePXgqWsHov3vGTy6Dk5j8nj/b6rbB5Xvq9mDGCN4rJJSc3JLEst 0rdL4Mr48mUac0GTdMX8S1dYGxiXiXUxcnJICJhIdL6bx9TFyMUhJHCUUeL+70VsEM5XRolH kzZCZS4wSqze3s0M4VxilFj6fgobSD+bgKfE5HfdLCC2iICQxNKPl9lBipgF9jJKvFo3Dywh LBAosfHOdLAGFgEVibXL2thBbF4BJ4n9hw+zQBwiL7F6wwFmEJtTwFni8fT9TCC2kEC1xIS3 zUwQ9YISJ2c+AatnFtCUaN3+mx3Clpdo3jqbGaJeS+L+ki/MExiFZiFpmYWkZRaSlgWMzKsY RYtTi4tz042M9VKLMpOLi/Pz9PJSSzYxAuPg4JbfujsYV792PMQowMGoxMMr3/s9Rog1say4 MvcQowQHs5IIb+LtLzFCvCmJlVWpRfnxRaU5qcWHGKU5WJTEeaNX74kREkhPLEnNTk0tSC2C yTJxcEo1MC7YtsvcPPa0vaR0rUGTf4Ln+0WvVhw9/mpb87ON/haiV/UiohIKGZfwGPF1z7/s UzY3Y0Pd432cxxkvnTq+QnEnb8uqt2d9H9r0LXpk1r5/tp9KO+Phummv9ka/9q+YbpZ47lGo 42OXs6+1uUxaUqLzBLw3Zsa4P2Fb8eZPPC/bjU/NSxm3MiqxFGckGmoxFxUnAgB9zlkKfwIA AA== Subject: [dpdk-dev] [PATCH v3 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 Acked-by: Bruce Richardson --- 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 20c1a9018..a53a29a35 100644 --- a/lib/librte_eal/rte_eal_version.map +++ b/lib/librte_eal/rte_eal_version.map @@ -384,6 +384,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 Wed Jun 5 10:43:59 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: 54410 X-Patchwork-Delegate: thomas@monjalon.net 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 624E11B9FC; Wed, 5 Jun 2019 12:44:35 +0200 (CEST) Received: from sessmg22.ericsson.net (sessmg22.ericsson.net [193.180.251.58]) by dpdk.org (Postfix) with ESMTP id 222B61BA6F for ; Wed, 5 Jun 2019 12:44:34 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1559731473; x=1562323473; 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=3Ap+CN9zIt0IVSCVp0UkH7TQWCMeT/jmWOComifs1o4=; b=KgY6MH3YDl4RG6ckt1WqSi0QV0lpJXHP5/7ZEu5AMTDXEcQzY/r3y9qUiGMMsGlO MuQnu/lLWd32dxEOLe2aShU+6lPSDKW39eE+V5N89iZb4tnePcq7piKVwitOLbzb AN5TttdvlMFxhSF5kAEh+Pz60ZROZdwuWo5obZOGBew=; X-AuditID: c1b4fb3a-b6bb79e00000189f-38-5cf79d11c0d3 Received: from ESESSMB501.ericsson.se (Unknown_Domain [153.88.183.119]) by sessmg22.ericsson.net (Symantec Mail Security) with SMTP id C4.FD.06303.11D97FC5; Wed, 5 Jun 2019 12:44:33 +0200 (CEST) Received: from ESESBMB504.ericsson.se (153.88.183.171) 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; Wed, 5 Jun 2019 12:44:27 +0200 Received: from selio1a020.lmera.ericsson.se (153.88.183.153) by smtp.internal.ericsson.com (153.88.183.187) with Microsoft SMTP Server id 15.1.1713.5 via Frontend Transport; Wed, 5 Jun 2019 12:44:26 +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 x55AiQRF025408; Wed, 5 Jun 2019 12:44:27 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , , , , =?utf-8?q?Mattias_R=C3=B6nnblom?= Date: Wed, 5 Jun 2019 12:43:59 +0200 Message-ID: <20190605104400.24484-6-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190605104400.24484-1-mattias.ronnblom@ericsson.com> References: <20190516203529.GA642@bricha3-MOBL.ger.corp.intel.com> <20190605104400.24484-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrDLMWRmVeSWpSXmKPExsUyM2J7ua7g3O8xBmtv6lvcWGVvsX1FF5vF u0/bmSxuNZ9ks1h8R86B1ePXgqWsHov3vGTy6Dk5j8nj/b6rbB5Xvq9mDGCN4rJJSc3JLEst 0rdL4MqY87GTteAlb0Xn61aWBsZ93F2MnBwSAiYS/zftZOxi5OIQEjjKKDFv4TJWCOcro8SD z+uZIZwLjBKrt3dDOZcYJbYd/c8C0s8m4Ckx+V03mC0iICSx9ONldpAiZoG9jBKv1s0DSwgL 2Eu8mjQTzGYRUJHYePErE4jNK+Ak8fDgDUaIQ+QlVm84wAxicwo4Szyevh+sRkigWmLC22ao ekGJkzOfgM1hFtCUaN3+mx3Clpdo3jqbGaJeS+L+ki/MExiFZiFpmYWkZRaSlgWMzKsYRYtT i4tz042M9FKLMpOLi/Pz9PJSSzYxAuPg4JbfVjsYDz53PMQowMGoxMN7rfd7jBBrYllxZe4h RgkOZiUR3sTbX2KEeFMSK6tSi/Lji0pzUosPMUpzsCiJ80av3hMjJJCeWJKanZpakFoEk2Xi 4JRqYHRVspuQdfe8aLruxHlfXTLvq+11Yp3amWb/fNXu5oVVNhWPPGXZ31hKdcpYsd/25fme x3MvW/D7tt++p+PFrglzVHPtc/shuzisjC9Y+FTF5nNn5t0SPfDQOFIjZHpB67dPl90uhS3c HtKosi165pInZlt6TecU/Pv6eVbHgmUFpybulD4je0CJpTgj0VCLuag4EQDN432+fwIAAA== Subject: [dpdk-dev] [PATCH v3 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 Acked-by: Bruce Richardson --- 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 Wed Jun 5 10:44:00 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: 54411 X-Patchwork-Delegate: thomas@monjalon.net 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 3E61F1BA8F; Wed, 5 Jun 2019 12:44:38 +0200 (CEST) Received: from sessmg22.ericsson.net (sessmg22.ericsson.net [193.180.251.58]) by dpdk.org (Postfix) with ESMTP id 496D11BA8F for ; Wed, 5 Jun 2019 12:44:34 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1559731473; x=1562323473; 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=AQGw568esTTlYd2rkgaHdF07pRl71eYbZvSR4Wk2VIo=; b=GroCxP8iV8jKD3cehBG+bG7m5B5+7aOQjUZF3oXSZXM9VHfEOg/lHflE7UGbchX+ oM+MguKFpooznEBNY1MWRotniVbIXif6/L8coRzNWbku8ju0TfRwTVwcq7uhlxAq awPMM7b7s0KwlwmCD0mdIfghRfPcHfSEQHTstgkw06s=; X-AuditID: c1b4fb3a-b6bb79e00000189f-39-5cf79d11e275 Received: from ESESSMB501.ericsson.se (Unknown_Domain [153.88.183.119]) by sessmg22.ericsson.net (Symantec Mail Security) with SMTP id E4.FD.06303.11D97FC5; Wed, 5 Jun 2019 12:44:33 +0200 (CEST) Received: from ESESSMB501.ericsson.se (153.88.183.162) 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; Wed, 5 Jun 2019 12:44:27 +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; Wed, 5 Jun 2019 12:44:27 +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 x55AiQRG025408; Wed, 5 Jun 2019 12:44:27 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , , , , =?utf-8?q?Mattias_R=C3=B6nnblom?= Date: Wed, 5 Jun 2019 12:44:00 +0200 Message-ID: <20190605104400.24484-7-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190605104400.24484-1-mattias.ronnblom@ericsson.com> References: <20190516203529.GA642@bricha3-MOBL.ger.corp.intel.com> <20190605104400.24484-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrJLMWRmVeSWpSXmKPExsUyM2J7ua7g3O8xBveuGFjcWGVvsX1FF5vF u0/bmSxuNZ9ks1h8R86B1ePXgqWsHov3vGTy6Dk5j8nj/b6rbB5Xvq9mDGCN4rJJSc3JLEst 0rdL4MrYMe0bc0E3f8XRdToNjOd4uhg5OSQETCRmnJrCBmILCRxllGh/Ht7FyAVkf2WUuHew mx3CucAo0XT4JDtE1SVGiXkHUkFsNgFPicnvullAbBEBIYmlHy+DNTAL7GWUeLVuHlhCWMBN on3+brBmFgEVieePj4Ot4xVwkmhseMwMcYa8xOoNB8BsTgFnicfT9zNBLKuWmPC2mQmiXlDi 5MwnYDOZBTQlWrf/Zoew5SWat85mhqjXkri/5AvzBEahWUhaZiFpmYWkZQEj8ypG0eLU4uLc dCMjvdSizOTi4vw8vbzUkk2MwAg4uOW31Q7Gg88dDzEKcDAq8fBe6/0eI8SaWFZcmXuIUYKD WUmEN/H2lxgh3pTEyqrUovz4otKc1OJDjNIcLErivNGr98QICaQnlqRmp6YWpBbBZJk4OKUa GN0KM5dqrewqWXF+ptW3TF/ZJm2+5gm8ehH9sQsXx1/eWVO60bhw5aqo694c6iaRsuL9YRbP RNktWtZs1Oh3XMZnet1j/vSOhaVLXDeuLs2cVzDVNN0qR+iC3t6Z59uvfBAuufgvK99W/oCc +hPPZQo8nzo3lagfutzin6oXltbhlHQgXP6ZEktxRqKhFnNRcSIA9HHJ83wCAAA= Subject: [dpdk-dev] [PATCH v3 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 Acked-by: Bruce Richardson --- MAINTAINERS | 6 ++++++ doc/guides/rel_notes/release_19_08.rst | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index d0bf259b8..fad4a7bc0 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 c199270c5..b6195dd14 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 -------------