xref: /spdk/test/event/event_perf/event_perf.c (revision 32999ab917f67af61872f868585fd3d78ad6fb8a)
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 #include "spdk/string.h"
41 
42 static uint64_t g_tsc_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)
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_end = spdk_get_ticks() + g_time_in_sec * g_tsc_rate;
100 
101 	printf("Running I/O for %d seconds...", g_time_in_sec);
102 	fflush(stdout);
103 
104 	SPDK_ENV_FOREACH_CORE(i) {
105 		spdk_event_call(spdk_event_allocate(i, event_work_fn,
106 						    NULL, NULL));
107 	}
108 
109 }
110 
111 static void
112 usage(char *program_name)
113 {
114 	printf("%s options\n", program_name);
115 	printf("\t[-m core mask for distributing I/O submission/completion work\n");
116 	printf("\t\t(default: 0x1 - use core 0 only)]\n");
117 	printf("\t[-t time in seconds]\n");
118 }
119 
120 static void
121 performance_dump(int io_time)
122 {
123 	uint32_t i;
124 
125 	if (call_count == NULL) {
126 		return;
127 	}
128 
129 	printf("\n");
130 	SPDK_ENV_FOREACH_CORE(i) {
131 		printf("lcore %2d: %8ju\n", i, call_count[i] / g_time_in_sec);
132 	}
133 
134 	fflush(stdout);
135 	free(call_count);
136 }
137 
138 int
139 main(int argc, char **argv)
140 {
141 	struct spdk_app_opts opts = {};
142 	int op;
143 	int rc = 0;
144 
145 	spdk_app_opts_init(&opts, sizeof(opts));
146 	opts.name = "event_perf";
147 
148 	g_time_in_sec = 0;
149 
150 	while ((op = getopt(argc, argv, "m:t:")) != -1) {
151 		switch (op) {
152 		case 'm':
153 			opts.reactor_mask = optarg;
154 			break;
155 		case 't':
156 			g_time_in_sec = spdk_strtol(optarg, 10);
157 			if (g_time_in_sec < 0) {
158 				fprintf(stderr, "Invalid run time\n");
159 				return g_time_in_sec;
160 			}
161 			break;
162 		default:
163 			usage(argv[0]);
164 			exit(1);
165 		}
166 	}
167 
168 	if (!g_time_in_sec) {
169 		usage(argv[0]);
170 		exit(1);
171 	}
172 
173 	printf("Running I/O for %d seconds...", g_time_in_sec);
174 	fflush(stdout);
175 
176 	rc = spdk_app_start(&opts, event_perf_start, NULL);
177 
178 	spdk_app_fini();
179 	performance_dump(g_time_in_sec);
180 
181 	printf("done.\n");
182 	return rc;
183 }
184