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