Whamcloud - gitweb
LU-12296 llite: improve ll_dom_lock_cancel
[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  * Lustre is a trademark of Sun Microsystems, Inc.
29  */
30
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35
36 #define CHUNK (128 * 1024)
37
38 void usage(const char *prog, FILE *out)
39 {
40         fprintf(out, "usage: %s allocsize\n", prog);
41         fprintf(out, " allocsize is kbytes, or number[KMGP] (P = pages)\n");
42         exit(out == stderr);
43 }
44
45 int main(int argc, char *argv[])
46 {
47         long long kbtotal = 0, kballoc;
48         int i, j, k, numchunk, alloc, sum, rc = 0;
49         char **mem, *tmp;
50
51         if (argc == 2) {
52                 char *end = NULL;
53                 kbtotal = strtoull(argv[1], &end, 0);
54
55                 switch(*end) {
56                 case 'g':
57                 case 'G':
58                         kbtotal *= 1024;
59                 case 'm':
60                 case 'M':
61                         kbtotal *= 1024;
62                 case '\0':
63                 case 'k':
64                 case 'K':
65                         break;
66                 case 'p':
67                 case 'P':
68                         kbtotal *= 4;
69                         break;
70                 default:
71                         usage(argv[0], stderr);
72                         break;
73                 }
74         }
75
76         if (argc != 2 || kbtotal == 0)
77                 usage(argv[0], stderr);
78
79         numchunk = (kbtotal + CHUNK - 1) / CHUNK;
80         mem = calloc(numchunk, sizeof(*mem));
81         if (mem == NULL) {
82                 fprintf(stderr, "error allocating initial chunk array\n");
83                 exit(-1);
84         }
85
86         alloc = CHUNK;
87         printf("[%d] allocating %lld kbytes in %u kbyte chunks\n",
88                getpid(), kbtotal, alloc);
89         for (i = kballoc = 0; i < numchunk && alloc > 0; i++, kballoc += alloc){
90                 if (kbtotal - kballoc < alloc)
91                         alloc = kbtotal - kballoc;
92
93                 while (alloc > 0 && (mem[i] = malloc(alloc * 1024)) == NULL) {
94                         fprintf(stderr, "malloc(%u) failed (%lld/%lld)\n",
95                                 alloc * 1024, kballoc, kbtotal);
96                         alloc /= 2;
97                 }
98                 if (alloc == 0)
99                         break;
100
101                 printf("touching %p ([%lld-%lld]/%lld)\n", mem[i], kballoc,
102                        kballoc + alloc - 1, kbtotal);
103                 for (j = 0, tmp = mem[i]; j < alloc; j += 4) {
104                         for (k = 0, sum = 0; k < 4095; k++, tmp++)
105                                 sum += *tmp;
106                         *tmp = sum;
107                 }
108         }
109         if (kballoc == 0)
110                 exit(-2);
111
112         kbtotal = kballoc;
113         printf("touched %lld kbytes\n", kballoc);
114
115         alloc = CHUNK;
116         printf("verifying %lld kbytes in %u kbyte chunks\n", kbtotal, alloc);
117         for (i = kballoc = 0; i < numchunk; i++, kballoc += alloc) {
118                 if (kbtotal - kballoc < alloc)
119                         alloc = kbtotal - kballoc;
120
121                 tmp = mem[i];
122                 if (tmp != NULL) {
123                         printf("verifying %p (%lld/%lld)\n",
124                                tmp, kballoc, kbtotal);
125                         for (j = 0; j < alloc; j += 4) {
126                                 for (k = 0, sum = 0; k < 4095; k++, tmp++)
127                                         sum += *tmp;
128                                 if (*tmp != sum) {
129                                         fprintf(stderr, "sum %x != %x at %p\n",
130                                                 *tmp, sum, tmp - 4092);
131                                         rc++;
132                                 }
133                         }
134                 }
135         }
136         printf("verified %lld kbytes\n", kballoc);
137         return rc;
138 }