first public release
[dpdk.git] / app / test / test_alarm.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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  *  version: DPDK.L.1.2.3-3
34  */
35
36 #include <stdio.h>
37 #include <stdint.h>
38
39 #include <cmdline_parse.h>
40
41 #include <rte_common.h>
42 #include <rte_cycles.h>
43 #include <rte_interrupts.h>
44 #include <rte_common.h>
45 #include <rte_atomic.h>
46 #include <rte_alarm.h>
47
48 #include "test.h"
49
50 #define US_PER_MS 1000
51
52 #define RTE_TEST_ALARM_TIMEOUT 3000 /* ms */
53 #define RTE_TEST_CHECK_PERIOD  1000 /* ms */
54
55 static volatile int flag;
56
57 static void
58 test_alarm_callback(void *cb_arg)
59 {
60         flag = 1;
61         printf("Callback setting flag - OK. [cb_arg = %p]\n", cb_arg);
62 }
63
64 static rte_atomic32_t cb_count;
65
66 static void
67 test_multi_cb(void *arg)
68 {
69         rte_atomic32_inc(&cb_count);
70         printf("In %s - arg = %p\n", __func__, arg);
71 }
72
73 static volatile int recursive_error = 0;
74
75 static void
76 test_remove_in_callback(void *arg)
77 {
78         printf("In %s - arg = %p\n", __func__, arg);
79         if (rte_eal_alarm_cancel(test_remove_in_callback, arg) ||
80                         rte_eal_alarm_cancel(test_remove_in_callback, (void *)-1)) {
81                 printf("Error - cancelling callback from within function succeeded!\n");
82                 recursive_error = 1;
83         }
84         flag = (int)((uintptr_t)arg);
85 }
86
87 static volatile int flag_2;
88
89 static void
90 test_remove_in_callback_2(void *arg)
91 {
92         if (rte_eal_alarm_cancel(test_remove_in_callback_2, arg) || rte_eal_alarm_cancel(test_remove_in_callback_2, (void *)-1)) {
93                 printf("Error - cancelling callback of test_remove_in_callback_2\n");
94                 return;
95         }
96         flag_2 = 1;
97 }
98
99 static int
100 test_multi_alarms(void)
101 {
102         int rm_count = 0;
103         cb_count.cnt = 0;
104
105         printf("Expect 6 callbacks in order...\n");
106         /* add two alarms in order */
107         rte_eal_alarm_set(1000 * US_PER_MS, test_multi_cb, (void *)1);
108         rte_eal_alarm_set(2000 * US_PER_MS, test_multi_cb, (void *)2);
109
110         /* now add in reverse order */
111         rte_eal_alarm_set(6000 * US_PER_MS, test_multi_cb, (void *)6);
112         rte_eal_alarm_set(5000 * US_PER_MS, test_multi_cb, (void *)5);
113         rte_eal_alarm_set(4000 * US_PER_MS, test_multi_cb, (void *)4);
114         rte_eal_alarm_set(3000 * US_PER_MS, test_multi_cb, (void *)3);
115
116         /* wait for expiry */
117         rte_delay_ms(6500);
118         if (cb_count.cnt != 6) {
119                 printf("Missing callbacks\n");
120                 /* remove any callbacks that might remain */
121                 rte_eal_alarm_cancel(test_multi_cb, (void *)-1);
122                 return -1;
123         }
124
125         cb_count.cnt = 0;
126         printf("Expect only callbacks with args 1 and 3...\n");
127         /* Add 3 flags, then delete one */
128         rte_eal_alarm_set(3000 * US_PER_MS, test_multi_cb, (void *)3);
129         rte_eal_alarm_set(2000 * US_PER_MS, test_multi_cb, (void *)2);
130         rte_eal_alarm_set(1000 * US_PER_MS, test_multi_cb, (void *)1);
131         rm_count = rte_eal_alarm_cancel(test_multi_cb, (void *)2);
132
133         rte_delay_ms(3500);
134         if (cb_count.cnt != 2 || rm_count != 1) {
135                 printf("Error: invalid flags count or alarm removal failure"
136                                 " -  flags value = %d, expected = %d\n", cb_count.cnt, 2);
137                 /* remove any callbacks that might remain */
138                 rte_eal_alarm_cancel(test_multi_cb, (void *)-1);
139                 return -1;
140         }
141
142         printf("Testing adding and then removing multiple alarms\n");
143         /* finally test that no callbacks are called if we delete them all*/
144         rte_eal_alarm_set(1000 * US_PER_MS, test_multi_cb, (void *)1);
145         rte_eal_alarm_set(1000 * US_PER_MS, test_multi_cb, (void *)2);
146         rte_eal_alarm_set(1000 * US_PER_MS, test_multi_cb, (void *)3);
147         rm_count = rte_eal_alarm_cancel(test_alarm_callback, (void *)-1);
148         if (rm_count != 0) {
149                 printf("Error removing non-existant alarm succeeded\n");
150                 rte_eal_alarm_cancel(test_multi_cb, (void *) -1);
151                 return -1;
152         }
153         rm_count = rte_eal_alarm_cancel(test_multi_cb, (void *) -1);
154         if (rm_count != 3) {
155                 printf("Error removing all pending alarm callbacks\n");
156                 return -1;
157         }
158
159         /* Test that we cannot cancel an alarm from within the callback itself
160          * Also test that we can cancel head-of-line callbacks ok.*/
161         flag = 0;
162         recursive_error = 0;
163         rte_eal_alarm_set(1000 * US_PER_MS, test_remove_in_callback, (void *)1);
164         rte_eal_alarm_set(2000 * US_PER_MS, test_remove_in_callback, (void *)2);
165         rm_count = rte_eal_alarm_cancel(test_remove_in_callback, (void *)1);
166         if (rm_count != 1) {
167                 printf("Error cancelling head-of-list callback\n");
168                 return -1;
169         }
170         rte_delay_ms(1500);
171         if (flag != 0) {
172                 printf("Error, cancelling head-of-list leads to premature callback\n");
173                 return -1;
174         }
175         rte_delay_ms(1000);
176         if (flag != 2) {
177                 printf("Error - expected callback not called\n");
178                 rte_eal_alarm_cancel(test_remove_in_callback, (void *)-1);
179                 return -1;
180         }
181         if (recursive_error == 1)
182                 return -1;
183
184         /* Check if it can cancel all for the same callback */
185         printf("Testing canceling all for the same callback\n");
186         flag_2 = 0;
187         rte_eal_alarm_set(1000 * US_PER_MS, test_remove_in_callback, (void *)1);
188         rte_eal_alarm_set(2000 * US_PER_MS, test_remove_in_callback_2, (void *)2);
189         rte_eal_alarm_set(3000 * US_PER_MS, test_remove_in_callback_2, (void *)3);
190         rte_eal_alarm_set(4000 * US_PER_MS, test_remove_in_callback, (void *)4);
191         rm_count = rte_eal_alarm_cancel(test_remove_in_callback_2, (void *)-1);
192         if (rm_count != 2) {
193                 printf("Error, cannot cancel all for the same callback\n");
194                 return -1;
195         }
196         rm_count = rte_eal_alarm_cancel(test_remove_in_callback, (void *)-1);
197         if (rm_count != 2) {
198                 printf("Error, cannot cancel all for the same callback\n");
199                 return -1;
200         }
201
202         return 0;
203 }
204
205 int
206 test_alarm(void)
207 {
208         int count = 0;
209
210         /* check if the callback will be called */
211         printf("check if the callback will be called\n");
212         flag = 0;
213         if (rte_eal_alarm_set(RTE_TEST_ALARM_TIMEOUT * US_PER_MS,
214                         test_alarm_callback, NULL) < 0) {
215                 printf("fail to set alarm callback\n");
216                 return -1;
217         }
218         while (flag == 0 && count ++ < 6)
219                 rte_delay_ms(RTE_TEST_CHECK_PERIOD);
220
221         if (flag == 0){
222                 printf("Callback not called\n");
223                 return -1;
224         }
225
226         /* check if it will fail to set alarm with wrong us value */
227         printf("check if it will fail to set alarm with wrong ms values\n");
228         if (rte_eal_alarm_set(0, test_alarm_callback,
229                                                 NULL) >= 0) {
230                 printf("should not be successful with 0 us value\n");
231                 return -1;
232         }
233         if (rte_eal_alarm_set(UINT64_MAX - 1, test_alarm_callback,
234                                                 NULL) >= 0) {
235                 printf("should not be successful with (UINT64_MAX-1) us value\n");
236                 return -1;
237         }
238
239         /* check if it will fail to set alarm with null callback parameter */
240         printf("check if it will fail to set alarm with null callback parameter\n");
241         if (rte_eal_alarm_set(RTE_TEST_ALARM_TIMEOUT, NULL, NULL) >= 0) {
242                 printf("should not be successful to set alarm with null callback parameter\n");
243                 return -1;
244         }
245
246         /* check if it will fail to remove alarm with null callback parameter */
247         printf("check if it will fail to remove alarm with null callback parameter\n");
248         if (rte_eal_alarm_cancel(NULL, NULL) == 0) {
249                 printf("should not be successful to remove alarm with null callback parameter");
250                 return -1;
251         }
252
253         if (test_multi_alarms() != 0)
254                 return -1;
255
256         return 0;
257 }
258