Added a BUSE loopback device for benchmarking purposes.

This commit is contained in:
Adam Cozzette 2011-12-15 00:39:59 -08:00
parent 5c1ba95802
commit 9a44badd00
3 changed files with 87 additions and 3 deletions

View File

@ -1,4 +1,4 @@
TARGET := busexmp
TARGET := busexmp loopback
LIBOBJS := buse.o
OBJS := $(TARGET:=.o) $(LIBOBJS)
STATIC_LIB := libbuse.a

4
buse.c
View File

@ -132,7 +132,7 @@ int buse_main(int argc, char *argv[], const struct buse_operations *aop, void *u
* and writes.
*/
case NBD_CMD_READ:
fprintf(stderr, "Request for read of size %d\n", len);
/* fprintf(stderr, "Request for read of size %d\n", len); */
chunk = malloc(len + sizeof(struct nbd_reply));
aop->read((char *)chunk + sizeof(struct nbd_reply), len, from);
memcpy(chunk, &reply, sizeof(struct nbd_reply));
@ -140,7 +140,7 @@ int buse_main(int argc, char *argv[], const struct buse_operations *aop, void *u
free(chunk);
break;
case NBD_CMD_WRITE:
fprintf(stderr, "Request for write of size %d\n", len);
/* fprintf(stderr, "Request for write of size %d\n", len); */
chunk = malloc(len);
read_all(sk, chunk, len);
aop->write(chunk, len, from);

84
loopback.c Normal file
View File

@ -0,0 +1,84 @@
#define _GNU_SOURCE
#define _LARGEFILE64_SOURCE
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "buse.h"
static int fd;
static void usage(void)
{
fprintf(stderr, "Usage: loopback <phyical device> <virtual device>\n");
}
static int loopback_read(void *buf, u_int32_t len, u_int64_t offset)
{
int bytes_read;
lseek64(fd, offset, SEEK_SET);
while (len > 0) {
bytes_read = read(fd, buf, len);
assert(bytes_read > 0);
len -= bytes_read;
buf = (char *) buf + bytes_read;
}
return 0;
}
static int loopback_write(const void *buf, u_int32_t len, u_int64_t offset)
{
int bytes_written;
lseek64(fd, offset, SEEK_SET);
while (len > 0) {
bytes_written = write(fd, buf, len);
assert(bytes_written > 0);
len -= bytes_written;
buf = (char *) buf + bytes_written;
}
return 0;
}
static struct buse_operations bop = {
.read = loopback_read,
.write = loopback_write
};
int main(int argc, char *argv[])
{
struct stat buf;
int err;
int64_t size;
if (argc != 3) {
usage();
return -1;
}
fd = open(argv[1], O_RDWR|O_LARGEFILE);
assert(fd != -1);
/* Let's verify that this file is actually a block device. We could support
* regular files, too, but we don't need to right now. */
fstat(fd, &buf);
assert(S_ISBLK(buf.st_mode));
/* Figure out the size of the underlying block device. */
err = ioctl(fd, BLKGETSIZE64, &size);
assert(err != -1);
fprintf(stderr, "The size of this device is %ld bytes.\n", size);
bop.size = size;
buse_main(argc, argv, &bop, NULL);
return 0;
}