int main (int argc, char *argv[]) { int i; unsigned int res; uint32_t maxsize; int opt; int bs; int write_size = 4096; int delay_time = 1000; int repetitions = 100; int print_time = 10; int have_size = 0; int listen_only = 0; int model = 1; while ( (opt = getopt(argc, argv, "qlsn:d:r:p:m:w:W:")) != -1 ) { switch (opt) { case 'w': // Write size in K bs = atoi(optarg); if (bs > 0) { write_size = bs*1024; have_size = 1; } break; case 'W': // Write size in bytes bs = atoi(optarg); if (bs > 0) { write_size = bs; have_size = 1; } break; case 'n': strcpy(group_name.value, optarg); group_name.length = strlen(group_name.value); break; case 'd': delay_time = atoi(optarg); break; case 'r': repetitions = atoi(optarg); break; case 'p': print_time = atoi(optarg); break; case 'l': listen_only = 1; break; case 's': do_syslog = 1; break; case 'q': quiet = 1; break; case 'm': model = atoi(optarg); if (model < 0 || model > 1) { fprintf(stderr, "%s: Model must be 0-1\n", argv[0]); exit(1); } break; case '?': usage(basename(argv[0])); exit(0); } } qb_log_init("cpghum", LOG_USER, LOG_EMERG); qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE); qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, "*", LOG_DEBUG); qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE); g_write_size = write_size; signal (SIGALRM, sigalrm_handler); signal (SIGINT, sigint_handler); switch (model) { case 0: res = cpg_initialize (&handle, &callbacks); break; case 1: res = cpg_model_initialize (&handle, CPG_MODEL_V1, (cpg_model_data_t *)&model1_data, NULL); break; default: res=999; // can't get here but it keeps the compiler happy break; } if (res != CS_OK) { printf ("cpg_initialize failed with result %d\n", res); exit (1); } pthread_create (&thread, NULL, dispatch_thread, NULL); res = cpg_join (handle, &group_name); if (res != CS_OK) { printf ("cpg_join failed with result %d\n", res); exit (1); } if (listen_only) { int secs; if (!quiet) { printf("-- Listening on CPG %s\n", group_name.value); printf("-- Ignore any starting \"counters don't match\" error while we catch up\n"); } /* Only check packet size if specified on the command-line */ if (!have_size) { g_write_size = 0; } while (!stopped) { sleep(1); if (++secs > print_time && !quiet) { printf ("%s: %5d message%s received. %d bytes\n", group_name.value, g_recv_count, g_recv_count==1?"":"s", g_recv_length); secs = 0; g_recv_count = 0; } } } else { cpg_max_atomic_msgsize_get (handle, &maxsize); if ( write_size > maxsize) { fprintf(stderr, "INFO: packet size (%d) is larger than the maximum atomic size (%d), libcpg will fragment\n", write_size, maxsize); } for (i = 0; i < repetitions && !stopped; i++) { cpg_test (handle, write_size, delay_time, print_time); signal (SIGALRM, sigalrm_handler); } } res = cpg_finalize (handle); if (res != CS_OK) { printf ("cpg_finalize failed with result %d\n", res); exit (1); } printf("\n"); printf("Stats:\n"); if (!listen_only) { printf(" packets sent: %d\n", packets_sent); printf(" send failures: %d\n", send_fails); printf(" send retries: %d\n", send_retries); } if (have_size) { printf(" length errors: %d\n", length_errors); } printf(" packets recvd: %d\n", packets_recvd); printf(" sequence errors: %d\n", sequence_errors); printf(" crc errors: %d\n", crc_errors); printf("\n"); return (0); }
int main (int argc, char *argv[]) { int i; unsigned int res; uint32_t maxsize; int opt; int bs; int write_size = 4096; int delay_time = 1000; int repetitions = 100; int print_time = 10; int have_size = 0; int listen_only = 0; int flood = 0; int model = 1; int option_index = 0; struct option long_options[] = { {"flood-start", required_argument, 0, 0 }, {"flood-mult", required_argument, 0, 0 }, {"flood-max", required_argument, 0, 0 }, {"size-kb", required_argument, 0, 'w' }, {"size-bytes", required_argument, 0, 'W' }, {"name", required_argument, 0, 'n' }, {"rtt", no_argument, 0, 't' }, {"flood", no_argument, 0, 'f' }, {"quiet", no_argument, 0, 'q' }, {"listen", no_argument, 0, 'l' }, {"help", no_argument, 0, '?' }, {0, 0, 0, 0 } }; while ( (opt = getopt_long(argc, argv, "qlstafMEn:d:r:p:m:w:W:D:", long_options, &option_index)) != -1 ) { switch (opt) { case 0: // Long-only options if (strcmp(long_options[option_index].name, "flood-start") == 0) { flood_start = parse_bytes(optarg); if (flood_start == 0) { fprintf(stderr, "flood-start value invalid\n"); exit(1); } } if (strcmp(long_options[option_index].name, "flood-mult") == 0) { flood_multiplier = parse_bytes(optarg); if (flood_multiplier == 0) { fprintf(stderr, "flood-mult value invalid\n"); exit(1); } } if (strcmp(long_options[option_index].name, "flood-max") == 0) { flood_max = parse_bytes(optarg); if (flood_max == 0) { fprintf(stderr, "flood-max value invalid\n"); exit(1); } } break; case 'w': // Write size in K bs = atoi(optarg); if (bs > 0) { write_size = bs*1024; have_size = 1; } break; case 'W': // Write size in bytes (or with a suffix) bs = parse_bytes(optarg); if (bs > 0) { write_size = bs; have_size = 1; } break; case 'n': if (strlen(optarg) >= CPG_MAX_NAME_LENGTH) { fprintf(stderr, "CPG name too long\n"); exit(1); } strcpy(group_name.value, optarg); group_name.length = strlen(group_name.value); break; case 't': report_rtt = 1; break; case 'E': to_stderr = 1; break; case 'M': machine_readable = 1; break; case 'f': flood = 1; break; case 'a': abort_on_error = 1; break; case 'd': delay_time = atoi(optarg); break; case 'D': delimiter = optarg[0]; break; case 'r': repetitions = atoi(optarg); break; case 'p': print_time = atoi(optarg); break; case 'l': listen_only = 1; break; case 's': do_syslog = 1; break; case 'q': quiet++; break; case 'm': model = atoi(optarg); if (model < 0 || model > 1) { fprintf(stderr, "%s: Model must be 0-1\n", argv[0]); exit(1); } break; case '?': usage(basename(argv[0])); exit(1); } } if (!have_size && flood) { write_size = flood_start; } signal (SIGALRM, sigalrm_handler); signal (SIGINT, sigint_handler); switch (model) { case 0: res = cpg_initialize (&handle, &callbacks); break; case 1: res = cpg_model_initialize (&handle, CPG_MODEL_V1, (cpg_model_data_t *)&model1_data, NULL); break; default: res=999; // can't get here but it keeps the compiler happy break; } if (res != CS_OK) { cpgh_log_printf(CPGH_LOG_ERR, "cpg_initialize failed with result %d\n", res); exit (1); } res = cpg_local_get(handle, &g_our_nodeid); if (res != CS_OK) { cpgh_log_printf(CPGH_LOG_ERR, "cpg_local_get failed with result %d\n", res); exit (1); } pthread_create (&thread, NULL, dispatch_thread, NULL); res = cpg_join (handle, &group_name); if (res != CS_OK) { cpgh_log_printf(CPGH_LOG_ERR, "cpg_join failed with result %d\n", res); exit (1); } if (listen_only) { int secs = 0; while (!stopped) { sleep(1); if (++secs > print_time && !quiet) { int nodes_printed = 0; if (!machine_readable) { for (i=1; i<MAX_NODEID; i++) { if (g_recv_counter[i]) { cpgh_log_printf(CPGH_LOG_INFO, "%s: %5d message%s of %d bytes received from node %d\n", group_name.value, g_recv_counter[i] - g_recv_start[i], g_recv_counter[i]==1?"":"s", g_recv_size[i], i); nodes_printed++; } } } /* Separate list of nodes if more than one */ if (nodes_printed > 1) { cpgh_log_printf(CPGH_LOG_INFO, "\n"); } secs = 0; } } } else { cpg_max_atomic_msgsize_get (handle, &maxsize); if (write_size > maxsize) { fprintf(stderr, "INFO: packet size (%d) is larger than the maximum atomic size (%d), libcpg will fragment\n", write_size, maxsize); } /* The main job starts here */ if (flood) { for (i = 0; i < 10; i++) { /* number of repetitions - up to 50k */ cpg_flood (handle, write_size); signal (SIGALRM, sigalrm_handler); write_size *= flood_multiplier; if (write_size > flood_max) { break; } } } else { send_counter = -1; /* So we start from zero to allow listeners to sync */ for (i = 0; i < repetitions && !stopped; i++) { cpg_test (handle, write_size, delay_time, print_time); signal (SIGALRM, sigalrm_handler); } } } res = cpg_finalize (handle); if (res != CS_OK) { cpgh_log_printf(CPGH_LOG_ERR, "cpg_finalize failed with result %d\n", res); exit (1); } if (quiet < 2) { /* Don't print LONG_MAX for min_rtt if we don't have a value */ if (min_rtt == LONG_MAX) { min_rtt = 0L; } if (machine_readable) { cpgh_log_printf(CPGH_LOG_STATS, "%d%c%d%c%d%c%d%c%d%c%d%c%d%c%ld%c%ld%c%ld\n", packets_sent, delimiter, send_fails, delimiter, send_retries, delimiter, length_errors, delimiter, packets_recvd, delimiter, sequence_errors, delimiter, crc_errors, delimiter, min_rtt, delimiter, avg_rtt, delimiter, max_rtt); } else { cpgh_log_printf(CPGH_LOG_STATS, "\n"); cpgh_log_printf(CPGH_LOG_STATS, "Stats:\n"); if (!listen_only) { cpgh_log_printf(CPGH_LOG_STATS, " packets sent: %d\n", packets_sent); cpgh_log_printf(CPGH_LOG_STATS, " send failures: %d\n", send_fails); cpgh_log_printf(CPGH_LOG_STATS, " send retries: %d\n", send_retries); } cpgh_log_printf(CPGH_LOG_STATS, " length errors: %d\n", length_errors); cpgh_log_printf(CPGH_LOG_STATS, " packets recvd: %d\n", packets_recvd); cpgh_log_printf(CPGH_LOG_STATS, " sequence errors: %d\n", sequence_errors); cpgh_log_printf(CPGH_LOG_STATS, " crc errors: %d\n", crc_errors); if (!listen_only) { cpgh_log_printf(CPGH_LOG_STATS, " min RTT: %ld\n", min_rtt); cpgh_log_printf(CPGH_LOG_STATS, " max RTT: %ld\n", max_rtt); cpgh_log_printf(CPGH_LOG_STATS, " avg RTT: %ld\n", avg_rtt); } cpgh_log_printf(CPGH_LOG_STATS, "\n"); } } res = 0; if (send_fails > 0 || (have_size && length_errors > 0) || sequence_errors > 0 || crc_errors > 0) { res = 2; } return (res); }