]> git.itanic.dy.fi Git - membench/blob - membench.c
Initial commit
[membench] / membench.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/time.h>
4
5 #define lsize sizeof(unsigned long)
6
7 long usec_diff(const struct timeval *a, const struct timeval *b)
8 {
9         long usec_a, usec_b;
10         usec_a = a->tv_sec * 1000000 + a->tv_usec;
11         usec_b = b->tv_sec * 1000000 + b->tv_usec;
12         return usec_b - usec_a;
13 }
14
15 int main(int argc, char *argv[])
16 {
17         int iterations, j, k, count, size;
18         unsigned long *buf, i, tmp = 0;
19         struct timeval start, end;
20
21         if (argc > 1)
22                 iterations = atoi(argv[1]);
23         else
24                 iterations = 24;
25
26         if (argc > 2)
27                 count = atoi(argv[2]) * 1024;
28         else
29                 count = 1024;
30
31         printf("Doing %d runs with buffer size goin up to %dk\n",
32                iterations, 1 << (iterations - 10));
33         printf("Running each round %d times\n", count);
34         printf("Size of unsigned long is %lu bits\n", lsize * 8);
35
36         switch (lsize) {
37         case 4:
38                 j = 12;
39                 break;
40         case 8:
41                 j = 10;
42                 break;
43         default:
44                 printf("WTF?\n");
45                 exit(1);
46         }
47
48         for (; j < iterations + 1; j++) {
49                 size = (1 << j) / lsize;
50                 buf = malloc(size * lsize);
51
52                 if (buf == NULL) {
53                         printf("Malloc failed\n");
54                         exit(1);
55                 }
56
57                 /* Touch the pages */
58                 for (i = 0; i < size; i++)
59                         buf[i] = i;
60
61                 printf("%luk\t", size * lsize / 1024);
62
63                 printf("read: ");
64                 fflush(stdout);
65
66                 gettimeofday(&start, 0);
67                 for (k = 0; k < count; k++) {
68                         buf[0] = tmp;
69                         for (i = 0; i < size; i++)
70                                 tmp += buf[i];
71                 }
72
73                 gettimeofday(&end, 0);
74
75                 printf("% 9.2f MB/s ", (double)(size * lsize) * 
76                        (1000000 / (1024.0 * 1024.0)) * count / 
77                        (double) usec_diff(&start, &end));
78
79
80                 printf("write: ");
81                 fflush(stdout);
82
83                 gettimeofday(&start, 0);
84                 for (k = 0; k < count; k++)
85                         for (i = 0; i < size; i++)
86                                  buf[i] = i;
87                 gettimeofday(&end, 0);
88
89                 printf("% 9.2f MB/s ", (double)(size * lsize) * 
90                        (1000000 / (1024.0 * 1024.0)) * count / 
91                        (double) usec_diff(&start, &end));
92         
93                 printf("\n");
94
95                 free(buf);
96         }
97         return 0;
98 }