]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/test/event/reactor/reactor.c
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / spdk / test / event / reactor / reactor.c
1 /*-
2 * BSD LICENSE
3 *
4 * Copyright (c) Intel Corporation.
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 "spdk/stdinc.h"
35
36 #include "spdk/event.h"
37 #include "spdk/thread.h"
38
39 static int g_time_in_sec;
40 static struct spdk_poller *test_end_poller;
41 static struct spdk_poller *poller_100ms;
42 static struct spdk_poller *poller_250ms;
43 static struct spdk_poller *poller_500ms;
44 static struct spdk_poller *poller_oneshot;
45 static struct spdk_poller *poller_unregister;
46
47 static int
48 test_end(void *arg)
49 {
50 printf("test_end\n");
51
52 spdk_poller_unregister(&test_end_poller);
53 spdk_poller_unregister(&poller_100ms);
54 spdk_poller_unregister(&poller_250ms);
55 spdk_poller_unregister(&poller_500ms);
56
57 spdk_app_stop(0);
58 return -1;
59 }
60
61 static int
62 tick(void *arg)
63 {
64 uintptr_t period = (uintptr_t)arg;
65
66 printf("tick %" PRIu64 "\n", (uint64_t)period);
67
68 return -1;
69 }
70
71 static int
72 oneshot(void *arg)
73 {
74 printf("oneshot\n");
75 spdk_poller_unregister(&poller_oneshot);
76
77 return -1;
78 }
79
80 static int
81 nop(void *arg)
82 {
83 return -1;
84 }
85
86 static void
87 test_start(void *arg1, void *arg2)
88 {
89 printf("test_start\n");
90
91 /* Register a poller that will stop the test after the time has elapsed. */
92 test_end_poller = spdk_poller_register(test_end, NULL, g_time_in_sec * 1000000ULL);
93
94 poller_100ms = spdk_poller_register(tick, (void *)100, 100000);
95 poller_250ms = spdk_poller_register(tick, (void *)250, 250000);
96 poller_500ms = spdk_poller_register(tick, (void *)500, 500000);
97 poller_oneshot = spdk_poller_register(oneshot, NULL, 0);
98
99 poller_unregister = spdk_poller_register(nop, NULL, 0);
100 spdk_poller_unregister(&poller_unregister);
101 }
102
103 static void
104 usage(const char *program_name)
105 {
106 printf("%s options\n", program_name);
107 printf("\t[-t time in seconds]\n");
108 }
109
110 int
111 main(int argc, char **argv)
112 {
113 struct spdk_app_opts opts;
114 int op;
115 int rc = 0;
116
117 spdk_app_opts_init(&opts);
118 opts.name = "reactor";
119 opts.max_delay_us = 1000;
120
121 g_time_in_sec = 0;
122
123 while ((op = getopt(argc, argv, "t:")) != -1) {
124 switch (op) {
125 case 't':
126 g_time_in_sec = atoi(optarg);
127 break;
128 default:
129 usage(argv[0]);
130 exit(1);
131 }
132 }
133
134 if (!g_time_in_sec) {
135 usage(argv[0]);
136 exit(1);
137 }
138
139 rc = spdk_app_start(&opts, test_start, NULL, NULL);
140
141 spdk_app_fini();
142
143 return rc;
144 }