loopback_test.c 24.1 KB
Newer Older
1
// SPDX-License-Identifier: BSD-3-Clause
2 3 4 5 6 7 8 9 10 11 12
/*
 * Loopback test application
 *
 * Copyright 2015 Google Inc.
 * Copyright 2015 Linaro Ltd.
 */
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
13 14
#include <stdint.h>
#include <poll.h>
15 16 17 18
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <dirent.h>
19
#include <signal.h>
20

21
#define MAX_NUM_DEVICES 10
22 23 24 25 26 27 28 29 30 31 32 33 34 35
#define MAX_SYSFS_PATH	0x200
#define CSV_MAX_LINE	0x1000
#define SYSFS_MAX_INT	0x20
#define MAX_STR_LEN	255
#define DEFAULT_ASYNC_TIMEOUT 200000

struct dict {
	char *name;
	int type;
};

static struct dict dict[] = {
	{"ping", 2},
	{"transfer", 3},
36 37
	{"sink", 4},
	{NULL,}		/* list termination */
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
};

struct loopback_results {
	float latency_avg;
	uint32_t latency_max;
	uint32_t latency_min;
	uint32_t latency_jitter;

	float request_avg;
	uint32_t request_max;
	uint32_t request_min;
	uint32_t request_jitter;

	float throughput_avg;
	uint32_t throughput_max;
	uint32_t throughput_min;
	uint32_t throughput_jitter;

	float apbridge_unipro_latency_avg;
	uint32_t apbridge_unipro_latency_max;
	uint32_t apbridge_unipro_latency_min;
	uint32_t apbridge_unipro_latency_jitter;

61 62 63 64
	float gbphy_firmware_latency_avg;
	uint32_t gbphy_firmware_latency_max;
	uint32_t gbphy_firmware_latency_min;
	uint32_t gbphy_firmware_latency_jitter;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

	uint32_t error;
};

struct loopback_device {
	char name[MAX_SYSFS_PATH];
	char sysfs_entry[MAX_SYSFS_PATH];
	char debugfs_entry[MAX_SYSFS_PATH];
	struct loopback_results results;
};

struct loopback_test {
	int verbose;
	int debug;
	int raw_data_dump;
	int porcelain;
	int mask;
	int size;
	int iteration_max;
	int aggregate_output;
	int test_id;
	int device_count;
	int list_devices;
	int use_async;
	int async_timeout;
	int async_outstanding_operations;
	int us_wait;
92
	int file_output;
93
	int stop_all;
94
	int poll_count;
95 96 97
	char test_name[MAX_STR_LEN];
	char sysfs_prefix[MAX_SYSFS_PATH];
	char debugfs_prefix[MAX_SYSFS_PATH];
98
	struct timespec poll_timeout;
99 100
	struct loopback_device devices[MAX_NUM_DEVICES];
	struct loopback_results aggregate_results;
101
	struct pollfd fds[MAX_NUM_DEVICES];
102
};
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
struct loopback_test t;

/* Helper macros to calculate the aggregate results for all devices */
static inline int device_enabled(struct loopback_test *t, int dev_idx);

#define GET_MAX(field)							\
static int get_##field##_aggregate(struct loopback_test *t)		\
{									\
	uint32_t max = 0;						\
	int i;								\
	for (i = 0; i < t->device_count; i++) {				\
		if (!device_enabled(t, i))				\
			continue;					\
		if (t->devices[i].results.field > max)			\
			max = t->devices[i].results.field;		\
	}								\
	return max;							\
}									\

#define GET_MIN(field)							\
static int get_##field##_aggregate(struct loopback_test *t)		\
{									\
	uint32_t min = ~0;						\
	int i;								\
	for (i = 0; i < t->device_count; i++) {				\
		if (!device_enabled(t, i))				\
			continue;					\
		if (t->devices[i].results.field < min)			\
			min = t->devices[i].results.field;		\
	}								\
	return min;							\
}									\

