Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 19 Jan 1997 18:11:57 +1100 (EST)
From:      Douglas Thomas Crosher  <dtc@scrooge.ee.swin.oz.au>
To:        current@freebsd.org
Subject:   madvise MADV_FREE behavior broken?
Message-ID:  <199701190711.SAA09054@scrooge.ee.swin.oz.au>

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

I've noticed that madvise MADV_FREE doesn't seem to be working as
documented.  On the next access it seems to usually zero pages if they
are in memory, and page them in if paged out. Seems almost the total
opposite of the ideal behaviour of avoiding a zero fill if the page is
in memory and zero filling if a page-in were required.

The example below illustrates this. It writes to a lot of pages, so
many are paged out (on a 64M machine), then it MADV_FREEs each page
and reads from it. This causes as many page-ins as not using
MADV_FREE, where I would have expect no page-ins.

If the pages are unmapped and re-mapped, the page-ins are minimal,
although there is still a lot of paging out?

If the amount of pages used is decreased so that few are paged out
then most are zero filled.

Regards
Douglas Crosher

-=-=-

#include <stdio.h>
#include <sys/types.h>
#include <sys/mman.h>

#define NUM_PAGES 16384

main()
{
  int n,i, page;
  int num_not_zero=0;

  if (mmap(0x9000000,NUM_PAGES*4096,
	   PROT_READ|PROT_WRITE|PROT_EXEC,MAP_PRIVATE|MAP_ANON,-1,0)
      == -1)
    printf("mmap erorr\n");

  printf("Writing\n");

  /* write to all the pages */
  for (page=0;page<NUM_PAGES;page++) {
    int  *addr = (int *)((void *)0x9000000 + 4096*page);
    int  j;
    for (j=0; j<1024; j++)
      addr[j] = page;
  }

  printf("Freeing\n");

  for (page=0;page<NUM_PAGES;page+=1) {
    int  *addr = (int *)((void *)0x9000000 + 4096*page);
    madvise(addr,4096,MADV_FREE);
    if (addr[0]!=0) {
      printf("N%d=%d,", page, addr[0]);
      num_not_zero++;
    }
  }
  printf("\nNumber not zero=%d of %d\n",num_not_zero,NUM_PAGES);
}



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