added basic ssl support to ircu
[ircu2.10.12-pk.git] / ircd / msgq.c
index 3fd22a06621ecf02e2a8aa2408cf525c0e80f0dc..379ffae6a873261e63620d17e8b1a81ff8048e80 100644 (file)
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * $Id$
  */
+/** @file
+ * @brief Outbound message queue implementation.
+ * @version $Id$
+ */
+#include "config.h"
+
 #include "msgq.h"
+#include "ircd.h"
 #include "ircd_alloc.h"
 #include "ircd_defs.h"
+#include "ircd_features.h"
+#include "ircd_log.h"
+#include "ircd_reply.h"
 #include "ircd_snprintf.h"
+#include "numeric.h"
+#include "send.h"
 #include "s_debug.h"
+#include "s_stats.h"
 
-#include <assert.h>
+/* #include <assert.h> -- Now using assert in ircd_log.h */
 #include <stdarg.h>
+#include <string.h>
 #include <sys/types.h>
 #include <sys/uio.h>   /* struct iovec */
 
+#define MB_BASE_SHIFT  5 /**< Log2 of smallest message body to allocate. */
+#define MB_MAX_SHIFT   9 /**< Log2 of largest message body to allocate. */
+
+/** Buffer for a single message. */
 struct MsgBuf {
-  struct MsgBuf *next;         /* next msg in global queue */
-  struct MsgBuf **prev_p;      /* what points to us in linked list */
-  unsigned int ref;            /* reference count */
-  unsigned int length;         /* length of message */
-  char msg[BUFSIZE + 1];       /* the message */
+  struct MsgBuf *next;         /**< next msg in global queue */
+  struct MsgBuf **prev_p;      /**< what points to us in linked list */
+  struct MsgBuf *real;         /**< the actual MsgBuf we're attaching */
+  unsigned int ref;            /**< reference count */
+  unsigned int length;         /**< length of message */
+  unsigned int power;          /**< size of buffer (power of 2) */
+  char msg[1];                 /**< the message */
 };
 
+/** Return allocated length of the buffer of \a buf. */
+#define bufsize(buf)   (1 << (buf)->power)
+
+/** Message body for a particular destination. */
 struct Msg {
-  struct Msg *next;            /* next msg */
-  unsigned int sent;           /* bytes in msg that have already been sent */
-  struct MsgBuf *msg;          /* actual message in queue */
+  struct Msg *next;            /**< next msg */
+  unsigned int sent;           /**< bytes in msg that have already been sent */
+  struct MsgBuf *msg;          /**< actual message in queue */
 };
 
-static struct {
-  struct MsgBuf *msgs;
-  struct MsgBuf *free_mbs;
-  struct Msg *free_msgs;
-} MQData = { 0, 0, 0 };
+/** Statistics tracking for message sizes. */
+struct MsgSizes {
+  unsigned int msgs;           /**< total number of messages */
+  unsigned int sizes[BUFSIZE]; /**< histogram of message sizes */
+};
 
-struct MsgCounts msgBufCounts = { 0, 0 };
-struct MsgCounts msgCounts = { 0, 0 };
+/** Global tracking data for message buffers. */
+static struct {
+  struct MsgBuf *msglist;      /**< list of in-use MsgBuf's */
+  struct {
+    unsigned int alloc;                /**< number of Msg's allocated */
+    unsigned int used;         /**< number of Msg's in use */
+    struct Msg *free;          /**< freelist of Msg's */
+  } msgs;                       /**< tracking info for Msg structs */
+  size_t tot_bufsize;          /**< total amount of memory in buffers */
+  /** Array of MsgBuf information, one entry for each used bucket size. */
+  struct {
+    unsigned int alloc;                /**< total MsgBuf's of this size */
+    unsigned int used;         /**< number of MsgBuf's of this size in use */
+    struct MsgBuf *free;       /**< list of free MsgBuf's */
+  } msgBufs[MB_MAX_SHIFT - MB_BASE_SHIFT + 1];
+  struct MsgSizes sizes;       /**< histogram of message sizes */
+} MQData;
 
 /*
  * This routine is used to remove a certain amount of data from a given
  * queue and release the Msg (and MsgBuf) structure if needed
  */