#define GET_AVG(field)							\
static int get_##field##_aggregate(struct loopback_test *t)		\
{									\
	uint32_t val = 0;						\
	uint32_t count = 0;						\
	int i;								\
	for (i = 0; i < t->device_count; i++) {				\
		if (!device_enabled(t, i))				\
			continue;					\
		count++;						\
		val += t->devices[i].results.field;			\
	}								\
	if (count)							\
		val /= count;						\
	return val;							\
}									\

GET_MAX(throughput_max);
GET_MAX(request_max);
GET_MAX(latency_max);
GET_MAX(apbridge_unipro_latency_max);
158
GET_MAX(gbphy_firmware_latency_max);
159 160 161 162
GET_MIN(throughput_min);
GET_MIN(request_min);
GET_MIN(latency_min);
GET_MIN(apbridge_unipro_latency_min);
163
GET_MIN(gbphy_firmware_latency_min);
164 165 166 167
GET_AVG(throughput_avg);
GET_AVG(request_avg);
GET_AVG(latency_avg);
GET_AVG(apbridge_unipro_latency_avg);
168
GET_AVG(gbphy_firmware_latency_avg);
169

170
void abort(void)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
{
	_exit(1);
}

void usage(void)
{
	fprintf(stderr, "Usage: loopback_test TEST [SIZE] ITERATIONS [SYSPATH] [DBGPATH]\n\n"
	"  Run TEST for a number of ITERATIONS with operation data SIZE bytes\n"
	"  TEST may be \'ping\' \'transfer\' or \'sink\'\n"
	"  SIZE indicates the size of transfer <= greybus max payload bytes\n"
	"  ITERATIONS indicates the number of times to execute TEST at SIZE bytes\n"
	"             Note if ITERATIONS is set to zero then this utility will\n"
	"             initiate an infinite (non terminating) test and exit\n"
	"             without logging any metrics data\n"
	"  SYSPATH indicates the sysfs path for the loopback greybus entries e.g.\n"
	"          /sys/bus/greybus/devices\n"
	"  DBGPATH indicates the debugfs path for the loopback greybus entries e.g.\n"
	"          /sys/kernel/debug/gb_loopback/\n"
	" Mandatory arguments\n"
	"   -t     must be one of the test names - sink, transfer or ping\n"
	"   -i     iteration count - the number of iterations to run the test over\n"
	" Optional arguments\n"
193
	"   -S     sysfs location - location for greybus 'endo' entries default /sys/bus/greybus/devices/\n"
194 195 196 197 198 199 200 201 202 203 204 205
	"   -D     debugfs location - location for loopback debugfs entries default /sys/kernel/debug/gb_loopback/\n"
	"   -s     size of data packet to send during test - defaults to zero\n"
	"   -m     mask - a bit mask of connections to include example: -m 8 = 4th connection -m 9 = 1st and 4th connection etc\n"
	"                 default is zero which means broadcast to all connections\n"
	"   -v     verbose output\n"
	"   -d     debug output\n"
	"   -r     raw data output - when specified the full list of latency values are included in the output CSV\n"
	"   -p     porcelain - when specified printout is in a user-friendly non-CSV format. This option suppresses writing to CSV file\n"
	"   -a     aggregate - show aggregation of all enabled devices\n"
	"   -l     list found loopback devices and exit\n"
	"   -x     Async - Enable async transfers\n"
	"   -o     Async Timeout - Timeout in uSec for async operations\n"
206
	"   -O     Poll loop time out in seconds(max time a test is expected to last, default: 30sec)\n"
207 208
	"   -c     Max number of outstanding operations for async operations\n"
	"   -w     Wait in uSec between operations\n"
209
	"   -z     Enable output to a CSV file (incompatible with -p)\n"
210
	"   -f     When starting new loopback test, stop currently running tests on all devices\n"
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
	"Examples:\n"
	"  Send 10000 transfers with a packet size of 128 bytes to all active connections\n"
	"  loopback_test -t transfer -s 128 -i 10000 -S /sys/bus/greybus/devices/ -D /sys/kernel/debug/gb_loopback/\n"
	"  loopback_test -t transfer -s 128 -i 10000 -m 0\n"
	"  Send 10000 transfers with a packet size of 128 bytes to connection 1 and 4\n"
	"  loopback_test -t transfer -s 128 -i 10000 -m 9\n"
	"  loopback_test -t ping -s 0 128 -i -S /sys/bus/greybus/devices/ -D /sys/kernel/debug/gb_loopback/\n"
	"  loopback_test -t sink -s 2030 -i 32768 -S /sys/bus/greybus/devices/ -D /sys/kernel/debug/gb_loopback/\n");
	abort();
}

