//{{{
void options_menu(void)
{
	int choice;
	int old_value, new_value;
	int change_made = 0;
	
	setTabStops(options_menu_tabs);
	UI_Menu_Set_Persist(1);
	while(1) {
#ifdef OPTIONS_SUBMENUS
		choice = UI_Menu_Pick(options_rect, count_options()+OPTIONS_SUBMENUS,
		                      &cb_options_menu);
		if(choice>=0 && choice<OPTIONS_SUBMENUS) {
			pick_submenu(choice);
			change_made = 1;
			continue;
		} else {
			choice -= OPTIONS_SUBMENUS;
		}
#else
		choice = UI_Menu_Pick(options_rect, count_options(), &cb_options_menu);
#endif
		
		if(choice<0)
			break;
		old_value = get_option(options[choice].key);
		new_value = (old_value+1) % count_choices(options[choice].choices);
		set_option(options[choice].key, new_value);
		change_made = 1;
		
#ifdef IS_CALCULATOR
		if(options[choice].key == OPTION_GRAYSCALE)
		{
			if(get_option(OPTION_GRAYSCALE) == OPTION_GRAY_ON)
				GrayOnThrow();
			else
				GrayOff();
			if(w->level) // Only if game is started
				full_redraw();
		}
#endif
#ifdef PALMOS
		if(options[choice].key == OPTION_INVERSE)
			init_colors();
#endif
#ifdef REALCOMPUTER
		if(options[choice].key == OPTION_COLOR)
			init_colors();
#endif
	}
	UI_Menu_Set_Persist(0);
	
	if(change_made)
		save_options();
}
//{{{
/// Return the currently-set value for the given option. If no value has been
/// set, return the default for that option. If there is no default, return -1.
int get_option(int option)
{
	int ii;
	int num_opts;
	// Find option setting
	for(ii=0; ii<num_options_set; ii++) {
		if(options_set[ii].key == option)
			return options_set[ii].value;
	}
	num_opts = count_options();
	// Find default value
	for(ii=0; ii<num_opts; ii++) {
		if(options[ii].key == option)
			return options[ii].default_value;
	}
	return -1;
}
Beispiel #3
0
static void parse_opts(int argc, char *argv[])
{
	unsigned int i, topts_len = count_options();
	char optstr[2 * ARRAY_SIZE(options) + 2 * topts_len];
	int opt;

	check_option_collision();

	optstr[0] = 0;

	for (i = 0; i < ARRAY_SIZE(options); i++)
		strcat(optstr, options[i].optstr);

	for (i = 0; i < topts_len; i++)
		strcat(optstr, tst_test->options[i].optstr);

	while ((opt = getopt(argc, argv, optstr)) > 0) {
		switch (opt) {
		case '?':
			print_help();
			tst_brk(TBROK, "Invalid option");
		case 'h':
			print_help();
			exit(0);
		case 'i':
			iterations = atoi(optarg);
		break;
		case 'I':
			duration = atof(optarg);
		break;
		case 'C':
#ifdef UCLINUX
			child_args = optarg;
#endif
		break;
		default:
			parse_topt(topts_len, opt, optarg);
		}
	}

	if (optind < argc)
		tst_brk(TBROK, "Unexpected argument(s) '%s'...", argv[optind]);
}
Beispiel #4
0
char * 
myargs_to_short(struct myargs options[])
{
    char * shortargs;
    int n = count_options(options);
    int i;
    int len=0;
    int max = n*2 + 1;
    shortargs = malloc(max);
    for(i=0; i< n; i++)
    {
        len+= snprintf(&shortargs[len], max-len, "%c", 
                options[i].shortname);
        if(options[i].type != MYARGS_NONE) {
            if(options[i].type == MYARGS_FLAG)
                len+= snprintf(&shortargs[len], max-len, "::");
            else
                len+= snprintf(&shortargs[len], max-len, ":");
        }
    }
    shortargs[len]=0;
    return shortargs;
}
Beispiel #5
0
const struct option *
myargs_to_long(struct myargs options[])
{
    struct option * longopts;
    int n = count_options(options);
    int i;
    longopts = malloc(sizeof(struct option) * (n+1));
    for(i=0;i<=n;i++)
    {
        if(options[i].name)
            longopts[i].name = strdup(options[i].name);
        else 
            longopts[i].name = NULL;
        if( options[i].type == MYARGS_NONE ) 
            longopts[i].has_arg = no_argument;
        else if ( options[i].type == MYARGS_FLAG)
            longopts[i].has_arg = optional_argument;
        else
            longopts[i].has_arg = required_argument;
        longopts[i].flag =  NULL;
        longopts[i].val  = options[i].shortname;
    }
    return longopts;
}
Beispiel #6
0
int main(int argc, char * argv[]) {
    struct fakeswitch *fakeswitches;

    char * controller_hostname = myargs_get_default_string(my_options,
            "controller");
    int controller_port = myargs_get_default_integer(my_options, "port");
    int n_fakeswitches = myargs_get_default_integer(my_options, "switches");
    int total_mac_addresses = myargs_get_default_integer(my_options,
            "mac-addresses");
    int mstestlen = myargs_get_default_integer(my_options, "ms-per-test");
    int should_test_range = myargs_get_default_flag(my_options, "ranged-test");
    int tests_per_loop = myargs_get_default_integer(my_options, "loops");
    int debug = myargs_get_default_flag(my_options, "debug");
    int warmup = myargs_get_default_integer(my_options, "warmup");
    int cooldown = myargs_get_default_integer(my_options, "cooldown");
    int delay = myargs_get_default_integer(my_options, "delay");
    int connect_delay = myargs_get_default_integer(my_options, "connect-delay");
    int connect_group_size = myargs_get_default_integer(my_options,
            "connect-group-size");
    int learn_dst_macs = myargs_get_default_flag(my_options, "learn-dst-macs");
    int dpid_offset = myargs_get_default_integer(my_options, "dpid-offset");
    int mode = MODE_LATENCY;
    int i, j;

    const struct option * long_opts = myargs_to_long(my_options);
    char * short_opts = myargs_to_short(my_options);

    /* parse args here */
    while (1) {
        int c;
        int option_index = 0;
        c = getopt_long(argc, argv, short_opts, long_opts, &option_index);
        if (c == -1)
            break;
        switch (c) {
        case 'c':
            controller_hostname = strdup(optarg);
            break;
        case 'd':
            debug = 1;
            break;
        case 'h':
            myargs_usage(my_options, PROG_TITLE, "help message", NULL, 1);
            break;
        case 'L':
            if (optarg)
                learn_dst_macs = (strcasecmp("true", optarg) == 0
                        || strcasecmp("on", optarg) == 0
                        || strcasecmp("1", optarg) == 0);
            else
                learn_dst_macs = 1;
            break;
        case 'l':
            tests_per_loop = atoi(optarg);
            break;
        case 'M':
            total_mac_addresses = atoi(optarg);
            break;
        case 'm':
            mstestlen = atoi(optarg);
            break;
        case 'r':
            should_test_range = 1;
            break;
        case 'p':
            controller_port = atoi(optarg);
            break;
        case 's':
            n_fakeswitches = atoi(optarg);
            break;
        case 't':
            mode = MODE_THROUGHPUT;
            break;
        case 'w':
            warmup = atoi(optarg);
            break;
        case 'C':
            cooldown = atoi(optarg);
            break;
        case 'D':
            delay = atoi(optarg);
            break;
        case 'i':
            connect_delay = atoi(optarg);
            break;
        case 'I':
            connect_group_size = atoi(optarg);
            break;
        case 'o':
            dpid_offset = atoi(optarg);
            break;
        default:
            myargs_usage(my_options, PROG_TITLE, "help message", NULL, 1);
        }
    }

    if (warmup + cooldown >= tests_per_loop) {
        fprintf(stderr,
                "Error warmup(%d) + cooldown(%d) >= number of tests (%d)\n",
                warmup, cooldown, tests_per_loop);
        exit(1);
    }

    printf(
            "cbench 1.4: controller benchmarking tool\n"
                    "   running in mode %s\n"
                    "   connecting to controller at %s:%d \n"
                    "   faking%s %d switches offset %d :: %d tests each; %d ms per test\n"
                    "   with %d unique source MACs per switch\n"
                    "   %s destination mac addresses before the test\n"
                    "   starting test with %d ms delay after features_reply\n"
                    "   ignoring first %d \"warmup\" and last %d \"cooldown\" loops\n"
                    "   connection delay of %dms per %d switch(es)\n"
                    "   debugging info is %s\n",
            mode == MODE_THROUGHPUT ? "'throughput'" : "'latency'",
            controller_hostname, controller_port,
            should_test_range ? " from 1 to" : "", n_fakeswitches, dpid_offset,
            tests_per_loop, mstestlen, total_mac_addresses,
            learn_dst_macs ? "learning" : "NOT learning", delay, warmup,
            cooldown, connect_delay, connect_group_size,
            debug == 1 ? "on" : "off");
    /* done parsing args */
    fakeswitches = malloc(n_fakeswitches * sizeof(struct fakeswitch));
    assert(fakeswitches);

    double *results;
    double min = DBL_MAX;
    double max = 0.0;
    double v;
    results = malloc(tests_per_loop * sizeof(double));

#ifdef USE_EPOLL
    struct epoll_event ev;
    epollfd = epoll_create(4096);
    if (epollfd == -1) {
        fprintf(stderr, "Cannot create epollfd.\n");
        exit(1);
    }
#endif

    for (i = 0; i < n_fakeswitches; i++) {
        int sock;
        double sum = 0;
        if (connect_delay != 0 && i != 0 && (i % connect_group_size == 0)) {
            if (debug)
                fprintf(stderr, "Delaying connection by %dms...",
                        connect_delay * 1000);
            usleep(connect_delay * 1000);
        }
        sock = make_tcp_connection(controller_hostname, controller_port, 3000,
                mode != MODE_THROUGHPUT);
        if (sock < 0) {
            fprintf(stderr, "make_nonblock_tcp_connection :: returned %d",
                    sock);
            exit(1);
        }
        if (debug)
            fprintf(stderr, "Initializing switch %d ... ", i + 1);
        fflush(stderr);
#ifdef USE_EPOLL
        fakeswitch_init(&fakeswitches[i], dpid_offset + i, sock, BUFLEN, debug,
                delay, mode, total_mac_addresses, learn_dst_macs);
#else
        fakeswitch_init(&fakeswitches[i], 0, sock, 65536, debug, delay, mode, total_mac_addresses, learn_dst_macs);
#endif

        if (debug)
            fprintf(stderr, " :: done.\n");
        fflush(stderr);

#ifdef USE_EPOLL
        ev.events = EPOLLIN | EPOLLOUT;
        ev.data.fd = sock;
        ev.data.ptr = &fakeswitches[i];

        if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &ev) == -1) {
            fprintf(stderr, "Cannot add sock to epoll\n");
            exit(1);
        }
