New upstream version 18.02
[deb_dpdk.git] / test / test / test_cycles.c
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 test_cycles(void)
28 {
29         unsigned i;
30         uint64_t start_cycles, cycles, prev_cycles;
31         uint64_t hz = rte_get_timer_hz();
32         uint64_t max_inc = (hz / 100); /* 10 ms max between 2 reads */
33
34         /* check that the timer is always incrementing */
35         start_cycles = rte_get_timer_cycles();
36         prev_cycles = start_cycles;
37         for (i=0; i<N; i++) {
38                 cycles = rte_get_timer_cycles();
39                 if ((uint64_t)(cycles - prev_cycles) > max_inc) {
40                         printf("increment too high or going backwards\n");
41                         return -1;
42                 }
43                 prev_cycles = cycles;
44         }
45
46         /* check that waiting 1 second is precise */
47         prev_cycles = rte_get_timer_cycles();
48         rte_delay_us(1000000);
49         cycles = rte_get_timer_cycles();
50
51         if ((uint64_t)(cycles - prev_cycles) > (hz + max_inc)) {
52                 printf("delay_us is not accurate: too long\n");
53                 return -1;
54         }
55         if ((uint64_t)(cycles - prev_cycles) < (hz - max_inc)) {
56                 printf("delay_us is not accurate: too short\n");
57                 return -1;
58         }
59
60         return 0;
61 }
62
63 REGISTER_TEST_COMMAND(cycles_autotest, test_cycles);
64
65 /*
66  * rte_delay_us_callback test
67  *
68  * - check if callback is correctly registered/unregistered
69  *
70  */
71
72 static unsigned int pattern;
73 static void my_rte_delay_us(unsigned int us)
74 {
75         pattern += us;
76 }
77
78 static int
79 test_user_delay_us(void)
80 {
81         pattern = 0;
82
83         rte_delay_us(2);
84         if (pattern != 0)
85                 return -1;
86
87         /* register custom delay function */
88         rte_delay_us_callback_register(my_rte_delay_us);
89
90         rte_delay_us(2);
91         if (pattern != 2)
92                 return -1;
93
94         rte_delay_us(3);
95         if (pattern != 5)
96                 return -1;
97
98         /* restore original delay function */
99         rte_delay_us_callback_register(rte_delay_us_block);
100
101         rte_delay_us(3);
102         if (pattern != 5)
103                 return -1;
104
105         return 0;
106 }
107
108 REGISTER_TEST_COMMAND(user_delay_us, test_user_delay_us);