/* mincore - counts noncached pages in files Copyright (C) 2005 Kasper Dupont This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or with permission from the author any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include #include #include /* Returns number of nonresident pages in a file. */ int fincore(int fd) { int r=0; unsigned i; uint8_t *vec; void * mapaddr; struct stat stbuf; off_t size; size_t veclen; int pagesize=getpagesize(); if (pagesize<1) return -1; if (fstat(fd,&stbuf)) return -1; if ((size=stbuf.st_size)<1) return 0; /* Empty file */ veclen=((size-1)/pagesize)+1; vec=malloc(veclen); if (!vec) return -1; mapaddr=mmap(NULL,size,PROT_READ,MAP_SHARED,fd,0); if (mapaddr == MAP_FAILED) { r=-1; } else { if (mincore(mapaddr,stbuf.st_size,vec)) { r=-1; } else { for (i=0;i