+/** Remove some data from a list within a message queue.
+ * @param[in,out] mq Message queue to remove from.
+ * @param[in,out] qlist Particular list within queue to remove from.
+ * @param[in,out] length_p Number of bytes left to remove.
+ */
 static void
 msgq_delmsg(struct MsgQ *mq, struct MsgQList *qlist, unsigned int *length_p)
 {
@@ -84,10 +126,10 @@ msgq_delmsg(struct MsgQ *mq, struct MsgQList *qlist, unsigned int *length_p)
     else
       qlist->head = m->next; /* just shift the list down some */
 
-    msgCounts.used--; /* struct Msg is not in use anymore */
+    MQData.msgs.used--; /* struct Msg is not in use anymore */
 
-    m->next = MQData.free_msgs; /* throw it onto the free list */
-    MQData.free_msgs = m;
+    m->next = MQData.msgs.free; /* throw it onto the free list */
+    MQData.msgs.free = m;
   } else {
     mq->length -= *length_p; /* decrement queue length */
     m->sent += *length_p; /* this much of the message has been sent */
@@ -95,8 +137,8 @@ msgq_delmsg(struct MsgQ *mq, struct MsgQList *qlist, unsigned int *length_p)
   }
 }
 
-/*
- * This just initializes a struct MsgQ.
+/** Initialize \a mq.
+ * @param[in] mq MsgQ to initialize.
  */
 void
 msgq_init(struct MsgQ *mq)
@@ -111,11 +153,9 @@ msgq_init(struct MsgQ *mq)
   mq->prio.tail = 0;
 }
 
-/*
- * This routine is used to delete the specified number of bytes off
- * of the queue.  We only really need to worry about one struct Msg*,
- * but this allows us to retain the flexibility to deal with more,
- * which means we could do something fancy involving writev...
+/** Delete bytes from the front of a message queue.
+ * @param[in] mq Queue to drop data from.
+ * @param[in] length Number of bytes to drop.
  */
 void
 msgq_delete(struct MsgQ *mq, unsigned int length)
@@ -134,41 +174,16 @@ msgq_delete(struct MsgQ *mq, unsigned int length)
   }
 }
 
-/*
- * This is similiar to the dbuf_map() function to allow us to plug it
- * into the existing code more easily; we may want to have something
- * more fancy in the future that would allow us to make some intelligent
- * use of writev or similiar functions.
- */
-const char *
-msgq_map(const struct MsgQ *mq, unsigned int *length_p)
-{
-  assert(0 != mq);
-  assert(0 != length_p);
-
-  if (mq->length <= 0)
-    return 0;
-
-  if (mq->queue.head && mq->queue.head->sent > 0) { /* partial msg on norm q */
-    *length_p = mq->queue.head->msg->length - mq->queue.head->sent;
-    return mq->queue.head->msg->msg + mq->queue.head->sent;
-  } else if (mq->prio.head) { /* message (partial or complete) on prio queue */
-    *length_p = mq->prio.head->msg->length - mq->prio.head->sent;
-    return mq->prio.head->msg->msg + mq->prio.head->sent;
-  } else if (mq->queue.head) { /* message on normal queue */
-    *length_p = mq->queue.head->msg->length; /* partial already dealt with */
-    return mq->queue.head->msg->msg;
-  }
-
-  return 0; /* shouldn't ever happen */
-}
-
-/*
- * This is the more intelligent routine that can fill in an array of
- * struct iovec's.
+/** Map data from a message queue to an I/O vector.
+ * @param[in] mq Message queue to send from.
+ * @param[out] iov Output vector.
+ * @param[in] count Number of elements in \a iov.
+ * @param[out] len Number of bytes mapped from \a mq to \a iov.
+ * @return Number of elements filled in \a iov.
  */
 int
-msgq_mapiov(const struct MsgQ *mq, struct iovec *iov, int count)
+msgq_mapiov(const struct MsgQ *mq, struct iovec *iov, int count,
+           unsigned int *len)
 {
   struct Msg *queue;
   struct Msg *prio;
@@ -177,6 +192,7 @@ msgq_mapiov(const struct MsgQ *mq, struct iovec *iov, int count)
   assert(0 != mq);
   assert(0 != iov);
   assert(0 != count);
+  assert(0 != len);
 
   if (mq->length <= 0) /* no data to map */
     return 0;
@@ -184,6 +200,7 @@ msgq_mapiov(const struct MsgQ *mq, struct iovec *iov, int count)
   if (mq->queue.head && mq->queue.head->sent > 0) { /* partial msg on norm q */
     iov[i].iov_base = mq->queue.head->msg->msg + mq->queue.head->sent;
     iov[i].iov_len = mq->queue.head->msg->length - mq->queue.head->sent;
+    *len += iov[i].iov_len;
 
     queue = mq->queue.head->next; /* where we start later... */
 
@@ -196,6 +213,7 @@ msgq_mapiov(const struct MsgQ *mq, struct iovec *iov, int count)
   if (mq->prio.head && mq->prio.head->sent > 0) { /* partial msg on prio q */
     iov[i].iov_base = mq->prio.head->msg->msg + mq->prio.head->sent;
     iov[i].iov_len = mq->prio.head->msg->length - mq->prio.head->sent;
+    *len += iov[i].iov_len;
 
     prio = mq->prio.head->next; /* where we start later... */
 
@@ -208,6 +226,7 @@ msgq_mapiov(const struct MsgQ *mq, struct iovec *iov, int count)
   for (; prio; prio = prio->next) { /* go through prio queue */
     iov[i].iov_base = prio->msg->msg; /* store message */
     iov[i].iov_len = prio->msg->length;
+    *len += iov[i].iov_len;
 
     i++; /* filled an iovec... */
     if (!--count) /* check for space */
@@ -217,6 +236,7 @@ msgq_mapiov(const struct MsgQ *mq, struct iovec *iov, int count)
   for (; queue; queue = queue->next) { /* go through normal queue */
     iov[i].iov_base = queue->msg->msg;
     iov[i].iov_len = queue->msg->length;
+    *len += iov[i].iov_len;
 
     i++; /* filled an iovec... */
     if (!--count) /* check for space */
@@ -226,10 +246,83 @@ msgq_mapiov(const struct MsgQ *mq, struct iovec *iov, int count)
   return i;
 }
 
-/*
- * This routine builds a struct MsgBuf with the appropriate contents
- * and returns it; this saves us from having to worry about the contents
- * of struct MsgBuf in anything other than this module
+/** Allocate a message buffer large enough to hold \a length bytes.
+ * TODO: \a in_mb needs better documentation.
+ * @param[in] in_mb Some other message buffer(?).
+ * @param[in] length Number of bytes of space to reserve in output.
+ * @return Pointer to some usable message buffer.
+ */
+static struct MsgBuf *
+msgq_alloc(struct MsgBuf *in_mb, int length)
+{
+  struct MsgBuf *mb;
+  int power;
+
+  /* Find the power of two size that will accommodate the message */
+  for (power = MB_BASE_SHIFT; power < MB_MAX_SHIFT + 1; power++)
+    if ((length - 1) >> power == 0)
+      break;
+  assert((1 << power) >= length);
+  assert((1 << power) <= 512);
+  length = 1 << power; /* reset the length */
+
+  /* If the message needs a buffer of exactly the existing size, just use it */
+  if (in_mb && in_mb->power == power) {
+    in_mb->real = in_mb; /* real buffer is this buffer */
+    return in_mb;
+  }
+
+  /* Try popping one off the freelist first */
+  if ((mb = MQData.msgBufs[power - MB_BASE_SHIFT].free)) {
+    MQData.msgBufs[power - MB_BASE_SHIFT].free = mb->next;
+  } else if (MQData.tot_bufsize < feature_int(FEAT_BUFFERPOOL)) {
+    /* Allocate another if we won't bust the BUFFERPOOL */
+    Debug((DEBUG_MALLOC, "Allocating MsgBuf of length %d (total size %zu)",
+          length, sizeof(struct MsgBuf) + length));
+    mb = (struct MsgBuf *)MyMalloc(sizeof(struct MsgBuf) + length);
+    MQData.msgBufs[power - MB_BASE_SHIFT].alloc++;
+    mb->power = power; /* remember size */
+    MQData.tot_bufsize += length;
+  }
+
+  if (mb) {
+    MQData.msgBufs[power - MB_BASE_SHIFT].used++; /* how many are we using? */
+
+    mb->real = 0; /* essential initializations */
+    mb->ref = 1;
+
+    if (in_mb) /* remember who's the *real* buffer */
+      in_mb->real = mb;
+  } else if (in_mb) /* just use the input buffer */
+    mb = in_mb->real = in_mb;
+
+  return mb; /* return the buffer */
+}
+
+/** Deallocate unused message buffers.
+ */
+static void
+msgq_clear_freembs(void)
+{
+  struct MsgBuf *mb;
+  int i;
+
+  /* Walk through the various size classes */
+  for (i = MB_BASE_SHIFT; i < MB_MAX_SHIFT + 1; i++)
+    /* walk down the free list */
+    while ((mb = MQData.msgBufs[i - MB_BASE_SHIFT].free)) {
+      MQData.msgBufs[i - MB_BASE_SHIFT].free = mb->next; /* shift free list */
+      MQData.msgBufs[i - MB_BASE_SHIFT].alloc--; /* reduce allocation count */
+      MQData.tot_bufsize -= 1 << i; /* reduce total buffer allocation count */
+      MyFree(mb); /* and free the buffer */
+    }
+}
+
+/** Format a message buffer for a client from a format string.
+ * @param[in] dest %Client that receives the data (may be NULL).
+ * @param[in] format Format string for message.
+ * @param[in] vl Argument list for \a format.
+ * @return Allocated MsgBuf.
  */
 struct MsgBuf *
 msgq_vmake(struct Client *dest, const char *format, va_list vl)
@@ -238,32 +331,73 @@ msgq_vmake(struct Client *dest, const char *format, va_list vl)
 
   assert(0 != format);
 
-  if (!(mb = MQData.free_mbs)) { /* do I need to allocate one? */
-    mb = (struct MsgBuf *)MyMalloc(sizeof(struct MsgBuf));
-    msgBufCounts.alloc++; /* we allocated another */
-  } else /* shift the free list */
-    MQData.free_mbs = MQData.free_mbs->next;
-
-  msgBufCounts.used++; /* we're using another */
+  if (!(mb = msgq_alloc(0, BUFSIZE))) {
+    if (feature_bool(FEAT_HAS_FERGUSON_FLUSHER)) {
+      /*
+       * from "Married With Children" episode were Al bought a REAL toilet
+       * on the black market because he was tired of the wimpy water
+       * conserving toilets they make these days --Bleep
+       */
+      /*
+       * Apparently this doesn't work, the server _has_ to
+       * dump a few clients to handle the load. A fully loaded
+       * server cannot handle a net break without dumping some
+       * clients. If we flush the connections here under a full
+       * load we may end up starving the kernel for mbufs and
+       * crash the machine
+       */
+      /*
+       * attempt to recover from buffer starvation before
+       * bailing this may help servers running out of memory
+       */
+      flush_connections(0);
+      mb = msgq_alloc(0, BUFSIZE);
+    }
+    if (!mb) { /* OK, try clearing the buffer free list */
+      msgq_clear_freembs();
+      mb = msgq_alloc(0, BUFSIZE);
+    }
+    if (!mb) { /* OK, try killing a client */
+      kill_highest_sendq(0); /* Don't kill any server connections */
+      msgq_clear_freembs();  /* Release whatever was just freelisted */
+      mb = msgq_alloc(0, BUFSIZE);
+    }
+    if (!mb) { /* hmmm... */
+      kill_highest_sendq(1); /* Try killing a server connection now */
+      msgq_clear_freembs();  /* Clear freelist again */
+      mb = msgq_alloc(0, BUFSIZE);
+    }
+    if (!mb) /* AIEEEE! */
+      server_panic("Unable to allocate buffers!");
+  }
 
-  mb->next = MQData.msgs; /* initialize the msgbuf */
-  mb->prev_p = &MQData.msgs;
-  mb->ref = 1;
+  mb->next = MQData.msglist; /* initialize the msgbuf */
+  mb->prev_p = &MQData.msglist;
 
   /* fill the buffer */
-  mb->length = ircd_vsnprintf(dest, mb->msg, sizeof(mb->msg) - 2, format, vl);
+  mb->length = ircd_vsnprintf(dest, mb->msg, bufsize(mb) - 1, format, vl);
+
+  if (mb->length > bufsize(mb) - 2)
+    mb->length = bufsize(mb) - 2;
 
   mb->msg[mb->length++] = '\r'; /* add \r\n to buffer */
   mb->msg[mb->length++] = '\n';
   mb->msg[mb->length] = '\0'; /* not strictly necessary */
 
-  if (MQData.msgs) /* link it into the list */
-    MQData.msgs->prev_p = &mb->next;
-  MQData.msgs = mb;
+  assert(mb->length <= bufsize(mb));
+
+  if (MQData.msglist) /* link it into the list */
+    MQData.msglist->prev_p = &mb->next;
+  MQData.msglist = mb;
 
   return mb;
 }
 
+/** Format a message buffer for a client from a format string.
+ * @param[in] dest %Client that receives the data (may be NULL).
+ * @param[in] format Format string for message.
+ * @return Allocated MsgBuf.
+ */
 struct MsgBuf *
 msgq_make(struct Client *dest, const char *format, ...)
 {
@@ -277,8 +411,10 @@ msgq_make(struct Client *dest, const char *format, ...)
   return mb;
 }
 
-/*
- * This routine is used to append a formatted string to a struct MsgBuf.
+/** Append text to an existing message buffer.
+ * @param[in] dest %Client for whom to format the message.
+ * @param[in] mb Message buffer to append to.
+ * @param[in] format Format string of what to append.
  */
 void
 msgq_append(struct Client *dest, struct MsgBuf *mb, const char *format, ...)
@@ -287,46 +423,59 @@ msgq_append(struct Client *dest, struct MsgBuf *mb, const char *format, ...)
 
   assert(0 != mb);
   assert(0 != format);
+  assert(0 == mb->real);
+
+  assert(2 < mb->length);
+  assert(bufsize(mb) >= mb->length);
 
   mb->length -= 2; /* back up to before \r\n */
 
   va_start(vl, format); /* append to the buffer */
   mb->length += ircd_vsnprintf(dest, mb->msg + mb->length,
-                              sizeof(mb->msg) - 2 - mb->length, format, vl);
+                              bufsize(mb) - mb->length - 1, format, vl);
   va_end(vl);
 
+  if (mb->length > bufsize(mb) - 2)
+    mb->length = bufsize(mb) - 2;
+
   mb->msg[mb->length++] = '\r'; /* add \r\n to buffer */
   mb->msg[mb->length++] = '\n';
   mb->msg[mb->length] = '\0'; /* not strictly necessary */
+
+  assert(mb->length <= bufsize(mb));
 }
 
-/*
- * This routine is called to decrement the reference count on a
- * struct MsgBuf and delete it if necessary.
+/** Decrement the reference count on \a mb, freeing it if needed.
+ * @param[in] mb MsgBuf to release.
  */
 void
 msgq_clean(struct MsgBuf *mb)
 {
   assert(0 != mb);
   assert(0 < mb->ref);
-  assert(0 != mb->prev_p);
 
   if (!--mb->ref) { /* deallocate the message */
-    *mb->prev_p = mb->next; /* clip it out of active MsgBuf's list */
-    if (mb->next)
-      mb->next->prev_p = mb->prev_p;
+    if (mb->prev_p) {
+      *mb->prev_p = mb->next; /* clip it out of active MsgBuf's list */
+      if (mb->next)
+       mb->next->prev_p = mb->prev_p;
+    }
 
-    mb->next = MQData.free_mbs; /* add it to free list */
-    MQData.free_mbs = mb;
+    if (mb->real && mb->real != mb) /* clean up the real buffer */
+      msgq_clean(mb->real);
 
-    mb->prev_p = 0;
+    mb->next = MQData.msgBufs[mb->power - MB_BASE_SHIFT].free;
+    MQData.msgBufs[mb->power - MB_BASE_SHIFT].free = mb;
+    MQData.msgBufs[mb->power - MB_BASE_SHIFT].used--;
 
-    msgBufCounts.used--; /* decrement the usage count */
+    mb->prev_p = 0;
   }
 }
 
-/*
- * This routine simply adds a struct Msg to the end of a user's MsgQ.
+/** Append a message to a peer's message queue.
+ * @param[in] mq Message queue to append to.
+ * @param[in] mb Message to append.
+ * @param[in] prio If non-zero, use the high-priority (lag-busting) message list; else use the normal list.
  */
 void
 msgq_add(struct MsgQ *mq, struct MsgBuf *mb, int prio)
@@ -337,26 +486,56 @@ msgq_add(struct MsgQ *mq, struct MsgBuf *mb, int prio)
   assert(0 != mq);
   assert(0 != mb);
   assert(0 < mb->ref);
+  assert(0 < mb->length);
 
-  Debug((DEBUG_SEND, "Adding buffer %p [%.*s] to %s queue", mb,
-        mb->length - 2, mb->msg, prio ? "priority" : "normal"));
+  Debug((DEBUG_SEND, "Adding buffer %p [%.*s] length %u to %s queue", mb,
+        mb->length - 2, mb->msg, mb->length, prio ? "priority" : "normal"));
 
   qlist = prio ? &mq->prio : &mq->queue;
 
-  if (!(msg = MQData.free_msgs)) { /* do I need to allocate one? */
+  if (!(msg = MQData.msgs.free)) { /* do I need to allocate one? */
     msg = (struct Msg *)MyMalloc(sizeof(struct Msg));
-    msgCounts.alloc++; /* we allocated another */
+    MQData.msgs.alloc++; /* we allocated another */
   } else /* shift the free list */
-    MQData.free_msgs = MQData.free_msgs->next;
+    MQData.msgs.free = MQData.msgs.free->next;
 
-  msgCounts.used++; /* we're using another */
+  MQData.msgs.used++; /* we're using another */
 
   msg->next = 0; /* initialize the msg */
   msg->sent = 0;
-  msg->msg = mb;
 
+  /* Get the real buffer, allocating one if necessary */
+  if (!mb->real) {
+    struct MsgBuf *tmp;
+
+    MQData.sizes.msgs++; /* update histogram counts */
+    MQData.sizes.sizes[mb->length - 1]++;
+
+    tmp = msgq_alloc(mb, mb->length); /* allocate a close-fitting buffer */
+
+    if (tmp != mb) { /* OK, prepare the new "real" buffer */
+      Debug((DEBUG_SEND, "Copying old buffer %p [%.*s] length %u into new "
+            "buffer %p size %u", mb, mb->length - 2, mb->msg, mb->length,
+            tmp, bufsize(tmp)));
+      memcpy(tmp->msg, mb->msg, mb->length + 1); /* copy string over */
+      tmp->length = mb->length;
+
+      tmp->next = mb->next; /* replace it in the list, now */
+      if (tmp->next)
+       tmp->next->prev_p = &tmp->next;
+      tmp->prev_p = mb->prev_p;
+      *tmp->prev_p = tmp;
+
+      mb->next = 0; /* this one's no longer in the list */
+      mb->prev_p = 0;
+    }
+  }
+
+  mb = mb->real; /* work with the real buffer */
   mb->ref++; /* increment the ref count on the buffer */
 
+  msg->msg = mb; /* point at the real message buffer now */
+
   if (!qlist->head) /* queue list was empty; head and tail point to msg */
     qlist->head = qlist->tail = msg;
   else {
@@ -370,31 +549,80 @@ msgq_add(struct MsgQ *mq, struct MsgBuf *mb, int prio)
   mq->count++; /* and the queue count */
 }
 
-/*
- * This is for reporting memory usage by the msgq system.
+/** Report memory statistics for message buffers.
+ * @param[in] cptr Client requesting information.
+ * @param[out] msg_alloc Receives number of bytes allocated in Msg structs.
+ * @param[out] msgbuf_alloc Receives number of bytes allocated in MsgBuf structs.
  */
 void
-msgq_count_memory(size_t *msg_alloc, size_t *msg_used, size_t *msgbuf_alloc,
-                 size_t *msgbuf_used)
+msgq_count_memory(struct Client *cptr, size_t *msg_alloc, size_t *msgbuf_alloc)
 {
+  int i;
+  size_t total = 0, size;
+
+  assert(0 != cptr);
   assert(0 != msg_alloc);
-  assert(0 != msg_used);
   assert(0 != msgbuf_alloc);
-  assert(0 != msgbuf_used);
 
-  *msg_alloc = msgCounts.alloc * sizeof(struct Msg);
-  *msg_used = msgCounts.used * sizeof(struct Msg);
-  *msgbuf_alloc = msgCounts.alloc * sizeof(struct MsgBuf);
-  *msgbuf_used = msgCounts.used * sizeof(struct MsgBuf);
+  /* Data for Msg's is simple, so just send it */
+  send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
+            ":Msgs allocated %d(%zu) used %d(%zu) text %zu",
+             MQData.msgs.alloc, MQData.msgs.alloc * sizeof(struct Msg),
+             MQData.msgs.used,  MQData.msgs.used * sizeof(struct Msg),
+             MQData.tot_bufsize);
+  /* count_memory() wants to know the total */
+  *msg_alloc = MQData.msgs.alloc * sizeof(struct Msg);
+
+  /* Ok, now walk through each size class */
+  for (i = MB_BASE_SHIFT; i < MB_MAX_SHIFT + 1; i++) {
+    size = sizeof(struct MsgBuf) + (1 << i); /* total size of a buffer */
+
+    /* Send information for this buffer size class */
+    send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
+              ":MsgBufs of size %zu allocated %d(%zu) used %d(%zu)", 1 << i,
+              MQData.msgBufs[i - MB_BASE_SHIFT].alloc,
+              MQData.msgBufs[i - MB_BASE_SHIFT].alloc * size,
+              MQData.msgBufs[i - MB_BASE_SHIFT].used,
+              MQData.msgBufs[i - MB_BASE_SHIFT].used * size);
+
+    /* count_memory() wants to know the total */
+    total += MQData.msgBufs[i - MB_BASE_SHIFT].alloc * size;
+  }
+  *msgbuf_alloc = total;
 }
 
