1 /* stats.c - malloc statistics */
3 /* Copyright (C) 2001 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
34 extern int malloc_free_blocks __P((int));
36 extern struct _malstats _mstats;
39 malloc_bucket_stats (size)
42 struct bucket_stats v;
46 if (size < 0 || size >= NBUCKETS)
49 v.nused = v.nmal = v.nmorecore = v.nlesscore = v.nsplit = 0;
53 v.blocksize = 1 << (size + 3);
54 v.nused = _mstats.nmalloc[size];
55 v.nmal = _mstats.tmalloc[size];
56 v.nmorecore = _mstats.nmorecore[size];
57 v.nlesscore = _mstats.nlesscore[size];
58 v.nsplit = _mstats.nsplit[size];
59 v.ncoalesce = _mstats.ncoalesce[size];
61 v.nfree = malloc_free_blocks (size); /* call back to malloc.c */
66 /* Return a copy of _MSTATS, with two additional fields filled in:
67 BYTESFREE is the total number of bytes on free lists. BYTESUSED
68 is the total number of bytes in use. These two fields are fairly
69 expensive to compute, so we do it only when asked to. */
73 struct _malstats result;
74 struct bucket_stats v;
78 result.bytesused = result.bytesfree = 0;
79 for (i = 0; i < NBUCKETS; i++)
81 v = malloc_bucket_stats (i);
82 result.bytesfree += v.nfree * v.blocksize;
83 result.bytesused += v.nused * v.blocksize;
89 _print_malloc_stats (s, fp)
94 unsigned long totused, totfree;
95 struct bucket_stats v;
97 fprintf (fp, "Memory allocation statistics: %s\n size\tfree\tin use\ttotal\tmorecore lesscore split\tcoalesce\n", s ? s : "");
98 for (i = totused = totfree = 0; i < NBUCKETS; i++)
100 v = malloc_bucket_stats (i);
102 fprintf (fp, "%8lu\t%4d\t%6d\t%5d\t%8d\t%d %5d %8d\n", (unsigned long)v.blocksize, v.nfree, v.nused, v.nmal, v.nmorecore, v.nlesscore, v.nsplit, v.ncoalesce);
103 totfree += v.nfree * v.blocksize;
104 totused += v.nused * v.blocksize;
106 fprintf (fp, "\nTotal bytes in use: %lu, total bytes free: %lu\n",
108 fprintf (fp, "\nTotal bytes requested by application: %lu\n", _mstats.bytesreq);
109 fprintf (fp, "Total mallocs: %d, total frees: %d, total reallocs: %d (%d copies)\n",
110 _mstats.nmal, _mstats.nfre, _mstats.nrealloc, _mstats.nrcopy);
111 fprintf (fp, "Total sbrks: %d, total bytes via sbrk: %d\n",
112 _mstats.nsbrk, _mstats.tsbrk);
113 fprintf (fp, "Total blocks split: %d, total block coalesces: %d\n",
114 _mstats.tbsplit, _mstats.tbcoalesce);
118 print_malloc_stats (s)
121 _print_malloc_stats (s, stderr);
125 fprint_malloc_stats (s, fp)
129 _print_malloc_stats (s, fp);
132 #define TRACEROOT "/var/tmp/maltrace/trace."
133 static char mallbuf[1024];
136 trace_malloc_stats (s, fn)
139 char defname[sizeof (TRACEROOT) + 64];
147 sprintf (defname, "%s%ld", TRACEROOT, l);
148 fp = fopen(defname, "w");
156 sprintf (pidbuf, "%ld", l);
157 if ((strlen (pidbuf) + strlen (fn) + 2) >= sizeof (fname))
159 for (sp = 0, p = fname, q = fn; *q; )
161 if (sp == 0 && *q == '%' && q[1] == 'p')
164 for (r = pidbuf; *r; )
172 fp = fopen (fname, "w");
177 setvbuf (fp, mallbuf, _IOFBF, sizeof (mallbuf));
178 _print_malloc_stats (s, fp);
184 #endif /* MALLOC_STATS */