1. lua-profile.c
// 獲取當前時間毫缆,按秒嚼锄。
double get_time();
// 獲取開始時間到當前時間的時間差,按秒。
double diff_time(double start);
// 初始化profile。
int lstart(lua_State *L);
// 清理profile數(shù)據(jù)。
int lstop(lua_State *L);
// coroutine.resume封裝實現(xiàn)工三。
int timing_resume(lua_State *L);
// coroutine.resume封裝。
int lresume(lua_State *L);
// 兩個coroutine參數(shù)的resume封裝先鱼。
int lresume_co(lua_State *L);
// coroutine.yield封裝實現(xiàn)俭正。
int timing_yield(lua_State *L);
// coroutine.yield封裝。
int lyield(lua_State *L);
// 兩個coroutine參數(shù)的yield封裝焙畔。
int lyield_co(lua_State *L);
// 庫加載函數(shù)掸读。
LUAMOD_API int luaopen_skynet_profile(lua_State *L);
這個文件實現(xiàn)了一個帶profile的coroutine.resume, coroutine.yield版本。如果不調(diào)用lstart宏多,則不會觸發(fā)profile儿惫,可以像coroutine.resume, coroutine.yield一樣使用。為了方便skynet實現(xiàn)伸但,其中添加了兩個特殊的版本:lresume_co和lyield_co肾请。
實現(xiàn)方法
resume-yield累計。使用upvalue存放數(shù)據(jù)更胖,結(jié)構(gòu)如下:
skynet.profile
?upvalues
??1 {meta: {__mode="kv"}} pair(thread, current time)
??2 {meta: {__mode="kv"}} pair(thread, total time)
??3 nil
?resume
??upvalues
???3 coroutine.resume
?resume_co
??upvalues
???3 coroutine.resume
?yield
??upvalues
???3 coroutine.yield
?yield_co
??upvalues
???3 coroutine.yield
加載庫的時候會初始化好這些upvalue铛铁。調(diào)用lstart會給相應(yīng)的coroutine添加時間數(shù)據(jù)隔显。調(diào)用lresume會更新起始時間,調(diào)用lyield會更新總時間饵逐。調(diào)用lstop會清理coroutine上的時間數(shù)據(jù)括眠,并返回總時間。一個coroutine的執(zhí)行時間為:
start ---- resume ---- yield ---- resume ---- yield ---- ... ---- stop
初始化 開始 累計 開始 累計 ... 總計
實現(xiàn)不必非得用upvalue倍权,只要把coroutine-時間對存放到某個地方就好了哺窄。