Skip to content

Commit 5a61e1a

Browse files
Buristankyukhin
authored andcommitted
misc: add C and Lua API for platform metrics
This patch introduces both C and Lua API for LuaJIT platform metrics implemented in scope of the previous patch. New <lmisclib.h> header provides <luaM_metrics> C interface that fills the given <luam_metrics> structure with the platform metrics. Additionally <misc> module is loaded to Lua space and provides <getmetrics> method that yields the corresponding metrics via table. Part of tarantool/tarantool#5187
1 parent 5f6775a commit 5a61e1a

13 files changed

+1047
-8
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ FILE_A= libluajit.a
8484
FILE_SO= libluajit.so
8585
FILE_MAN= luajit.1
8686
FILE_PC= luajit.pc
87-
FILES_INC= lua.h lualib.h lauxlib.h luaconf.h lua.hpp luajit.h
87+
FILES_INC= lua.h lualib.h lauxlib.h luaconf.h lua.hpp luajit.h lmisclib.h
8888
FILES_JITLIB= bc.lua bcsave.lua dump.lua p.lua v.lua zone.lua \
8989
dis_x86.lua dis_x64.lua dis_arm.lua dis_arm64.lua \
9090
dis_arm64be.lua dis_ppc.lua dis_mips.lua dis_mipsel.lua \

src/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,13 +480,14 @@ LJVM_BOUT= $(LJVM_S)
480480
LJVM_MODE= elfasm
481481

482482
LJLIB_O= lib_base.o lib_math.o lib_bit.o lib_string.o lib_table.o \
483-
lib_io.o lib_os.o lib_package.o lib_debug.o lib_jit.o lib_ffi.o
483+
lib_io.o lib_os.o lib_package.o lib_debug.o lib_jit.o lib_ffi.o \
484+
lib_misc.o
484485
LJLIB_C= $(LJLIB_O:.o=.c)
485486

486487
LJCORE_O= lj_gc.o lj_err.o lj_char.o lj_bc.o lj_obj.o lj_buf.o \
487488
lj_str.o lj_tab.o lj_func.o lj_udata.o lj_meta.o lj_debug.o \
488489
lj_state.o lj_dispatch.o lj_vmevent.o lj_vmmath.o lj_strscan.o \
489-
lj_strfmt.o lj_strfmt_num.o lj_api.o lj_profile.o \
490+
lj_strfmt.o lj_strfmt_num.o lj_api.o lj_mapi.o lj_profile.o \
490491
lj_lex.o lj_parse.o lj_bcread.o lj_bcwrite.o lj_load.o \
491492
lj_ir.o lj_opt_mem.o lj_opt_fold.o lj_opt_narrow.o \
492493
lj_opt_dce.o lj_opt_loop.o lj_opt_split.o lj_opt_sink.o \

