[FEATURE]support xx_plugin statistics(new API maat_register_thread)

This commit is contained in:
liuwentan
2023-10-13 17:55:51 +08:00
parent 93bfab81e8
commit 48af7e7aac
17 changed files with 800 additions and 259 deletions

View File

@@ -57,6 +57,10 @@ int bool_plugin_runtime_get_ex_data(void *bool_plugin_runtime, unsigned long lon
long long bool_plugin_runtime_update_err_count(void *bool_plugin_runtime);
void bool_plugin_runtime_scan_inc(void *bool_plugin_runtime, int thread_id);
long long bool_plugin_runtime_scan_count(void *bool_plugin_runtime);
#ifdef __cplusplus
}
#endif

View File

@@ -61,6 +61,10 @@ void fqdn_rule_free(struct FQDN_rule *fqdn_rule);
long long fqdn_plugin_runtime_update_err_count(void *fqdn_plugin_runtime);
void fqdn_plugin_runtime_scan_inc(void *fqdn_plugin_runtime, int thread_id);
long long fqdn_plugin_runtime_scan_count(void *fqdn_plugin_runtime);
#ifdef __cplusplus
}
#endif

View File

@@ -57,6 +57,10 @@ int ip_plugin_runtime_get_ex_data(void *ip_plugin_runtime, const struct ip_addr
long long ip_plugin_runtime_update_err_count(void *ip_plugin_runtime);
void ip_plugin_runtime_scan_inc(void *ip_plugin_rt, int thread_id);
long long ip_plugin_runtime_scan_count(void *ip_plugin_runtime);
#ifdef __cplusplus
}
#endif

View File

@@ -59,6 +59,10 @@ int ipport_plugin_runtime_get_ex_data(void *ipport_plugin_runtime, const struct
long long ipport_plugin_runtime_update_err_count(void *ipport_plugin_runtime);
void ipport_plugin_runtime_scan_inc(void *ipport_plugin_rt, int thread_id);
long long ipport_plugin_runtime_scan_count(void *ipport_plugin_runtime);
#ifdef __cplusplus
}
#endif

View File

@@ -73,6 +73,10 @@ const char *plugin_runtime_cached_row_get(void *plugin_runtime, size_t index);
void *plugin_runtime_get_ex_data(void *plugin_runtime, void *plugin_schema,
const char *key, size_t key_len);
void plugin_runtime_scan_inc(void *plugin_runtime, int thread_id);
long long plugin_runtime_scan_count(void *plugin_runtime);
#ifdef __cplusplus
}
#endif

View File

