48fba1bad48e6404caff5810c97b09df55ed5031
[platform/upstream/bash.git] / lib / malloc / stats.c
1 /* stats.c - malloc statistics */
2
3 /*  Copyright (C) 2001 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 #include "mstats.h"
29
30 struct _malstats _mstats;
31
32 struct bucket_stats
33 malloc_bucket_stats (size)
34      int size;
35 {
36   struct bucket_stats v;
37
38   v.nfree = 0;
39
40   if (size < 0 || size >= NBUCKETS)
41     {
42       v.blocksize = 0;
43       v.nused = v.nmal = v.nmorecore = v.nsplit = 0;
44       return v;
45     }
46
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];
52
53   v.nfree = malloc_free_blocks (size);  /* call back to malloc.c */
54
55   return v;
56 }
57
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. */
62 struct _malstats
63 malloc_stats ()
64 {
65   struct _malstats result;
66   struct bucket_stats v;
67   register int i;
68
69   result = _mstats;
70   result.bytesused = result.bytesfree = 0;
71   for (i = 0; i < NBUCKETS; i++)
72     {
73       v = malloc_bucket_stats (i);
74       result.bytesfree += v.nfree * v.blocksize;
75       result.bytesused += v.nused * v.blocksize;
76     }
77   return (result);
78 }
79
80 static void
81 _print_malloc_stats (s, fp)
82      char *s;
83      FILE *fp;
84 {
85   register int i;
86   unsigned long totused, totfree;
87   struct bucket_stats v;
88
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++)
91     {
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;
96     }
97   fprintf (fp, "\nTotal bytes in use: %lu, total bytes free: %lu\n",
98            totused, totfree);
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);
105 }
106
107 void
108 print_malloc_stats (s)
109      char *s;
110 {
111   _print_malloc_stats (s, stderr);
112 }
113
114 void
115 fprint_malloc_stats (s, fp)
116      char *s;
117      FILE *fp;
118 {
119   _print_malloc_stats (s, fp);
120 }
121
122 #define TRACEROOT "/var/tmp/maltrace/trace."
123 extern char *inttostr ();
124
125 void
126 trace_malloc_stats (s)
127      char *s;
128 {
129   char ibuf[32], *ip;
130   char fname[64];
131   long p;
132   FILE *fp;
133
134   p = getpid();
135   ip = inttostr(p, ibuf, sizeof(ibuf));
136   strcpy (fname, TRACEROOT);
137   strcat (fname, ip);
138   fp = fopen(fname, "w");
139   if (fp)
140     {
141       _print_malloc_stats (s, fp);
142       fflush(fp);
143       fclose(fp);
144     }
145 }
146
147 #endif /* MALLOC_STATS */