src/Makefile.dep

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ lib_ffi.o: lib_ffi.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \
1818
lj_ctype.h lj_cparse.h lj_cdata.h lj_cconv.h lj_carith.h lj_ccall.h \
1919
lj_ccallback.h lj_clib.h lj_strfmt.h lj_ff.h lj_ffdef.h lj_lib.h \
2020
lj_libdef.h
21-
lib_init.o: lib_init.c lua.h luaconf.h lauxlib.h lualib.h lj_arch.h
21+
lib_init.o: lib_init.c lua.h luaconf.h lauxlib.h lualib.h lmisclib.h lj_arch.h
2222
lib_io.o: lib_io.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \
2323
lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_state.h \
2424
lj_strfmt.h lj_ff.h lj_ffdef.h lj_lib.h lj_libdef.h
@@ -29,6 +29,8 @@ lib_jit.o: lib_jit.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \
2929
lj_vm.h lj_vmevent.h lj_lib.h luajit.h lj_libdef.h
3030
lib_math.o: lib_math.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
3131
lj_def.h lj_arch.h lj_lib.h lj_vm.h lj_libdef.h
32+
lib_misc.o: lib_misc.c lua.h luaconf.h lmisclib.h lj_obj.h lj_def.h lj_arch.h \
33+
lj_str.h lj_tab.h lj_lib.h lj_libdef.h
3234
lib_os.o: lib_os.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \
3335
lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_lib.h \
3436
lj_libdef.h
@@ -137,6 +139,8 @@ lj_lib.o: lj_lib.c lauxlib.h lua.h luaconf.h lj_obj.h lj_def.h lj_arch.h \
137139
lj_load.o: lj_load.c lua.h luaconf.h lauxlib.h lj_obj.h lj_def.h \
138140
lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_func.h \
139141
lj_frame.h lj_bc.h lj_vm.h lj_lex.h lj_bcdump.h lj_parse.h
142+
lj_mapi.o: lj_mapi.c lua.h luaconf.h lmisclib.h lj_obj.h lj_def.h lj_arch.h \
143+
lj_dispatch.h lj_bc.h lj_jit.h lj_ir.h
140144
lj_mcode.o: lj_mcode.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
141145
lj_gc.h lj_err.h lj_errmsg.h lj_jit.h lj_ir.h lj_mcode.h lj_trace.h \
142146
lj_dispatch.h lj_bc.h lj_traceerr.h lj_vm.h
@@ -215,9 +219,9 @@ ljamalg.o: ljamalg.c lua.h luaconf.h lauxlib.h lj_gc.c lj_obj.h lj_def.h \
215219
lj_func.c lj_udata.c lj_meta.c lj_strscan.h lj_lib.h lj_debug.c \
216220
lj_state.c lj_lex.h lj_alloc.h luajit.h lj_dispatch.c lj_ccallback.h \
217221
lj_profile.h lj_vmevent.c lj_vmevent.h lj_vmmath.c lj_strscan.c \
218-
lj_strfmt.c lj_strfmt_num.c lj_api.c lj_profile.c lj_lex.c lualib.h \
219-
lj_parse.h lj_parse.c lj_bcread.c lj_bcdump.h lj_bcwrite.c lj_load.c \
220-
lj_ctype.c lj_cdata.c lj_cconv.h lj_cconv.c lj_ccall.c lj_ccall.h \
222+
lj_strfmt.c lj_strfmt_num.c lj_api.c lj_mapi.c lmisclib.h lj_profile.c \
223+
lj_lex.c lualib.h lj_parse.h lj_parse.c lj_bcread.c lj_bcdump.h lj_bcwrite.c \
224+
lj_load.c lj_ctype.c lj_cdata.c lj_cconv.h lj_cconv.c lj_ccall.c lj_ccall.h \
221225
lj_ccallback.c lj_target.h lj_target_*.h lj_mcode.h lj_carith.c \
222226
lj_carith.h lj_clib.c lj_clib.h lj_cparse.c lj_cparse.h lj_lib.c lj_ir.c \
223227
lj_ircall.h lj_iropt.h lj_opt_mem.c lj_opt_fold.c lj_folddef.h \
@@ -227,7 +231,7 @@ ljamalg.o: ljamalg.c lua.h luaconf.h lauxlib.h lj_gc.c lj_obj.h lj_def.h \
227231
lj_emit_*.h lj_asm_*.h lj_trace.c lj_gdbjit.h lj_gdbjit.c lj_alloc.c \
228232
lib_aux.c lib_base.c lj_libdef.h lib_math.c lib_string.c lib_table.c \
229233
lib_io.c lib_os.c lib_package.c lib_debug.c lib_bit.c lib_jit.c \
230-
lib_ffi.c lib_init.c
234+
lib_ffi.c lib_misc.c lib_init.c
231235
luajit.o: luajit.c lua.h luaconf.h lauxlib.h lualib.h luajit.h lj_arch.h
232236
host/buildvm.o: host/buildvm.c host/buildvm.h lj_def.h lua.h luaconf.h \
233237
lj_arch.h lj_obj.h lj_def.h lj_arch.h lj_gc.h lj_obj.h lj_bc.h lj_ir.h \

src/lib_init.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "lua.h"
1313
#include "lauxlib.h"
1414
#include "lualib.h"
15+
#include "lmisclib.h"
1516

1617
#include "lj_arch.h"
1718

@@ -26,6 +27,7 @@ static const luaL_Reg lj_lib_load[] = {
2627
{ LUA_DBLIBNAME, luaopen_debug },
2728
{ LUA_BITLIBNAME, luaopen_bit },
2829
{ LUA_JITLIBNAME, luaopen_jit },
30+
{ LUAM_MISCLIBNAME, luaopen_misc },
2931
{ NULL, NULL }
3032
};
3133