@@ -179,6 +179,7 @@ struct maat {
long long new_version;
int is_running;
int thread_counter;
pthread_t cfg_mon_thread;
pthread_mutex_t background_update_mutex;

View File

@@ -436,6 +436,25 @@ void maat_free(struct maat *maat_inst)
pthread_join(maat_inst->cfg_mon_thread, &ret);
}
__thread int _thread_local_tid = -1;
void maat_register_thread(struct maat *maat_inst)
{
int thread_id = __sync_fetch_and_add(&maat_inst->thread_counter, 1);
assert(thread_id < maat_inst->opts.nr_worker_thread);
_thread_local_tid = thread_id;
}
static int _get_tid(struct maat *maat_inst)
{
if (_thread_local_tid < 0 ||
_thread_local_tid >= maat_inst->opts.nr_worker_thread) {
return -1;
}
return _thread_local_tid;
}
int maat_helper_read_column(const char *table_line, int Nth_column,
size_t *column_offset, size_t *column_len)
{
@@ -870,6 +889,11 @@ void *maat_plugin_table_get_ex_data(struct maat *maat_inst, int table_id,
return NULL;
}
int thread_id = _get_tid(maat_inst);
if (thread_id >= 0 && thread_id < maat_inst->opts.nr_worker_thread) {
plugin_runtime_scan_inc(runtime, thread_id);
}
void *ret = NULL;
enum table_type table_type = table_manager_get_table_type(maat_inst->tbl_mgr,
table_id);
@@ -901,13 +925,12 @@ int maat_ip_plugin_table_get_ex_data(struct maat *maat_inst, int table_id,
return -1;
}
int n_ex_data = ip_plugin_runtime_get_ex_data(ip_plugin_rt, ip_addr,
ex_data_array, array_size);
if (n_ex_data < 0) {
return -1;
int thread_id = _get_tid(maat_inst);
if (thread_id >= 0 && thread_id < maat_inst->opts.nr_worker_thread) {
ip_plugin_runtime_scan_inc(ip_plugin_rt, thread_id);
}
return n_ex_data;
return ip_plugin_runtime_get_ex_data(ip_plugin_rt, ip_addr, ex_data_array, array_size);
}
int maat_ipport_plugin_table_get_ex_data(struct maat *maat_inst, int table_id,
@@ -929,13 +952,13 @@ int maat_ipport_plugin_table_get_ex_data(struct maat *maat_inst, int table_id,
return -1;
}
int n_ex_data = ipport_plugin_runtime_get_ex_data(ipport_plugin_rt, ip_addr, port,
ex_data_array, array_size);
if (n_ex_data < 0) {
return -1;
int thread_id = _get_tid(maat_inst);
if (thread_id >= 0 && thread_id < maat_inst->opts.nr_worker_thread) {
ipport_plugin_runtime_scan_inc(ipport_plugin_rt, thread_id);
}
return n_ex_data;
return ipport_plugin_runtime_get_ex_data(ipport_plugin_rt, ip_addr, port,
ex_data_array, array_size);
}
int maat_fqdn_plugin_table_get_ex_data(struct maat *maat_inst, int table_id,
@@ -957,13 +980,12 @@ int maat_fqdn_plugin_table_get_ex_data(struct maat *maat_inst, int table_id,
return -1;
}
int n_ex_data = fqdn_plugin_runtime_get_ex_data(fqdn_plugin_rt, fqdn,
ex_data_array, array_size);
if (n_ex_data < 0) {
return -1;
int thread_id = _get_tid(maat_inst);
if (thread_id >= 0 && thread_id < maat_inst->opts.nr_worker_thread) {
fqdn_plugin_runtime_scan_inc(fqdn_plugin_rt, thread_id);
}
return n_ex_data;
return fqdn_plugin_runtime_get_ex_data(fqdn_plugin_rt, fqdn, ex_data_array, array_size);
}
int maat_bool_plugin_table_get_ex_data(struct maat *maat_inst, int table_id,
@@ -985,13 +1007,13 @@ int maat_bool_plugin_table_get_ex_data(struct maat *maat_inst, int table_id,
return -1;
}
int n_ex_data = bool_plugin_runtime_get_ex_data(bool_plugin_rt, item_ids, n_item,
ex_data_array, array_size);
if (n_ex_data < 0) {
return -1;
int thread_id = _get_tid(maat_inst);
if (thread_id >= 0 && thread_id < maat_inst->opts.nr_worker_thread) {
bool_plugin_runtime_scan_inc(bool_plugin_rt, thread_id);
}
return n_ex_data;
return bool_plugin_runtime_get_ex_data(bool_plugin_rt, item_ids, n_item,
ex_data_array, array_size);
}
static inline int scan_status_should_compile_NOT(struct maat_state *state)

View File

@@ -39,6 +39,7 @@ struct bool_plugin_runtime {
long long rule_num;
long long update_err_cnt;
long long *scan_cnt;
};
/* bool plugin schema API */
@@ -198,6 +199,7 @@ void *bool_plugin_runtime_new(void *bool_plugin_schema, size_t max_thread_num,
bool_plugin_rt->n_worker_thread = max_thread_num;
bool_plugin_rt->ref_garbage_bin = garbage_bin;
bool_plugin_rt->logger = logger;
bool_plugin_rt->scan_cnt = alignment_int64_array_alloc(max_thread_num);
return bool_plugin_rt;
}
@@ -219,6 +221,11 @@ void bool_plugin_runtime_free(void *bool_plugin_runtime)
bool_plugin_rt->ex_data_rt = NULL;
}
if (bool_plugin_rt->scan_cnt != NULL) {
alignment_int64_array_free(bool_plugin_rt->scan_cnt);
bool_plugin_rt->scan_cnt = NULL;
}
FREE(bool_plugin_rt);
}
@@ -568,4 +575,28 @@ long long bool_plugin_runtime_update_err_count(void *bool_plugin_runtime)
struct bool_plugin_runtime *bool_plugin_rt = (struct bool_plugin_runtime *)bool_plugin_runtime;
return bool_plugin_rt->update_err_cnt;
}
void bool_plugin_runtime_scan_inc(void *bool_plugin_runtime, int thread_id)
{
if (NULL == bool_plugin_runtime || thread_id < 0) {
return;
}
struct bool_plugin_runtime *bool_plugin_rt = (struct bool_plugin_runtime *)bool_plugin_runtime;
alignment_int64_array_add(bool_plugin_rt->scan_cnt, thread_id, 1);
}
long long bool_plugin_runtime_scan_count(void *bool_plugin_runtime)
{
if (NULL == bool_plugin_runtime) {
return 0;
}
struct bool_plugin_runtime *bool_plugin_rt = (struct bool_plugin_runtime *)bool_plugin_runtime;
long long sum = alignment_int64_array_sum(bool_plugin_rt->scan_cnt,
bool_plugin_rt->n_worker_thread);
alignment_int64_array_reset(bool_plugin_rt->scan_cnt, bool_plugin_rt->n_worker_thread);
return sum;
}

View File

@@ -40,6 +40,7 @@ struct fqdn_plugin_runtime {
long long rule_num;
long long update_err_cnt;
long long *scan_cnt;
};
void *fqdn_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
@@ -197,6 +198,7 @@ void *fqdn_plugin_runtime_new(void *fqdn_plugin_schema, size_t max_thread_num,
fqdn_plugin_rt->n_worker_thread = max_thread_num;
fqdn_plugin_rt->ref_garbage_bin = garbage_bin;
fqdn_plugin_rt->logger = logger;
fqdn_plugin_rt->scan_cnt = alignment_int64_array_alloc(max_thread_num);
return fqdn_plugin_rt;
}
@@ -218,6 +220,11 @@ void fqdn_plugin_runtime_free(void *fqdn_plugin_runtime)
fqdn_plugin_rt->ex_data_rt = NULL;
}
if (fqdn_plugin_rt->scan_cnt != NULL) {
alignment_int64_array_free(fqdn_plugin_rt->scan_cnt);
fqdn_plugin_rt->scan_cnt = NULL;
}
FREE(fqdn_plugin_rt);
}
@@ -582,4 +589,28 @@ long long fqdn_plugin_runtime_update_err_count(void *fqdn_plugin_runtime)
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
return fqdn_plugin_rt->update_err_cnt;
}
void fqdn_plugin_runtime_scan_inc(void *fqdn_plugin_runtime, int thread_id)
{
if (NULL == fqdn_plugin_runtime || thread_id < 0) {
return;
}
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
alignment_int64_array_add(fqdn_plugin_rt->scan_cnt, thread_id, 1);
}
long long fqdn_plugin_runtime_scan_count(void *fqdn_plugin_runtime)
{
if (NULL == fqdn_plugin_runtime) {
return 0;
}
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
long long sum = alignment_int64_array_sum(fqdn_plugin_rt->scan_cnt,
fqdn_plugin_rt->n_worker_thread);
alignment_int64_array_reset(fqdn_plugin_rt->scan_cnt, fqdn_plugin_rt->n_worker_thread);
return sum;
}

