⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mmap_sanity.c

📁 lustre 1.6.5 source code
💻 C
📖 第 1 页 / 共 2 页
字号:
/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*- * vim:expandtab:shiftwidth=8:tabstop=8: */#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <sys/mman.h>#include <errno.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <netinet/in.h>#include <sys/socket.h>#include <netdb.h>#include <string.h>#include <sys/wait.h>char *dir = NULL, *dir2 = NULL;long page_size;char mmap_sanity[256];static void usage(void){        printf("Usage: mmap_sanity -d dir [-m dir2]\n");        printf("       dir      lustre mount point\n");        printf("       dir2     another mount point\n");        exit(127);}static int remote_tst(int tc, char *mnt);static int mmap_run(int tc){        pid_t child;        int rc = 0;        child = fork();        if (child < 0)                return errno;        else if (child)                return 0;        if (dir2 != NULL) {                rc = remote_tst(tc, dir2);        } else {                rc = EINVAL;                fprintf(stderr, "invalid argument!\n");        }        _exit(rc);}static int mmap_initialize(char *myself){        char buf[1024], *file;        int fdr, fdw, count, rc = 0;                page_size = sysconf(_SC_PAGESIZE);        if (page_size == -1) {                perror("sysconf(_SC_PAGESIZE)");                return errno;        }        /* copy myself to lustre for another client */        fdr = open(myself, O_RDONLY);        if (fdr < 0) {                perror(myself);                return EINVAL;        }        file = strrchr(myself, '/');        if (file == NULL) {                fprintf(stderr, "can't get test filename\n");                close(fdr);                return EINVAL;        }        file++;        sprintf(mmap_sanity, "%s/%s", dir, file);        fdw = open(mmap_sanity, O_CREAT|O_WRONLY, 0777);        if (fdw < 0) {                perror(mmap_sanity);                close(fdr);                return EINVAL;        }        while ((count = read(fdr, buf, sizeof(buf))) != 0) {                int writes;                if (count < 0) {                        perror("read()");                        rc = errno;                        break;                }                writes = write(fdw, buf, count);                if (writes != count) {                        perror("write()");                        rc = errno;                        break;                }        }        close(fdr);        close(fdw);        return rc;}static void mmap_finalize(){        unlink(mmap_sanity);}/* basic mmap operation on single node */static int mmap_tst1(char *mnt){        char *ptr, mmap_file[256];        int region, fd, rc = 0;        region = page_size * 10;        sprintf(mmap_file, "%s/%s", mnt, "mmap_file1");                if (unlink(mmap_file) && errno != ENOENT) {                perror("unlink()");                return errno;        }        fd = open(mmap_file, O_CREAT|O_RDWR, 0600);        if (fd < 0) {                perror(mmap_file);                return errno;        }        ftruncate(fd, region);        ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);        if (ptr == MAP_FAILED) {                perror("mmap()");                rc = errno;                goto out_close;        }        memset(ptr, 'a', region);        munmap(ptr, region);out_close:        close(fd);        unlink(mmap_file);        return rc;}/* MAP_PRIVATE create a copy-on-write mmap */static int mmap_tst2(char *mnt){        char *ptr, mmap_file[256], buf[256];        int fd, rc = 0;        sprintf(mmap_file, "%s/%s", mnt, "mmap_file2");        if (unlink(mmap_file) && errno != ENOENT) {                perror("unlink()");                return errno;        }        fd = open(mmap_file, O_CREAT|O_RDWR, 0600);        if (fd < 0) {                perror(mmap_file);                return errno;        }        ftruncate(fd, page_size);        ptr = mmap(NULL, page_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);        if (ptr == MAP_FAILED) {                perror("mmap()");                rc = errno;                goto out_close;        }        memcpy(ptr, "blah", strlen("blah"));        munmap(ptr, page_size);out_close:        close(fd);        if (rc)                return rc;        fd = open(mmap_file, O_RDONLY);        if (fd < 0) {                perror(mmap_file);                return errno;        }        rc = read(fd, buf, sizeof(buf));        if (rc < 0) {                perror("read()");                rc = errno;                goto out_close;        }        rc = 0;                if (strncmp("blah", buf, strlen("blah")) == 0) {                fprintf(stderr, "mmap write back with MAP_PRIVATE!\n");                rc = EFAULT;        }        close(fd);        unlink(mmap_file);        return rc;}/* concurrent mmap operations on two nodes */static int mmap_tst3(char *mnt){        char *ptr, mmap_file[256];        int region, fd, rc = 0;        region = page_size * 100;        sprintf(mmap_file, "%s/%s", mnt, "mmap_file3");                if (unlink(mmap_file) && errno != ENOENT) {                perror("unlink()");                return errno;        }        fd = open(mmap_file, O_CREAT|O_RDWR, 0600);        if (fd < 0) {                perror(mmap_file);                return errno;        }        ftruncate(fd, region);        ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);        if (ptr == MAP_FAILED) {                perror("mmap()");                rc = errno;                goto out_close;        }        rc = mmap_run(3);        if (rc)                goto out_unmap;                memset(ptr, 'a', region);        sleep(2);       /* wait for remote test finish */out_unmap:        munmap(ptr, region);out_close:        close(fd);        unlink(mmap_file);        return rc;}       static int remote_tst3(char *mnt){        char *ptr, mmap_file[256];        int region, fd, rc = 0;        region = page_size * 100;        sprintf(mmap_file, "%s/%s", mnt, "mmap_file3");        fd = open(mmap_file, O_RDWR, 0600);        if (fd < 0) {                perror(mmap_file);                return errno;        }        ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);        if (ptr == MAP_FAILED) {                perror("mmap()");                rc = errno;                goto out_close;        }        memset(ptr, 'b', region);        memset(ptr, 'c', region);                munmap(ptr, region);out_close:        close(fd);        return rc;}/* client1 write to file_4a from mmap()ed file_4b; * client2 write to file_4b from mmap()ed file_4a. */static int mmap_tst4(char *mnt){        char *ptr, filea[256], fileb[256];        int region, fdr, fdw, rc = 0;        region = page_size * 100;        sprintf(filea, "%s/%s", mnt, "mmap_file_4a");        sprintf(fileb, "%s/%s", mnt, "mmap_file_4b");        if (unlink(filea) && errno != ENOENT) {                perror("unlink()");                return errno;        }        if (unlink(fileb) && errno != ENOENT) {                perror("unlink()");                return errno;        }        fdr = fdw = -1;        fdr = open(fileb, O_CREAT|O_RDWR, 0600);        if (fdr < 0) {                perror(fileb);                return errno;        }        ftruncate(fdr, region);        fdw = open(filea, O_CREAT|O_RDWR, 0600);        if (fdw < 0) {                perror(filea);                rc = errno;                goto out_close;        }        ftruncate(fdw, region);                ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fdr, 0);        if (ptr == MAP_FAILED) {                perror("mmap()");                rc = errno;                goto out_close;        }        rc = mmap_run(4);        if (rc)                goto out_unmap;                memset(ptr, '1', region);                rc = write(fdw, ptr, region);        if (rc <= 0) {                perror("write()");                rc = errno;        } else                rc = 0;        sleep(2);       /* wait for remote test finish */out_unmap:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -