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>
54 char *dir = NULL, *dir2 = NULL;
56 char mmap_sanity[256];
59 static void usage(void)
61 printf("Usage: mmap_sanity -d dir [-m dir2]\n");
62 printf(" dir lustre mount point\n");
63 printf(" dir2 another mount point\n");
67 static int remote_tst(int tc, char *mnt);
68 static int mmap_run(int tc)
80 rc = remote_tst(tc, dir2);
83 fprintf(stderr, "invalid argument!\n");
88 static int mmap_initialize(char *myself)
90 char buf[1024], *file;
91 int fdr, fdw, count, rc = 0;
93 page_size = sysconf(_SC_PAGESIZE);
94 if (page_size == -1) {
95 perror("sysconf(_SC_PAGESIZE)");
99 /* copy myself to lustre for another client */
100 fdr = open(myself, O_RDONLY);
105 file = strrchr(myself, '/');
107 fprintf(stderr, "can't get test filename\n");
112 sprintf(mmap_sanity, "%s/%s", dir, file);
114 fdw = open(mmap_sanity, O_CREAT|O_WRONLY, 0777);
120 while ((count = read(fdr, buf, sizeof(buf))) != 0) {
128 writes = write(fdw, buf, count);
129 if (writes != count) {
140 static void mmap_finalize()
145 /* basic mmap operation on single node */
146 static int mmap_tst1(char *mnt)
148 char *ptr, mmap_file[256];
149 int region, fd, rc = 0;
151 region = page_size * 10;
152 sprintf(mmap_file, "%s/%s", mnt, "mmap_file1");
154 if (unlink(mmap_file) && errno != ENOENT) {
159 fd = open(mmap_file, O_CREAT|O_RDWR, 0600);
164 if (ftruncate(fd, region) < 0) {
165 perror("ftruncate()");
170 ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
171 if (ptr == MAP_FAILED) {
176 memset(ptr, 'a', region);
185 /* MAP_PRIVATE create a copy-on-write mmap */
186 static int mmap_tst2(char *mnt)
188 char *ptr, mmap_file[256], buf[256];
191 sprintf(mmap_file, "%s/%s", mnt, "mmap_file2");
193 if (unlink(mmap_file) && errno != ENOENT) {
198 fd = open(mmap_file, O_CREAT|O_RDWR, 0600);
203 if (ftruncate(fd, page_size) < 0) {
204 perror("ftruncate()");
209 ptr = mmap(NULL, page_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
210 if (ptr == MAP_FAILED) {
215 memcpy(ptr, "blah", strlen("blah"));
217 munmap(ptr, page_size);
223 fd = open(mmap_file, O_RDONLY);
228 rc = read(fd, buf, sizeof(buf));
236 if (strncmp("blah", buf, strlen("blah")) == 0) {
237 fprintf(stderr, "mmap write back with MAP_PRIVATE!\n");
245 /* concurrent mmap operations on two nodes */
246 static int mmap_tst3(char *mnt)
248 char *ptr, mmap_file[256];
249 int region, fd, rc = 0;
251 region = page_size * 100;
252 sprintf(mmap_file, "%s/%s", mnt, "mmap_file3");
254 if (unlink(mmap_file) && errno != ENOENT) {
259 fd = open(mmap_file, O_CREAT|O_RDWR, 0600);
264 if (ftruncate(fd, region) < 0) {
265 perror("ftruncate()");
270 ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
271 if (ptr == MAP_FAILED) {
281 memset(ptr, 'a', region);
282 sleep(2); /* wait for remote test finish */
291 static int remote_tst3(char *mnt)
293 char *ptr, mmap_file[256];
294 int region, fd, rc = 0;
296 region = page_size * 100;
297 sprintf(mmap_file, "%s/%s", mnt, "mmap_file3");
299 fd = open(mmap_file, O_RDWR, 0600);
305 ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
306 if (ptr == MAP_FAILED) {
311 memset(ptr, 'b', region);
312 memset(ptr, 'c', region);
320 /* client1 write to file_4a from mmap()ed file_4b;
321 * client2 write to file_4b from mmap()ed file_4a. */
322 static int mmap_tst4(char *mnt)
324 char *ptr, filea[256], fileb[256];
325 int region, fdr, fdw, rc = 0;
327 region = page_size * 100;
328 sprintf(filea, "%s/%s", mnt, "mmap_file_4a");
329 sprintf(fileb, "%s/%s", mnt, "mmap_file_4b");
331 if (unlink(filea) && errno != ENOENT) {
335 if (unlink(fileb) && errno != ENOENT) {
341 fdr = open(fileb, O_CREAT|O_RDWR, 0600);
346 if (ftruncate(fdr, region) < 0) {
347 perror("ftruncate()");
351 fdw = open(filea, O_CREAT|O_RDWR, 0600);
357 if (ftruncate(fdw, region) < 0) {
358 perror("ftruncate()");
363 ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fdr, 0);
364 if (ptr == MAP_FAILED) {
374 memset(ptr, '1', region);
376 rc = write(fdw, ptr, region);
383 sleep(2); /* wait for remote test finish */
396 static int remote_tst4(char *mnt)
398 char *ptr, filea[256], fileb[256];
399 int region, fdr, fdw, rc = 0;
401 region = page_size * 100;
402 sprintf(filea, "%s/%s", mnt, "mmap_file_4a");
403 sprintf(fileb, "%s/%s", mnt, "mmap_file_4b");
406 fdr = open(filea, O_RDWR, 0600);
411 fdw = open(fileb, O_RDWR, 0600);
418 ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fdr, 0);
419 if (ptr == MAP_FAILED) {
425 memset(ptr, '2', region);
427 rc = write(fdw, ptr, region);
443 static int cancel_lru_locks(char *prefix)
445 char cmd[256], line[1024];
448 int len = 1024, rc = 0;
455 rc = waitpid(child, &status, WNOHANG);
462 sprintf(cmd, "ls /proc/fs/lustre/ldlm/namespaces/*-%s-*/lru_size", prefix);
464 sprintf(cmd, "ls /proc/fs/lustre/ldlm/namespaces/*/lru_size");
466 file = popen(cmd, "r");
472 while (fgets(line, len, file)) {
477 /* trim newline character */
478 *(line + strlen(line) - 1) = '\0';
479 f = fopen(line, "w");
485 rc = fwrite("clear", strlen("clear") + 1, 1, f);
499 /* don't dead lock while read/write file to/from the buffer which
500 * mmaped to just this file */
501 static int mmap_tst5(char *mnt)
503 char *ptr, mmap_file[256];
504 int region, fd, off, rc = 0;
506 region = page_size * 40;
507 off = page_size * 10;
508 sprintf(mmap_file, "%s/%s", mnt, "mmap_file5");
510 if (unlink(mmap_file) && errno != ENOENT) {
515 fd = open(mmap_file, O_CREAT|O_RDWR, 0600);
520 if (ftruncate(fd, region) < 0) {
521 perror("ftruncate()");
526 ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
527 if (ptr == MAP_FAILED) {
532 memset(ptr, 'a', region);
534 /* cancel unused locks */
535 rc = cancel_lru_locks("osc");
539 /* read/write region of file and buffer should be overlap */
540 rc = read(fd, ptr + off, off * 2);
546 rc = write(fd, ptr + off, off * 2);
560 /* mmap write to a file form client1 then mmap read from client2 */
561 static int mmap_tst6(char *mnt)
563 char mmap_file[256], mmap_file2[256];
564 char *ptr = NULL, *ptr2 = NULL;
565 int fd = 0, fd2 = 0, rc = 0;
567 sprintf(mmap_file, "%s/%s", mnt, "mmap_file6");
568 sprintf(mmap_file2, "%s/%s", dir2, "mmap_file6");
569 if (unlink(mmap_file) && errno != ENOENT) {
574 fd = open(mmap_file, O_CREAT|O_RDWR, 0600);
579 if (ftruncate(fd, page_size) < 0) {
580 perror("ftruncate()");
585 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",
642 mnt, (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)
672 fprintf(stderr, "%s returned %zd, errno = %d\n",
673 (rw == 0)?"read":"write", bytes, errno);
676 if (buf != MAP_FAILED)
677 munmap(buf, page_size);
683 static int mmap_tst7(char *mnt)
687 rc = mmap_tst7_func(mnt, 0);
690 rc = mmap_tst7_func(mnt, 1);
694 static int mmap_tst8(char *mnt)
697 char *buf = MAP_FAILED;
701 char xyz[page_size * 2];
703 if (snprintf(fname, 256, "%s/mmap_tst8", mnt) >= 256) {
704 fprintf(stderr, "dir name too long\n");
708 fd = open(fname, O_RDWR | O_CREAT, 0644);
714 if (ftruncate(fd, page_size) == -1) {
719 buf = mmap(NULL, page_size * 2,
720 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
721 if (buf == MAP_FAILED) {
728 if (pid == 0) { /* child */
729 memcpy(xyz, buf, page_size * 2);
730 /* shouldn't reach here. */
732 } else if (pid > 0) { /* parent */
734 pid = waitpid(pid, &status, 0);
742 if (WIFSIGNALED(status) && SIGBUS == WTERMSIG(status))
750 if (buf != MAP_FAILED)
751 munmap(buf, page_size);
757 static int remote_tst(int tc, char *mnt)
762 rc = remote_tst3(mnt);
765 rc = remote_tst4(mnt);
768 fprintf(stderr, "wrong test case number %d\n", tc);
776 int tc; /* test case number */
777 char *desc; /* test description */
778 int (* test_fn)(char *mnt); /* test function */
779 int node_cnt; /* node count */
782 struct test_case tests[] = {
783 { 1, "mmap test1: basic mmap operation", mmap_tst1, 1 },
784 { 2, "mmap test2: MAP_PRIVATE not write back", mmap_tst2, 1 },
785 { 3, "mmap test3: concurrent mmap ops on two nodes", mmap_tst3, 2 },
786 { 4, "mmap test4: c1 write to f1 from mmapped f2, "
787 "c2 write to f1 from mmapped f1", mmap_tst4, 2 },
788 { 5, "mmap test5: read/write file to/from the buffer "
789 "which mmapped to just this file", mmap_tst5, 1 },
790 { 6, "mmap test6: check mmap write/read content on two nodes",
792 { 7, "mmap test7: file i/o with an unmapped buffer", mmap_tst7, 1},
793 { 8, "mmap test8: SIGBUS for beyond file size", mmap_tst8, 1},
797 int main(int argc, char **argv)
800 struct test_case *test;
804 c = getopt(argc, argv, "d:m:");
825 if (mmap_initialize(argv[0]) != 0) {
826 fprintf(stderr, "mmap_initialize failed!\n");
830 for (test = tests; test->tc; test++) {
834 if (test->node_cnt == 1 || dir2 != NULL) {
835 struct timeval start, end;
837 gettimeofday(&start, NULL);
838 rc = test->test_fn(dir);
839 gettimeofday(&end, NULL);
841 duration = (double)(end.tv_sec - start.tv_sec) +
842 (double)(end.tv_usec - start.tv_usec) / 1000000;
843 rs = rc ? "FAIL" : "PASS";
848 fprintf(stderr, "%s (%s, %.5gs)\n", test->desc, rs, duration);