View File

@@ -43,6 +43,7 @@ struct ip_plugin_runtime {
long long rule_num;
long long update_err_cnt;
long long *scan_cnt;
};
void *ip_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
@@ -360,7 +361,8 @@ void *ip_plugin_runtime_new(void *ip_plugin_schema, size_t max_thread_num,
ip_plugin_rt->n_worker_thread = max_thread_num;
ip_plugin_rt->ref_garbage_bin = garbage_bin;
ip_plugin_rt->logger = logger;
ip_plugin_rt->scan_cnt = alignment_int64_array_alloc(max_thread_num);
return ip_plugin_rt;
}
@@ -381,6 +383,11 @@ void ip_plugin_runtime_free(void *ip_plugin_runtime)
ip_plugin_rt->ex_data_rt = NULL;
}
if (ip_plugin_rt->scan_cnt != NULL) {
alignment_int64_array_free(ip_plugin_rt->scan_cnt);
ip_plugin_rt->scan_cnt = NULL;
}
FREE(ip_plugin_rt);
}
@@ -582,4 +589,28 @@ long long ip_plugin_runtime_update_err_count(void *ip_plugin_runtime)
struct ip_plugin_runtime *ip_plugin_rt = (struct ip_plugin_runtime *)ip_plugin_runtime;
return ip_plugin_rt->update_err_cnt;
}
void ip_plugin_runtime_scan_inc(void *ip_plugin_runtime, int thread_id)
{
if (NULL == ip_plugin_runtime || thread_id < 0) {
return;
}
struct ip_plugin_runtime *ip_plugin_rt = (struct ip_plugin_runtime *)ip_plugin_runtime;
alignment_int64_array_add(ip_plugin_rt->scan_cnt, thread_id, 1);
}
long long ip_plugin_runtime_scan_count(void *ip_plugin_runtime)
{
if (NULL == ip_plugin_runtime) {
return 0;
}
struct ip_plugin_runtime *ip_plugin_rt = (struct ip_plugin_runtime *)ip_plugin_runtime;
long long sum = alignment_int64_array_sum(ip_plugin_rt->scan_cnt,
ip_plugin_rt->n_worker_thread);
alignment_int64_array_reset(ip_plugin_rt->scan_cnt, ip_plugin_rt->n_worker_thread);
return sum;
}

