🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.nntp
Article #15600

patch for nnrpd 1.5.1 to mmap .overview files

#15600
From: Jan-Pieter Cornet
Date: Thu, 06 Mar 1997 00:00
843 lines
24160 bytes
Below is a patch to mmap() .overview files, and binary search them.
This greatly speeds up the XOVER command (and other commands that
use the .overview database), especially when you have really large
groups.

I wrote this especially because the disk holding the overview database
was so overloaded it caused enourmous delays. Using mmap and a binary
search greatly reduces the amount of data read from disk, because
.overview files are now shared between processes, and because people
usually only want the last part of the .overview files anyway.

This consists of two parts: first the unified diff for nnrpd/article.c,
adding the mmap()ed .overview files. If you remove the #define OVERSCREAM,
then you get the old behaviour back.

The second part is a script to test your mmap() implementation, it's a
shar consisting of a C program and a bash script. You'll have to compile
the C program yourself (usually "make mmapme" will do just fine), and then
run the script. The test script hexdumps three files. All files should
only contain "1" (hex 31) characters. If you also see hex 00 characters,
then your mmap is flawed, and you need to bug your vendor for a patch.
(If the test script says "mmap: invalid parameters" or some similar error,
try running "mmap-test -p").

The patch has code to work around the mmap() bug, but it's kludgy, slow,
and not always returns the correct results.

--
#! ##### Jan-Pieter Cornet ##### <johnpc@xs4all.net> ##### perl
++$_;$!=$_+++$_;($:,$,,$/,$*)=$!=~/.(.)...(.)(.).(.)/;$!=$_+$_;
($@,$\,$~)=$!=~/(.)(.).(.)/; $_="$,$/$:"; $@++; $~="$~$_";($_)=
\$$=~/\((.)/;$|=++$_;$_++;$|++;$~="$~ $@$:";`$~$/$\$*$, $|>&$_`


----8<----------------------------------------------------------------

--- article.c	1997/02/08 14:19:13	1.1
+++ article.c	1997/03/06 17:29:27
@@ -1,4 +1,4 @@
-/*  $Revision: 1.1 $
+/*  $Revision: 1.31 $
 **
 **  Article-related routines.
 */
@@ -8,6 +8,18 @@
 #include "clibrary.h"
 #include "nnrpd.h"
 
+/*
+ * OVERSCREAM - to make the overview database screaming fast, and because
+ * I scream in terror about the previous implementation.
+ */
+
+#define OVERSCREAM
+
+#ifdef OVERSCREAM
+# include <sys/types.h>
+# include <sys/mman.h>
+#endif /* OVERSCREAM */
+
 
 /*
 **  Data structures for use in ARTICLE/HEAD/BODY/STAT common code.
@@ -61,11 +73,32 @@
 /*
 **  Overview state information.
 */
+#ifdef OVERSCREAM
+
+STATIC caddr_t		OVERshm = (caddr_t) NULL; /* location of mmap	*/
+STATIC size_t		OVERsize;		/* size of mmap		*/
+STATIC int		OVERfd;			/* fd of file 		*/
+STATIC ARTNUM		OVERfirst, OVERlast;	/* first/last entries	*/
+STATIC int		OVERopens;		/* Number of opens done	*/
+STATIC char*		OVERcache;		/* cached position	*/
+STATIC ARTNUM		OVERprev;		/* previous found art	*/
+#define LINSEARCH	5			/* linear search range  */
+#define MIDSKEW		0.1			/* 10% bias toward middle */
+
+STATIC int		mmapsuck;		/* did we syslog already */
+#define YOUR_MMAP_SUCKS if ( ! mmapsuck++ ) \
+    syslog(L_NOTICE, "Your mmap() implementation sucks.")
+
+#else /* !OVERSCREAM */
+
 STATIC QIOSTATE		*OVERqp;		/* Open overview file	*/
 STATIC char		*OVERline;		/* Current line		*/
 STATIC ARTNUM		OVERarticle;		/* Current article	*/
 STATIC int		OVERopens;		/* Number of opens done	*/
 
+#endif
+
+
 
 /*
 **  Read the overview schema.
@@ -676,6 +709,10 @@
     register int	i;
     ARTOVERFIELD	*fp;
     char		*next;
+#ifdef OVERSCREAM
+    char*		eol = strchr(p, '\n');
+#endif
+
 
     fp = &ARTfields[field - 1];
 
@@ -683,8 +720,13 @@
       field = ARTfirstfullfield;
  
     /* Skip leading headers. */
-    for (; --field >= 0 && *p; p++)
+    for (; --field >= 0 && *p && *p != '\n'; p++)
+#ifdef OVERSCREAM
+	if ((p = memchr(p, '\t', OVERsize - (p - OVERshm))) == NULL ||
+	    p > eol )
+#else
 	if ((p = strchr(p, '\t')) == NULL)
+#endif
 	    return NULL;
     if (*p == '\0')
 	return NULL;
@@ -702,10 +744,22 @@
     }
   
     /* Figure out length; get space. */
