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. */
30 struct _malstats _mstats;
33 malloc_bucket_stats (size)
36 struct bucket_stats v;
40 if (size < 0 || size >= NBUCKETS)
43 v.nused = v.nmal = v.nmorecore = v.nsplit = 0;
47 v.blocksize = 1 << (size + 3);
48 v.nused = _mstats.nmalloc[size];
49 v.nmal = _mstats.tmalloc[size];
50 v.nmorecore = _mstats.nmorecore[size];
51 v.nsplit = _mstats.nsplit[size];
53 v.nfree = malloc_free_blocks (size); /* call back to malloc.c */
58 /* Return a copy of _MSTATS, with two additional fields filled in:
59 BYTESFREE is the total number of bytes on free lists. BYTESUSED
60 is the total number of bytes in use. These two fields are fairly
61 expensive to compute, so we do it only when asked to. */
65 struct _malstats result;
66 struct bucket_stats v;
70 result.bytesused = result.bytesfree = 0;
71 for (i = 0; i < NBUCKETS; i++)
73 v = malloc_bucket_stats (i);
74 result.bytesfree += v.nfree * v.blocksize;
75 result.bytesused += v.nused * v.blocksize;
81 _print_malloc_stats (s, fp)
86 unsigned long totused, totfree;
87 struct bucket_stats v;
89 fprintf (fp, "Memory allocation statistics: %s\n\tsize\tfree\tin use\ttotal\tmorecore\tsplit\n", s ? s : "");
90 for (i = totused = totfree = 0; i < NBUCKETS; i++)
92 v = malloc_bucket_stats (i);
93 fprintf (fp, "%12lu\t%4d\t%6d\t%5d\t%8d\t%5d\n", (unsigned long)v.blocksize, v.nfree, v.nused, v.nmal, v.nmorecore, v.nsplit);
94 totfree += v.nfree * v.blocksize;
95 totused += v.nused * v.blocksize;
97 fprintf (fp, "\nTotal bytes in use: %lu, total bytes free: %lu\n",
99 fprintf (fp, "Total mallocs: %d, total frees: %d, total reallocs: %d (%d copies)\n",
100 _mstats.nmal, _mstats.nfre, _mstats.nrealloc, _mstats.nrcopy);
101 fprintf (fp, "Total sbrks: %d, total bytes via sbrk: %d\n",
102 _mstats.nsbrk, _mstats.tsbrk);
103 fprintf (fp, "Total blocks split: %d, total block coalesces: %d\n",
104 _mstats.tbsplit, _mstats.tbcoalesce);
108 print_malloc_stats (s)
111 _print_malloc_stats (s, stderr);
115 fprint_malloc_stats (s, fp)
119 _print_malloc_stats (s, fp);
122 #define TRACEROOT "/var/tmp/maltrace/trace."
123 extern char *inttostr ();
126 trace_malloc_stats (s)
135 ip = inttostr(p, ibuf, sizeof(ibuf));
136 strcpy (fname, TRACEROOT);
138 fp = fopen(fname, "w");
141 _print_malloc_stats (s, fp);
147 #endif /* MALLOC_STATS */