src/lib_misc.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
** Miscellaneous Lua extensions library.
3+
**
4+
** Major portions taken verbatim or adapted from the LuaVela interpreter.
5+
** Copyright (C) 2015-2019 IPONWEB Ltd.
6+
*/
7+
8+
#define lib_misc_c
9+
#define LUA_LIB
10+
11+
#include "lua.h"
12+
#include "lmisclib.h"
13+
14+
#include "lj_obj.h"
15+
#include "lj_str.h"
16+
#include "lj_tab.h"
17+
#include "lj_lib.h"
18+
19+
/* ------------------------------------------------------------------------ */
20+
21+
static LJ_AINLINE void setnumfield(struct lua_State *L, GCtab *t,
22+
const char *name, int64_t val)
23+
{
24+
setnumV(lj_tab_setstr(L, t, lj_str_newz(L, name)), (double)val);
25+
}
26+
27+
#define LJLIB_MODULE_misc
28+
29+
LJLIB_CF(misc_getmetrics)
30+
{
31+
struct luam_Metrics metrics;
32+
GCtab *m;
33+
34+
lua_createtable(L, 0, 19);
35+
m = tabV(L->top - 1);
36+
37+
luaM_metrics(L, &metrics);
38+
39+
setnumfield(L, m, "strhash_hit", metrics.strhash_hit);
40+
setnumfield(L, m, "strhash_miss", metrics.strhash_miss);
41+
42+
setnumfield(L, m, "gc_strnum", metrics.gc_strnum);
43+
setnumfield(L, m, "gc_tabnum", metrics.gc_tabnum);
44+
setnumfield(L, m, "gc_udatanum", metrics.gc_udatanum);
45+
setnumfield(L, m, "gc_cdatanum", metrics.gc_cdatanum);
46+
47+
setnumfield(L, m, "gc_total", metrics.gc_total);
48+
setnumfield(L, m, "gc_freed", metrics.gc_freed);
49+
setnumfield(L, m, "gc_allocated", metrics.gc_allocated);
50+
51+
setnumfield(L, m, "gc_steps_pause", metrics.gc_steps_pause);
52+
setnumfield(L, m, "gc_steps_propagate", metrics.gc_steps_propagate);
53+
setnumfield(L, m, "gc_steps_atomic", metrics.gc_steps_atomic);
54+
setnumfield(L, m, "gc_steps_sweepstring", metrics.gc_steps_sweepstring);
55+
setnumfield(L, m, "gc_steps_sweep", metrics.gc_steps_sweep);
56+
setnumfield(L, m, "gc_steps_finalize", metrics.gc_steps_finalize);
57+
58+
setnumfield(L, m, "jit_snap_restore", metrics.jit_snap_restore);
59+
setnumfield(L, m, "jit_trace_abort", metrics.jit_trace_abort);
60+
setnumfield(L, m, "jit_mcode_size", metrics.jit_mcode_size);
61+
setnumfield(L, m, "jit_trace_num", metrics.jit_trace_num);
62+
63+
return 1;
64+
}
65+
66+
/* ------------------------------------------------------------------------ */
67+
68+
#include "lj_libdef.h"
69+
70+
LUALIB_API int luaopen_misc(struct lua_State *L)
71+
{
72+
LJ_LIB_REG(L, LUAM_MISCLIBNAME, misc);
73+
return 1;
74+
}

src/lj_mapi.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
** Miscellaneous public C API extensions.
3+
**
4+
** Major portions taken verbatim or adapted from the LuaVela.
5+
** Copyright (C) 2015-2019 IPONWEB Ltd.
6+
*/
7+
8+
#include "lua.h"
9+
#include "lmisclib.h"
10+
11+
#include "lj_obj.h"
12+
#include "lj_dispatch.h"
13+
14+
#if LJ_HASJIT
15+
#include "lj_jit.h"
16+
#endif
17+
18+
LUAMISC_API void luaM_metrics(lua_State *L, struct luam_Metrics *metrics)
19+
{
20+
global_State *g = G(L);
21+
GCState *gc = &g->gc;
22+
#if LJ_HASJIT
23+
jit_State *J = G2J(g);
24+
#endif
25+
26+
lua_assert(metrics != NULL);
27+
28+
metrics->strhash_hit = g->strhash_hit;
29+
metrics->strhash_miss = g->strhash_miss;
30+
31+
metrics->gc_strnum = g->strnum;
32+
metrics->gc_tabnum = gc->tabnum;
33+
metrics->gc_udatanum = gc->udatanum;
34+
#if LJ_HASFFI
35+
metrics->gc_cdatanum = gc->cdatanum;
36+
#else
37+
metrics->gc_cdatanum = 0;
38+
#endif
39+
40+
metrics->gc_total = gc->total;
41+
metrics->gc_freed = gc->freed;
42+
metrics->gc_allocated = gc->allocated;
43+
44+
metrics->gc_steps_pause = gc->state_count[GCSpause];
45+
metrics->gc_steps_propagate = gc->state_count[GCSpropagate];
46+
metrics->gc_steps_atomic = gc->state_count[GCSatomic];
47+
metrics->gc_steps_sweepstring = gc->state_count[GCSsweepstring];
48+
metrics->gc_steps_sweep = gc->state_count[GCSsweep];
49+
metrics->gc_steps_finalize = gc->state_count[GCSfinalize];
50+
51+
#if LJ_HASJIT
52+
metrics->jit_snap_restore = J->nsnaprestore;
53+
metrics->jit_trace_abort = J->ntraceabort;
54+
metrics->jit_mcode_size = J->szallmcarea;
55+
metrics->jit_trace_num = J->tracenum;
56+
#else
57+
metrics->jit_snap_restore = 0;
58+
metrics->jit_trace_abort = 0;
59+
metrics->jit_mcode_size = 0;
60+
metrics->jit_trace_num = 0;
61+
#endif
62+
}