-/*
- * This routine is used simply to report how much bufferspace is left.
+/** Report remaining space in a MsgBuf.
+ * @param[in] mb Message buffer to check.
+ * @return Number of additional bytes that can be appended to the message.
  */
 unsigned int
 msgq_bufleft(struct MsgBuf *mb)
 {
   assert(0 != mb);
 
-  return sizeof(mb->msg) - mb->length; /* the -2 for \r\n is in mb->length */
+  return bufsize(mb) - mb->length; /* \r\n counted in mb->length */
+}
+
+/** Send histogram of message lengths to a client.
+ * @param[in] cptr Client requesting statistics.
+ * @param[in] sd Stats descriptor for request (ignored).
+ * @param[in] param Extra parameter from user (ignored).
+ */
+void
+msgq_histogram(struct Client *cptr, const struct StatDesc *sd, char *param)
+{
+  struct MsgSizes tmp = MQData.sizes; /* All hail structure copy! */
+  int i;
+
+  send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
+            ":Histogram of message lengths (%lu messages)", tmp.msgs);
+  for (i = 0; i + 16 <= BUFSIZE; i += 16)
+    send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":% 4d: %u %u %u %u "
+              "%u %u %u %u %u %u %u %u %u %u %u %u", i + 1,
+              tmp.sizes[i +  0], tmp.sizes[i +  1], tmp.sizes[i +  2],
+              tmp.sizes[i +  3], tmp.sizes[i +  4], tmp.sizes[i +  5],
+              tmp.sizes[i +  6], tmp.sizes[i +  7], tmp.sizes[i +  8],
+              tmp.sizes[i +  9], tmp.sizes[i + 10], tmp.sizes[i + 11],
+              tmp.sizes[i + 12], tmp.sizes[i + 13], tmp.sizes[i + 14],
+              tmp.sizes[i + 15]);
 }