Skip to content

Commit

Permalink
code style
Browse files Browse the repository at this point in the history
  • Loading branch information
detailyang committed Oct 25, 2016
1 parent 63bb95c commit ea28060
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 165 deletions.
8 changes: 0 additions & 8 deletions include/tsar_lua_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,7 @@

#include "define.h"

void inject_lua_package_path(lua_State *L);
void inject_tsar_consts(lua_State *L);
void inject_tsar_api(lua_State *L);
void close_luavm(lua_State *L);
lua_State *load_luavm();
void lua_set_mod(lua_State *L, struct module *mod);
void load_lua_module(lua_State *L, struct module *mod);
void lua_module_set_st_record_wrapper(struct module *mod, double st_array[],
U_64 pre_array[], U_64 cur_array[], int inter);
void lua_module_data_collect_wrapper(struct module *mod, char *parameter);

#endif
315 changes: 158 additions & 157 deletions src/tsar_lua_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,7 @@
#include "tsar.h"


void
close_luavm(lua_State *L)
{
lua_close(L);
}


lua_State *
load_luavm()
{
lua_State *vm;

vm = luaL_newstate();
if (vm == NULL) {
return NULL;
}

luaL_openlibs(vm);

inject_lua_package_path(vm);
inject_tsar_api(vm);

return vm;
}


void
static void
lua_set_mod(lua_State *L, struct module *mod)
{
lua_newtable(L);
Expand All @@ -64,7 +38,7 @@ lua_set_mod(lua_State *L, struct module *mod)
}


void
static void
inject_lua_package_path(lua_State *L)
{
const char *old_path;
Expand Down Expand Up @@ -102,7 +76,7 @@ inject_lua_package_path(lua_State *L)
}


void
static void
inject_tsar_consts(lua_State *L)
{
lua_pushinteger(L, HIDE_BIT);
Expand Down Expand Up @@ -137,7 +111,7 @@ inject_tsar_consts(lua_State *L)
}


void
static void
inject_tsar_api(lua_State *L)
{
/* tsar */
Expand All @@ -149,7 +123,158 @@ inject_tsar_api(lua_State *L)
}


static int
load_lua_module_info(lua_State *L, struct module *mod)
{
int i = 0;
int info_len = 0;


lua_getfield(L, -1, "info");
if (!lua_istable(L, -1)) {
do_debug(LOG_ERR, "load_lua_module info isn't table\n");
return 1;
}

info_len = lua_objlen(L, -1);
if (info_len == 0) {
return 0;
}
mod->n_col = info_len;

if (mod->info) {
do_debug(LOG_ERR, "load_lua_module duplicated malloc info\n");
return 1;
}

mod->info = malloc(info_len * sizeof(struct mod_info));
if (!mod->info) {
do_debug(LOG_ERR, "load_lua_module malloc info error\n");
return 1;
}

for (i = 0; i < info_len; i ++) {
lua_rawgeti(L, -1, i + 1); // lua begin from 1
if (!lua_istable(L, -1)) {
do_debug(LOG_ERR, "load_lua_module info[%d] isn't table\n", i);
return 1;
}
lua_getfield(L, -1, "hdr");
if (!lua_isstring(L, -1)) {
do_debug(LOG_ERR, "load_lua_module info.hdr isn't string\n");
return 1;
}
sprintf(mod->info[i].hdr, "%s", lua_tostring(L, -1));
lua_pop(L, 1);

lua_getfield(L, -1, "summary_bit");
if (!lua_isnumber(L, -1)) {
do_debug(LOG_ERR, "load_lua_module info.summary_bit isn't number\n");
return 1;
}
mod->info[i].summary_bit = lua_tointeger(L, -1);
lua_pop(L, 1);

lua_getfield(L, -1, "merge_mode");
if (!lua_isnumber(L, -1)) {
do_debug(LOG_ERR, "load_lua_module info.merge_mode isn't number\n");
return 1;
}
mod->info[i].merge_mode = lua_tointeger(L, -1);
lua_pop(L, 1);

lua_getfield(L, -1, "stats_opt");
if (!lua_isnumber(L, -1)) {
do_debug(LOG_ERR, "load_lua_module info.stats_opt isn't number\n");
return 1;
}
mod->info[i].stats_opt = lua_tointeger(L, -1);
lua_pop(L, 1);

lua_pop(L, 1);
do_debug(LOG_DEBUG, "load_lua_module hdr:%s summary_bit:%d, merge_mode:%d, stats_opt:%d\n",
mod->info[i].hdr, mod->info[i].summary_bit, mod->info[i].merge_mode, mod->info[i].stats_opt);
}

lua_pop(L, 2);

return 0;
}


static int
load_lua_module_optusage(lua_State *L, struct module *mod)
{
lua_getfield(L, -1, "opt");
if (!lua_isstring(L, -1)) {
do_debug(LOG_ERR, "load_lua_module opt isn't string\n");
return 1;
}
sprintf(mod->opt_line, "%s", lua_tostring(L, -1));
do_debug(LOG_DEBUG, "load_lua_module opt:%s\n", mod->opt_line);
lua_pop(L, 1);

lua_getfield(L, -1, "usage");
if (!lua_isstring(L, -1)) {
do_debug(LOG_ERR, "load_lua_module usage isn't string\n");
return 1;
}
sprintf(mod->usage, "%s", lua_tostring(L, -1));
do_debug(LOG_DEBUG, "load_lua_module usage:%s\n", mod->usage);
lua_pop(L, 1);

return 0;
}