#endif

        if (count_bits(i + 1) == 0)  // only test for 1,2,4,8,16 switches
            continue;
        if (!should_test_range && ((i + 1) != n_fakeswitches)) // only if testing range or this is last
            continue;
        for (j = 0; j < tests_per_loop; j++) {
            if (j > 0)
                delay = 0;      // only delay on the first run
            v = 1000.0 * run_test(i + 1, fakeswitches, mstestlen, delay);
            results[j] = v;
            if (j < warmup || j >= tests_per_loop - cooldown)
                continue;
            sum += v;
            if (v > max)
                max = v;
            if (v < min)
                min = v;
        }

        int counted_tests = (tests_per_loop - warmup - cooldown);
        // compute std dev
        double avg = sum / counted_tests;
        sum = 0.0;
        for (j = warmup; j < tests_per_loop - cooldown; ++j) {
            sum += pow(results[j] - avg, 2);
        }
        sum = sum / (double) (counted_tests);
        double std_dev = sqrt(sum);

        printf("RESULT: %d switches %d tests "
                "min/max/avg/stdev = %.2lf/%.2lf/%.2lf/%.2lf responses/s\n",
                i + 1, counted_tests, min, max, avg, std_dev);
    }
    printf("\n\nDestroying switches...\n\n");
    for (i = 0; i < n_fakeswitches; i++) {
        fakeswitch_destroy(&fakeswitches[i]);
    }

    int n = count_options(my_options);
    for (i = 0; i <= n; i++) {
        if (long_opts[i].name)
            free((char*)long_opts[i].name); // need cast because its const
    }

    if (long_opts->flag)
        free(long_opts->flag);
    free((struct option *) long_opts); // need cast because its const

    free(short_opts);

    free(results);
    free(fakeswitches);

    return 0;
}