Skip to content

Commit

Permalink
add skynet.stat 'time'
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwu committed Oct 26, 2016
1 parent 249ffb9 commit ffe5de4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
14 changes: 12 additions & 2 deletions skynet-src/skynet_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct skynet_context {
struct message_queue *queue;
FILE * logfile;
uint64_t cpu_cost; // in microsec
uint64_t cpu_start; // in microsec
char result[32];
uint32_t handle;
int session_id;
Expand Down Expand Up @@ -146,6 +147,7 @@ skynet_context_new(const char * name, const char *param) {
ctx->endless = false;

ctx->cpu_cost = 0;
ctx->cpu_start = 0;
ctx->message_count = 0;
ctx->profile = G_NODE.profile;
// Should set to 0 first to avoid skynet_handle_retireall get an uninitialized handle
Expand Down Expand Up @@ -268,9 +270,9 @@ dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) {
++ctx->message_count;
int reserve_msg;
if (ctx->profile) {
uint64_t start_time = skynet_thread_time();
ctx->cpu_start = 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;
uint64_t cost_time = skynet_thread_time() - ctx->cpu_start;
ctx->cpu_cost += cost_time;
} else {
reserve_msg = ctx->cb(ctx, ctx->cb_ud, type, msg->session, msg->source, msg->data, sz);
Expand Down Expand Up @@ -562,6 +564,14 @@ cmd_stat(struct skynet_context * context, const char * param) {
} else if (strcmp(param, "cpu") == 0) {
double t = (double)context->cpu_cost / 1000000.0; // microsec
sprintf(context->result, "%lf", t);
} else if (strcmp(param, "time") == 0) {
if (context->profile) {
uint64_t ti = skynet_thread_time() - context->cpu_start;
double t = (double)ti / 1000000.0; // microsec
sprintf(context->result, "%lf", t);
} else {
strcpy(context->result, "0");
}
} else if (strcmp(param, "message") == 0) {
sprintf(context->result, "%d", context->message_count);
} else {
Expand Down
11 changes: 11 additions & 0 deletions test/testendless.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local skynet = require "skynet"

skynet.start(function()
for i = 1, 1000000000 do -- very long loop
if i%100000000 == 0 then
print("Endless = ", skynet.stat "endless")
print("Cost time = ", skynet.stat "time")
end
end
skynet.exit()
end)

0 comments on commit ffe5de4

Please sign in to comment.