static inline int device_enabled(struct loopback_test *t, int dev_idx)
{
	if (!t->mask || (t->mask & (1 << dev_idx)))
		return 1;

	return 0;
}

static void show_loopback_devices(struct loopback_test *t)
{
	int i;

	if (t->device_count == 0) {
		printf("No loopback devices.\n");
		return;
	}

	for (i = 0; i < t->device_count; i++)
		printf("device[%d] = %s\n", i, t->devices[i].name);

}

int open_sysfs(const char *sys_pfx, const char *node, int flags)
{
	int fd;
	char path[MAX_SYSFS_PATH];

	snprintf(path, sizeof(path), "%s%s", sys_pfx, node);
	fd = open(path, flags);
	if (fd < 0) {
		fprintf(stderr, "unable to open %s\n", path);
		abort();
	}
	return fd;
}

int read_sysfs_int_fd(int fd, const char *sys_pfx, const char *node)
{
	char buf[SYSFS_MAX_INT];

	if (read(fd, buf, sizeof(buf)) < 0) {
		fprintf(stderr, "unable to read from %s%s %s\n", sys_pfx, node,
			strerror(errno));
		close(fd);
		abort();
	}
	return atoi(buf);
}

float read_sysfs_float_fd(int fd, const char *sys_pfx, const char *node)
{
	char buf[SYSFS_MAX_INT];

	if (read(fd, buf, sizeof(buf)) < 0) {

		fprintf(stderr, "unable to read from %s%s %s\n", sys_pfx, node,
			strerror(errno));
		close(fd);
		abort();
	}
	return atof(buf);
}

int read_sysfs_int(const char *sys_pfx, const char *node)
{
	int fd, val;

	fd = open_sysfs(sys_pfx, node, O_RDONLY);
	val = read_sysfs_int_fd(fd, sys_pfx, node);
	close(fd);
	return val;
}

float read_sysfs_float(const char *sys_pfx, const char *node)
{
	int fd;
	float val;

	fd = open_sysfs(sys_pfx, node, O_RDONLY);
	val = read_sysfs_float_fd(fd, sys_pfx, node);
	close(fd);
	return val;
}

void write_sysfs_val(const char *sys_pfx, const char *node, int val)
{
	int fd, len;
	char buf[SYSFS_MAX_INT];

	fd = open_sysfs(sys_pfx, node, O_RDWR);
	len = snprintf(buf, sizeof(buf), "%d", val);
	if (write(fd, buf, len) < 0) {
		fprintf(stderr, "unable to write to %s%s %s\n", sys_pfx, node,
			strerror(errno));
		close(fd);
		abort();
	}
	close(fd);
}

