summaryrefslogtreecommitdiff
path: root/src/lua_plugin_manage.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua_plugin_manage.c')
-rw-r--r--src/lua_plugin_manage.c171
1 files changed, 114 insertions, 57 deletions
diff --git a/src/lua_plugin_manage.c b/src/lua_plugin_manage.c
index c5c17b7..27e1df3 100644
--- a/src/lua_plugin_manage.c
+++ b/src/lua_plugin_manage.c
@@ -29,7 +29,7 @@
* 08-06
* 1. 实现函数
* int script_execute;
- *
+ *
* 08-13
* 1. 修改部分创建列表使用的结构
* 2. 实现函数
@@ -43,7 +43,7 @@
#include <string.h>
#include <unistd.h>
-struct lua_plugin_manage_schema * global_schema = NULL;
+struct lua_plugin_manage_schema *global_schema = NULL;
#if 0
void lua_message_mq_destory(void * elt)
@@ -59,9 +59,10 @@ static UT_icd lua_message_mq_icd = {sizeof(struct lua_message_mq), NULL, NULL, N
void lua_plugin_destory(void *elt)
{
- if ( !elt ) return;
- struct lua_plugin * plugin = (struct lua_plugin *)elt;
- if ( plugin->sub_topic_array )
+ if (!elt)
+ return;
+ struct lua_plugin *plugin = (struct lua_plugin *)elt;
+ if (plugin->sub_topic_array)
utarray_free(plugin->sub_topic_array);
return;
}
@@ -74,14 +75,16 @@ static UT_icd lua_plugin_icd = {sizeof(struct lua_plugin), NULL, NULL, lua_plugi
* Return: 查找得到的plugin结构
* Description: 在global schema中根据plugin_id查找一个plugin
*/
-struct lua_plugin * search_plugin_by_id(int plugin_id)
+struct lua_plugin *search_plugin_by_id(int plugin_id)
{
- for ( int i = 0; i < global_schema->model_count; ++i) {
- struct lua_model * model = &global_schema->model[i];
+ for (int i = 0; i < global_schema->model_count; ++i)
+ {
+ struct lua_model *model = &global_schema->model[i];
- struct lua_plugin * plugin = NULL;
- while ( (plugin = utarray_next(model->plugin_array, plugin)) ) {
- if ( plugin->plugin_id == plugin_id )
+ struct lua_plugin *plugin = NULL;
+ while ((plugin = utarray_next(model->plugin_array, plugin)))
+ {
+ if (plugin->plugin_id == plugin_id)
return plugin;
else if (plugin->plugin_id > plugin_id)
return NULL;
@@ -97,11 +100,12 @@ struct lua_plugin * search_plugin_by_id(int plugin_id)
* Return: 查找得到的message_mq结构
* Description: 在global schema中根据topic_id查找一个message_mq
*/
-struct lua_message_mq * search_message_mq_by_id(int topic_id)
+struct lua_message_mq *search_message_mq_by_id(int topic_id)
{
- struct lua_message_mq * mq = NULL;
- while ((mq = utarray_next(global_schema->message_mq_array, mq))) {
- if ( mq->topic_id == topic_id )
+ struct lua_message_mq *mq = NULL;
+ while ((mq = utarray_next(global_schema->message_mq_array, mq)))
+ {
+ if (mq->topic_id == topic_id)
return mq;
}
return NULL;
@@ -339,7 +343,7 @@ struct lua_plugin_manage_schema *lua_plugin_manage_init(
/* 为将要加载的模块预分配内存 */
new_schema->model_count = specific_count;
/* 如果没有配置, 也应该算是创建成功 */
- if ( specific_count == 0 )
+ if (specific_count == 0)
return new_schema;
new_schema->model = (struct lua_model *)calloc(specific_count, sizeof(struct lua_model));
if (__glibc_unlikely(!new_schema->model))
@@ -389,6 +393,16 @@ struct lua_plugin_manage_schema *lua_plugin_manage_init(
#ifdef LUAPLUGIN_BASIC_UNITTEST
debug_lua_plugin_manage_schema(new_schema);
#endif
+
+ /* 初始化全局状态统计 */
+ global_plugin_statistics = (struct lua_plugin_statistics *)calloc((thread_count * global_max_plugin_id), sizeof(struct lua_plugin_statistics));
+ if (!global_plugin_statistics)
+ {
+ LOGERROR("create plugin statistics failed");
+ lua_plugin_manage_exit(new_schema);
+ return NULL;
+ }
+
return new_schema;
}
@@ -406,7 +420,8 @@ int lua_plugin_manage_load_one_specific(
if (__glibc_unlikely(!schema || !specific))
return -1;
- if ( schema->model ) {
+ if (schema->model)
+ {
schema->model = (struct lua_model *)realloc(schema->model, (schema->model_count + 1) * sizeof(struct lua_model));
schema->model_count += 1;
}
@@ -434,6 +449,17 @@ int lua_plugin_manage_load_one_specific(
return -1;
}
}
+
+ if (global_plugin_statistics)
+ free(global_plugin_statistics);
+ /* 初始化全局状态统计 */
+ global_plugin_statistics = (struct lua_plugin_statistics *)calloc((schema->state_count * global_max_plugin_id), sizeof(struct lua_plugin_statistics));
+ if (!global_plugin_statistics)
+ {
+ LOGERROR("create plugin statistics failed");
+ return -1;
+ }
+
return 0;
}
@@ -457,7 +483,8 @@ void lua_plugin_manage_exit(struct lua_plugin_manage_schema *lua_plug_mgr)
/* 在状态机中对每一个模块调用对应的卸载函数 */
int call_unload_ret = thread_state_call_unload(lua_plug_mgr->thread_state[state_index],
&lua_plug_mgr->model[model_index]);
- if ( call_unload_ret ) {
+ if (call_unload_ret)
+ {
LOGERROR("call state unload function failed, ret is %d", call_unload_ret);
}
}
@@ -473,72 +500,102 @@ void lua_plugin_manage_exit(struct lua_plugin_manage_schema *lua_plug_mgr)
}
free(lua_plug_mgr->model);
- if ( lua_plug_mgr->message_mq_array )
+ if (lua_plug_mgr->message_mq_array)
utarray_free(lua_plug_mgr->message_mq_array);
free(lua_plug_mgr);
return;
}
+/*
+ * Function:
+ * Input:
+ * Output:
+ * Return:
+ * Description: 获取某插件的运行成功及失败次数
+ */
+void lua_plugin_get_statistics(int plugin_id, int thread_id, int *new_success, int *new_fail, int *free_success, int *free_fail)
+{
+ int statistic_id = thread_id * global_max_plugin_id + plugin_id;
+ if (new_success)
+ *new_success = global_plugin_statistics[statistic_id].new_success_count;
+ if (new_fail)
+ *new_fail = global_plugin_statistics[statistic_id].new_failed_count;
+ if (free_success)
+ *free_success = global_plugin_statistics[statistic_id].free_success_count;
+ if (free_fail)
+ *free_fail = global_plugin_statistics[statistic_id].free_failed_count;
+ return;
+}
+
#ifdef LUAPLUGIN_BASIC_UNITTEST
-void debug_lua_state_stack(lua_State * state, int mod, const char * message)
+void debug_lua_state_stack(lua_State *state, int mod, const char *message)
{
int stackcount = lua_gettop(state);
- /* nothing in stack */
- if ( !stackcount ) {
+ /* nothing in stack */
+ if (!stackcount)
+ {
printf("debug stack, but stack is empty");
return;
}
- printf("\n***** begin to debug one lua stack *****\n");
- if ( message ) printf("debug here: %s\n", message);
- int i = stackcount;
- /* print variables one by one */
- for ( ; i > 0; --i ) {
- /* adjust variables according to mod */
- if ( mod ) i = 0 - i;
- int type = lua_type(state, i);
- printf("stack[%d]: ", i);
- switch (type) {
- case LUA_TBOOLEAN:
- printf(lua_toboolean(state, i) ? "true\n" : "false\n");
- break;
- case LUA_TNUMBER:
- printf("%g\n", lua_tonumber(state, i));
- break;
- case LUA_TSTRING:
- printf("%s\n", lua_tostring(state, i));
- break;
- default:
- printf("this is not normal type, type is: %d[%s]\n", type, lua_typename(state, type));
- break;
- }
- /* adjust variables according to mod */
- if ( mod ) i = 0 - i;
- }
- printf("***** end of debug one lua stack *****\n\n");
+ printf("\n***** begin to debug one lua stack *****\n");
+ if (message)
+ printf("debug here: %s\n", message);
+ int i = stackcount;
+ /* print variables one by one */
+ for (; i > 0; --i)
+ {
+ /* adjust variables according to mod */
+ if (mod)
+ i = 0 - i;
+ int type = lua_type(state, i);
+ printf("stack[%d]: ", i);
+ switch (type)
+ {
+ case LUA_TBOOLEAN:
+ printf(lua_toboolean(state, i) ? "true\n" : "false\n");
+ break;
+ case LUA_TNUMBER:
+ printf("%g\n", lua_tonumber(state, i));
+ break;
+ case LUA_TSTRING:
+ printf("%s\n", lua_tostring(state, i));
+ break;
+ default:
+ printf("this is not normal type, type is: %d[%s]\n", type, lua_typename(state, type));
+ break;
+ }
+ /* adjust variables according to mod */
+ if (mod)
+ i = 0 - i;
+ }
+ printf("***** end of debug one lua stack *****\n\n");
}
-void debug_lua_plugin_manage_schema(struct lua_plugin_manage_schema * schema)
+void debug_lua_plugin_manage_schema(struct lua_plugin_manage_schema *schema)
{
printf("\n***** begin to debug one lua schema *****\n");
printf("schema.st is %p\n", schema->st);
printf("schema state count is %d\n", schema->state_count);
- for ( int i = 0; i < schema->state_count; ++i ) {
+ for (int i = 0; i < schema->state_count; ++i)
+ {
printf("schema state[%d]pointer is %p\n", i, schema->thread_state[i]);
}
printf("schema model count is %d\n", schema->model_count);
- for ( int i = 0; i < schema->model_count; ++i ) {
+ for (int i = 0; i < schema->model_count; ++i)
+ {
printf("debug model[%d]\n", i);
printf("array %p, load %d, unload %d, env %d, mark %04x, count %04x\n",
- schema->model[i].plugin_array, schema->model[i].load_ref, schema->model[i].unload_ref,
- schema->model[i].private_env_ref, schema->model[i].model_mark, schema->model[i].plugin_count);
- struct lua_plugin * plugin = NULL;
- while ((plugin = utarray_next(schema->model[i].plugin_array, plugin))) {
+ schema->model[i].plugin_array, schema->model[i].load_ref, schema->model[i].unload_ref,
+ schema->model[i].private_env_ref, schema->model[i].model_mark, schema->model[i].plugin_count);
+ struct lua_plugin *plugin = NULL;
+ while ((plugin = utarray_next(schema->model[i].plugin_array, plugin)))
+ {
printf("%d, %d, %d\n", plugin->plugin_id, plugin->ctx_new_ref, plugin->ctx_free_ref);
}
}
- printf("***** end of debug one lua schema *****\n\n");
+ printf("***** end of debug one lua schema *****\n\n");
}
#endif \ No newline at end of file