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 rte_delay_us_callback_register(rte_delay_us_sleep); 83 return check_wait_one_second(); 84 } 85 86 REGISTER_TEST_COMMAND(delay_us_sleep_autotest, test_delay_us_sleep); 87 88 /* 89 * rte_delay_us_callback test 90 * 91 * - check if callback is correctly registered/unregistered 92 * 93 */ 94 95 static unsigned int pattern; 96 static void my_rte_delay_us(unsigned int us) 97 { 98 pattern += us; 99 } 100 101 static int 102 test_user_delay_us(void) 103 { 104 pattern = 0; 105 106 rte_delay_us(2); 107 if (pattern != 0) 108 return -1; 109 110 /* register custom delay function */ 111 rte_delay_us_callback_register(my_rte_delay_us); 112 113 rte_delay_us(2); 114 if (pattern != 2) 115 return -1; 116 117 rte_delay_us(3); 118 if (pattern != 5) 119 return -1; 120 121 /* restore original delay function */ 122 rte_delay_us_callback_register(rte_delay_us_block); 123 124 rte_delay_us(3); 125 if (pattern != 5) 126 return -1; 127 128 return 0; 129 } 130 131 REGISTER_TEST_COMMAND(user_delay_us, test_user_delay_us); 132