Skip to content

Commit

Permalink
Merge pull request cloudwu#5 from cloudwu/master
Browse files Browse the repository at this point in the history
Update to date
  • Loading branch information
coolflyreg committed Dec 22, 2015
2 parents 8682316 + 7acf6d8 commit 066a024
Show file tree
Hide file tree
Showing 10 changed files with 320 additions and 25 deletions.
7 changes: 7 additions & 0 deletions lualib-src/lua-memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ lexpandshrtbl(lua_State *L) {
return 0;
}

static int
lcurrent(lua_State *L) {
lua_pushinteger(L, malloc_current_memory());
return 1;
}

int
luaopen_memory(lua_State *L) {
luaL_checkversion(L);
Expand All @@ -53,6 +59,7 @@ luaopen_memory(lua_State *L) {
{ "info", dump_mem_lua },
{ "ssinfo", luaS_shrinfo },
{ "ssexpand", lexpandshrtbl },
{ "current", lcurrent },
{ NULL, NULL },
};

Expand Down
99 changes: 88 additions & 11 deletions lualib-src/lua-profile.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>

Expand All @@ -11,6 +12,8 @@
#define NANOSEC 1000000000
#define MICROSEC 1000000

// #define DEBUG_LOG

static double
get_time() {
#if !defined(__APPLE__)
Expand Down Expand Up @@ -47,7 +50,11 @@ diff_time(double start) {

static int
lstart(lua_State *L) {
lua_pushthread(L);
if (lua_type(L,1) == LUA_TTHREAD) {
lua_settop(L,1);
} else {
lua_pushthread(L);
}
lua_rawget(L, lua_upvalueindex(2));
if (!lua_isnil(L, -1)) {
return luaL_error(L, "Thread %p start profile more than once", lua_topointer(L, 1));
Expand All @@ -57,17 +64,27 @@ lstart(lua_State *L) {
lua_rawset(L, lua_upvalueindex(2));

lua_pushthread(L);
lua_pushnumber(L, get_time());
double ti = get_time();
#ifdef DEBUG_LOG
fprintf(stderr, "PROFILE [%p] start\n", L);
#endif
lua_pushnumber(L, ti);
lua_rawset(L, lua_upvalueindex(1));

return 0;
}

static int
lstop(lua_State *L) {
lua_pushthread(L);
if (lua_type(L,1) == LUA_TTHREAD) {
lua_settop(L,1);
} else {
lua_pushthread(L);
}
lua_rawget(L, lua_upvalueindex(1));
luaL_checktype(L, -1, LUA_TNUMBER);
if (lua_type(L, -1) != LUA_TNUMBER) {
return luaL_error(L, "Call profile.start() before profile.stop()");
}
double ti = diff_time(lua_tonumber(L, -1));
lua_pushthread(L);
lua_rawget(L, lua_upvalueindex(2));
Expand All @@ -81,21 +98,30 @@ lstop(lua_State *L) {
lua_pushnil(L);
lua_rawset(L, lua_upvalueindex(2));

lua_pushnumber(L, ti + total_time);
total_time += ti;
lua_pushnumber(L, total_time);
#ifdef DEBUG_LOG
fprintf(stderr, "PROFILE [%p] stop (%lf / %lf)\n", L, ti, total_time);
#endif

return 1;
}

static int
lresume(lua_State *L) {
lua_pushvalue(L,1);
timing_resume(lua_State *L) {
#ifdef DEBUG_LOG
lua_State *from = lua_tothread(L, -1);
#endif
lua_rawget(L, lua_upvalueindex(2));
if (lua_isnil(L, -1)) { // check total time
lua_pop(L,1);
} else {
lua_pop(L,1);
lua_pushvalue(L,1);
double ti = get_time();
#ifdef DEBUG_LOG
fprintf(stderr, "PROFILE [%p] resume\n", from);
#endif
lua_pushnumber(L, ti);
lua_rawset(L, lua_upvalueindex(1)); // set start time
}
Expand All @@ -106,8 +132,25 @@ lresume(lua_State *L) {
}

static int
lyield(lua_State *L) {
lua_pushthread(L);
lresume(lua_State *L) {
lua_pushvalue(L,1);

return timing_resume(L);
}

static int
lresume_co(lua_State *L) {
luaL_checktype(L, 2, LUA_TTHREAD);
lua_rotate(L, 2, -1);

return timing_resume(L);
}

static int
timing_yield(lua_State *L) {
#ifdef DEBUG_LOG
lua_State *from = lua_tothread(L, -1);
#endif
lua_rawget(L, lua_upvalueindex(2)); // check total time
if (lua_isnil(L, -1)) {
lua_pop(L,1);
Expand All @@ -120,7 +163,11 @@ lyield(lua_State *L) {
double starttime = lua_tonumber(L, -1);
lua_pop(L,1);

ti += diff_time(starttime);
double diff = diff_time(starttime);
ti += diff;
#ifdef DEBUG_LOG
fprintf(stderr, "PROFILE [%p] yield (%lf/%lf)\n", from, diff, ti);
#endif

lua_pushthread(L);
lua_pushnumber(L, ti);
Expand All @@ -132,6 +179,21 @@ lyield(lua_State *L) {
return co_yield(L);
}

static int
lyield(lua_State *L) {
lua_pushthread(L);

return timing_yield(L);
}

static int
lyield_co(lua_State *L) {
luaL_checktype(L, 1, LUA_TTHREAD);
lua_rotate(L, 1, -1);

return timing_yield(L);
}

int
luaopen_profile(lua_State *L) {
luaL_checkversion(L);
Expand All @@ -140,6 +202,8 @@ luaopen_profile(lua_State *L) {
{ "stop", lstop },
{ "resume", lresume },
{ "yield", lyield },
{ "resume_co", lresume_co },
{ "yield_co", lyield_co },
{ NULL, NULL },
};
luaL_newlibtable(L,l);
Expand All @@ -154,7 +218,7 @@ luaopen_profile(lua_State *L) {
lua_setmetatable(L, -3);
lua_setmetatable(L, -3);

lua_pushnil(L);
lua_pushnil(L); // cfunction (coroutine.resume or coroutine.yield)
luaL_setfuncs(L,l,3);

int libtable = lua_gettop(L);
Expand All @@ -166,20 +230,33 @@ luaopen_profile(lua_State *L) {
if (co_resume == NULL)
return luaL_error(L, "Can't get coroutine.resume");
lua_pop(L,1);

lua_getfield(L, libtable, "resume");
lua_pushcfunction(L, co_resume);
lua_setupvalue(L, -2, 3);
lua_pop(L,1);

lua_getfield(L, libtable, "resume_co");
lua_pushcfunction(L, co_resume);
lua_setupvalue(L, -2, 3);
lua_pop(L,1);

lua_getfield(L, -1, "yield");

lua_CFunction co_yield = lua_tocfunction(L, -1);
if (co_yield == NULL)
return luaL_error(L, "Can't get coroutine.yield");
lua_pop(L,1);

lua_getfield(L, libtable, "yield");
lua_pushcfunction(L, co_yield);
lua_setupvalue(L, -2, 3);
lua_pop(L,1);

lua_getfield(L, libtable, "yield_co");
lua_pushcfunction(L, co_yield);
lua_setupvalue(L, -2, 3);
lua_pop(L,1);

lua_settop(L, libtable);

Expand Down
26 changes: 14 additions & 12 deletions lualib/skynet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ local pcall = pcall

local profile = require "profile"

coroutine.resume = profile.resume
coroutine.yield = profile.yield
local coroutine_resume = profile.resume
local coroutine_yield = profile.yield

local proto = {}
local skynet = {
Expand Down Expand Up @@ -69,7 +69,7 @@ local function dispatch_error_queue()
if session then
local co = session_id_coroutine[session]
session_id_coroutine[session] = nil
return suspend(co, coroutine.resume(co, false))
return suspend(co, coroutine_resume(co, false))
end
end

Expand All @@ -96,7 +96,6 @@ end
-- coroutine reuse

local coroutine_pool = {}
local coroutine_yield = coroutine.yield

local function co_create(f)
local co = table.remove(coroutine_pool)
Expand All @@ -111,7 +110,7 @@ local function co_create(f)
end
end)
else
coroutine.resume(co, f)
coroutine_resume(co, f)
end
return co
end
Expand All @@ -123,7 +122,7 @@ local function dispatch_wakeup()
local session = sleep_session[co]
if session then
session_id_coroutine[session] = "BREAK"
return suspend(co, coroutine.resume(co, false, "BREAK"))
return suspend(co, coroutine_resume(co, false, "BREAK"))
end
end
end
Expand Down Expand Up @@ -178,7 +177,7 @@ function suspend(co, result, command, param, size)
c.trash(param, size)
ret = false
end
return suspend(co, coroutine.resume(co, ret))
return suspend(co, coroutine_resume(co, ret))
elseif command == "RESPONSE" then
local co_session = session_coroutine_id[co]
local co_address = session_coroutine_address[co]
Expand Down Expand Up @@ -227,7 +226,7 @@ function suspend(co, result, command, param, size)
watching_service[co_address] = watching_service[co_address] + 1
session_response[co] = true
unresponse[response] = true
return suspend(co, coroutine.resume(co, response))
return suspend(co, coroutine_resume(co, response))
elseif command == "EXIT" then
-- coroutine exit
local address = session_coroutine_address[co]
Expand All @@ -238,6 +237,9 @@ function suspend(co, result, command, param, size)
elseif command == "QUIT" then
-- service exit
return
elseif command == "USER" then
-- See skynet.coutine for detail
error("Call skynet.coroutine.yield out of skynet.coroutine.resume\n" .. debug.traceback(co))
elseif command == nil then
-- debug trace
return
Expand Down Expand Up @@ -458,7 +460,7 @@ function skynet.fork(func,...)
return co
end

local function raw_dispatch_message(prototype, msg, sz, session, source, ...)
local function raw_dispatch_message(prototype, msg, sz, session, source)
-- skynet.PTYPE_RESPONSE = 1, read skynet.h
if prototype == 1 then
local co = session_id_coroutine[session]
Expand All @@ -468,7 +470,7 @@ local function raw_dispatch_message(prototype, msg, sz, session, source, ...)
unknown_response(session, source, msg, sz)
else
session_id_coroutine[session] = nil
suspend(co, coroutine.resume(co, true, msg, sz))
suspend(co, coroutine_resume(co, true, msg, sz))
end
else
local p = proto[prototype]
Expand All @@ -491,7 +493,7 @@ local function raw_dispatch_message(prototype, msg, sz, session, source, ...)
local co = co_create(f)
session_coroutine_id[co] = session
session_coroutine_address[co] = source
suspend(co, coroutine.resume(co, session,source, p.unpack(msg,sz, ...)))
suspend(co, coroutine_resume(co, session,source, p.unpack(msg,sz)))
else
unknown_request(session, source, msg, sz, proto[prototype].name)
end
Expand All @@ -506,7 +508,7 @@ function skynet.dispatch_message(...)
break
end
fork_queue[key] = nil
local fork_succ, fork_err = pcall(suspend,co,coroutine.resume(co))
local fork_succ, fork_err = pcall(suspend,co,coroutine_resume(co))
if not fork_succ then
if succ then
succ = false
Expand Down
Loading

0 comments on commit 066a024

Please sign in to comment.