]>
git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/membanger.c
14 static hash_table
*mem_table
= NULL
;
15 static hash_link
*mem_entry
;
16 struct rusage myusage
;
21 extern void sizeToPoolInit();
22 extern MemPool
*sizeToPool(size_t size
);
24 extern char *malloc_options
;
25 void my_free(char *, int, void *);
44 void *my_xmalloc(size_t);
45 void *my_xcalloc(int, size_t);
48 #define xmalloc my_xmalloc
49 #define xcalloc my_xcalloc
50 #define xfree my_xfree
52 int *size2id_array
[2];
54 int size2id_alloc
= 0;
66 int mallocs
, frees
, callocs
, reallocs
;
70 void size2id(size_t, memitem
*);
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
);
78 ptrcmp(const void *a
, const void *b
)
80 return (strcmp(a
, b
));
83 main(int argc
, char **argv
)
89 while ((c
= getopt(argc
, argv
, "f:i:M:l:m:r:N")) != -1) {
95 run_stats
= atoi(optarg
);
102 initsiz
= atoi(optarg
);
105 mem_max_size
= atoi(optarg
) * 1024 * 1024;
108 maxsiz
= atoi(optarg
);
111 minchunk
= atoi(optarg
);
115 "Usage: %s -f file -M maxsiz -i initsiz -m minchunk", argv
[0]);
122 "%s pummels %s\n%s . o O ( You't supply a valid tracefile.)\n",
123 argv
[0], getenv("USER"), argv
[0]);
129 mem_table
= hash_create(ptrcmp
, 229, hash4
); /* small hash table */
131 while (fgets(mbuf
, 256, fp
) != NULL
) {
132 if (run_stats
> 0 && (++a
) % run_stats
== 0)
136 case 'm': /* malloc */
137 p
= strtok(&mbuf
[2], ":");
141 p
= strtok(NULL
, "\n");
144 mi
= malloc(sizeof(memitem
));
145 strcpy(mi
->orig_ptr
, p
);
148 mi
->my_ptr
= xmemAlloc(mi
); /* (void *)xmalloc(size); */
150 my_hash_insert(mem_table
, mi
->orig_ptr
, mi
);
153 case 'c': /* calloc */
154 p
= strtok(&mbuf
[2], ":");
158 p
= strtok(NULL
, ":");
162 p
= strtok(NULL
, "\n");
165 mi
= malloc(sizeof(memitem
));
166 strcpy(mi
->orig_ptr
, p
);
168 mi
->size
= amt
* size
;
169 mi
->my_ptr
= xmemAlloc(mi
); /*(void *)xmalloc(amt*size); */
171 my_hash_insert(mem_table
, mi
->orig_ptr
, mi
);
175 p
= strtok(&mbuf
[2], ":");
179 p
= strtok(NULL
, ":");
182 mem_entry
= hash_lookup(mem_table
, p
);
183 if (mem_entry
== NULL
) {
184 fprintf(stderr
, "invalid realloc (%s)!\n", p
);
187 mi
= (memitem
*) (mem_entry
->item
);
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");
196 mi
->my_ptr
= xmemAlloc(mi
); /* (char *)xmalloc(atoi(p)); */
201 p
= strtok(&mbuf
[2], "\n");
202 mem_entry
= hash_lookup(mem_table
, p
);
203 if (mem_entry
== NULL
) {
205 fprintf(stderr
, "invalid free (%s) at line %d!\n", p
, a
);
208 mi
= (memitem
*) (mem_entry
->item
);
211 xmemFree(mi
); /* xfree(mi->my_ptr); */
212 hash_unlink(mem_table
, mem_entry
, 1);
217 fprintf(stderr
, "%s pummels %s.bad.format\n", argv
[0], fn
);
233 my_xcalloc(int a
, size_t b
)
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
);
259 printf("types : %d\n", size2id_len
);
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
);
270 size2id(size_t sz
, memitem
* mi
)
273 mi
->pool
= sizeToPool(sz
);
282 fprintf(stderr
, "pummel.bad.format\n");
286 /* unused code, saved for parts */
288 make_nam(int id
, int size
)
290 const char *buf
= malloc(30); /* argh */
291 snprintf((char *)buf
, sizeof(buf
)-1, "pl:%d/%d", id
, size
);
296 my_hash_insert(hash_table
* h
, const char *k
, memitem
* item
)
300 assert(item
->my_ptr
);
301 hash_insert(h
, k
, item
);
305 xmemAlloc(memitem
* item
)
307 extern MemPool
*StringPool
;
308 assert(item
&& item
->pool
);
309 if (StringPool
== item
->pool
)
310 return memStringAlloc(item
->pool
, item
->size
);
312 return memAlloc(item
->pool
);
316 xmemFree(memitem
* item
)
318 extern MemPool
*StringPool
;
319 assert(item
&& item
->pool
);
320 if (StringPool
== item
->pool
)
321 return memStringFree(item
->pool
, item
->my_ptr
, item
->size
);
323 return memFree(item
->pool
, item
->my_ptr
);
327 my_free(char *file
, int line
, void *ptr
)
330 fprintf(stderr
, "{%s:%d:%p", file
, line
, ptr
);
334 fprintf(stderr
, "}\n");