show worst latencies as well
[tridge/junkcode.git] / swaptest.c
1 /* swaptest.c   0.1   (C) Jon Burgess 92jjb@eng.cam.ac.uk
2  *
3  * Tests the performance of the linux swap system
4  * Generates a block of random data, reading it a number of times.
5  */
6 /* TODO: options 
7  *   <size>  memory size to try
8  *   -time  do automatic timing
9  *   -random  random pattern of data access
10  *   -dirty dirty data on every access
11  *   -cycles number of iterations of read to try.
12  */
13
14
15 #include <stdio.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19
20 #define MEM_MB 64
21 #define MEM_KB (MEM_MB*1024)
22 #define MEM_SIZE (MEM_KB*1024)
23 #define ITERATIONS 2
24
25 main(int argc, char *argv[])
26 {
27     char *buf;
28     int i,ptr;
29     char junk;
30     unsigned long test;
31     FILE *file;
32     
33     buf = (char *) malloc(MEM_SIZE);
34     
35 #if 0
36     file = fopen ("/dev/urandom","r");
37     if (!file) {
38       printf("Error reading /dev/random\n");
39         exit(1);
40     }
41
42     /* Get a 1kB of random data */
43     if (1 != fread( buf, 1024, 1, file)) {
44          printf("Error reading random data\n");
45          exit(1);
46     }
47     for (i=1; i<MEM_KB;i++)
48        memcpy(buf+(i*1024),buf,1024);
49 #endif
50
51     for(ptr = 0; ptr < MEM_SIZE; ptr+=4) {
52       *(unsigned long *)(ptr + buf) = ptr;
53     }
54     
55     for(i=0;i<ITERATIONS;i++) {
56       printf("Running loop %d\n",i);
57       for(ptr=0; ptr<MEM_SIZE; ptr+=4096){
58 #if 0
59         junk =  *(ptr+buf);
60 #endif
61         test = *(unsigned long *)(ptr + buf);
62         if(test != ptr) {
63           printf("AIEEE: memory corrupted at %d, reads as %08lx\n",
64                  ptr, test);
65         }
66
67         /* Do we dirty each page? */
68 #if 0
69         *(ptr+buf) = 0;
70 #endif
71       }
72     }
73     exit(0);
74 }
75