Skip to content

Commit

Permalink
more debug info for console.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
findstr committed Sep 28, 2017
1 parent 9f333e5 commit 4b3e64e
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 74 deletions.
138 changes: 98 additions & 40 deletions lualib-src/lualib-silly.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <unistd.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
Expand Down Expand Up @@ -120,16 +122,6 @@ lexit(lua_State *L)
return 0;
}

static int
lmsgsize(lua_State *L)
{
size_t sz;
sz = silly_worker_msgsize();
lua_pushinteger(L, sz);
return 1;
}


static int
llog(lua_State *L)
{
Expand Down Expand Up @@ -165,33 +157,32 @@ llog(lua_State *L)
}

static int
lmemallocator(lua_State *L)
lgenid(lua_State *L)
{
const char *ver;
ver = silly_allocator();
lua_pushstring(L, ver);
uint32_t id = silly_worker_genid();
lua_pushinteger(L, id);
return 1;
}

static int
lmemused(lua_State *L)
ltostring(lua_State *L)
{
size_t sz;
sz = silly_memused();
lua_pushinteger(L, sz);
char *buff;
int size;
buff = lua_touserdata(L, 1);
size = luaL_checkinteger(L, 2);
lua_pushlstring(L, buff, size);
return 1;
}

static int
lmemrss(lua_State *L)
lgetpid(lua_State *L)
{
size_t sz;
sz = silly_memrss();
lua_pushinteger(L, sz);
int pid = getpid();
lua_pushinteger(L, pid);
return 1;
}


static int
ltimeout(lua_State *L)
{
Expand Down Expand Up @@ -219,6 +210,14 @@ ltimemonotonic(lua_State *L)
return 1;
}

static int
ltimemonotonicsec(lua_State *L)
{
uint64_t monotonic = silly_timer_monotonicsec();
lua_pushinteger(L, monotonic);
return 1;
}

static int
ldispatch(lua_State *L)
{
Expand Down Expand Up @@ -463,24 +462,78 @@ lclose(lua_State *L)
}

static int
ltostring(lua_State *L)
lversion(lua_State *L)
{
char *buff;
int size;
buff = lua_touserdata(L, 1);
size = luaL_checkinteger(L, 2);
lua_pushlstring(L, buff, size);
const char *ver = SILLY_VERSION;
lua_pushstring(L, ver);
return 1;
}

static int
lgenid(lua_State *L)
lmemallocator(lua_State *L)
{
uint32_t id = silly_worker_genid();
lua_pushinteger(L, id);
const char *ver;
ver = silly_allocator();
lua_pushstring(L, ver);
return 1;
}

static int
lmemused(lua_State *L)
{
size_t sz;
sz = silly_memused();
lua_pushinteger(L, sz);
return 1;
}

static int
lmemrss(lua_State *L)
{
size_t sz;
sz = silly_memrss();
lua_pushinteger(L, sz);
return 1;
}

static int
lmsgsize(lua_State *L)
{
size_t sz;
sz = silly_worker_msgsize();
lua_pushinteger(L, sz);
return 1;
}

static int
lcpuinfo(lua_State *L)
{
struct rusage ru;
float stime,utime;
getrusage(RUSAGE_SELF, &ru);
stime = (float)ru.ru_stime.tv_sec;
stime += (float)ru.ru_stime.tv_usec / 1000000;
utime = (float)ru.ru_utime.tv_sec;
utime += (float)ru.ru_utime.tv_usec / 1000000;
lua_pushnumber(L, stime);
lua_pushnumber(L, utime);
return 2;
}

static int
lpollapi(lua_State *L)
{
const char *api = silly_socket_pollapi();
lua_pushstring(L, api);
return 1;
}

static int
ltimerresolution(lua_State *L)
{
lua_pushinteger(L, TIMER_RESOLUTION);
return 1;
}

int
luaopen_silly(lua_State *L)
Expand All @@ -492,15 +545,14 @@ luaopen_silly(lua_State *L)
{"setenv", lsetenv},
{"exit", lexit},
{"log", llog},
{"msgsize", lmsgsize},
//memory
{"memused", lmemused},
{"memrss", lmemrss},
{"memallocator", lmemallocator},
{"genid", lgenid},
{"tostring", ltostring},
{"getpid", lgetpid},
//timer
{"timeout", ltimeout},
{"timenow", ltimenow},
{"timemonotonic", ltimemonotonic},
{"timemonotonicsec", ltimemonotonicsec},
//socket
{"connect", ltcpconnect},
{"listen", ltcplisten},
Expand All @@ -512,9 +564,15 @@ luaopen_silly(lua_State *L)
{"udp", ludpconnect},
{"udpsend", ludpsend},
{"close", lclose},
//
{"tostring", ltostring},
{"genid", lgenid},
//probe
{"version", lversion},
{"memused", lmemused},
{"memrss", lmemrss},
{"memallocator", lmemallocator},
{"msgsize", lmsgsize},
{"cpuinfo", lcpuinfo},
{"pollapi", lpollapi},
{"timerresolution", ltimerresolution},
//end
{NULL, NULL},
};
Expand Down
57 changes: 42 additions & 15 deletions lualib/console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ local socket = require "socket"
local lower = string.lower
local format = string.format
local concat = table.concat
local insert = table.insert