View File

@@ -66,6 +66,7 @@ struct ipport_plugin_runtime {
long long rule_num;
long long update_err_cnt;
long long *scan_cnt;
};
void *ipport_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
@@ -223,7 +224,8 @@ void *ipport_plugin_runtime_new(void *ipport_plugin_schema, size_t max_thread_nu
ipport_plugin_rt->n_worker_thread = max_thread_num;
ipport_plugin_rt->ref_garbage_bin = garbage_bin;
ipport_plugin_rt->logger = logger;
ipport_plugin_rt->scan_cnt = alignment_int64_array_alloc(max_thread_num);
return ipport_plugin_rt;
}
@@ -244,6 +246,11 @@ void ipport_plugin_runtime_free(void *ipport_plugin_runtime)
ipport_plugin_rt->ex_data_rt = NULL;
}
if (ipport_plugin_rt->scan_cnt != NULL) {
alignment_int64_array_free(ipport_plugin_rt->scan_cnt);
ipport_plugin_rt->scan_cnt = NULL;
}
FREE(ipport_plugin_rt);
}
@@ -621,4 +628,28 @@ long long ipport_plugin_runtime_update_err_count(void *ipport_plugin_runtime)
struct ipport_plugin_runtime *ipport_plugin_rt = (struct ipport_plugin_runtime *)ipport_plugin_runtime;
return ipport_plugin_rt->update_err_cnt;
}
void ipport_plugin_runtime_scan_inc(void *ipport_plugin_runtime, int thread_id)
{
if (NULL == ipport_plugin_runtime || thread_id < 0) {
return;
}
struct ipport_plugin_runtime *ipport_plugin_rt = (struct ipport_plugin_runtime *)ipport_plugin_runtime;
alignment_int64_array_add(ipport_plugin_rt->scan_cnt, thread_id, 1);
}
long long ipport_plugin_runtime_scan_count(void *ipport_plugin_runtime)
{
if (NULL == ipport_plugin_runtime) {
return 0;
}
struct ipport_plugin_runtime *ipport_plugin_rt = (struct ipport_plugin_runtime *)ipport_plugin_runtime;
long long sum = alignment_int64_array_sum(ipport_plugin_rt->scan_cnt,
ipport_plugin_rt->n_worker_thread);
alignment_int64_array_reset(ipport_plugin_rt->scan_cnt, ipport_plugin_rt->n_worker_thread);
return sum;
}

View File

@@ -18,6 +18,7 @@
#include "maat_plugin.h"
#include "maat_limits.h"
#include "maat_table.h"
#include "alignment.h"
#define MODULE_PLUGIN module_name_str("maat.plugin")
@@ -33,12 +34,14 @@ struct plugin_callback_schema {
struct plugin_runtime {
struct ex_data_runtime *ex_data_rt;
size_t n_worker_thread;
struct maat_garbage_bin *ref_garbage_bin;
struct log_handle *logger;
long long rule_num;
long long acc_line_num;
long long update_err_cnt;
long long *scan_cnt;
};
enum plugin_key_type {
@@ -314,8 +317,10 @@ void *plugin_runtime_new(void *plugin_schema, size_t max_thread_num,
&(schema->container_schema));
}
plugin_rt->n_worker_thread = max_thread_num;
plugin_rt->ref_garbage_bin = garbage_bin;
plugin_rt->logger = logger;
plugin_rt->scan_cnt = alignment_int64_array_alloc(max_thread_num);
return plugin_rt;
}
@@ -332,6 +337,11 @@ void plugin_runtime_free(void *plugin_runtime)
plugin_rt->ex_data_rt = NULL;
}
if (plugin_rt->scan_cnt != NULL) {
alignment_int64_array_free(plugin_rt->scan_cnt);
plugin_rt->scan_cnt = NULL;
}
FREE(plugin_rt);
}
@@ -673,4 +683,28 @@ void *plugin_runtime_get_ex_data(void *plugin_runtime, void *plugin_schema,
}
return ex_data_runtime_get_ex_data_by_key(plugin_rt->ex_data_rt, key, key_len);
}
void plugin_runtime_scan_inc(void *plugin_runtime, int thread_id)
{
if (NULL == plugin_runtime || thread_id < 0) {
return;
}
struct plugin_runtime *plugin_rt = (struct plugin_runtime *)plugin_runtime;
alignment_int64_array_add(plugin_rt->scan_cnt, thread_id, 1);
}
long long plugin_runtime_scan_count(void *plugin_runtime)
{
if (NULL == plugin_runtime) {
return 0;
}
struct plugin_runtime *plugin_rt = (struct plugin_runtime *)plugin_runtime;
long long sum = alignment_int64_array_sum(plugin_rt->scan_cnt,
plugin_rt->n_worker_thread);
alignment_int64_array_reset(plugin_rt->scan_cnt, plugin_rt->n_worker_thread);
return sum;
}

