From 69e179a52e0878d3777508f75b9a5fc6a799d525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Fri, 21 Feb 2014 15:03:20 +0800 Subject: [PATCH] add skynet command MQLEN for debug --- lualib/skynet.lua | 5 +++++ skynet-src/skynet_mq.c | 13 +++++++++++++ skynet-src/skynet_mq.h | 3 +++ skynet-src/skynet_server.c | 6 ++++++ 4 files changed, 27 insertions(+) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 8c6872e7..72688aa3 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -526,6 +526,7 @@ function dbgcmd.STAT() query_state(stat, "count") query_state(stat, "time") stat.boottime = debug.getregistry().skynet_boottime + stat.mqlen = skynet.mqlen() skynet.ret(skynet.pack(stat)) end @@ -692,4 +693,8 @@ function skynet.monitor(service, query) c.command("MONITOR", string.format(":%08x", monitor)) end +function skynet.mqlen() + return tonumber(c.command "MQLEN") +end + return skynet diff --git a/skynet-src/skynet_mq.c b/skynet-src/skynet_mq.c index abdeb9b0..da7fe170 100644 --- a/skynet-src/skynet_mq.c +++ b/skynet-src/skynet_mq.c @@ -109,6 +109,19 @@ skynet_mq_handle(struct message_queue *q) { return q->handle; } +int +skynet_mq_length(struct message_queue *q) { + int head, tail; + LOCK(q) + head = q->head; + tail = q->tail; + UNLOCK(q) + + if (head <= tail) { + return tail - head; + } + return tail + q->cap - head; +} int skynet_mq_pop(struct message_queue *q, struct skynet_message *message) { diff --git a/skynet-src/skynet_mq.h b/skynet-src/skynet_mq.h index ced1beff..11945ce9 100644 --- a/skynet-src/skynet_mq.h +++ b/skynet-src/skynet_mq.h @@ -26,6 +26,9 @@ void skynet_mq_push(struct message_queue *q, struct skynet_message *message); void skynet_mq_lock(struct message_queue *q, int session); void skynet_mq_unlock(struct message_queue *q); +// return the length of message queue, for debug +int skynet_mq_length(struct message_queue *q); + void skynet_mq_force_push(struct message_queue *q); void skynet_mq_pushglobal(struct message_queue *q); diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 0e55e01f..f017c25c 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -563,6 +563,12 @@ skynet_command(struct skynet_context * context, const char * cmd , const char * return NULL; } + if (strcmp(cmd, "MQLEN") == 0) { + int len = skynet_mq_length(context->queue); + sprintf(context->result, "%d", len); + return context->result; + } + return NULL; }