static int get_results(struct loopback_test *t)
{
	struct loopback_device *d;
	struct loopback_results *r;
	int i;

	for (i = 0; i < t->device_count; i++) {
		if (!device_enabled(t, i))
			continue;

		d = &t->devices[i];
		r = &d->results;

		r->error = read_sysfs_int(d->sysfs_entry, "error");
		r->request_min = read_sysfs_int(d->sysfs_entry, "requests_per_second_min");
		r->request_max = read_sysfs_int(d->sysfs_entry, "requests_per_second_max");
		r->request_avg = read_sysfs_float(d->sysfs_entry, "requests_per_second_avg");

		r->latency_min = read_sysfs_int(d->sysfs_entry, "latency_min");
		r->latency_max = read_sysfs_int(d->sysfs_entry, "latency_max");
		r->latency_avg = read_sysfs_float(d->sysfs_entry, "latency_avg");

		r->throughput_min = read_sysfs_int(d->sysfs_entry, "throughput_min");
		r->throughput_max = read_sysfs_int(d->sysfs_entry, "throughput_max");
		r->throughput_avg = read_sysfs_float(d->sysfs_entry, "throughput_avg");

		r->apbridge_unipro_latency_min =
			read_sysfs_int(d->sysfs_entry, "apbridge_unipro_latency_min");
		r->apbridge_unipro_latency_max =
			read_sysfs_int(d->sysfs_entry, "apbridge_unipro_latency_max");
		r->apbridge_unipro_latency_avg =
			read_sysfs_float(d->sysfs_entry, "apbridge_unipro_latency_avg");

355 356 357 358 359 360
		r->gbphy_firmware_latency_min =
			read_sysfs_int(d->sysfs_entry, "gbphy_firmware_latency_min");
		r->gbphy_firmware_latency_max =
			read_sysfs_int(d->sysfs_entry, "gbphy_firmware_latency_max");
		r->gbphy_firmware_latency_avg =
			read_sysfs_float(d->sysfs_entry, "gbphy_firmware_latency_avg");
361 362 363 364 365 366

		r->request_jitter = r->request_max - r->request_min;
		r->latency_jitter = r->latency_max - r->latency_min;
		r->throughput_jitter = r->throughput_max - r->throughput_min;
		r->apbridge_unipro_latency_jitter =
			r->apbridge_unipro_latency_max - r->apbridge_unipro_latency_min;
367 368
		r->gbphy_firmware_latency_jitter =
			r->gbphy_firmware_latency_max - r->gbphy_firmware_latency_min;
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

	}

	/*calculate the aggregate results of all enabled devices */
	if (t->aggregate_output) {
		r = &t->aggregate_results;

		r->request_min = get_request_min_aggregate(t);
		r->request_max = get_request_max_aggregate(t);
		r->request_avg = get_request_avg_aggregate(t);

		r->latency_min = get_latency_min_aggregate(t);
		r->latency_max = get_latency_max_aggregate(t);
		r->latency_avg = get_latency_avg_aggregate(t);

		r->throughput_min = get_throughput_min_aggregate(t);
		r->throughput_max = get_throughput_max_aggregate(t);
		r->throughput_avg = get_throughput_avg_aggregate(t);

		r->apbridge_unipro_latency_min =
			get_apbridge_unipro_latency_min_aggregate(t);
		r->apbridge_unipro_latency_max =
			get_apbridge_unipro_latency_max_aggregate(t);
		r->apbridge_unipro_latency_avg =
			get_apbridge_unipro_latency_avg_aggregate(t);

395 396 397 398 399 400
		r->gbphy_firmware_latency_min =
			get_gbphy_firmware_latency_min_aggregate(t);
		r->gbphy_firmware_latency_max =
			get_gbphy_firmware_latency_max_aggregate(t);
		r->gbphy_firmware_latency_avg =
			get_gbphy_firmware_latency_avg_aggregate(t);
401 402 403 404 405 406

		r->request_jitter = r->request_max - r->request_min;
		r->latency_jitter = r->latency_max - r->latency_min;
		r->throughput_jitter = r->throughput_max - r->throughput_min;
		r->apbridge_unipro_latency_jitter =
			r->apbridge_unipro_latency_max - r->apbridge_unipro_latency_min;
407 408
		r->gbphy_firmware_latency_jitter =
			r->gbphy_firmware_latency_max - r->gbphy_firmware_latency_min;
409 410 411 412 413 414 415

	}

	return 0;
}

