testio.c

text/plain

Filename: testio.c
Type: text/plain
Part: 0
Message: Re: Slow count(*) again...
#define _GNU_SOURCE

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

// 4k alignment
static const unsigned int ALIGN_SHIFT = 12;

static void usage() {
	printf("Usage: test chunksize_kb filename\n");
	exit(1);
}

int main(int argc, char * argv[]) {
	if (argc != 3) {
		usage();
	}
	char * end;
	long chunksize_kb = strtol(argv[1], &end, 10);
	if (end == argv[1]) {
		printf("Cannot parse chunk size as number");
		usage();
	}
	long chunksize_bytes = chunksize_kb * 1024;
	int fd = open(argv[2], O_RDONLY|O_NOATIME|O_DIRECT);
	if (fd == -1) {
		perror("Unable to open input file");
		usage();
	}
	void * buf = malloc(chunksize_bytes + (1<<ALIGN_SHIFT));
	void * aligned_buf = (void*) ((((unsigned long)buf)>>ALIGN_SHIFT)<<ALIGN_SHIFT);
	ssize_t ret;
	do {
		ret = read(fd, aligned_buf, chunksize_bytes);
	} while (ret > 0);
	perror("read");
}