diff --git a/Makefile b/Makefile index 2b50929..802ca05 100644 --- a/Makefile +++ b/Makefile @@ -28,23 +28,22 @@ all: tests benchmarks .PHONY: tests benchmarks libs -tests benchmarks: bin/run-tests lib/slowfs/slowfs +tests benchmarks: bin/run-tests libs $(MAKE) -C $@ all bin/run-tests: lib/talloc/talloc.o bin/run-tests.o bin/capabilities.o bin/test.o $(LINK.o) -o $@ $^ -libs: lib/talloc/talloc.o +libs: lib/talloc/talloc.o lib/slowfs/slowfs.o -lib/slowfs/slowfs: CFLAGS += $(shell pkg-config fuse --cflags) -lib/slowfs/slowfs: LDFLAGS += $(shell pkg-config fuse --libs) +lib/slowfs/slowfs.o: CFLAGS += $(shell pkg-config fuse --cflags) clean: cd tests && make clean cd benchmarks && make clean rm -f bin/capabilities bin/run-tests - rm -f bin/*.o lib/talloc/talloc.o + rm -f bin/*.o lib/talloc/talloc.o lib/slowfs/slowfs.o check: clean all bin/run-tests diff --git a/lib/slowfs/slowfs.c b/lib/slowfs/slowfs.c index 9979988..08a3a58 100644 --- a/lib/slowfs/slowfs.c +++ b/lib/slowfs/slowfs.c @@ -3,13 +3,29 @@ #include #include +#include #include #include + +#include + +#include + #include +#include + #define FILE_SIZE 4096 static const char *file_name = "/slowfs"; +struct slowfs { + struct fuse *fuse; + struct fuse_chan *chan; + char *mountpoint; + int fuse_rc; + pthread_t pthread; +}; + static int slowfs_getattr(const char *path, struct stat *statbuf) { int rc = -ENOENT; @@ -74,7 +90,67 @@ static struct fuse_operations ops = { .read = slowfs_read, }; -int main(int argc, char *argv[]) +static void *slowfs_fn(void *arg) +{ + struct slowfs *slowfs = arg; + + slowfs->fuse_rc = fuse_loop(slowfs->fuse); + + return slowfs; +} + +struct slowfs *slowfs_mount(const char *mountpoint) +{ + struct slowfs *slowfs; + + slowfs = talloc(NULL, struct slowfs); + if (!slowfs) { + perror("talloc"); + goto out_free; + } + + slowfs->mountpoint = talloc_strdup(slowfs, mountpoint); + if (!slowfs->mountpoint) { + perror("talloc"); + goto out_free; + } + + slowfs->chan = fuse_mount(mountpoint, NULL); + if (!slowfs->chan) { + perror("fuse_mount"); + goto out_free; + } + + slowfs->fuse = fuse_new(slowfs->chan, NULL, &ops, sizeof(ops), NULL); + if (!slowfs->fuse) { + perror("fuse_new"); + goto out_free; + } + + if (fuse_daemonize(1)) { + perror("fuse_daemonize"); + goto out_free; + } + + if (pthread_create(&slowfs->pthread, NULL, slowfs_fn, slowfs)) { + perror("pthread_create"); + goto out_free; + } + + /* fuse_set_signal_handlers() ? */ + + return slowfs; + +out_free: + talloc_free(slowfs); + return NULL; +} + +void slowfs_unmount(struct slowfs *slowfs) { - return fuse_main(argc, argv, &ops, NULL); + fuse_exit(slowfs->fuse); + fuse_unmount(slowfs->mountpoint, slowfs->chan); + pthread_join(slowfs->pthread, NULL); + talloc_free(slowfs); } + diff --git a/tests/14-dma/05-schedule-during-fault.c b/tests/14-dma/05-schedule-during-fault.c index e494355..7b2d369 100644 --- a/tests/14-dma/05-schedule-during-fault.c +++ b/tests/14-dma/05-schedule-during-fault.c @@ -38,6 +38,8 @@ #include #include +#include + #define SLOWFS_BINARY "lib/slowfs/slowfs" #define SLOWFS_DIR "mnt" #define SLOWFS_FILE "mnt/slowfs" @@ -118,25 +120,11 @@ static uint32_t spe_program[] = { 0x00001337, /* stop */ }; - -static void unmount_slowfs(void) -{ - int pid; - - pid = fork(); - if (!pid) { - execlp("fusermount", "fusermount", "-u", SLOWFS_DIR, NULL); - exit(EXIT_FAILURE); - } - - waitpid(pid, NULL, 0); - - rmdir(SLOWFS_DIR); -} +static struct slowfs *slowfs; static int map_slowfs_file(void **map, int *len) { - int rc, fd, status; + int rc, fd; struct stat statbuf; rc = mkdir(SLOWFS_DIR, 0755); @@ -145,23 +133,10 @@ static int map_slowfs_file(void **map, int *len) return rc; } - rc = fork(); - - if (rc < 0) { - perror("fork"); - goto err_umount; - } - - if (rc == 0) { - execl(SLOWFS_BINARY, SLOWFS_BINARY, SLOWFS_DIR, NULL); - exit(EXIT_FAILURE); - } - - waitpid(rc, &status, 0); - - if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { - fprintf(stderr, "failed mounting slowfs\n"); - goto err_umount; + slowfs = slowfs_mount(SLOWFS_DIR); + if (!slowfs) { + fprintf(stderr, "failed to mount slowfs on %s\n", SLOWFS_DIR); + return -1; } fd = open(SLOWFS_FILE, O_RDONLY); @@ -193,17 +168,18 @@ static int map_slowfs_file(void **map, int *len) err_close: close(fd); err_umount: - unmount_slowfs(); + slowfs_unmount(slowfs); return rc; } static void unmap_slow_file(void *map, int len) { - if (len) + if (map != MAP_FAILED) munmap(map, len); - unmount_slowfs(); + slowfs_unmount(slowfs); + rmdir(SLOWFS_DIR); } struct spe_thread_info { @@ -280,7 +256,6 @@ int main(void) rc = 0; } - out_unmap: unmap_slow_file(slow_map, slow_map_len); return rc; diff --git a/tests/14-dma/Makefile b/tests/14-dma/Makefile index 94217ac..eab069f 100644 --- a/tests/14-dma/Makefile +++ b/tests/14-dma/Makefile @@ -21,6 +21,8 @@ obj += 01-spe-get 02-spe-put 03-hugepage-spe-faulted 04-hugepage-ppe-faulted \ 05-schedule-during-fault -05-schedule-during-fault: LDFLAGS += -lpthread +05-schedule-during-fault: LDFLAGS += -lpthread -lfuse +05-schedule-during-fault: 05-schedule-during-fault.o ../../lib/slowfs/slowfs.o \ + ../../lib/talloc/talloc.o $(obj): pattern.o