add skynet.stat() to get cpu cost and message count

This commit is contained in:
Cloud Wu
2016-10-26 17:43:52 +08:00
parent 07a1499886
commit 249ffb9362
10 changed files with 104 additions and 27 deletions

View File

@@ -10,6 +10,7 @@
#include "skynet_monitor.h"
#include "skynet_imp.h"
#include "skynet_log.h"
#include "skynet_timer.h"
#include "spinlock.h"
#include "atomic.h"
@@ -46,12 +47,15 @@ struct skynet_context {
skynet_cb cb;
struct message_queue *queue;
FILE * logfile;
uint64_t cpu_cost; // in microsec
char result[32];
uint32_t handle;
int session_id;
int ref;
int message_count;
bool init;
bool endless;
bool profile;
CHECKCALLING_DECL
};
@@ -61,6 +65,7 @@ struct skynet_node {
int init;
uint32_t monitor_exit;
pthread_key_t handle_key;
bool profile; // default is off
};
static struct skynet_node G_NODE;
@@ -139,6 +144,10 @@ skynet_context_new(const char * name, const char *param) {
ctx->init = false;
ctx->endless = false;
ctx->cpu_cost = 0;
ctx->message_count = 0;
ctx->profile = G_NODE.profile;
// Should set to 0 first to avoid skynet_handle_retireall get an uninitialized handle
ctx->handle = 0;
ctx->handle = skynet_handle_register(ctx);
@@ -256,9 +265,19 @@ dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) {
if (ctx->logfile) {
skynet_log_output(ctx->logfile, msg->source, type, msg->session, msg->data, sz);
}
if (!ctx->cb(ctx, ctx->cb_ud, type, msg->session, msg->source, msg->data, sz)) {
++ctx->message_count;
int reserve_msg;
if (ctx->profile) {
uint64_t start_time = skynet_thread_time();
reserve_msg = ctx->cb(ctx, ctx->cb_ud, type, msg->session, msg->source, msg->data, sz);
uint64_t cost_time = skynet_thread_time() - start_time;
ctx->cpu_cost += cost_time;
} else {
reserve_msg = ctx->cb(ctx, ctx->cb_ud, type, msg->session, msg->source, msg->data, sz);
}
if (!reserve_msg) {
skynet_free(msg->data);
}
}
CHECKCALLING_END(ctx)
}
@@ -505,16 +524,6 @@ cmd_starttime(struct skynet_context * context, const char * param) {
return context->result;
}
static const char *
cmd_endless(struct skynet_context * context, const char * param) {
if (context->endless) {
strcpy(context->result, "1");
context->endless = false;
return context->result;
}
return NULL;
}
static const char *
cmd_abort(struct skynet_context * context, const char * param) {
skynet_handle_retireall();
@@ -539,9 +548,25 @@ cmd_monitor(struct skynet_context * context, const char * param) {
}
static const char *
cmd_mqlen(struct skynet_context * context, const char * param) {
int len = skynet_mq_length(context->queue);
sprintf(context->result, "%d", len);
cmd_stat(struct skynet_context * context, const char * param) {
if (strcmp(param, "mqlen") == 0) {
int len = skynet_mq_length(context->queue);
sprintf(context->result, "%d", len);
} else if (strcmp(param, "endless") == 0) {
if (context->endless) {
strcpy(context->result, "1");
context->endless = false;
} else {
strcpy(context->result, "0");
}
} else if (strcmp(param, "cpu") == 0) {
double t = (double)context->cpu_cost / 1000000.0; // microsec
sprintf(context->result, "%lf", t);
} else if (strcmp(param, "message") == 0) {
sprintf(context->result, "%d", context->message_count);
} else {
context->result[0] = '\0';
}
return context->result;
}
@@ -618,10 +643,9 @@ static struct command_func cmd_funcs[] = {
{ "GETENV", cmd_getenv },
{ "SETENV", cmd_setenv },
{ "STARTTIME", cmd_starttime },
{ "ENDLESS", cmd_endless },
{ "ABORT", cmd_abort },
{ "MONITOR", cmd_monitor },
{ "MQLEN", cmd_mqlen },
{ "STAT", cmd_stat },
{ "LOGON", cmd_logon },
{ "LOGOFF", cmd_logoff },
{ "SIGNAL", cmd_signal },
@@ -779,3 +803,7 @@ skynet_initthread(int m) {
pthread_setspecific(G_NODE.handle_key, (void *)v);
}
void
skynet_profile_enable(int enable) {
G_NODE.profile = (bool)enable;
}