1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2019 Ericsson AB 3 */ 4 5 #include <inttypes.h> 6 #include <stdio.h> 7 8 #include <rte_common.h> 9 #include <rte_cycles.h> 10 #include <rte_random.h> 11 12 #include "test.h" 13 14 static volatile uint64_t vsum; 15 16 #define ITERATIONS (100000000) 17 18 #define BEST_CASE_BOUND (1<<16) 19 #define WORST_CASE_BOUND (BEST_CASE_BOUND + 1) 20 21 enum rand_type { 22 rand_type_64, 23 rand_type_float, 24 rand_type_bounded_best_case, 25 rand_type_bounded_worst_case 26 }; 27 28 static const char * rand_type_desc(enum rand_type rand_type)29rand_type_desc(enum rand_type rand_type) 30 { 31 switch (rand_type) { 32 case rand_type_64: 33 return "Full 64-bit [rte_rand()]"; 34 case rand_type_float: 35 return "Floating point [rte_drand()]"; 36 case rand_type_bounded_best_case: 37 return "Bounded average best-case [rte_rand_max()]"; 38 case rand_type_bounded_worst_case: 39 return "Bounded average worst-case [rte_rand_max()]"; 40 default: 41 return NULL; 42 } 43 } 44 45 static __rte_always_inline void test_rand_perf_type(enum rand_type rand_type)46test_rand_perf_type(enum rand_type rand_type) 47 { 48 uint64_t start; 49 uint32_t i; 50 uint64_t end; 51 uint64_t sum = 0; 52 uint64_t op_latency; 53 54 start = rte_rdtsc(); 55 56 for (i = 0; i < ITERATIONS; i++) { 57 switch (rand_type) { 58 case rand_type_64: 59 sum += rte_rand(); 60 break; 61 case rand_type_float: 62 sum += 1000. * rte_drand(); 63 break; 64 case rand_type_bounded_best_case: 65 sum += rte_rand_max(BEST_CASE_BOUND); 66 break; 67 case rand_type_bounded_worst_case: 68 sum += rte_rand_max(WORST_CASE_BOUND); 69 break; 70 } 71 } 72 73 end = rte_rdtsc(); 74 75 /* to avoid an optimizing compiler removing the whole loop */ 76 vsum = sum; 77 78 op_latency = (end - start) / ITERATIONS; 79 80 printf("%s: %"PRId64" TSC cycles/op\n", rand_type_desc(rand_type), 81 op_latency); 82 } 83 84 static int test_rand_perf(void)85test_rand_perf(void) 86 { 87 rte_srand(42); 88 89 printf("Pseudo-random number generation latencies:\n"); 90 91 test_rand_perf_type(rand_type_64); 92 test_rand_perf_type(rand_type_float); 93 test_rand_perf_type(rand_type_bounded_best_case); 94 test_rand_perf_type(rand_type_bounded_worst_case); 95 96 return 0; 97 } 98 99 REGISTER_PERF_TEST(rand_perf_autotest, test_rand_perf); 100