xref: /spdk/test/event/event_perf/event_perf.c (revision 5db859da082485cbb7a8429d13818da4131da63a)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2016 Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 #include "spdk/stdinc.h"
7 
8 #include "spdk/env.h"
9 #include "spdk/event.h"
10 #include "spdk/log.h"
11 #include "spdk/string.h"
12 
13 static uint64_t g_tsc_rate;
14 static uint64_t g_tsc_end;
15 
16 static int g_time_in_sec;
17 
18 static uint64_t *call_count;
19 
20 static bool g_app_stopped = false;
21 
22 static void
submit_new_event(void * arg1,void * arg2)23 submit_new_event(void *arg1, void *arg2)
24 {
25 	struct spdk_event *event;
26 	static __thread uint32_t next_lcore = UINT32_MAX;
27 
28 	if (spdk_get_ticks() > g_tsc_end) {
29 		if (__sync_bool_compare_and_swap(&g_app_stopped, false, true)) {
30 			spdk_app_stop(0);
31 		}
32 		return;
33 	}
34 
35 	if (next_lcore == UINT32_MAX) {
36 		next_lcore = spdk_env_get_next_core(spdk_env_get_current_core());
37 		if (next_lcore == UINT32_MAX) {
38 			next_lcore = spdk_env_get_first_core();
39 		}
40 	}
41 
42 	call_count[next_lcore]++;
43 	event = spdk_event_allocate(next_lcore, submit_new_event, NULL, NULL);
44 	spdk_event_call(event);
45 }
46 
47 static void
event_work_fn(void * arg1,void * arg2)48 event_work_fn(void *arg1, void *arg2)
49 {
50 
51 	submit_new_event(NULL, NULL);
52 	submit_new_event(NULL, NULL);
53 	submit_new_event(NULL, NULL);
54 	submit_new_event(NULL, NULL);
55 }
56 
57 static void
event_perf_start(void * arg1)58 event_perf_start(void *arg1)
59 {
60 	uint32_t i;
61 
62 	call_count = calloc(spdk_env_get_last_core() + 1, sizeof(*call_count));
63 	if (call_count == NULL) {
64 		fprintf(stderr, "call_count allocation failed\n");
65 		spdk_app_stop(1);
66 		return;
67 	}
68 
69 	g_tsc_rate = spdk_get_ticks_hz();
70 	g_tsc_end = spdk_get_ticks() + g_time_in_sec * g_tsc_rate;
71 
72 	printf("Running I/O for %d seconds...", g_time_in_sec);
73 	fflush(stdout);
74 
75 	SPDK_ENV_FOREACH_CORE(i) {
76 		spdk_event_call(spdk_event_allocate(i, event_work_fn,
77 						    NULL, NULL));
78 	}
79 
80 }
81 
82 static void
usage(char * program_name)83 usage(char *program_name)
84 {
85 	printf("%s options\n", program_name);
86 	printf("\t[-m core mask for distributing I/O submission/completion work\n");
87 	printf("\t\t(default: 0x1 - use core 0 only)]\n");
88 	printf("\t[-t time in seconds]\n");
89 }
90 
91 static void
performance_dump(int io_time)92 performance_dump(int io_time)
93 {
94 	uint32_t i;
95 
96 	if (call_count == NULL) {
97 		return;
98 	}
99 
100 	printf("\n");
101 	SPDK_ENV_FOREACH_CORE(i) {
102 		printf("lcore %2d: %8ju\n", i, call_count[i] / g_time_in_sec);
103 	}
104 
105 	fflush(stdout);
106 	free(call_count);
107 }
108 
109 int
main(int argc,char ** argv)110 main(int argc, char **argv)
111 {
112 	struct spdk_app_opts opts = {};
113 	int op;
114 	int rc = 0;
115 
116 	spdk_app_opts_init(&opts, sizeof(opts));
117 	opts.name = "event_perf";
118 	opts.rpc_addr = NULL;
119 
120 	g_time_in_sec = 0;
121 
122 	while ((op = getopt(argc, argv, "m:t:")) != -1) {
123 		switch (op) {
124 		case 'm':
125 			opts.reactor_mask = optarg;
126 			break;
127 		case 't':
128 			g_time_in_sec = spdk_strtol(optarg, 10);
129 			if (g_time_in_sec < 0) {
130 				fprintf(stderr, "Invalid run time\n");
131 				return g_time_in_sec;
132 			}
133 			break;
134 		default:
135 			usage(argv[0]);
136 			exit(1);
137 		}
138 	}
139 
140 	if (!g_time_in_sec) {
141 		usage(argv[0]);
142 		exit(1);
143 	}
144 
145 	printf("Running I/O for %d seconds...", g_time_in_sec);
146 	fflush(stdout);
147 
148 	rc = spdk_app_start(&opts, event_perf_start, NULL);
149 
150 	spdk_app_fini();
151 	performance_dump(g_time_in_sec);
152 
153 	printf("done.\n");
154 	return rc;
155 }
156