int format_output(struct loopback_test *t,
416 417 418 419
		  struct loopback_results *r,
		  const char *dev_name,
		  char *buf, int buf_len,
		  struct tm *tm)
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
{
	int len = 0;

	memset(buf, 0x00, buf_len);
	len = snprintf(buf, buf_len, "%u-%u-%u %u:%u:%u",
		       tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
		       tm->tm_hour, tm->tm_min, tm->tm_sec);

	if (t->porcelain) {
		len += snprintf(&buf[len], buf_len - len,
			"\n test:\t\t\t%s\n path:\t\t\t%s\n size:\t\t\t%u\n iterations:\t\t%u\n errors:\t\t%u\n async:\t\t\t%s\n",
			t->test_name,
			dev_name,
			t->size,
			t->iteration_max,
			r->error,
			t->use_async ? "Enabled" : "Disabled");

		len += snprintf(&buf[len], buf_len - len,
			" requests per-sec:\tmin=%u, max=%u, average=%f, jitter=%u\n",
			r->request_min,
			r->request_max,
			r->request_avg,
			r->request_jitter);

		len += snprintf(&buf[len], buf_len - len,
			" ap-throughput B/s:\tmin=%u max=%u average=%f jitter=%u\n",
			r->throughput_min,
			r->throughput_max,
			r->throughput_avg,
			r->throughput_jitter);
		len += snprintf(&buf[len], buf_len - len,
			" ap-latency usec:\tmin=%u max=%u average=%f jitter=%u\n",
			r->latency_min,
			r->latency_max,
			r->latency_avg,
			r->latency_jitter);
		len += snprintf(&buf[len], buf_len - len,
			" apbridge-latency usec:\tmin=%u max=%u average=%f jitter=%u\n",
			r->apbridge_unipro_latency_min,
			r->apbridge_unipro_latency_max,
			r->apbridge_unipro_latency_avg,
			r->apbridge_unipro_latency_jitter);

		len += snprintf(&buf[len], buf_len - len,
465 466 467 468 469
			" gbphy-latency usec:\tmin=%u max=%u average=%f jitter=%u\n",
			r->gbphy_firmware_latency_min,
			r->gbphy_firmware_latency_max,
			r->gbphy_firmware_latency_avg,
			r->gbphy_firmware_latency_jitter);
470 471

	} else {
472
		len += snprintf(&buf[len], buf_len - len, ",%s,%s,%u,%u,%u",
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
			t->test_name, dev_name, t->size, t->iteration_max,
			r->error);

		len += snprintf(&buf[len], buf_len - len, ",%u,%u,%f,%u",
			r->request_min,
			r->request_max,
			r->request_avg,
			r->request_jitter);

		len += snprintf(&buf[len], buf_len - len, ",%u,%u,%f,%u",
			r->latency_min,
			r->latency_max,
			r->latency_avg,
			r->latency_jitter);

		len += snprintf(&buf[len], buf_len - len, ",%u,%u,%f,%u",
			r->throughput_min,
			r->throughput_max,
			r->throughput_avg,
			r->throughput_jitter);

		len += snprintf(&buf[len], buf_len - len, ",%u,%u,%f,%u",
			r->apbridge_unipro_latency_min,
			r->apbridge_unipro_latency_max,
			r->apbridge_unipro_latency_avg,
			r->apbridge_unipro_latency_jitter);

		len += snprintf(&buf[len], buf_len - len, ",%u,%u,%f,%u",
501 502 503 504
			r->gbphy_firmware_latency_min,
			r->gbphy_firmware_latency_max,
			r->gbphy_firmware_latency_avg,
			r->gbphy_firmware_latency_jitter);
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
	}

	printf("\n%s\n", buf);

	return len;
}

static int log_results(struct loopback_test *t)
{
	int fd, i, len, ret;
	struct tm tm;
	time_t local_time;
	char file_name[MAX_SYSFS_PATH];
	char data[CSV_MAX_LINE];

	local_time = time(NULL);
	tm = *localtime(&local_time);

	/*
524 525 526 527 528
	 * file name will test_name_size_iteration_max.csv
	 * every time the same test with the same parameters is run we will then
	 * append to the same CSV with datestamp - representing each test
	 * dataset.
	 */
529
	if (t->file_output && !t->porcelain) {
530
		snprintf(file_name, sizeof(file_name), "%s_%d_%d.csv",
531
			 t->test_name, t->size, t->iteration_max);
532

533
		fd = open(file_name, O_WRONLY | O_CREAT | O_APPEND, 0644);
534 535 536 537 538 539 540 541 542 543 544
		if (fd < 0) {
			fprintf(stderr, "unable to open %s for appendation\n", file_name);
			abort();
		}

	}
	for (i = 0; i < t->device_count; i++) {
		if (!device_enabled(t, i))
			continue;

		len = format_output(t, &t->devices[i].results,
545 546
				    t->devices[i].name,
				    data, sizeof(data), &tm);
547
		if (t->file_output && !t->porcelain) {
548 549 550 551 552 553 554 555 556 557
			ret = write(fd, data, len);
			if (ret == -1)
				fprintf(stderr, "unable to write %d bytes to csv.\n", len);
		}

	}


	if (t->aggregate_output) {
		len = format_output(t, &t->aggregate_results, "aggregate",
558
				    data, sizeof(data), &tm);
559
		if (t->file_output && !t->porcelain) {
560 561 562 563 564 565
			ret = write(fd, data, len);
			if (ret == -1)
				fprintf(stderr, "unable to write %d bytes to csv.\n", len);
		}
	}

566
	if (t->file_output && !t->porcelain)
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
		close(fd);

	return 0;
}

