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