1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
44 #include <sys/types.h>
47 #include <netinet/in.h>
48 #include <sys/socket.h>
53 char *dir = NULL, *dir2 = NULL;
55 char mmap_sanity[256];
58 static void usage(void)
60 printf("Usage: mmap_sanity -d dir [-m dir2]\n");
61 printf(" dir lustre mount point\n");
62 printf(" dir2 another mount point\n");
66 static int remote_tst(int tc, char *mnt);
67 static int mmap_run(int tc)
79 rc = remote_tst(tc, dir2);
82 fprintf(stderr, "invalid argument!\n");
87 static int mmap_initialize(char *myself)
89 char buf[1024], *file;
90 int fdr, fdw, count, rc = 0;
92 page_size = sysconf(_SC_PAGESIZE);
93 if (page_size == -1) {
94 perror("sysconf(_SC_PAGESIZE)");
98 /* copy myself to lustre for another client */
99 fdr = open(myself, O_RDONLY);
104 file = strrchr(myself, '/');
106 fprintf(stderr, "can't get test filename\n");
111 sprintf(mmap_sanity, "%s/%s", dir, file);
113 fdw = open(mmap_sanity, O_CREAT|O_WRONLY, 0777);
119 while ((count = read(fdr, buf, sizeof(buf))) != 0) {
127 writes = write(fdw, buf, count);
128 if (writes != count) {
139 static void mmap_finalize()
144 /* basic mmap operation on single node */
145 static int mmap_tst1(char *mnt)
147 char *ptr, mmap_file[256];
148 int region, fd, rc = 0;
150 region = page_size * 10;
151 sprintf(mmap_file, "%s/%s", mnt, "mmap_file1");
153 if (unlink(mmap_file) && errno != ENOENT) {
158 fd = open(mmap_file, O_CREAT|O_RDWR, 0600);
163 if (ftruncate(fd, region) < 0) {
164 perror("ftruncate()");
169 ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
170 if (ptr == MAP_FAILED) {
175 memset(ptr, 'a', region);
184 /* MAP_PRIVATE create a copy-on-write mmap */
185 static int mmap_tst2(char *mnt)
187 char *ptr, mmap_file[256], buf[256];
190 sprintf(mmap_file, "%s/%s", mnt, "mmap_file2");
192 if (unlink(mmap_file) && errno != ENOENT) {
197 fd = open(mmap_file, O_CREAT|O_RDWR, 0600);
202 if (ftruncate(fd, page_size) < 0) {
203 perror("ftruncate()");
208 ptr = mmap(NULL, page_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
209 if (ptr == MAP_FAILED) {
214 memcpy(ptr, "blah", strlen("blah"));
216 munmap(ptr, page_size);
222 fd = open(mmap_file, O_RDONLY);
227 rc = read(fd, buf, sizeof(buf));
235 if (strncmp("blah", buf, strlen("blah")) == 0) {
236 fprintf(stderr, "mmap write back with MAP_PRIVATE!\n");
244 /* concurrent mmap operations on two nodes */
245 static int mmap_tst3(char *mnt)
247 char *ptr, mmap_file[256];
248 int region, fd, rc = 0;
250 region = page_size * 100;
251 sprintf(mmap_file, "%s/%s", mnt, "mmap_file3");
253 if (unlink(mmap_file) && errno != ENOENT) {
258 fd = open(mmap_file, O_CREAT|O_RDWR, 0600);
263 if (ftruncate(fd, region) < 0) {
264 perror("ftruncate()");
269 ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
270 if (ptr == MAP_FAILED) {
280 memset(ptr, 'a', region);
281 sleep(2); /* wait for remote test finish */
290 static int remote_tst3(char *mnt)
292 char *ptr, mmap_file[256];
293 int region, fd, rc = 0;
295 region = page_size * 100;
296 sprintf(mmap_file, "%s/%s", mnt, "mmap_file3");
298 fd = open(mmap_file, O_RDWR, 0600);
304 ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
305 if (ptr == MAP_FAILED) {
310 memset(ptr, 'b', region);
311 memset(ptr, 'c', region);
319 /* client1 write to file_4a from mmap()ed file_4b;
320 * client2 write to file_4b from mmap()ed file_4a. */
321 static int mmap_tst4(char *mnt)
323 char *ptr, filea[256], fileb[256];
324 int region, fdr, fdw, rc = 0;
326 region = page_size * 100;
327 sprintf(filea, "%s/%s", mnt, "mmap_file_4a");
328 sprintf(fileb, "%s/%s", mnt, "mmap_file_4b");
330 if (unlink(filea) && errno != ENOENT) {
334 if (unlink(fileb) && errno != ENOENT) {
340 fdr = open(fileb, O_CREAT|O_RDWR, 0600);
345 if (ftruncate(fdr, region) < 0) {
346 perror("ftruncate()");
350 fdw = open(filea, O_CREAT|O_RDWR, 0600);
356 if (ftruncate(fdw, region) < 0) {
357 perror("ftruncate()");
362 ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fdr, 0);
363 if (ptr == MAP_FAILED) {
373 memset(ptr, '1', region);
375 rc = write(fdw, ptr, region);
382 sleep(2); /* wait for remote test finish */
395 static int remote_tst4(char *mnt)
397 char *ptr, filea[256], fileb[256];
398 int region, fdr, fdw, rc = 0;
400 region = page_size * 100;
401 sprintf(filea, "%s/%s", mnt, "mmap_file_4a");
402 sprintf(fileb, "%s/%s", mnt, "mmap_file_4b");
405 fdr = open(filea, O_RDWR, 0600);
410 fdw = open(fileb, O_RDWR, 0600);
417 ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fdr, 0);
418 if (ptr == MAP_FAILED) {
424 memset(ptr, '2', region);
426 rc = write(fdw, ptr, region);
442 static int cancel_lru_locks(char *prefix)
444 char cmd[256], line[1024];
447 int len = 1024, rc = 0;
454 rc = waitpid(child, &status, WNOHANG);
461 sprintf(cmd, "ls /proc/fs/lustre/ldlm/namespaces/*-%s-*/lru_size", prefix);
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);
590 ptr = mmap(NULL, page_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
591 if (ptr == MAP_FAILED) {
597 ptr2 = mmap(NULL, page_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd2, 0);
598 if (ptr2 == MAP_FAILED) {
604 rc = cancel_lru_locks("osc");
608 memcpy(ptr, "blah", strlen("blah"));
609 if (strncmp(ptr, ptr2, strlen("blah"))) {
610 fprintf(stderr, "client2 mmap mismatch!\n");
614 memcpy(ptr2, "foo", strlen("foo"));
615 if (strncmp(ptr, ptr2, strlen("foo"))) {
616 fprintf(stderr, "client1 mmap mismatch!\n");
621 munmap(ptr2, page_size);
623 munmap(ptr, page_size);
632 static int mmap_tst7_func(char *mnt, int rw)
635 char *buf = MAP_FAILED;
640 if (snprintf(fname, 256, "%s/mmap_tst7.%s",
641 mnt, (rw == 0) ? "read":"write") >= 256) {
642 fprintf(stderr, "dir name too long\n");
646 fd = open(fname, O_RDWR | O_DIRECT | O_CREAT, 0644);
652 if (ftruncate(fd, 2 * page_size) == -1) {
657 buf = mmap(NULL, page_size * 2,
658 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
659 if (buf == MAP_FAILED) {
664 /* ensure the second page isn't mapped */
665 munmap(buf + page_size, page_size);
666 bytes = (rw == 0) ? read(fd, buf, 2 * page_size) :
667 write(fd, buf, 2 * page_size);
668 /* Expected behavior */
669 if (bytes == page_size)
671 fprintf(stderr, "%s returned %zd, errno = %d\n",
672 (rw == 0)?"read":"write", bytes, errno);
675 if (buf != MAP_FAILED)
676 munmap(buf, page_size);
682 static int mmap_tst7(char *mnt)
686 rc = mmap_tst7_func(mnt, 0);
689 rc = mmap_tst7_func(mnt, 1);
693 static int mmap_tst8(char *mnt)
696 char *buf = MAP_FAILED;
700 char xyz[page_size * 2];
702 if (snprintf(fname, 256, "%s/mmap_tst8", mnt) >= 256) {
703 fprintf(stderr, "dir name too long\n");
707 fd = open(fname, O_RDWR | O_CREAT, 0644);
713 if (ftruncate(fd, page_size) == -1) {
718 buf = mmap(NULL, page_size * 2,
719 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
720 if (buf == MAP_FAILED) {
727 if (pid == 0) { /* child */
728 memcpy(xyz, buf, page_size * 2);
729 /* shouldn't reach here. */
731 } else if (pid > 0) { /* parent */
733 pid = waitpid(pid, &status, 0);
741 if (WIFSIGNALED(status) && SIGBUS == WTERMSIG(status))
749 if (buf != MAP_FAILED)
750 munmap(buf, page_size);
756 static int remote_tst(int tc, char *mnt)
761 rc = remote_tst3(mnt);
764 rc = remote_tst4(mnt);
767 fprintf(stderr, "wrong test case number %d\n", tc);
775 int tc; /* test case number */
776 char *desc; /* test description */
777 int (* test_fn)(char *mnt); /* test function */
778 int node_cnt; /* node count */
781 struct test_case tests[] = {
782 { 1, "mmap test1: basic mmap operation", mmap_tst1, 1 },
783 { 2, "mmap test2: MAP_PRIVATE not write back", mmap_tst2, 1 },
784 { 3, "mmap test3: concurrent mmap ops on two nodes", mmap_tst3, 2 },
785 { 4, "mmap test4: c1 write to f1 from mmapped f2, "
786 "c2 write to f1 from mmapped f1", mmap_tst4, 2 },
787 { 5, "mmap test5: read/write file to/from the buffer "
788 "which mmapped to just this file", mmap_tst5, 1 },
789 { 6, "mmap test6: check mmap write/read content on two nodes",
791 { 7, "mmap test7: file i/o with an unmapped buffer", mmap_tst7, 1},
792 { 8, "mmap test8: SIGBUS for beyond file size", mmap_tst8, 1},
796 int main(int argc, char **argv)
799 struct test_case *test;
803 c = getopt(argc, argv, "d:m:");
824 if (mmap_initialize(argv[0]) != 0) {
825 fprintf(stderr, "mmap_initialize failed!\n");
829 for (test = tests; test->tc; test++) {
832 if (test->node_cnt == 1 || dir2 != NULL) {
833 rc = test->test_fn(dir);
834 rs = rc ? "fail" : "pass";
836 fprintf(stderr, "%s (%s)\n", test->desc, rs);