static int
load_lua_module_register(lua_State *L, struct module *mod)
{
lua_getfield(L, -1, "register");
if (lua_pcall(L, 0, 1, 0) != 0) {
do_debug(LOG_ERR, "load_lua_module call _M.register() err %s\n", lua_tostring(L, -1));
return 1;
}

if (load_lua_module_optusage(L, mod) != 0) {
return 1;
}

if (load_lua_module_info(L, mod) != 0) {
return 1;
}

return 0;
}


void
close_luavm(lua_State *L)
{
lua_close(L);
}


lua_State *
load_luavm()
{
lua_State *vm;

vm = luaL_newstate();
if (vm == NULL) {
return NULL;
}

luaL_openlibs(vm);

inject_lua_package_path(vm);
inject_tsar_api(vm);

return vm;
}


static void
lua_module_set_st_record_wrapper(struct module *mod, double st_array[],
U_64 pre_array[], U_64 cur_array[], int inter)
{
Expand Down Expand Up @@ -248,7 +373,7 @@ lua_module_set_st_record_wrapper(struct module *mod, double st_array[],
}


void
static void
lua_module_data_collect_wrapper(struct module *mod, char *parameter)
{
lua_getglobal(L, mod->name);
Expand Down Expand Up @@ -283,133 +408,9 @@ lua_module_data_collect_wrapper(struct module *mod, char *parameter)
}


static int
load_lua_module_info(lua_State *L, struct module *mod)
{
int i = 0;
int info_len = 0;


lua_getfield(L, -1, "info");
if (!lua_istable(L, -1)) {
do_debug(LOG_ERR, "load_lua_module info isn't table\n");
return 1;
}

info_len = lua_objlen(L, -1);
if (info_len == 0) {
return 0;
}
mod->n_col = info_len;

if (mod->info) {
do_debug(LOG_ERR, "load_lua_module duplicated malloc info\n");
return 1;
}

mod->info = malloc(info_len * sizeof(struct mod_info));
if (!mod->info) {
do_debug(LOG_ERR, "load_lua_module malloc info error\n");
return 1;
}

for (i = 0; i < info_len; i ++) {
lua_rawgeti(L, -1, i + 1); // lua begin from 1
if (!lua_istable(L, -1)) {
do_debug(LOG_ERR, "load_lua_module info[%d] isn't table\n", i);
return 1;
}
lua_getfield(L, -1, "hdr");
if (!lua_isstring(L, -1)) {
do_debug(LOG_ERR, "load_lua_module info.hdr isn't string\n");
return 1;
}
sprintf(mod->info[i].hdr, "%s", lua_tostring(L, -1));
lua_pop(L, 1);

lua_getfield(L, -1, "summary_bit");
if (!lua_isnumber(L, -1)) {
do_debug(LOG_ERR, "load_lua_module info.summary_bit isn't number\n");
return 1;
}
mod->info[i].summary_bit = lua_tointeger(L, -1);
lua_pop(L, 1);

lua_getfield(L, -1, "merge_mode");
if (!lua_isnumber(L, -1)) {
do_debug(LOG_ERR, "load_lua_module info.merge_mode isn't number\n");
return 1;
}
mod->info[i].merge_mode = lua_tointeger(L, -1);
lua_pop(L, 1);

lua_getfield(L, -1, "stats_opt");
if (!lua_isnumber(L, -1)) {
do_debug(LOG_ERR, "load_lua_module info.stats_opt isn't number\n");
return 1;
}
mod->info[i].stats_opt = lua_tointeger(L, -1);
lua_pop(L, 1);

lua_pop(L, 1);
do_debug(LOG_DEBUG, "load_lua_module hdr:%s summary_bit:%d, merge_mode:%d, stats_opt:%d\n",
mod->info[i].hdr, mod->info[i].summary_bit, mod->info[i].merge_mode, mod->info[i].stats_opt);
}

lua_pop(L, 2);

return 0;
}


static int
load_lua_module_optusage(lua_State *L, struct module *mod)
{
lua_getfield(L, -1, "opt");
if (!lua_isstring(L, -1)) {
do_debug(LOG_ERR, "load_lua_module opt isn't string\n");
return 1;
}
sprintf(mod->opt_line, "%s", lua_tostring(L, -1));
do_debug(LOG_DEBUG, "load_lua_module opt:%s\n", mod->opt_line);
lua_pop(L, 1);

lua_getfield(L, -1, "usage");
if (!lua_isstring(L, -1)) {
do_debug(LOG_ERR, "load_lua_module usage isn't string\n");
return 1;
}
sprintf(mod->usage, "%s", lua_tostring(L, -1));
do_debug(LOG_DEBUG, "load_lua_module usage:%s\n", mod->usage);
lua_pop(L, 1);

return 0;
}


static int
load_lua_module_register(lua_State *L, struct module *mod)
{
lua_getfield(L, -1, "register");
if (lua_pcall(L, 0, 1, 0) != 0) {
do_debug(LOG_ERR, "load_lua_module call _M.register() err %s\n", lua_tostring(L, -1));
return 1;
}

if (load_lua_module_optusage(L, mod) != 0) {
return 1;
}

if (load_lua_module_info(L, mod) != 0) {
return 1;
}

return 0;
}

void
load_lua_module(lua_State *L, struct module *mod) {

load_lua_module(lua_State *L, struct module *mod)
{
char buff[LEN_128] = {0};
char mod_path[LEN_128] = {0};

Expand Down

0 comments on commit ea28060

Please sign in to comment.