+
+#ifdef OVERSCREAM
+    if ((next = memchr(p, '\t', OVERsize - (p - OVERshm))) != NULL &&
+	  p < eol )
+	i = next - p;
+    else 
+	i = eol - p;
+
+#else /* !OVERSCREAM */
+
     if ((next = strchr(p, '\t')) != NULL)
 	i = next - p;
     else
 	i = strlen(p);
+#endif
+
     if (buffsize == 0) {
 	buffsize = i;
 	buff = NEW(char, buffsize + 1);
@@ -720,6 +774,408 @@
     return buff;
 }
 
+#ifdef OVERSCREAM
+
+/*
+ * helper function, search backwards in memory
+ */
+
+STATIC char*
+memrchr(p, c, l)
+    register char* p;
+    register char c;
+    register int l;
+{
+    for (; l--; --p)
+	if ( *p == c )
+	    return(p);
+    return(NULL);
+}
+
+/*
+ * mmap an OVERVIEW file.
+ */
+
+STATIC BOOL
+OVERopen()
+{
+    char		name[SPOOLNAMEBUFF];
+    struct stat 	sb;
+    char*		p;
+    static int		pagesize = 0;
+
+    /* return true if already mapped */
+    if ( OVERshm ) {
+	return TRUE;
+    }
+    /* return false if already failed */
+    if ( OVERopens++ ) {
+	return FALSE;
+    }
+    /* get memory pagesize if we don't have it already */
+    if ( ! pagesize && ! (pagesize = getpagesize()) ) {
+	syslog(L_NOTICE, "%s: Can't getpagesize", ClientHost);
+	return FALSE;
+    }
+    /* mmap the file */
+    (void)sprintf(name, "%s/%s/%s", _PATH_OVERVIEWDIR, GRPlast, _PATH_OVERVIEW);
+    if ( (OVERfd = open(name, O_RDONLY)) < 0 ) {
+	/* no overview file */
+	syslog(L_NOTICE, "%s can't open %s: %m", ClientHost, name);
+	return FALSE;
+    }
+    if ( fstat(OVERfd, &sb) == -1 ) {
+	syslog(L_NOTICE, "%s can't stat %s: %m", ClientHost, name);
+	(void)close(OVERfd);
+	return FALSE;
+    }
+    if ( (OVERsize = sb.st_size) <= 1 ) {
+	syslog(L_NOTICE, "%s: %s is too small", ClientHost, name);
+	(void)close(OVERfd);
+	return FALSE;
+    }
+    if ( (OVERshm = mmap(NULL, (OVERsize + pagesize - 1) & ~pagesize,
+	                 PROT_READ, 0, OVERfd, 0)) == (caddr_t) -1 )
+    {
+	syslog(L_NOTICE, "%s can't mmap %s: %m", ClientHost, name);
+	(void)close(OVERfd);
+	OVERshm = NULL;
+	return FALSE;
+    }
+    /* get first entry */
+    if ( (OVERfirst = atol((char*) OVERshm)) == 0 ) {
+	syslog(L_NOTICE, "%s: %s: bad format", ClientHost, name);
+	(void)munmap(OVERshm, OVERsize);
+	(void)close(OVERfd);
+	OVERshm = NULL;
+	return FALSE;
+    }
+
+    /* get last entry */
+    if ( *(OVERshm + OVERsize - 1) != '\n' ) {
+	/*
+	 * If you get here, then your mmap() implementation sucks.
+	 * Go complain with your OS vendor, that their mmap() can't
+	 * do mmap()ing of growing files properly.
+	 * We try to find a decent record near the end, for the poor
+	 * sobs without proper mmap. There are a lot of other places
+	 * in the code with hacks for bad mmap(). Mainly because I'm
+	 * one of the poor sobs :(
+	 */
+	YOUR_MMAP_SUCKS;
+    }
+    do {
+	p = memrchr(OVERshm + OVERsize - 1, '\n', OVERsize - 1);
+	if ( p == NULL ) {
+	    (void)munmap(OVERshm, OVERsize);
+	    (void)close(OVERfd);
+	    OVERshm = NULL;
+	    return FALSE;
+	}
+	OVERsize = p - OVERshm + 1;
+	if ( (p = memrchr((char*) OVERshm + OVERsize - 2, '\n',
+			    OVERsize - 1)) == NULL )
+	{
+	    syslog(L_NOTICE, "%s: %s: bad format", ClientHost, name);
+	    (void)munmap(OVERshm, OVERsize);
+	    (void)close(OVERfd);
+	    OVERshm = NULL;
+	    return FALSE;
+	}
+	OVERlast = atol(p+1);
+    }
+    while ( OVERlast == 0 && OVERsize-- );
+
+    OVERcache = NULL;
+    return TRUE;
+}
+
+/*
+ * Close an overview file, if any.
+ */
+
+void
+OVERclose()
+{
+    if ( OVERshm ) {
+	if ( munmap(OVERshm, OVERsize) == -1 ) {
+	    syslog(L_NOTICE, "%s can't munmap: %m", ClientHost);
+	}
+	(void)close(OVERfd);
+	OVERshm = NULL;
+    }
+    OVERopens = 0;
+}
+
+/*
+ * find an overview article using binary search in the overview file.
+ * Returns a pointer to the actual line in the overview file (so it's
+ * !!NOT!! null terminated, and can't be written to!!), or NULL on failure.
+ */
+
+STATIC char*
+OVERfind(artnum)
+    ARTNUM	artnum;
+{
+    char*	bottom;
+    char*	top;
+    ARTNUM	bottomnr;
+    ARTNUM	topnr;
+    char*	pos;
+    ARTNUM	nr;
+    int		i;
+
+    /* default startpos */
+    bottom = OVERshm;
+    bottomnr = OVERfirst;
+    top = OVERshm + OVERsize - 1;
+    topnr = OVERlast;
+
+    if ( OVERcache ) {
+	/*
+	 * for speedy sequential access. OVERcache, if non-NULL, points to
+	 * the "next" entry. OVERprev is the previous article number found.
+	 * Also check for sucking mmap() implementations.
+	 */
+	if ( *OVERcache == '\0' ) {
+	    YOUR_MMAP_SUCKS;
+	    OVERcache = memchr(OVERcache, '\n',
+				OVERsize - (OVERshm - OVERcache));
+	    if ( OVERcache == NULL || OVERcache == OVERshm + OVERsize - 1 ) {
+		OVERcache = NULL;
+		return NULL;
+	    }
+	    OVERcache++;
+	}
+	nr = atol(OVERcache);
+	if ( nr < OVERfirst || nr > OVERlast ) {
+	    /* boo */
+	    OVERcache = NULL;
+	    return NULL;
+	}
+	if ( nr == artnum ) {
+	    pos = OVERcache;
+	    goto bingo; /* calculate next OVERcache + return. (EW! a goto! :) */
+	}
+	else if ( artnum > nr ) {
+	    /* treat cache as first binary search */
+	    bottom = OVERcache;
+	    bottomnr = nr;
+	}
+	else {
+	    /* cache is first top */
+	    top = OVERcache - 1;
+	    topnr = nr - 1;
+	    if ( artnum > OVERprev ) {
+		/*
+		 * optimization: we're searching for something that isn't
+		 * in the database, but we want to keep the cache clean.
+		 * this occurs when we think an article is there, but it
+		 * really isn't, eg. because NOSCANDIR is on, or simply
+		 * because the overview database leaks.
+		 */
+		return(NULL);
+	    }
+	}
+    }
+
+    /* sanity check */
+    if ( artnum < bottomnr || artnum > topnr ) {
+	OVERcache = NULL;
+	return NULL;
+    }
+
+    for (;;) {
+	/*
+	 * This is the binary search loop, there are about a zillion
+	 * exits so I found it neater to code it in an endless loop :)
+	 * It simply continues until it is either found or it isn't...
+	 *
+	 * Note that we don't do a real binary search, but we guess
+	 * a position using the fact that the overview database usually
+	 * contains a reasonably linear range of articles, without any
+	 * big leaps, but we skew it a bit towards the middle to prevent
+	 * slow convergence in boundary cases (see also below).
+	 *
+	 * We switch to linear searching when we're "close",
+	 * because on short ranges, linear searches are about as fast
+	 * (or faster) anyway. LINSEARCH is currently guessed at 5,
+	 * because on average it takes 2.5 searches using a linear search,
+	 * where it usually takes 3 "straight" binary searches.
+	 *
+	 * Unfortunately, we can't be sure we get into linear search when
+	 * we're close, because the database may have large holes.
+	 */
+	/* test if it's near the bottom */
+	if ( artnum < bottomnr + LINSEARCH ) {
+	    i = 0;
+	    while ( artnum > bottomnr && i++ < LINSEARCH ) {
+		/* search next line */
+		bottom = memchr(bottom, '\n', OVERsize - (bottom - OVERshm));
+		if ( bottom == NULL || bottom == top + 1 ) {
+		    /* reached end of file */
+		    OVERcache = NULL;
+		    return NULL;
+		}
+		if ( *++bottom == 0 ) {
+		    YOUR_MMAP_SUCKS;
+		    continue;
+		}
+		bottomnr = atol(bottom);
+		if ( bottomnr < OVERfirst || bottomnr > OVERlast ) {
+		    OVERcache = NULL;
+		    return NULL;
+		}
+	    }
+	    if ( artnum == bottomnr ) {
+		pos = bottom;
+		goto bingo; /* calculate next OVERcache + return. */
+	    }
+	    else {
+		/* didn't find it, but we came close. still cache position */
+		OVERcache = bottom;
+		OVERprev = artnum;
+		return NULL;
+	    }
+	    /*NOTREACHED*/
+	}
+	/* test if it's near the top */
+	if ( artnum > topnr - LINSEARCH ) {
+	    /*
+	     * topnr is frequently guessed, so we must first determine it
+	     * correctly. The fun part about searching backwards is that
+	     * the next position (OVERcache) follows easily...
+	     */
+	    i = 0;
+	    do {
+		OVERcache = (top == OVERshm + OVERsize - 1) ? NULL : top + 1;
+		if ( (top = memrchr(--top, '\n', top - OVERshm))
+		    == NULL || top + 1 == bottom )
+		{
+		    /* search hit bottom */
+		    OVERcache = NULL;
+		    return NULL;
+		}
+		if ( *(top + 1) == 0 ) {
+		    YOUR_MMAP_SUCKS;
+		    /* make sure we continue */
+		    topnr = artnum + 1;
+		    continue;
+		}
+		topnr = atol(top + 1);
+		if ( topnr < OVERfirst || topnr > OVERlast ) {
+		    OVERcache = NULL;
+		    return NULL;
+		}
+	    }
+	    while ( artnum < topnr && i++ < LINSEARCH );
+	    if ( artnum == topnr ) {
+		/* bingo. This time we know OVERcache already */
+		OVERprev = artnum;
+		return(top + 1);
+	    }
+	    else {
+		/* not found, but close. cache position */
+		OVERprev = artnum;
+		return NULL;
+	    }
+	    /*NOTREACHED*/
+	}
+
+	/*
+	 * now for the real binary search:
+	 * Estimate the position of artnum, but with a small offset towards
+	 * the middle, for better convergence in case the set of articles
+	 * is non-linear (you get a straight binary search if MIDSKEW is 1.0).
+	 * MIDSKEW is currently determined using a big thumb, occultism,
+	 * astrology, cat /dev/uri-geller and some common sense (but not much)
+	 * MIDSKEW == 0.0 makes the search take only 1 iteration in case
+	 * the overview database is a monotonous array of lines with equal
+	 * length, but can make for really lousy searches in anything not like
+	 * the above, which, in the real world, is practically always.
+	 * MIDSKEW == 1.0 gives you a true binary search without any guessing
+	 * whatsoever.
+	 * I thought 10% would be good enough. Only riggid testing can
+	 * determine the optimal value, and then it still depends on a lot
+	 * of settings, like expire times, user newsgroups preference,
+	 * presence of cancelbots or cancelwars, frequency of expireover
+	 * runs... need I say more? :)
+	 */
+	if ( topnr <= bottomnr ) {
+	    /* Safety net. This REALLY should never happen. */
+	    syslog(L_NOTICE,
+		   "%s: ASSERTION FAILED: %d < %d looking for %d in %s",
+		   ClientHost, topnr, bottomnr, artnum, GRPlast);
+	}
+	pos = bottom + (int) ((double) (top - bottom) * (MIDSKEW * 0.5) +
+			      (top - bottom) * (1.0 - MIDSKEW) *
+			      (artnum - bottomnr) / (topnr - bottomnr));
+	/* search forward for newline */
+	if ( (pos = memchr(pos, '\n', OVERsize - (pos - OVERshm))) == NULL ) {
+	    /* this shouldn't happen */
+	    OVERcache = NULL;
+	    return NULL;
+	}
+	if ( pos == top ) {
+	    /* hmm... */
+	    if ( (pos = memrchr(--pos, '\n', pos - OVERshm))
+		 == NULL || pos == bottom - 1 )
+	    {
+		/*
+		 * This is what happens when there's a large hole and we're
+		 * looking for something inside the hole (which isn't there).
+		 * still record the position in this case...
+		 */
+		OVERcache = (top == OVERshm + OVERsize - 1) ? NULL : top + 1;
+		OVERprev = artnum;
+		return NULL;
+	    }
+	}
+	/* see where we are */
+	if ( *++pos == 0 ) {
+	    YOUR_MMAP_SUCKS;
+	    pos = memchr(pos, '\n', OVERsize - (pos - OVERshm));
+	    if ( pos == NULL || pos == OVERshm + OVERsize - 1 || pos == top ) {
+		OVERcache = NULL;
+		return NULL;
+	    }
+	    pos++;
+	}
+	nr = atol(pos);
+	if ( nr < OVERfirst || nr > OVERlast ) {
+	    OVERcache = NULL;
+	    return NULL;
+	}
+	if ( nr == artnum ) {
+	    /* bingo. Set cache to next entry */
+bingo:
+	    OVERcache = memchr(pos, '\n', OVERsize - (pos - OVERshm));
+	    if ( OVERcache == OVERshm + OVERsize - 1 )
+		OVERcache = NULL;
+	    else if ( OVERcache )
+		OVERcache++;
+	    OVERprev = artnum;
+	    return (pos);
+	}
+	if ( nr < artnum ) {
+	    /* found a new bottom */
+	    bottom = pos;
+	    bottomnr = nr;
+	}
+	else /* nr > artnum */ {
+	    /*
+	     * found a new top. Setting topnr to nr-1 is not entirely
+	     * correct, but who cares. (In fact we do care, but adjust
+	     * later :)
+	     */
+	    top = pos - 1;
+	    topnr = nr - 1;
+	}
+    }
+    /*NOTREACHED*/
+}
+
+#else /* !OVERSCREAM */
 
 /*
 **  Open an OVERVIEW file.
@@ -756,7 +1212,7 @@
     if (OVERqp != NULL) {
 	QIOclose(OVERqp);
 	OVERqp = NULL;
-	OVERopens = 0;
+	OVERopens = 0; /* this is a bug */
     }
 }
 
