From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by dpdk.space (Postfix) with ESMTP id D00BDA0096 for ; Mon, 8 Apr 2019 14:30:47 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 92CAA2C54; Mon, 8 Apr 2019 14:30:47 +0200 (CEST) Received: from sessmg22.ericsson.net (sessmg22.ericsson.net [193.180.251.58]) by dpdk.org (Postfix) with ESMTP id F26DD2C24 for ; Mon, 8 Apr 2019 14:30:46 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1554726646; x=1557318646; 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=ujLrsN6Wq+geN6v+VL00LAfWCUF73KMZFs4yGVEvHqM=; b=SepuksCkalmJ56m3anqOVeBm7jpRqlg/2IuLQMpjbFCVQIKs6ZeHOQKDyRss7yCW hbfxTKF/N2TMHc1aCDqzDLi4EsME8Du9689gUTAntuPN5oJBFOtcgYolLUCvKW1q SADqXZVWQ8cD1soJk3WB2JvvSag1D38HHvSgOWBFcfk=; X-AuditID: c1b4fb3a-ccb299e000005461-b4-5cab3ef6993d Received: from ESESSMB501.ericsson.se (Unknown_Domain [153.88.183.119]) by sessmg22.ericsson.net (Symantec Mail Security) with SMTP id 70.D7.21601.6FE3BAC5; Mon, 8 Apr 2019 14:30:46 +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; Mon, 8 Apr 2019 14:30:45 +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; Mon, 8 Apr 2019 14:30:45 +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 x38CUjiE011282; Mon, 8 Apr 2019 14:30:45 +0200 (CEST) From: =?UTF-8?q?Mattias=20R=C3=B6nnblom?= To: CC: =?UTF-8?q?Mattias=20R=C3=B6nnblom?= Date: Mon, 8 Apr 2019 14:30:27 +0200 Message-ID: <20190408123029.6701-1-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190405135030.09c5291a@shemminger-XPS-13-9360> References: <20190405135030.09c5291a@shemminger-XPS-13-9360> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrMLMWRmVeSWpSXmKPExsUyM2J7ue43u9UxBse2aFq8+7SdyYHR49eC pawBjFFcNimpOZllqUX6dglcGbNP3mQqWOZQ8W6nRAPjZ9MuRk4OCQETiVX9PWxdjFwcQgJH GSXmbt7DCOF8ZZR48WIRC4RzgVHiYMduqLJLjBKLZl1hBelnE/CUmPyumwXEFhEQklj68TI7 iM0s4C7x/tFypi5GDg5hAVeJf0vNQMIsAioSV7cfYwaxeQUcJWY37GGDOENeYvWGA2BxTgFb if7GnWBjhARsJDbMf8kKUS8ocXLmExaI8ZoSrdt/Q62Sl2jeOpsZol5L4v6SL8wTGIVmIWmZ haRlFpKWBYzMqxhFi1OLi3PTjYz0Uosyk4uL8/P08lJLNjECg/bglt9WOxgPPnc8xCjAwajE w6tnvDpGiDWxrLgy9xCjBAezkgjvzqmrYoR4UxIrq1KL8uOLSnNSiw8xSnOwKInz/hESjBES SE8sSc1OTS1ILYLJMnFwSjUwhrt7ZT5+/tHr6rLZ8R88vCYudJE+MS/p8xt/1VXPK/5sLnl7 ZHYqU/rhT3GsZ3fxlc+7/uvgTm2/F0p9W98n5r3UzLY8+2Li5cSf07Tl0juvVxyZnOmcNye/ pOPFuxWTb3y3TJD0UFWK7v1qJPnx1o3m9wpPduVlVfXwOz8+cuXZRIfSWw+vlyixFGckGmox FxUnAgBQEfshVgIAAA== Subject: [dpdk-dev] [RFC 1/3] Replace lrand48-based rte_rand with LFSR generator 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" Message-ID: <20190408123027.KVANlMCbt6KjrGy6BWz1bvKYkcuFyToqLPBqykeWUG8@z> This commit replaces rte_rand()'s use of lrand48() with a DPDK-native combined Linear Feedback Shift Register (LFSR) (also known as Tausworthe) pseudo-number generator, with four sequences. This generator is faster and produces better quality random numbers than libc's lrand48() implementation. This implementation, as opposed to lrand48(), is multi-thread safe in regards to concurrent rte_rand() calls from different lcore threads. Signed-off-by: Mattias Rönnblom --- lib/librte_eal/common/include/rte_random.h | 25 ++--- lib/librte_eal/common/meson.build | 1 + lib/librte_eal/common/rte_random.c | 104 +++++++++++++++++++++ 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, 119 insertions(+), 19 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..bca85a672 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,14 +24,15 @@ 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. @@ -41,18 +41,13 @@ rte_srand(uint64_t seedval) * congruential algorithm and 48-bit integer arithmetic, called twice * to generate a 64-bit value. * + * 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..b8fb7006e --- /dev/null +++ b/lib/librte_eal/common/rte_random.c @@ -0,0 +1,104 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2019 Ericsson AB + */ + +#include + +#include +#include +#include +#include + +struct rte_rand_state { + uint32_t z1; + uint32_t z2; + uint32_t z3; + uint32_t z4; +} __rte_cache_aligned; + +static struct rte_rand_state rand_states[RTE_MAX_LCORE]; + +static inline uint32_t +__rte_rand_lcg32(uint32_t *seed) +{ + *seed = 1103515245U * *seed + 12345; + return *seed; +} + +static uint32_t +__rte_rand_lfsr113_gen_seed(uint32_t *seed, uint32_t min_value) +{ + uint32_t res = __rte_rand_lcg32(seed); + + return res < min_value ? res + min_value : res; +} + +static void +__rte_srand_lfsr113(uint32_t seed, struct rte_rand_state *state) +{ + uint32_t lcg32_seed = seed; + state->z1 = __rte_rand_lfsr113_gen_seed(&lcg32_seed, 2U); + state->z2 = __rte_rand_lfsr113_gen_seed(&lcg32_seed, 8U); + state->z3 = __rte_rand_lfsr113_gen_seed(&lcg32_seed, 16U); + state->z4 = __rte_rand_lfsr113_gen_seed(&lcg32_seed, 128U); +} + +void __rte_experimental +rte_srand(uint64_t seedval) +{ + 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_lfsr113(seedval + lcore_id, &rand_states[lcore_id]); +} + +static __rte_always_inline uint32_t +__rte_rand_lfsr113(uint32_t s, uint32_t a, uint32_t b, uint32_t c, uint32_t d) +{ + return ((s & c) << d) ^ (((s << a) ^ s) >> b); +} + +static __rte_always_inline uint32_t +__rte_rand32(struct rte_rand_state *state) +{ + state->z1 = __rte_rand_lfsr113(state->z1, 6U, 13U, 4294967294U, 18U); + state->z2 = __rte_rand_lfsr113(state->z2, 2U, 27U, 4294967288U, 2U); + state->z3 = __rte_rand_lfsr113(state->z3, 13U, 21U, 4294967280U, 7U); + state->z4 = __rte_rand_lfsr113(state->z4, 3U, 12U, 4294967168U, 13U); + + return state->z1 ^ state->z2 ^ state->z3 ^ state->z4; +} + +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_experimental +rte_rand(void) +{ + struct rte_rand_state *state; + uint64_t low; + uint64_t high; + + state = __rte_rand_get_state(); + + low = __rte_rand32(state); + high = __rte_rand32(state); + + return low | (high << 32); +} + +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 f7ae62d7b..c2bdf0a67 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 d6e375135..0d60668fa 100644 --- a/lib/librte_eal/rte_eal_version.map +++ b/lib/librte_eal/rte_eal_version.map @@ -366,10 +366,12 @@ EXPERIMENTAL { rte_mp_request_async; rte_mp_sendmsg; rte_option_register; + rte_rand; rte_realloc_socket; rte_service_lcore_attr_get; rte_service_lcore_attr_reset_all; rte_service_may_be_active; rte_socket_count; rte_socket_id_by_idx; + rte_srand; }; -- 2.17.1