2d16d6ffe773ae7cf50c1db772ae58fd76776ba7
[deb_dpdk.git] / test / test / test_alarm.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <stdint.h>
36
37 #include <rte_common.h>
38 #include <rte_cycles.h>
39 #include <rte_interrupts.h>
40 #include <rte_atomic.h>
41 #include <rte_alarm.h>
42
43 #include "test.h"
44
45 #define US_PER_MS 1000
46
47 #define RTE_TEST_ALARM_TIMEOUT 10 /* ms */
48 #define RTE_TEST_CHECK_PERIOD   3 /* ms */
49 #define RTE_TEST_MAX_REPEAT    20
50
51 static volatile int flag;
52
53 static void
54 test_alarm_callback(void *cb_arg)
55 {
56         flag = 1;
57         printf("Callback setting flag - OK. [cb_arg = %p]\n", cb_arg);
58 }
59
60 static rte_atomic32_t cb_count;
61
62 static void
63 test_multi_cb(void *arg)
64 {
65         rte_atomic32_inc(&cb_count);
66         printf("In %s - arg = %p\n", __func__, arg);
67 }
68
69 static volatile int recursive_error = 0;
70
71 static void
72 test_remove_in_callback(void *arg)
73 {
74         printf("In %s - arg = %p\n", __func__, arg);
75         if (rte_eal_alarm_cancel(test_remove_in_callback, arg) ||
76                         rte_eal_alarm_cancel(test_remove_in_callback, (void *)-1)) {
77                 printf("Error - cancelling callback from within function succeeded!\n");
78                 recursive_error = 1;
79         }
80         flag = (int)((uintptr_t)arg);
81 }
82
83 static volatile int flag_2;
84
85 static void
86 test_remove_in_callback_2(void *arg)
87 {
88         if (rte_eal_alarm_cancel(test_remove_in_callback_2, arg) || rte_eal_alarm_cancel(test_remove_in_callback_2, (void *)-1)) {
89                 printf("Error - cancelling callback of test_remove_in_callback_2\n");
90                 return;
91         }
92         flag_2 = 1;
93 }
94
95 static int
96 test_multi_alarms(void)
97 {
98         int rm_count = 0;
99         int count = 0;
100         cb_count.cnt = 0;
101
102         printf("Expect 6 callbacks in order...\n");
103         /* add two alarms in order */
104         rte_eal_alarm_set(10 * US_PER_MS, test_multi_cb, (void *)1);
105         rte_eal_alarm_set(20 * US_PER_MS, test_multi_cb, (void *)2);
106
107         /* now add in reverse order */
108         rte_eal_alarm_set(60 * US_PER_MS, test_multi_cb, (void *)6);
109         rte_eal_alarm_set(50 * US_PER_MS, test_multi_cb, (void *)5);
110         rte_eal_alarm_set(40 * US_PER_MS, test_multi_cb, (void *)4);
111         rte_eal_alarm_set(30 * US_PER_MS, test_multi_cb, (void *)3);
112
113         /* wait for expiry */
114         rte_delay_ms(65);
115         if (cb_count.cnt != 6) {
116                 printf("Missing callbacks\n");
117                 /* remove any callbacks that might remain */
118                 rte_eal_alarm_cancel(test_multi_cb, (void *)-1);
119                 return -1;
120         }
121
122         cb_count.cnt = 0;
123         printf("Expect only callbacks with args 1 and 3...\n");
124         /* Add 3 flags, then delete one */
125         rte_eal_alarm_set(30 * US_PER_MS, test_multi_cb, (void *)3);
126         rte_eal_alarm_set(20 * US_PER_MS, test_multi_cb, (void *)2);
127         rte_eal_alarm_set(10 * US_PER_MS, test_multi_cb, (void *)1);
128         rm_count = rte_eal_alarm_cancel(test_multi_cb, (void *)2);
129
130         rte_delay_ms(35);
131         if (cb_count.cnt != 2 || rm_count != 1) {
132                 printf("Error: invalid flags count or alarm removal failure"
133                                 " -  flags value = %d, expected = %d\n",
134                                 (int)cb_count.cnt, 2);
135                 /* remove any callbacks that might remain */
136                 rte_eal_alarm_cancel(test_multi_cb, (void *)-1);
137                 return -1;
138         }
139
140         printf("Testing adding and then removing multiple alarms\n");
141         /* finally test that no callbacks are called if we delete them all*/
142         rte_eal_alarm_set(10 * US_PER_MS, test_multi_cb, (void *)1);
143         rte_eal_alarm_set(10 * US_PER_MS, test_multi_cb, (void *)2);
144         rte_eal_alarm_set(10 * US_PER_MS, test_multi_cb, (void *)3);
145         rm_count = rte_eal_alarm_cancel(test_alarm_callback, (void *)-1);
146         if (rm_count != 0) {
147                 printf("Error removing non-existant alarm succeeded\n");
148                 rte_eal_alarm_cancel(test_multi_cb, (void *) -1);
149                 return -1;
150         }
151         rm_count = rte_eal_alarm_cancel(test_multi_cb, (void *) -1);
152         if (rm_count != 3) {
153                 printf("Error removing all pending alarm callbacks\n");
154                 return -1;
155         }
156
157         /* Test that we cannot cancel an alarm from within the callback itself
158          * Also test that we can cancel head-of-line callbacks ok.*/
159         flag = 0;
160         recursive_error = 0;
161         rte_eal_alarm_set(10 * US_PER_MS, test_remove_in_callback, (void *)1);
162         rte_eal_alarm_set(20 * US_PER_MS, test_remove_in_callback, (void *)2);
163         rm_count = rte_eal_alarm_cancel(test_remove_in_callback, (void *)1);
164         if (rm_count != 1) {
165                 printf("Error cancelling head-of-list callback\n");
166                 return -1;
167         }
168         rte_delay_ms(15);
169         if (flag != 0) {
170                 printf("Error, cancelling head-of-list leads to premature callback\n");
171                 return -1;
172         }
173
174         while (flag != 2 && count++ < RTE_TEST_MAX_REPEAT)
175                 rte_delay_ms(10);
176
177         if (flag != 2) {
178                 printf("Error - expected callback not called\n");
179                 rte_eal_alarm_cancel(test_remove_in_callback, (void *)-1);
180                 return -1;
181         }
182         if (recursive_error == 1)
183                 return -1;
184
185         /* Check if it can cancel all for the same callback */
186         printf("Testing canceling all for the same callback\n");
187         flag_2 = 0;
188         rte_eal_alarm_set(10 * US_PER_MS, test_remove_in_callback, (void *)1);
189         rte_eal_alarm_set(20 * US_PER_MS, test_remove_in_callback_2, (void *)2);
190         rte_eal_alarm_set(30 * US_PER_MS, test_remove_in_callback_2, (void *)3);
191         rte_eal_alarm_set(40 * US_PER_MS, test_remove_in_callback, (void *)4);
192         rm_count = rte_eal_alarm_cancel(test_remove_in_callback_2, (void *)-1);
193         if (rm_count != 2) {
194                 printf("Error, cannot cancel all for the same callback\n");
195                 return -1;
196         }
197         rm_count = rte_eal_alarm_cancel(test_remove_in_callback, (void *)-1);
198         if (rm_count != 2) {
199                 printf("Error, cannot cancel all for the same callback\n");
200                 return -1;
201         }
202
203         return 0;
204 }
205
206 static int
207 test_alarm(void)
208 {
209         int count = 0;
210
211         /* check if the callback will be called */
212         printf("check if the callback will be called\n");
213         flag = 0;
214         if (rte_eal_alarm_set(RTE_TEST_ALARM_TIMEOUT * US_PER_MS,
215                         test_alarm_callback, NULL) < 0) {
216                 printf("fail to set alarm callback\n");
217                 return -1;
218         }
219         while (flag == 0 && count++ < RTE_TEST_MAX_REPEAT)
220                 rte_delay_ms(RTE_TEST_CHECK_PERIOD);
221
222         if (flag == 0){
223                 printf("Callback not called\n");
224                 return -1;
225         }
226
227         /* check if it will fail to set alarm with wrong us value */
228         printf("check if it will fail to set alarm with wrong ms values\n");
229         if (rte_eal_alarm_set(0, test_alarm_callback,
230                                                 NULL) >= 0) {
231                 printf("should not be successful with 0 us value\n");
232                 return -1;
233         }
234         if (rte_eal_alarm_set(UINT64_MAX - 1, test_alarm_callback,
235                                                 NULL) >= 0) {
236                 printf("should not be successful with (UINT64_MAX-1) us value\n");
237                 return -1;
238         }
239
240         /* check if it will fail to set alarm with null callback parameter */
241         printf("check if it will fail to set alarm with null callback parameter\n");
242         if (rte_eal_alarm_set(RTE_TEST_ALARM_TIMEOUT, NULL, NULL) >= 0) {
243                 printf("should not be successful to set alarm with null callback parameter\n");
244                 return -1;
245         }
246
247         /* check if it will fail to remove alarm with null callback parameter */
248         printf("check if it will fail to remove alarm with null callback parameter\n");
249         if (rte_eal_alarm_cancel(NULL, NULL) == 0) {
250                 printf("should not be successful to remove alarm with null callback parameter");
251                 return -1;
252         }
253
254         if (test_multi_alarms() != 0)
255                 return -1;
256
257         return 0;
258 }
259
260 REGISTER_TEST_COMMAND(alarm_autotest, test_alarm);