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