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