From patchwork Fri Jun 28 09:01:20 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: 55584 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 4F9141B99A; Fri, 28 Jun 2019 11:01:31 +0200 (CEST) Received: from sesbmg22.ericsson.net (sesbmg22.ericsson.net [193.180.251.48]) by dpdk.org (Postfix) with ESMTP id 263C24C6C for ; Fri, 28 Jun 2019 11:01:30 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1561712489; x=1564304489; 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=+jqqRp1ZocOB69gs+FBWdnMQ0sOqM8XEJhNaVIhZLx8=; b=Ejacjh37rpj3+kF61u24QiMguA4EGCxD3NL4Xz+2HfFJB0HfK/V9BxjTT3GlpnXN 74DH/xmT0M3Z5aq0Rl+RDsUvSGSLDmcOVUQu4q8Vp1PWCzlDkPXXi18wYqilLIoR 4w1D/hzY0iqXLxWwV234fW7BVSrKjT2OVf6Va75lLgw=; X-AuditID: c1b4fb30-6ddff70000001814-7f-5d15d7697883 Received: from ESESSMB504.ericsson.se (Unknown_Domain [153.88.183.122]) by sesbmg22.ericsson.net (Symantec Mail Security) with SMTP id CD.97.06164.967D51D5; Fri, 28 Jun 2019 11:01:29 +0200 (CEST) Received: from ESESBMB503.ericsson.se (153.88.183.170) by ESESSMB504.ericsson.se (153.88.183.122) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Fri, 28 Jun 2019 11:01:29 +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; Fri, 28 Jun 2019 11:01:29 +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 x5S91SYV019342; Fri, 28 Jun 2019 11:01:29 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , , , , , =?utf-8?q?Mattias_R=C3=B6nnblom?= Date: Fri, 28 Jun 2019 11:01:20 +0200 Message-ID: <20190628090124.16849-2-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190628090124.16849-1-mattias.ronnblom@ericsson.com> References: <20190605104400.24484-1-mattias.ronnblom@ericsson.com> <20190628090124.16849-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrKLMWRmVeSWpSXmKPExsUyM2J7lW7mddFYg+5eHYsbq+wttq/oYrN4 92k7k8Wt5pNsFovvyFl8enCCxYHN49eCpawei/e8ZPI4dnMau0fPyXlMHu/3XWXzuPJ9NWMA WxSXTUpqTmZZapG+XQJXxq5ld5kL5oZUrJ73kaWB8Z9rFyMnh4SAicTOrsVMXYxcHEICRxkl 9kxZzw7hfGOUeHTwJFTmIqPE0U3TGEFahAQuM0oc354LYrMJeEpMftfNAmKLCAhJLP14Gayb WeAao8TXSy+YQRLCAkESR5+dBCtiEVCVWPxkPhOIzSvgJPG2aQsrxB3yEqs3HACr5xRwluha 28sOsaxa4ueJh2wQ9YISJ2c+AZvDLKAp0br9NzuELS/RvHU2M0S9lsT9JV+YJzAKzULSMgtJ yywkLQsYmVcxihanFiflphsZ6aUWZSYXF+fn6eWllmxiBEbGwS2/DXYwvnzueIhRgINRiYd3 wR7RWCHWxLLiytxDjBIczEoivJLnRGKFeFMSK6tSi/Lji0pzUosPMUpzsCiJ8673/hcjJJCe WJKanZpakFoEk2Xi4JRqYAyoS0q3eLA/xHana5mHs8z+yScTrwjqXFt/3u2J17vLmXcqp4g5 ycTHKCWLmygFzPHNdteRvetzRHJyzOlzLhekimMFk7/3/PVclPy96W+258PZAQUc209c+mF+ hDOD76hD8/sAjY6s1x+ceLl3uzLY54SEXPj7nV3ou62Kht02Scel+sXuSizFGYmGWsxFxYkA eEfTnIgCAAA= Subject: [dpdk-dev] [PATCH v4 1/5] 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 --- MAINTAINERS | 5 + doc/guides/rel_notes/release_19_08.rst | 9 ++ 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 + 10 files changed, 169 insertions(+), 22 deletions(-) create mode 100644 lib/librte_eal/common/rte_random.c diff --git a/MAINTAINERS b/MAINTAINERS index 0c3b48920..75775129d 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -227,6 +227,11 @@ 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 + 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 3da266705..7080d1f47 100644 --- a/doc/guides/rel_notes/release_19_08.rst +++ b/doc/guides/rel_notes/release_19_08.rst @@ -99,6 +99,15 @@ New Features Updated ``librte_telemetry`` to fetch the global metrics from the ``librte_metrics`` library. +* **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. Removed Items ------------- 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 97633a55a..3ccc5503c 100644 --- a/lib/librte_eal/freebsd/eal/eal.c +++ b/lib/librte_eal/freebsd/eal/eal.c @@ -758,8 +758,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 d96ed3a7c..5f2083c4b 100644 --- a/lib/librte_eal/linux/eal/eal.c +++ b/lib/librte_eal/linux/eal/eal.c @@ -1122,8 +1122,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 Fri Jun 28 09:01:21 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: 55585 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 376551B99E; Fri, 28 Jun 2019 11:01:37 +0200 (CEST) Received: from sesbmg22.ericsson.net (sesbmg22.ericsson.net [193.180.251.48]) by dpdk.org (Postfix) with ESMTP id 4E9D81B99A for ; Fri, 28 Jun 2019 11:01:30 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1561712489; x=1564304489; 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=osiYzTIgt+Sd6qE1DOqagjLt36VM7pB6xHqnDlhu5F4=; b=Ao0CIC0o+g/38N6BKXdpMnHwcd2CjHLmmUc8oPEiPMB/jBnmHdmsHKetvZYNIN32 +YYn2ke2kfgXNwh86z2v9mb+kEswjpVyLE0uKR252YLJ8gTK7gKXRP/avDsqdyvc WzXapyknp4uWH8sdOQK70BjBdtxpbWTMJi4kf0rSftA=; X-AuditID: c1b4fb30-6f5ff70000001814-80-5d15d7696c67 Received: from ESESBMB505.ericsson.se (Unknown_Domain [153.88.183.118]) by sesbmg22.ericsson.net (Symantec Mail Security) with SMTP id 3E.97.06164.967D51D5; Fri, 28 Jun 2019 11:01:29 +0200 (CEST) Received: from ESESBMB505.ericsson.se (153.88.183.172) by ESESBMB505.ericsson.se (153.88.183.172) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Fri, 28 Jun 2019 11:01:29 +0200 Received: from selio1a020.lmera.ericsson.se (153.88.183.153) by smtp.internal.ericsson.com (153.88.183.188) with Microsoft SMTP Server id 15.1.1713.5 via Frontend Transport; Fri, 28 Jun 2019 11:01:29 +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 x5S91SYW019342; Fri, 28 Jun 2019 11:01:29 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , , , , , =?utf-8?q?Mattias_R=C3=B6nnblom?= Date: Fri, 28 Jun 2019 11:01:21 +0200 Message-ID: <20190628090124.16849-3-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190628090124.16849-1-mattias.ronnblom@ericsson.com> References: <20190605104400.24484-1-mattias.ronnblom@ericsson.com> <20190628090124.16849-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrKLMWRmVeSWpSXmKPExsUyM2J7mW7mddFYg98nDC1urLK32L6ii83i 3aftTBa3mk+yWSy+I2fx6cEJFgc2j18LlrJ6LN7zksnj2M1p7B49J+cxebzfd5XN48r31YwB bFFcNimpOZllqUX6dglcGbe2fGcueCVV8en+c6YGxs+iXYycHBICJhK/Zj1g6WLk4hASOMoo MWlOOyOE841RYvfaU6wQzkVGibUrbrFBOJcZJaY87mEE6WcT8JSY/K6bBcQWERCSWPrxMjtI EbPANUaJr5deMIMkhAUCJU68PQfUzcHBIqAq0XPABSTMK+AksfHDGUaIO+QlVm84AFbOKeAs 0bW2lx3EFhKolvh54iEbRL2gxMmZT8B2MQtoSrRu/80OYctLNG+dzQxRryVxf8kX5gmMQrOQ tMxC0jILScsCRuZVjKLFqcVJuelGRnqpRZnJxcX5eXp5qSWbGIGRcXDLb4MdjC+fOx5iFOBg VOLhXbBHNFaINbGsuDL3EKMEB7OSCK/kOZFYId6UxMqq1KL8+KLSnNTiQ4zSHCxK4rzrvf/F CAmkJ5akZqemFqQWwWSZODilGhjzd2+8Ub+0ZVqAx1cNLTdj9+IaYa7JZ3l+s5YHhlyXe3p8 p80tX4ZbP+OFJ6UwdjM88Wz+PSF3R0toidqr/Rwqbs42dQftg/55Tp61lJ2XSdqrotGR7UlA xrpMdwkB+56n/63/cLpEJ+2b/HbnJ+PYWzsiIxi48jg850yftHndvvBdPz7/vK/EUpyRaKjF XFScCAAfZ5+riAIAAA== Subject: [dpdk-dev] [PATCH v4 2/5] 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 --- MAINTAINERS | 1 + app/test/Makefile | 1 + app/test/meson.build | 2 ++ app/test/test_rand_perf.c | 75 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 app/test/test_rand_perf.c diff --git a/MAINTAINERS b/MAINTAINERS index 75775129d..bbec1982c 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -231,6 +231,7 @@ 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 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/meson.build b/app/test/meson.build index 4de856f93..a47e001bf 100644 --- a/app/test/meson.build +++ b/app/test/meson.build @@ -89,6 +89,7 @@ test_sources = files('commands.c', 'test_power_acpi_cpufreq.c', 'test_power_kvm_vm.c', 'test_prefetch.c', + 'test_rand_perf.c', 'test_rcu_qsbr.c', 'test_rcu_qsbr_perf.c', 'test_reciprocal_division.c', @@ -254,6 +255,7 @@ perf_test_names = [ 'pmd_perf_autotest', 'stack_perf_autotest', 'stack_nb_perf_autotest', + 'rand_perf_autotest' ] # All test cases in driver_test_names list are non-parallel 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 Fri Jun 28 09:01:22 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: 55589 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 2800F1B9C3; Fri, 28 Jun 2019 11:01:46 +0200 (CEST) Received: from sessmg22.ericsson.net (sessmg22.ericsson.net [193.180.251.58]) by dpdk.org (Postfix) with ESMTP id DEFAD1B99C for ; Fri, 28 Jun 2019 11:01:31 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1561712491; x=1564304491; 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=axKpiD3JZAYuhYH8SAkjGKdcEpHhhwrBuA+d9/QHSsynBlE1wNpf3gJWv+EQo3c9 7BwO9AVrcbgf8fCf4afVUw5Tu20aTsAAznXnw4EHFhmfPzvmOaHJVn7+rqLW2aYA hVqy3DGCGvpFTNtrg1KUkklR0SJV+u5ssEVek+lwxOs=; X-AuditID: c1b4fb3a-b6bb79e00000189f-00-5d15d76ba0bd Received: from ESESSMB505.ericsson.se (Unknown_Domain [153.88.183.123]) by sessmg22.ericsson.net (Symantec Mail Security) with SMTP id 26.64.06303.B67D51D5; Fri, 28 Jun 2019 11:01:31 +0200 (CEST) Received: from ESESSMR504.ericsson.se (153.88.183.126) by ESESSMB505.ericsson.se (153.88.183.193) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Fri, 28 Jun 2019 11:01:29 +0200 Received: from ESESBMB502.ericsson.se (153.88.183.169) 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; Fri, 28 Jun 2019 11:01:29 +0200 Received: from selio1a020.lmera.ericsson.se (153.88.183.153) by smtp.internal.ericsson.com (153.88.183.185) with Microsoft SMTP Server id 15.1.1713.5 via Frontend Transport; Fri, 28 Jun 2019 11:01:30 +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 x5S91SYX019342; Fri, 28 Jun 2019 11:01:29 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , , , , , =?utf-8?q?Mattias_R=C3=B6nnblom?= Date: Fri, 28 Jun 2019 11:01:22 +0200 Message-ID: <20190628090124.16849-4-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190628090124.16849-1-mattias.ronnblom@ericsson.com> References: <20190605104400.24484-1-mattias.ronnblom@ericsson.com> <20190628090124.16849-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmpmkeLIzCtJLcpLzFFi42KZGbG9Wjf7umisQet2CYsbq+wttq/oYrN4 92k7k8Wt5pNsFovvyFl8enCCxYHN49eCpawei/e8ZPI4dnMau0fPyXlMHu/3XWXzuPJ9NWMA WxSXTUpqTmZZapG+XQJXxqypbcwFz0QrlndNZ2lgXCnUxcjJISFgIjH90mXWLkYuDiGBo4wS HQvOMUM43xgl5iyZhOB8PP2QCcK5yChx/G4DI4RzmVHizOYNrCDD2AQ8JSa/62YBsUUEhCSW frzMDlLELHCNUeLrpRfMIAlhASeJk+8Xs4PYLAKqEu+3r2ICsXmB4mtvzmKFuEpeYvWGA2D1 nALOEl1re8HqhQSqJX6eeMgGUS8ocXLmE7BlzAKaEq3bf7ND2PISzVtnM0PUa0ncX/KFeQKj 8CwkLbOQtMxC0rKAkXkVo2hxanFxbrqRkV5qUWZycXF+nl5easkmRmDUHNzy22oH48HnjocY BTgYlXh4U3eLxgqxJpYVV+YeYpTgYFYS4ZU8JxIrxJuSWFmVWpQfX1Sak1p8iFGag0VJnHe9 978YIYH0xJLU7NTUgtQimCwTB6dUA6PKDtcHN3Yp8Xz6VelWZODvwXHcRzsg6q6C6sp7PJuM Wf0WR9SfqxBV379s0SONyXU1eoEK/NnzT7gU/c+ZwrVU2yXT1CfXnC/dOFdutfzsu2zXFG6t Obtj8mf1TfEs4V5z5hsf4Ag+GSl6ZOpJr56S9vpXXb/KHO5PUpwspi/TJn07/FtmpBJLcUai oRZzUXEiAKHYzjmWAgAA Subject: [dpdk-dev] [PATCH v4 3/5] 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 Fri Jun 28 09:01:23 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: 55587 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 81B571B9B2; Fri, 28 Jun 2019 11:01:41 +0200 (CEST) Received: from sessmg22.ericsson.net (sessmg22.ericsson.net [193.180.251.58]) by dpdk.org (Postfix) with ESMTP id DCB9E1B99A for ; Fri, 28 Jun 2019 11:01:30 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1561712490; x=1564304490; 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=CMG/8U/KE3q5YcCNGl5DRWSxMs9JxUK6mx9QHy4Aug8=; b=fncbLOa7z6HiFvPt0hKVNgb/btM1gGY8rIcv8BakwZfAp0mqVePpYkjnLyKEehTm JVTGPJt6OFZUQMxNMd+e81xW/MYkbuBK6K8Y3SOVwKpuFFyvPLdOO5IMM0ziVVw7 3A+KRGeiH1iHop/1psLQA92XYw1HpC1Ltl4Q4Antxso=; X-AuditID: c1b4fb3a-6f1ff7000000189f-f7-5d15d76ac059 Received: from ESESBMB503.ericsson.se (Unknown_Domain [153.88.183.116]) by sessmg22.ericsson.net (Symantec Mail Security) with SMTP id 84.64.06303.A67D51D5; Fri, 28 Jun 2019 11:01:30 +0200 (CEST) Received: from ESESBMR506.ericsson.se (153.88.183.202) 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; Fri, 28 Jun 2019 11:01:29 +0200 Received: from ESESBMB501.ericsson.se (153.88.183.168) by ESESBMR506.ericsson.se (153.88.183.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Fri, 28 Jun 2019 11:01:29 +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; Fri, 28 Jun 2019 11:01:30 +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 x5S91SYY019342; Fri, 28 Jun 2019 11:01:29 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , , , , , =?utf-8?q?Mattias_R=C3=B6nnblom?= Date: Fri, 28 Jun 2019 11:01:23 +0200 Message-ID: <20190628090124.16849-5-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190628090124.16849-1-mattias.ronnblom@ericsson.com> References: <20190605104400.24484-1-mattias.ronnblom@ericsson.com> <20190628090124.16849-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmpkkeLIzCtJLcpLzFFi42KZGbG9RDfrumiswZ9JAhY3VtlbbF/RxWbx 7tN2JotbzSfZLBbfkbP49OAEiwObx68FS1k9Fu95yeRx7OY0do+ek/OYPN7vu8rmceX7asYA tigum5TUnMyy1CJ9uwSujAs7m9kKHihUfDxZ2MDYIt3FyMkhIWAisfb6DjYQW0jgKKPEp+VV XYxcQPY3Rol7W9sY4Zztx35AORcZJTo372GHcC4zSjxYtwasn03AU2Lyu24WEFtEQEhi6cfL YEXMAtcYJb5eesEMkhAWCJS4deAFWBGLgKrEurP3GEFsXgEniRNrdzJBHCUvsXrDAbB6TgFn ia61vewQB1ZL/DzxkA2iXlDi5MwnYHOYBTQlWrf/Zoew5SWat85mhqjXkri/5AvzBEbhWUha ZiFpmYWkZQEj8ypG0eLU4uLcdCMjvdSizOTi4vw8vbzUkk2MwIg5uOW31Q7Gg88dDzEKcDAq 8fCm7haNFWJNLCuuzD3EKMHBrCTCK3lOJFaINyWxsiq1KD++qDQntfgQozQHi5I473rvfzFC AumJJanZqakFqUUwWSYOTqkGRnv1J135mWEmR183/rzbtPhXclHm6aA7U9MOXV8X2cU5sTzl 27aXmqd7BQ527LLJK15wvUcs44jY39MOLhMdE16ZCc6Wu6+jr7Rxkc4a/vd7nnTMemi2d/ef Fe/Fb+5yPs//Z4f81L5TGw6bmM36VdLt6v7ln3v5W6lrZV8XCazlNropdLnbtU+JpTgj0VCL uag4EQCoPdn4lAIAAA== Subject: [dpdk-dev] [PATCH v4 4/5] 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 --- doc/guides/rel_notes/release_19_08.rst | 5 ++- 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 + 4 files changed, 62 insertions(+), 1 deletion(-) diff --git a/doc/guides/rel_notes/release_19_08.rst b/doc/guides/rel_notes/release_19_08.rst index 7080d1f47..f1c83d603 100644 --- a/doc/guides/rel_notes/release_19_08.rst +++ b/doc/guides/rel_notes/release_19_08.rst @@ -109,6 +109,10 @@ New Features 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 ------------- @@ -125,7 +129,6 @@ Removed Items * build: armv8 crypto extension is disabled. - API Changes ----------- 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 Fri Jun 28 09:01:24 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: 55588 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 C77971B9BD; Fri, 28 Jun 2019 11:01:43 +0200 (CEST) Received: from sessmg23.ericsson.net (sessmg23.ericsson.net [193.180.251.45]) by dpdk.org (Postfix) with ESMTP id 0C5AC1B99B for ; Fri, 28 Jun 2019 11:01:30 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/relaxed; q=dns/txt; i=@ericsson.com; t=1561712490; x=1564304490; 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=WBYmIqQhbRTnvwGw2ROPQfatvOm/vizU2IxNDVhIsV7cSsHUujiqnTvh/UALpyby ApcMKRvrtpO7sQpc3P9FU5rRuKOXESGlantuRlDo5hjjXDU1LLVD6xeMTqNOzVWS oZLrx4m2p/mmy+wksOEMm64X/p67Q/0dmHl6tgLDCms=; X-AuditID: c1b4fb2d-195ff70000001a6d-06-5d15d76a8bac Received: from ESESBMB503.ericsson.se (Unknown_Domain [153.88.183.116]) by sessmg23.ericsson.net (Symantec Mail Security) with SMTP id 55.5C.06765.A67D51D5; Fri, 28 Jun 2019 11:01:30 +0200 (CEST) Received: from ESESSMR502.ericsson.se (153.88.183.110) by ESESBMB503.ericsson.se (153.88.183.116) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Fri, 28 Jun 2019 11:01:29 +0200 Received: from ESESSMB505.ericsson.se (153.88.183.166) by ESESSMR502.ericsson.se (153.88.183.110) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1713.5; Fri, 28 Jun 2019 11:01:29 +0200 Received: from selio1a020.lmera.ericsson.se (153.88.183.153) by smtp.internal.ericsson.com (153.88.183.193) with Microsoft SMTP Server id 15.1.1713.5 via Frontend Transport; Fri, 28 Jun 2019 11:01:29 +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 x5S91SYZ019342; Fri, 28 Jun 2019 11:01:29 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , , , , , =?utf-8?q?Mattias_R=C3=B6nnblom?= Date: Fri, 28 Jun 2019 11:01:24 +0200 Message-ID: <20190628090124.16849-6-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190628090124.16849-1-mattias.ronnblom@ericsson.com> References: <20190605104400.24484-1-mattias.ronnblom@ericsson.com> <20190628090124.16849-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmpikeLIzCtJLcpLzFFi42KZGbG9RDfrumisweujUhY3VtlbbF/RxWbx 7tN2JotbzSfZLBbfkbP49OAEiwObx68FS1k9Fu95yeRx7OY0do+ek/OYPN7vu8rmceX7asYA tigum5TUnMyy1CJ9uwSujDkfO1kLXvJWdL5uZWlg3MfdxcjJISFgInF97262LkYuDiGBo4wS x3a/Z4FwvjFKPG58zQjnrOt7C5W5yCixYPIWdgjnMqPE75dX2ECGsQl4Skx+180CYosICEks /XgZrIhZ4BqjxNdLL5i7GDk4hAXsJc71eILUsAioSjxrfM8IYvMKOEkcfXaLDeIoeYnVGw4w g9icAs4SXWt72UFsIYFqiZ8nHrJB1AtKnJz5BGwXs4CmROv23+wQtrxE89bZzBD1WhL3l3xh nsAoPAtJyywkLbOQtCxgZF7FKFqcWlycm25krJdalJlcXJyfp5eXWrKJERgzB7f81t3BuPq1 4yFGAQ5GJR5ew92isUKsiWXFlbmHGCU4mJVEeCXPicQK8aYkVlalFuXHF5XmpBYfYpTmYFES 513v/S9GSCA9sSQ1OzW1ILUIJsvEwSnVwKj7cWX5mmSWOqu4PzdVFytr8LQw/5Ra4N4r+Lnu hPIOIdHOv9xCnko8nc7K3oYlKWUT65yUHvKud3mj7aJ9xdV9Zf6K9QKPT3EVvuw856TxMvaR yeL7sXu97txel1Ja84Ij7LPfymvBV+44x3MWxxass71sZ5G1u+NHf3LiuibWlWKnCh9ZKbEU ZyQaajEXFScCALSjOzyVAgAA Subject: [dpdk-dev] [PATCH v4 5/5] 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; }