Skip to content

Commit

Permalink
add metrics of prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
findstr authored and findstr committed Jul 14, 2023
1 parent dd8d958 commit 8905b00
Show file tree
Hide file tree
Showing 20 changed files with 748 additions and 208 deletions.
8 changes: 8 additions & 0 deletions lualib-src/lualib-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ lgetpid(lua_State *L)
return 1;
}

static int
lgitsha1(lua_State *L)
{
lua_pushstring(L, STR(SILLY_GIT_SHA1));
return 1;
}

static int
lversion(lua_State *L)
{
Expand Down Expand Up @@ -479,6 +486,7 @@ luaopen_sys_core_c(lua_State *L)
{
luaL_Reg tbl[] = {
//core
{"gitsha1", lgitsha1},
{"version", lversion},
{"dispatch", ldispatch},
{"timeout", ltimeout},
Expand Down
207 changes: 149 additions & 58 deletions lualib-src/lualib-metrics.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <dirent.h>
#include <string.h>
#include <sys/time.h>
#include <sys/resource.h>
Expand All @@ -19,6 +22,10 @@
#include "silly_malloc.h"
#include "silly_timer.h"

#ifndef DISABLE_JEMALLOC
#include <jemalloc/jemalloc.h>
#endif

static int
lmemallocator(lua_State *L)
{
Expand All @@ -29,55 +36,135 @@ lmemallocator(lua_State *L)
}

static int
lmemallocatorinfo(lua_State *L)
lcpustat(lua_State *L)
{
size_t allocated, active, resident;
silly_allocator_info(&allocated, &active, &resident);
lua_pushinteger(L, allocated);
lua_pushinteger(L, active);
lua_pushinteger(L, resident);
return 3;
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
lmemused(lua_State *L)
lmaxfds(lua_State *L)
{
size_t sz;
sz = silly_memused();
lua_pushinteger(L, sz);
return 1;
struct rlimit rlim;
int ret = getrlimit(RLIMIT_NOFILE, &rlim);
if (ret != 0) {
silly_log_error("[metrics] getrlimit errno:%d", errno);
rlim.rlim_cur = 0;
rlim.rlim_max = 0;
}
lua_pushinteger(L, rlim.rlim_cur); //soft
lua_pushinteger(L, rlim.rlim_max); //hard
return 2;
}

static int
lmemrss(lua_State *L)
lopenfds(lua_State *L)
{
size_t sz;
sz = silly_memrss();
lua_pushinteger(L, sz);
int fd_count = 0;
struct dirent *entry;
DIR *fd_dir = opendir("/proc/self/fd");
if (fd_dir == NULL) {
silly_log_error("[metrics] failed to open /proc/self/fd");
lua_pushinteger(L, 0);
return 1;
}
while ((entry = readdir(fd_dir)) != NULL) {
if (entry->d_name[0] != '.') {
fd_count++;
}
}
closedir(fd_dir);
lua_pushinteger(L, fd_count);
return 1;
}

static void
parse_memstat(char *p, char *e, int page, size_t vmstat[2])
{
//VmSize is the 23th field in /proc/self/stat
//RSS is the 24th field in /proc/self/stat
int i = 0;
char *end;
while (p < e) {
if (*p++ != ' ')
continue;
switch (++i) {
case 22: //vmsize
end = strchr(p, ' ');
vmstat[0] = strtoll(p, &end, 10);
break;
case 23: //rss
end = strchr(p, ' ');
vmstat[1] = strtoll(p, &end, 10) * page;
goto finish;
break;
}
}
finish:
return ;
}

static int
lmsgsize(lua_State *L)
lmemstat(lua_State *L)
{
size_t sz;
sz = silly_worker_msgsize();
lua_pushinteger(L, sz);
return 1;
int fd, count;
char buf[4096];
int page = sysconf(_SC_PAGESIZE);
size_t vmstat[2] = {-1, -1}; //vmsize, vmrss;
fd = open("/proc/self/stat", O_RDONLY);
if (fd >= 0) {
count = read(fd, buf, sizeof(buf) - 1);
close(fd);
} else {
count = 0;
}
if (count > 0) {
parse_memstat(buf, &buf[count], page, vmstat);
}
lua_pushinteger(L, vmstat[0]);
lua_pushinteger(L, vmstat[1]);
lua_pushinteger(L, silly_memused());
return 3;
}

static int
lcpuinfo(lua_State *L)
ljestat(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;
uint64_t epoch = 1;
size_t allocated, active, resident, retained;
#ifndef DISABLE_JEMALLOC
size_t sz = sizeof(epoch);
je_mallctl("epoch", &epoch, &sz, &epoch, sz);
sz = sizeof(size_t);
je_mallctl("stats.resident", &resident, &sz, NULL, 0);
je_mallctl("stats.active", &active, &sz, NULL, 0);
je_mallctl("stats.allocated", &allocated, &sz, NULL, 0);
je_mallctl("stats.retained", &retained, &sz, NULL, 0);
#else
allocated = resident = active = retained = 0;
#endif
lua_pushinteger(L, allocated);
lua_pushinteger(L, active);
lua_pushinteger(L, resident);
lua_pushinteger(L, retained);
return 4;
}

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

static int
Expand All @@ -103,23 +190,20 @@ table_set_str(lua_State *L, int table, const char *k, const char *v)
}

static int
lnetinfo(lua_State *L)
lnetstat(lua_State *L)
{
struct silly_netinfo info;
silly_socket_netinfo(&info);
lua_newtable(L);
table_set_int(L, -1, "tcplisten", info.tcplisten);
table_set_int(L, -1, "udpbind", info.udpbind);
table_set_int(L, -1, "connecting", info.connecting);
table_set_int(L, -1, "udpclient", info.udpclient);
table_set_int(L, -1, "tcpclient", info.tcpclient);
table_set_int(L, -1, "tcphalfclose", info.tcphalfclose);
table_set_int(L, -1, "sendsize", info.sendsize);
return 1;
struct silly_netstat *stat;
stat = silly_socket_netstat();
lua_pushinteger(L, stat->connecting);
lua_pushinteger(L, stat->tcpclient);
lua_pushinteger(L, silly_socket_ctrlcount());
lua_pushinteger(L, stat->sendsize);
lua_pushinteger(L, stat->recvsize);
return 5;
}

static int
ltimerinfo(lua_State *L)
ltimerstat(lua_State *L)
{
uint32_t active, expired;
active = silly_timer_info(&expired);
Expand All @@ -129,12 +213,12 @@ ltimerinfo(lua_State *L)
}

static int
lsocketinfo(lua_State *L)
lsocketstat (lua_State *L)
{
int sid;
struct silly_socketinfo info;
struct silly_socketstat info;
sid = luaL_checkinteger(L, 1);
silly_socket_socketinfo(sid, &info);
silly_socket_socketstat(sid, &info);
lua_newtable(L);
table_set_int(L, -1, "fd", info.sid);
table_set_int(L, -1, "os_fd", info.fd);
Expand All @@ -154,20 +238,27 @@ ltimerresolution(lua_State *L)
}

int
luaopen_sys_metrics(lua_State *L)
luaopen_sys_metrics_c(lua_State *L)
{
luaL_Reg tbl[] = {
{"memused", lmemused},
{"memrss", lmemrss},
{"memallocator", lmemallocator},
{"memallocatorinfo", lmemallocatorinfo},
{"msgsize", lmsgsize},
{"cpuinfo", lcpuinfo},
//build
{"pollapi", lpollapi},
{"netinfo", lnetinfo},
{"timerinfo", ltimerinfo},
{"socketinfo", lsocketinfo},
{"memallocator", lmemallocator},
{"timerresolution", ltimerresolution},
//process
{"cpustat", lcpustat},
{"maxfds", lmaxfds},
{"openfds", lopenfds},
//memory
{"memstat", lmemstat},
#ifndef DISABLE_JEMALLOC
{"jestat", ljestat},
#endif
//core
{"workerstat", lworkerstat},
{"timerstat", ltimerstat},
{"netstat", lnetstat},
{"socketstat", lsocketstat},
//end
{NULL, NULL},
};
Expand Down
2 changes: 1 addition & 1 deletion lualib/http/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ local server = {
listen = function (conf)
local fd1, fd2
local handler = conf.handler
local port = conf.port
local port = conf.port or conf.addr
if port then
fd1 = tcp.listen(port, httpd("http", handler))
end
Expand Down
Loading

0 comments on commit 8905b00

Please sign in to comment.