1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2014 Intel Corporation 3 */ 4 5 #include <stdio.h> 6 #include <stdint.h> 7 8 #include <rte_common.h> 9 #include <rte_cycles.h> 10 11 #include "test.h" 12 13 #define N 10000 14 15 /* 16 * Cycles test 17 * =========== 18 * 19 * - Loop N times and check that the timer always increments and 20 * never decrements during this loop. 21 * 22 * - Wait one second using rte_usleep() and check that the increment 23 * of cycles is correct with regard to the frequency of the timer. 24 */ 25 26 static int 27 check_wait_one_second(void) 28 { 29 uint64_t cycles, prev_cycles; 30 uint64_t hz = rte_get_timer_hz(); 31 uint64_t max_inc = (hz / 100); /* 10 ms max between 2 reads */ 32 33 /* check that waiting 1 second is precise */ 34 prev_cycles = rte_get_timer_cycles(); 35 rte_delay_us(1000000); 36 cycles = rte_get_timer_cycles(); 37 38 if ((uint64_t)(cycles - prev_cycles) > (hz + max_inc)) { 39 printf("delay_us is not accurate: too long\n"); 40 return -1; 41 } 42 if ((uint64_t)(cycles - prev_cycles) < (hz - max_inc)) { 43 printf("delay_us is not accurate: too short\n"); 44 return -1; 45 } 46 47 return 0; 48 } 49 50 static int 51 test_cycles(void) 52 { 53 unsigned i; 54 uint64_t start_cycles, cycles, prev_cycles; 55 uint64_t hz = rte_get_timer_hz(); 56 uint64_t max_inc = (hz / 100); /* 10 ms max between 2 reads */ 57 58 /* check that the timer is always incrementing */ 59 start_cycles = rte_get_timer_cycles(); 60 prev_cycles = start_cycles; 61 for (i=0; i<N; i++) { 62 cycles = rte_get_timer_cycles(); 63 if ((uint64_t)(cycles - prev_cycles) > max_inc) { 64 printf("increment too high or going backwards\n"); 65 return -1; 66 } 67 prev_cycles = cycles; 68 } 69 70 return check_wait_one_second(); 71 } 72 73 REGISTER_TEST_COMMAND(cycles_autotest, test_cycles); 74 75 /* 76 * One second precision test with rte_delay_us_sleep. 77 */ 78 79 static int 80 test_delay_us_sleep(void) 81 { 82 int rv; 83 84 rte_delay_us_callback_register(rte_delay_us_sleep); 85 rv = check_wait_one_second(); 86 /* restore original delay function */ 87 rte_delay_us_callback_register(rte_delay_us_block); 88 89 return rv; 90 } 91 92 REGISTER_TEST_COMMAND(delay_us_sleep_autotest, test_delay_us_sleep); 93 94 /* 95 * rte_delay_us_callback test 96 * 97 * - check if callback is correctly registered/unregistered 98 * 99 */ 100 101 static unsigned int pattern; 102 static void my_rte_delay_us(unsigned int us) 103 { 104 pattern += us; 105 } 106 107 static int 108 test_user_delay_us(void) 109 { 110 pattern = 0; 111 112 rte_delay_us(2); 113 if (pattern != 0) 114 return -1; 115 116 /* register custom delay function */ 117 rte_delay_us_callback_register(my_rte_delay_us); 118 119 rte_delay_us(2); 120 if (pattern != 2) 121 return -1; 122 123 rte_delay_us(3); 124 if (pattern != 5) 125 return -1; 126 127 /* restore original delay function */ 128 rte_delay_us_callback_register(rte_delay_us_block); 129 130 rte_delay_us(3); 131 if (pattern != 5) 132 return -1; 133 134 return 0; 135 } 136 137 REGISTER_TEST_COMMAND(user_delay_us, test_user_delay_us); 138