int is_loopback_device(const char *path, const char *node)
{
	char file[MAX_SYSFS_PATH];

	snprintf(file, MAX_SYSFS_PATH, "%s%s/iteration_count", path, node);
	if (access(file, F_OK) == 0)
		return 1;
	return 0;
}

int find_loopback_devices(struct loopback_test *t)
{
	struct dirent **namelist;
	int i, n, ret;
	unsigned int dev_id;
	struct loopback_device *d;

	n = scandir(t->sysfs_prefix, &namelist, NULL, alphasort);
	if (n < 0) {
		perror("scandir");
		ret = -ENODEV;
		goto baddir;
	}

	/* Don't include '.' and '..' */
	if (n <= 2) {
		ret = -ENOMEM;
		goto done;
	}

	for (i = 0; i < n; i++) {
		ret = sscanf(namelist[i]->d_name, "gb_loopback%u", &dev_id);
		if (ret != 1)
			continue;

		if (!is_loopback_device(t->sysfs_prefix, namelist[i]->d_name))
			continue;

		if (t->device_count == MAX_NUM_DEVICES) {
			fprintf(stderr, "max number of devices reached!\n");
			break;
		}

		d = &t->devices[t->device_count++];
		snprintf(d->name, MAX_STR_LEN, "gb_loopback%u", dev_id);

		snprintf(d->sysfs_entry, MAX_SYSFS_PATH, "%s%s/",
619
			 t->sysfs_prefix, d->name);
620 621

		snprintf(d->debugfs_entry, MAX_SYSFS_PATH, "%sraw_latency_%s",
622
			 t->debugfs_prefix, d->name);
623 624

		if (t->debug)
625
			printf("add %s %s\n", d->sysfs_entry, d->debugfs_entry);
626 627 628 629 630
	}

	ret = 0;
done:
	for (i = 0; i < n; i++)
631
		free(namelist[i]);
632 633 634 635 636
	free(namelist);
baddir:
	return ret;
}

637
static int open_poll_files(struct loopback_test *t)
638
{
639
	struct loopback_device *dev;
640
	char buf[MAX_SYSFS_PATH + MAX_STR_LEN];
641 642
	char dummy;
	int fds_idx = 0;
643 644 645
	int i;

	for (i = 0; i < t->device_count; i++) {
646 647
		dev = &t->devices[i];

648 649 650
		if (!device_enabled(t, i))
			continue;

651 652 653 654 655
		snprintf(buf, sizeof(buf), "%s%s", dev->sysfs_entry, "iteration_count");
		t->fds[fds_idx].fd = open(buf, O_RDONLY);
		if (t->fds[fds_idx].fd < 0) {
			fprintf(stderr, "Error opening poll file!\n");
			goto err;
656
		}
657
		read(t->fds[fds_idx].fd, &dummy, 1);
658
		t->fds[fds_idx].events = POLLERR | POLLPRI;
659 660
		t->fds[fds_idx].revents = 0;
		fds_idx++;
661 662
	}

663 664
	t->poll_count = fds_idx;

665
	return 0;
666 667 668

err:
	for (i = 0; i < fds_idx; i++)
669
		close(t->fds[i].fd);
670 671

	return -1;
672 673
}

