s3-messages: only include messages.h where needed.
[idra/samba.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 "librpc/gen_ndr/messaging.h"
21 #include "messages.h"
22
23 /**
24  * @file tallocmsg.c
25  *
26  * Glue code between talloc profiling and the Samba messaging system.
27  **/
28
29 struct msg_pool_usage_state {
30         TALLOC_CTX *mem_ctx;
31         ssize_t len;
32         size_t buflen;
33         char *s;
34 };
35
36 static void msg_pool_usage_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_s)
37 {
38         const char *name = talloc_get_name(ptr);
39         struct msg_pool_usage_state *state = (struct msg_pool_usage_state *)_s;
40
41         if (is_ref) {
42                 sprintf_append(state->mem_ctx, &state->s, &state->len, &state->buflen,
43                                "%*sreference to: %s\n", depth*4, "", name);
44                 return;
45         }
46
47         if (depth == 0) {
48                 sprintf_append(state->mem_ctx, &state->s, &state->len, &state->buflen,
49                                "%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n", 
50                                (max_depth < 0 ? "full " :""), name,
51                                (unsigned long)talloc_total_size(ptr),
52                                (unsigned long)talloc_total_blocks(ptr));
53                 return;
54         }
55
56         if (strcmp(name, "char") == 0) {
57                 /*
58                  * Print out the first 50 bytes of the string
59                  */
60                 sprintf_append(state->mem_ctx, &state->s, &state->len,
61                                &state->buflen,
62                                "%*s%-30s contains %6lu bytes in %3lu blocks "
63                                "(ref %d): %*s\n", depth*4, "",
64                                name,
65                                (unsigned long)talloc_total_size(ptr),
66                                (unsigned long)talloc_total_blocks(ptr),
67                                talloc_reference_count(ptr),
68                                MIN(50, talloc_get_size(ptr)),
69                                (char *)ptr);
70                 return;
71         }
72
73         sprintf_append(state->mem_ctx, &state->s, &state->len, &state->buflen,
74                        "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d)\n", 
75                        depth*4, "",
76                        name,
77                        (unsigned long)talloc_total_size(ptr),
78                        (unsigned long)talloc_total_blocks(ptr),
79                        talloc_reference_count(ptr));
80 }
81
82 /**
83  * Respond to a POOL_USAGE message by sending back string form of memory
84  * usage stats.
85  **/
86 static void msg_pool_usage(struct messaging_context *msg_ctx,
87                            void *private_data, 
88                            uint32_t msg_type, 
89                            struct server_id src,
90                            DATA_BLOB *data)
91 {
92         struct msg_pool_usage_state state;
93
94         SMB_ASSERT(msg_type == MSG_REQ_POOL_USAGE);
95
96         DEBUG(2,("Got POOL_USAGE\n"));
97
98         state.mem_ctx = talloc_init("msg_pool_usage");
99         if (!state.mem_ctx) {
100                 return;
101         }
102         state.len       = 0;
103         state.buflen    = 512;
104         state.s         = NULL;
105
106         talloc_report_depth_cb(NULL, 0, -1, msg_pool_usage_helper, &state);
107
108         if (!state.s) {
109                 talloc_destroy(state.mem_ctx);
110                 return;
111         }
112
113         messaging_send_buf(msg_ctx, src, MSG_POOL_USAGE,
114                            (uint8 *)state.s, strlen(state.s)+1);
115
116         talloc_destroy(state.mem_ctx);
117 }
118
119 /**
120  * Register handler for MSG_REQ_POOL_USAGE
121  **/
122 void register_msg_pool_usage(struct messaging_context *msg_ctx)
123 {
124         messaging_register(msg_ctx, NULL, MSG_REQ_POOL_USAGE, msg_pool_usage);
125         DEBUG(2, ("Registered MSG_REQ_POOL_USAGE\n"));
126 }