@@ -789,6 +1245,8 @@
     return OVERarticle == artnum ? OVERline : NULL;
 }
 
+#endif
+
 
 /*
 **  Read an article and create an overview line without the trailing
@@ -1078,12 +1536,22 @@
 	if (ARTfind(i) < 0)
 	    continue;
 
+	/*OVERVIEWcount++;*/
 	if (Opened && (p = OVERfind(i)) != NULL) {
+#ifdef OVERSCREAM
+	    char* eol = memchr(p, '\n', OVERsize - (p - OVERshm));
+	    if ( eol == NULL )
+		continue; /* this should NEVER NEVER EVER NEVER EVER happen */
+	    fwrite(p, 1, eol - p, stdout);
+	    fwrite("\r\n", 1, 2, stdout);
+#else
 	    Printf("%s\r\n", p);
+#endif
 	    continue;
 	}
 
 	(void)sprintf(buff, "%ld", i);
+	/*OVERGENcount++;*/
 	if ((p = OVERgen(buff)) != NULL)
 	    Printf("%s\r\n", p);
     }

----8<----------------------------------------------------------------


#!/bin/sh
# This is a shell archive (produced by GNU sharutils 4.2).
# To extract the files from this archive, save it to some FILE, remove
# everything before the `!/bin/sh' line above, then type `sh FILE'.
#
# Made on 1997-02-17 19:09 MET by <john@pc.xs4all.nl>.
# Source directory was `/home/john/c'.
#
# Existing files will *not* be overwritten unless `-c' is specified.
#
# This shar contains:
# length mode       name
# ------ ---------- ------------------------------------------
#   2073 -rw-r--r-- mmapme.c
#    360 -rwxr-xr-x mmap-test
#
save_IFS="${IFS}"
IFS="${IFS}:"
gettext_dir=FAILED
locale_dir=FAILED
first_param="$1"
for dir in $PATH
do
  if test "$gettext_dir" = FAILED && test -f $dir/gettext \
     && ($dir/gettext --version >/dev/null 2>&1)
  then
    set `$dir/gettext --version 2>&1`
    if test "$3" = GNU
    then
      gettext_dir=$dir
    fi
  fi
  if test "$locale_dir" = FAILED && test -f $dir/shar \
     && ($dir/shar --print-text-domain-dir >/dev/null 2>&1)
  then
    locale_dir=`$dir/shar --print-text-domain-dir`
  fi
