Whamcloud - gitweb
- added test_3b which emulates recursive mount. Does not pass yet.
[fs/lustre-release.git] / lustre / tests / memhog.c
1 #include <sys/types.h>
2 #include <unistd.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #define CHUNK (128 * 1024)
7
8 void usage(const char *prog, FILE *out)
9 {
10         fprintf(out, "usage: %s allocsize\n", prog);
11         fprintf(out, " allocsize is kbytes, or number[KMGP] (P = pages)\n");
12         exit(out == stderr);
13 }
14
15 int main(int argc, char *argv[])
16 {
17         long long kbtotal = 0, kballoc;
18         int i, j, k, numchunk, alloc, sum, rc = 0;
19         char **mem, *tmp;
20
21         if (argc == 2) {
22                 char *end = NULL;
23                 kbtotal = strtoull(argv[1], &end, 0);
24
25                 switch(*end) {
26                 case 'g':
27                 case 'G':
28                         kbtotal *= 1024;
29                 case 'm':
30                 case 'M':
31                         kbtotal *= 1024;
32                 case '\0':
33                 case 'k':
34                 case 'K':
35                         break;
36                 case 'p':
37                 case 'P':
38                         kbtotal *= 4;
39                         break;
40                 default:
41                         usage(argv[0], stderr);
42                         break;
43                 }
44         }
45
46         if (argc != 2 || kbtotal == 0)
47                 usage(argv[0], stderr);
48
49         numchunk = (kbtotal + CHUNK - 1) / CHUNK;
50         mem = calloc(numchunk, sizeof(*mem));
51         if (mem == NULL) {
52                 fprintf(stderr, "error allocating initial chunk array\n");
53                 exit(1);
54         }
55
56         alloc = CHUNK;
57         printf("[%d] allocating %lld kbytes in %u kbyte chunks\n",
58                getpid(), kbtotal, alloc);
59         for (i = kballoc = 0; i < numchunk; i++, kballoc += alloc) {
60                 if (kbtotal - kballoc < alloc)
61                         alloc = kbtotal - kballoc;
62
63                 tmp = mem[i] = malloc(alloc * 1024);
64                 if (tmp == NULL) {
65                         fprintf(stderr, "malloc(%u) failed (%lld/%lld)\n",
66                                 alloc * 1024, kballoc, kbtotal);
67                 } else {
68                         printf("touching %p (%lld/%lld)\n",
69                                tmp, kballoc, kbtotal);
70                         for (j = 0; j < alloc; j += 4) {
71                                 for (k = 0, sum = 0; k < 4095; k++, tmp++)
72                                         sum += *tmp;
73                                 *tmp = sum;
74                         }
75                 }
76         }
77         printf("touched %lld kbytes\n", kballoc);
78
79         alloc = CHUNK;
80         printf("verifying %lld kbytes in %u kbyte chunks\n", kbtotal, alloc);
81         for (i = kballoc = 0; i < numchunk; i++, kballoc += alloc) {
82                 if (kbtotal - kballoc < alloc)
83                         alloc = kbtotal - kballoc;
84
85                 tmp = mem[i];
86                 if (tmp != NULL) {
87                         printf("verifying %p (%lld/%lld)\n",
88                                tmp, kballoc, kbtotal);
89                         for (j = 0; j < alloc; j += 4) {
90                                 for (k = 0, sum = 0; k < 4095; k++, tmp++)
91                                         sum += *tmp;
92                                 if (*tmp != sum) {
93                                         fprintf(stderr, "sum %x != %x at %p\n",
94                                                 *tmp, sum, tmp - 4092);
95                                         rc = 1;
96                                 }
97                         }
98                 }
99         }
100         printf("verified %lld kbytes\n", kballoc);
101         return rc;
102 }