#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>

#define SIXTEENMB 1024*1024*16
#define EIGHTKB 1024*8

void writeout(int fd, char *buf)
{
	int i;
	for (i = 0; i < SIXTEENMB / EIGHTKB; ++i) {
		if (write(fd, buf, EIGHTKB) != EIGHTKB) {
			fprintf(stderr, "Error in write: %m!\n");
			exit(1);
		}
	}
}

int main(int argc, char *argv[])
{
	int with_fallocate, open_close_iterations, rewrite_iterations;
	int fd, i, j;
	double tt;
	struct timeval tv1, tv2;
	char *buf0, *buf1;
	const char *filename; /* for convenience */

	if (argc != 4) {
		fprintf(stderr, "Usage: %s <filename> <open/close iterations> <rewrite iterations>\n", argv[0]);
		exit(1);
	}
	
	filename = argv[1];

	open_close_iterations = atoi(argv[2]);
	if (open_close_iterations < 0) {
		fprintf(stderr, "Error parsing 'open_close_iterations'!\n");
		exit(1);
	}

	rewrite_iterations = atoi(argv[3]);
	if (rewrite_iterations < 0) {
		fprintf(stderr, "Error parsing 'rewrite_iterations'!\n");
		exit(1);
	}

	buf0 = malloc(SIXTEENMB);
	if (!buf0) {
		fprintf(stderr, "Unable to allocate memory!\n");
		exit(1);
	}
	memset(buf0, 0, SIXTEENMB);

	buf1 = malloc(SIXTEENMB);
	if (!buf1) {
		fprintf(stderr, "Unable to allocate memory!\n");
		exit(1);
	}
	memset(buf1, 1, SIXTEENMB);

	for (with_fallocate = 0;with_fallocate < 2;++with_fallocate) {
		for (i = 0;i < open_close_iterations; ++i) {
			gettimeofday(&tv1, NULL);
			fd = open(filename, O_CREAT | O_EXCL | O_WRONLY);
			if (fd < 0) {
				fprintf(stderr, "Error opening file: %m\n");
                exit(1);
			}
			if (with_fallocate) {
				if (posix_fallocate(fd, 0, SIXTEENMB) != 0) {
					fprintf(stderr, "Error in posix_fallocate!\n");
                    exit(1);
				}
			} else {
				writeout(fd, buf0);
			}
			if (fsync(fd)) {
				fprintf(stderr, "Error in fdatasync: %m!\n");
				exit(1);
			}
			for (j = 0; j < rewrite_iterations; ++j) {
				lseek(fd, 0, SEEK_SET);
				writeout(fd, buf1);
				if (fdatasync(fd)) {
					fprintf(stderr, "Error in fdatasync: %m!\n");
					exit(1);
				}
			}
			if (close(fd)) {
				fprintf(stderr, "Error in close: %m!\n");
				exit(1);
			}
			unlink(filename);		/* don't check for error */
		}
		gettimeofday(&tv2, NULL);
		tt = (tv2.tv_usec + tv2.tv_sec * 1000000) - (tv1.tv_usec + tv1.tv_sec * 1000000);
		tt /= 1000000;
		fprintf(stderr,
			"with%s posix_fallocate: %d open/close iterations, %d rewrite in %0.4fs\n",
			(with_fallocate ? "" : "out"), open_close_iterations, rewrite_iterations, tt);
		sleep(5);
	}
	/* cleanup */
	free(buf0);
	free(buf1);
	return 0;
}
