Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 29 Jul 1999 12:09:42 -0700 (PDT)
From:      Matthew Dillon <dillon@apollo.backplane.com>
To:        Alan Cox <alc@cs.rice.edu>, David Greenman <dg@root.com>, hackers@FreeBSD.ORG
Subject:   Re: patch for behavior changes and madvise MADV_DONTNEED
Message-ID:  <199907291909.MAA77624@apollo.backplane.com>
References:  <199907162234.PAA21850@apollo.backplane.com> <19990720014804.A21777@cs.rice.edu> <199907291856.LAA77471@apollo.backplane.com>

next in thread | previous in thread | raw e-mail | index | archive | help

:    extremely well.  So well, in fact, that I can have a program which
:    mmap()'s a large file and continuously scans it - generating 4MB/sec of
:    network traffic, with virtually no effect to the rest of the system.

    Oh, clarification:  continuously scans it but also calls
    madvise(... MADV_DONTNEED) on the pages once it is through, that is.

    If you mmap and scan a file without any madvise's it still loads the 
    system down just like it always has.

    The program below simulates scanning a file sequentially via a mmap
    which also uses the MADV_DONTNEED feature.

    I did find one disadvantage to my patch - easily fixed (not enough to
    generate a new patch before this one goes in) - and that is by moving
    pages to the cache a lot of unnecessary page faults can be incurred.
    What we want to do, in fact, is for the pages we would normally move
    to the cache we instead move to the *head* of the inactive list (rather
    then the tail, which is what normally happens).  This disadvantage
    becomes apparent when you run the below program on a small file that
    easily fits in main memory.

						-Matt


#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>

volatile char c;

int
main(int ac, char **av)
{
    int fd;
    int i;
    int j;
    int pgsize = getpagesize();
    int pgmask = pgsize - 1;
    char *ptr;
    struct stat st;

    if (ac == 1) {
	printf("%s filename\n", av[0]);
	exit(1);
    }
    if ((fd = open(av[1], O_RDONLY)) < 0) {
	perror("open");
	exit(1);
    }
    fstat(fd, &st);
    ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
    madvise(ptr, st.st_size & ~pgmask, MADV_SEQUENTIAL);
    if (ptr == (char *)-1) {
	perror("mmap");
	exit(1);
    }
    for (i = j = 0; i < (int)st.st_size; (i += pgsize), ++j) {
	c = ptr[i];
	if (i && (j & 7) == 0)
	    madvise(ptr + i - pgsize * 8, pgsize * 8, MADV_DONTNEED);
    }
    madvise(ptr, st.st_size & ~pgmask, MADV_DONTNEED);
    return(0);
}



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199907291909.MAA77624>