Exemple #1
0
int
main(int argc, char *argv[])
{
	time_t start;
	int ch, onerun, reps;
	const char *config, *home;

	config = NULL;

#ifdef _WIN32
	g.progname = "t_format.exe";
#else
	if ((g.progname = strrchr(argv[0], DIR_DELIM)) == NULL)
		g.progname = argv[0];
	else
		++g.progname;
#endif

#if 0
	/* Configure the GNU malloc for debugging. */
	(void)setenv("MALLOC_CHECK_", "2", 1);
#endif
#if 0
	/* Configure the FreeBSD malloc for debugging. */
	(void)setenv("MALLOC_OPTIONS", "AJ", 1);
#endif

	/* Track progress unless we're re-directing output to a file. */
	g.c_quiet = isatty(1) ? 0 : 1;

	/* Set values from the command line. */
	home = NULL;
	onerun = 0;
	while ((ch = __wt_getopt(
	    g.progname, argc, argv, "1C:c:H:h:Llqrt:")) != EOF)
		switch (ch) {
		case '1':			/* One run */
			onerun = 1;
			break;
		case 'C':			/* wiredtiger_open config */
			g.config_open = __wt_optarg;
			break;
		case 'c':			/* Configuration from a file */
			config = __wt_optarg;
			break;
		case 'H':
			g.helium_mount = __wt_optarg;
			break;
		case 'h':
			home = __wt_optarg;
			break;
		case 'L':			/* Re-direct output to a log */
			/*
			 * The -l option is a superset of -L, ignore -L if we
			 * have already configured logging for operations.
			 */
			if (g.logging == 0)
				g.logging = LOG_FILE;
			break;
		case 'l':			/* Turn on operation logging */
			g.logging = LOG_OPS;
			break;
		case 'q':			/* Quiet */
			g.c_quiet = 1;
			break;
		case 'r':			/* Replay a run */
			g.replay = 1;
			break;
		default:
			usage();
		}
	argc -= __wt_optind;
	argv += __wt_optind;

	/* Initialize the global RNG. */
	testutil_check(__wt_random_init_seed(NULL, &g.rnd));

	/* Set up paths. */
	path_setup(home);

	/* If it's a replay, use the home directory's CONFIG file. */
	if (g.replay) {
		if (config != NULL)
			testutil_die(EINVAL, "-c incompatible with -r");
		if (access(g.home_config, R_OK) != 0)
			testutil_die(ENOENT, "%s", g.home_config);
		config = g.home_config;
	}

	/*
	 * If we weren't given a configuration file, set values from "CONFIG",
	 * if it exists.
	 *
	 * Small hack to ignore any CONFIG file named ".", that just makes it
	 * possible to ignore any local CONFIG file, used when running checks.
	 */
	if (config == NULL && access("CONFIG", R_OK) == 0)
		config = "CONFIG";
	if (config != NULL && strcmp(config, ".") != 0)
		config_file(config);

	/*
	 * The rest of the arguments are individual configurations that modify
	 * the base configuration.
	 */
	for (; *argv != NULL; ++argv)
		config_single(*argv, 1);

	/*
	 * Multithreaded runs can be replayed: it's useful and we'll get the
	 * configuration correct.  Obviously the order of operations changes,
	 * warn the user.
	 */
	if (g.replay && !SINGLETHREADED)
		printf("Warning: replaying a threaded run\n");

	/*
	 * Single-threaded runs historically exited after a single replay, which
	 * makes sense when you're debugging, leave that semantic in place.
	 */
	if (g.replay && SINGLETHREADED)
		g.c_runs = 1;

	/*
	 * Let the command line -1 flag override runs configured from other
	 * sources.
	 */
	if (onerun)
		g.c_runs = 1;

	/*
	 * Initialize locks to single-thread named checkpoints and backups, last
	 * last-record updates, and failures.
	 */
	testutil_check(pthread_rwlock_init(&g.append_lock, NULL));
	testutil_check(pthread_rwlock_init(&g.backup_lock, NULL));
	testutil_check(pthread_rwlock_init(&g.checkpoint_lock, NULL));
	testutil_check(pthread_rwlock_init(&g.death_lock, NULL));

	printf("%s: process %" PRIdMAX "\n", g.progname, (intmax_t)getpid());
	while (++g.run_cnt <= g.c_runs || g.c_runs == 0 ) {
		startup();			/* Start a run */

		config_setup();			/* Run configuration */
		config_print(0);		/* Dump run configuration */
		key_len_setup();		/* Setup keys */

		start = time(NULL);
		track("starting up", 0ULL, NULL);

#ifdef HAVE_BERKELEY_DB
		if (SINGLETHREADED)
			bdb_open();		/* Initial file config */
#endif
		wts_open(g.home, true, &g.wts_conn);
		wts_init();

		wts_load();			/* Load initial records */
		wts_verify("post-bulk verify");	/* Verify */

		/*
		 * If we're not doing any operations, scan the bulk-load, copy
		 * the statistics and we're done. Otherwise, loop reading and
		 * operations, with a verify after each set.
		 */
		if (g.c_timer == 0 && g.c_ops == 0) {
			wts_read_scan();		/* Read scan */
			wts_stats();			/* Statistics */
		} else
			for (reps = 1; reps <= FORMAT_OPERATION_REPS; ++reps) {
				wts_read_scan();	/* Read scan */

							/* Operations */
				wts_ops(reps == FORMAT_OPERATION_REPS);

				/*
				 * Copy out the run's statistics after the last
				 * set of operations.
				 *
				 * XXX
				 * Verify closes the underlying handle and
				 * discards the statistics, read them first.
				 */
				if (reps == FORMAT_OPERATION_REPS)
					wts_stats();

							/* Verify */
				wts_verify("post-ops verify");
			}

		track("shutting down", 0ULL, NULL);
#ifdef HAVE_BERKELEY_DB
		if (SINGLETHREADED)
			bdb_close();
#endif
		wts_close();

		/*
		 * Rebalance testing.
		 */
		wts_rebalance();

		/*
		 * If single-threaded, we can dump and compare the WiredTiger
		 * and Berkeley DB data sets.
		 */
		if (SINGLETHREADED)
			wts_dump("standard", 1);

		/*
		 * Salvage testing.
		 */
		wts_salvage();

		/* Overwrite the progress line with a completion line. */
		if (!g.c_quiet)
			printf("\r%78s\r", " ");
		printf("%4d: %s, %s (%.0f seconds)\n",
		    g.run_cnt, g.c_data_source,
		    g.c_file_type, difftime(time(NULL), start));
		fflush(stdout);
	}

	/* Flush/close any logging information. */
	fclose_and_clear(&g.logfp);
	fclose_and_clear(&g.randfp);

	config_print(0);

	testutil_check(pthread_rwlock_destroy(&g.append_lock));
	testutil_check(pthread_rwlock_destroy(&g.backup_lock));
	testutil_check(pthread_rwlock_destroy(&g.checkpoint_lock));
	testutil_check(pthread_rwlock_destroy(&g.death_lock));

	config_clear();

	return (EXIT_SUCCESS);
}
int main(int argc, char *argv[]) {
	wispy_tcpserv wts;
	char errstr[WISPY_ERROR_MAX];
	fd_set sel_r_fds, sel_w_fds;
	struct timeval tm;
	
	wispy_device_list list;
	wispy_tcpserv_dev *devs = NULL;
	int ndev = 0;

	int x = 0, r = 0;

	static struct option long_options[] = {
		{ "port", required_argument, 0, 'p' },
		{ "bindaddr", required_argument, 0, 'a' },
		{ "broadcast", required_argument, 0, 'b' },
		{ "help", no_argument, 0, 'h' },
		{ "list", no_argument, 0, 'l' },
		{ "range", required_argument, 0, 'r' },
		{ 0, 0, 0, 0 }
	};
	int option_index;

	char *bindaddr = NULL;
	short int bindport = WISPY_NET_DEFAULT_PORT;

	int broadcast = 0, bcast_sock = -1;
	time_t last_bcast = 0;

	int list_only = 0;

	ndev = wispy_device_scan(&list);

	int *rangeset = NULL;
	if (ndev > 0) {
		rangeset = (int *) malloc(sizeof(int) * ndev);
		memset(rangeset, 0, sizeof(int) * ndev);
	}

	while (1) {
		int o = getopt_long(argc, argv, "p:a:b:lr:h",
							long_options, &option_index);

		if (o < 0)
			break;

		if (o == 'h') {
			Usage();
			exit(-1);
		} else if (o == 'a') {
			bindaddr = strdup(optarg);
			continue;
		} else if (o == 'p') {
			if (sscanf(optarg, "%hd", &bindport) != 1) {
				fprintf(stderr, "Expected port number\n");
				Usage();
				exit(-1);
			}
		} else if (o == 'b') {
			if (sscanf(optarg, "%d", &broadcast) != 1) {
				fprintf(stderr, "Expected broadcast time in seconds\n");
				Usage();
				exit(-1);
			}
		} else if (o == 'l') {
			list_only = 1;
		} else if (o == 'r' && ndev > 0) {
			if (sscanf(optarg, "%d:%d", &x, &r) != 2) {
				if (sscanf(optarg, "%d", &r) != 1) {
					fprintf(stderr, "Invalid range, expected device#:range# "
							"or range#\n");
					exit(-1);
				} else {
					rangeset[0] = r;
				}
			} else {
				if (x < 0 || x >= ndev) {
					fprintf(stderr, "Invalid range, no device %d\n", x);
					exit(-1);
				} else {
					rangeset[x] = r;
				}
			}
		}
	}

	if (list_only) {
		if (ndev <= 0) {
			printf("No wispy devices found, bailing\n");
			exit(1);
		}

		printf("Found %d devices...\n", ndev);

		for (x = 0; x < ndev; x++) {
			printf("Device %d: %s id %u\n", 
				   x, list.list[x].name, list.list[x].device_id);

			for (r = 0; r < list.list[x].num_sweep_ranges; r++) {
				wispy_sample_sweep *ran = 
					&(list.list[x].supported_ranges[r]);

				printf("  Range %d: \"%s\" %d%s-%d%s @ %0.2f%s, %d samples\n", r, 
					   ran->name,
					   ran->start_khz > 1000 ? 
					   ran->start_khz / 1000 : ran->start_khz,
					   ran->start_khz > 1000 ? "MHz" : "KHz",
					   ran->end_khz > 1000 ? ran->end_khz / 1000 : ran->end_khz,
					   ran->end_khz > 1000 ? "MHz" : "KHz",
					   (ran->res_hz / 1000) > 1000 ? 
					   		((float) ran->res_hz / 1000) / 1000 : ran->res_hz / 1000,
					   (ran->res_hz / 1000) > 1000 ? "MHz" : "KHz",
					   ran->num_samples);
			}

		}

		exit(0);
	}

	if (ndev <= 0) {
		printf("No wispy devices found, bailing\n");
		exit(1);
	}

	signal(SIGPIPE, &sigcatch);

	fprintf(stderr, "Found %d wispy devices...\n", ndev);

	/* devs = (wispy_phy *) malloc(WISPY_PHY_SIZE * ndev); */
	devs = (wispy_tcpserv_dev *) malloc(sizeof(wispy_tcpserv_dev) * ndev);

	for (x = 0; x < ndev; x++) {
		fprintf(stderr, "Initializing WiSPY device %s id %u\n", 
				list.list[x].name, list.list[x].device_id);

		devs[x].lock_fd = -1;

		if (wispy_device_init(&(devs[x].phydev), &(list.list[x])) < 0) {
			fprintf(stderr, "Error initializing WiSPY device %s id %u\n",
					list.list[x].name, list.list[x].device_id);
			fprintf(stderr, "%s\n", wispy_get_error(&(devs[x].phydev)));
			exit(1);
		}

		if (wispy_phy_open(&(devs[x].phydev)) < 0) {
			fprintf(stderr, "Error opening WiSPY device %s id %u\n",
					list.list[x].name, list.list[x].device_id);
			fprintf(stderr, "%s\n", wispy_get_error(&(devs[x].phydev)));
			exit(1);
		}

		wispy_phy_setcalibration(&(devs[x].phydev), 1);

		/* configure the default sweep block */
		wispy_phy_setposition(&(devs[x].phydev), rangeset[x], 0, 0);
	}
	wispy_device_scan_free(&list);

	wts_init(&wts);

	if (broadcast > 0) {
		if ((bcast_sock = wts_init_bcast(errstr, bindport)) < 0) {
			fprintf(stderr, "Broadcast init failed: %s\n", errstr);
			exit(1);
		}

		last_bcast = time(0);
	}

	wts.devs = devs;
	wts.ndev = ndev;

	if (wts_bind(&wts, bindaddr, bindport, errstr) < 0) {
		fprintf(stderr, "TCP bind failed: %s\n", errstr);
		exit(1);
	}

	fprintf(stderr, "TCP server listening on %s port %hd\n",
			bindaddr == NULL ? "(any)" : bindaddr,
			bindport);

	if (broadcast) {
		fprintf(stderr, "Broadcast server announcing on port %hd, %d seconds\n",
				bindport, broadcast);
	}

	while (1) {
		FD_ZERO(&sel_r_fds);
		FD_ZERO(&sel_w_fds);

		wts_fdset(&wts, &sel_r_fds, &sel_w_fds);

		tm.tv_sec = 0;
		tm.tv_usec = 100000;

		if (broadcast > 0 && time(0) - last_bcast > broadcast) {
			if (wts_send_bcast(bcast_sock, bindport, errstr) < 0) {
				fprintf(stderr, "Sending broadcast packet failed, %s\n", errstr);
				wts_shutdown(&wts);
				exit(1);
			}

			last_bcast = time(0);
		}

		if (select(wts.maxfd + 1, &sel_r_fds, &sel_w_fds, NULL, &tm) < 0) {
			fprintf(stderr, "Select() failed: %s\n", strerror(errno));
			wts_shutdown(&wts);
			exit(1);
		}

		if (wts_poll(&wts, &sel_r_fds, &sel_w_fds, errstr) < 0) {
			fprintf(stderr, "Polling failed: %s\n", errstr);
			wts_shutdown(&wts);
			exit(1);
		}
	}
}