4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
42 #include <sys/types.h>
45 #include <netinet/in.h>
46 #include <sys/socket.h>
52 char *dir = NULL, *dir2 = NULL;
54 char mmap_sanity[256];
56 static void usage(void)
58 printf("Usage: mmap_sanity -d dir [-m dir2]\n");
59 printf(" dir lustre mount point\n");
60 printf(" dir2 another mount point\n");
64 static int remote_tst(int tc, char *mnt);
65 static int mmap_run(int tc)
77 rc = remote_tst(tc, dir2);
80 fprintf(stderr, "invalid argument!\n");
85 static int mmap_initialize(char *myself)
87 char buf[1024], *file;
88 int fdr, fdw, count, rc = 0;
90 page_size = sysconf(_SC_PAGESIZE);
91 if (page_size == -1) {
92 perror("sysconf(_SC_PAGESIZE)");
96 /* copy myself to lustre for another client */
97 fdr = open(myself, O_RDONLY);
102 file = strrchr(myself, '/');
104 fprintf(stderr, "can't get test filename\n");
109 sprintf(mmap_sanity, "%s/%s", dir, file);
111 fdw = open(mmap_sanity, O_CREAT|O_WRONLY, 0777);
117 while ((count = read(fdr, buf, sizeof(buf))) != 0) {
125 writes = write(fdw, buf, count);
126 if (writes != count) {
137 static void mmap_finalize()
142 /* basic mmap operation on single node */
143 static int mmap_tst1(char *mnt)
145 char *ptr, mmap_file[256];
146 int region, fd, rc = 0;
148 region = page_size * 10;
149 sprintf(mmap_file, "%s/%s", mnt, "mmap_file1");
151 if (unlink(mmap_file) && errno != ENOENT) {
156 fd = open(mmap_file, O_CREAT|O_RDWR, 0600);
161 if (ftruncate(fd, region) < 0) {
162 perror("ftruncate()");
167 ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
168 if (ptr == MAP_FAILED) {
173 memset(ptr, 'a', region);
182 /* MAP_PRIVATE create a copy-on-write mmap */
183 static int mmap_tst2(char *mnt)
185 char *ptr, mmap_file[256], buf[256];
188 sprintf(mmap_file, "%s/%s", mnt, "mmap_file2");
190 if (unlink(mmap_file) && errno != ENOENT) {
195 fd = open(mmap_file, O_CREAT|O_RDWR, 0600);
200 if (ftruncate(fd, page_size) < 0) {
201 perror("ftruncate()");
206 ptr = mmap(NULL, page_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
207 if (ptr == MAP_FAILED) {
212 memcpy(ptr, "blah", strlen("blah"));
214 munmap(ptr, page_size);
220 fd = open(mmap_file, O_RDONLY);
225 rc = read(fd, buf, sizeof(buf));
233 if (strncmp("blah", buf, strlen("blah")) == 0) {
234 fprintf(stderr, "mmap write back with MAP_PRIVATE!\n");
242 /* concurrent mmap operations on two nodes */
243 static int mmap_tst3(char *mnt)
245 char *ptr, mmap_file[256];
246 int region, fd, rc = 0;
248 region = page_size * 100;
249 sprintf(mmap_file, "%s/%s", mnt, "mmap_file3");
251 if (unlink(mmap_file) && errno != ENOENT) {
256 fd = open(mmap_file, O_CREAT|O_RDWR, 0600);
261 if (ftruncate(fd, region) < 0) {
262 perror("ftruncate()");
267 ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
268 if (ptr == MAP_FAILED) {
278 memset(ptr, 'a', region);
279 sleep(2); /* wait for remote test finish */
288 static int remote_tst3(char *mnt)
290 char *ptr, mmap_file[256];
291 int region, fd, rc = 0;
293 region = page_size * 100;
294 sprintf(mmap_file, "%s/%s", mnt, "mmap_file3");
296 fd = open(mmap_file, O_RDWR, 0600);
302 ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
303 if (ptr == MAP_FAILED) {
308 memset(ptr, 'b', region);
309 memset(ptr, 'c', region);
317 /* client1 write to file_4a from mmap()ed file_4b;
318 * client2 write to file_4b from mmap()ed file_4a. */
319 static int mmap_tst4(char *mnt)
321 char *ptr, filea[256], fileb[256];
322 int region, fdr, fdw, rc = 0;
324 region = page_size * 100;
325 sprintf(filea, "%s/%s", mnt, "mmap_file_4a");
326 sprintf(fileb, "%s/%s", mnt, "mmap_file_4b");
328 if (unlink(filea) && errno != ENOENT) {
332 if (unlink(fileb) && errno != ENOENT) {
338 fdr = open(fileb, O_CREAT|O_RDWR, 0600);
343 if (ftruncate(fdr, region) < 0) {
344 perror("ftruncate()");
348 fdw = open(filea, O_CREAT|O_RDWR, 0600);
354 if (ftruncate(fdw, region) < 0) {
355 perror("ftruncate()");
360 ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fdr, 0);
361 if (ptr == MAP_FAILED) {
371 memset(ptr, '1', region);
373 rc = write(fdw, ptr, region);
380 sleep(2); /* wait for remote test finish */
393 static int remote_tst4(char *mnt)
395 char *ptr, filea[256], fileb[256];
396 int region, fdr, fdw, rc = 0;
398 region = page_size * 100;
399 sprintf(filea, "%s/%s", mnt, "mmap_file_4a");
400 sprintf(fileb, "%s/%s", mnt, "mmap_file_4b");
403 fdr = open(filea, O_RDWR, 0600);
408 fdw = open(fileb, O_RDWR, 0600);
415 ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fdr, 0);
416 if (ptr == MAP_FAILED) {
422 memset(ptr, '2', region);
424 rc = write(fdw, ptr, region);
440 static int cancel_lru_locks(char *prefix)
442 char cmd[256], line[1024];
445 int len = 1024, rc = 0;
452 rc = waitpid(child, &status, WNOHANG);
460 "ls /proc/fs/lustre/ldlm/namespaces/*-%s-*/lru_size",
463 sprintf(cmd, "ls /proc/fs/lustre/ldlm/namespaces/*/lru_size");
465 file = popen(cmd, "r");
471 while (fgets(line, len, file)) {
476 /* trim newline character */
477 *(line + strlen(line) - 1) = '\0';
478 f = fopen(line, "w");
484 rc = fwrite("clear", strlen("clear") + 1, 1, f);
498 /* don't dead lock while read/write file to/from the buffer which
499 * mmaped to just this file */
500 static int mmap_tst5(char *mnt)
502 char *ptr, mmap_file[256];
503 int region, fd, off, rc = 0;
505 region = page_size * 40;
506 off = page_size * 10;
507 sprintf(mmap_file, "%s/%s", mnt, "mmap_file5");
509 if (unlink(mmap_file) && errno != ENOENT) {
514 fd = open(mmap_file, O_CREAT|O_RDWR, 0600);
519 if (ftruncate(fd, region) < 0) {
520 perror("ftruncate()");
525 ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
526 if (ptr == MAP_FAILED) {
531 memset(ptr, 'a', region);
533 /* cancel unused locks */
534 rc = cancel_lru_locks("osc");
538 /* read/write region of file and buffer should be overlap */
539 rc = read(fd, ptr + off, off * 2);
545 rc = write(fd, ptr + off, off * 2);
559 /* mmap write to a file form client1 then mmap read from client2 */
560 static int mmap_tst6(char *mnt)
562 char mmap_file[256], mmap_file2[256];
563 char *ptr = NULL, *ptr2 = NULL;
564 int fd = 0, fd2 = 0, rc = 0;
566 sprintf(mmap_file, "%s/%s", mnt, "mmap_file6");
567 sprintf(mmap_file2, "%s/%s", dir2, "mmap_file6");
568 if (unlink(mmap_file) && errno != ENOENT) {
573 fd = open(mmap_file, O_CREAT|O_RDWR, 0600);
578 if (ftruncate(fd, page_size) < 0) {
579 perror("ftruncate()");
584 fd2 = open(mmap_file2, O_RDWR, 0600);
591 ptr = mmap(NULL, page_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
592 if (ptr == MAP_FAILED) {
598 ptr2 = mmap(NULL, page_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd2, 0);
599 if (ptr2 == MAP_FAILED) {
605 rc = cancel_lru_locks("osc");
609 memcpy(ptr, "blah", strlen("blah"));
610 if (strncmp(ptr, ptr2, strlen("blah"))) {
611 fprintf(stderr, "client2 mmap mismatch!\n");
615 memcpy(ptr2, "foo", strlen("foo"));
616 if (strncmp(ptr, ptr2, strlen("foo"))) {
617 fprintf(stderr, "client1 mmap mismatch!\n");
622 munmap(ptr2, page_size);
624 munmap(ptr, page_size);
633 static int mmap_tst7_func(char *mnt, int rw)
636 char *buf = MAP_FAILED;
641 if (snprintf(fname, 256, "%s/mmap_tst7.%s", mnt,
642 (rw == 0) ? "read" : "write") >= 256) {
643 fprintf(stderr, "dir name too long\n");
647 fd = open(fname, O_RDWR | O_DIRECT | O_CREAT, 0644);
653 if (ftruncate(fd, 2 * page_size) == -1) {
658 buf = mmap(NULL, page_size * 2,
659 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
660 if (buf == MAP_FAILED) {
665 /* ensure the second page isn't mapped */
666 munmap(buf + page_size, page_size);
667 bytes = (rw == 0) ? read(fd, buf, 2 * page_size) :
668 write(fd, buf, 2 * page_size);
669 /* Expected behavior */
670 if (bytes == page_size)
673 fprintf(stderr, "%s returned %zd, errno = %d\n",
674 (rw == 0) ? "read" : "write", bytes, errno);
677 if (buf != MAP_FAILED)
678 munmap(buf, page_size);
684 static int mmap_tst7(char *mnt)
688 rc = mmap_tst7_func(mnt, 0);
691 rc = mmap_tst7_func(mnt, 1);
695 static int mmap_tst8(char *mnt)
698 char *buf = MAP_FAILED;
702 char xyz[page_size * 2];
704 if (snprintf(fname, 256, "%s/mmap_tst8", mnt) >= 256) {
705 fprintf(stderr, "dir name too long\n");
709 fd = open(fname, O_RDWR | O_CREAT, 0644);
715 if (ftruncate(fd, page_size) == -1) {
720 buf = mmap(NULL, page_size * 2,
721 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
722 if (buf == MAP_FAILED) {
729 if (pid == 0) { /* child */
730 memcpy(xyz, buf, page_size * 2);
731 /* shouldn't reach here. */
733 } else if (pid > 0) { /* parent */
735 pid = waitpid(pid, &status, 0);
743 if (WIFSIGNALED(status) && SIGBUS == WTERMSIG(status))
751 if (buf != MAP_FAILED)
752 munmap(buf, page_size);
758 static int remote_tst(int tc, char *mnt)
763 rc = remote_tst3(mnt);
766 rc = remote_tst4(mnt);
769 fprintf(stderr, "wrong test case number %d\n", tc);
777 int tc; /* test case number */
778 char *desc; /* test description */
779 int (*test_fn)(char *mnt); /* test function */
780 int node_cnt; /* node count */
783 struct test_case tests[] = {
784 { 1, "mmap test1: basic mmap operation", mmap_tst1, 1 },
785 { 2, "mmap test2: MAP_PRIVATE not write back", mmap_tst2, 1 },
786 { 3, "mmap test3: concurrent mmap ops on two nodes", mmap_tst3, 2 },
787 { 4, "mmap test4: c1 write to f1 from mmapped f2, "
788 "c2 write to f1 from mmapped f1", mmap_tst4, 2 },
789 { 5, "mmap test5: read/write file to/from the buffer "
790 "which mmapped to just this file", mmap_tst5, 1 },
791 { 6, "mmap test6: check mmap write/read content on two nodes",
793 { 7, "mmap test7: file i/o with an unmapped buffer", mmap_tst7, 1},
794 { 8, "mmap test8: SIGBUS for beyond file size", mmap_tst8, 1},
798 int main(int argc, char **argv)
800 struct test_case *test;
803 while ((c = getopt(argc, argv, "d:m:")) != -1) {
820 if (mmap_initialize(argv[0]) != 0) {
821 fprintf(stderr, "mmap_initialize failed!\n");
825 for (test = tests; test->tc; test++) {
829 if (test->node_cnt == 1 || dir2 != NULL) {
830 struct timeval start, end;
832 gettimeofday(&start, NULL);
833 rc = test->test_fn(dir);
834 gettimeofday(&end, NULL);
836 duration = (double)(end.tv_sec - start.tv_sec) +
837 (double)(end.tv_usec - start.tv_usec) / 1000000;
838 rs = rc ? "FAIL" : "PASS";
843 fprintf(stderr, "%s (%s, %.5gs)\n", test->desc, rs, duration);