xref: /spdk/examples/accel/perf/accel_perf.c (revision a6dbe3721eb3b5990707fc3e378c95e505dd8ab5)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2020 Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 #include "spdk/stdinc.h"
7 #include "spdk/thread.h"
8 #include "spdk/env.h"
9 #include "spdk/event.h"
10 #include "spdk/log.h"
11 #include "spdk/string.h"
12 #include "spdk/accel.h"
13 #include "spdk/crc32.h"
14 #include "spdk/util.h"
15 
16 #define DATA_PATTERN 0x5a
17 #define ALIGN_4K 0x1000
18 
19 static uint64_t	g_tsc_rate;
20 static uint64_t g_tsc_end;
21 static int g_rc;
22 static int g_xfer_size_bytes = 4096;
23 static int g_queue_depth = 32;
24 /* g_allocate_depth indicates how many tasks we allocate per worker. It will
25  * be at least as much as the queue depth.
26  */
27 static int g_allocate_depth = 0;
28 static int g_threads_per_core = 1;
29 static int g_time_in_sec = 5;
30 static uint32_t g_crc32c_seed = 0;
31 static uint32_t g_chained_count = 1;
32 static int g_fail_percent_goal = 0;
33 static uint8_t g_fill_pattern = 255;
34 static bool g_verify = false;
35 static const char *g_workload_type = NULL;
36 static enum accel_opcode g_workload_selection;
37 static struct worker_thread *g_workers = NULL;
38 static int g_num_workers = 0;
39 static pthread_mutex_t g_workers_lock = PTHREAD_MUTEX_INITIALIZER;
40 static struct spdk_app_opts g_opts = {};
41 
42 struct worker_thread;
43 static void accel_done(void *ref, int status);
44 
45 struct display_info {
46 	int core;
47 	int thread;
48 };
49 
50 struct ap_task {
51 	void			*src;
52 	struct iovec		*src_iovs;
53 	uint32_t		src_iovcnt;
54 	struct iovec		*dst_iovs;
55 	uint32_t		dst_iovcnt;
56 	void			*dst;
57 	void			*dst2;
58 	uint32_t		crc_dst;
59 	struct worker_thread	*worker;
60 	int			expected_status; /* used for the compare operation */
61 	TAILQ_ENTRY(ap_task)	link;
62 };
63 
64 struct worker_thread {
65 	struct spdk_io_channel		*ch;
66 	uint64_t			xfer_completed;
67 	uint64_t			xfer_failed;
68 	uint64_t			injected_miscompares;
69 	uint64_t			current_queue_depth;
70 	TAILQ_HEAD(, ap_task)		tasks_pool;
71 	struct worker_thread		*next;
72 	unsigned			core;
73 	struct spdk_thread		*thread;
74 	bool				is_draining;
75 	struct spdk_poller		*is_draining_poller;
76 	struct spdk_poller		*stop_poller;
77 	void				*task_base;
78 	struct display_info		display;
79 	enum accel_opcode		workload;
80 };
81 
82 static void
83 dump_user_config(void)
84 {
85 	const char *module_name = NULL;
86 	int rc;
87 
88 	rc = spdk_accel_get_opc_module_name(g_workload_selection, &module_name);
89 	if (rc) {
90 		printf("error getting module name (%d)\n", rc);
91 	}
92 
93 	printf("\nSPDK Configuration:\n");
94 	printf("Core mask:      %s\n\n", g_opts.reactor_mask);
95 	printf("Accel Perf Configuration:\n");
96 	printf("Workload Type:  %s\n", g_workload_type);
97 	if (g_workload_selection == ACCEL_OPC_CRC32C || g_workload_selection == ACCEL_OPC_COPY_CRC32C) {
98 		printf("CRC-32C seed:   %u\n", g_crc32c_seed);
99 	} else if (g_workload_selection == ACCEL_OPC_FILL) {
100 		printf("Fill pattern:   0x%x\n", g_fill_pattern);
101 	} else if ((g_workload_selection == ACCEL_OPC_COMPARE) && g_fail_percent_goal > 0) {
102 		printf("Failure inject: %u percent\n", g_fail_percent_goal);
103 	}
104 	if (g_workload_selection == ACCEL_OPC_COPY_CRC32C) {
105 		printf("Vector size:    %u bytes\n", g_xfer_size_bytes);
106 		printf("Transfer size:  %u bytes\n", g_xfer_size_bytes * g_chained_count);
107 	} else {
108 		printf("Transfer size:  %u bytes\n", g_xfer_size_bytes);
109 	}
110 	printf("vector count    %u\n", g_chained_count);
111 	printf("Module:         %s\n", module_name);
112 	printf("Queue depth:    %u\n", g_queue_depth);
113 	printf("Allocate depth: %u\n", g_allocate_depth);
114 	printf("# threads/core: %u\n", g_threads_per_core);
115 	printf("Run time:       %u seconds\n", g_time_in_sec);
116 	printf("Verify:         %s\n\n", g_verify ? "Yes" : "No");
117 }
118 
119 static void
120 usage(void)
121 {
122 	printf("accel_perf options:\n");
123 	printf("\t[-h help message]\n");
124 	printf("\t[-q queue depth per core]\n");
125 	printf("\t[-C for supported workloads, use this value to configure the io vector size to test (default 1)\n");
126 	printf("\t[-T number of threads per core\n");
127 	printf("\t[-n number of channels]\n");
128 	printf("\t[-o transfer size in bytes]\n");
129 	printf("\t[-t time in seconds]\n");
130 	printf("\t[-w workload type must be one of these: copy, fill, crc32c, copy_crc32c, compare, dualcast\n");
131 	printf("\t[-s for crc32c workload, use this seed value (default 0)\n");
132 	printf("\t[-P for compare workload, percentage of operations that should miscompare (percent, default 0)\n");
133 	printf("\t[-f for fill workload, use this BYTE value (default 255)\n");
134 	printf("\t[-y verify result if this switch is on]\n");
135 	printf("\t[-a tasks to allocate per core (default: same value as -q)]\n");
136 	printf("\t\tCan be used to spread operations across a wider range of memory.\n");
137 }
138 
139 static int
140 parse_args(int argc, char *argv)
141 {
142 	int argval = 0;
143 
144 	switch (argc) {
145 	case 'a':
146 	case 'C':
147 	case 'f':
148 	case 'T':
149 	case 'o':
150 	case 'P':
151 	case 'q':
152 	case 's':
153 	case 't':
154 		argval = spdk_strtol(optarg, 10);
155 		if (argval < 0) {
156 			fprintf(stderr, "-%c option must be non-negative.\n", argc);
157 			usage();
158 			return 1;
159 		}
160 		break;
161 	default:
162 		break;
163 	};
164 
165 	switch (argc) {
166 	case 'a':
167 		g_allocate_depth = argval;
168 		break;
169 	case 'C':
170 		g_chained_count = argval;
171 		break;
172 	case 'f':
173 		g_fill_pattern = (uint8_t)argval;
174 		break;
175 	case 'T':
176 		g_threads_per_core = argval;
177 		break;
178 	case 'o':
179 		g_xfer_size_bytes = argval;
180 		break;
181 	case 'P':
182 		g_fail_percent_goal = argval;
183 		break;
184 	case 'q':
185 		g_queue_depth = argval;
186 		break;
187 	case 's':
188 		g_crc32c_seed = argval;
189 		break;
190 	case 't':
191 		g_time_in_sec = argval;
192 		break;
193 	case 'y':
194 		g_verify = true;
195 		break;
196 	case 'w':
197 		g_workload_type = optarg;
198 		if (!strcmp(g_workload_type, "copy")) {
199 			g_workload_selection = ACCEL_OPC_COPY;
200 		} else if (!strcmp(g_workload_type, "fill")) {
201 			g_workload_selection = ACCEL_OPC_FILL;
202 		} else if (!strcmp(g_workload_type, "crc32c")) {
203 			g_workload_selection = ACCEL_OPC_CRC32C;
204 		} else if (!strcmp(g_workload_type, "copy_crc32c")) {
205 			g_workload_selection = ACCEL_OPC_COPY_CRC32C;
206 		} else if (!strcmp(g_workload_type, "compare")) {
207 			g_workload_selection = ACCEL_OPC_COMPARE;
208 		} else if (!strcmp(g_workload_type, "dualcast")) {
209 			g_workload_selection = ACCEL_OPC_DUALCAST;
210 		} else {
211 			usage();
212 			return 1;
213 		}
214 		break;
215 	default:
216 		usage();
217 		return 1;
218 	}
219 
220 	return 0;
221 }
222 
223 static int dump_result(void);
224 static void
225 unregister_worker(void *arg1)
226 {
227 	struct worker_thread *worker = arg1;
228 
229 	free(worker->task_base);
230 	spdk_put_io_channel(worker->ch);
231 	pthread_mutex_lock(&g_workers_lock);
232 	assert(g_num_workers >= 1);
233 	if (--g_num_workers == 0) {
234 		pthread_mutex_unlock(&g_workers_lock);
235 		g_rc = dump_result();
236 		spdk_app_stop(0);
237 	}
238 	pthread_mutex_unlock(&g_workers_lock);
239 }
240 
241 static int
242 _get_task_data_bufs(struct ap_task *task)
243 {
244 	uint32_t align = 0;
245 	uint32_t i = 0;
246 	int dst_buff_len = g_xfer_size_bytes;
247 
248 	/* For dualcast, the DSA HW requires 4K alignment on destination addresses but
249 	 * we do this for all modules to keep it simple.
250 	 */
251 	if (g_workload_selection == ACCEL_OPC_DUALCAST) {
252 		align = ALIGN_4K;
253 	}
254 
255 	if (g_workload_selection == ACCEL_OPC_CRC32C || g_workload_selection == ACCEL_OPC_COPY_CRC32C) {
256 		assert(g_chained_count > 0);
257 		task->src_iovcnt = g_chained_count;
258 		task->src_iovs = calloc(task->src_iovcnt, sizeof(struct iovec));
259 		if (!task->src_iovs) {
260 			fprintf(stderr, "cannot allocated task->src_iovs fot task=%p\n", task);
261 			return -ENOMEM;
262 		}
263 
264 		if (g_workload_selection == ACCEL_OPC_COPY_CRC32C) {
265 			dst_buff_len = g_xfer_size_bytes * g_chained_count;
266 		}
267 
268 		for (i = 0; i < task->src_iovcnt; i++) {
269 			task->src_iovs[i].iov_base = spdk_dma_zmalloc(g_xfer_size_bytes, 0, NULL);
270 			if (task->src_iovs[i].iov_base == NULL) {
271 				return -ENOMEM;
272 			}
273 			memset(task->src_iovs[i].iov_base, DATA_PATTERN, g_xfer_size_bytes);
274 			task->src_iovs[i].iov_len = g_xfer_size_bytes;
275 		}
276 
277 	} else {
278 		task->src = spdk_dma_zmalloc(g_xfer_size_bytes, 0, NULL);
279 		if (task->src == NULL) {
280 			fprintf(stderr, "Unable to alloc src buffer\n");
281 			return -ENOMEM;
282 		}
283 
284 		/* For fill, set the entire src buffer so we can check if verify is enabled. */
285 		if (g_workload_selection == ACCEL_OPC_FILL) {
286 			memset(task->src, g_fill_pattern, g_xfer_size_bytes);
287 		} else {
288 			memset(task->src, DATA_PATTERN, g_xfer_size_bytes);
289 		}
290 	}
291 
292 	if (g_workload_selection != ACCEL_OPC_CRC32C) {
293 		task->dst = spdk_dma_zmalloc(dst_buff_len, align, NULL);
294 		if (task->dst == NULL) {
295 			fprintf(stderr, "Unable to alloc dst buffer\n");
296 			return -ENOMEM;
297 		}
298 
299 		/* For compare we want the buffers to match, otherwise not. */
300 		if (g_workload_selection == ACCEL_OPC_COMPARE) {
301 			memset(task->dst, DATA_PATTERN, dst_buff_len);
302 		} else {
303 			memset(task->dst, ~DATA_PATTERN, dst_buff_len);
304 		}
305 	}
306 
307 	/* For dualcast 2 buffers are needed for the operation.  */
308 	if (g_workload_selection == ACCEL_OPC_DUALCAST) {
309 		task->dst2 = spdk_dma_zmalloc(g_xfer_size_bytes, align, NULL);
310 		if (task->dst2 == NULL) {
311 			fprintf(stderr, "Unable to alloc dst buffer\n");
312 			return -ENOMEM;
313 		}
314 		memset(task->dst2, ~DATA_PATTERN, g_xfer_size_bytes);
315 	}
316 
317 	return 0;
318 }
319 
320 inline static struct ap_task *
321 _get_task(struct worker_thread *worker)
322 {
323 	struct ap_task *task;
324 
325 	if (!TAILQ_EMPTY(&worker->tasks_pool)) {
326 		task = TAILQ_FIRST(&worker->tasks_pool);
327 		TAILQ_REMOVE(&worker->tasks_pool, task, link);
328 	} else {
329 		fprintf(stderr, "Unable to get ap_task\n");
330 		return NULL;
331 	}
332 
333 	return task;
334 }
335 
336 /* Submit one operation using the same ap task that just completed. */
337 static void
338 _submit_single(struct worker_thread *worker, struct ap_task *task)
339 {
340 	int random_num;
341 	int rc = 0;
342 	int flags = 0;
343 
344 	assert(worker);
345 
346 	switch (worker->workload) {
347 	case ACCEL_OPC_COPY:
348 		rc = spdk_accel_submit_copy(worker->ch, task->dst, task->src,
349 					    g_xfer_size_bytes, flags, accel_done, task);
350 		break;
351 	case ACCEL_OPC_FILL:
352 		/* For fill use the first byte of the task->dst buffer */
353 		rc = spdk_accel_submit_fill(worker->ch, task->dst, *(uint8_t *)task->src,
354 					    g_xfer_size_bytes, flags, accel_done, task);
355 		break;
356 	case ACCEL_OPC_CRC32C:
357 		rc = spdk_accel_submit_crc32cv(worker->ch, &task->crc_dst,
358 					       task->src_iovs, task->src_iovcnt, g_crc32c_seed,
359 					       accel_done, task);
360 		break;
361 	case ACCEL_OPC_COPY_CRC32C:
362 		rc = spdk_accel_submit_copy_crc32cv(worker->ch, task->dst, task->src_iovs, task->src_iovcnt,
363 						    &task->crc_dst, g_crc32c_seed, flags, accel_done, task);
364 		break;
365 	case ACCEL_OPC_COMPARE:
366 		random_num = rand() % 100;
367 		if (random_num < g_fail_percent_goal) {
368 			task->expected_status = -EILSEQ;
369 			*(uint8_t *)task->dst = ~DATA_PATTERN;
370 		} else {
371 			task->expected_status = 0;
372 			*(uint8_t *)task->dst = DATA_PATTERN;
373 		}
374 		rc = spdk_accel_submit_compare(worker->ch, task->dst, task->src,
375 					       g_xfer_size_bytes, accel_done, task);
376 		break;
377 	case ACCEL_OPC_DUALCAST:
378 		rc = spdk_accel_submit_dualcast(worker->ch, task->dst, task->dst2,
379 						task->src, g_xfer_size_bytes, flags, accel_done, task);
380 		break;
381 	default:
382 		assert(false);
383 		break;
384 
385 	}
386 
387 	worker->current_queue_depth++;
388 	if (rc) {
389 		accel_done(task, rc);
390 	}
391 }
392 
393 static void
394 _free_task_buffers(struct ap_task *task)
395 {
396 	uint32_t i;
397 
398 	if (g_workload_selection == ACCEL_OPC_CRC32C || g_workload_selection == ACCEL_OPC_COPY_CRC32C) {
399 		if (task->src_iovs) {
400 			for (i = 0; i < task->src_iovcnt; i++) {
401 				if (task->src_iovs[i].iov_base) {
402 					spdk_dma_free(task->src_iovs[i].iov_base);
403 				}
404 			}
405 			free(task->src_iovs);
406 		}
407 	} else {
408 		spdk_dma_free(task->src);
409 	}
410 
411 	spdk_dma_free(task->dst);
412 	if (g_workload_selection == ACCEL_OPC_DUALCAST) {
413 		spdk_dma_free(task->dst2);
414 	}
415 }
416 
417 static int
418 _vector_memcmp(void *_dst, struct iovec *src_src_iovs, uint32_t iovcnt)
419 {
420 	uint32_t i;
421 	uint32_t ttl_len = 0;
422 	uint8_t *dst = (uint8_t *)_dst;
423 
424 	for (i = 0; i < iovcnt; i++) {
425 		if (memcmp(dst, src_src_iovs[i].iov_base, src_src_iovs[i].iov_len)) {
426 			return -1;
427 		}
428 		dst += src_src_iovs[i].iov_len;
429 		ttl_len += src_src_iovs[i].iov_len;
430 	}
431 
432 	if (ttl_len != iovcnt * g_xfer_size_bytes) {
433 		return -1;
434 	}
435 
436 	return 0;
437 }
438 
439 static int _worker_stop(void *arg);
440 
441 static void
442 accel_done(void *arg1, int status)
443 {
444 	struct ap_task *task = arg1;
445 	struct worker_thread *worker = task->worker;
446 	uint32_t sw_crc32c;
447 
448 	assert(worker);
449 	assert(worker->current_queue_depth > 0);
450 
451 	if (g_verify && status == 0) {
452 		switch (worker->workload) {
453 		case ACCEL_OPC_COPY_CRC32C:
454 			sw_crc32c = spdk_crc32c_iov_update(task->src_iovs, task->src_iovcnt, ~g_crc32c_seed);
455 			if (task->crc_dst != sw_crc32c) {
456 				SPDK_NOTICELOG("CRC-32C miscompare\n");
457 				worker->xfer_failed++;
458 			}
459 			if (_vector_memcmp(task->dst, task->src_iovs, task->src_iovcnt)) {
460 				SPDK_NOTICELOG("Data miscompare\n");
461 				worker->xfer_failed++;
462 			}
463 			break;
464 		case ACCEL_OPC_CRC32C:
465 			sw_crc32c = spdk_crc32c_iov_update(task->src_iovs, task->src_iovcnt, ~g_crc32c_seed);
466 			if (task->crc_dst != sw_crc32c) {
467 				SPDK_NOTICELOG("CRC-32C miscompare\n");
468 				worker->xfer_failed++;
469 			}
470 			break;
471 		case ACCEL_OPC_COPY:
472 			if (memcmp(task->src, task->dst, g_xfer_size_bytes)) {
473 				SPDK_NOTICELOG("Data miscompare\n");
474 				worker->xfer_failed++;
475 			}
476 			break;
477 		case ACCEL_OPC_DUALCAST:
478 			if (memcmp(task->src, task->dst, g_xfer_size_bytes)) {
479 				SPDK_NOTICELOG("Data miscompare, first destination\n");
480 				worker->xfer_failed++;
481 			}
482 			if (memcmp(task->src, task->dst2, g_xfer_size_bytes)) {
483 				SPDK_NOTICELOG("Data miscompare, second destination\n");
484 				worker->xfer_failed++;
485 			}
486 			break;
487 		case ACCEL_OPC_FILL:
488 			if (memcmp(task->dst, task->src, g_xfer_size_bytes)) {
489 				SPDK_NOTICELOG("Data miscompare\n");
490 				worker->xfer_failed++;
491 			}
492 			break;
493 		case ACCEL_OPC_COMPARE:
494 			break;
495 		default:
496 			assert(false);
497 			break;
498 		}
499 	}
500 
501 	if (task->expected_status == -EILSEQ) {
502 		assert(status != 0);
503 		worker->injected_miscompares++;
504 		status = 0;
505 	} else if (status) {
506 		/* Expected to pass but the accel module reported an error (ex: COMPARE operation). */
507 		worker->xfer_failed++;
508 	}
509 
510 	worker->xfer_completed++;
511 	worker->current_queue_depth--;
512 
513 	if (!worker->is_draining && status == 0) {
514 		TAILQ_INSERT_TAIL(&worker->tasks_pool, task, link);
515 		task = _get_task(worker);
516 		_submit_single(worker, task);
517 	} else {
518 		TAILQ_INSERT_TAIL(&worker->tasks_pool, task, link);
519 	}
520 }
521 
522 static int
523 dump_result(void)
524 {
525 	uint64_t total_completed = 0;
526 	uint64_t total_failed = 0;
527 	uint64_t total_miscompared = 0;
528 	uint64_t total_xfer_per_sec, total_bw_in_MiBps;
529 	struct worker_thread *worker = g_workers;
530 
531 	printf("\nCore,Thread   Transfers     Bandwidth     Failed     Miscompares\n");
532 	printf("------------------------------------------------------------------------\n");
533 	while (worker != NULL) {
534 
535 		uint64_t xfer_per_sec = worker->xfer_completed / g_time_in_sec;
536 		uint64_t bw_in_MiBps = (worker->xfer_completed * g_xfer_size_bytes) /
537 				       (g_time_in_sec * 1024 * 1024);
538 
539 		total_completed += worker->xfer_completed;
540 		total_failed += worker->xfer_failed;
541 		total_miscompared += worker->injected_miscompares;
542 
543 		if (xfer_per_sec) {
544 			printf("%u,%u%17" PRIu64 "/s%9" PRIu64 " MiB/s%7" PRIu64 " %11" PRIu64 "\n",
545 			       worker->display.core, worker->display.thread, xfer_per_sec,
546 			       bw_in_MiBps, worker->xfer_failed, worker->injected_miscompares);
547 		}
548 
549 		worker = worker->next;
550 	}
551 
552 	total_xfer_per_sec = total_completed / g_time_in_sec;
553 	total_bw_in_MiBps = (total_completed * g_xfer_size_bytes) /
554 			    (g_time_in_sec * 1024 * 1024);
555 
556 	printf("=========================================================================\n");
557 	printf("Total:%15" PRIu64 "/s%9" PRIu64 " MiB/s%6" PRIu64 " %11" PRIu64"\n\n",
558 	       total_xfer_per_sec, total_bw_in_MiBps, total_failed, total_miscompared);
559 
560 	return total_failed ? 1 : 0;
561 }
562 
563 static inline void
564 _free_task_buffers_in_pool(struct worker_thread *worker)
565 {
566 	struct ap_task *task;
567 
568 	assert(worker);
569 	while ((task = TAILQ_FIRST(&worker->tasks_pool))) {
570 		TAILQ_REMOVE(&worker->tasks_pool, task, link);
571 		_free_task_buffers(task);
572 	}
573 }
574 
575 static int
576 _check_draining(void *arg)
577 {
578 	struct worker_thread *worker = arg;
579 
580 	assert(worker);
581 
582 	if (worker->current_queue_depth == 0) {
583 		_free_task_buffers_in_pool(worker);
584 		spdk_poller_unregister(&worker->is_draining_poller);
585 		unregister_worker(worker);
586 	}
587 
588 	return SPDK_POLLER_BUSY;
589 }
590 
591 static int
592 _worker_stop(void *arg)
593 {
594 	struct worker_thread *worker = arg;
595 
596 	assert(worker);
597 
598 	spdk_poller_unregister(&worker->stop_poller);
599 
600 	/* now let the worker drain and check it's outstanding IO with a poller */
601 	worker->is_draining = true;
602 	worker->is_draining_poller = SPDK_POLLER_REGISTER(_check_draining, worker, 0);
603 
604 	return SPDK_POLLER_BUSY;
605 }
606 
607 static void
608 _init_thread(void *arg1)
609 {
610 	struct worker_thread *worker;
611 	struct ap_task *task;
612 	int i, num_tasks = g_allocate_depth;
613 	struct display_info *display = arg1;
614 
615 	worker = calloc(1, sizeof(*worker));
616 	if (worker == NULL) {
617 		fprintf(stderr, "Unable to allocate worker\n");
618 		free(display);
619 		return;
620 	}
621 
622 	worker->workload = g_workload_selection;
623 	worker->display.core = display->core;
624 	worker->display.thread = display->thread;
625 	free(display);
626 	worker->core = spdk_env_get_current_core();
627 	worker->thread = spdk_get_thread();
628 	pthread_mutex_lock(&g_workers_lock);
629 	g_num_workers++;
630 	worker->next = g_workers;
631 	g_workers = worker;
632 	pthread_mutex_unlock(&g_workers_lock);
633 	worker->ch = spdk_accel_get_io_channel();
634 	if (worker->ch == NULL) {
635 		fprintf(stderr, "Unable to get an accel channel\n");
636 		goto error;
637 	}
638 
639 	TAILQ_INIT(&worker->tasks_pool);
640 
641 	worker->task_base = calloc(num_tasks, sizeof(struct ap_task));
642 	if (worker->task_base == NULL) {
643 		fprintf(stderr, "Could not allocate task base.\n");
644 		goto error;
645 	}
646 
647 	task = worker->task_base;
648 	for (i = 0; i < num_tasks; i++) {
649 		TAILQ_INSERT_TAIL(&worker->tasks_pool, task, link);
650 		task->worker = worker;
651 		if (_get_task_data_bufs(task)) {
652 			fprintf(stderr, "Unable to get data bufs\n");
653 			goto error;
654 		}
655 		task++;
656 	}
657 
658 	/* Register a poller that will stop the worker at time elapsed */
659 	worker->stop_poller = SPDK_POLLER_REGISTER(_worker_stop, worker,
660 			      g_time_in_sec * 1000000ULL);
661 
662 	/* Load up queue depth worth of operations. */
663 	for (i = 0; i < g_queue_depth; i++) {
664 		task = _get_task(worker);
665 		if (task == NULL) {
666 			goto error;
667 		}
668 
669 		_submit_single(worker, task);
670 	}
671 	return;
672 error:
673 
674 	_free_task_buffers_in_pool(worker);
675 	free(worker->task_base);
676 	spdk_app_stop(-1);
677 }
678 
679 static void
680 accel_perf_start(void *arg1)
681 {
682 	struct spdk_cpuset tmp_cpumask = {};
683 	char thread_name[32];
684 	uint32_t i;
685 	int j;
686 	struct spdk_thread *thread;
687 	struct display_info *display;
688 
689 	g_tsc_rate = spdk_get_ticks_hz();
690 	g_tsc_end = spdk_get_ticks() + g_time_in_sec * g_tsc_rate;
691 
692 	dump_user_config();
693 
694 	printf("Running for %d seconds...\n", g_time_in_sec);
695 	fflush(stdout);
696 
697 	/* Create worker threads for each core that was specified. */
698 	SPDK_ENV_FOREACH_CORE(i) {
699 		for (j = 0; j < g_threads_per_core; j++) {
700 			snprintf(thread_name, sizeof(thread_name), "ap_worker_%u_%u", i, j);
701 			spdk_cpuset_zero(&tmp_cpumask);
702 			spdk_cpuset_set_cpu(&tmp_cpumask, i, true);
703 			thread = spdk_thread_create(thread_name, &tmp_cpumask);
704 			display = calloc(1, sizeof(*display));
705 			if (display == NULL) {
706 				fprintf(stderr, "Unable to allocate memory\n");
707 				spdk_app_stop(-1);
708 				return;
709 			}
710 			display->core = i;
711 			display->thread = j;
712 			spdk_thread_send_msg(thread, _init_thread, display);
713 		}
714 	}
715 }
716 
717 int
718 main(int argc, char **argv)
719 {
720 	struct worker_thread *worker, *tmp;
721 
722 	pthread_mutex_init(&g_workers_lock, NULL);
723 	spdk_app_opts_init(&g_opts, sizeof(g_opts));
724 	g_opts.name = "accel_perf";
725 	g_opts.reactor_mask = "0x1";
726 	if (spdk_app_parse_args(argc, argv, &g_opts, "a:C:o:q:t:yw:P:f:T:", NULL, parse_args,
727 				usage) != SPDK_APP_PARSE_ARGS_SUCCESS) {
728 		g_rc = -1;
729 		goto cleanup;
730 	}
731 
732 	if ((g_workload_selection != ACCEL_OPC_COPY) &&
733 	    (g_workload_selection != ACCEL_OPC_FILL) &&
734 	    (g_workload_selection != ACCEL_OPC_CRC32C) &&
735 	    (g_workload_selection != ACCEL_OPC_COPY_CRC32C) &&
736 	    (g_workload_selection != ACCEL_OPC_COMPARE) &&
737 	    (g_workload_selection != ACCEL_OPC_DUALCAST)) {
738 		usage();
739 		g_rc = -1;
740 		goto cleanup;
741 	}
742 
743 	if (g_allocate_depth > 0 && g_queue_depth > g_allocate_depth) {
744 		fprintf(stdout, "allocate depth must be at least as big as queue depth\n");
745 		usage();
746 		g_rc = -1;
747 		goto cleanup;
748 	}
749 
750 	if (g_allocate_depth == 0) {
751 		g_allocate_depth = g_queue_depth;
752 	}
753 
754 	if ((g_workload_selection == ACCEL_OPC_CRC32C || g_workload_selection == ACCEL_OPC_COPY_CRC32C) &&
755 	    g_chained_count == 0) {
756 		usage();
757 		g_rc = -1;
758 		goto cleanup;
759 	}
760 
761 	g_rc = spdk_app_start(&g_opts, accel_perf_start, NULL);
762 	if (g_rc) {
763 		SPDK_ERRLOG("ERROR starting application\n");
764 	}
765 
766 	pthread_mutex_destroy(&g_workers_lock);
767 
768 	worker = g_workers;
769 	while (worker) {
770 		tmp = worker->next;
771 		free(worker);
772 		worker = tmp;
773 	}
774 cleanup:
775 	spdk_app_fini();
776 	return g_rc;
777 }
778