Fix statistics formatting.
[rsync.git] / memstats.c
1 /*
2  * Copyright (C) 2002 by Martin Pool
3  * 
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include "rsync.h"
20
21 /**
22  * @file memstats.c
23  *
24  * Show moderately high-level information about how much memory was
25  * used for different things.  The counting relies on various parts of
26  * the program updating counters as they use memory, so at first it
27  * will not be super accurate.
28  **/
29
30 /* TODO: Perhaps show these numbers with commas to make them a bit
31  * more readable. */
32
33 /**
34  * If our C library can get malloc statistics, then show them to FINFO
35  **/
36 void show_mem_stats(void)
37 {
38         extern struct stats stats;
39         
40         rprintf(FINFO, "Memory usage:\n"
41                 "  %10s\n"
42                 "  %10ld                               file_list\n"
43                 "  %10ld                                 flist_size\n"  
44                 "  %10ld                               hlink_list\n"
45                 "  %10ld                               file_structs (and associated data)\n"
46                 "  %10ld                               string_areas\n"
47                 "  %10ld                               exclude_structs\n"
48                 "  %10ld                               map_structs\n"
49                 "  %10ld                               tags\n"
50                 "  %10ld                               delete_list\n"
51                 ,
52                 "bytes",
53                 (long) stats.main_flist,
54                 (long) stats.flist_size,
55                 (long) stats.hlink_list,
56                 (long) stats.file_structs,
57                 (long) stats.string_areas,
58                 (long) stats.exclude_struct,
59                 (long) stats.map_struct,
60                 (long) stats.tags,
61                 (long) stats.delete_list);
62 }
63
64
65 void * malloc_counted(size_t howmuch, size_t *pot)
66 {
67         *pot += howmuch;
68         return malloc(howmuch);
69 }
70
71
72 /**
73  * If our C library can get malloc statistics, then show them to FINFO
74  **/
75 void show_malloc_stats(void)
76 {
77 #ifdef HAVE_MALLINFO
78         struct mallinfo mi;
79
80         mi = mallinfo();
81
82         rprintf(FINFO, "VM statistics:\n"
83                 "  %10s\n",
84                 "bytes");
85
86         rprintf(FINFO, "  %10ld                               from sbrk()\n",
87                 (long) mi.arena);
88         rprintf(FINFO, "  %10ld                               from mmap()\n",
89                 (long) mi.hblkhd);
90         rprintf(FINFO, "  %10ld                               in use\n",
91                 (long) mi.uordblks);
92         rprintf(FINFO, "  %10ld                               free in heap\n",
93                 (long) mi.fordblks);
94 #endif                          /* HAVE_MALLINFO */
95 }