2011-06-08 05:09:24 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <stdlib.h>
|
2011-06-14 05:11:51 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
2013-02-12 15:48:15 +00:00
|
|
|
#include <errno.h>
|
2011-06-08 05:09:24 +00:00
|
|
|
|
|
|
|
//#include "../client/ioctl.h"
|
|
|
|
|
|
|
|
#include <linux/ioctl.h>
|
|
|
|
#define CEPH_IOCTL_MAGIC 0x97
|
|
|
|
#define CEPH_IOC_SYNCIO _IO(CEPH_IOCTL_MAGIC, 5)
|
|
|
|
|
|
|
|
void write_pattern()
|
|
|
|
{
|
|
|
|
printf("writing pattern\n");
|
|
|
|
|
|
|
|
uint64_t i;
|
2011-06-14 19:53:33 +00:00
|
|
|
int r;
|
2011-06-08 05:09:24 +00:00
|
|
|
|
2013-02-12 15:48:15 +00:00
|
|
|
int fd = open("foo", O_CREAT|O_WRONLY, 0644);
|
|
|
|
if (fd < 0) {
|
|
|
|
r = errno;
|
|
|
|
printf("write_pattern: error: open() failed with: %d (%s)\n", r, strerror(r));
|
|
|
|
exit(r);
|
|
|
|
}
|
2011-06-08 05:09:24 +00:00
|
|
|
for (i=0; i<1048576 * sizeof(i); i += sizeof(i)) {
|
2011-06-14 19:53:33 +00:00
|
|
|
r = write(fd, &i, sizeof(i));
|
2013-03-20 15:56:03 +00:00
|
|
|
if (r == -1) {
|
|
|
|
r = errno;
|
|
|
|
printf("write_pattern: error: write() failed with: %d (%s)\n", r, strerror(r));
|
|
|
|
break;
|
|
|
|
}
|
2011-06-08 05:09:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int verify_pattern(char *buf, size_t len, uint64_t off)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i += sizeof(uint64_t)) {
|
|
|
|
uint64_t expected = i + off;
|
|
|
|
uint64_t actual = *(uint64_t*)(buf + i);
|
|
|
|
if (expected != actual) {
|
2011-06-14 05:11:51 +00:00
|
|
|
printf("error: offset %llu had %llu\n", (unsigned long long)expected,
|
|
|
|
(unsigned long long)actual);
|
2011-06-13 23:18:23 +00:00
|
|
|
exit(1);
|
2011-06-08 05:09:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-10 04:29:00 +00:00
|
|
|
void generate_pattern(void *buf, size_t len, uint64_t offset)
|
|
|
|
{
|
|
|
|
uint64_t *v = buf;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i=0; i<len / sizeof(v); i++)
|
|
|
|
v[i] = i * sizeof(v) + offset;
|
|
|
|
verify_pattern(buf, len, offset);
|
|
|
|
}
|
|
|
|
|
2013-02-12 15:48:15 +00:00
|
|
|
int read_file(int buf_align, uint64_t offset, int len, int direct) {
|
|
|
|
|
|
|
|
printf("read_file buf_align %d offset %llu len %d\n", buf_align,
|
2011-06-14 05:11:51 +00:00
|
|
|
(unsigned long long)offset, len);
|
2011-06-08 05:09:24 +00:00
|
|
|
void *rawbuf;
|
2013-02-12 15:48:15 +00:00
|
|
|
int r;
|
|
|
|
int flags;
|
2013-03-20 15:56:03 +00:00
|
|
|
int err = 0;
|
|
|
|
|
2013-02-12 15:48:15 +00:00
|
|
|
if(direct)
|
|
|
|
flags = O_RDONLY|O_DIRECT;
|
|
|
|
else
|
|
|
|
flags = O_RDONLY;
|
|
|
|
|
|
|
|
int fd = open("foo", flags);
|
|
|
|
if (fd < 0) {
|
2013-03-20 15:56:03 +00:00
|
|
|
err = errno;
|
2013-02-12 15:48:15 +00:00
|
|
|
printf("read_file: error: open() failed with: %d (%s)\n", err, strerror(err));
|
|
|
|
exit(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!direct)
|
|
|
|
ioctl(fd, CEPH_IOC_SYNCIO);
|
|
|
|
|
|
|
|
if ((r = posix_memalign(&rawbuf, 4096, len + buf_align)) != 0) {
|
|
|
|
printf("read_file: error: posix_memalign failed with %d", r);
|
2013-03-20 15:56:03 +00:00
|
|
|
close(fd);
|
2013-02-12 15:48:15 +00:00
|
|
|
exit (r);
|
|
|
|
}
|
|
|
|
|
2011-06-08 05:09:24 +00:00
|
|
|
void *buf = (char *)rawbuf + buf_align;
|
2011-06-13 23:23:28 +00:00
|
|
|
memset(buf, 0, len);
|
2011-06-14 19:53:33 +00:00
|
|
|
r = pread(fd, buf, len, offset);
|
2013-03-20 15:56:03 +00:00
|
|
|
if (r == -1) {
|
|
|
|
err = errno;
|
|
|
|
printf("read_file: error: pread() failed with: %d (%s)\n", err, strerror(err));
|
|
|
|
goto out;
|
|
|
|
}
|
2011-06-14 19:53:33 +00:00
|
|
|
r = verify_pattern(buf, len, offset);
|
2013-03-20 15:56:03 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
close(fd);
|
2011-06-08 05:09:24 +00:00
|
|
|
free(rawbuf);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-02-12 15:48:15 +00:00
|
|
|
int read_direct(int buf_align, uint64_t offset, int len)
|
|
|
|
{
|
|
|
|
printf("read_direct buf_align %d offset %llu len %d\n", buf_align,
|
|
|
|
(unsigned long long)offset, len);
|
|
|
|
return read_file(buf_align, offset, len, 1);
|
|
|
|
}
|
|
|
|
|
2011-06-08 05:09:24 +00:00
|
|
|
int read_sync(int buf_align, uint64_t offset, int len)
|
|
|
|
{
|
2011-06-14 05:11:51 +00:00
|
|
|
printf("read_sync buf_align %d offset %llu len %d\n", buf_align,
|
|
|
|
(unsigned long long)offset, len);
|
2013-02-12 15:48:15 +00:00
|
|
|
return read_file(buf_align, offset, len, 0);
|
2011-06-08 05:09:24 +00:00
|
|
|
}
|
|
|
|
|
2013-02-12 15:48:15 +00:00
|
|
|
int write_file(int buf_align, uint64_t offset, int len, int direct)
|
2011-06-10 04:29:00 +00:00
|
|
|
{
|
2013-02-12 15:48:15 +00:00
|
|
|
printf("write_file buf_align %d offset %llu len %d\n", buf_align,
|
2011-06-14 05:11:51 +00:00
|
|
|
(unsigned long long)offset, len);
|
2011-06-10 04:29:00 +00:00
|
|
|
void *rawbuf;
|
2011-06-14 19:53:33 +00:00
|
|
|
int r;
|
2013-02-12 15:48:15 +00:00
|
|
|
int err = 0;
|
|
|
|
int flags;
|
|
|
|
if (direct)
|
|
|
|
flags = O_WRONLY|O_DIRECT|O_CREAT;
|
|
|
|
else
|
|
|
|
flags = O_WRONLY|O_CREAT;
|
|
|
|
|
|
|
|
int fd = open("foo", flags, 0644);
|
|
|
|
if (fd < 0) {
|
|
|
|
int err = errno;
|
|
|
|
printf("write_file: error: open() failed with: %d (%s)\n", err, strerror(err));
|
|
|
|
exit(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((r = posix_memalign(&rawbuf, 4096, len + buf_align)) != 0) {
|
|
|
|
printf("write_file: error: posix_memalign failed with %d", r);
|
|
|
|
err = r;
|
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!direct)
|
|
|
|
ioctl(fd, CEPH_IOC_SYNCIO);
|
|
|
|
|
|
|
|
void *buf = (char *)rawbuf + buf_align;
|
2011-06-10 04:29:00 +00:00
|
|
|
|
|
|
|
generate_pattern(buf, len, offset);
|
|
|
|
|
2011-06-14 19:53:33 +00:00
|
|
|
r = pwrite(fd, buf, len, offset);
|
2011-06-10 04:29:00 +00:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
fd = open("foo", O_RDONLY);
|
2013-02-12 15:48:15 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
err = errno;
|
|
|
|
printf("write_file: error: open() failed with: %d (%s)\n", err, strerror(err));
|
|
|
|
free(rawbuf);
|
|
|
|
goto out_unlink;
|
|
|
|
}
|
2011-06-13 23:18:23 +00:00
|
|
|
void *buf2 = malloc(len);
|
2013-02-12 15:48:15 +00:00
|
|
|
if (!buf2) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
printf("write_file: error: malloc failed\n");
|
|
|
|
goto out_free;
|
|
|
|
}
|
|
|
|
|
2011-06-13 23:23:28 +00:00
|
|
|
memset(buf2, 0, len);
|
2011-06-14 19:53:33 +00:00
|
|
|
r = pread(fd, buf2, len, offset);
|
2013-03-20 15:56:03 +00:00
|
|
|
if (r == -1) {
|
|
|
|
err = errno;
|
|
|
|
printf("write_file: error: pread() failed with: %d (%s)\n", err, strerror(err));
|
|
|
|
goto out_free_buf;
|
|
|
|
}
|
2011-06-14 19:53:33 +00:00
|
|
|
r = verify_pattern(buf2, len, offset);
|
2011-06-13 23:18:23 +00:00
|
|
|
|
2013-03-20 15:56:03 +00:00
|
|
|
out_free_buf:
|
2011-06-13 23:18:23 +00:00
|
|
|
free(buf2);
|
2013-02-12 15:48:15 +00:00
|
|
|
out_free:
|
|
|
|
free(rawbuf);
|
|
|
|
out_close:
|
|
|
|
close(fd);
|
|
|
|
out_unlink:
|
|
|
|
unlink("foo");
|
|
|
|
if (err)
|
|
|
|
exit(err);
|
2011-06-10 04:29:00 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-02-12 15:48:15 +00:00
|
|
|
int write_direct(int buf_align, uint64_t offset, int len)
|
|
|
|
{
|
|
|
|
printf("write_direct buf_align %d offset %llu len %d\n", buf_align,
|
|
|
|
(unsigned long long)offset, len);
|
|
|
|
return write_file (buf_align, offset, len, 1);
|
|
|
|
}
|
|
|
|
|
2011-06-10 04:29:00 +00:00
|
|
|
int write_sync(int buf_align, uint64_t offset, int len)
|
|
|
|
{
|
2011-06-14 05:11:51 +00:00
|
|
|
printf("write_sync buf_align %d offset %llu len %d\n", buf_align,
|
|
|
|
(unsigned long long)offset, len);
|
2013-02-12 15:48:15 +00:00
|
|
|
return write_file (buf_align, offset, len, 0);
|
2011-06-10 04:29:00 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 05:09:24 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
uint64_t i, j, k;
|
2011-06-13 23:18:23 +00:00
|
|
|
int read = 1;
|
|
|
|
int write = 1;
|
|
|
|
|
|
|
|
if (argc >= 2 && strcmp(argv[1], "read") == 0)
|
|
|
|
write = 0;
|
|
|
|
if (argc >= 2 && strcmp(argv[1], "write") == 0)
|
|
|
|
read = 0;
|
|
|
|
|
|
|
|
if (read) {
|
|
|
|
write_pattern();
|
|
|
|
|
|
|
|
for (i = 0; i < 4096; i += 512)
|
|
|
|
for (j = 4*1024*1024 - 4096; j < 4*1024*1024 + 4096; j += 512)
|
|
|
|
for (k = 1024; k <= 16384; k *= 2) {
|
|
|
|
read_direct(i, j, k);
|
|
|
|
read_sync(i, j, k);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2011-06-10 04:29:00 +00:00
|
|
|
unlink("foo");
|
2011-06-13 23:18:23 +00:00
|
|
|
if (write) {
|
|
|
|
for (i = 0; i < 4096; i += 512)
|
|
|
|
for (j = 4*1024*1024 - 4096 + 512; j < 4*1024*1024 + 4096; j += 512)
|
|
|
|
for (k = 1024; k <= 16384; k *= 2) {
|
|
|
|
write_direct(i, j, k);
|
|
|
|
write_sync(i, j, k);
|
|
|
|
}
|
|
|
|
}
|
2011-06-10 04:29:00 +00:00
|
|
|
|
|
|
|
|
2011-06-08 05:09:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|