#include #include #include #include #include #include int main(int argc, char **argv) { int fd, i, pos; struct stat st; char *map; unsigned char *buf; size_t ps; unsigned int incore=0; unsigned int notincore=0; unsigned int percent=0; char obuf[64+2+1]; if ( argc != 2 ) { printf("Usage: \n"); printf(" %s filename\n", argv[0]); return 1; } if ( (fd=open(argv[1], O_RDONLY))<0 ) { perror("open()"); return 1; } if ( fstat(fd, &st) ) { perror("open()"); return 1; } if ( (map=mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED ) { perror("mmap()"); return 1; } /* Don't bother if its empty */ if ( !st.st_size ) { printf("[]\n"); goto summary; } ps=getpagesize(); if ( !(buf=(unsigned char *)calloc(1, st.st_size/ps)) ) { perror("calloc()"); return 1; } if ( mincore(map, st.st_size, buf) ) { perror("mincore()"); return 1; } #if 0 printf("%s: [", argv[1]); for(i=0; i<=st.st_size/ps; i++) { if ( buf[i] & 1 ) { incore++; printf("#"); }else{ notincore++; printf("_"); } } printf("]\n"); #endif pos=0; for(;;) { int cont=1; obuf[0]='['; for(i=0; i<64; i++) { if ( pos<=st.st_size/ps ) { if ( buf[pos]&1 ) { obuf[i+1]='#'; incore++; }else { obuf[i+1]='.'; notincore++; } }else{ cont=0; break; } pos++; } /* Print out this line */ obuf[i+1]=']'; obuf[i+2]='\0'; printf("%s: %s\n", argv[1], obuf); if ( !cont ) break; } summary: if ( incore+notincore ) /* Fear my floating point skillz! */ percent=(incore*10000)/(incore+notincore); else percent=100000; printf("%s: %u bytes, %u pages, %u cached (%u.%.2u%%), %u not cached\n", argv[1], st.st_size, incore+notincore, incore, percent/100, percent%100, notincore); return 0; }