| 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 |
|
|---|
| 34 | extern int malloc_free_blocks __P((int));
|
|---|
| 35 |
|
|---|
| 36 | extern struct _malstats _mstats;
|
|---|
| 37 |
|
|---|
| 38 | extern FILE *_imalloc_fopen __P((char *, char *, char *, char *, size_t));
|
|---|
| 39 |
|
|---|
| 40 | struct bucket_stats
|
|---|
| 41 | malloc_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. */
|
|---|
| 72 | struct _malstats
|
|---|
| 73 | malloc_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 |
|
|---|
| 90 | static void
|
|---|
| 91 | _print_malloc_stats (s, fp)
|
|---|
|
|---|