done
IFS="$save_IFS"
if test "$locale_dir" = FAILED || test "$gettext_dir" = FAILED
then
  echo=echo
else
  TEXTDOMAINDIR=$locale_dir
  export TEXTDOMAINDIR
  TEXTDOMAIN=sharutils
  export TEXTDOMAIN
  echo="$gettext_dir/gettext -s"
fi
touch -am 1231235999 $$.touch >/dev/null 2>&1
if test ! -f 1231235999 && test -f $$.touch; then
  shar_touch=touch
else
  shar_touch=:
  echo
  $echo 'WARNING: not restoring timestamps.  Consider getting and'
  $echo "installing GNU \`touch', distributed in GNU File Utilities..."
  echo
fi
rm -f 1231235999 $$.touch
#
if mkdir _sh06781; then
  $echo 'x -' 'creating lock directory'
else
  $echo 'failed to create lock directory'
  exit 1
fi
# ============= mmapme.c ==============
if test -f 'mmapme.c' && test "$first_param" != -c; then
  $echo 'x -' SKIPPING 'mmapme.c' '(file already exists)'
else
  $echo 'x -' extracting 'mmapme.c' '(text)'
  sed 's/^X//' << 'SHAR_EOF' > 'mmapme.c' &&
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
X
#define USAGE "\
Usage:\n\
\n\
mmapme [-p] filename\n"
X
extern int optind;
X
int main(int argc, char** argv) {
X    int fd;
X    char* p;
X    char* shm;
X    struct stat st;
X    int size, pagesize;
X    char line[80];
X    char prevline[80];
X    char* c;
X    int i, j, sup, previ;
X    int adjust = 0;
X
X    while ( (i = getopt(argc, argv, "p")) != EOF ) {
X	if ( i == 'p' ) {
X	    adjust++;
X	}
X	else {
X	    fprintf(stderr, USAGE);
X	    exit(1);
X	}
X    }
X    if ( optind != argc - 1 ) {
X	fprintf(stderr, USAGE);
X	exit(1);
X    }
X    if ( (fd = open(argv[optind], O_RDONLY)) == -1 ) {
X	perror("open");
X	exit(1);
X    }
X    if ( fstat(fd, &st) ) {
X	perror("stat");
X	exit(1);
X    }
X    size = st.st_size;
X    if ( adjust ) {
#ifdef _SC_PAGE_SIZE
X	pagesize = sysconf(_SC_PAGE_SIZE);
#else
# ifdef _SC_PAGESIZE
X	pagesize = sysconf(_SC_PAGESIZE);
# else
X	pagesize = getpagesize();
# endif
#endif
X	size = (size + pagesize - 1) & ~pagesize;
X    }
X    if ( (shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0))
X          == (void*) -1 ) {
X	perror("mmap");
X	exit(1);
X    }
X    prevline[0] = '\0';
X    sup = 0;
X    p = shm;
X    for (i = 0; i < st.st_size; i += 16 ) {
X	c = line;
X	for (j = i; j < i+16 && j < st.st_size; j++) {
X	    sprintf(c, "%02X ", *p++);
X	    c += 3;
X	}
X	if ( !strcmp(prevline, line) ) {
X	    previ = i;
X	    sup++;
X	    continue;
X	}
X	if ( sup == 1 ) {
X	    printf("%08X %s\n", previ, prevline);
X	    sup = 0;
X	}
X	else if ( sup == 2 ) {
X	    printf("%08X %s\n%08X %s\n", previ - 16, prevline, previ, prevline);
X	    sup = 0;
X	}
X	else if ( sup > 2 ) {
X	    printf("... %d lines ...\n%08X %s\n", sup - 1, previ, prevline);
X	    sup = 0;
X	}
X	printf("%08X %s\n", i, line);
X	strcpy(prevline, line);
X	previ = i;
X    }
X    if ( sup == 1 ) {
X	printf("%08X %s\n", previ, prevline);
X    }
X    else if ( sup > 1 ) {
X	printf("... %d lines ...\n%08X %s\n", sup - 1, previ, prevline);
X    }
X    sleep(60);
X    munmap(shm, size);
X    close(fd);
X    return(0);
}
SHAR_EOF
  $shar_touch -am 0217163997 'mmapme.c' &&
  chmod 0644 'mmapme.c' ||
  $echo 'restore of' 'mmapme.c' 'failed'
  if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
  && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
    md5sum -c << SHAR_EOF >/dev/null 2>&1 \
    || $echo 'mmapme.c:' 'MD5 check failed'