local NULL = ""
local desc = {

"HELP: List command description [HELP]",
"PING: Test connection alive [PING <text>]",
"INFO: Show all information of server, include MINFO,QINFO,CPUINFO,...[INFO]",
"MINFO: Show memory infomation [MINFO <kb|mb>]",
"QINFO: Show framework message queue size[QINFO]",
"CPUINFO: Show system time and user time statistics [CPUINFO]",
"PATCH: Hot patch the code [PATCH <fixfile> <modulename> <funcname> ...]"

}
Expand Down Expand Up @@ -74,31 +77,55 @@ function console.minfo(fmt)
else
fmt = NULL
end
tbl[1] = "Memory used:"
tbl[1] = "#Memory\r\n"
tbl[2] = "memory_used:"
if fmt == "kb" then
tbl[2] = sz // 1024
tbl[3] = " KByte\r\n"
tbl[3] = format("%.2f", sz / 1024)
tbl[4] = " KByte\r\n"
elseif fmt == "mb" then
tbl[2] = sz // (1024 * 1024)
tbl[3] = " MByte\r\n"
tbl[3] = format("%.2f", sz / (1024 * 1024))
tbl[4] = " MByte\r\n"
else
tbl[2] = sz
tbl[3] = " Byte\r\n"
tbl[3] = sz
tbl[4] = " Byte\r\n"
end
local rss = core.memrss()
tbl[4] = "Memory rss:"
tbl[5] = rss
tbl[6] = " Byte\r\n"
tbl[7] = "Memory fragmentation_ratio:"
tbl[8] = format("%.2f\r\n", rss / sz)
tbl[9] = "Memory allocator:"
tbl[10] = core.allocator()
tbl[5] = "memory_rss:"
tbl[6] = rss
tbl[7] = " Byte\r\n"
tbl[8] = "memory_fragmentation_ratio:"
tbl[9] = format("%.2f\r\n", rss / sz)
tbl[10] = "memory_allocator:"
tbl[11] = core.allocator
return concat(tbl)
end

function console.qinfo()
local sz = core.msgsize();
return string.format("Message pending:%d", sz)
return format("#Message\r\nmessage pending:%d\n", sz)
end

function console.cpuinfo()
local sys, usr = core.cpuinfo()
return format("#CPU\r\ncpu_sys:%.2fs\r\ncpu_user:%.2fs\r\n", sys, usr)
end

function console.info()
local tbl = {}
local uptime = core.monotonicsec()
insert(tbl, "\r\n#Build")
insert(tbl, format("version:%s", core.version))
insert(tbl, format("process_id:%s", core.getpid()))
insert(tbl, format("multiplexing_api:%s", core.pollapi))
insert(tbl, format("timer_resolution:%s", core.timerrs))
insert(tbl, format("uptime_in_seconds:%s", uptime))
insert(tbl, format("uptime_in_days:%.2f\r\n", uptime / (24 * 3600)))
insert(tbl, console.cpuinfo())
insert(tbl, console.qinfo())
insert(tbl, console.minfo("MB"))


return concat(tbl, "\r\n")
end

function console.patch(fix, module, ...)
Expand Down
12 changes: 10 additions & 2 deletions lualib/silly/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,21 @@ core.log = core_log
core.exit = silly.exit
core.tostring = silly.tostring
core.genid = silly.genid
core.msgsize = silly.msgsize
core.now = silly.timenow
core.monotonic = silly.timemonotonic
core.monotonicsec = silly.timemonotonicsec

--debug interface
core.memused = silly.memused
core.memrss = silly.memrss
core.allocator = silly.memallocator
core.msgsize = silly.msgsize
core.cpuinfo = silly.cpuinfo
core.getpid = silly.getpid
--const
core.allocator = silly.memallocator()
core.version = silly.version()
core.pollapi = silly.pollapi()
core.timerrs = silly.timerresolution()

local function errmsg(msg)
return debug.traceback("error: " .. tostring(msg), 2)
Expand Down
19 changes: 12 additions & 7 deletions silly-src/silly.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@
#define _SILLY_H
#include <assert.h>
#include <stdint.h>
#include "silly_conf.h"
#include "silly_malloc.h"
#include "silly_socket.h"

#define MSGCOMMONFIELD \
struct silly_message *next;\
enum silly_message_type type;
#define SILLY_VERSION_MAJOR 0
#define SILLY_VERSION_MINOR 2
#define SILLY_VERSION_RELEASE 2
#define SILLY_VERSION_NUM ((SILLY_VERSION_MAJOR * 100) + SILLY_VERSION_MINOR)
#define SILLY_VERSION STR(SILLY_VERSION_MAJOR) "." STR(SILLY_VERSION_MINOR)
#define SILLY_RELEASE SILLY_VERSION "." STR(SILLY_VERSION_RELEASE)


#define tocommon(msg) ((struct silly_message *)(msg))
#define totexpire(msg) ((struct silly_message_texpire *)(msg))
#define tosocket(msg) ((struct silly_message_socket *)(msg))
#define COMMONFIELD struct silly_message *next; enum silly_message_type type;

struct silly_listen {
char name[64];
Expand Down Expand Up @@ -42,18 +48,17 @@ enum silly_message_type {
SILLY_SUDP, //data packet(raw) from client(udp)
};


struct silly_message {
MSGCOMMONFIELD
COMMONFIELD
};

struct silly_message_texpire { //timer expire
MSGCOMMONFIELD
COMMONFIELD
uint32_t session;
};

struct silly_message_socket { //socket accept
MSGCOMMONFIELD
COMMONFIELD
int sid;
//SACCEPT, it used as portid,
//SCLOSE used as errorcode
Expand Down
5 changes: 5 additions & 0 deletions silly-src/silly_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@

#define TIMER_RESOLUTION (10)



#define STR(s) __STR(s)
#define __STR(s) #s

#endif
Loading

0 comments on commit 4b3e64e

Please sign in to comment.