xref: /netbsd-src/external/bsd/unbound/dist/testcode/memstats.c (revision 01049ae6d55a7fce6c6379cd1e0c997c04dc0061)
13b6c3722Schristos /*
23b6c3722Schristos  * testcode/memstats.c - debug tool to show memory allocation statistics.
33b6c3722Schristos  *
43b6c3722Schristos  * Copyright (c) 2007, NLnet Labs. All rights reserved.
53b6c3722Schristos  *
63b6c3722Schristos  * This software is open source.
73b6c3722Schristos  *
83b6c3722Schristos  * Redistribution and use in source and binary forms, with or without
93b6c3722Schristos  * modification, are permitted provided that the following conditions
103b6c3722Schristos  * are met:
113b6c3722Schristos  *
123b6c3722Schristos  * Redistributions of source code must retain the above copyright notice,
133b6c3722Schristos  * this list of conditions and the following disclaimer.
143b6c3722Schristos  *
153b6c3722Schristos  * Redistributions in binary form must reproduce the above copyright notice,
163b6c3722Schristos  * this list of conditions and the following disclaimer in the documentation
173b6c3722Schristos  * and/or other materials provided with the distribution.
183b6c3722Schristos  *
193b6c3722Schristos  * Neither the name of the NLNET LABS nor the names of its contributors may
203b6c3722Schristos  * be used to endorse or promote products derived from this software without
213b6c3722Schristos  * specific prior written permission.
223b6c3722Schristos  *
233b6c3722Schristos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
243b6c3722Schristos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
253b6c3722Schristos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
263b6c3722Schristos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
273b6c3722Schristos  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
283b6c3722Schristos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
293b6c3722Schristos  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
303b6c3722Schristos  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
313b6c3722Schristos  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
323b6c3722Schristos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
333b6c3722Schristos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
343b6c3722Schristos  */
353b6c3722Schristos 
363b6c3722Schristos /**
373b6c3722Schristos  * \file
383b6c3722Schristos  *
393b6c3722Schristos  * This program reads a log file and prints the memory allocation summed
403b6c3722Schristos  * up.
413b6c3722Schristos  */
423b6c3722Schristos #include "config.h"
433b6c3722Schristos #include "util/log.h"
443b6c3722Schristos #include "util/rbtree.h"
453b6c3722Schristos #include "util/locks.h"
463b6c3722Schristos #include "util/fptr_wlist.h"
473b6c3722Schristos #include <sys/stat.h>
483b6c3722Schristos 
493b6c3722Schristos /**
503b6c3722Schristos  * The allocation statistics block
513b6c3722Schristos  */
523b6c3722Schristos struct codeline {
533b6c3722Schristos 	/** rbtree node */
540cd9f4ecSchristos 	rbnode_type node;
553b6c3722Schristos 	/** the name of the file:linenumber */
563b6c3722Schristos 	char* codeline;
573b6c3722Schristos 	/** the name of the function */
583b6c3722Schristos 	char* func;
593b6c3722Schristos 	/** number of bytes allocated */
603b6c3722Schristos 	uint64_t alloc;
613b6c3722Schristos 	/** number of bytes freed */
623b6c3722Schristos 	uint64_t free;
633b6c3722Schristos 	/** number allocations and frees */
643b6c3722Schristos 	uint64_t calls;
653b6c3722Schristos };
663b6c3722Schristos 
673b6c3722Schristos /** print usage and exit */
683b6c3722Schristos static void
usage(void)690cd9f4ecSchristos usage(void)
703b6c3722Schristos {
713b6c3722Schristos 	printf("usage:	memstats <logfile>\n");
723b6c3722Schristos 	printf("statistics are printed on stdout.\n");
733b6c3722Schristos 	exit(1);
743b6c3722Schristos }
753b6c3722Schristos 
763b6c3722Schristos /** match logfile line to see if it needs accounting processing */
773b6c3722Schristos static int
match(char * line)783b6c3722Schristos match(char* line)
793b6c3722Schristos {
803b6c3722Schristos 	/* f.e.:
813b6c3722Schristos 	 * [1187340064] unbound[24604:0] info: ul/rb.c:81 r_create malloc(12)
823b6c3722Schristos 	 * 0123456789 123456789 123456789 123456789
833b6c3722Schristos 	 * But now also:
843b6c3722Schristos 	 * Sep 16 15:18:20 unbound[1:0] info: ul/nh.c:143 memdup malloc(11)
853b6c3722Schristos 	 */
863b6c3722Schristos 	if(strlen(line) < 32) /* up to 'info: ' */
873b6c3722Schristos 		return 0;
883b6c3722Schristos 	if(!strstr(line, " info: "))
893b6c3722Schristos 		return 0;
903b6c3722Schristos 	if(strstr(line, "info: stat "))
913b6c3722Schristos 		return 0; /* skip the hex dumps */
923b6c3722Schristos 	if(strstr(line+30, "malloc("))
933b6c3722Schristos 		return 1;
943b6c3722Schristos 	else if(strstr(line+30, "calloc("))
953b6c3722Schristos 		return 1;
963b6c3722Schristos 	/* skip reallocs */
973b6c3722Schristos 	return 0;
983b6c3722Schristos }
993b6c3722Schristos 
1003b6c3722Schristos /** find or alloc codeline in tree */
1013b6c3722Schristos static struct codeline*
get_codeline(rbtree_type * tree,char * key,char * func)1020cd9f4ecSchristos get_codeline(rbtree_type* tree, char* key, char* func)
1033b6c3722Schristos {
1043b6c3722Schristos 	struct codeline* cl = (struct codeline*)rbtree_search(tree, key);
1053b6c3722Schristos 	if(!cl) {
1063b6c3722Schristos 		cl = calloc(1, sizeof(*cl));
1073b6c3722Schristos 		if(!cl) return 0;
1083b6c3722Schristos 		cl->codeline = strdup(key);
109*01049ae6Schristos 		if(!cl->codeline) {
110*01049ae6Schristos 			free(cl);
111*01049ae6Schristos 			return 0;
112*01049ae6Schristos 		}
1133b6c3722Schristos 		cl->func = strdup(func);
114*01049ae6Schristos 		if(!cl->func) {
115*01049ae6Schristos 			free(cl->codeline);
116*01049ae6Schristos 			free(cl);
117*01049ae6Schristos 			return 0;
118*01049ae6Schristos 		}
1193b6c3722Schristos 		cl->alloc = 0;
1203b6c3722Schristos 		cl->node.key = cl->codeline;
1213b6c3722Schristos 		(void)rbtree_insert(tree, &cl->node);
1223b6c3722Schristos 	}
1233b6c3722Schristos 	return cl;
1243b6c3722Schristos }
1253b6c3722Schristos 
1263b6c3722Schristos /** read up the malloc stats */
1273b6c3722Schristos static void
read_malloc_stat(char * line,rbtree_type * tree)1280cd9f4ecSchristos read_malloc_stat(char* line, rbtree_type* tree)
1293b6c3722Schristos {
1303b6c3722Schristos 	char codeline[10240];
1313b6c3722Schristos 	char name[10240];
1323b6c3722Schristos 	int skip = 0;
1333b6c3722Schristos 	long num = 0;
1343b6c3722Schristos 	struct codeline* cl = 0;
1353b6c3722Schristos 	line = strstr(line, "info: ")+6;
1363b6c3722Schristos 	if(sscanf(line, "%s %s %n", codeline, name, &skip) != 2) {
1373b6c3722Schristos 		printf("%s\n", line);
1383b6c3722Schristos 		fatal_exit("unhandled malloc");
1393b6c3722Schristos 	}
1403b6c3722Schristos 	if(sscanf(line+skip+7, "%ld", &num) != 1) {
1413b6c3722Schristos 		printf("%s\n%s\n", line, line+skip+7);
1423b6c3722Schristos 		fatal_exit("unhandled malloc");
1433b6c3722Schristos 	}
1443b6c3722Schristos 	cl = get_codeline(tree, codeline, name);
1453b6c3722Schristos 	if(!cl)
1463b6c3722Schristos 		fatal_exit("alloc failure");
1473b6c3722Schristos 	cl->alloc += num;
1483b6c3722Schristos 	cl->calls ++;
1493b6c3722Schristos }
1503b6c3722Schristos 
1513b6c3722Schristos /** read up the calloc stats */
1523b6c3722Schristos static void
read_calloc_stat(char * line,rbtree_type * tree)1530cd9f4ecSchristos read_calloc_stat(char* line, rbtree_type* tree)
1543b6c3722Schristos {
1553b6c3722Schristos 	char codeline[10240];
1563b6c3722Schristos 	char name[10240];
1573b6c3722Schristos 	int skip = 0;
1583b6c3722Schristos 	long num = 0, sz = 0;
1593b6c3722Schristos 	struct codeline* cl = 0;
1603b6c3722Schristos 	line = strstr(line, "info: ")+6;
1613b6c3722Schristos 	if(sscanf(line, "%s %s %n", codeline, name, &skip) != 2) {
1623b6c3722Schristos 		printf("%s\n", line);
1633b6c3722Schristos 		fatal_exit("unhandled calloc");
1643b6c3722Schristos 	}
1653b6c3722Schristos 	if(sscanf(line+skip+7, "%ld, %ld", &num, &sz) != 2) {
1663b6c3722Schristos 		printf("%s\n%s\n", line, line+skip+7);
1673b6c3722Schristos 		fatal_exit("unhandled calloc");
1683b6c3722Schristos 	}
1693b6c3722Schristos 
1703b6c3722Schristos 	cl = get_codeline(tree, codeline, name);
1713b6c3722Schristos 	if(!cl)
1723b6c3722Schristos 		fatal_exit("alloc failure");
1733b6c3722Schristos 	cl->alloc += num*sz;
1743b6c3722Schristos 	cl->calls ++;
1753b6c3722Schristos }
1763b6c3722Schristos 
1773b6c3722Schristos /** get size of file */
1783b6c3722Schristos static off_t
get_file_size(const char * fname)1793b6c3722Schristos get_file_size(const char* fname)
1803b6c3722Schristos {
1813b6c3722Schristos 	struct stat s;
1823b6c3722Schristos 	if(stat(fname, &s) < 0) {
1833b6c3722Schristos 		fatal_exit("could not stat %s: %s", fname, strerror(errno));
1843b6c3722Schristos 	}
1853b6c3722Schristos 	return s.st_size;
1863b6c3722Schristos }
1873b6c3722Schristos 
1883b6c3722Schristos /** read the logfile */
1893b6c3722Schristos static void
readfile(rbtree_type * tree,const char * fname)1900cd9f4ecSchristos readfile(rbtree_type* tree, const char* fname)
1913b6c3722Schristos {
1923b6c3722Schristos 	off_t total = get_file_size(fname);
1933b6c3722Schristos 	off_t done = (off_t)0;
1943b6c3722Schristos 	int report = 0;
1953b6c3722Schristos 	FILE* in = fopen(fname, "r");
1963b6c3722Schristos 	char buf[102400];
1973b6c3722Schristos 	if(!in)
1983b6c3722Schristos 		fatal_exit("could not open %s: %s", fname, strerror(errno));
1993b6c3722Schristos 	printf("Reading %s of size " ARG_LL "d\n", fname, (long long)total);
2003b6c3722Schristos 	while(fgets(buf, 102400, in)) {
2013b6c3722Schristos 		buf[102400-1] = 0;
2023b6c3722Schristos 		done += (off_t)strlen(buf);
2033b6c3722Schristos 		/* progress count */
2043b6c3722Schristos 		if((int)(((double)done / (double)total)*100.) > report) {
2053b6c3722Schristos 			report = (int)(((double)done / (double)total)*100.);
2063b6c3722Schristos 			fprintf(stderr, " %d%%", report);
2073b6c3722Schristos 		}
2083b6c3722Schristos 
2093b6c3722Schristos 		if(!match(buf))
2103b6c3722Schristos 			continue;
2113b6c3722Schristos 		else if(strstr(buf+30, "malloc("))
2123b6c3722Schristos 			read_malloc_stat(buf, tree);
2133b6c3722Schristos 		else if(strstr(buf+30, "calloc("))
2143b6c3722Schristos 			read_calloc_stat(buf, tree);
2153b6c3722Schristos 		else {
2163b6c3722Schristos 			printf("%s\n", buf);
2173b6c3722Schristos 			fatal_exit("unhandled input");
2183b6c3722Schristos 		}
2193b6c3722Schristos 	}
2203b6c3722Schristos 	fprintf(stderr, " done\n");
2213b6c3722Schristos 	fclose(in);
2223b6c3722Schristos }
2233b6c3722Schristos 
2243b6c3722Schristos /** print memory stats */
2253b6c3722Schristos static void
printstats(rbtree_type * tree)2260cd9f4ecSchristos printstats(rbtree_type* tree)
2273b6c3722Schristos {
2283b6c3722Schristos 	struct codeline* cl;
2293b6c3722Schristos 	uint64_t total = 0, tcalls = 0;
2303b6c3722Schristos 	RBTREE_FOR(cl, struct codeline*, tree) {
2313b6c3722Schristos 		printf("%12lld / %8lld in %s %s\n", (long long)cl->alloc,
2323b6c3722Schristos 			(long long)cl->calls, cl->codeline, cl->func);
2333b6c3722Schristos 		total += cl->alloc;
2343b6c3722Schristos 		tcalls += cl->calls;
2353b6c3722Schristos 	}
2363b6c3722Schristos 	printf("------------\n");
2373b6c3722Schristos 	printf("%12lld / %8lld total in %ld code lines\n", (long long)total,
2383b6c3722Schristos 		(long long)tcalls, (long)tree->count);
2393b6c3722Schristos 	printf("\n");
2403b6c3722Schristos }
2413b6c3722Schristos 
2423b6c3722Schristos /** main program */
main(int argc,const char * argv[])2433b6c3722Schristos int main(int argc, const char* argv[])
2443b6c3722Schristos {
2450cd9f4ecSchristos 	rbtree_type* tree = 0;
2460cd9f4ecSchristos 	log_init(NULL, 0, 0);
2473b6c3722Schristos 	if(argc != 2) {
2483b6c3722Schristos 		usage();
2493b6c3722Schristos 	}
2503b6c3722Schristos 	tree = rbtree_create(codeline_cmp);
2513b6c3722Schristos 	if(!tree)
2523b6c3722Schristos 		fatal_exit("alloc failure");
2533b6c3722Schristos 	readfile(tree, argv[1]);
2543b6c3722Schristos 	printstats(tree);
2553b6c3722Schristos 	return 0;
2563b6c3722Schristos }
257