add skynet_now() for 64bit time

This commit is contained in:
Cloud Wu
2015-11-16 19:48:38 +08:00
parent 87bc0815f6
commit 2252409eaa
7 changed files with 32 additions and 36 deletions

View File

@@ -326,6 +326,13 @@ ltrash(lua_State *L) {
return 0; return 0;
} }
static int
lnow(lua_State *L) {
uint64_t ti = skynet_now();
lua_pushinteger(L, ti);
return 1;
}
int int
luaopen_skynet_core(lua_State *L) { luaopen_skynet_core(lua_State *L) {
luaL_checkversion(L); luaL_checkversion(L);
@@ -344,6 +351,7 @@ luaopen_skynet_core(lua_State *L) {
{ "packstring", lpackstring }, { "packstring", lpackstring },
{ "trash" , ltrash }, { "trash" , ltrash },
{ "callback", _callback }, { "callback", _callback },
{ "now", lnow },
{ NULL, NULL }, { NULL, NULL },
}; };

View File

@@ -299,16 +299,19 @@ function skynet.localname(name)
end end
end end
function skynet.now() skynet.now = c.now
return c.intcommand("NOW")
end local starttime
function skynet.starttime() function skynet.starttime()
return c.intcommand("STARTTIME") if not starttime then
starttime = c.intcommand("STARTTIME")
end
return starttime
end end
function skynet.time() function skynet.time()
return skynet.now()/100 + skynet.starttime() -- get now first would be better return skynet.now()/100 + starttime or skynet.starttime()
end end
function skynet.exit() function skynet.exit()

View File

@@ -38,5 +38,6 @@ typedef int (*skynet_cb)(struct skynet_context * context, void *ud, int type, in
void skynet_callback(struct skynet_context * context, void *ud, skynet_cb cb); void skynet_callback(struct skynet_context * context, void *ud, skynet_cb cb);
uint32_t skynet_current_handle(void); uint32_t skynet_current_handle(void);
uint64_t skynet_now(void);
#endif #endif

View File

@@ -15,11 +15,11 @@ skynet_log_open(struct skynet_context * ctx, uint32_t handle) {
sprintf(tmp, "%s/%08x.log", logpath, handle); sprintf(tmp, "%s/%08x.log", logpath, handle);
FILE *f = fopen(tmp, "ab"); FILE *f = fopen(tmp, "ab");
if (f) { if (f) {
uint32_t starttime = skynet_gettime_fixsec(); uint32_t starttime = skynet_starttime();
uint32_t currenttime = skynet_gettime(); uint64_t currenttime = skynet_now();
time_t ti = starttime + currenttime/100; time_t ti = starttime + currenttime/100;
skynet_error(ctx, "Open log file %s", tmp); skynet_error(ctx, "Open log file %s", tmp);
fprintf(f, "open time: %u %s", currenttime, ctime(&ti)); fprintf(f, "open time: %u %s", (uint32_t)currenttime, ctime(&ti));
fflush(f); fflush(f);
} else { } else {
skynet_error(ctx, "Open log file %s fail", tmp); skynet_error(ctx, "Open log file %s fail", tmp);
@@ -30,7 +30,7 @@ skynet_log_open(struct skynet_context * ctx, uint32_t handle) {
void void
skynet_log_close(struct skynet_context * ctx, FILE *f, uint32_t handle) { skynet_log_close(struct skynet_context * ctx, FILE *f, uint32_t handle) {
skynet_error(ctx, "Close log file :%08x", handle); skynet_error(ctx, "Close log file :%08x", handle);
fprintf(f, "close time: %u\n", skynet_gettime()); fprintf(f, "close time: %u\n", (uint32_t)skynet_now());
fclose(f); fclose(f);
} }
@@ -68,7 +68,7 @@ skynet_log_output(FILE *f, uint32_t source, int type, int session, void * buffer
if (type == PTYPE_SOCKET) { if (type == PTYPE_SOCKET) {
log_socket(f, buffer, sz); log_socket(f, buffer, sz);
} else { } else {
uint32_t ti = skynet_gettime(); uint32_t ti = (uint32_t)skynet_now();
fprintf(f, ":%08x %d %d %u ", source, type, session, ti); fprintf(f, ":%08x %d %d %u ", source, type, session, ti);
log_blob(f, buffer, sz); log_blob(f, buffer, sz);
fprintf(f,"\n"); fprintf(f,"\n");

View File

@@ -429,13 +429,6 @@ cmd_name(struct skynet_context * context, const char * param) {
return NULL; return NULL;
} }
static const char *
cmd_now(struct skynet_context * context, const char * param) {
uint32_t ti = skynet_gettime();
sprintf(context->result,"%u",ti);
return context->result;
}
static const char * static const char *
cmd_exit(struct skynet_context * context, const char * param) { cmd_exit(struct skynet_context * context, const char * param) {
handle_exit(context, 0); handle_exit(context, 0);
@@ -507,7 +500,7 @@ cmd_setenv(struct skynet_context * context, const char * param) {
static const char * static const char *
cmd_starttime(struct skynet_context * context, const char * param) { cmd_starttime(struct skynet_context * context, const char * param) {
uint32_t sec = skynet_gettime_fixsec(); uint32_t sec = skynet_starttime();
sprintf(context->result,"%u",sec); sprintf(context->result,"%u",sec);
return context->result; return context->result;
} }
@@ -619,7 +612,6 @@ static struct command_func cmd_funcs[] = {
{ "REG", cmd_reg }, { "REG", cmd_reg },
{ "QUERY", cmd_query }, { "QUERY", cmd_query },
{ "NAME", cmd_name }, { "NAME", cmd_name },
{ "NOW", cmd_now },
{ "EXIT", cmd_exit }, { "EXIT", cmd_exit },
{ "KILL", cmd_kill }, { "KILL", cmd_kill },
{ "LAUNCH", cmd_launch }, { "LAUNCH", cmd_launch },

View File

@@ -45,10 +45,9 @@ struct timer {
struct link_list t[4][TIME_LEVEL]; struct link_list t[4][TIME_LEVEL];
struct spinlock lock; struct spinlock lock;
uint32_t time; uint32_t time;
uint32_t current;
uint32_t starttime; uint32_t starttime;
uint64_t current;
uint64_t current_point; uint64_t current_point;
uint64_t origin_point;
}; };
static struct timer * TI = NULL; static struct timer * TI = NULL;
@@ -277,13 +276,7 @@ skynet_updatetime(void) {
} else if (cp != TI->current_point) { } else if (cp != TI->current_point) {
uint32_t diff = (uint32_t)(cp - TI->current_point); uint32_t diff = (uint32_t)(cp - TI->current_point);
TI->current_point = cp; TI->current_point = cp;
uint32_t oc = TI->current;
TI->current += diff; TI->current += diff;
if (TI->current < oc) {
// when cs > 0xffffffff(about 497 days), time rewind
TI->starttime += 0xffffffff / 100;
}
int i; int i;
for (i=0;i<diff;i++) { for (i=0;i<diff;i++) {
timer_update(TI); timer_update(TI);
@@ -292,21 +285,21 @@ skynet_updatetime(void) {
} }
uint32_t uint32_t
skynet_gettime_fixsec(void) { skynet_starttime(void) {
return TI->starttime; return TI->starttime;
} }
uint32_t uint64_t
skynet_gettime(void) { skynet_now(void) {
return TI->current; return TI->current;
} }
void void
skynet_timer_init(void) { skynet_timer_init(void) {
TI = timer_create_timer(); TI = timer_create_timer();
systime(&TI->starttime, &TI->current); uint32_t current = 0;
uint64_t point = gettime(); systime(&TI->starttime, &current);
TI->current_point = point; TI->current = current;
TI->origin_point = point; TI->current_point = gettime();
} }

View File

@@ -5,8 +5,7 @@
int skynet_timeout(uint32_t handle, int time, int session); int skynet_timeout(uint32_t handle, int time, int session);
void skynet_updatetime(void); void skynet_updatetime(void);
uint32_t skynet_gettime(void); uint32_t skynet_starttime(void);
uint32_t skynet_gettime_fixsec(void);
void skynet_timer_init(void); void skynet_timer_init(void);