xref: /spdk/test/event/event_perf/event_perf.c (revision a5f87f39127c7e0da8d9c4fcd042a27e350be84e)
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
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
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
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
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
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
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 
119 	g_time_in_sec = 0;
120 
121 	while ((op = getopt(argc, argv, "m:t:")) != -1) {
122 		switch (op) {
123 		case 'm':
124 			opts.reactor_mask = optarg;
125 			break;
126 		case 't':
127 			g_time_in_sec = spdk_strtol(optarg, 10);
128 			if (g_time_in_sec < 0) {
129 				fprintf(stderr, "Invalid run time\n");
130 				return g_time_in_sec;
131 			}
132 			break;
133 		default:
134 			usage(argv[0]);
135 			exit(1);
136 		}
137 	}
138 
139 	if (!g_time_in_sec) {
140 		usage(argv[0]);
141 		exit(1);
142 	}
143 
144 	printf("Running I/O for %d seconds...", g_time_in_sec);
145 	fflush(stdout);
146 
147 	rc = spdk_app_start(&opts, event_perf_start, NULL);
148 
149 	spdk_app_fini();
150 	performance_dump(g_time_in_sec);
151 
152 	printf("done.\n");
153 	return rc;
154 }
155