mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 12:20:41 +00:00
Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c458eba6b4 | ||
|
|
6d51c21c06 | ||
|
|
d2aa2069f7 | ||
|
|
8de9c22fd7 | ||
|
|
d649d0adf1 | ||
|
|
0aa5274799 | ||
|
|
bfc3fd425a | ||
|
|
05f508fb42 | ||
|
|
c8b8dc1276 | ||
|
|
1c62785d71 | ||
|
|
4a8bf4f39a | ||
|
|
2f6bfe9104 | ||
|
|
535bbc7219 |
@@ -1,3 +1,8 @@
|
|||||||
|
v0.7.3 (2014-10-13)
|
||||||
|
-----------
|
||||||
|
* Add some logs (warning) when overload
|
||||||
|
* Bugfix: crash on exit
|
||||||
|
|
||||||
v0.7.2 (2014-9-29)
|
v0.7.2 (2014-9-29)
|
||||||
-----------
|
-----------
|
||||||
* Bugfix : datacenter.wait
|
* Bugfix : datacenter.wait
|
||||||
|
|||||||
@@ -368,7 +368,7 @@ lunpack(lua_State *L) {
|
|||||||
lua_pushinteger(L, message->id);
|
lua_pushinteger(L, message->id);
|
||||||
lua_pushinteger(L, message->ud);
|
lua_pushinteger(L, message->ud);
|
||||||
if (message->buffer == NULL) {
|
if (message->buffer == NULL) {
|
||||||
lua_pushlstring(L, (char *)(message+1),size - sizeof(*message));
|
lua_pushlstring(L, (char *)(message+1),size - sizeof(*message) - 1);
|
||||||
} else {
|
} else {
|
||||||
lua_pushlightuserdata(L, message->buffer);
|
lua_pushlightuserdata(L, message->buffer);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -95,6 +95,7 @@ end
|
|||||||
|
|
||||||
local coroutine_pool = {}
|
local coroutine_pool = {}
|
||||||
local coroutine_yield = coroutine.yield
|
local coroutine_yield = coroutine.yield
|
||||||
|
local coroutine_count = 0
|
||||||
|
|
||||||
local function co_create(f)
|
local function co_create(f)
|
||||||
local co = table.remove(coroutine_pool)
|
local co = table.remove(coroutine_pool)
|
||||||
@@ -109,6 +110,11 @@ local function co_create(f)
|
|||||||
f(coroutine_yield())
|
f(coroutine_yield())
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
coroutine_count = coroutine_count + 1
|
||||||
|
if coroutine_count > 1024 then
|
||||||
|
skynet.error("May overload, create 1024 task")
|
||||||
|
coroutine_count = 0
|
||||||
|
end
|
||||||
else
|
else
|
||||||
coroutine.resume(co, f)
|
coroutine.resume(co, f)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ function gateserver.start(handler)
|
|||||||
local port = assert(conf.port)
|
local port = assert(conf.port)
|
||||||
maxclient = conf.maxclient or 1024
|
maxclient = conf.maxclient or 1024
|
||||||
nodelay = conf.nodelay
|
nodelay = conf.nodelay
|
||||||
|
skynet.error(string.format("Listen on %s:%d", address, port))
|
||||||
socket = socketdriver.listen(address, port)
|
socket = socketdriver.listen(address, port)
|
||||||
socketdriver.start(socket)
|
socketdriver.start(socket)
|
||||||
if handler.open then
|
if handler.open then
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
#include <lualib.h>
|
#include <lualib.h>
|
||||||
#include <lauxlib.h>
|
#include <lauxlib.h>
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|||||||
@@ -31,5 +31,17 @@ skynet_harbor_init(int harbor) {
|
|||||||
|
|
||||||
void
|
void
|
||||||
skynet_harbor_start(void *ctx) {
|
skynet_harbor_start(void *ctx) {
|
||||||
|
// the HARBOR must be reserved to ensure the pointer is valid.
|
||||||
|
// It will be released at last by calling skynet_harbor_exit
|
||||||
|
skynet_context_reserve(ctx);
|
||||||
REMOTE = ctx;
|
REMOTE = ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
skynet_harbor_exit() {
|
||||||
|
struct skynet_context * ctx = REMOTE;
|
||||||
|
REMOTE= NULL;
|
||||||
|
if (ctx) {
|
||||||
|
skynet_context_release(ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -26,5 +26,6 @@ void skynet_harbor_send(struct remote_message *rmsg, uint32_t source, int sessio
|
|||||||
int skynet_harbor_message_isremote(uint32_t handle);
|
int skynet_harbor_message_isremote(uint32_t handle);
|
||||||
void skynet_harbor_init(int harbor);
|
void skynet_harbor_init(int harbor);
|
||||||
void skynet_harbor_start(void * ctx);
|
void skynet_harbor_start(void * ctx);
|
||||||
|
void skynet_harbor_exit();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
// 1 means mq is in global mq , or the message is dispatching.
|
// 1 means mq is in global mq , or the message is dispatching.
|
||||||
|
|
||||||
#define MQ_IN_GLOBAL 1
|
#define MQ_IN_GLOBAL 1
|
||||||
|
#define MQ_OVERLOAD 1024
|
||||||
|
|
||||||
struct message_queue {
|
struct message_queue {
|
||||||
uint32_t handle;
|
uint32_t handle;
|
||||||
@@ -24,6 +25,8 @@ struct message_queue {
|
|||||||
int lock;
|
int lock;
|
||||||
int release;
|
int release;
|
||||||
int in_global;
|
int in_global;
|
||||||
|
int overload;
|
||||||
|
int overload_threshold;
|
||||||
struct skynet_message *queue;
|
struct skynet_message *queue;
|
||||||
struct message_queue *next;
|
struct message_queue *next;
|
||||||
};
|
};
|
||||||
@@ -116,6 +119,8 @@ skynet_mq_create(uint32_t handle) {
|
|||||||
// If the service init success, skynet_context_new will call skynet_mq_force_push to push it to global queue.
|
// If the service init success, skynet_context_new will call skynet_mq_force_push to push it to global queue.
|
||||||
q->in_global = MQ_IN_GLOBAL;
|
q->in_global = MQ_IN_GLOBAL;
|
||||||
q->release = 0;
|
q->release = 0;
|
||||||
|
q->overload = 0;
|
||||||
|
q->overload_threshold = MQ_OVERLOAD;
|
||||||
q->queue = skynet_malloc(sizeof(struct skynet_message) * q->cap);
|
q->queue = skynet_malloc(sizeof(struct skynet_message) * q->cap);
|
||||||
q->next = NULL;
|
q->next = NULL;
|
||||||
|
|
||||||
@@ -150,17 +155,42 @@ skynet_mq_length(struct message_queue *q) {
|
|||||||
return tail + cap - head;
|
return tail + cap - head;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
skynet_mq_overload(struct message_queue *q) {
|
||||||
|
if (q->overload) {
|
||||||
|
int overload = q->overload;
|
||||||
|
q->overload = 0;
|
||||||
|
return overload;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
skynet_mq_pop(struct message_queue *q, struct skynet_message *message) {
|
skynet_mq_pop(struct message_queue *q, struct skynet_message *message) {
|
||||||
int ret = 1;
|
int ret = 1;
|
||||||
LOCK(q)
|
LOCK(q)
|
||||||
|
|
||||||
if (q->head != q->tail) {
|
if (q->head != q->tail) {
|
||||||
*message = q->queue[q->head];
|
*message = q->queue[q->head++];
|
||||||
ret = 0;
|
ret = 0;
|
||||||
if ( ++ q->head >= q->cap) {
|
int head = q->head;
|
||||||
q->head = 0;
|
int tail = q->tail;
|
||||||
|
int cap = q->cap;
|
||||||
|
|
||||||
|
if (head >= cap) {
|
||||||
|
q->head = head = 0;
|
||||||
}
|
}
|
||||||
|
int length = tail - head;
|
||||||
|
if (length < 0) {
|
||||||
|
length += cap;
|
||||||
|
}
|
||||||
|
while (length > q->overload_threshold) {
|
||||||
|
q->overload = length;
|
||||||
|
q->overload_threshold *= 2;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// reset overload_threshold when queue is empty
|
||||||
|
q->overload_threshold = MQ_OVERLOAD;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret) {
|
if (ret) {
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ void skynet_mq_push(struct message_queue *q, struct skynet_message *message);
|
|||||||
|
|
||||||
// return the length of message queue, for debug
|
// return the length of message queue, for debug
|
||||||
int skynet_mq_length(struct message_queue *q);
|
int skynet_mq_length(struct message_queue *q);
|
||||||
|
int skynet_mq_overload(struct message_queue *q);
|
||||||
|
|
||||||
void skynet_mq_init();
|
void skynet_mq_init();
|
||||||
|
|
||||||
|
|||||||
@@ -178,6 +178,14 @@ skynet_context_grab(struct skynet_context *ctx) {
|
|||||||
__sync_add_and_fetch(&ctx->ref,1);
|
__sync_add_and_fetch(&ctx->ref,1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
skynet_context_reserve(struct skynet_context *ctx) {
|
||||||
|
skynet_context_grab(ctx);
|
||||||
|
// don't count the context reserved, because skynet abort (the worker threads terminate) only when the total context is 0 .
|
||||||
|
// the reserved context will be release at last.
|
||||||
|
context_dec();
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
delete_context(struct skynet_context *ctx) {
|
delete_context(struct skynet_context *ctx) {
|
||||||
if (ctx->logfile) {
|
if (ctx->logfile) {
|
||||||
@@ -283,6 +291,10 @@ skynet_context_message_dispatch(struct skynet_monitor *sm, struct message_queue
|
|||||||
n = skynet_mq_length(q);
|
n = skynet_mq_length(q);
|
||||||
n >>= weight;
|
n >>= weight;
|
||||||
}
|
}
|
||||||
|
int overload = skynet_mq_overload(q);
|
||||||
|
if (overload) {
|
||||||
|
skynet_error(ctx, "May overload, message queue length = %d", overload);
|
||||||
|
}
|
||||||
|
|
||||||
skynet_monitor_trigger(sm, msg.source , handle);
|
skynet_monitor_trigger(sm, msg.source , handle);
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ struct skynet_monitor;
|
|||||||
|
|
||||||
struct skynet_context * skynet_context_new(const char * name, const char * parm);
|
struct skynet_context * skynet_context_new(const char * name, const char * parm);
|
||||||
void skynet_context_grab(struct skynet_context *);
|
void skynet_context_grab(struct skynet_context *);
|
||||||
|
void skynet_context_reserve(struct skynet_context *ctx);
|
||||||
struct skynet_context * skynet_context_release(struct skynet_context *);
|
struct skynet_context * skynet_context_release(struct skynet_context *);
|
||||||
uint32_t skynet_context_handle(struct skynet_context *);
|
uint32_t skynet_context_handle(struct skynet_context *);
|
||||||
int skynet_context_push(uint32_t handle, struct skynet_message *message);
|
int skynet_context_push(uint32_t handle, struct skynet_message *message);
|
||||||
|
|||||||
@@ -127,7 +127,6 @@ _worker(void *p) {
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
q = skynet_context_message_dispatch(sm, q, weight);
|
q = skynet_context_message_dispatch(sm, q, weight);
|
||||||
if (q == NULL) {
|
if (q == NULL) {
|
||||||
CHECK_ABORT
|
|
||||||
if (pthread_mutex_lock(&m->mutex) == 0) {
|
if (pthread_mutex_lock(&m->mutex) == 0) {
|
||||||
++ m->sleep;
|
++ m->sleep;
|
||||||
// "spurious wakeup" is harmless,
|
// "spurious wakeup" is harmless,
|
||||||
@@ -140,6 +139,7 @@ _worker(void *p) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
CHECK_ABORT
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -232,6 +232,9 @@ skynet_start(struct skynet_config * config) {
|
|||||||
bootstrap(ctx, config->bootstrap);
|
bootstrap(ctx, config->bootstrap);
|
||||||
|
|
||||||
_start(config->thread);
|
_start(config->thread);
|
||||||
|
|
||||||
|
// harbor_exit may call socket send, so it should exit before socket_free
|
||||||
|
skynet_harbor_exit();
|
||||||
skynet_socket_free();
|
skynet_socket_free();
|
||||||
if (config->daemon) {
|
if (config->daemon) {
|
||||||
daemon_exit(config->daemon);
|
daemon_exit(config->daemon);
|
||||||
|
|||||||
44
test/testoverload.lua
Normal file
44
test/testoverload.lua
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
|
||||||
|
local mode = ...
|
||||||
|
|
||||||
|
if mode == "slave" then
|
||||||
|
|
||||||
|
local CMD = {}
|
||||||
|
|
||||||
|
function CMD.sum(n)
|
||||||
|
skynet.error("for loop begin")
|
||||||
|
local s = 0
|
||||||
|
for i = 1, n do
|
||||||
|
s = s + i
|
||||||
|
end
|
||||||
|
skynet.error("for loop end")
|
||||||
|
end
|
||||||
|
|
||||||
|
function CMD.blackhole()
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.start(function()
|
||||||
|
skynet.dispatch("lua", function(_,_, cmd, ...)
|
||||||
|
local f = CMD[cmd]
|
||||||
|
f(...)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
else
|
||||||
|
|
||||||
|
skynet.start(function()
|
||||||
|
local slave = skynet.newservice(SERVICE_NAME, "slave")
|
||||||
|
for step = 1, 20 do
|
||||||
|
skynet.error("overload test ".. step)
|
||||||
|
for i = 1, 512 * step do
|
||||||
|
skynet.send(slave, "lua", "blackhole")
|
||||||
|
end
|
||||||
|
skynet.sleep(step)
|
||||||
|
end
|
||||||
|
local n = 1000000000
|
||||||
|
skynet.error(string.format("endless test n=%d", n))
|
||||||
|
skynet.send(slave, "lua", "sum", n)
|
||||||
|
end)
|
||||||
|
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user