Whamcloud - gitweb
LU-15103 tests: clean up busy cifs mount
[fs/lustre-release.git] / lustre / tests / memhog.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * This file is part of Lustre, http://www.lustre.org/
28  */
29
30 #include <sys/types.h>
31 #include <unistd.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #define CHUNK (128 * 1024)
36
37 void usage(const char *prog, FILE *out)
38 {
39         fprintf(out, "usage: %s allocsize\n", prog);
40         fprintf(out, " allocsize is kbytes, or number[KMGP] (P = pages)\n");
41         exit(out == stderr);
42 }
43
44 int main(int argc, char *argv[])
45 {
46         long long kbtotal = 0, kballoc;
47         int i, j, k, numchunk, alloc, sum, rc = 0;
48         char **mem, *tmp;
49
50         if (argc == 2) {
51                 char *end = NULL;
52                 kbtotal = strtoull(argv[1], &end, 0);
53
54                 switch(*end) {
55                 case 'g':
56                 case 'G':
57                         kbtotal *= 1024;
58                 case 'm':
59                 case 'M':
60                         kbtotal *= 1024;
61                 case '\0':
62                 case 'k':
63                 case 'K':
64                         break;
65                 case 'p':
66                 case 'P':
67                         kbtotal *= 4;
68                         break;
69                 default:
70                         usage(argv[0], stderr);
71                         break;
72                 }
73         }
74
75         if (argc != 2 || kbtotal == 0)
76                 usage(argv[0], stderr);
77
78         numchunk = (kbtotal + CHUNK - 1) / CHUNK;
79         mem = calloc(numchunk, sizeof(*mem));
80         if (mem == NULL) {
81                 fprintf(stderr, "error allocating initial chunk array\n");
82                 exit(-1);
83         }
84
85         alloc = CHUNK;
86         printf("[%d] allocating %lld kbytes in %u kbyte chunks\n",
87                getpid(), kbtotal, alloc);
88         for (i = kballoc = 0; i < numchunk && alloc > 0; i++, kballoc += alloc){
89                 if (kbtotal - kballoc < alloc)
90                         alloc = kbtotal - kballoc;
91
92                 while (alloc > 0 && (mem[i] = malloc(alloc * 1024)) == NULL) {
93                         fprintf(stderr, "malloc(%u) failed (%lld/%lld)\n",
94                                 alloc * 1024, kballoc, kbtotal);
95                         alloc /= 2;
96                 }
97                 if (alloc == 0)
98                         break;
99
100                 printf("touching %p ([%lld-%lld]/%lld)\n", mem[i], kballoc,
101                        kballoc + alloc - 1, kbtotal);
102                 for (j = 0, tmp = mem[i]; j < alloc; j += 4) {
103                         for (k = 0, sum = 0; k < 4095; k++, tmp++)
104                                 sum += *tmp;
105                         *tmp = sum;
106                 }
107         }
108         if (kballoc == 0)
109                 exit(-2);
110
111         kbtotal = kballoc;
112         printf("touched %lld kbytes\n", kballoc);
113
114         alloc = CHUNK;
115         printf("verifying %lld kbytes in %u kbyte chunks\n", kbtotal, alloc);
116         for (i = kballoc = 0; i < numchunk; i++, kballoc += alloc) {
117                 if (kbtotal - kballoc < alloc)
118                         alloc = kbtotal - kballoc;
119
120                 tmp = mem[i];
121                 if (tmp != NULL) {
122                         printf("verifying %p (%lld/%lld)\n",
123                                tmp, kballoc, kbtotal);
124                         for (j = 0; j < alloc; j += 4) {
125                                 for (k = 0, sum = 0; k < 4095; k++, tmp++)
126                                         sum += *tmp;
127                                 if (*tmp != sum) {
128                                         fprintf(stderr, "sum %x != %x at %p\n",
129                                                 *tmp, sum, tmp - 4092);
130                                         rc++;
131                                 }
132                         }
133                 }
134         }
135         printf("verified %lld kbytes\n", kballoc);
136         return rc;
137 }