1712b2f30Ssthen /*
2712b2f30Ssthen * testcode/memstats.c - debug tool to show memory allocation statistics.
3712b2f30Ssthen *
4712b2f30Ssthen * Copyright (c) 2007, NLnet Labs. All rights reserved.
5712b2f30Ssthen *
6712b2f30Ssthen * This software is open source.
7712b2f30Ssthen *
8712b2f30Ssthen * Redistribution and use in source and binary forms, with or without
9712b2f30Ssthen * modification, are permitted provided that the following conditions
10712b2f30Ssthen * are met:
11712b2f30Ssthen *
12712b2f30Ssthen * Redistributions of source code must retain the above copyright notice,
13712b2f30Ssthen * this list of conditions and the following disclaimer.
14712b2f30Ssthen *
15712b2f30Ssthen * Redistributions in binary form must reproduce the above copyright notice,
16712b2f30Ssthen * this list of conditions and the following disclaimer in the documentation
17712b2f30Ssthen * and/or other materials provided with the distribution.
18712b2f30Ssthen *
19712b2f30Ssthen * Neither the name of the NLNET LABS nor the names of its contributors may
20712b2f30Ssthen * be used to endorse or promote products derived from this software without
21712b2f30Ssthen * specific prior written permission.
22712b2f30Ssthen *
23712b2f30Ssthen * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24712b2f30Ssthen * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25712b2f30Ssthen * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26712b2f30Ssthen * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27712b2f30Ssthen * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28712b2f30Ssthen * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29712b2f30Ssthen * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30712b2f30Ssthen * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31712b2f30Ssthen * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32712b2f30Ssthen * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33712b2f30Ssthen * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34712b2f30Ssthen */
35712b2f30Ssthen
36712b2f30Ssthen /**
37712b2f30Ssthen * \file
38712b2f30Ssthen *
39712b2f30Ssthen * This program reads a log file and prints the memory allocation summed
40712b2f30Ssthen * up.
41712b2f30Ssthen */
42712b2f30Ssthen #include "config.h"
43712b2f30Ssthen #include "util/log.h"
44712b2f30Ssthen #include "util/rbtree.h"
45712b2f30Ssthen #include "util/locks.h"
46712b2f30Ssthen #include "util/fptr_wlist.h"
47712b2f30Ssthen #include <sys/stat.h>
48712b2f30Ssthen
49712b2f30Ssthen /**
50712b2f30Ssthen * The allocation statistics block
51712b2f30Ssthen */
52712b2f30Ssthen struct codeline {
53712b2f30Ssthen /** rbtree node */
54712b2f30Ssthen rbnode_type node;
55712b2f30Ssthen /** the name of the file:linenumber */
56712b2f30Ssthen char* codeline;
57712b2f30Ssthen /** the name of the function */
58712b2f30Ssthen char* func;
59712b2f30Ssthen /** number of bytes allocated */
60712b2f30Ssthen uint64_t alloc;
61712b2f30Ssthen /** number of bytes freed */
62712b2f30Ssthen uint64_t free;
63712b2f30Ssthen /** number allocations and frees */
64712b2f30Ssthen uint64_t calls;
65712b2f30Ssthen };
66712b2f30Ssthen
67712b2f30Ssthen /** print usage and exit */
68712b2f30Ssthen static void
usage(void)69712b2f30Ssthen usage(void)
70712b2f30Ssthen {
71712b2f30Ssthen printf("usage: memstats <logfile>\n");
72712b2f30Ssthen printf("statistics are printed on stdout.\n");
73712b2f30Ssthen exit(1);
74712b2f30Ssthen }
75712b2f30Ssthen
76712b2f30Ssthen /** match logfile line to see if it needs accounting processing */
77712b2f30Ssthen static int
match(char * line)78712b2f30Ssthen match(char* line)
79712b2f30Ssthen {
80712b2f30Ssthen /* f.e.:
81712b2f30Ssthen * [1187340064] unbound[24604:0] info: ul/rb.c:81 r_create malloc(12)
82712b2f30Ssthen * 0123456789 123456789 123456789 123456789
83712b2f30Ssthen * But now also:
84712b2f30Ssthen * Sep 16 15:18:20 unbound[1:0] info: ul/nh.c:143 memdup malloc(11)
85712b2f30Ssthen */
86712b2f30Ssthen if(strlen(line) < 32) /* up to 'info: ' */
87712b2f30Ssthen return 0;
88712b2f30Ssthen if(!strstr(line, " info: "))
89712b2f30Ssthen return 0;
90712b2f30Ssthen if(strstr(line, "info: stat "))
91712b2f30Ssthen return 0; /* skip the hex dumps */
92712b2f30Ssthen if(strstr(line+30, "malloc("))
93712b2f30Ssthen return 1;
94712b2f30Ssthen else if(strstr(line+30, "calloc("))
95712b2f30Ssthen return 1;
96712b2f30Ssthen /* skip reallocs */
97712b2f30Ssthen return 0;
98712b2f30Ssthen }
99712b2f30Ssthen
100712b2f30Ssthen /** find or alloc codeline in tree */
101712b2f30Ssthen static struct codeline*
get_codeline(rbtree_type * tree,char * key,char * func)102712b2f30Ssthen get_codeline(rbtree_type* tree, char* key, char* func)
103712b2f30Ssthen {
104712b2f30Ssthen struct codeline* cl = (struct codeline*)rbtree_search(tree, key);
105712b2f30Ssthen if(!cl) {
106712b2f30Ssthen cl = calloc(1, sizeof(*cl));
107712b2f30Ssthen if(!cl) return 0;
108712b2f30Ssthen cl->codeline = strdup(key);
109*c981f76fSsthen if(!cl->codeline) {
110*c981f76fSsthen free(cl);
111*c981f76fSsthen return 0;
112*c981f76fSsthen }
113712b2f30Ssthen cl->func = strdup(func);
114*c981f76fSsthen if(!cl->func) {
115*c981f76fSsthen free(cl->codeline);
116*c981f76fSsthen free(cl);
117*c981f76fSsthen return 0;
118*c981f76fSsthen }
119712b2f30Ssthen cl->alloc = 0;
120712b2f30Ssthen cl->node.key = cl->codeline;
121712b2f30Ssthen (void)rbtree_insert(tree, &cl->node);
122712b2f30Ssthen }
123712b2f30Ssthen return cl;
124712b2f30Ssthen }
125712b2f30Ssthen
126712b2f30Ssthen /** read up the malloc stats */
127712b2f30Ssthen static void
read_malloc_stat(char * line,rbtree_type * tree)128712b2f30Ssthen read_malloc_stat(char* line, rbtree_type* tree)
129712b2f30Ssthen {
130712b2f30Ssthen char codeline[10240];
131712b2f30Ssthen char name[10240];
132712b2f30Ssthen int skip = 0;
133712b2f30Ssthen long num = 0;
134712b2f30Ssthen struct codeline* cl = 0;
135712b2f30Ssthen line = strstr(line, "info: ")+6;
136712b2f30Ssthen if(sscanf(line, "%s %s %n", codeline, name, &skip) != 2) {
137712b2f30Ssthen printf("%s\n", line);
138712b2f30Ssthen fatal_exit("unhandled malloc");
139712b2f30Ssthen }
140712b2f30Ssthen if(sscanf(line+skip+7, "%ld", &num) != 1) {
141712b2f30Ssthen printf("%s\n%s\n", line, line+skip+7);
142712b2f30Ssthen fatal_exit("unhandled malloc");
143712b2f30Ssthen }
144712b2f30Ssthen cl = get_codeline(tree, codeline, name);
145712b2f30Ssthen if(!cl)
146712b2f30Ssthen fatal_exit("alloc failure");
147712b2f30Ssthen cl->alloc += num;
148712b2f30Ssthen cl->calls ++;
149712b2f30Ssthen }
150712b2f30Ssthen
151712b2f30Ssthen /** read up the calloc stats */
152712b2f30Ssthen static void
read_calloc_stat(char * line,rbtree_type * tree)153712b2f30Ssthen read_calloc_stat(char* line, rbtree_type* tree)
154712b2f30Ssthen {
155712b2f30Ssthen char codeline[10240];
156712b2f30Ssthen char name[10240];
157712b2f30Ssthen int skip = 0;
158712b2f30Ssthen long num = 0, sz = 0;
159712b2f30Ssthen struct codeline* cl = 0;
160712b2f30Ssthen line = strstr(line, "info: ")+6;
161712b2f30Ssthen if(sscanf(line, "%s %s %n", codeline, name, &skip) != 2) {
162712b2f30Ssthen printf("%s\n", line);
163712b2f30Ssthen fatal_exit("unhandled calloc");
164712b2f30Ssthen }
165712b2f30Ssthen if(sscanf(line+skip+7, "%ld, %ld", &num, &sz) != 2) {
166712b2f30Ssthen printf("%s\n%s\n", line, line+skip+7);
167712b2f30Ssthen fatal_exit("unhandled calloc");
168712b2f30Ssthen }
169712b2f30Ssthen
170712b2f30Ssthen cl = get_codeline(tree, codeline, name);
171712b2f30Ssthen if(!cl)
172712b2f30Ssthen fatal_exit("alloc failure");
173712b2f30Ssthen cl->alloc += num*sz;
174712b2f30Ssthen cl->calls ++;
175712b2f30Ssthen }
176712b2f30Ssthen
177712b2f30Ssthen /** get size of file */
178712b2f30Ssthen static off_t
get_file_size(const char * fname)179712b2f30Ssthen get_file_size(const char* fname)
180712b2f30Ssthen {
181712b2f30Ssthen struct stat s;
182712b2f30Ssthen if(stat(fname, &s) < 0) {
183712b2f30Ssthen fatal_exit("could not stat %s: %s", fname, strerror(errno));
184712b2f30Ssthen }
185712b2f30Ssthen return s.st_size;
186712b2f30Ssthen }
187712b2f30Ssthen
188712b2f30Ssthen /** read the logfile */
189712b2f30Ssthen static void
readfile(rbtree_type * tree,const char * fname)190712b2f30Ssthen readfile(rbtree_type* tree, const char* fname)
191712b2f30Ssthen {
192712b2f30Ssthen off_t total = get_file_size(fname);
193712b2f30Ssthen off_t done = (off_t)0;
194712b2f30Ssthen int report = 0;
195712b2f30Ssthen FILE* in = fopen(fname, "r");
196712b2f30Ssthen char buf[102400];
197712b2f30Ssthen if(!in)
198712b2f30Ssthen fatal_exit("could not open %s: %s", fname, strerror(errno));
199712b2f30Ssthen printf("Reading %s of size " ARG_LL "d\n", fname, (long long)total);
200712b2f30Ssthen while(fgets(buf, 102400, in)) {
201712b2f30Ssthen buf[102400-1] = 0;
202712b2f30Ssthen done += (off_t)strlen(buf);
203712b2f30Ssthen /* progress count */
204712b2f30Ssthen if((int)(((double)done / (double)total)*100.) > report) {
205712b2f30Ssthen report = (int)(((double)done / (double)total)*100.);
206712b2f30Ssthen fprintf(stderr, " %d%%", report);
207712b2f30Ssthen }
208712b2f30Ssthen
209712b2f30Ssthen if(!match(buf))
210712b2f30Ssthen continue;
211712b2f30Ssthen else if(strstr(buf+30, "malloc("))
212712b2f30Ssthen read_malloc_stat(buf, tree);
213712b2f30Ssthen else if(strstr(buf+30, "calloc("))
214712b2f30Ssthen read_calloc_stat(buf, tree);
215712b2f30Ssthen else {
216712b2f30Ssthen printf("%s\n", buf);
217712b2f30Ssthen fatal_exit("unhandled input");
218712b2f30Ssthen }
219712b2f30Ssthen }
220712b2f30Ssthen fprintf(stderr, " done\n");
221712b2f30Ssthen fclose(in);
222712b2f30Ssthen }
223712b2f30Ssthen
224712b2f30Ssthen /** print memory stats */
225712b2f30Ssthen static void
printstats(rbtree_type * tree)226712b2f30Ssthen printstats(rbtree_type* tree)
227712b2f30Ssthen {
228712b2f30Ssthen struct codeline* cl;
229712b2f30Ssthen uint64_t total = 0, tcalls = 0;
230712b2f30Ssthen RBTREE_FOR(cl, struct codeline*, tree) {
231712b2f30Ssthen printf("%12lld / %8lld in %s %s\n", (long long)cl->alloc,
232712b2f30Ssthen (long long)cl->calls, cl->codeline, cl->func);
233712b2f30Ssthen total += cl->alloc;
234712b2f30Ssthen tcalls += cl->calls;
235712b2f30Ssthen }
236712b2f30Ssthen printf("------------\n");
237712b2f30Ssthen printf("%12lld / %8lld total in %ld code lines\n", (long long)total,
238712b2f30Ssthen (long long)tcalls, (long)tree->count);
239712b2f30Ssthen printf("\n");
240712b2f30Ssthen }
241712b2f30Ssthen
242712b2f30Ssthen /** main program */
main(int argc,const char * argv[])243712b2f30Ssthen int main(int argc, const char* argv[])
244712b2f30Ssthen {
245712b2f30Ssthen rbtree_type* tree = 0;
246712b2f30Ssthen log_init(NULL, 0, 0);
247712b2f30Ssthen if(argc != 2) {
248712b2f30Ssthen usage();
249712b2f30Ssthen }
250712b2f30Ssthen tree = rbtree_create(codeline_cmp);
251712b2f30Ssthen if(!tree)
252712b2f30Ssthen fatal_exit("alloc failure");
253712b2f30Ssthen readfile(tree, argv[1]);
254712b2f30Ssthen printstats(tree);
255712b2f30Ssthen return 0;
256712b2f30Ssthen }
257