aa506ce6ec404c9619828b709ccf214f  mmapme.c
SHAR_EOF
  else
    shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'mmapme.c'`"
    test 2073 -eq "$shar_count" ||
    $echo 'mmapme.c:' 'original size' '2073,' 'current size' "$shar_count!"
  fi
fi
# ============= mmap-test ==============
if test -f 'mmap-test' && test "$first_param" != -c; then
  $echo 'x -' SKIPPING 'mmap-test' '(file already exists)'
else
  $echo 'x -' extracting 'mmap-test' '(text)'
  sed 's/^X//' << 'SHAR_EOF' > 'mmap-test' &&
#!/bin/bash
X
echo -n 1111111111111111111111111111111111111111 > 40
cat 40 40 40 40 40 40 > 240
cat 240 240 240 240 40 > 1k
cat 1k 1k 1k 1k 1k > 5k
rm 40 240
cp 5k testfile
echo 1
X./mmapme $* testfile &
sleep 5
echo 1.5
cat 1k >> testfile
echo 2
X./mmapme $* testfile &
sleep 5
echo 2.5
cat 5k >> testfile
echo 3
X./mmapme $* testfile &
sleep 2
rm 1k 5k testfile
SHAR_EOF
  $shar_touch -am 0217120797 'mmap-test' &&
  chmod 0755 'mmap-test' ||
  $echo 'restore of' 'mmap-test' 'failed'
  if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
  && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
    md5sum -c << SHAR_EOF >/dev/null 2>&1 \
    || $echo 'mmap-test:' 'MD5 check failed'
204a9c56650abab19f4f9d1e5012f401  mmap-test
SHAR_EOF
  else
    shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'mmap-test'`"
    test 360 -eq "$shar_count" ||
    $echo 'mmap-test:' 'original size' '360,' 'current size' "$shar_count!"
  fi
fi
rm -fr _sh06781
exit 0

Technical Details
Message-ID: <5fn38s$3q0$1@news1.xs4all.nl>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mail
References: <5fmjjf$qcg$1@news1.xs4all.nl>

Show Full Headers
Complete Article Headers
Message-ID: <5fn38s$3q0$1@news1.xs4all.nl>
Subject: patch for nnrpd 1.5.1 to mmap .overview files
Date: Thu, 06 Mar 1997 00:00:00 +0000
From: johnpc@xs4all.nl (Jan-Pieter Cornet)
References: <5fmjjf$qcg$1@news1.xs4all.nl>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mail
X-Deja-AN: 223616896
Organization: XS4ALL Internet B.V.
X-XS4ALL-Date: Thu, 06 Mar 1997 19:43:40 CET
Newsgroups: news.software.nntp