9be1bf58a3c3168312a5bd90fbce884fea7b1f1f
[bbaumbach/samba-autobuild/.git] / source3 / lib / tallocmsg.c
1 /* 
2    samba -- Unix SMB/CIFS implementation.
3    Copyright (C) 2001, 2002 by Martin Pool
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 3 of the License, or
8    (at your option) 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, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "includes.h"
20 #include "messages.h"
21 #include "lib/util/talloc_report.h"
22 #ifdef HAVE_MALLINFO
23 #include <malloc.h>
24 #endif /* HAVE_MALLINFO */
25
26  /**
27  * Prepare memory allocation report based on mallinfo()
28  **/
29 static char *get_mallinfo_report(void *mem_ctx)
30 {
31         char *report = NULL;
32 #ifdef HAVE_MALLINFO
33         struct mallinfo mi;
34
35         mi = mallinfo();
36         report = talloc_asprintf(mem_ctx,
37                                  "mallinfo:\n"
38                                  "    arena: %d\n"
39                                  "    ordblks: %d\n"
40                                  "    smblks: %d\n"
41                                  "    hblks: %d\n"
42                                  "    hblkhd: %d\n"
43                                  "    usmblks: %d\n"
44                                  "    fsmblks: %d\n"
45                                  "    uordblks: %d\n"
46                                  "    fordblks: %d\n"
47                                  "    keepcost: %d\n",
48                                  mi.arena,
49                                  mi.ordblks,
50                                  mi.smblks,
51                                  mi.hblks,
52                                  mi.hblkhd,
53                                  mi.usmblks,
54                                  mi.fsmblks,
55                                  mi.uordblks,
56                                  mi.fordblks,
57                                  mi.keepcost);
58 #endif /* HAVE_MALLINFO */
59
60         return report;
61 }
62 /**
63  * Respond to a POOL_USAGE message by sending back string form of memory
64  * usage stats.
65  **/
66 static void msg_pool_usage(struct messaging_context *msg_ctx,
67                            void *private_data, 
68                            uint32_t msg_type, 
69                            struct server_id src,
70                            DATA_BLOB *data)
71 {
72         char *report = NULL;
73         char *mreport = NULL;
74         int iov_size = 0;
75         struct iovec iov[2];
76
77         SMB_ASSERT(msg_type == MSG_REQ_POOL_USAGE);
78
79         DEBUG(2,("Got POOL_USAGE\n"));
80
81         report = talloc_report_str(msg_ctx, NULL);
82         if (report != NULL) {
83                 iov[iov_size].iov_base = report;
84                 iov[iov_size].iov_len = talloc_get_size(report) - 1;
85                 iov_size++;
86         }
87
88         mreport = get_mallinfo_report(msg_ctx);
89         if (mreport != NULL) {
90                 iov[iov_size].iov_base = mreport;
91                 iov[iov_size].iov_len = talloc_get_size(mreport) - 1;
92                 iov_size++;
93         }
94
95         if (iov_size) {
96                 messaging_send_iov(msg_ctx,
97                                    src,
98                                    MSG_POOL_USAGE,
99                                    iov,
100                                    iov_size,
101                                    NULL,
102                                    0);
103         }
104
105         TALLOC_FREE(report);
106         TALLOC_FREE(mreport);
107 }
108
109 /**
110  * Register handler for MSG_REQ_POOL_USAGE
111  **/
112 void register_msg_pool_usage(struct messaging_context *msg_ctx)
113 {
114         messaging_register(msg_ctx, NULL, MSG_REQ_POOL_USAGE, msg_pool_usage);
115         DEBUG(2, ("Registered MSG_REQ_POOL_USAGE\n"));
116 }