Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/cmp mmap normal files in 1MB chunks.



details:   https://anonhg.NetBSD.org/src/rev/f7e39d1f6692
branches:  trunk
changeset: 573746:f7e39d1f6692
user:      dsl <dsl%NetBSD.org@localhost>
date:      Sun Feb 06 21:19:46 2005 +0000

description:
mmap normal files in 1MB chunks.
Due to a bug in mmap() the address space isn't counted against ulimit
until the page is faulted in - so mmapping a large file suceeds, but
cmp SIGSEGVs when it runs out of address space.
Also I don't believe that the madvise( MAP_SEQUENTIAL) has the desired
effect, and mmaping a large file is probably very bad for the UBC.

diffstat:

 usr.bin/cmp/regular.c |  62 +++++++++++++++++++++++++++-----------------------
 1 files changed, 34 insertions(+), 28 deletions(-)

diffs (99 lines):

diff -r e2488055dee8 -r f7e39d1f6692 usr.bin/cmp/regular.c
--- a/usr.bin/cmp/regular.c     Sun Feb 06 20:50:34 2005 +0000
+++ b/usr.bin/cmp/regular.c     Sun Feb 06 21:19:46 2005 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: regular.c,v 1.13 2005/02/06 20:50:34 dsl Exp $ */
+/*     $NetBSD: regular.c,v 1.14 2005/02/06 21:19:46 dsl Exp $ */
 
 /*-
  * Copyright (c) 1991, 1993, 1994
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)regular.c  8.3 (Berkeley) 4/2/94";
 #else
-__RCSID("$NetBSD: regular.c,v 1.13 2005/02/06 20:50:34 dsl Exp $");
+__RCSID("$NetBSD: regular.c,v 1.14 2005/02/06 21:19:46 dsl Exp $");
 #endif
 #endif /* not lint */
 
@@ -57,6 +57,7 @@
        u_char ch, *p1, *p2;
        off_t byte, length, line;
        int dfound;
+       size_t blk_sz, blk_cnt;
 
        if (sflag && len1 != len2)
                exit(1);
@@ -69,41 +70,46 @@
        len2 -= skip2;
 
        length = MIN(len1, len2);
-       if (length > SIZE_T_MAX) {
-mmap_failed:
-               c_special(fd1, file1, skip1, fd2, file2, skip2);
-               return;
-       }
-
-       if ((p1 = (u_char *)mmap(NULL, (size_t)length,
-           PROT_READ, MAP_FILE|MAP_SHARED, fd1, skip1)) == MAP_FAILED)
-               goto mmap_failed;
-       (void)madvise(p1, (size_t)length, MADV_SEQUENTIAL);
+       for (blk_sz = 1024 * 1024; length != 0; length -= blk_sz) {
+               if (blk_sz > length)
+                       blk_sz = length;
+               p1 = mmap(NULL, blk_sz, PROT_READ, MAP_FILE|MAP_SHARED, fd1, skip1);
+               if (p1 == MAP_FAILED)
+                       goto mmap_failed;
 
-       if ((p2 = (u_char *)mmap(NULL, (size_t)length,
-           PROT_READ, MAP_FILE|MAP_SHARED, fd2, skip2)) == MAP_FAILED) {
-               munmap(p1, (size_t) length);
-               goto mmap_failed;
-       }
-       (void)madvise(p2, (size_t)length, MADV_SEQUENTIAL);
+               p2 = mmap(NULL, blk_sz, PROT_READ, MAP_FILE|MAP_SHARED, fd2, skip2);
+               if (p2 == MAP_FAILED) {
+                       munmap(p1, blk_sz);
+                       goto mmap_failed;
+               }
 
-       dfound = 0;
-       for (byte = line = 1; length--; ++p1, ++p2, ++byte) {
-               if ((ch = *p1) != *p2) {
-                       if (lflag) {
+               dfound = 0;
+               blk_cnt = blk_sz;
+               for (byte = line = 1; blk_cnt--; ++p1, ++p2, ++byte) {
+                       if ((ch = *p1) != *p2) {
+                               if (!lflag) {
+                                       diffmsg(file1, file2, byte, line);
+                                       /* NOTREACHED */
+                               }
                                dfound = 1;
                                (void)printf("%6lld %3o %3o\n", (long long)byte,
-                                   ch, *p2);
-                       } else
-                               diffmsg(file1, file2, byte, line);
-                               /* NOTREACHED */
+                                           ch, *p2);
+                       }
+                       if (ch == '\n')
+                               ++line;
                }
-               if (ch == '\n')
-                       ++line;
+               munmap(p1 - blk_sz, blk_sz);
+               munmap(p2 - blk_sz, blk_sz);
+               skip1 += blk_sz;
+               skip2 += blk_sz;
        }
 
        if (len1 != len2)
                eofmsg (len1 > len2 ? file2 : file1);
        if (dfound)
                exit(DIFF_EXIT);
+       return;
+
+mmap_failed:
+       c_special(fd1, file1, skip1, fd2, file2, skip2);
 }



Home | Main Index | Thread Index | Old Index