xref: /spdk/test/event/reactor_perf/reactor_perf.c (revision c6c1234de9e0015e670dd0b51bf6ce39ee0e07bd)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2017 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/string.h"
11 #include "spdk/thread.h"
12 
13 static int g_time_in_sec;
14 static int g_queue_depth;
15 static struct spdk_poller *g_test_end_poller;
16 static uint64_t g_call_count = 0;
17 
18 static int
19 __test_end(void *arg)
20 {
21 	printf("test_end\n");
22 	spdk_poller_unregister(&g_test_end_poller);
23 	spdk_app_stop(0);
24 	return -1;
25 }
26 
27 static void
28 __submit_next(void *arg1, void *arg2)
29 {
30 	struct spdk_event *event;
31 
32 	g_call_count++;
33 
34 	event = spdk_event_allocate(spdk_env_get_current_core(),
35 				    __submit_next, NULL, NULL);
36 	spdk_event_call(event);
37 }
38 
39 static void
40 test_start(void *arg1)
41 {
42 	int i;
43 
44 	printf("test_start\n");
45 
46 	/* Register a poller that will stop the test after the time has elapsed. */
47 	g_test_end_poller = SPDK_POLLER_REGISTER(__test_end, NULL,
48 			    g_time_in_sec * 1000000ULL);
49 
50 	for (i = 0; i < g_queue_depth; i++) {
51 		__submit_next(NULL, NULL);
52 	}
53 }
54 
55 static void
56 test_cleanup(void)
57 {
58 	printf("test_abort\n");
59 
60 	spdk_poller_unregister(&g_test_end_poller);
61 	spdk_app_stop(0);
62 }
63 
64 static void
65 usage(const char *program_name)
66 {
67 	printf("%s options\n", program_name);
68 	printf("\t[-q Queue depth (default: 1)]\n");
69 	printf("\t[-t time in seconds]\n");
70 }
71 
72 int
73 main(int argc, char **argv)
74 {
75 	struct spdk_app_opts opts;
76 	int op;
77 	int rc;
78 	long int val;
79 
80 	spdk_app_opts_init(&opts, sizeof(opts));
81 	opts.name = "reactor_perf";
82 	opts.rpc_addr = NULL;
83 
84 	g_time_in_sec = 0;
85 	g_queue_depth = 1;
86 
87 	while ((op = getopt(argc, argv, "q:t:")) != -1) {
88 		if (op == '?') {
89 			usage(argv[0]);
90 			exit(1);
91 		}
92 		val = spdk_strtol(optarg, 10);
93 		if (val < 0) {
94 			fprintf(stderr, "Converting a string to integer failed\n");
95 			exit(1);
96 		}
97 		switch (op) {
98 		case 'q':
99 			g_queue_depth = val;
100 			break;
101 		case 't':
102 			g_time_in_sec = val;
103 			break;
104 		default:
105 			usage(argv[0]);
106 			exit(1);
107 		}
108 	}
109 
110 	if (!g_time_in_sec) {
111 		usage(argv[0]);
112 		exit(1);
113 	}
114 
115 	opts.shutdown_cb = test_cleanup;
116 
117 	rc = spdk_app_start(&opts, test_start, NULL);
118 
119 	spdk_app_fini();
120 
121 	printf("Performance: %8ju events per second\n", g_call_count / g_time_in_sec);
122 
123 	return rc;
124 }
125