Exemplo n.º 1
0
dt_writer_t* dt_writer_create(const char *file_path, const char *version)
{
    struct fstrm_file_options *fopt = NULL;
    struct fstrm_writer_options *wopt = NULL;
    dt_writer_t *writer = NULL;
    fstrm_res res;

    writer = calloc(1, sizeof(dt_writer_t));
    if (writer == NULL) {
        goto fail;
    }

    // Set "version".
    if (version != NULL) {
        writer->len_version = strlen(version);
        writer->version = strdup(version);
        if (!writer->version) {
            goto fail;
        }
    }

    // Open writer.
    fopt = fstrm_file_options_init();
    fstrm_file_options_set_file_path(fopt, file_path);
    wopt = fstrm_writer_options_init();
    fstrm_writer_options_add_content_type(wopt,
                                          (const uint8_t *) DNSTAP_CONTENT_TYPE,
                                          strlen(DNSTAP_CONTENT_TYPE));
    writer->fw = fstrm_file_writer_init(fopt, wopt);
    fstrm_file_options_destroy(&fopt);
    fstrm_writer_options_destroy(&wopt);
    if (writer->fw == NULL) {
        goto fail;
    }

    res = fstrm_writer_open(writer->fw);
    if (res != fstrm_res_success) {
        goto fail;
    }

    return writer;
fail:
    dt_writer_free(writer);
    return NULL;
}
Exemplo n.º 2
0
int main(int argc, char **argv)
{
	const char *input_fname = NULL;
	const char *output_fname = NULL;

	fstrm_res res = fstrm_res_failure;
	struct fstrm_file_options *fopt = NULL;
	struct fstrm_writer_options *wopt = NULL;
	struct fstrm_reader *r = NULL;
	struct fstrm_writer *w = NULL;

	int rv = EXIT_FAILURE;

	/* Args. */
	if (argc != 2 && argc != 3) {
		fprintf(stderr, "Usage: %s <INPUT FILE> [<OUTPUT FILE>]\n", argv[0]);
		fprintf(stderr, "Dumps a Frame Streams formatted input file.\n\n");
		return EXIT_FAILURE;
	}
	input_fname = argv[1];
	if (argc == 3)
		output_fname = argv[2];

	/* Line buffering. */
	setvbuf(stdout, NULL, _IOLBF, 0);
	setvbuf(stderr, NULL, _IOLBF, 0);

	/* Setup file reader options. */
	fopt = fstrm_file_options_init();
	fstrm_file_options_set_file_path(fopt, input_fname);

	/* Initialize file reader. */
	r = fstrm_file_reader_init(fopt, NULL);
	if (r == NULL) {
		fputs("Error: fstrm_file_reader_init() failed.\n", stderr);
		goto out;
	}
	res = fstrm_reader_open(r);
	if (res != fstrm_res_success) {
		fputs("Error: fstrm_reader_open() failed.\n", stderr);
		goto out;
	}

	if (output_fname != NULL) {
		/* Setup file writer options. */
		fstrm_file_options_set_file_path(fopt, output_fname);

		/* Setup writer options. */
		wopt = fstrm_writer_options_init();

		/* Copy "content type" from the reader's START frame. */
		res = process_start_frame(r, wopt);
		if (res != fstrm_res_success) {
			fputs("Error: process_start_frame() failed.\n", stderr);
			goto out;
		}

		/* Initialize file writer. */
		w = fstrm_file_writer_init(fopt, wopt);
		if (w == NULL) {
			fputs("Error: fstrm_file_writer_init() failed.\n", stderr);
			goto out;
		}
		res = fstrm_writer_open(w);
		if (res != fstrm_res_success) {
			fstrm_writer_destroy(&w);
			fputs("Error: fstrm_writer_open() failed.\n", stderr);
			goto out;
		}
	} else {
		/* Process the START frame. */
		res = process_start_frame(r, NULL);
		if (res != fstrm_res_success) {
			fprintf(stderr, "Error: process_start_frame() failed.\n");
			goto out;
		}
	}

	/* Loop over data frames. */
	for (;;) {
		const uint8_t *data;
		size_t len_data;

		res = fstrm_reader_read(r, &data, &len_data);
		if (res == fstrm_res_success) {
			/* Got a data frame. */
			res = print_data_frame(data, len_data);
			if (res != fstrm_res_success) {
				fprintf(stderr, "Error: print_data_frame() failed.\n");
				goto out;
			}
			if (w != NULL) {
				/* Write the data frame. */
				res = fstrm_writer_write(w, data, len_data);
				if (res != fstrm_res_success) {
					fprintf(stderr, "Error: write_data_frame() failed.\n");
					goto out;
				}
			}
		} else if (res == fstrm_res_stop) {
			/* Normal end of data stream. */
			res = print_stop_frame(r);
			if (res != fstrm_res_success) {
				fprintf(stderr, "Error: unable to read STOP frame.\n");
				goto out;
			}
			rv = EXIT_SUCCESS;
			break;
		} else {
			/* Abnormal end. */
			fprintf(stderr, "Error: fstrm_reader_read() failed.\n");
			goto out;
		}
	}

out:
	/* Cleanup options. */
	fstrm_file_options_destroy(&fopt);
	fstrm_writer_options_destroy(&wopt);

	/* Cleanup reader. */
	fstrm_reader_destroy(&r);

	/* Cleanup writer. */
	if (w != NULL) {
		res = fstrm_writer_close(w);
		if (res != fstrm_res_success) {
			fprintf(stderr, "Error: fstrm_writer_close() failed.\n");
			rv = EXIT_FAILURE;
		}
		fstrm_writer_destroy(&w);
	}

	return rv;
}
Exemplo n.º 3
0
int
main(int argc, char **argv)
{
	struct timespec ts_a, ts_b;
	double elapsed;
	char *file_path;
	char *queue_model_str;
	unsigned num_messages;
	unsigned num_threads;
	fstrm_iothr_queue_model queue_model;

	if (argc != 5) {
		fprintf(stderr, "Usage: %s <FILE> <QUEUE MODEL> <NUM THREADS> <NUM MESSAGES>\n", argv[0]);
		fprintf(stderr, "\n");
		fprintf(stderr, "FILE is a filesystem path.\n");
		fprintf(stderr, "QUEUE MODEL is the string 'SPSC' or 'MPSC'.\n");
		fprintf(stderr, "NUM THREADS is an integer.\n");
		fprintf(stderr, "NUM MESSAGES is an integer.\n");
		return EXIT_FAILURE;
	}
	file_path = argv[1];
	queue_model_str = argv[2];
	num_threads = atoi(argv[3]);
	num_messages = atoi(argv[4]);
	if (num_threads < 1) {
		fprintf(stderr, "%s: Error: invalid number of threads\n", argv[0]);
		return EXIT_FAILURE;
	}
	if (num_messages < 1) {
		fprintf(stderr, "%s: Error: invalid number of messages\n", argv[0]);
		return EXIT_FAILURE;
	}

	if (strcasecmp(queue_model_str, "SPSC") == 0) {
		queue_model = FSTRM_IOTHR_QUEUE_MODEL_SPSC;
	} else if (strcasecmp(queue_model_str, "MPSC") == 0) {
		queue_model = FSTRM_IOTHR_QUEUE_MODEL_MPSC;
	} else {
		fprintf(stderr, "%s: Error: invalid queue model\n", argv[0]);
		return EXIT_FAILURE;
	}

	printf("testing fstrm_iothr with file= %s "
	       "queue_model= %s "
	       "num_threads= %u "
	       "num_messages= %u\n",
	       file_path, queue_model_str, num_threads, num_messages);

	struct fstrm_file_options *fopt;
	fopt = fstrm_file_options_init();
	fstrm_file_options_set_file_path(fopt, file_path);
	struct fstrm_writer *w = fstrm_file_writer_init(fopt, NULL);
	assert(w != NULL);
	fstrm_file_options_destroy(&fopt);

	struct fstrm_iothr_options *iothr_opt;
	iothr_opt = fstrm_iothr_options_init();

	if (queue_model == FSTRM_IOTHR_QUEUE_MODEL_SPSC) {
		fstrm_iothr_options_set_num_input_queues(iothr_opt, num_threads);
	} else if (queue_model == FSTRM_IOTHR_QUEUE_MODEL_MPSC) {
		fstrm_iothr_options_set_num_input_queues(iothr_opt, 1);
	} else {
		assert(0); /* not reached */
	}
	fstrm_iothr_options_set_queue_model(iothr_opt, queue_model);

	struct fstrm_iothr *iothr = fstrm_iothr_init(iothr_opt, &w);
	assert(iothr != NULL);
	fstrm_iothr_options_destroy(&iothr_opt);

	struct consumer test_consumer;
	struct producer test_producers[num_threads];

	for (unsigned i = 0; i < num_threads; i++) {
		test_producers[i].iothr = iothr;
		test_producers[i].num_messages = num_messages;
	}

	if (queue_model == FSTRM_IOTHR_QUEUE_MODEL_SPSC) {
		for (unsigned i = 0; i < num_threads; i++) {
			test_producers[i].ioq = fstrm_iothr_get_input_queue(iothr);
			assert(test_producers[i].ioq != NULL);
		}
	} else if (queue_model == FSTRM_IOTHR_QUEUE_MODEL_MPSC) {
		struct fstrm_iothr_queue *ioq = fstrm_iothr_get_input_queue(iothr);
		assert(ioq != NULL);
		for (unsigned i = 0; i < num_threads; i++)
			test_producers[i].ioq = ioq;
	} else {
		assert(0); /* not reached */
	}

	my_gettime(CLOCK_MONOTONIC, &ts_a);

	printf("creating %u producer threads\n", num_threads);
	for (unsigned i = 0; i < num_threads; i++)
		pthread_create(&test_producers[i].thr, NULL, thr_producer, &test_producers[i]);

	printf("joining %u producer threads\n", num_threads);
	for (unsigned i = 0; i < num_threads; i++)
		pthread_join(test_producers[i].thr, (void **) NULL);

	printf("destroying fstrm_iothr object\n");
	fstrm_iothr_destroy(&iothr);

	my_gettime(CLOCK_MONOTONIC, &ts_b);
	my_timespec_sub(&ts_a, &ts_b);
	elapsed = my_timespec_to_double(&ts_b);
	printf("completed in %.2f seconds\n", elapsed);

	int res = consume_input(&test_consumer, file_path);
	if (res != EXIT_SUCCESS)
		return res;

	struct producer_stats pstat_sum;
	memset(&pstat_sum, 0, sizeof(pstat_sum));
	for (unsigned i = 0; i < num_threads; i++) {
		pstat_sum.count_generated += test_producers[i].pstat.count_generated;
		pstat_sum.count_submitted += test_producers[i].pstat.count_submitted;
		pstat_sum.bytes_generated += test_producers[i].pstat.bytes_generated;
		pstat_sum.bytes_submitted += test_producers[i].pstat.bytes_submitted;
	}
	printf("count_generated= %" PRIu64 "\n", pstat_sum.count_generated);
	printf("bytes_generated= %" PRIu64 "\n", pstat_sum.bytes_generated);
	printf("count_submitted= %" PRIu64 "\n", pstat_sum.count_submitted);
	printf("bytes_submitted= %" PRIu64 "\n", pstat_sum.bytes_submitted);

	printf("count_received= %" PRIu64 " (%.3f)\n",
	       test_consumer.cstat.count_received,
	       (test_consumer.cstat.count_received + 0.0) / (pstat_sum.count_generated + 0.0)
	);
	printf("bytes_received= %" PRIu64 " (%.3f)\n",
	       test_consumer.cstat.bytes_received,
	       (test_consumer.cstat.bytes_received + 0.0) / (pstat_sum.bytes_generated + 0.0)
	);

	assert(pstat_sum.count_submitted == test_consumer.cstat.count_received);
	assert(pstat_sum.bytes_submitted == test_consumer.cstat.bytes_received);

	putchar('\n');

	return EXIT_SUCCESS;
}