]>
git.ipfire.org Git - thirdparty/bash.git/blob - lib/malloc/stats.c
1 /* stats.c - malloc statistics */
3 /* Copyright (C) 2001-2003 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne-Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
37 extern int malloc_free_blocks
__P((int));
39 extern struct _malstats _mstats
;
41 extern FILE *_imalloc_fopen
__P((char *, char *, char *, char *, size_t));
44 malloc_bucket_stats (size
)
47 struct bucket_stats v
;
51 if (size
< 0 || size
>= NBUCKETS
)
54 v
.nused
= v
.nmal
= v
.nmorecore
= v
.nlesscore
= v
.nsplit
= 0;
58 v
.blocksize
= 1 << (size
+ 3);
59 v
.nused
= _mstats
.nmalloc
[size
];
60 v
.nmal
= _mstats
.tmalloc
[size
];
61 v
.nmorecore
= _mstats
.nmorecore
[size
];
62 v
.nlesscore
= _mstats
.nlesscore
[size
];
63 v
.nsplit
= _mstats
.nsplit
[size
];
64 v
.ncoalesce
= _mstats
.ncoalesce
[size
];
66 v
.nfree
= malloc_free_blocks (size
); /* call back to malloc.c */
71 /* Return a copy of _MSTATS, with two additional fields filled in:
72 BYTESFREE is the total number of bytes on free lists. BYTESUSED
73 is the total number of bytes in use. These two fields are fairly
74 expensive to compute, so we do it only when asked to. */
78 struct _malstats result
;
79 struct bucket_stats v
;
83 result
.bytesused
= result
.bytesfree
= 0;
84 for (i
= 0; i
< NBUCKETS
; i
++)
86 v
= malloc_bucket_stats (i
);
87 result
.bytesfree
+= v
.nfree
* v
.blocksize
;
88 result
.bytesused
+= v
.nused
* v
.blocksize
;
94 _print_malloc_stats (s
, fp
)
99 unsigned long totused
, totfree
;
100 struct bucket_stats v
;
102 fprintf (fp
, "Memory allocation statistics: %s\n size\tfree\tin use\ttotal\tmorecore lesscore split\tcoalesce\n", s
? s
: "");
103 for (i
= totused
= totfree
= 0; i
< NBUCKETS
; i
++)
105 v
= malloc_bucket_stats (i
);
107 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
);
108 totfree
+= v
.nfree
* v
.blocksize
;
109 totused
+= v
.nused
* v
.blocksize
;
111 fprintf (fp
, "\nTotal bytes in use: %lu, total bytes free: %lu\n",
113 fprintf (fp
, "\nTotal bytes requested by application: %lu\n", (unsigned long)_mstats
.bytesreq
);
114 fprintf (fp
, "Total mallocs: %d, total frees: %d, total reallocs: %d (%d copies)\n",
115 _mstats
.nmal
, _mstats
.nfre
, _mstats
.nrealloc
, _mstats
.nrcopy
);
116 fprintf (fp
, "Total sbrks: %d, total bytes via sbrk: %d\n",
117 _mstats
.nsbrk
, _mstats
.tsbrk
);
118 fprintf (fp
, "Total blocks split: %d, total block coalesces: %d\n",
119 _mstats
.tbsplit
, _mstats
.tbcoalesce
);
123 print_malloc_stats (s
)
126 _print_malloc_stats (s
, stderr
);
130 fprint_malloc_stats (s
, fp
)
134 _print_malloc_stats (s
, fp
);
137 #define TRACEROOT "/var/tmp/maltrace/stats."
140 trace_malloc_stats (s
, fn
)
144 char defname
[sizeof (TRACEROOT
) + 64];
145 static char mallbuf
[1024];
147 fp
= _imalloc_fopen (s
, fn
, TRACEROOT
, defname
, sizeof (defname
));
150 setvbuf (fp
, mallbuf
, _IOFBF
, sizeof (mallbuf
));
151 _print_malloc_stats (s
, fp
);
157 #endif /* MALLOC_STATS */
159 #if defined (MALLOC_STATS) || defined (MALLOC_TRACE)
161 _imalloc_fopen (s
, fn
, def
, defbuf
, defsiz
)
175 sprintf (defbuf
, "%s%ld", def
, l
);
176 fp
= fopen(defbuf
, "w");
184 sprintf (pidbuf
, "%ld", l
);
185 if ((strlen (pidbuf
) + strlen (fn
) + 2) >= sizeof (fname
))
187 for (sp
= 0, p
= fname
, q
= fn
; *q
; )
189 if (sp
== 0 && *q
== '%' && q
[1] == 'p')
192 for (r
= pidbuf
; *r
; )
200 fp
= fopen (fname
, "w");
205 #endif /* MALLOC_STATS || MALLOC_TRACE */