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 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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>
51 char *dir = NULL, *dir2 = NULL;
53 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);
459 sprintf(cmd, "ls /proc/fs/lustre/ldlm/namespaces/*-%s-*/lru_size", prefix);
461 sprintf(cmd, "ls /proc/fs/lustre/ldlm/namespaces/*/lru_size");
463 file = popen(cmd, "r");
469 while (fgets(line, len, file)) {
474 /* trim newline character */
475 *(line + strlen(line) - 1) = '\0';
476 f = fopen(line, "w");
482 rc = fwrite("clear", strlen("clear") + 1, 1, f);
496 /* don't dead lock while read/write file to/from the buffer which
497 * mmaped to just this file */
498 static int mmap_tst5(char *mnt)
500 char *ptr, mmap_file[256];
501 int region, fd, off, rc = 0;
503 region = page_size * 40;
504 off = page_size * 10;
505 sprintf(mmap_file, "%s/%s", mnt, "mmap_file5");
507 if (unlink(mmap_file) && errno != ENOENT) {
512 fd = open(mmap_file, O_CREAT|O_RDWR, 0600);
517 if (ftruncate(fd, region) < 0) {
518 perror("ftruncate()");
523 ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
524 if (ptr == MAP_FAILED) {
529 memset(ptr, 'a', region);
531 /* cancel unused locks */
532 rc = cancel_lru_locks("osc");
536 /* read/write region of file and buffer should be overlap */
537 rc = read(fd, ptr + off, off * 2);
543 rc = write(fd, ptr + off, off * 2);
557 /* mmap write to a file form client1 then mmap read from client2 */
558 static int mmap_tst6(char *mnt)
560 char mmap_file[256], mmap_file2[256];
561 char *ptr = NULL, *ptr2 = NULL;
562 int fd = 0, fd2 = 0, rc = 0;
564 sprintf(mmap_file, "%s/%s", mnt, "mmap_file6");
565 sprintf(mmap_file2, "%s/%s", dir2, "mmap_file6");
566 if (unlink(mmap_file) && errno != ENOENT) {
571 fd = open(mmap_file, O_CREAT|O_RDWR, 0600);
576 if (ftruncate(fd, page_size) < 0) {
577 perror("ftruncate()");
582 fd2 = open(mmap_file2, O_RDWR, 0600);
588 ptr = mmap(NULL, page_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
589 if (ptr == MAP_FAILED) {
595 ptr2 = mmap(NULL, page_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd2, 0);
596 if (ptr2 == MAP_FAILED) {
602 rc = cancel_lru_locks("osc");
606 memcpy(ptr, "blah", strlen("blah"));
607 if (strncmp(ptr, ptr2, strlen("blah"))) {
608 fprintf(stderr, "client2 mmap mismatch!\n");
612 memcpy(ptr2, "foo", strlen("foo"));
613 if (strncmp(ptr, ptr2, strlen("foo"))) {
614 fprintf(stderr, "client1 mmap mismatch!\n");
619 munmap(ptr2, page_size);
621 munmap(ptr, page_size);
630 static int mmap_tst7_func(char *mnt, int rw)
633 char *buf = MAP_FAILED;
638 if (snprintf(fname, 256, "%s/mmap_tst7.%s",
639 mnt, (rw == 0) ? "read":"write") >= 256) {
640 fprintf(stderr, "dir name too long\n");
644 fd = open(fname, O_RDWR | O_DIRECT | O_CREAT, 0644);
650 if (ftruncate(fd, 2 * page_size) == -1) {
655 buf = mmap(NULL, page_size,
656 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
657 if (buf == MAP_FAILED) {
662 /* ensure the second page isn't mapped */
663 munmap(buf + page_size, page_size);
664 bytes = (rw == 0) ? read(fd, buf, 2 * page_size) :
665 write(fd, buf, 2 * page_size);
666 /* Expected behavior */
667 if (bytes == page_size)
669 fprintf(stderr, "%s returned %zd, errno = %d\n",
670 (rw == 0)?"read":"write", bytes, errno);
673 if (buf != MAP_FAILED)
674 munmap(buf, page_size);
680 static int mmap_tst7(char *mnt)
684 rc = mmap_tst7_func(mnt, 0);
687 rc = mmap_tst7_func(mnt, 1);
691 static int remote_tst(int tc, char *mnt)
696 rc = remote_tst3(mnt);
699 rc = remote_tst4(mnt);
702 fprintf(stderr, "wrong test case number %d\n", tc);
710 int tc; /* test case number */
711 char *desc; /* test description */
712 int (* test_fn)(char *mnt); /* test function */
713 int node_cnt; /* node count */
716 struct test_case tests[] = {
717 { 1, "mmap test1: basic mmap operation", mmap_tst1, 1 },
718 { 2, "mmap test2: MAP_PRIVATE not write back", mmap_tst2, 1 },
719 { 3, "mmap test3: concurrent mmap ops on two nodes", mmap_tst3, 2 },
720 { 4, "mmap test4: c1 write to f1 from mmapped f2, "
721 "c2 write to f1 from mmapped f1", mmap_tst4, 2 },
722 { 5, "mmap test5: read/write file to/from the buffer "
723 "which mmapped to just this file", mmap_tst5, 1 },
724 { 6, "mmap test6: check mmap write/read content on two nodes",
726 { 7, "mmap test7: file i/o with an unmapped buffer", mmap_tst7, 1},
730 int main(int argc, char **argv)
733 struct test_case *test;
737 c = getopt(argc, argv, "d:m:");
758 if (mmap_initialize(argv[0]) != 0) {
759 fprintf(stderr, "mmap_initialize failed!\n");
763 for (test = tests; test->tc; test++) {
766 if (test->node_cnt == 1 || dir2 != NULL) {
767 rc = test->test_fn(dir);
768 rs = rc ? "fail" : "pass";
770 fprintf(stderr, "%s (%s)\n", test->desc, rs);