674
static int close_poll_files(struct loopback_test *t)
675 676
{
	int i;
677 678
	for (i = 0; i < t->poll_count; i++)
		close(t->fds[i].fd);
679 680 681 682 683

	return 0;
}
static int is_complete(struct loopback_test *t)
{
684
	int iteration_count;
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
	int i;

	for (i = 0; i < t->device_count; i++) {
		if (!device_enabled(t, i))
			continue;

		iteration_count = read_sysfs_int(t->devices[i].sysfs_entry,
						 "iteration_count");

		/* at least one device did not finish yet */
		if (iteration_count != t->iteration_max)
			return 0;
	}

	return 1;
}

702 703 704 705 706 707 708 709 710 711 712 713 714
static void stop_tests(struct loopback_test *t)
{
	int i;

	for (i = 0; i < t->device_count; i++) {
		if (!device_enabled(t, i))
			continue;
		write_sysfs_val(t->devices[i].sysfs_entry, "type", 0);
	}
}

static void handler(int sig) { /* do nothing */  }

715 716
static int wait_for_complete(struct loopback_test *t)
{
717 718
	int number_of_events = 0;
	char dummy;
719
	int ret;
720
	int i;
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
	struct timespec *ts = NULL;
	struct sigaction sa;
	sigset_t mask_old, mask;

	sigemptyset(&mask);
	sigemptyset(&mask_old);
	sigaddset(&mask, SIGINT);
	sigprocmask(SIG_BLOCK, &mask, &mask_old);

	sa.sa_handler = handler;
	sa.sa_flags = 0;
	sigemptyset(&sa.sa_mask);
	if (sigaction(SIGINT, &sa, NULL) == -1) {
		fprintf(stderr, "sigaction error\n");
		return -1;
	}

	if (t->poll_timeout.tv_sec != 0)
		ts = &t->poll_timeout;
740 741 742

	while (1) {

743 744 745 746
		ret = ppoll(t->fds, t->poll_count, ts, &mask_old);
		if (ret <= 0) {
			stop_tests(t);
			fprintf(stderr, "Poll exit with errno %d\n", errno);
747 748
			return -1;
		}
749

750
		for (i = 0; i < t->poll_count; i++) {
751
			if (t->fds[i].revents & POLLPRI) {
752 753 754
				/* Dummy read to clear the event */
				read(t->fds[i].fd, &dummy, 1);
				number_of_events++;
755 756
			}
		}
757 758 759 760 761 762 763 764

		if (number_of_events == t->poll_count)
			break;
	}

	if (!is_complete(t)) {
		fprintf(stderr, "Iteration count did not finish!\n");
		return -1;
765 766 767 768 769 770 771 772 773
	}

	return 0;
}

static void prepare_devices(struct loopback_test *t)
{
	int i;

774 775
	/*
	 * Cancel any running tests on enabled devices. If
776 777
	 * stop_all option is given, stop test on all devices.
	 */
778
	for (i = 0; i < t->device_count; i++)
779
		if (t->stop_all || device_enabled(t, i))
780
			write_sysfs_val(t->devices[i].sysfs_entry, "type", 0);
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797


	for (i = 0; i < t->device_count; i++) {
		if (!device_enabled(t, i))
			continue;

		write_sysfs_val(t->devices[i].sysfs_entry, "us_wait",
				t->us_wait);

		/* Set operation size */
		write_sysfs_val(t->devices[i].sysfs_entry, "size", t->size);

		/* Set iterations */
		write_sysfs_val(t->devices[i].sysfs_entry, "iteration_max",
				t->iteration_max);

		if (t->use_async) {
798
			write_sysfs_val(t->devices[i].sysfs_entry, "async", 1);
799
			write_sysfs_val(t->devices[i].sysfs_entry,
800
					"timeout", t->async_timeout);
801
			write_sysfs_val(t->devices[i].sysfs_entry,
802 803
					"outstanding_operations_max",
					t->async_outstanding_operations);
804
		} else
805
			write_sysfs_val(t->devices[i].sysfs_entry, "async", 0);
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
	}
}

static int start(struct loopback_test *t)
{
	int i;

	/* the test starts by writing test_id to the type file. */
	for (i = 0; i < t->device_count; i++) {
		if (!device_enabled(t, i))
			continue;

		write_sysfs_val(t->devices[i].sysfs_entry, "type", t->test_id);
	}

	return 0;
}


