]> git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/membanger.c
Cleanup: un-wrap C++ header includes
[thirdparty/squid.git] / test-suite / membanger.c
1 #include "squid.h"
2 #include "hash.h"
3
4 #if HAVE_UNISTD_H
5 #include <unistd.h>
6 #endif
7 #if HAVE_CTYPE_H
8 #include <ctype.h>
9 #endif
10 #if HAVE_STRINGS_H
11 #include <strings.h>
12 #endif
13
14 static hash_table *mem_table = NULL;
15 static hash_link *mem_entry;
16 struct rusage myusage;
17
18 #ifdef WITH_LIB
19 #include "Mem.h"
20 #include <assert.h>
21 extern void sizeToPoolInit();
22 extern MemPool *sizeToPool(size_t size);
23 #endif
24 extern char *malloc_options;
25 void my_free(char *, int, void *);
26
27 FILE *fp;
28 char *fn;
29 int initsiz;
30 int maxsiz;
31 int minchunk;
32 HASHCMP ptrcmp;
33 char mbuf[256];
34 char abuf[32];
35 char *p;
36
37 int size;
38 void *addr;
39 int amt;
40
41 int i;
42 int a;
43 int run_stats = 0;
44 void *my_xmalloc(size_t);
45 void *my_xcalloc(int, size_t);
46 int my_xfree(void *);
47
48 #define xmalloc my_xmalloc
49 #define xcalloc my_xcalloc
50 #define xfree my_xfree
51
52 int *size2id_array[2];
53 int size2id_len = 0;
54 int size2id_alloc = 0;
55
56 typedef struct {
57 char orig_ptr[32];
58 void *my_ptr;
59 #ifdef WITH_LIB
60 MemPool *pool;
61 #endif
62 int size;
63 } memitem;
64
65 struct {
66 int mallocs, frees, callocs, reallocs;
67 } mstat;
68
69 memitem *mi;
70 void size2id(size_t, memitem *);
71 void badformat();
72 void init_stats(), print_stats();
73 void my_hash_insert(hash_table * h, const char *k, memitem * item);
74 static void *xmemAlloc(memitem * item);
75 static void xmemFree(memitem * item);
76
77 int
78 ptrcmp(const void *a, const void *b)
79 {
80 return (strcmp(a, b));
81 }
82
83 main(int argc, char **argv)
84 {
85 char c;
86 extern char *optarg;
87 malloc_options = "A";
88 a = 0;
89 while ((c = getopt(argc, argv, "f:i:M:l:m:r:N")) != -1) {
90 switch (c) {
91 case 'N':
92 mem_pools_on = 0;
93 break;
94 case 'r':
95 run_stats = atoi(optarg);
96 break;
97 case 'f':
98 fn = xstrdup(optarg);
99 fp = fopen(fn, "r");
100 break;
101 case 'i':
102 initsiz = atoi(optarg);
103 break;
104 case 'l':
105 mem_max_size = atoi(optarg) * 1024 * 1024;
106 break;
107 case 'M':
108 maxsiz = atoi(optarg);
109 break;
110 case 'm':
111 minchunk = atoi(optarg);
112 break;
113 default:
114 fprintf(stderr,
115 "Usage: %s -f file -M maxsiz -i initsiz -m minchunk", argv[0]);
116 exit(1);
117 }
118
119 }
120 if (!fp) {
121 fprintf(stderr,
122 "%s pummels %s\n%s . o O ( You't supply a valid tracefile.)\n",
123 argv[0], getenv("USER"), argv[0]);
124 exit(1);
125 }
126 #ifdef WITH_LIB
127 sizeToPoolInit();
128 #endif
129 mem_table = hash_create(ptrcmp, 229, hash4); /* small hash table */
130 init_stats();
131 while (fgets(mbuf, 256, fp) != NULL) {
132 if (run_stats > 0 && (++a) % run_stats == 0)
133 print_stats();
134 p = NULL;
135 switch (mbuf[0]) {
136 case 'm': /* malloc */
137 p = strtok(&mbuf[2], ":");
138 if (!p)
139 badformat();
140 size = atoi(p);
141 p = strtok(NULL, "\n");
142 if (!p)
143 badformat();
144 mi = malloc(sizeof(memitem));
145 strcpy(mi->orig_ptr, p);
146 mi->size = size;
147 size2id(size, mi);
148 mi->my_ptr = xmemAlloc(mi); /* (void *)xmalloc(size); */
149 assert(mi->my_ptr);
150 my_hash_insert(mem_table, mi->orig_ptr, mi);
151 mstat.mallocs++;
152 break;
153 case 'c': /* calloc */
154 p = strtok(&mbuf[2], ":");
155 if (!p)
156 badformat();
157 amt = atoi(p);
158 p = strtok(NULL, ":");
159 if (!p)
160 badformat();
161 size = atoi(p);
162 p = strtok(NULL, "\n");
163 if (!p)
164 badformat();
165 mi = malloc(sizeof(memitem));
166 strcpy(mi->orig_ptr, p);
167 size2id(size, mi);
168 mi->size = amt * size;
169 mi->my_ptr = xmemAlloc(mi); /*(void *)xmalloc(amt*size); */
170 assert(mi->my_ptr);
171 my_hash_insert(mem_table, mi->orig_ptr, mi);
172 mstat.callocs++;
173 break;
174 case 'r':
175 p = strtok(&mbuf[2], ":");
176 if (!p)
177 badformat();
178 strcpy(abuf, p);
179 p = strtok(NULL, ":");
180 if (!p)
181 badformat();
182 mem_entry = hash_lookup(mem_table, p);
183 if (mem_entry == NULL) {
184 fprintf(stderr, "invalid realloc (%s)!\n", p);
185 break;
186 }
187 mi = (memitem *) (mem_entry->item);
188 assert(mi->pool);
189 assert(mi->my_ptr);
190 xmemFree(mi); /* xfree(mi->my_ptr); */
191 size2id(atoi(p), mi); /* we don't need it here I guess? */
192 strcpy(mi->orig_ptr, abuf);
193 p = strtok(NULL, "\n");
194 if (!p)
195 badformat();
196 mi->my_ptr = xmemAlloc(mi); /* (char *)xmalloc(atoi(p)); */
197 assert(mi->my_ptr);
198 mstat.reallocs++;
199 break;
200 case 'f':
201 p = strtok(&mbuf[2], "\n");
202 mem_entry = hash_lookup(mem_table, p);
203 if (mem_entry == NULL) {
204 if (p[0] != '0')
205 fprintf(stderr, "invalid free (%s) at line %d!\n", p, a);
206 break;
207 }
208 mi = (memitem *) (mem_entry->item);
209 assert(mi->pool);
210 assert(mi->my_ptr);
211 xmemFree(mi); /* xfree(mi->my_ptr); */
212 hash_unlink(mem_table, mem_entry, 1);
213 free(mi);
214 mstat.frees++;
215 break;
216 default:
217 fprintf(stderr, "%s pummels %s.bad.format\n", argv[0], fn);
218 exit(1);
219 }
220
221 }
222 fclose(fp);
223 print_stats();
224 }
225
226 void *
227 my_xmalloc(size_t a)
228 {
229 return NULL;
230 }
231
232 void *
233 my_xcalloc(int a, size_t b)
234 {
235 return NULL;
236 }
237
238 int
239 my_xfree(void *p)
240 {
241 return 0;
242 }
243 void
244 init_stats()
245 {
246
247 }
248
249 void
250 print_stats()
251 {
252 #ifdef WITH_LIB
253 memReport(stdout);
254 #endif
255 getrusage(RUSAGE_SELF, &myusage);
256 printf("m/c/f/r=%d/%d/%d/%d\n", mstat.mallocs, mstat.callocs,
257 mstat.frees, mstat.reallocs);
258 #if 0
259 printf("types : %d\n", size2id_len);
260 #endif
261 printf("user time used : %d.%d\n", (int) myusage.ru_utime.tv_sec,
262 (int) myusage.ru_utime.tv_usec);
263 printf("system time used : %d.%d\n", (int) myusage.ru_stime.tv_sec,
264 (int) myusage.ru_stime.tv_usec);
265 printf("max resident set size : %d\n", (int) myusage.ru_maxrss);
266 printf("page faults : %d\n", (int) myusage.ru_majflt);
267 }
268
269 void
270 size2id(size_t sz, memitem * mi)
271 {
272 #ifdef WITH_LIB
273 mi->pool = sizeToPool(sz);
274 assert(mi->pool);
275 #endif
276 return;
277 }
278
279 void
280 badformat()
281 {
282 fprintf(stderr, "pummel.bad.format\n");
283 exit(1);
284 }
285
286 /* unused code, saved for parts */
287 const char *
288 make_nam(int id, int size)
289 {
290 const char *buf = malloc(30); /* argh */
291 snprintf((char *)buf, sizeof(buf)-1, "pl:%d/%d", id, size);
292 return buf;
293 }
294
295 void
296 my_hash_insert(hash_table * h, const char *k, memitem * item)
297 {
298 memitem *l;
299 assert(item->pool);
300 assert(item->my_ptr);
301 hash_insert(h, k, item);
302 }
303
304 static void *
305 xmemAlloc(memitem * item)
306 {
307 extern MemPool *StringPool;
308 assert(item && item->pool);
309 if (StringPool == item->pool)
310 return memStringAlloc(item->pool, item->size);
311 else
312 return memAlloc(item->pool);
313 }
314
315 static void
316 xmemFree(memitem * item)
317 {
318 extern MemPool *StringPool;
319 assert(item && item->pool);
320 if (StringPool == item->pool)
321 return memStringFree(item->pool, item->my_ptr, item->size);
322 else
323 return memFree(item->pool, item->my_ptr);
324 }
325
326 void
327 my_free(char *file, int line, void *ptr)
328 {
329 #if 0
330 fprintf(stderr, "{%s:%d:%p", file, line, ptr);
331 #endif
332 free(ptr);
333 #if 0
334 fprintf(stderr, "}\n");
335 #endif
336 }