]>
git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/membanger.c
9884b84379ffd9217d74080a3f43404fb7dc10ba
2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
22 static hash_table
*mem_table
= NULL
;
23 static hash_link
*mem_entry
;
24 struct rusage myusage
;
29 extern void sizeToPoolInit();
30 extern MemPool
*sizeToPool(size_t size
);
32 extern char *malloc_options
;
33 void my_free(char *, int, void *);
52 void *my_xmalloc(size_t);
53 void *my_xcalloc(int, size_t);
56 #define xmalloc my_xmalloc
57 #define xcalloc my_xcalloc
58 #define xfree my_xfree
60 int *size2id_array
[2];
62 int size2id_alloc
= 0;
74 int mallocs
, frees
, callocs
, reallocs
;
78 void size2id(size_t, memitem
*);
80 void init_stats(), print_stats();
81 void my_hash_insert(hash_table
* h
, const char *k
, memitem
* item
);
82 static void *xmemAlloc(memitem
* item
);
83 static void xmemFree(memitem
* item
);
86 ptrcmp(const void *a
, const void *b
)
88 return (strcmp(a
, b
));
91 main(int argc
, char **argv
)
97 while ((c
= getopt(argc
, argv
, "f:i:M:l:m:r:N")) != -1) {
103 run_stats
= atoi(optarg
);
106 fn
= xstrdup(optarg
);
110 initsiz
= atoi(optarg
);
113 mem_max_size
= atoi(optarg
) * 1024 * 1024;
116 maxsiz
= atoi(optarg
);
119 minchunk
= atoi(optarg
);
123 "Usage: %s -f file -M maxsiz -i initsiz -m minchunk", argv
[0]);
130 "%s pummels %s\n%s . o O ( You't supply a valid tracefile.)\n",
131 argv
[0], getenv("USER"), argv
[0]);
137 mem_table
= hash_create(ptrcmp
, 229, hash4
); /* small hash table */
139 while (fgets(mbuf
, 256, fp
) != NULL
) {
140 if (run_stats
> 0 && (++a
) % run_stats
== 0)
144 case 'm': /* malloc */
145 p
= strtok(&mbuf
[2], ":");
149 p
= strtok(NULL
, "\n");
152 mi
= malloc(sizeof(memitem
));
153 strcpy(mi
->orig_ptr
, p
);
156 mi
->my_ptr
= xmemAlloc(mi
); /* (void *)xmalloc(size); */
158 my_hash_insert(mem_table
, mi
->orig_ptr
, mi
);
161 case 'c': /* calloc */
162 p
= strtok(&mbuf
[2], ":");
166 p
= strtok(NULL
, ":");
170 p
= strtok(NULL
, "\n");
173 mi
= malloc(sizeof(memitem
));
174 strcpy(mi
->orig_ptr
, p
);
176 mi
->size
= amt
* size
;
177 mi
->my_ptr
= xmemAlloc(mi
); /*(void *)xmalloc(amt*size); */
179 my_hash_insert(mem_table
, mi
->orig_ptr
, mi
);
183 p
= strtok(&mbuf
[2], ":");
187 p
= strtok(NULL
, ":");
190 mem_entry
= hash_lookup(mem_table
, p
);
191 if (mem_entry
== NULL
) {
192 fprintf(stderr
, "invalid realloc (%s)!\n", p
);
195 mi
= (memitem
*) (mem_entry
->item
);
198 xmemFree(mi
); /* xfree(mi->my_ptr); */
199 size2id(atoi(p
), mi
); /* we don't need it here I guess? */
200 strcpy(mi
->orig_ptr
, abuf
);
201 p
= strtok(NULL
, "\n");
204 mi
->my_ptr
= xmemAlloc(mi
); /* (char *)xmalloc(atoi(p)); */
209 p
= strtok(&mbuf
[2], "\n");
210 mem_entry
= hash_lookup(mem_table
, p
);
211 if (mem_entry
== NULL
) {
213 fprintf(stderr
, "invalid free (%s) at line %d!\n", p
, a
);
216 mi
= (memitem
*) (mem_entry
->item
);
219 xmemFree(mi
); /* xfree(mi->my_ptr); */
220 hash_unlink(mem_table
, mem_entry
, 1);
225 fprintf(stderr
, "%s pummels %s.bad.format\n", argv
[0], fn
);
241 my_xcalloc(int a
, size_t b
)
263 getrusage(RUSAGE_SELF
, &myusage
);
264 printf("m/c/f/r=%d/%d/%d/%d\n", mstat
.mallocs
, mstat
.callocs
,
265 mstat
.frees
, mstat
.reallocs
);
267 printf("types : %d\n", size2id_len
);
269 printf("user time used : %d.%d\n", (int) myusage
.ru_utime
.tv_sec
,
270 (int) myusage
.ru_utime
.tv_usec
);
271 printf("system time used : %d.%d\n", (int) myusage
.ru_stime
.tv_sec
,
272 (int) myusage
.ru_stime
.tv_usec
);
273 printf("max resident set size : %d\n", (int) myusage
.ru_maxrss
);
274 printf("page faults : %d\n", (int) myusage
.ru_majflt
);
278 size2id(size_t sz
, memitem
* mi
)
281 mi
->pool
= sizeToPool(sz
);
290 fprintf(stderr
, "pummel.bad.format\n");
294 /* unused code, saved for parts */
296 make_nam(int id
, int size
)
298 const char *buf
= malloc(30); /* argh */
299 snprintf((char *)buf
, sizeof(buf
)-1, "pl:%d/%d", id
, size
);
304 my_hash_insert(hash_table
* h
, const char *k
, memitem
* item
)
308 assert(item
->my_ptr
);
309 hash_insert(h
, k
, item
);
313 xmemAlloc(memitem
* item
)
315 extern MemPool
*StringPool
;
316 assert(item
&& item
->pool
);
317 if (StringPool
== item
->pool
)
318 return memStringAlloc(item
->pool
, item
->size
);
320 return memAlloc(item
->pool
);
324 xmemFree(memitem
* item
)
326 extern MemPool
*StringPool
;
327 assert(item
&& item
->pool
);
328 if (StringPool
== item
->pool
)
329 return memStringFree(item
->pool
, item
->my_ptr
, item
->size
);
331 return memFree(item
->pool
, item
->my_ptr
);
335 my_free(char *file
, int line
, void *ptr
)
338 fprintf(stderr
, "{%s:%d:%p", file
, line
, ptr
);
342 fprintf(stderr
, "}\n");