mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 04:33:05 +00:00
delete unused feature : group/localcast/forward, etc
This commit is contained in:
@@ -193,7 +193,10 @@ readline_stdin(void * arg) {
|
||||
struct queue * q = arg;
|
||||
char tmp[1024];
|
||||
while (!feof(stdin)) {
|
||||
fgets(tmp,sizeof(tmp),stdin);
|
||||
if (fgets(tmp,sizeof(tmp),stdin) == NULL) {
|
||||
// read stdin failed
|
||||
exit(1);
|
||||
}
|
||||
int n = strlen(tmp) -1;
|
||||
|
||||
char * str = malloc(n+1);
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include "localcast.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
table handles
|
||||
string msg
|
||||
lightuserdata ptr
|
||||
integer sz
|
||||
*/
|
||||
static int
|
||||
_pack_message(lua_State *L) {
|
||||
luaL_checktype(L,1,LUA_TTABLE);
|
||||
int type = lua_type(L,2);
|
||||
void * msg = NULL;
|
||||
size_t sz = 0;
|
||||
switch(type) {
|
||||
case LUA_TSTRING: {
|
||||
const char * str = lua_tolstring(L,2,&sz);
|
||||
msg = malloc(sz);
|
||||
memcpy(msg, str, sz);
|
||||
break;
|
||||
}
|
||||
case LUA_TLIGHTUSERDATA:
|
||||
msg = lua_touserdata(L,2);
|
||||
sz = luaL_checkinteger(L,3);
|
||||
break;
|
||||
default:
|
||||
luaL_error(L, "type error : %s", lua_typename(L,type));
|
||||
break;
|
||||
}
|
||||
struct localcast *lc = malloc(sizeof(struct localcast));
|
||||
lc->n = lua_rawlen(L,1);
|
||||
uint32_t *group = malloc(lc->n * sizeof(uint32_t));
|
||||
int i;
|
||||
for (i=0;i<lc->n;i++) {
|
||||
lua_rawgeti(L,1,i+1);
|
||||
group[i] = lua_tounsigned(L,-1);
|
||||
lua_pop(L,1);
|
||||
}
|
||||
lc->msg = msg;
|
||||
lc->sz = sz;
|
||||
lc->group = group;
|
||||
lua_pushlightuserdata(L,lc);
|
||||
lua_pushinteger(L, sizeof(*lc));
|
||||
return 2;
|
||||
};
|
||||
|
||||
int
|
||||
luaopen_mcast_c(lua_State *L) {
|
||||
luaL_checkversion(L);
|
||||
lua_pushcfunction(L, _pack_message);
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
#include "skynet.h"
|
||||
#include "trace_service.h"
|
||||
#include "lua-seri.h"
|
||||
#include "service_lua.h"
|
||||
#include "timingqueue.h"
|
||||
|
||||
#define KNRM "\x1B[0m"
|
||||
#define KRED "\x1B[31m"
|
||||
@@ -12,89 +10,10 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <time.h>
|
||||
|
||||
#if defined(__APPLE__)
|
||||
#include <mach/task.h>
|
||||
#include <mach/mach.h>
|
||||
#endif
|
||||
|
||||
struct stat {
|
||||
lua_State *L;
|
||||
int count;
|
||||
uint32_t ti_sec;
|
||||
uint32_t ti_nsec;
|
||||
struct trace_pool *trace;
|
||||
struct tqueue * tq;
|
||||
struct snlua *lua;
|
||||
};
|
||||
|
||||
static void
|
||||
_stat_begin(struct stat *S, struct timespec *ti) {
|
||||
S->count++;
|
||||
current_time(ti);
|
||||
}
|
||||
|
||||
inline static void
|
||||
_stat_end(struct stat *S, struct timespec *ti) {
|
||||
diff_time(ti, &S->ti_sec, &S->ti_nsec);
|
||||
}
|
||||
|
||||
static int
|
||||
_stat(lua_State *L) {
|
||||
lua_rawgetp(L, LUA_REGISTRYINDEX, _stat);
|
||||
struct stat *S = lua_touserdata(L,-1);
|
||||
if (S==NULL) {
|
||||
luaL_error(L, "set callback first");
|
||||
}
|
||||
const char * what = luaL_checkstring(L,1);
|
||||
if (strcmp(what,"count")==0) {
|
||||
lua_pushinteger(L, S->count);
|
||||
return 1;
|
||||
}
|
||||
if (strcmp(what,"time")==0) {
|
||||
double t = (double)S->ti_sec + (double)S->ti_nsec / NANOSEC;
|
||||
lua_pushnumber(L, t);
|
||||
return 1;
|
||||
}
|
||||
if (strcmp(what,"trace")==0) {
|
||||
lua_pushlightuserdata(L, S->trace);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline double
|
||||
current_time_tick(struct stat *S) {
|
||||
return (double)S->ti_sec + (double)S->ti_nsec / NANOSEC;
|
||||
}
|
||||
|
||||
static struct stat *
|
||||
get_stat(lua_State *L) {
|
||||
struct stat * S = lua_touserdata(L, lua_upvalueindex(2));
|
||||
if (S == NULL) {
|
||||
lua_rawgetp(L, LUA_REGISTRYINDEX, _stat);
|
||||
S = lua_touserdata(L, -1);
|
||||
lua_replace(L, lua_upvalueindex(2));
|
||||
}
|
||||
|
||||
return S;
|
||||
}
|
||||
|
||||
static inline void
|
||||
save_session(lua_State *L, int type, int session) {
|
||||
if (session > 0 && (type & 0xff) != PTYPE_RESPONSE) {
|
||||
struct stat * S = get_stat(L);
|
||||
tqueue_push(S->tq, session, current_time_tick(S));
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
_cb(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) {
|
||||
struct stat *S = ud;
|
||||
lua_State *L = S->L;
|
||||
struct timespec ti;
|
||||
_stat_begin(S, &ti);
|
||||
lua_State *L = ud;
|
||||
int trace = 1;
|
||||
int r;
|
||||
int top = lua_gettop(L);
|
||||
@@ -111,45 +30,9 @@ _cb(struct skynet_context * context, void * ud, int type, int session, uint32_t
|
||||
lua_pushinteger(L, session);
|
||||
lua_pushnumber(L, source);
|
||||
|
||||
if (type == PTYPE_RESPONSE && session > 0) {
|
||||
double t = tqueue_pop(S->tq, session);
|
||||
if (t != 0) {
|
||||
t = current_time_tick(S) - t;
|
||||
lua_pushnumber(L, t);
|
||||
r = lua_pcall(L, 6, 0 , trace);
|
||||
} else {
|
||||
r = lua_pcall(L, 5, 0 , trace);
|
||||
}
|
||||
} else {
|
||||
r = lua_pcall(L, 5, 0 , trace);
|
||||
}
|
||||
|
||||
_stat_end(S, &ti);
|
||||
|
||||
struct trace_info *tti = trace_yield(S->trace);
|
||||
if (tti) {
|
||||
skynet_error(context, "Untraced time %f", trace_delete(S->trace, tti));
|
||||
}
|
||||
r = lua_pcall(L, 5, 0 , trace);
|
||||
|
||||
if (r == LUA_OK) {
|
||||
if (S->lua->reload) {
|
||||
skynet_callback(context, NULL, 0);
|
||||
struct snlua * lua = S->lua;
|
||||
assert(lua->L == L);
|
||||
const char * cmd = lua->reload;
|
||||
lua->reload = NULL;
|
||||
lua->L = luaL_newstate();
|
||||
int err = lua->init(lua, context, cmd);
|
||||
if (err) {
|
||||
skynet_callback(context, S, _cb);
|
||||
skynet_error(context, "lua reload failed : %s", cmd);
|
||||
lua_close(lua->L);
|
||||
lua->L = L;
|
||||
} else {
|
||||
skynet_error(context, "lua reload %s", cmd);
|
||||
lua_close(L);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
const char * self = skynet_command(context, "REG", NULL);
|
||||
@@ -173,34 +56,9 @@ _cb(struct skynet_context * context, void * ud, int type, int session, uint32_t
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
_timing(lua_State *L) {
|
||||
int session = luaL_checkinteger(L,1);
|
||||
struct stat * S = get_stat(L);
|
||||
double t = tqueue_pop(S->tq, session);
|
||||
if (t != 0) {
|
||||
t = current_time_tick(S) - t;
|
||||
}
|
||||
lua_pushnumber(L, t);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
_delete_stat(lua_State *L) {
|
||||
struct stat * S = lua_touserdata(L,1);
|
||||
trace_release(S->trace);
|
||||
tqueue_delete(S->tq);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
_callback(lua_State *L) {
|
||||
struct snlua *lua = lua_touserdata(L, lua_upvalueindex(1));
|
||||
if (lua == NULL || lua->ctx == NULL) {
|
||||
return luaL_error(L, "Init skynet context first");
|
||||
}
|
||||
struct skynet_context * context = lua->ctx;
|
||||
struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
|
||||
|
||||
luaL_checktype(L,1,LUA_TFUNCTION);
|
||||
lua_settop(L,1);
|
||||
@@ -209,21 +67,7 @@ _callback(lua_State *L) {
|
||||
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD);
|
||||
lua_State *gL = lua_tothread(L,-1);
|
||||
|
||||
struct stat * S = lua_newuserdata(L, sizeof(*S));
|
||||
memset(S, 0, sizeof(*S));
|
||||
S->L = gL;
|
||||
S->trace = trace_create();
|
||||
S->tq = tqueue_new();
|
||||
S->lua = lua;
|
||||
|
||||
lua_createtable(L,0,1);
|
||||
lua_pushcfunction(L, _delete_stat);
|
||||
lua_setfield(L,-2,"__gc");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
lua_rawsetp(L, LUA_REGISTRYINDEX, _stat);
|
||||
|
||||
skynet_callback(context, S, _cb);
|
||||
skynet_callback(context, gL, _cb);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -272,19 +116,16 @@ _sendname(lua_State *L, struct skynet_context * context, const char * dest) {
|
||||
size_t len = 0;
|
||||
void * msg = (void *)lua_tolstring(L,4,&len);
|
||||
session = skynet_sendname(context, dest, type, session , msg, len);
|
||||
save_session(L, type, session);
|
||||
break;
|
||||
}
|
||||
case LUA_TNIL :
|
||||
session = skynet_sendname(context, dest, type, session , NULL, 0);
|
||||
save_session(L, type, session);
|
||||
break;
|
||||
case LUA_TLIGHTUSERDATA: {
|
||||
luaL_checktype(L, 4, LUA_TLIGHTUSERDATA);
|
||||
void * msg = lua_touserdata(L,4);
|
||||
int size = luaL_checkinteger(L,5);
|
||||
session = skynet_sendname(context, dest, type | PTYPE_TAG_DONTCOPY, session, msg, size);
|
||||
save_session(L, type, session);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@@ -350,14 +191,12 @@ _send(lua_State *L) {
|
||||
msg = NULL;
|
||||
}
|
||||
session = skynet_send(context, 0, dest, type, session , msg, len);
|
||||
save_session(L, type, session);
|
||||
break;
|
||||
}
|
||||
case LUA_TLIGHTUSERDATA: {
|
||||
void * msg = lua_touserdata(L,4);
|
||||
int size = luaL_checkinteger(L,5);
|
||||
session = skynet_send(context, 0, dest, type | PTYPE_TAG_DONTCOPY, session, msg, size);
|
||||
save_session(L, type, session);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@@ -389,14 +228,12 @@ _redirect(lua_State *L) {
|
||||
msg = NULL;
|
||||
}
|
||||
session = skynet_send(context, source, dest, type, session , msg, len);
|
||||
save_session(L, type, session);
|
||||
break;
|
||||
}
|
||||
case LUA_TLIGHTUSERDATA: {
|
||||
void * msg = lua_touserdata(L,5);
|
||||
int size = luaL_checkinteger(L,6);
|
||||
session = skynet_send(context, source, dest, type | PTYPE_TAG_DONTCOPY, session, msg, size);
|
||||
save_session(L, type, session);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@@ -405,15 +242,6 @@ _redirect(lua_State *L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
_forward(lua_State *L) {
|
||||
struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
|
||||
uint32_t dest = luaL_checkunsigned(L,1);
|
||||
skynet_forward(context, dest);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
_error(lua_State *L) {
|
||||
struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
|
||||
@@ -444,75 +272,6 @@ _harbor(lua_State *L) {
|
||||
return 2;
|
||||
}
|
||||
|
||||
static int
|
||||
_context(lua_State *L) {
|
||||
struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
|
||||
lua_pushlightuserdata(L, context);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// trace api
|
||||
static int
|
||||
_trace_new(lua_State *L) {
|
||||
struct trace_pool *p = lua_touserdata(L,1);
|
||||
struct trace_info *t = trace_new(p);
|
||||
if (t==NULL) {
|
||||
return luaL_error(L, "Last trace didn't close");
|
||||
}
|
||||
lua_pushlightuserdata(L,t);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
_trace_delete(lua_State *L) {
|
||||
struct trace_pool *p = lua_touserdata(L,1);
|
||||
struct trace_info *t = lua_touserdata(L,2);
|
||||
double ti = trace_delete(p,t);
|
||||
lua_pushnumber(L, ti);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
_trace_switch(lua_State *L) {
|
||||
int session = luaL_checkinteger(L,2);
|
||||
if (session <=0)
|
||||
return 0;
|
||||
struct trace_pool *p = lua_touserdata(L,1);
|
||||
trace_switch(p, session);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
_trace_yield(lua_State *L) {
|
||||
struct trace_pool *p = lua_touserdata(L,1);
|
||||
struct trace_info * t = trace_yield(p);
|
||||
if (t) {
|
||||
lua_pushlightuserdata(L,t);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
_trace_register(lua_State *L) {
|
||||
int session = luaL_checkinteger(L,2);
|
||||
if (session <=0)
|
||||
return 0;
|
||||
struct trace_pool *p = lua_touserdata(L,1);
|
||||
trace_register(p, session);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
_reload(lua_State *L) {
|
||||
struct snlua *lua = lua_touserdata(L,lua_upvalueindex(1));
|
||||
lua->reload = luaL_checkstring(L,1);
|
||||
lua_settop(L,1);
|
||||
lua_replace(L,lua_upvalueindex(2));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
luaopen_skynet_c(lua_State *L) {
|
||||
luaL_checkversion(L);
|
||||
@@ -520,31 +279,17 @@ luaopen_skynet_c(lua_State *L) {
|
||||
luaL_Reg l[] = {
|
||||
{ "send" , _send },
|
||||
{ "genid", _genid },
|
||||
{ "timing", _timing },
|
||||
{ "redirect", _redirect },
|
||||
{ "forward", _forward },
|
||||
{ "command" , _command },
|
||||
{ "error", _error },
|
||||
{ "tostring", _tostring },
|
||||
{ "harbor", _harbor },
|
||||
{ "context", _context },
|
||||
{ "pack", _luaseri_pack },
|
||||
{ "unpack", _luaseri_unpack },
|
||||
{ "callback", _callback },
|
||||
{ NULL, NULL },
|
||||
};
|
||||
|
||||
luaL_Reg l2[] = {
|
||||
{ "stat", _stat },
|
||||
{ "trace_new", _trace_new },
|
||||
{ "trace_delete", _trace_delete },
|
||||
{ "trace_switch", _trace_switch },
|
||||
{ "trace_yield", _trace_yield },
|
||||
{ "trace_register", _trace_register },
|
||||
{ NULL, NULL },
|
||||
};
|
||||
|
||||
lua_createtable(L, 0, (sizeof(l) + sizeof(l2))/sizeof(luaL_Reg)-2);
|
||||
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, "skynet_lua");
|
||||
struct snlua *lua = lua_touserdata(L,-1);
|
||||
if (lua == NULL || lua->ctx == NULL) {
|
||||
@@ -552,19 +297,9 @@ luaopen_skynet_c(lua_State *L) {
|
||||
}
|
||||
assert(lua->L == L);
|
||||
|
||||
lua_pushvalue(L,-1);
|
||||
lua_pushcclosure(L,_callback,1);
|
||||
lua_setfield(L, -3, "callback");
|
||||
|
||||
lua_pushnil(L);
|
||||
lua_pushcclosure(L,_reload,2);
|
||||
lua_setfield(L, -2, "reload");
|
||||
|
||||
luaL_newlibtable(L, l);
|
||||
lua_pushlightuserdata(L, lua->ctx);
|
||||
lua_pushnil(L);
|
||||
luaL_setfuncs(L,l,2);
|
||||
|
||||
luaL_setfuncs(L,l2,0);
|
||||
luaL_setfuncs(L,l,1);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -1,197 +0,0 @@
|
||||
#include "timingqueue.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define INIT_CAP 8
|
||||
|
||||
struct session_time {
|
||||
int session;
|
||||
double time;
|
||||
};
|
||||
|
||||
struct tqueue {
|
||||
int cap;
|
||||
int n;
|
||||
int head;
|
||||
int tail;
|
||||
struct session_time * q;
|
||||
};
|
||||
|
||||
struct tqueue *
|
||||
tqueue_new() {
|
||||
struct tqueue * tq = malloc(sizeof(*tq));
|
||||
tq->cap = INIT_CAP;
|
||||
tq->head = 0;
|
||||
tq->tail = 0;
|
||||
tq->n = 0;
|
||||
tq->q = malloc(sizeof(struct session_time) * tq->cap);
|
||||
return tq;
|
||||
}
|
||||
|
||||
void
|
||||
tqueue_delete(struct tqueue *tq) {
|
||||
free(tq->q);
|
||||
free(tq);
|
||||
}
|
||||
|
||||
static struct session_time *
|
||||
expand_queue(struct tqueue *tq) {
|
||||
if (tq->n == tq->cap) {
|
||||
struct session_time * q = malloc(sizeof(*q) * tq->cap * 2);
|
||||
int i;
|
||||
for (i=0;i<tq->cap;i++) {
|
||||
int index = tq->head + i;
|
||||
if (index >= tq->cap) {
|
||||
index -= tq->cap;
|
||||
}
|
||||
q[i] = tq->q[index];
|
||||
}
|
||||
tq->head = 0;
|
||||
tq->tail = tq->cap;
|
||||
tq->cap *= 2;
|
||||
free(tq->q);
|
||||
tq->q = q;
|
||||
return &q[tq->tail-1];
|
||||
} else {
|
||||
int i;
|
||||
for (i=0;i<tq->cap-1;i++) {
|
||||
int index = tq->head + i;
|
||||
if (index >= tq->cap) {
|
||||
index -= tq->cap;
|
||||
}
|
||||
if (tq->q[index].session == 0)
|
||||
break;
|
||||
}
|
||||
int p = i + tq->head;
|
||||
if (p >= tq->cap)
|
||||
p -= tq->cap;
|
||||
for (++i;i<tq->cap-1;i++) {
|
||||
int index = tq->head + i;
|
||||
if (index >= tq->cap) {
|
||||
index -= tq->cap;
|
||||
}
|
||||
if (tq->q[index].session == 0) {
|
||||
continue;
|
||||
}
|
||||
tq->q[p] = tq->q[index];
|
||||
++ p;
|
||||
if (p >= tq->cap) {
|
||||
p -= tq->cap;
|
||||
}
|
||||
}
|
||||
tq->tail = p + 1;
|
||||
if (tq->tail >= tq->cap) {
|
||||
tq->tail -= tq->cap;
|
||||
}
|
||||
return &tq->q[p];
|
||||
}
|
||||
}
|
||||
|
||||
static inline struct session_time *
|
||||
last_one(struct tqueue * tq, struct session_time *current) {
|
||||
--current;
|
||||
if (current < tq->q) {
|
||||
return &tq->q[tq->cap-1];
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
void
|
||||
tqueue_push(struct tqueue *tq, int session, double time) {
|
||||
assert(session !=0);
|
||||
++ tq->n;
|
||||
if (tq->head == tq->tail) {
|
||||
// queue is empty;
|
||||
tq->head = 0;
|
||||
tq->tail = 1;
|
||||
tq->q[0].session = session;
|
||||
tq->q[0].time = time;
|
||||
return;
|
||||
}
|
||||
struct session_time * st = &tq->q[tq->tail++];
|
||||
if (tq->tail >= tq->cap) {
|
||||
tq->tail = 0;
|
||||
}
|
||||
if (tq->tail == tq->head) {
|
||||
st = expand_queue(tq);
|
||||
}
|
||||
st->session = session;
|
||||
st->time = time;
|
||||
|
||||
// session must great than last one
|
||||
int i;
|
||||
for (i=1;i<tq->n;i++) {
|
||||
struct session_time *last = last_one(tq, st);
|
||||
if (session > last->session)
|
||||
return;
|
||||
// swap st, last
|
||||
struct session_time temp = *last;
|
||||
*last = *st;
|
||||
*st = temp;
|
||||
|
||||
st = last;
|
||||
}
|
||||
}
|
||||
|
||||
double
|
||||
tqueue_pop(struct tqueue *tq, int session) {
|
||||
if (tq->head == tq->tail) {
|
||||
return 0;
|
||||
}
|
||||
if (session == tq->q[tq->head].session) {
|
||||
--tq->n;
|
||||
double ret = tq->q[tq->head].time;
|
||||
do {
|
||||
++tq->head;
|
||||
if (tq->head == tq->cap) {
|
||||
tq->head = 0;
|
||||
}
|
||||
} while (tq->head != tq->tail && tq->q[tq->head].session == 0);
|
||||
return ret;
|
||||
}
|
||||
// binary search session
|
||||
int n = tq->tail - tq->head;
|
||||
if (n < 0) {
|
||||
n += tq->cap;
|
||||
}
|
||||
int begin = 1;
|
||||
int end = n;
|
||||
while (begin < end) {
|
||||
int mid = (begin + end)/2;
|
||||
int index = mid + tq->head;
|
||||
if (index >= tq->cap) {
|
||||
index -= tq->cap;
|
||||
}
|
||||
struct session_time *st = &tq->q[index];
|
||||
if (st->session == session) {
|
||||
--tq->n;
|
||||
st->session = 0;
|
||||
return st->time;
|
||||
}
|
||||
if (session > st->session) {
|
||||
begin = mid + 1;
|
||||
} else {
|
||||
end = mid;
|
||||
}
|
||||
}
|
||||
// not found
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
tqueue_dump(struct tqueue *tq) {
|
||||
printf("cap = %d, head = %d, tail = %d, n = %d\n",
|
||||
tq->cap, tq->head, tq->tail, tq->n);
|
||||
int head = tq->head;
|
||||
while (head!=tq->tail) {
|
||||
struct session_time *st = &tq->q[head];
|
||||
if (st->session != 0) {
|
||||
printf("%d : %f\n", st->session, st->time);
|
||||
}
|
||||
++ head;
|
||||
if (head >= tq->cap)
|
||||
head -= tq->cap;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
#ifndef skynet_timing_queue_h
|
||||
#define skynet_timing_queue_h
|
||||
|
||||
struct tqueue * tqueue_new();
|
||||
void tqueue_delete(struct tqueue *);
|
||||
|
||||
void tqueue_push(struct tqueue *, int session, double time);
|
||||
double tqueue_pop(struct tqueue *, int session);
|
||||
|
||||
// for debug
|
||||
void tqueue_dump(struct tqueue *tq);
|
||||
|
||||
#endif
|
||||
@@ -1,172 +0,0 @@
|
||||
#include "trace_service.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <time.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define HASH_SIZE 32
|
||||
|
||||
#if defined(__APPLE__)
|
||||
#include <mach/task.h>
|
||||
#include <mach/mach.h>
|
||||
#endif
|
||||
|
||||
void
|
||||
current_time(struct timespec *ti) {
|
||||
#if !defined(__APPLE__)
|
||||
clock_gettime(CLOCK_THREAD_CPUTIME_ID, ti);
|
||||
#else
|
||||
struct task_thread_times_info aTaskInfo;
|
||||
mach_msg_type_number_t aTaskInfoCount = TASK_THREAD_TIMES_INFO_COUNT;
|
||||
assert(KERN_SUCCESS == task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t )&aTaskInfo, &aTaskInfoCount));
|
||||
ti->tv_sec = aTaskInfo.user_time.seconds;
|
||||
ti->tv_nsec = aTaskInfo.user_time.microseconds * 1000;
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
diff_time(struct timespec *ti, uint32_t *sec, uint32_t *nsec) {
|
||||
struct timespec end;
|
||||
current_time(&end);
|
||||
int diffsec = end.tv_sec - ti->tv_sec;
|
||||
assert(diffsec>=0);
|
||||
int diffnsec = end.tv_nsec - ti->tv_nsec;
|
||||
if (diffnsec < 0) {
|
||||
--diffsec;
|
||||
diffnsec += NANOSEC;
|
||||
}
|
||||
*nsec += diffnsec;
|
||||
if (*nsec > NANOSEC) {
|
||||
++*sec;
|
||||
*nsec -= NANOSEC;
|
||||
}
|
||||
*sec += diffsec;
|
||||
}
|
||||
|
||||
struct trace_info {
|
||||
int session;
|
||||
struct trace_info * prev;
|
||||
struct trace_info * next;
|
||||
struct timespec ti;
|
||||
uint32_t ti_sec;
|
||||
uint32_t ti_nsec;
|
||||
};
|
||||
|
||||
struct trace_pool {
|
||||
struct trace_info * current;
|
||||
struct trace_info * slot[HASH_SIZE];
|
||||
};
|
||||
|
||||
struct trace_pool *
|
||||
trace_create() {
|
||||
struct trace_pool * p = malloc(sizeof(*p));
|
||||
memset(p, 0, sizeof(*p));
|
||||
return p;
|
||||
}
|
||||
|
||||
static void
|
||||
_free_slot(struct trace_info *t) {
|
||||
while (t) {
|
||||
struct trace_info *next = t->next;
|
||||
free(t);
|
||||
t = next;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
trace_release(struct trace_pool *p) {
|
||||
int i;
|
||||
for (i=0;i<HASH_SIZE;i++) {
|
||||
_free_slot(p->slot[i]);
|
||||
}
|
||||
free(p->current);
|
||||
}
|
||||
|
||||
struct trace_info *
|
||||
trace_new(struct trace_pool *p) {
|
||||
if (p->current) {
|
||||
return NULL;
|
||||
}
|
||||
struct trace_info *t = malloc(sizeof(*t));
|
||||
p->current = t;
|
||||
t->session = 0;
|
||||
t->prev = NULL;
|
||||
t->next = NULL;
|
||||
t->ti_sec = 0;
|
||||
t->ti_nsec = 0;
|
||||
current_time(&t->ti);
|
||||
return t;
|
||||
}
|
||||
|
||||
void
|
||||
trace_register(struct trace_pool *p, int session) {
|
||||
struct trace_info *t = p->current;
|
||||
if (t == NULL) {
|
||||
return;
|
||||
}
|
||||
int hash = session % HASH_SIZE;
|
||||
assert(t->session == 0 && session > 0);
|
||||
t->session = session;
|
||||
t->prev = NULL;
|
||||
t->next = p->slot[hash];
|
||||
if (p->slot[hash]) {
|
||||
p->slot[hash]->prev = t;
|
||||
}
|
||||
p->slot[hash] = t;
|
||||
}
|
||||
|
||||
struct trace_info *
|
||||
trace_yield(struct trace_pool *p) {
|
||||
struct trace_info *t = p->current;
|
||||
if (t == NULL)
|
||||
return NULL;
|
||||
|
||||
diff_time(&t->ti,&t->ti_sec,&t->ti_nsec);
|
||||
|
||||
if (t->session == 0) {
|
||||
return t;
|
||||
} else {
|
||||
p->current = NULL;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
trace_switch(struct trace_pool *p, int session) {
|
||||
assert(p->current == NULL && session > 0);
|
||||
struct trace_info *t = p->slot[session % HASH_SIZE];
|
||||
struct trace_info *prev = NULL;
|
||||
while (t) {
|
||||
if (t->session == session) {
|
||||
p->current = t;
|
||||
t->session = 0;
|
||||
if (prev == NULL) {
|
||||
p->slot[session % HASH_SIZE] = t->next;
|
||||
} else {
|
||||
prev->next = t->next;
|
||||
}
|
||||
if (t->next) {
|
||||
t->next->prev = prev;
|
||||
}
|
||||
current_time(&t->ti);
|
||||
return;
|
||||
}
|
||||
prev = t;
|
||||
t=t->next;
|
||||
}
|
||||
}
|
||||
|
||||
double
|
||||
trace_delete(struct trace_pool *p, struct trace_info *t) {
|
||||
assert(p->current == t);
|
||||
p->current = NULL;
|
||||
if (t) {
|
||||
double ti = (double)t->ti_sec + (double)t->ti_nsec / NANOSEC;
|
||||
free(t);
|
||||
return ti;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
#ifndef TRACE_SERVICE_H
|
||||
#define TRACE_SERVICE_H
|
||||
|
||||
#include <time.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define NANOSEC 1000000000
|
||||
|
||||
void current_time(struct timespec *ti);
|
||||
void diff_time(struct timespec *ti, uint32_t *sec, uint32_t *nsec);
|
||||
|
||||
struct trace_pool;
|
||||
struct trace_info;
|
||||
|
||||
struct trace_pool * trace_create();
|
||||
void trace_release(struct trace_pool *);
|
||||
struct trace_info * trace_new(struct trace_pool *);
|
||||
void trace_register(struct trace_pool *, int session);
|
||||
void trace_switch(struct trace_pool *, int session);
|
||||
struct trace_info * trace_yield(struct trace_pool *);
|
||||
double trace_delete(struct trace_pool *, struct trace_info *);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user