void loopback_run(struct loopback_test *t)
{
	int i;
	int ret;

830
	for (i = 0; dict[i].name != NULL; i++) {
831 832 833 834 835 836 837 838 839 840 841
		if (strstr(dict[i].name, t->test_name))
			t->test_id = dict[i].type;
	}
	if (!t->test_id) {
		fprintf(stderr, "invalid test %s\n", t->test_name);
		usage();
		return;
	}

	prepare_devices(t);

842
	ret = open_poll_files(t);
843 844 845 846 847
	if (ret)
		goto err;

	start(t);

848 849 850 851
	ret = wait_for_complete(t);
	close_poll_files(t);
	if (ret)
		goto err;
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887


	get_results(t);

	log_results(t);

	return;

err:
	printf("Error running test\n");
	return;
}

static int sanity_check(struct loopback_test *t)
{
	int i;

	if (t->device_count == 0) {
		fprintf(stderr, "No loopback devices found\n");
		return -1;
	}

	for (i = 0; i < MAX_NUM_DEVICES; i++) {
		if (!device_enabled(t, i))
			continue;

		if (t->mask && !strcmp(t->devices[i].name, "")) {
			fprintf(stderr, "Bad device mask %x\n", (1 << i));
			return -1;
		}

	}


	return 0;
}
888

889 890 891 892 893 894 895 896 897
int main(int argc, char *argv[])
{
	int o, ret;
	char *sysfs_prefix = "/sys/class/gb_loopback/";
	char *debugfs_prefix = "/sys/kernel/debug/gb_loopback/";

	memset(&t, 0, sizeof(t));

	while ((o = getopt(argc, argv,
898
			   "t:s:i:S:D:m:v::d::r::p::a::l::x::o:O:c:w:z::f::")) != -1) {
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
		switch (o) {
		case 't':
			snprintf(t.test_name, MAX_STR_LEN, "%s", optarg);
			break;
		case 's':
			t.size = atoi(optarg);
			break;
		case 'i':
			t.iteration_max = atoi(optarg);
			break;
		case 'S':
			snprintf(t.sysfs_prefix, MAX_SYSFS_PATH, "%s", optarg);
			break;
		case 'D':
			snprintf(t.debugfs_prefix, MAX_SYSFS_PATH, "%s", optarg);
			break;
		case 'm':
			t.mask = atol(optarg);
			break;
		case 'v':
			t.verbose = 1;
			break;
		case 'd':
			t.debug = 1;
			break;
		case 'r':
			t.raw_data_dump = 1;
			break;
		case 'p':
			t.porcelain = 1;
			break;
		case 'a':
			t.aggregate_output = 1;
			break;
		case 'l':
			t.list_devices = 1;
			break;
		case 'x':
			t.use_async = 1;
			break;
		case 'o':
			t.async_timeout = atoi(optarg);
			break;
942
		case 'O':
943
			t.poll_timeout.tv_sec = atoi(optarg);
944
			break;
945 946 947 948 949 950
		case 'c':
			t.async_outstanding_operations = atoi(optarg);
			break;
		case 'w':
			t.us_wait = atoi(optarg);
			break;
951 952
		case 'z':
			t.file_output = 1;
953
			break;
954 955 956
		case 'f':
			t.stop_all = 1;
			break;
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
		default:
			usage();
			return -EINVAL;
		}
	}

	if (!strcmp(t.sysfs_prefix, ""))
		snprintf(t.sysfs_prefix, MAX_SYSFS_PATH, "%s", sysfs_prefix);

	if (!strcmp(t.debugfs_prefix, ""))
		snprintf(t.debugfs_prefix, MAX_SYSFS_PATH, "%s", debugfs_prefix);

	ret = find_loopback_devices(&t);
	if (ret)
		return ret;
	ret = sanity_check(&t);
	if (ret)
		return ret;

	if (t.list_devices) {
		show_loopback_devices(&t);
		return 0;
	}

	if (t.test_name[0] == '\0' || t.iteration_max == 0)
		usage();

	if (t.async_timeout == 0)
		t.async_timeout = DEFAULT_ASYNC_TIMEOUT;

	loopback_run(&t);

	return 0;
}