src/ljamalg.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "lj_strfmt.c"
4949
#include "lj_strfmt_num.c"
5050
#include "lj_api.c"
51+
#include "lj_mapi.c"
5152
#include "lj_profile.c"
5253
#include "lj_lex.c"
5354
#include "lj_parse.c"
@@ -93,5 +94,6 @@
9394
#include "lib_bit.c"
9495
#include "lib_jit.c"
9596
#include "lib_ffi.c"
97+
#include "lib_misc.c"
9698
#include "lib_init.c"
9799

src/lmisclib.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
** Miscellaneous public C API extensions.
3+
**
4+
** Major portions taken verbatim or adapted from the LuaVela.
5+
** Copyright (C) 2015-2019 IPONWEB Ltd.
6+
*/
7+
8+
#ifndef _LMISCLIB_H
9+
#define _LMISCLIB_H
10+
11+
#include "lua.h"
12+
13+
/* API for obtaining various platform metrics. */
14+
15+
struct luam_Metrics {
16+
/*
17+
** Number of strings being interned (i.e. the string with the
18+
** same payload is found, so a new one is not created/allocated).
19+
*/
20+
size_t strhash_hit;
21+
/* Total number of strings allocations during the platform lifetime. */
22+
size_t strhash_miss;
23+
24+
/* Amount of allocated string objects. */
25+
size_t gc_strnum;
26+
/* Amount of allocated table objects. */
27+
size_t gc_tabnum;
28+
/* Amount of allocated udata objects. */
29+
size_t gc_udatanum;
30+
/* Amount of allocated cdata objects. */
31+
size_t gc_cdatanum;
32+
33+
/* Memory currently allocated. */
34+
size_t gc_total;
35+
/* Total amount of freed memory. */
36+
size_t gc_freed;
37+
/* Total amount of allocated memory. */
38+
size_t gc_allocated;
39+
40+
/* Count of incremental GC steps per state. */
41+
size_t gc_steps_pause;
42+
size_t gc_steps_propagate;
43+
size_t gc_steps_atomic;
44+
size_t gc_steps_sweepstring;
45+
size_t gc_steps_sweep;
46+
size_t gc_steps_finalize;
47+
48+
/*
49+
** Overall number of snap restores (amount of guard assertions
50+
** leading to stopping trace executions).
51+
*/
52+
size_t jit_snap_restore;
53+
/* Overall number of abort traces. */
54+
size_t jit_trace_abort;
55+
/* Total size of all allocated machine code areas. */
56+
size_t jit_mcode_size;
57+
/* Amount of JIT traces. */
58+
unsigned int jit_trace_num;
59+
};
60+
61+
LUAMISC_API void luaM_metrics(lua_State *L, struct luam_Metrics *metrics);
62+
63+
#define LUAM_MISCLIBNAME "misc"
64+
LUALIB_API int luaopen_misc(lua_State *L);
65+
66+
#endif /* _LMISCLIB_H */

src/luaconf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
#endif
145145

146146
#define LUALIB_API LUA_API
147+
#define LUAMISC_API LUA_API
147148

148149
/* Support for internal assertions. */
149150
#if defined(LUA_USE_ASSERT) || defined(LUA_USE_APICHECK)

0 commit comments

Comments
 (0)