View File

@@ -379,6 +379,10 @@ static void maat_fieldstat_table_row_output(struct maat_stat *stat, int perf_on)
fieldstat_value_set(stat->fs_handle, stat->fs_column_id[i][COLUMN_RULE_NUM], rule_num);
total_rule_num += rule_num;
long long scan_cnt = table_manager_runtime_scan_count(stat->ref_tbl_mgr, i);
fieldstat_value_set(stat->fs_handle, stat->fs_column_id[i][COLUMN_SCAN_CNT], scan_cnt);
total_scan_cnt += scan_cnt;
if (table_type == TABLE_TYPE_PLUGIN || table_type == TABLE_TYPE_IP_PLUGIN ||
table_type == TABLE_TYPE_IPPORT_PLUGIN || table_type == TABLE_TYPE_BOOL_PLUGIN ||
table_type == TABLE_TYPE_FQDN_PLUGIN) {
@@ -406,10 +410,6 @@ static void maat_fieldstat_table_row_output(struct maat_stat *stat, int perf_on)
total_scan_cpu_time += scan_cpu_time;
}
long long scan_cnt = table_manager_runtime_scan_count(stat->ref_tbl_mgr, i);
fieldstat_value_set(stat->fs_handle, stat->fs_column_id[i][COLUMN_SCAN_CNT], scan_cnt);
total_scan_cnt += scan_cnt;
long long hit_cnt = table_manager_runtime_hit_count(stat->ref_tbl_mgr, i);
fieldstat_value_set(stat->fs_handle, stat->fs_column_id[i][COLUMN_HIT_CNT], hit_cnt);
total_hit_cnt += hit_cnt;

View File

@@ -194,6 +194,7 @@ struct table_operations table_ops[TABLE_TYPE_MAX] = {
.free_runtime = plugin_runtime_free,
.update_runtime = plugin_runtime_update,
.commit_runtime = plugin_runtime_commit,
.scan_count = plugin_runtime_scan_count,
.rule_count = plugin_runtime_rule_count,
.update_err_count = plugin_runtime_update_err_count
},
@@ -205,6 +206,7 @@ struct table_operations table_ops[TABLE_TYPE_MAX] = {
.free_runtime = ip_plugin_runtime_free,
.update_runtime = ip_plugin_runtime_update,
.commit_runtime = ip_plugin_runtime_commit,
.scan_count = ip_plugin_runtime_scan_count,
.rule_count = ip_plugin_runtime_rule_count,
.update_err_count = ip_plugin_runtime_update_err_count
},
@@ -217,6 +219,7 @@ struct table_operations table_ops[TABLE_TYPE_MAX] = {
.update_runtime = ipport_plugin_runtime_update,
.commit_runtime = ipport_plugin_runtime_commit,
.rule_count = ipport_plugin_runtime_rule_count,
.scan_count = ipport_plugin_runtime_scan_count,
.update_err_count = ipport_plugin_runtime_update_err_count
},
{
@@ -227,6 +230,7 @@ struct table_operations table_ops[TABLE_TYPE_MAX] = {
.free_runtime = fqdn_plugin_runtime_free,
.update_runtime = fqdn_plugin_runtime_update,
.commit_runtime = fqdn_plugin_runtime_commit,
.scan_count = fqdn_plugin_runtime_scan_count,
.rule_count = fqdn_plugin_runtime_rule_count,
.update_err_count = fqdn_plugin_runtime_update_err_count
},
@@ -238,6 +242,7 @@ struct table_operations table_ops[TABLE_TYPE_MAX] = {
.free_runtime = bool_plugin_runtime_free,
.update_runtime = bool_plugin_runtime_update,
.commit_runtime = bool_plugin_runtime_commit,
.scan_count = bool_plugin_runtime_scan_count,
.rule_count = bool_plugin_runtime_rule_count,
.update_err_count = bool_plugin_runtime_update_err_count
},