]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/test/event/event_perf/event_perf.c
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / spdk / test / event / event_perf / event_perf.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/env.h"
37 #include "spdk/event.h"
38 #include "spdk_internal/event.h"
39 #include "spdk/log.h"
40
41 static uint64_t g_tsc_rate;
42 static uint64_t g_tsc_us_rate;
43 static uint64_t g_tsc_end;
44
45 static int g_time_in_sec;
46
47 static uint64_t *call_count;
48
49 static bool g_app_stopped = false;
50
51 static void
52 submit_new_event(void *arg1, void *arg2)
53 {
54 struct spdk_event *event;
55 static __thread uint32_t next_lcore = UINT32_MAX;
56
57 if (spdk_get_ticks() > g_tsc_end) {
58 if (__sync_bool_compare_and_swap(&g_app_stopped, false, true)) {
59 spdk_app_stop(0);
60 }
61 return;
62 }
63
64 if (next_lcore == UINT32_MAX) {
65 next_lcore = spdk_env_get_next_core(spdk_env_get_current_core());
66 if (next_lcore == UINT32_MAX) {
67 next_lcore = spdk_env_get_first_core();
68 }
69 }
70
71 call_count[next_lcore]++;
72 event = spdk_event_allocate(next_lcore, submit_new_event, NULL, NULL);
73 spdk_event_call(event);
74 }
75
76 static void
77 event_work_fn(void *arg1, void *arg2)
78 {
79
80 submit_new_event(NULL, NULL);
81 submit_new_event(NULL, NULL);
82 submit_new_event(NULL, NULL);
83 submit_new_event(NULL, NULL);
84 }
85
86 static void
87 event_perf_start(void *arg1, void *arg2)
88 {
89 uint32_t i;
90
91 call_count = calloc(spdk_env_get_last_core() + 1, sizeof(*call_count));
92 if (call_count == NULL) {
93 fprintf(stderr, "call_count allocation failed\n");
94 spdk_app_stop(1);
95 return;
96 }
97
98 g_tsc_rate = spdk_get_ticks_hz();
99 g_tsc_us_rate = g_tsc_rate / (1000 * 1000);
100 g_tsc_end = spdk_get_ticks() + g_time_in_sec * g_tsc_rate;
101
102 printf("Running I/O for %d seconds...", g_time_in_sec);
103 fflush(stdout);
104
105 SPDK_ENV_FOREACH_CORE(i) {
106 spdk_event_call(spdk_event_allocate(i, event_work_fn,
107 NULL, NULL));
108 }
109
110 }
111
112 static void
113 usage(char *program_name)
114 {
115 printf("%s options\n", program_name);
116 printf("\t[-m core mask for distributing I/O submission/completion work\n");
117 printf("\t\t(default: 0x1 - use core 0 only)]\n");
118 printf("\t[-t time in seconds]\n");
119 }
120
121 static void
122 performance_dump(int io_time)
123 {
124 uint32_t i;
125
126 if (call_count == NULL) {
127 return;
128 }
129
130 printf("\n");
131 SPDK_ENV_FOREACH_CORE(i) {
132 printf("lcore %2d: %8ju\n", i, call_count[i] / g_time_in_sec);
133 }
134
135 fflush(stdout);
136 free(call_count);
137 }
138
139 int
140 main(int argc, char **argv)
141 {
142 struct spdk_app_opts opts = {};
143 int op;
144 int rc = 0;
145
146 opts.name = "event_perf";
147 opts.mem_size = 256;
148
149 g_time_in_sec = 0;
150
151 while ((op = getopt(argc, argv, "m:t:")) != -1) {
152 switch (op) {
153 case 'm':
154 opts.reactor_mask = optarg;
155 break;
156 case 't':
157 g_time_in_sec = atoi(optarg);
158 break;
159 default:
160 usage(argv[0]);
161 exit(1);
162 }
163 }
164
165 if (!g_time_in_sec) {
166 usage(argv[0]);
167 exit(1);
168 }
169
170 printf("Running I/O for %d seconds...", g_time_in_sec);
171 fflush(stdout);
172
173 rc = spdk_app_start(&opts, event_perf_start, NULL, NULL);
174
175 spdk_app_fini();
176 performance_dump(g_time_in_sec);
177
178 printf("done.\n");
179 return rc;
180 }