source: vendor/bash/3.1/lib/malloc/stats.c

Last change on this file was 3228, checked in by bird, 18 years ago

bash 3.1

File size: 4.9 KB
Line 
1/* stats.c - malloc statistics */
2
3/* Copyright (C) 2001-2003 Free Software Foundation, Inc.
4
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)
8 any later version.
9
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.
14
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. */
18
19#ifdef HAVE_CONFIG_H
20# include <config.h>
21#endif
22
23#include "imalloc.h"
24
25#ifdef MALLOC_STATS
26
27#include <stdio.h>
28#ifdef HAVE_UNISTD_H
29# include <unistd.h>
30#endif
31
32#include "mstats.h"
33
34extern int malloc_free_blocks __P((int));
35
36extern struct _malstats _mstats;
37
38extern FILE *_imalloc_fopen __P((char *, char *, char *, char *, size_t));
39
40struct bucket_stats
41malloc_bucket_stats (size)
42 int size;
43{
44 struct bucket_stats v;
45
46 v.nfree = 0;
47
48 if (size < 0 || size >= NBUCKETS)
49 {
50 v.blocksize = 0;
51 v.nused = v.nmal = v.nmorecore = v.nlesscore = v.nsplit = 0;
52 return v;
53 }
54
55 v.blocksize = 1 << (size + 3);
56 v.nused = _mstats.nmalloc[size];
57 v.nmal = _mstats.tmalloc[size];
58 v.nmorecore = _mstats.nmorecore[size];
59 v.nlesscore = _mstats.nlesscore[size];
60 v.nsplit = _mstats.nsplit[size];
61 v.ncoalesce = _mstats.ncoalesce[size];
62
63 v.nfree = malloc_free_blocks (size); /* call back to malloc.c */
64
65 return v;
66}
67
68/* Return a copy of _MSTATS, with two additional fields filled in:
69 BYTESFREE is the total number of bytes on free lists. BYTESUSED
70 is the total number of bytes in use. These two fields are fairly
71 expensive to compute, so we do it only when asked to. */
72struct _malstats
73malloc_stats ()
74{
75 struct _malstats result;
76 struct bucket_stats v;
77 register int i;
78
79 result = _mstats;
80 result.bytesused = result.bytesfree = 0;
81 for (i = 0; i < NBUCKETS; i++)
82 {
83 v = malloc_bucket_stats (i);
84 result.bytesfree += v.nfree * v.blocksize;
85 result.bytesused += v.nused * v.blocksize;
86 }
87 return (result);
88}
89
90static void
91_print_malloc_stats (s, fp)
92 char *s;
93 FILE *fp;
94{
95 register int i;
96 unsigned long totused, totfree;
97 struct bucket_stats v;
98
99 fprintf (fp, "Memory allocation statistics: %s\n size\tfree\tin use\ttotal\tmorecore lesscore split\tcoalesce\n", s ? s : "");
100 for (i = totused = totfree = 0; i < NBUCKETS; i++)
101 {
102 v = malloc_bucket_stats (i);
103 if (v.nmal > 0)
104 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);
105 totfree += v.nfree * v.blocksize;
106 totused += v.nused * v.blocksize;
107 }
108 fprintf (fp, "\nTotal bytes in use: %lu, total bytes free: %lu\n",
109 totused, totfree);
110 fprintf (fp, "\nTotal bytes requested by application: %lu\n", _mstats.bytesreq);
111 fprintf (fp, "Total mallocs: %d, total frees: %d, total reallocs: %d (%d copies)\n",
112 _mstats.nmal, _mstats.nfre, _mstats.nrealloc, _mstats.nrcopy);
113 fprintf (fp, "Total sbrks: %d, total bytes via sbrk: %d\n",
114 _mstats.nsbrk, _mstats.tsbrk);
115 fprintf (fp, "Total blocks split: %d, total block coalesces: %d\n",
116 _mstats.tbsplit, _mstats.tbcoalesce);
117}
118
119void
120print_malloc_stats (s)
121 char *s;
122{
123 _print_malloc_stats (s, stderr);
124}
125
126void
127fprint_malloc_stats (s, fp)
128 char *s;
129 FILE *fp;
130{
131 _print_malloc_stats (s, fp);
132}
133
134#define TRACEROOT "/var/tmp/maltrace/stats."
135
136void
137trace_malloc_stats (s, fn)
138 char *s, *fn;
139{
140 FILE *fp;
141 char defname[sizeof (TRACEROOT) + 64];
142 static char mallbuf[1024];
143
144 fp = _imalloc_fopen (s, fn, TRACEROOT, defname, sizeof (defname));
145 if (fp)
146 {
147 setvbuf (fp, mallbuf, _IOFBF, sizeof (mallbuf));
148 _print_malloc_stats (s, fp);
149 fflush(fp);
150 fclose(fp);
151 }
152}
153
154#endif /* MALLOC_STATS */
155
156#if defined (MALLOC_STATS) || defined (MALLOC_TRACE)
157FILE *
158_imalloc_fopen (s, fn, def, defbuf, defsiz)
159 char *s;
160 char *fn;
161 char *def;
162 char *defbuf;
163 size_t defsiz;
164{
165 char fname[1024];
166 long l;
167 FILE *fp;
168
169 l = (long)getpid ();
170 if (fn == 0)
171 {
172 sprintf (defbuf, "%s%ld", def, l);
173 fp = fopen(defbuf, "w");
174 }
175 else
176 {
177 char *p, *q, *r;
178 char pidbuf[32];
179 int sp;
180
181 sprintf (pidbuf, "%ld", l);
182 if ((strlen (pidbuf) + strlen (fn) + 2) >= sizeof (fname))
183 return;
184 for (sp = 0, p = fname, q = fn; *q; )
185 {
186 if (sp == 0 && *q == '%' && q[1] == 'p')
187 {
188 sp = 1;
189 for (r = pidbuf; *r; )
190 *p++ = *r++;
191 q += 2;
192 }
193 else
194 *p++ = *q++;
195 }
196 *p = '\0';
197 fp = fopen (fname, "w");
198 }
199
200 return fp;
201}
202#endif /* MALLOC_STATS || MALLOC_TRACE */
Note: See TracBrowser for help on using the repository browser.