In the mmap-testing backend program, read the mmap'ed file completely.
authorErez_Zadok <ezk@cs.sunysb.edu>
Sat, 14 Jul 2007 07:46:13 +0000 (03:46 -0400)
committerErez_Zadok <ezk@cs.sunysb.edu>
Sat, 14 Jul 2007 07:46:13 +0000 (03:46 -0400)
Instead of reading just the first byte of the file, read a few bytes in each
page, all the way to the end of the file, so we touch the entire file.  This
is useful to simulate memory-pressure conditions.

progs/mapper.c

index c18e1571ac88904807b2a90add3046316329bcb3..411f141da1bcc37995053d6fe014afcafdd43f3f 100644 (file)
 #include <stdio.h>
 
 #define BASE 0
-#define SIZE 10
 
 #ifndef MAP_FAILED
 #define MAP_FAILED ((void *)-1)
-#endif
+#endif /* not MAP_FAILED */
 
 void usage(void)
 {
@@ -50,6 +49,8 @@ main(int argc, char *argv[])
   caddr_t pa;
   int rw_flag = -1;            /* 0=read, 1=read+write */
   char *filename, byte;
+  int i, size;
+  struct stat buf;
 
   /* check input args */
   if (argc != 3)
@@ -63,6 +64,13 @@ main(int argc, char *argv[])
 
   filename = argv[2];
 
+  /* find size of file */
+  if (stat(filename, &buf) < 0) {
+    perror("stat");
+    exit(1);
+  }
+  size = buf.st_size;
+
   /* open input file */
   if (rw_flag)
     fd = open(filename, O_RDWR);
@@ -76,7 +84,7 @@ main(int argc, char *argv[])
 
   /* mmap the input file */
   pa = mmap((caddr_t) 0,
-           SIZE,
+           size,
            (rw_flag ? (PROT_READ | PROT_WRITE) : PROT_READ),
            MAP_SHARED,
            fd,
@@ -87,24 +95,28 @@ main(int argc, char *argv[])
   }
   printf("file is mapped with address %p\n", pa);
 
-  /* now read the first byte of that file */
-  byte = pa[0];
-  printf("byte 0 is decimal %03d (\'%c\')\n",
-        byte, isprint(byte) ? byte : '.');
+  /* now read every 1024-th byte */
+  for (i=0; i<size; i+=1024) {
+    byte = pa[i];
+    printf("byte 0 is decimal %03d (\'%c\')\n",
+          byte, isprint(byte) ? byte : '.');
+  }
 
   /* for a read-write test, modify the first byte */
   if (rw_flag)
     pa[0] += 1;
 
-  /* msync the file */
-  if (msync(pa, SIZE, MS_SYNC) < 0) {
-    perror("msync");
-    exit(1);
-  }
+
+  /* msync the file, but only if we're modifying the file */
+  if (rw_flag)
+    if (msync(pa, size, MS_SYNC) < 0) {
+      perror("msync");
+      exit(1);
+    }
   printf("file is msynchronized\n");
 
   /* unmap the file */
-  if (munmap(pa, SIZE) < 0) {
+  if (munmap(pa, size) < 0) {
     perror("munmap");
     exit(1);
   }
@@ -129,7 +141,7 @@ main(int argc, char *argv[])
 
   /* re-mmap the input file */
   pa = mmap((caddr_t) 0,
-           SIZE,
+           size,
            PROT_READ,
            MAP_SHARED,
            fd,
@@ -147,7 +159,7 @@ main(int argc, char *argv[])
     exit(1);
   }
   /* unmap the file */
-  if (munmap(pa, SIZE) < 0) {
+  if (munmap(pa, size) < 0) {
     perror("munmap");
     exit(1);
   }