Merge tag 'v1.8'

修复可导致误命中的严重错误

# Conflicts:
#	inc/Maat_rule.h
#	src/entry/Maat_rule.cpp
This commit is contained in:
zhengchao
2016-06-03 15:21:31 +08:00
31 changed files with 4496 additions and 1457 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -11,11 +11,13 @@
#include <MESA/MESA_htable.h>
#include <MESA/MESA_list_queue.h>
#include <MESA/MESA_handle_logger.h>
#include <MESA/field_stat2.h>
#include "Maat_rule.h"
#include "Maat_rule_internal.h"
#include "json2iris.h"
#include "dynamic_array.h"
#include "aligment_int64.h"
#include "config_monitor.h"
#include "map_str2int.h"
@@ -24,11 +26,11 @@
#include "mesa_fuzzy.h"
#include "great_index_engine.h"
int MAAT_FRAME_VERSION_1_3_20160131=1;
int MAAT_FRAME_VERSION_1_8_20160603=1;
const char *maat_module="MAAT Frame";
const char* CHARSET_STRING[]={"CHARSET_NONE","GBK","BIG5","UNICODE","UTF-8"};
const char* CHARSET_STRING[]={"NONE","gbk","big5","unicode","utf8","bin",
"unicode_ascii_esc","unicode_ascii_aligned","unicode_ncr_dec","unicode_ncr_hex","url_encode_gb2312","url_encode_utf8",""};
int converHextoint(char srctmp)
{
if(isdigit(srctmp))
@@ -78,6 +80,7 @@ iconv_t maat_iconv_open(struct _Maat_scanner_t* scanner,enum MAAT_CHARSET to,enu
cd=scanner->iconv_handle[to][from];
return cd;
}
int iconv_convert(struct _Maat_scanner_t* scanner,enum MAAT_CHARSET from,enum MAAT_CHARSET to,char *src,int srclen,char *dst,int *dstlen)
{
size_t ret;
@@ -103,7 +106,8 @@ int iconv_convert(struct _Maat_scanner_t* scanner,enum MAAT_CHARSET from,enum MA
if(ret!=(size_t)(-1))
{
if(to==CHARSET_UNICODE)//jump unicode 2 bytes head 0xFF 0xFE
if(to==CHARSET_UNICODE&&
(*(unsigned short*)pOutBuff==0xFFFE||*(unsigned short*)pOutBuff==0XFEFF))//jump unicode 2 bytes BOM, 0xFF 0xFE
{
copy_len=iOutBuffLen-iLeftLen-2;
copy_buf=pOutBuff+2;
@@ -132,7 +136,137 @@ int iconv_convert(struct _Maat_scanner_t* scanner,enum MAAT_CHARSET from,enum MA
}
}
int URLEncode(const char* str, const int strSize, char* result, const int resultSize)
{
int i;
int j = 0;//for result index
char ch;
if ((str==NULL) || (result==NULL) || (strSize<=0) || (resultSize<=0))
{
return -1;
}
for ( i=0; (i<strSize)&&(j<resultSize); ++i)
{
ch = str[i];
if (((ch>='A') && (ch<'Z')) ||
((ch>='a') && (ch<'z')) ||
((ch>='0') && (ch<'9')))
{
result[j++] = ch;
}
else if (ch == ' ')
{
result[j++] = '+';
}
else if (ch == '.' || ch == '-' || ch == '_' || ch == '*')
{
result[j++] = ch;
}
else
{
if (j+3 < resultSize)
{
sprintf(result+j, "%%%02X", (unsigned char)ch);
j += 3;
}
else
{
return -1;
}
}
}
result[j] = '\0';
return j;
}
int uni2ascii(const char* fmt,const char* src, const int srclen, char* dst, const int dstsize)
{
int i=0,j=0;
assert(srclen%2==0);//unicode must be 2 bytes aligned.
while(i<srclen&&j<dstsize)
{
if(*(unsigned short*)(src+i)<0x7f)
{
dst[j]=*(unsigned short*)(src+i);
j++;
}
else
{
j+=snprintf(dst+j,dstsize-j,fmt,*(unsigned short*)(src+i));
}
i+=2;
}
return j;
}
int universal_charset_convert(struct _Maat_scanner_t* scanner,enum MAAT_CHARSET from,enum MAAT_CHARSET to,char *src,int srclen,char *dst,int *dstlen)
{
int ret=0;
char* tmp_buff=NULL;
int tmp_buff_size=0;
MAAT_CHARSET tmp_dst_code=CHARSET_NONE;
const char* fmt=NULL;
switch(to)
{
case CHARSET_GBK:
case CHARSET_BIG5:
case CHARSET_UNICODE:
case CHARSET_UTF8:
ret=iconv_convert(scanner,from,to,src,srclen,dst,dstlen);
return ret;
break;
case CHARSET_UNICODE_ASCII_ESC:
tmp_dst_code=CHARSET_UNICODE;
fmt="\\u%x;";
break;
case CHARSET_UNICODE_ASCII_ALIGNED:
tmp_dst_code=CHARSET_UNICODE;
fmt="\\u%04x";
break;
case CHARSET_UNICODE_NCR_DEC:
tmp_dst_code=CHARSET_UNICODE;
fmt="&#%u;";
break;
case CHARSET_UNICODE_NCR_HEX:
tmp_dst_code=CHARSET_UNICODE;
fmt="&#x%x;";
break;
case CHARSET_URL_ENCODE_GB2312:
tmp_dst_code=CHARSET_GBK;
fmt=NULL;
break;
case CHARSET_URL_ENCODE_UTF8:
tmp_dst_code=CHARSET_UTF8;
fmt=NULL;
break;
default:
return -1;
break;
}
tmp_buff_size=*dstlen;
tmp_buff=(char*)malloc(tmp_buff_size);
ret=iconv_convert(scanner,from,tmp_dst_code,src,srclen,tmp_buff,&tmp_buff_size);
if(ret<0)
{
goto error_out;
}
if(fmt!=NULL)
{
ret=uni2ascii(fmt, tmp_buff, tmp_buff_size, dst,*dstlen);
}
else
{
ret=URLEncode(tmp_buff,tmp_buff_size,dst,*dstlen);
}
*dstlen=ret;
error_out:
free(tmp_buff);
tmp_buff=NULL;
return ret;
}
char* strlwr(char* string)
{
int i=0;
@@ -218,17 +352,7 @@ int cnt_maskbits(struct in6_addr mask)
}
return bits_cnt;
}
int aligment_int_array_sum(int * array,int size)
{
int sum=0,i=0;
int offset=0;
for(i=0;i<size;i++)
{
offset=(CPU_CACHE_ALIGMENT/sizeof(int))*i;
sum+=array[offset];
}
return sum;
}
int lqueue_destroy_cb(void *data, long data_len, void *arg)
{
assert(0);
@@ -253,12 +377,34 @@ int HASH_delete_by_id(MESA_htable_handle hash,int id)
ret=MESA_htable_del(hash,(unsigned char*)&id, sizeof(id), NULL);
return ret;
}
int read_table_info(struct _Maat_table_info_t** p_table_info,int num,const char* table_info_path,void* logger)
_Maat_table_info_t* create_table_info(int max_thread_num)
{
struct _Maat_table_info_t*p=NULL;
p=(struct _Maat_table_info_t*)calloc(sizeof(struct _Maat_table_info_t),1);
p->scan_cnt=aligment_int64_array_alloc(max_thread_num);
p->scan_cpu_time=aligment_int64_array_alloc(max_thread_num);
p->input_bytes=aligment_int64_array_alloc(max_thread_num);
p->stream_num=aligment_int64_array_alloc(max_thread_num);
p->hit_cnt=aligment_int64_array_alloc(max_thread_num);
p->cross_cache_size=0;
return p;
}
void destroy_table_info(struct _Maat_table_info_t*p)
{
aligment_int64_array_free(p->scan_cnt);
aligment_int64_array_free(p->scan_cpu_time);
aligment_int64_array_free(p->input_bytes);
aligment_int64_array_free(p->stream_num);
aligment_int64_array_free(p->hit_cnt);
free(p);
return;
}
int read_table_info(struct _Maat_table_info_t** p_table_info,int num,const char* table_info_path,int max_thread_num,void* logger)
{
FILE*fp=NULL;
char line[MAX_TABLE_LINE_SIZE];
int i=0,j=0,ret[4]={0},table_cnt=0;
char table_type[16],src_charset[16],dst_charset[64],merge[4];
char table_type[16],src_charset[256],dst_charset[256],merge[4];
MESA_htable_handle string2int_map=map_create();
char *token=NULL,*sub_token=NULL,*saveptr;
struct _Maat_table_info_t*p=NULL;
@@ -269,12 +415,28 @@ int read_table_info(struct _Maat_table_info_t** p_table_info,int num,const char*
map_register(string2int_map,"plugin", TABLE_TYPE_PLUGIN);
map_register(string2int_map,"intval", TABLE_TYPE_INTVAL);
map_register(string2int_map,"digest", TABLE_TYPE_DIGEST);
map_register(string2int_map,"expr_plus", TABLE_TYPE_EXPR_PLUS);
map_register(string2int_map,"group", TABLE_TYPE_GROUP);
map_register(string2int_map,"bin", CHARSET_NONE);
for(i=0;i<MAX_CHARSET_NUM;i++)
{
if(strlen(CHARSET_STRING[i])>0)
{
map_register(string2int_map,CHARSET_STRING[i], i);
}
else
{
break;
}
}
/*
map_register(string2int_map,"gbk", CHARSET_GBK);
map_register(string2int_map,"big5", CHARSET_BIG5);
map_register(string2int_map,"unicode", CHARSET_UNICODE);
map_register(string2int_map,"utf8", CHARSET_UTF8);
map_register(string2int_map,"unicode_hex", CHARSET_UNICODE_ASCII_ESC);
map_register(string2int_map,"unicode_hex", CHARSET_UNICODE_ASCII_ESC);
*/
map_register(string2int_map,"yes", 1);
map_register(string2int_map,"no", 0);
@@ -285,6 +447,7 @@ int read_table_info(struct _Maat_table_info_t** p_table_info,int num,const char*
MESA_handle_runtime_log(logger, RLOG_LV_FATAL,maat_module,
"Maat read table info %s error.\n",table_info_path);
}
i=0;
while(NULL!=fgets(line,sizeof(line),fp))
{
i++;
@@ -293,8 +456,8 @@ int read_table_info(struct _Maat_table_info_t** p_table_info,int num,const char*
{
continue;
}
p=(struct _Maat_table_info_t*)calloc(sizeof(struct _Maat_table_info_t),1);
p->cross_cache_size=0;
p=create_table_info(max_thread_num);
sscanf(line,"%hu\t%s\t%s\t%s\t%s\t%s\t%d",&(p->table_id)
,p->table_name
,table_type
@@ -309,9 +472,9 @@ int read_table_info(struct _Maat_table_info_t** p_table_info,int num,const char*
{
if(ret[j]<0)
{
fprintf(stderr,"Maat read table info %s line %d error.\n",table_info_path,i);
fprintf(stderr,"Maat read table info %s line %d error:unknown column.\n",table_info_path,i);
MESA_handle_runtime_log(logger, RLOG_LV_FATAL,maat_module,
"Maat read table info %s line %d error.\n",table_info_path,i);
"Maat read table info %s line %d error:unknown column.\n",table_info_path,i);
goto error_jump;
}
}
@@ -332,9 +495,9 @@ int read_table_info(struct _Maat_table_info_t** p_table_info,int num,const char*
}
else
{
fprintf(stderr,"Maat read table info %s line %d error.\n",table_info_path,i);
fprintf(stderr,"Maat read table info %s line %d error:unknown dest charset %s.\n",table_info_path,i,sub_token);
MESA_handle_runtime_log(logger, RLOG_LV_FATAL,maat_module,
"Maat read table info %s line %d error.\n",table_info_path,i);
"Maat read table info %s line %d error: unknown dest charset %s.\n",table_info_path,i,sub_token);
goto error_jump;
}
@@ -365,7 +528,7 @@ int read_table_info(struct _Maat_table_info_t** p_table_info,int num,const char*
table_cnt++;
continue;
error_jump:
free(p);
destroy_table_info(p);
p=NULL;
}
fclose(fp);
@@ -601,6 +764,7 @@ struct op_expr_t* create_op_expr(unsigned int expr_id,int operation,void* u_para
{
struct op_expr_t* op_expr=NULL;
op_expr=(struct op_expr_t*)calloc(sizeof(struct op_expr_t),1);
op_expr->no_effect_convert_cnt=0;
op_expr->p_expr=(boolean_expr_t*)calloc(sizeof(boolean_expr_t),1);
op_expr->p_expr->expr_id=expr_id;
op_expr->p_expr->operation=operation;
@@ -673,7 +837,7 @@ void destroy_digest_rule(GIE_digest_t*rule)
rule=NULL;
return;
}
struct _Maat_scanner_t* create_maat_scanner(unsigned int version,int scan_thread_num,MESA_lqueue_head tomb)
struct _Maat_scanner_t* create_maat_scanner(unsigned int version,int scan_thread_num,MESA_lqueue_head tomb,int rs_scan_type)
{
int i=0;
@@ -682,7 +846,7 @@ struct _Maat_scanner_t* create_maat_scanner(unsigned int version,int scan_thread
hargs.thread_safe=0;
hargs.hash_slot_size = 1024*1024;
hargs.max_elem_num = 0;
hargs.eliminate_type = HASH_ELIMINATE_ALGO_LRU;
hargs.eliminate_type = HASH_ELIMINATE_ALGO_FIFO;
hargs.expire_time = 0;
hargs.key_comp = NULL;
hargs.key2index = NULL;
@@ -706,14 +870,17 @@ struct _Maat_scanner_t* create_maat_scanner(unsigned int version,int scan_thread
scanner->region_hash=MESA_htable_create(&hargs, sizeof(hargs));
MESA_htable_print_crtl(scanner->region_hash,0);
scanner->district_map=map_create();
scanner->version=version;
scanner->cfg_num=0;
scanner->max_thread_num=scan_thread_num;
//optimized for CPU cache_alignment 64
scanner->ref_cnt=(int*)calloc(CPU_CACHE_ALIGMENT,scan_thread_num);
scanner->ref_cnt=aligment_int64_array_alloc(scan_thread_num);
scanner->region_update_q=MESA_lqueue_create(0,0);
scanner->region=rulescan_initialize(scan_thread_num);
rulescan_set_param(scanner->region,rs_scan_type);
scanner->tomb_ref=tomb;
scanner->region_rslt_buff=(scan_result_t*)malloc(sizeof(scan_result_t)*MAX_SCANNER_HIT_NUM*scan_thread_num);
@@ -737,7 +904,7 @@ void destroy_maat_scanner(struct _Maat_scanner_t*scanner)
MESA_htable_destroy(scanner->compile_hash,NULL);
MESA_htable_destroy(scanner->group_hash, NULL);
MESA_htable_destroy(scanner->region_hash, NULL);
map_destroy(scanner->district_map);
destroy_bool_matcher((void*)scanner->expr_compiler);
q_cnt=MESA_lqueue_get_count(scanner->region_update_q);
for(i=0;i<q_cnt;i++)
@@ -917,11 +1084,12 @@ void batch_update(struct _Maat_scanner_t *scanner,void*logger)
scanner->last_update_time=time(NULL);
return;
}
struct _Maat_group_rule_t* add_region_to_group(struct _Maat_group_rule_t* group,int region_id,int expr_id,enum MAAT_TABLE_TYPE region_type)
struct _Maat_group_rule_t* add_region_to_group(struct _Maat_group_rule_t* group,int region_id,int district_id,int expr_id,enum MAAT_TABLE_TYPE region_type)
{
struct _Maat_region_rule_t* region_rule=(struct _Maat_region_rule_t*)malloc(sizeof(struct _Maat_region_rule_t));
region_rule->region_id=region_id;
region_rule->expr_id=expr_id;
region_rule->district_id=district_id;
region_rule->region_type=region_type;
pthread_mutex_lock(&(group->mutex));
dynamic_array_write(group->region_rules,group->region_boundary,region_rule);
@@ -1075,12 +1243,24 @@ int sync_region(MESA_htable_handle region_hash,int region_id,const char* table_n
}
return 1;
}
int get_district_id(_Maat_scanner_t *scanner,const char* district_str)
{
int map_ret=0,district_id=-1;
map_ret=map_str2int(scanner->district_map, district_str,&district_id);
if(map_ret<0)
{
district_id= scanner->district_num;
map_register(scanner->district_map,district_str, district_id);
scanner->district_num++;
}
return district_id;
}
int add_expr_rule(struct _Maat_table_info_t* table,struct db_str_rule_t* db_rule,struct _Maat_scanner_t *scanner,void* logger)
{
unsigned int i=0,j=0;
char* p=NULL,*saveptr=NULL,*region_string=NULL;
int region_str_len=0,ret=0,k=0;
int expr_id=0;
int expr_id=0,district_id=-1;
scan_rule_t*p_rule=NULL;
struct _Maat_group_rule_t* group_rule=NULL;
@@ -1095,7 +1275,12 @@ int add_expr_rule(struct _Maat_table_info_t* table,struct db_str_rule_t* db_rule
int sub_expr_cnt=0;
struct op_expr_t *op_expr=NULL;
struct _Maat_group_rule_t* u_para=NULL;
if(table->table_type==TABLE_TYPE_EXPR_PLUS)
{
assert(strlen(db_rule->district)>0);
district_id=get_district_id(scanner, db_rule->district);
}
group_rule=(struct _Maat_group_rule_t*)HASH_fetch_by_id(scanner->group_hash, db_rule->group_id);
if(group_rule==NULL)
{
@@ -1167,7 +1352,7 @@ int add_expr_rule(struct _Maat_table_info_t* table,struct db_str_rule_t* db_rule
break;
case EXPR_TYPE_REGEX://it's easy,no need to charset convert
expr_id=scanner->exprid_generator++;
u_para=add_region_to_group(group_rule,db_rule->region_id,expr_id,TABLE_TYPE_EXPR);
u_para=add_region_to_group(group_rule,db_rule->region_id,district_id,expr_id,TABLE_TYPE_EXPR);
if(u_para==NULL)
{
return -1;
@@ -1233,7 +1418,7 @@ int add_expr_rule(struct _Maat_table_info_t* table,struct db_str_rule_t* db_rule
break;
}
expr_id=scanner->exprid_generator++;
u_para=add_region_to_group(group_rule, db_rule->region_id,expr_id, TABLE_TYPE_EXPR);
u_para=add_region_to_group(group_rule, db_rule->region_id,district_id,expr_id, table->table_type);
if(u_para==NULL)//duplicate
{
return -1;
@@ -1248,12 +1433,12 @@ int add_expr_rule(struct _Maat_table_info_t* table,struct db_str_rule_t* db_rule
{
continue;
}
region_str_len=strlen(sub_key_array[k])*2+1;
region_str_len=strlen(sub_key_array[k])*8+1; // 1 byte map to 8 bytes maximum, e.g. "&#x0627;" or "\u63221;"
region_string=(char*)calloc(sizeof(char),region_str_len);
if(table->src_charset!=dst_charset)//need convert
{
ret=iconv_convert(scanner,table->src_charset, dst_charset,
ret=universal_charset_convert(scanner,table->src_charset, dst_charset,
sub_key_array[k],strlen(sub_key_array[k]),
region_string, &region_str_len);
if(ret<0)
@@ -1266,13 +1451,10 @@ int add_expr_rule(struct _Maat_table_info_t* table,struct db_str_rule_t* db_rule
free(region_string);
continue;
}
//if convert take no effect
if(region_str_len==(int)strlen(sub_key_array[k])&&
0==memcmp(sub_key_array[k],region_string,region_str_len)&&
TRUE==table->src_charset_in_dst)
0==memcmp(sub_key_array[k],region_string,region_str_len))
{
free(region_string);
continue;
op_expr->no_effect_convert_cnt++;;
}
}
else
@@ -1291,14 +1473,23 @@ int add_expr_rule(struct _Maat_table_info_t* table,struct db_str_rule_t* db_rule
free(region_string);
region_string=NULL;
}
MESA_lqueue_join_tail(scanner->region_update_q,&op_expr, sizeof(void*));
//if each sub string's convert take no effect and src charset is one of the dst.
if(TRUE==table->src_charset_in_dst&&op_expr->no_effect_convert_cnt==sub_expr_cnt)
{
destroy_op_expr(op_expr);
op_expr=NULL;
}
else
{
MESA_lqueue_join_tail(scanner->region_update_q,&op_expr, sizeof(void*));
}
}
}
else
{
expr_id=scanner->exprid_generator++;
u_para=add_region_to_group(group_rule, db_rule->region_id,expr_id, TABLE_TYPE_EXPR);
u_para=add_region_to_group(group_rule, db_rule->region_id,district_id,expr_id, table->table_type);
if(u_para==NULL)
{
return -1;
@@ -1334,7 +1525,7 @@ int add_ip_rule(struct _Maat_table_info_t* table,struct db_ip_rule_t* db_ip_rule
scan_rule_t* p_rule=NULL;
struct op_expr_t* op_expr=NULL;
struct _Maat_group_rule_t* u_para=NULL;
int expr_id=0;
int expr_id=0,district_id=-1;
group_rule=(struct _Maat_group_rule_t*)HASH_fetch_by_id(scanner->group_hash, db_ip_rule->group_id);
if(group_rule==NULL)
@@ -1344,7 +1535,7 @@ int add_ip_rule(struct _Maat_table_info_t* table,struct db_ip_rule_t* db_ip_rule
}
expr_id=scanner->exprid_generator++;
u_para=add_region_to_group(group_rule,db_ip_rule->region_id,expr_id,TABLE_TYPE_IP);
u_para=add_region_to_group(group_rule,db_ip_rule->region_id,district_id,expr_id,TABLE_TYPE_IP);
if(u_para==NULL)
{
return -1;
@@ -1365,8 +1556,8 @@ int add_intval_rule(struct _Maat_table_info_t* table,struct db_intval_rule_t* in
scan_rule_t* p_rule=NULL;
struct op_expr_t* op_expr=NULL;
struct _Maat_group_rule_t* u_para=NULL;
int expr_id=0;
int expr_id=0,district_id=-1;
group_rule=(struct _Maat_group_rule_t*)HASH_fetch_by_id(scanner->group_hash, intval_rule->group_id);
if(group_rule==NULL)
{
@@ -1374,7 +1565,7 @@ int add_intval_rule(struct _Maat_table_info_t* table,struct db_intval_rule_t* in
HASH_add_by_id(scanner->group_hash, intval_rule->group_id, group_rule);
}
expr_id=scanner->exprid_generator++;
u_para=add_region_to_group(group_rule,intval_rule->region_id,expr_id,TABLE_TYPE_INTVAL);
u_para=add_region_to_group(group_rule,intval_rule->region_id,district_id,expr_id,TABLE_TYPE_INTVAL);
if(u_para==NULL)
{
return -1;
@@ -1394,7 +1585,7 @@ int add_digest_rule(struct _Maat_table_info_t* table,struct db_digest_rule_t* db
struct _Maat_group_rule_t* group_rule=NULL;
GIE_digest_t* digest_rule=NULL;
struct _Maat_group_rule_t* u_para=NULL;
int expr_id=0;
int expr_id=0,district_id=-1;
group_rule=(struct _Maat_group_rule_t*)HASH_fetch_by_id(scanner->group_hash, db_digest_rule->group_id);
if(group_rule==NULL)
@@ -1403,7 +1594,7 @@ int add_digest_rule(struct _Maat_table_info_t* table,struct db_digest_rule_t* db
HASH_add_by_id(scanner->group_hash, db_digest_rule->group_id, group_rule);
}
expr_id=scanner->exprid_generator++;
u_para=add_region_to_group(group_rule,db_digest_rule->region_id,expr_id,TABLE_TYPE_DIGEST);
u_para=add_region_to_group(group_rule,db_digest_rule->region_id,expr_id,district_id,TABLE_TYPE_DIGEST);
if(u_para==NULL)
{
return -1;
@@ -1607,7 +1798,8 @@ void update_group_rule(struct _Maat_table_info_t* table,const char* table_line,s
if(db_group_rule.is_valid==FALSE)
{
del_group_rule(table, &db_group_rule,scanner,logger);
table->cfg_num--;
//leave no trace for virtual group rule
// table->cfg_num--;
}
else
{
@@ -1623,7 +1815,7 @@ void update_group_rule(struct _Maat_table_info_t* table,const char* table_line,s
else
{
//no need to free db_group_rule,it was saved in scanner->compile_hash
table->cfg_num++;
// table->cfg_num++;
}
}
@@ -1642,20 +1834,46 @@ void update_expr_rule(struct _Maat_table_info_t* table,const char* table_line,st
{
struct db_str_rule_t* maat_str_rule=(struct db_str_rule_t*)malloc(sizeof(struct db_str_rule_t));
int ret=0,db_hexbin=0;
ret=sscanf(table_line,"%d\t%d\t%s\t%d\t%d\t%d\t%d",&(maat_str_rule->region_id)
,&(maat_str_rule->group_id)
,maat_str_rule->keywords
,(int*)&(maat_str_rule->expr_type)
,(int*)&(maat_str_rule->match_method)
,&db_hexbin
,&(maat_str_rule->is_valid));
if(ret!=7)
switch(table->table_type)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_module ,
"update error,invalid format of expr table %s:%s",table->table_name,table_line);
free(maat_str_rule);
maat_str_rule=NULL;
return;
case TABLE_TYPE_EXPR:
ret=sscanf(table_line,"%d\t%d\t%s\t%d\t%d\t%d\t%d",&(maat_str_rule->region_id)
,&(maat_str_rule->group_id)
,maat_str_rule->keywords
,(int*)&(maat_str_rule->expr_type)
,(int*)&(maat_str_rule->match_method)
,&db_hexbin
,&(maat_str_rule->is_valid));
if(ret!=7)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_module ,
"update error,invalid format of expr table %s:%s",table->table_name,table_line);
free(maat_str_rule);
maat_str_rule=NULL;
return;
}
break;
case TABLE_TYPE_EXPR_PLUS:
ret=sscanf(table_line,"%d\t%d\t%s\t%s\t%d\t%d\t%d\t%d",&(maat_str_rule->region_id)
,&(maat_str_rule->group_id)
,maat_str_rule->district
,maat_str_rule->keywords
,(int*)&(maat_str_rule->expr_type)
,(int*)&(maat_str_rule->match_method)
,&db_hexbin
,&(maat_str_rule->is_valid));
if(ret!=8)
{
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_module ,
"update error,invalid format of expr_plus table %s:%s",table->table_name,table_line);
free(maat_str_rule);
maat_str_rule=NULL;
return;
}
break;
default:
assert(0);
break;
}
switch(db_hexbin)
{
@@ -1830,6 +2048,14 @@ void update_ip_rule(struct _Maat_table_info_t* table,const char* table_line,stru
if(ret>0)
{
table->cfg_num--;
if(ip_rule->addr_type==4)
{
table->ipv4_rule_cnt--;
}
else
{
table->ipv6_rule_cnt--;
}
}
}
else
@@ -1845,6 +2071,14 @@ void update_ip_rule(struct _Maat_table_info_t* table,const char* table_line,stru
else
{
table->cfg_num++;
if(ip_rule->addr_type==4)
{
table->ipv4_rule_cnt++;
}
else
{
table->ipv6_rule_cnt++;
}
}
}
error_out:
@@ -2061,7 +2295,7 @@ void garbage_bury(MESA_lqueue_head garbage_q,void *logger)
long data_size=0;
const long q_cnt=MESA_lqueue_get_count(garbage_q);
int i=0,bury_cnt=0;
int ref_cnt=0;
long long ref_cnt=0;
int have_timeout=0;
time_t now=time(NULL);
for(i=0;i<q_cnt;i++)
@@ -2084,7 +2318,7 @@ void garbage_bury(MESA_lqueue_head garbage_q,void *logger)
destroy_group_rule(bag->group_rule);
break;
case GARBAGE_SCANNER:
ref_cnt=aligment_int_array_sum(bag->scanner->ref_cnt,bag->scanner->max_thread_num);
ref_cnt=aligment_int64_array_sum(bag->scanner->ref_cnt,bag->scanner->max_thread_num);
if(ref_cnt==0)
{
@@ -2094,7 +2328,7 @@ void garbage_bury(MESA_lqueue_head garbage_q,void *logger)
else
{
MESA_handle_runtime_log(logger,RLOG_LV_INFO,maat_module,
"scanner %p version %d force destroyed,ref_cnt %d.",
"scanner %p version %d force destroyed,ref_cnt %lld.",
bag->scanner,bag->scanner->version,ref_cnt);
}
@@ -2124,15 +2358,6 @@ void plugin_table_callback(struct _Maat_table_info_t* table,const char* table_li
struct _plugin_table_info* p_table_cb=table->cb_info;
char *p=NULL;
pthread_mutex_lock(&(p_table_cb->plugin_mutex));
if(p_table_cb->is_called_started==0)
{
for(i=0;i<p_table_cb->cb_plug_cnt;i++)
{
p_table_cb->cb_plug[i].start(p_table_cb->update_type,p_table_cb->cb_plug[i].u_para);
}
p_table_cb->is_called_started=1;
}
if(p_table_cb->cb_plug_cnt>0)
{
for(i=0;i<p_table_cb->cb_plug_cnt;i++)
@@ -2197,10 +2422,10 @@ void maat_start_cb(unsigned int new_version,int update_type,void*u_para)
struct _Maat_feather_t *feather=(struct _Maat_feather_t *)u_para;
struct _Maat_table_info_t* p_table=NULL;
struct _plugin_table_info* p_table_cb=NULL;
int i=0;
int i=0,j=0;
if(update_type==CM_UPDATE_TYPE_FULL)
{
feather->update_tmp_scanner=create_maat_scanner(new_version,feather->scan_thread_num,feather->garbage_q);
feather->update_tmp_scanner=create_maat_scanner(new_version,feather->scan_thread_num,feather->garbage_q,feather->rule_scan_type);
MESA_handle_runtime_log(feather->logger,RLOG_LV_INFO,maat_module,
"Full config version %u -> %u update start",
feather->maat_version,new_version);
@@ -2213,7 +2438,7 @@ void maat_start_cb(unsigned int new_version,int update_type,void*u_para)
feather->maat_version,new_version);
feather->maat_version=new_version;
}
for(i=0;i<MAX_TABLE_NUM;i++)
for(i=0;i<feather->table_cnt;i++)
{
p_table=feather->p_table_info[i];
if(p_table==NULL||p_table->table_type!=TABLE_TYPE_PLUGIN)
@@ -2221,8 +2446,13 @@ void maat_start_cb(unsigned int new_version,int update_type,void*u_para)
continue;
}
p_table_cb=p_table->cb_info;
p_table_cb->update_type=update_type;
assert(p_table_cb->is_called_started==0);
for(j=0;j<p_table_cb->cb_plug_cnt;j++)
{
if(p_table_cb->cb_plug[j].start!=NULL)
{
p_table_cb->cb_plug[j].start(update_type,p_table_cb->cb_plug[j].u_para);
}
}
}
return;
}
@@ -2249,17 +2479,13 @@ void maat_finish_cb(void* u_para)
continue;
}
p_table_cb=p_table->cb_info;
if(p_table_cb->is_called_started==0)
{
continue;
}
p_table_cb->is_called_started=0;
pthread_mutex_lock(&(p_table_cb->plugin_mutex));
for(j=0;j<p_table_cb->cb_plug_cnt;j++)
{
p_table_cb->cb_plug[j].finish(p_table_cb->cb_plug[j].u_para);
if(p_table_cb->cb_plug[j].finish!=NULL)
{
p_table_cb->cb_plug[j].finish(p_table_cb->cb_plug[j].u_para);
}
}
pthread_mutex_unlock(&(p_table_cb->plugin_mutex));
}
if(feather->update_tmp_scanner!=NULL)
{
@@ -2289,10 +2515,16 @@ void maat_finish_cb(void* u_para)
else
{
MESA_handle_runtime_log(feather->logger,RLOG_LV_INFO,maat_module,
"Postpone config version %u (%d entries) load to rulescan.",
"Postpone config version %u %d entries load to rulescan.",
feather->scanner->version,feather->scanner->cfg_num);
}
}
else
{
MESA_handle_runtime_log(feather->logger,RLOG_LV_INFO,maat_module,
"Version %d have no valid scan rules, plugin callback complete.",
feather->maat_version);
}
return;
}
void maat_update_cb(const char* table_name,const char* line,void *u_para)
@@ -2318,6 +2550,7 @@ void maat_update_cb(const char* table_name,const char* line,void *u_para)
switch(feather->p_table_info[table_id]->table_type)
{
case TABLE_TYPE_EXPR:
case TABLE_TYPE_EXPR_PLUS:
update_expr_rule(feather->p_table_info[table_id], line, scanner,feather->logger,feather->GROUP_MODE_ON);
break;
case TABLE_TYPE_IP:
@@ -2349,9 +2582,11 @@ void *thread_rule_monitor(void *arg)
const char* inc_cfg_dir=(const char*)feather->inc_dir;
struct _Maat_scanner_t* old_scanner=NULL;
long expr_wait_q_cnt=0;
int scan_dir_cnt=0;
while(feather->still_working)
{
usleep(feather->scan_interval_ms*1000);
scan_dir_cnt++;
config_monitor_traverse(feather->maat_version,
inc_cfg_dir,
maat_start_cb,
@@ -2389,12 +2624,17 @@ void *thread_rule_monitor(void *arg)
}
}
garbage_bury(feather->garbage_q,feather->logger);
if(feather->stat_on==1&&scan_dir_cnt%2==0)//output every 2 seconds
{
maat_stat_output(feather);
}
}
MESA_htable_destroy(feather->map_tablename2id,free);
destroy_maat_scanner(feather->scanner);
garbage_bury(feather->garbage_q,feather->logger);
MESA_lqueue_destroy(feather->garbage_q,lqueue_destroy_cb,NULL);
FS_stop(&(feather->stat_handle));
int i=0,j=0;
struct dynamic_array_t* d_array=NULL;
@@ -2414,9 +2654,14 @@ void *thread_rule_monitor(void *arg)
free(lines);
}
}
free(feather->p_table_info[i]);
destroy_table_info(feather->p_table_info[i]);
feather->p_table_info[i]=NULL;
}
aligment_int64_array_free(feather->thread_call_cnt);
aligment_int64_array_free(feather->inner_mid_cnt);
aligment_int64_array_free(feather->outer_mid_cnt);
aligment_int64_array_free(feather->hit_cnt);
free(feather);
return NULL;
}

View File

@@ -5,13 +5,14 @@
#include <MESA/MESA_htable.h>
#include <MESA/MESA_list_queue.h>
#include <MESA/field_stat2.h>
#include "dynamic_array.h"
#include "UniversalBoolMatch.h"
#include "rulescan.h"
#include "mesa_fuzzy.h"
#include "great_index_engine.h"
#include "aligment_int64.h"
#include <pthread.h>
#include <iconv.h>
@@ -30,15 +31,15 @@ typedef int atomic_t;
#else
#include <alsa/iatomic.h>
#endif
#define CPU_CACHE_ALIGMENT 64
#define TRUE 1
#define FALSE 0
#define MAX_TABLE_NUM 256
#define MAX_CHARSET_NUM 6
#define MAX_CHARSET_NUM 16
#define MAX_TABLE_NAME_LEN 256
#define MAX_TABLE_LINE_SIZE (1024*4)
#define MAX_EXPR_KEYLEN 1024
#define MAX_DISTRICT_LEN 64
#define MAX_PLUGING_NUM 32
#define MAX_SCANNER_HIT_NUM 64
@@ -47,6 +48,7 @@ typedef int atomic_t;
#define MAX_FAILED_NUM 128
#define MAX_MAAT_STAT_NUM 64
#ifndef MAX
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif
@@ -74,6 +76,7 @@ enum MAAT_TABLE_TYPE
TABLE_TYPE_PLUGIN,
TABLE_TYPE_INTVAL,
TABLE_TYPE_DIGEST,
TABLE_TYPE_EXPR_PLUS,
TABLE_TYPE_GROUP
};
@@ -91,11 +94,13 @@ enum MAAT_MATCH_METHOD
MATCH_METHOD_LEFT,
MATCH_METHOD_FULL
};
struct db_str_rule_t
{
int region_id;
int group_id;
char keywords[MAX_EXPR_KEYLEN];
char district[MAX_DISTRICT_LEN];
enum MAAT_EXPR_TYPE expr_type;
enum MAAT_MATCH_METHOD match_method;
int is_hexbin;
@@ -162,12 +167,14 @@ struct op_expr_t
{
boolean_expr_t* p_expr;
scan_rule_t* p_rules[MAAT_MAX_EXPR_ITEM_NUM];
int no_effect_convert_cnt;
};
struct _Maat_region_rule_t
{
int region_id;
int expr_id;
int district_id;
enum MAAT_TABLE_TYPE region_type;
};
struct _Maat_group_rule_t
@@ -223,20 +230,42 @@ struct _Maat_table_info_t
int do_charset_merge;
int cfg_num;
int cross_cache_size;
int expr_rule_cnt; //expr_type=0,1,3
int regex_rule_cnt; //expr_type=2
union
{
int expr_rule_cnt; //expr_type=0,1,3
int ipv4_rule_cnt;
};
union
{
int regex_rule_cnt; //expr_type=2
int ipv6_rule_cnt;
};
struct _plugin_table_info *cb_info;
//for stat>>>>>>>>
int stat_line_id;
mcore_long_t scan_cnt;
mcore_long_t scan_cpu_time; //nano
mcore_long_t input_bytes;
mcore_long_t stream_num;
mcore_long_t hit_cnt;
};
struct _scan_status_t
struct _INNER_scan_status_t
{
struct _Maat_feather_t* feather;
int thread_num;
int cur_hit_cnt;
int hit_group_cnt;
int hit_group_size;
unsigned int cur_hit_id[MAX_SCANNER_HIT_NUM];
unsigned int *hitted_group_id;
};
struct _OUTER_scan_status_t
{
struct _Maat_feather_t* feather;
unsigned short thread_num;
unsigned short is_set_district;
int district_id;
struct _INNER_scan_status_t* inner;
};
enum maat_garbage_type
{
GARBAGE_SCANNER=0,
@@ -273,13 +302,15 @@ struct _Maat_scanner_t
{
int version;
time_t last_update_time;
int *ref_cnt; //optimized for cache_alignment 64
long long *ref_cnt; //optimized for cache_alignment 64
rule_scanner_t region;
pthread_rwlock_t digest_rwlock[MAX_TABLE_NUM];
GIE_handle_t* digest_handle[MAX_TABLE_NUM];
MESA_htable_handle region_hash;
MESA_htable_handle group_hash;
MESA_htable_handle compile_hash;
MESA_htable_handle district_map;
unsigned int district_num;
unsigned int cfg_num;
unsigned int exprid_generator;
MESA_lqueue_head region_update_q;
@@ -296,17 +327,32 @@ struct _Maat_feather_t
struct _Maat_scanner_t *update_tmp_scanner;
MESA_lqueue_head garbage_q;
int table_cnt;
int GROUP_MODE_ON;
int still_working;
int scan_interval_ms;
int effect_interval_ms;
int stat_on;
int perf_on;
struct _Maat_table_info_t *p_table_info[MAX_TABLE_NUM];
MESA_htable_handle map_tablename2id;
void* logger;
int maat_version;
int scan_thread_num;
int rule_scan_type;
char inc_dir[MAX_TABLE_NAME_LEN];
char full_dir[MAX_TABLE_NAME_LEN];
int GROUP_MODE_ON;
int still_working;
int scan_interval_ms;
int effect_interval_ms;
char stat_file[MAX_TABLE_NAME_LEN];
//for stat>>>>
screen_stat_handle_t stat_handle;
int total_stat_id;
int fs_status_id[MAX_MAAT_STAT_NUM];
int fs_column_id[MAX_MAAT_STAT_NUM];
mcore_long_t outer_mid_cnt;
mcore_long_t inner_mid_cnt;
mcore_long_t hit_cnt;
mcore_long_t thread_call_cnt;//size indicate by scan_thread_num,
long long total_scan_bytes;
long long total_scan_cnt;
};
struct _maat_garbage_t
{
@@ -325,7 +371,7 @@ struct _maat_garbage_t
void garbage_bagging(enum maat_garbage_type type,void *p,MESA_lqueue_head garbage_q);
void garbage_bury(MESA_lqueue_head garbage_q,void *logger);
void make_group_set(const struct _Maat_compile_rule_t* compile_rule,universal_bool_expr_t* a_set);
int read_table_info(struct _Maat_table_info_t** p_table_info,int num,const char* table_info_path,void*logger);
int read_table_info(struct _Maat_table_info_t** p_table_info,int num,const char* table_info_path,int max_thread_num,void* logger);
void maat_start_cb(unsigned int new_version,int update_type,void*u_para);
void maat_update_cb(const char* table_name,const char* line,void *u_para);
void maat_finish_cb(void* u_para);
@@ -340,5 +386,11 @@ inline void ipv6_ntoh(unsigned int *v6_addr)
}
return;
}
void maat_stat_init(struct _Maat_feather_t* feather);
void maat_stat_table(struct _Maat_table_info_t* p_table,int scan_len,struct timespec* start, struct timespec* end,int thread_num);
void maat_stat_output(struct _Maat_feather_t* feather);
#endif

253
src/entry/Maat_stat.cpp Normal file
View File

@@ -0,0 +1,253 @@
#include "Maat_rule_internal.h"
#include "aligment_int64.h"
#include <time.h>
#include <MESA/field_stat.h>
enum MAAT_FS_STATUS{
STATUS_VERSION=0,
STATUS_THRED_NUM,
STATUS_TABLE_NUM,
STATUS_OUTER_MID_NUM,
STATUS_INNER_MID_NUM,
STATUS_GARBAGE_QSIZE,
STATUS_TOTAL_SCAN_LEN,
STATUS_TOTAL_SCAN_CNT,
};
enum MAAT_FS_COLUMN
{
COLUMN_TABLE_RULE_NUM=0,
COLUMN_TABLE_REGEX_NUM,
COLUMN_TABLE_STREAM_NUM,
COLUMN_TABLE_SCAN_CNT,
COLUMN_TABLE_SCAN_BYTES,
COLUMN_TABLE_CPU_TIME,//microseconds
COLUMN_TABLE_HIT_CNT,
};
void maat_stat_init(struct _Maat_feather_t* feather)
{
int value=0;
int i=0;
struct _Maat_table_info_t* p_table=NULL;
feather->stat_handle=FS_create_handle();
FS_set_para(feather->stat_handle, OUTPUT_DEVICE, feather->stat_file, strlen(feather->stat_file)+1);
value=1;
FS_set_para(feather->stat_handle, PRINT_MODE, &value, sizeof(value));
value=0;
FS_set_para(feather->stat_handle, CREATE_THREAD, &value, sizeof(value));
feather->fs_status_id[STATUS_VERSION]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT,"version");
feather->fs_status_id[STATUS_THRED_NUM]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT,"active_thread");
feather->fs_status_id[STATUS_TABLE_NUM]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT,"table_num");
feather->fs_status_id[STATUS_OUTER_MID_NUM]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT,"outer_mid");
feather->fs_status_id[STATUS_INNER_MID_NUM]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT,"inner_mid");
feather->fs_status_id[STATUS_GARBAGE_QSIZE]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT,"garbage_num");
feather->fs_status_id[STATUS_TOTAL_SCAN_LEN]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT,"scan_bytes");
feather->fs_status_id[STATUS_TOTAL_SCAN_CNT]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT,"scan_times");
feather->fs_column_id[COLUMN_TABLE_RULE_NUM]=FS_register(feather->stat_handle, FS_STYLE_COLUMN, FS_CALC_CURRENT,"rule");
feather->fs_column_id[COLUMN_TABLE_REGEX_NUM]=FS_register(feather->stat_handle, FS_STYLE_COLUMN, FS_CALC_CURRENT,"regex");
feather->fs_column_id[COLUMN_TABLE_STREAM_NUM]=FS_register(feather->stat_handle, FS_STYLE_COLUMN, FS_CALC_CURRENT,"stream");
feather->fs_column_id[COLUMN_TABLE_SCAN_BYTES]=FS_register(feather->stat_handle, FS_STYLE_COLUMN, FS_CALC_SPEED,"IN_Bps");
if(feather->perf_on==1)
{
feather->fs_column_id[COLUMN_TABLE_CPU_TIME]=FS_register(feather->stat_handle, FS_STYLE_COLUMN, FS_CALC_SPEED,"cpu_us");
value=feather->fs_column_id[COLUMN_TABLE_CPU_TIME];
FS_set_para(feather->stat_handle, ID_INVISBLE, &value, sizeof(value));
FS_register_ratio(feather->stat_handle,
feather->fs_column_id[COLUMN_TABLE_SCAN_BYTES],
feather->fs_column_id[COLUMN_TABLE_CPU_TIME],
1000000, //microsecond to second
FS_STYLE_COLUMN,
FS_CALC_SPEED,
"PROC_Bps");
}
feather->fs_column_id[COLUMN_TABLE_SCAN_CNT]=FS_register(feather->stat_handle, FS_STYLE_COLUMN, FS_CALC_SPEED,"IN_Tps");
if(feather->perf_on==1)
{
FS_register_ratio(feather->stat_handle,
feather->fs_column_id[COLUMN_TABLE_SCAN_CNT],
feather->fs_column_id[COLUMN_TABLE_CPU_TIME],
1000000, //microsecond to second
FS_STYLE_COLUMN,
FS_CALC_SPEED,
"PROC_Tps");
}
feather->fs_column_id[COLUMN_TABLE_HIT_CNT]=FS_register(feather->stat_handle, FS_STYLE_COLUMN, FS_CALC_SPEED,"hit_cnt");
value=feather->fs_column_id[COLUMN_TABLE_HIT_CNT];
FS_set_para(feather->stat_handle, ID_INVISBLE, &value, sizeof(value));
FS_register_ratio(feather->stat_handle,
feather->fs_column_id[COLUMN_TABLE_HIT_CNT],
feather->fs_column_id[COLUMN_TABLE_SCAN_CNT],
1,
FS_STYLE_COLUMN,
FS_CALC_SPEED,
"hit_rate");
feather->total_stat_id=FS_register(feather->stat_handle, FS_STYLE_LINE, FS_CALC_CURRENT,"Sum");
for(i=0;i<MAX_TABLE_NUM;i++)
{
p_table=feather->p_table_info[i];
if(p_table==NULL||p_table->table_type==TABLE_TYPE_PLUGIN
||p_table->table_type==TABLE_TYPE_GROUP
||p_table->table_type==TABLE_TYPE_COMPILE)
{
continue;
}
p_table->stat_line_id=FS_register(feather->stat_handle, FS_STYLE_LINE, FS_CALC_CURRENT,p_table->table_name);
}
FS_start(feather->stat_handle);
return;
}
void maat_stat_table(struct _Maat_table_info_t* p_table,int scan_len,struct timespec* start, struct timespec* end,int thread_num)
{
aligment_int64_array_add(p_table->scan_cnt,thread_num,1);
aligment_int64_array_add(p_table->input_bytes,thread_num,scan_len);
if(start!=NULL&&end!=NULL)
{
aligment_int64_array_add(p_table->scan_cpu_time,thread_num,(end->tv_sec-start->tv_sec)*1000000000+end->tv_nsec-start->tv_nsec);
}
return;
}
void maat_stat_output(struct _Maat_feather_t* feather)
{
long value=0;
long long total_cfg_num=0, total_input_bytes=0, total_regex_num=0,total_hit_cnt=0;
long long total_scan_cnt=0, total_cpu_time=0,total_stream_cnt=0,active_thread_num=0;
long long table_stream_num=0,table_scan_cnt=0,table_input_bytes=0,table_scan_cpu_time=0,table_hit_cnt=0;
long long outer_mid_cnt=0,inner_mid_cnt=0;
int i=0;
time_t now;
struct _Maat_table_info_t* p_table=NULL;
time(&now);
active_thread_num=aligment_int64_array_cnt(feather->thread_call_cnt, feather->scan_thread_num);
outer_mid_cnt=aligment_int64_array_sum(feather->outer_mid_cnt,feather->scan_thread_num);
inner_mid_cnt=aligment_int64_array_sum(feather->inner_mid_cnt,feather->scan_thread_num);
FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_VERSION], 0,FS_OP_SET,feather->maat_version);
FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_THRED_NUM], 0,FS_OP_SET,active_thread_num);
FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_TABLE_NUM], 0,FS_OP_SET,feather->table_cnt);
FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_OUTER_MID_NUM], 0,FS_OP_SET,outer_mid_cnt);
FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_INNER_MID_NUM], 0,FS_OP_SET,inner_mid_cnt);
value=MESA_lqueue_get_count(feather->garbage_q);
FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_GARBAGE_QSIZE], 0,FS_OP_SET,value);
for(i=0;i<MAX_TABLE_NUM;i++)
{
table_stream_num=0;
table_scan_cnt=0;
table_input_bytes=0;
table_scan_cpu_time=0;
table_hit_cnt=0;
p_table=feather->p_table_info[i];
if(p_table==NULL||p_table->table_type==TABLE_TYPE_PLUGIN
||p_table->table_type==TABLE_TYPE_GROUP
||p_table->table_type==TABLE_TYPE_COMPILE)
{
continue;
}
FS_operate(feather->stat_handle,
p_table->stat_line_id,
feather->fs_column_id[COLUMN_TABLE_RULE_NUM],
FS_OP_SET,
p_table->cfg_num);
total_cfg_num+=p_table->cfg_num;
FS_operate(feather->stat_handle,
p_table->stat_line_id,
feather->fs_column_id[COLUMN_TABLE_REGEX_NUM],
FS_OP_SET,
p_table->regex_rule_cnt);
total_regex_num+= p_table->regex_rule_cnt;
table_stream_num=aligment_int64_array_sum(p_table->stream_num,feather->scan_thread_num);
FS_operate(feather->stat_handle,
p_table->stat_line_id,
feather->fs_column_id[COLUMN_TABLE_STREAM_NUM],
FS_OP_SET,
table_stream_num);
total_stream_cnt+= table_stream_num;
table_scan_cnt=aligment_int64_array_sum(p_table->scan_cnt,feather->scan_thread_num);
FS_operate(feather->stat_handle,
p_table->stat_line_id,
feather->fs_column_id[COLUMN_TABLE_SCAN_CNT],
FS_OP_SET,
table_scan_cnt);
total_scan_cnt+=table_scan_cnt;
table_input_bytes=aligment_int64_array_sum(p_table->input_bytes,feather->scan_thread_num);
FS_operate(feather->stat_handle,
p_table->stat_line_id,
feather->fs_column_id[COLUMN_TABLE_SCAN_BYTES],
FS_OP_SET,
table_input_bytes);
total_input_bytes+=table_input_bytes;
if(feather->perf_on==1)
{
table_scan_cpu_time=aligment_int64_array_sum(p_table->scan_cpu_time,feather->scan_thread_num);
table_scan_cpu_time/=1000;
FS_operate(feather->stat_handle,
p_table->stat_line_id,
feather->fs_column_id[COLUMN_TABLE_CPU_TIME],
FS_OP_SET,
table_scan_cpu_time);
total_cpu_time+=table_scan_cpu_time;
}
table_hit_cnt=aligment_int64_array_sum(p_table->hit_cnt,feather->scan_thread_num);
FS_operate(feather->stat_handle,
p_table->stat_line_id,
feather->fs_column_id[COLUMN_TABLE_HIT_CNT],
FS_OP_SET,
table_hit_cnt);
//total hit count stat in region_compile
}
FS_operate(feather->stat_handle,
feather->total_stat_id,
feather->fs_column_id[COLUMN_TABLE_RULE_NUM],
FS_OP_SET,
total_cfg_num);
FS_operate(feather->stat_handle,
feather->total_stat_id,
feather->fs_column_id[COLUMN_TABLE_REGEX_NUM],
FS_OP_SET,
total_regex_num);
FS_operate(feather->stat_handle,
feather->total_stat_id,
feather->fs_column_id[COLUMN_TABLE_STREAM_NUM],
FS_OP_SET,
total_stream_cnt);
FS_operate(feather->stat_handle,
feather->total_stat_id,
feather->fs_column_id[COLUMN_TABLE_SCAN_CNT],
FS_OP_SET,
total_scan_cnt);
FS_operate(feather->stat_handle,
feather->total_stat_id,
feather->fs_column_id[COLUMN_TABLE_SCAN_BYTES],
FS_OP_SET,
total_input_bytes);
if(feather->perf_on==1)
{
FS_operate(feather->stat_handle,
feather->total_stat_id,
feather->fs_column_id[COLUMN_TABLE_CPU_TIME],
FS_OP_SET,
total_cpu_time);
}
total_hit_cnt=aligment_int64_array_sum(feather->hit_cnt,feather->scan_thread_num);
FS_operate(feather->stat_handle,
feather->total_stat_id,
feather->fs_column_id[COLUMN_TABLE_HIT_CNT],
FS_OP_SET,
total_hit_cnt);
feather->total_scan_bytes=total_input_bytes;
feather->total_scan_cnt=total_scan_cnt;
FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_TOTAL_SCAN_LEN], 0,FS_OP_SET,feather->total_scan_bytes);
FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_TOTAL_SCAN_CNT], 0,FS_OP_SET,feather->total_scan_cnt);
FS_passive_output(feather->stat_handle);
return;
}

View File

@@ -6,7 +6,7 @@ CCC = g++
CFLAGS = -Wall -g -fPIC
CFLAGS += $(OPTFLAGS)
LDDICTATOR = -Wl,-wrap,malloc -Wl,-wrap,calloc -Wl,-wrap,free -Wl,-wrap,realloc
LDFLAGS = -lMESA_handle_logger -lMESA_htable -lpthread -lm -lrulescan -lpcre
LDFLAGS = -lMESA_handle_logger -lMESA_htable -lpthread -lrt -lm -lrulescan -lpcre -lMESA_field_stat2
#LDFLAGS += $(LDDICTATOR)
MAILLIB = ../lib
@@ -15,8 +15,8 @@ H_DIR =-I$(G_H_DIR) -I../../inc
LIBMAAT = libmaatframe.a
LIBMAAT_SO = libmaatframe.so
OBJS=config_monitor.o Maat_rule.o Maat_api.o UniversalBoolMatch.o dynamic_array.o cJSON.o json2iris.o map_str2int.o\
interval_index.o great_index_engine.o mesa_fuzzy.o
OBJS=config_monitor.o Maat_rule.o Maat_api.o Maat_stat.o UniversalBoolMatch.o dynamic_array.o cJSON.o\
json2iris.o map_str2int.o interval_index.o great_index_engine.o mesa_fuzzy.o rbtree.o
.c.o:
$(CC) -c $(CFLAGS) -I. $(H_DIR) $<

View File

@@ -0,0 +1,52 @@
#ifndef H_ALIGMENT_INT64_H_INCLUDE
#define H_ALIGMENT_INT64_H_INCLUDE
#include <stdlib.h>
#define CPU_CACHE_ALIGMENT 64
typedef long long* mcore_long_t;
inline long long *aligment_int64_array_alloc(int size)
{
long long *ret=NULL;
ret=(long long*)calloc(CPU_CACHE_ALIGMENT,size);
return ret;
}
inline long long aligment_int64_array_sum(mcore_long_t array,int size)
{
long long sum=0;
int offset=0,i=0;
for(i=0;i<size;i++)
{
offset=(CPU_CACHE_ALIGMENT/sizeof(long long))*i;
sum+=array[offset];
}
return sum;
}
inline long long aligment_int64_array_add(mcore_long_t array,int offset,long long op_val)
{
int idx=(CPU_CACHE_ALIGMENT/sizeof(long long))*offset;
array[idx]+=op_val;
return array[idx];
}
inline long long aligment_int64_array_cnt(mcore_long_t array,int size)
{
int offset=0,i=0;
int cnt=0;
for(i=0;i<size;i++)
{
offset=(CPU_CACHE_ALIGMENT/sizeof(long long))*i;
if(array[offset]>0)
{
cnt++;
}
}
return cnt;
}
inline void aligment_int64_array_free(mcore_long_t array)
{
free(array);
}
#endif

View File

@@ -97,7 +97,7 @@ GIE_handle_t * GIE_create(const GIE_create_para_t * para)
idtable_args.hash_slot_size = HTABLE_SIZE;
idtable_args.max_elem_num = 4 * HTABLE_SIZE;
idtable_args.expire_time = 0;
idtable_args.eliminate_type = HASH_ELIMINATE_ALGO_LRU;
idtable_args.eliminate_type = HASH_ELIMINATE_ALGO_FIFO;
idtable_args.key_comp = NULL;
idtable_args.key2index = NULL;
idtable_args.data_free = idtable_free;
@@ -108,7 +108,7 @@ GIE_handle_t * GIE_create(const GIE_create_para_t * para)
indextable_args.hash_slot_size = HTABLE_SIZE;
indextable_args.max_elem_num = 4 * HTABLE_SIZE;
indextable_args.expire_time = 0;
indextable_args.eliminate_type = HASH_ELIMINATE_ALGO_LRU;
indextable_args.eliminate_type = HASH_ELIMINATE_ALGO_FIFO;
indextable_args.key_comp = NULL;
indextable_args.key2index = NULL;
indextable_args.data_free = indextable_free;

View File

@@ -1,7 +1,17 @@
#include<stdio.h>
#include<stdlib.h>
#include"interval_index.h"
/*********************************************************************
* File:
* interval_index.c
* Author:
* TangQi
* E-mail:
* tangqi@iie.ac.cn
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "interval_index.h"
#include "rbtree.h"
#include "rbtree_augmented.h"
/**
* There is a trick here. In order to hide specific
@@ -16,73 +26,113 @@
* Structure of inner segment
**/
typedef struct __IVI_shadow_seg_t{
IVI_seg_t lightseg;
TAILQ_ENTRY(__IVI_shadow_seg_t) ENTRY;
IVI_seg_t lightseg; /* interval for user, including left edge, right edge, and user's data */
struct rb_node rb; /* node of rb-tree */
OFFSET_TYPE max; /* max edge of subtree */
}IVI_shadow_seg_t;
TAILQ_HEAD(TQ, __IVI_shadow_seg_t);
/* Structure of inner InterVal Index */
typedef struct __IVI_shadow_t{
struct TQ ivi_queue;
struct rb_root root;
/* statistics */
int segs_cnt;
OFFSET_TYPE segs_length;
unsigned long long mem_occupy; //do not include user data
}IVI_shadow_t;
/**
* new is closer to head or tail ?
* Return 1 if closer to head than tail
* Else return 0
*/
int closer_to_head(IVI_shadow_seg_t * head, IVI_shadow_seg_t * tail, OFFSET_TYPE target)
IVI_seg_t * IVI_first_seg(IVI_t * handler)
{
if(head == NULL || tail == NULL)
return 1;
S_OFFSET_TYPE tmp1 = (S_OFFSET_TYPE)(target - head->lightseg.left);
S_OFFSET_TYPE tmp2 = (S_OFFSET_TYPE)(target - tail->lightseg.left);
S_OFFSET_TYPE distance_to_head = tmp1 > 0 ? tmp1 : -tmp1;
S_OFFSET_TYPE distance_to_tail = tmp2 > 0 ? tmp2 : -tmp2;
return (distance_to_tail - distance_to_head > 0);
assert(handler != NULL);
IVI_shadow_t * shadow_ivi = (IVI_shadow_t *)handler;
struct rb_node *first_node = rb_first(&(shadow_ivi->root));
if(first_node == NULL)
return NULL;
return (IVI_seg_t *)(rb_entry(first_node, IVI_shadow_seg_t, rb));
}
IVI_seg_t * IVI_last_seg(IVI_t * handler)
{
assert(handler != NULL);
IVI_shadow_t * shadow_ivi = (IVI_shadow_t *)handler;
struct rb_node *last_node = rb_last(&(shadow_ivi->root));
if(last_node == NULL)
return NULL;
return (IVI_seg_t *)(rb_entry(last_node, IVI_shadow_seg_t, rb));
}
IVI_seg_t * IVI_prev_seg(IVI_seg_t * seg)
{
assert(seg != NULL);
IVI_shadow_seg_t * shadow_seg = (IVI_shadow_seg_t *)seg;
struct rb_node * prev_node = rb_prev(&(shadow_seg->rb));
if(prev_node == NULL)
return NULL;
return (IVI_seg_t *)(rb_entry(prev_node, IVI_shadow_seg_t, rb));
}
IVI_seg_t * IVI_next_seg(IVI_seg_t * seg)
{
assert(seg != NULL);
IVI_shadow_seg_t * shadow_seg = (IVI_shadow_seg_t *)seg;
struct rb_node * next_node = rb_next(&(shadow_seg->rb));
if(next_node == NULL)
return NULL;
return (IVI_seg_t *)(rb_entry(next_node, IVI_shadow_seg_t, rb));
}
IVI_seg_t * IVI_prev_continuous_seg(IVI_seg_t * seg)
{
if(NULL == seg)
assert(seg != NULL);
IVI_shadow_seg_t * shadow_seg = (IVI_shadow_seg_t *)seg;
struct rb_node * prev_node = rb_prev(&(shadow_seg->rb));
if(prev_node == NULL)
{
return NULL;
}
IVI_shadow_seg_t * _seg = (IVI_shadow_seg_t *)seg;
IVI_shadow_seg_t * prev = TAILQ_PREV(_seg, TQ, ENTRY);
if(NULL == prev)
IVI_seg_t * prev_seg = (IVI_seg_t *)(rb_entry(prev_node, IVI_shadow_seg_t, rb));
if(!continuous(prev_seg->right, seg->left))
{
return NULL;
}
if(continuous((prev->lightseg).right, seg->left))
return (IVI_seg_t *)prev;
return NULL;
return prev_seg;
}
IVI_seg_t * IVI_next_continuous_seg(IVI_seg_t * seg)
{
if(NULL == seg)
assert(seg != NULL);
IVI_shadow_seg_t * shadow_seg = (IVI_shadow_seg_t *)seg;
struct rb_node * next_node = rb_next(&(shadow_seg->rb));
if(next_node == NULL)
{
return NULL;
}
IVI_shadow_seg_t * _seg = (IVI_shadow_seg_t *)seg;
IVI_shadow_seg_t * next = TAILQ_NEXT(_seg, ENTRY);
if(NULL == next)
IVI_seg_t * next_seg = (IVI_seg_t *)(rb_entry(next_node, IVI_shadow_seg_t, rb));
if(!continuous(seg->right, next_seg->left))
{
return NULL;
}
if(continuous(seg->right, (next->lightseg).left))
return (IVI_seg_t *)next;
return NULL;
return next_seg;
}
static inline int __is_overlapped(OFFSET_TYPE left1, OFFSET_TYPE right1, OFFSET_TYPE left2, OFFSET_TYPE right2)
{
if(!after(left1, right2) && !after(left2, right1))
return 1;
return 0;
}
@@ -152,13 +202,42 @@ Relation_t IVI_relative_position(IVI_seg_t * seg1, IVI_seg_t * seg2)
IVI_t * IVI_create(void)
{
IVI_shadow_t * shadow_ivi = (IVI_shadow_t *)malloc(sizeof(IVI_shadow_t));
TAILQ_INIT(&(shadow_ivi->ivi_queue));
shadow_ivi->root = RB_ROOT; //init rb tree's root
shadow_ivi->segs_cnt = 0;
shadow_ivi->segs_length = 0;
shadow_ivi->mem_occupy = sizeof(IVI_shadow_t);
return (IVI_t *)shadow_ivi;
}
static void __free_rb_tree(struct rb_node * root, IVI_callback_t cb, void * usr_para)
{
if(root == NULL)
{
return;
}
if(root->rb_left != NULL)
{
__free_rb_tree(root->rb_left, cb, usr_para);
}
if(root->rb_right != NULL)
{
__free_rb_tree(root->rb_right, cb, usr_para);
}
/* free user data */
IVI_shadow_seg_t * shadow_seg = rb_entry(root, IVI_shadow_seg_t, rb);
if(cb != NULL)
{
cb((IVI_seg_t *)shadow_seg, usr_para);
}
/* free seg */
free(shadow_seg);
shadow_seg = NULL;
return;
}
/**
* Name:
* IVI_destroy
@@ -177,26 +256,11 @@ void IVI_destroy(IVI_t * handler, IVI_callback_t cb, void * usr_para)
{
return;
}
IVI_shadow_t * shadow_ivi = (IVI_shadow_t *)handler;
IVI_shadow_seg_t * tmpseg = TAILQ_FIRST(&(shadow_ivi->ivi_queue));
IVI_shadow_seg_t * tmp;
/* Free each seg in IVI */
while(tmpseg != NULL)
{
tmp = TAILQ_NEXT(tmpseg, ENTRY);
/* Free *data in seg */
if(NULL != cb)
{
cb(&(tmpseg->lightseg), usr_para);
}
free(tmpseg);
tmpseg = tmp;
}
/* Free IVI */
__free_rb_tree(shadow_ivi->root.rb_node, cb, usr_para);
free(shadow_ivi);
handler = NULL;
return;
}
@@ -224,6 +288,7 @@ IVI_seg_t * IVI_seg_malloc(OFFSET_TYPE left, OFFSET_TYPE right, void * data)
shadow_seg->lightseg.left = left;
shadow_seg->lightseg.right= right;
shadow_seg->lightseg.data = data;
shadow_seg->max = 0;
return (IVI_seg_t *)shadow_seg;
}
@@ -244,6 +309,8 @@ IVI_seg_t * IVI_seg_malloc(OFFSET_TYPE left, OFFSET_TYPE right, void * data)
**/
void IVI_seg_free(IVI_seg_t * seg, IVI_callback_t cb, void * usr_para)
{
assert(seg != NULL);
/* Free user data first */
if(cb != NULL)
{
@@ -257,6 +324,70 @@ void IVI_seg_free(IVI_seg_t * seg, IVI_callback_t cb, void * usr_para)
}
static inline OFFSET_TYPE __interval_tree_get_subtree_max(IVI_shadow_seg_t * node)
{
OFFSET_TYPE max = node->lightseg.right, subtree_max;
if(node->rb.rb_left)
{
subtree_max = (rb_entry(node->rb.rb_left, IVI_shadow_seg_t, rb))->max;
if(before(max, subtree_max))
max = subtree_max;
}
if(node->rb.rb_right)
{
subtree_max = (rb_entry(node->rb.rb_right, IVI_shadow_seg_t, rb))->max;
if(before(max, subtree_max))
max = subtree_max;
}
return max;
}
static void __interval_tree_augment_propagate(struct rb_node * rb, struct rb_node * stop)
{
while(rb != stop)
{
IVI_shadow_seg_t * node = rb_entry(rb, IVI_shadow_seg_t, rb);
OFFSET_TYPE subtree_max = __interval_tree_get_subtree_max(node);
if(node->max == subtree_max)
{
break;
}
node->max = subtree_max;
rb = rb_parent(&node->rb);
}
return;
}
static void __interval_tree_augment_copy(struct rb_node * rb_old, struct rb_node * rb_new)
{
IVI_shadow_seg_t * old = rb_entry(rb_old, IVI_shadow_seg_t, rb);
IVI_shadow_seg_t * new = rb_entry(rb_new, IVI_shadow_seg_t, rb);
new->max = old->max;
return;
}
static void __interval_tree_augment_rotate(struct rb_node * rb_old, struct rb_node * rb_new)
{
IVI_shadow_seg_t * old = rb_entry(rb_old, IVI_shadow_seg_t, rb);
IVI_shadow_seg_t * new = rb_entry(rb_new, IVI_shadow_seg_t, rb);
new->max = old->max;
old->max = __interval_tree_get_subtree_max(old);
return;
}
static const struct rb_augment_callbacks __interval_tree_augment_callbacks = {
__interval_tree_augment_propagate,
__interval_tree_augment_copy,
__interval_tree_augment_rotate
};
/**
* Name:
* IVI_insert
@@ -273,74 +404,49 @@ void IVI_seg_free(IVI_seg_t * seg, IVI_callback_t cb, void * usr_para)
**/
int IVI_insert(IVI_t * handler, IVI_seg_t * seg)
{
IVI_shadow_t * shadow_ivi;
IVI_shadow_seg_t *head, *tail, *new_seg, *tmp_seg;
if(NULL == handler || NULL == seg)
{
return -1;
}
shadow_ivi = (IVI_shadow_t *)handler;
new_seg = (IVI_shadow_seg_t *)seg;
head = TAILQ_FIRST(&(shadow_ivi->ivi_queue));
tail = TAILQ_LAST(&(shadow_ivi->ivi_queue), TQ);
if(closer_to_head(head, tail, seg->left))
IVI_shadow_t * shadow_ivi = (IVI_shadow_t *)handler;
struct rb_root * root = &(shadow_ivi->root);
OFFSET_TYPE left = seg->left, right = seg->right;
struct rb_node **link = &root->rb_node, *rb_parent = NULL;
IVI_shadow_seg_t * parent = NULL;
IVI_shadow_seg_t * new_seg = (IVI_shadow_seg_t *)seg;
while(*link)
{
TAILQ_FOREACH(tmp_seg, &(shadow_ivi->ivi_queue), ENTRY)
rb_parent = *link;
parent = rb_entry(rb_parent, IVI_shadow_seg_t, rb);
/* is overlapped */
if(__is_overlapped(left, right, parent->lightseg.left, parent->lightseg.right))
{
//overlapped, return
return -1;
}
if(before(parent->max, right))
{
/* Find the first seg whose left is bigger than given seg's right, we will insert new seg before it */
if(after(tmp_seg->lightseg.left, new_seg->lightseg.right))
{
TAILQ_INSERT_BEFORE(tmp_seg, new_seg, ENTRY);
shadow_ivi->segs_cnt ++;
shadow_ivi->segs_length += (seg->right - seg->left + 1);
return 0;
}
else if(before(tmp_seg->lightseg.right, new_seg->lightseg.left))
{
continue;
}
else /* Overlap */
{
return -1;
}
parent->max = right;
}
/* If have searched to the end of list, we will inset it to the tail */
TAILQ_INSERT_TAIL(&(shadow_ivi->ivi_queue), new_seg, ENTRY);
shadow_ivi->segs_cnt ++;
shadow_ivi->segs_length += (seg->right - seg->left + 1);
}
else
{
TAILQ_FOREACH_REVERSE(tmp_seg, &(shadow_ivi->ivi_queue), TQ, ENTRY)
if(before(left, parent->lightseg.left))
{
/* Find the first seg whose right is smaller than given seg's left, we will insert new seg after it */
if(before(tmp_seg->lightseg.right, new_seg->lightseg.left))
{
TAILQ_INSERT_AFTER(&(shadow_ivi->ivi_queue), tmp_seg, new_seg, ENTRY);
shadow_ivi->segs_cnt ++;
shadow_ivi->segs_length += (seg->right - seg->left + 1);
return 0;
}
else if(after(tmp_seg->lightseg.left, new_seg->lightseg.right))
{
continue;
}
else /* Overlap */
{
return -1;
}
link = &parent->rb.rb_left;
}
else
{
link = &parent->rb.rb_right;
}
/* If have searched to the head of list, we will inset it to the head */
TAILQ_INSERT_HEAD(&(shadow_ivi->ivi_queue), new_seg, ENTRY);
shadow_ivi->segs_cnt ++;
shadow_ivi->segs_length += (seg->right - seg->left + 1);
}
new_seg->max = right;
rb_link_node(&new_seg->rb, rb_parent, link);
rb_insert_augmented(&new_seg->rb, root, &__interval_tree_augment_callbacks);
/* updata statistics */
shadow_ivi->segs_cnt ++;
shadow_ivi->segs_length += seg->right - seg->left + 1;
shadow_ivi->mem_occupy += sizeof(IVI_shadow_seg_t);
return 0;
}
@@ -367,16 +473,56 @@ int IVI_remove(IVI_t * handler, IVI_seg_t * seg)
}
IVI_shadow_t * shadow_ivi = (IVI_shadow_t *)handler;
IVI_shadow_seg_t * shadow_seg = (IVI_shadow_seg_t *)seg;
struct rb_root * root = &(shadow_ivi->root);
IVI_shadow_seg_t * new_seg = (IVI_shadow_seg_t *)seg;
rb_erase_augmented(&new_seg->rb, root, &__interval_tree_augment_callbacks);
TAILQ_REMOVE(&(shadow_ivi->ivi_queue), shadow_seg, ENTRY);
/* updata statistics */
shadow_ivi->segs_cnt --;
shadow_ivi->segs_length -= (seg->right - seg->left + 1);
shadow_ivi->segs_length -= seg->right - seg->left + 1;
shadow_ivi->mem_occupy -= sizeof(IVI_shadow_seg_t);
return 0;
}
static struct rb_node * __min_interval_search_from(struct rb_node * node, OFFSET_TYPE left, OFFSET_TYPE right)
{
if(node == NULL)
{
return NULL;
}
IVI_shadow_seg_t * seg = rb_entry(node, IVI_shadow_seg_t, rb);
IVI_shadow_seg_t * left_seg = rb_entry(node->rb_left, IVI_shadow_seg_t, rb);
if(node->rb_left != NULL && !before(left_seg->max, left))
{
struct rb_node * ret = __min_interval_search_from(node->rb_left, left, right);
if(ret != NULL)
{
return ret;
}
else if(__is_overlapped(left, right, seg->lightseg.left, seg->lightseg.right))
{
return node;
}
else
{
return NULL;
}
}
else if(__is_overlapped(left, right, seg->lightseg.left, seg->lightseg.right))
{
return node;
}
else
{
return __min_interval_search_from(node->rb_right, left, right);
}
}
/**
* Name:
* IVI_query
@@ -397,115 +543,34 @@ int IVI_remove(IVI_t * handler, IVI_seg_t * seg)
**/
int IVI_query(IVI_t * handler, OFFSET_TYPE left, OFFSET_TYPE right, IVI_seg_t *** segs)
{
IVI_shadow_t * shadow_ivi;
IVI_shadow_seg_t *head, *tail, *tmp, *left_tmp, *right_tmp;
int interval_cnt = 0, i;
if(NULL == handler || after(left, right))
{
//augments error
return -1;
}
shadow_ivi = (IVI_shadow_t *)handler;
head = TAILQ_FIRST(&(shadow_ivi->ivi_queue));
tail = TAILQ_LAST(&(shadow_ivi->ivi_queue), TQ);
int interval_cnt = 0, max_cnt = 8;
IVI_shadow_t * shadow_ivi = (IVI_shadow_t *)handler;
struct rb_node * root = shadow_ivi->root.rb_node;
struct rb_node * min_overlap = __min_interval_search_from(root, left, right);
struct rb_node * tmp_node = min_overlap;
/* Traverse from head or tail? We need to decide */
if(closer_to_head(head, tail, left))
*segs = (IVI_seg_t **)malloc(max_cnt * sizeof(IVI_seg_t *));
while (tmp_node != NULL)
{
tmp = head;
while(tmp != NULL)
IVI_seg_t * tmp_seg = (IVI_seg_t *)(rb_entry(tmp_node, IVI_shadow_seg_t, rb));
if(!__is_overlapped(tmp_seg->left, tmp_seg->right, left, right))
{
if(after(left, tmp->lightseg.right))
{
tmp = TAILQ_NEXT(tmp, ENTRY);
}
else
{
/* Get the seg which left is in or before*/
left_tmp = tmp;
break;
}
break;
}
if(tmp == NULL)
if(interval_cnt > max_cnt)
{
*segs = NULL;
return 0;
}
/* Get the num of overlapped segs */
while(tmp != NULL)
{
if(!before(right, tmp->lightseg.left))
{
tmp = TAILQ_NEXT(tmp, ENTRY);
interval_cnt ++;
}
else
{
break;
}
}
tmp = left_tmp;
if(interval_cnt == 0)
{
*segs = NULL;
return 0;
}
*segs = (IVI_seg_t **)malloc(interval_cnt * sizeof(IVI_seg_t *));
for(i = 0; i < interval_cnt; i++)
{
(*segs)[i] = (IVI_seg_t *)tmp;
tmp = TAILQ_NEXT(tmp, ENTRY);
}
}
else
{
tmp = tail;
while(tmp != NULL)
{
if(before(right, tmp->lightseg.left))
{
tmp = TAILQ_PREV(tmp, TQ, ENTRY);
}
else
{
right_tmp = tmp;
break;
}
}
if(tmp == NULL)
{
*segs = NULL;
return 0;
}
/* Get the num of overlapped segs */
while(tmp != NULL)
{
if(!after(left, tmp->lightseg.right))
{
tmp = TAILQ_PREV(tmp, TQ, ENTRY);
interval_cnt ++;
}
else
{
break;
}
}
tmp = right_tmp;
if(interval_cnt == 0)
{
*segs = NULL;
return 0;
}
*segs = (IVI_seg_t **)malloc(interval_cnt * sizeof(IVI_seg_t *));
for(i = interval_cnt - 1; i >= 0; i--)
{
(*segs)[i] = (IVI_seg_t *)tmp;
tmp = TAILQ_PREV(tmp, TQ, ENTRY);
max_cnt *= 2;
*segs = (IVI_seg_t **)realloc(*segs, max_cnt * sizeof(IVI_seg_t *));
}
(*segs)[interval_cnt] = tmp_seg;
interval_cnt ++;
tmp_node = rb_next(tmp_node);
}
return interval_cnt;
}
@@ -531,129 +596,41 @@ int IVI_query(IVI_t * handler, OFFSET_TYPE left, OFFSET_TYPE right, IVI_seg_t **
**/
int IVI_query_continuous(IVI_t * handler, OFFSET_TYPE left, OFFSET_TYPE right, IVI_seg_t *** segs)
{
IVI_shadow_t * shadow_ivi;
IVI_shadow_seg_t *head, *tail, *tmp, *left_tmp, *right_tmp;
int interval_cnt = 0, i;
if(NULL == handler || after(left, right))
{
//augments error
return -1;
}
shadow_ivi = (IVI_shadow_t *)handler;
head = TAILQ_FIRST(&(shadow_ivi->ivi_queue));
tail = TAILQ_LAST(&(shadow_ivi->ivi_queue), TQ);
int interval_cnt = 0, max_cnt = 8;
IVI_shadow_t * shadow_ivi = (IVI_shadow_t *)handler;
struct rb_node * root = shadow_ivi->root.rb_node;
struct rb_node * min_overlap = __min_interval_search_from(root, left, right);
struct rb_node * tmp_node = min_overlap;
/* Traverse from head or tail? We need to decide */
if(closer_to_head(head, tail, left))
*segs = (IVI_seg_t **)malloc(max_cnt * sizeof(IVI_seg_t *));
while (tmp_node != NULL)
{
tmp = head;
while(tmp != NULL)
IVI_seg_t * tmp_seg = (IVI_seg_t *)(rb_entry(tmp_node, IVI_shadow_seg_t, rb));
if(!__is_overlapped(tmp_seg->left, tmp_seg->right, left, right))
{
if(after(left, tmp->lightseg.right))
{
tmp = TAILQ_NEXT(tmp, ENTRY);
}
else
{
/* Get the seg which left is in or before*/
left_tmp = tmp;
break;
}
break;
}
if(tmp == NULL)
if(interval_cnt > max_cnt)
{
*segs = NULL;
return 0;
max_cnt += 8;
*segs = (IVI_seg_t **)realloc(*segs, max_cnt * sizeof(IVI_seg_t *));
}
/* Get the num of overlapped segs */
while(tmp != NULL)
(*segs)[interval_cnt] = tmp_seg;
interval_cnt ++;
tmp_node = rb_next(tmp_node);
IVI_seg_t * prev_tmp_seg = tmp_seg;
tmp_seg = (IVI_seg_t *)(rb_entry(tmp_node, IVI_shadow_seg_t, rb));
if(!continuous(prev_tmp_seg->right, tmp_seg->left))
{
if(!before(right, tmp->lightseg.left))
{
tmp = TAILQ_NEXT(tmp, ENTRY);
interval_cnt ++;
}
else
{
break;
}
IVI_shadow_seg_t * prev = TAILQ_PREV(tmp, TQ, ENTRY);
if(tmp != NULL && !continuous(prev->lightseg.right, tmp->lightseg.left))
{
break;
}
}
tmp = left_tmp;
if(interval_cnt == 0)
{
*segs = NULL;
return 0;
}
*segs = (IVI_seg_t **)malloc(interval_cnt * sizeof(IVI_seg_t *));
for(i = 0; i < interval_cnt; i++)
{
(*segs)[i] = (IVI_seg_t *)tmp;
tmp = TAILQ_NEXT(tmp, ENTRY);
break;
}
}
else
{
tmp = tail;
while(tmp != NULL)
{
if(before(right, tmp->lightseg.left))
{
tmp = TAILQ_PREV(tmp, TQ, ENTRY);
}
else
{
right_tmp = tmp;
break;
}
}
if(tmp == NULL)
{
*segs = NULL;
return 0;
}
/* Get the num of overlapped segs */
while(tmp != NULL)
{
if(!after(left, tmp->lightseg.right))
{
tmp = TAILQ_PREV(tmp, TQ, ENTRY);
interval_cnt ++;
}
else
{
break;
}
IVI_shadow_seg_t * next = TAILQ_NEXT(tmp, ENTRY);
if(tmp != NULL && !continuous(tmp->lightseg.right, next->lightseg.left))
{
break;
}
}
tmp = right_tmp;
if(interval_cnt == 0)
{
*segs = NULL;
return 0;
}
*segs = (IVI_seg_t **)malloc(interval_cnt * sizeof(IVI_seg_t *));
for(i = interval_cnt - 1; i >= 0; i--)
{
(*segs)[i] = (IVI_seg_t *)tmp;
tmp = TAILQ_PREV(tmp, TQ, ENTRY);
}
}
return interval_cnt;
}
@@ -698,6 +675,41 @@ OFFSET_TYPE IVI_seg_length(IVI_t * handler)
}
/**
* Name:
* IVI_mem_occupy
* Description:
* Get the memory occupy of given interval index handler
* Params:
* handler: The handler of InterVal Index created by IVI_create.
* Return:
* Return the memory occupy of given interval index handler
**/
unsigned long long IVI_mem_occupy(IVI_t * handler)
{
if(handler == NULL)
return 0;
IVI_shadow_t * shadow_ivi = (IVI_shadow_t *)handler;
return shadow_ivi->mem_occupy;
}
static void __inorder_traverse(struct rb_node * root, IVI_callback_t cb, void * usr_para)
{
if(root == NULL)
{
return;
}
/* save first in case of root is freed in callback */
struct rb_node * left_node = root->rb_left;
struct rb_node * right_node = root->rb_right;
__inorder_traverse(left_node, cb, usr_para);
IVI_seg_t * seg = (IVI_seg_t *)(rb_entry(root, IVI_shadow_seg_t, rb));
cb(seg, usr_para);
__inorder_traverse(right_node, cb, usr_para);
return;
}
/**
* Name:
@@ -720,17 +732,6 @@ void IVI_traverse(IVI_t * handler, IVI_callback_t cb, void * usr_para)
}
IVI_shadow_t * shadow_ivi = (IVI_shadow_t *)handler;
IVI_shadow_seg_t * tmp_seg = TAILQ_FIRST(&(shadow_ivi->ivi_queue));
IVI_shadow_seg_t * tmp;
/* Traverse the IVI */
while(tmp_seg != NULL)
{
/*
* The place we can't use TAILQ_FOREACH because we
* do not no what will callback funciton does.
* */
tmp = TAILQ_NEXT(tmp_seg, ENTRY);
cb((IVI_seg_t *)tmp_seg, usr_para);
tmp_seg = tmp;
}
__inorder_traverse(shadow_ivi->root.rb_node, cb, usr_para);
return;
}

View File

@@ -6,7 +6,7 @@
* (3) The interval supports rollback.
*
* author: zhengchao@iie.ac.cn tangqi@iie.ac.cn
* last modify time: 2015-08-29
* last modify time: 2015-12-04
*************************************************************************/
#ifndef _INTERVAL_INDEX_H_
@@ -16,12 +16,11 @@
extern "C"{
#endif
#include "queue.h"
#define SIZE_8
#ifdef SIZE_8
typedef unsigned long long OFFSET_TYPE;
typedef unsigned long long OFFSET_TYPE;
typedef signed long long S_OFFSET_TYPE;
#else
typedef unsigned int OFFSET_TYPE;
@@ -61,6 +60,10 @@ static inline int continuous(OFFSET_TYPE prev, OFFSET_TYPE next)
}
IVI_seg_t * IVI_first_seg(IVI_t * handler);
IVI_seg_t * IVI_last_seg(IVI_t * handler);
IVI_seg_t * IVI_prev_seg(IVI_seg_t * seg);
IVI_seg_t * IVI_next_seg(IVI_seg_t * seg);
IVI_seg_t * IVI_prev_continuous_seg(IVI_seg_t * seg);
IVI_seg_t * IVI_next_continuous_seg(IVI_seg_t * seg);
@@ -71,22 +74,22 @@ IVI_seg_t * IVI_next_continuous_seg(IVI_seg_t * seg);
typedef enum __Relation_t{
LEFT_NO_OVERLAP = 1, // |___A___|
// |___B___|
LEFT_OVERLAP, // |___A___|
// |___B___|
CONTAINED, // |___A___|
// |_____B_____|
CONTAIN, // |_____A_____|
// |___B___|
RIGHT_OVERLAP, // |___A___|
// |___B___|
RIGHT_NO_OVERLAP, // |___A___|
// |___B___|
ERROR
}Relation_t;
@@ -273,6 +276,18 @@ int IVI_seg_cnt(IVI_t * handler);
OFFSET_TYPE IVI_seg_length(IVI_t * handler);
/**
* Name:
* IVI_mem_occupy
* Description:
* Get the memory occupy of given interval index handler
* Params:
* handler: The handler of InterVal Index created by IVI_create.
* Return:
* Return the memory occupy of given interval index handler
**/
unsigned long long IVI_mem_occupy(IVI_t * handler);
/**
* Name:

View File

@@ -89,7 +89,7 @@ int set_iris_descriptor(const char* json_file,cJSON *json,struct iris_descriptio
hargs.thread_safe=1;
hargs.hash_slot_size = 1024;
hargs.max_elem_num = 0;
hargs.eliminate_type = HASH_ELIMINATE_ALGO_LRU;
hargs.eliminate_type = HASH_ELIMINATE_ALGO_FIFO;
hargs.expire_time = 0;
hargs.key_comp = NULL;
hargs.key2index = NULL;
@@ -110,8 +110,11 @@ int set_iris_descriptor(const char* json_file,cJSON *json,struct iris_descriptio
map_register(iris_cfg->str2int_map, "ip",TABLE_TYPE_IP);
map_register(iris_cfg->str2int_map, "string",TABLE_TYPE_EXPR);
map_register(iris_cfg->str2int_map, "expr",TABLE_TYPE_EXPR);
map_register(iris_cfg->str2int_map, "expr_plus",TABLE_TYPE_EXPR_PLUS);
map_register(iris_cfg->str2int_map, "intval",TABLE_TYPE_INTVAL);
map_register(iris_cfg->str2int_map, "digest",TABLE_TYPE_DIGEST);
map_register(iris_cfg->str2int_map, "ipv4",4);
map_register(iris_cfg->str2int_map, "ipv6",6);
@@ -364,7 +367,7 @@ int write_ip_rule(cJSON *region_json,struct iris_description_t *p_iris,const cha
return direct_write_rule(region_json, p_iris->str2int_map,json_cmd, cmd_cnt,path,logger);
}
int write_expr_rule(cJSON *region_json,struct iris_description_t *p_iris,const char* path,void * logger)
int write_expr_rule(cJSON *region_json,struct iris_description_t *p_iris,const char* path,enum MAAT_TABLE_TYPE table_type,void * logger)
{
struct traslate_command_t json_cmd[MAX_COLUMN_NUM];
int cmd_cnt=0;
@@ -378,6 +381,13 @@ int write_expr_rule(cJSON *region_json,struct iris_description_t *p_iris,const c
json_cmd[cmd_cnt].json_type=cJSON_Number;
cmd_cnt++;
if(table_type==TABLE_TYPE_EXPR_PLUS)
{
json_cmd[cmd_cnt].json_string="district";
json_cmd[cmd_cnt].json_type=cJSON_String;
cmd_cnt++;
}
json_cmd[cmd_cnt].json_string="keywords";
json_cmd[cmd_cnt].json_type=cJSON_String;
cmd_cnt++;
@@ -598,7 +608,8 @@ int write_region_rule(cJSON* region_json,int compile_id,int group_id,iris_descri
switch(table_type)
{
case TABLE_TYPE_EXPR:
ret=write_expr_rule(table_content, p_iris, table_info->table_path, logger);
case TABLE_TYPE_EXPR_PLUS:
ret=write_expr_rule(table_content, p_iris, table_info->table_path,table_type, logger);
break;
case TABLE_TYPE_IP:
ret=write_ip_rule(table_content, p_iris, table_info->table_path, logger);

View File

@@ -1,21 +1,35 @@
#include <MESA/MESA_htable.h>
void map_tmp_free(void* ptr)
{
free(ptr);
}
long read_map_val(void *data, const uchar *key, uint size, void *user_arg)
{
if(data!=NULL)
{
*(int*)user_arg=*(int*)data;
return 1;
}
else
{
return 0;
}
}
MESA_htable_handle map_create(void)
{
MESA_htable_handle string2int_map;
MESA_htable_create_args_t hargs;
memset(&hargs,0,sizeof(hargs));
hargs.thread_safe=1;
hargs.hash_slot_size = 1024*1024;
hargs.thread_safe=8;
hargs.hash_slot_size = 4*1024;
hargs.max_elem_num = 0;
hargs.eliminate_type = HASH_ELIMINATE_ALGO_LRU;
hargs.eliminate_type = HASH_ELIMINATE_ALGO_FIFO;
hargs.expire_time = 0;
hargs.key_comp = NULL;
hargs.key2index = NULL;
hargs.recursive = 0;
hargs.recursive = 1;
hargs.data_free = map_tmp_free;
hargs.data_expire_with_condition = NULL;
string2int_map=MESA_htable_create(&hargs, sizeof(hargs));
@@ -31,20 +45,28 @@ int map_register(MESA_htable_handle handle,const char* string,int value)
{
unsigned int size=strlen(string);
unsigned char *key=(unsigned char *)string;
int *data=(int*)malloc(sizeof(int));
int ret=0;
int * data= (int*)malloc(sizeof(int));
*data=value;
ret=MESA_htable_add(handle,key,size,data);
if(ret<0)
{
free(data);
}
return ret;
}
int map_str2int(MESA_htable_handle handle,const char* string,int* value)
{
int *data=NULL;
long cb_ret=0;
unsigned int size=strlen(string);
data=(int*)MESA_htable_search(handle,(unsigned char*)string,size);
if(data!=NULL)
data=(int*)MESA_htable_search_cb(handle,(unsigned char*)string,size,
read_map_val,value,&cb_ret);
// data=(int*)MESA_htable_search(handle,(unsigned char*)string,size);
if(cb_ret>0)
{
*value=*data;
return 1;
}
else

File diff suppressed because it is too large Load Diff

548
src/entry/rbtree.c Normal file
View File

@@ -0,0 +1,548 @@
/*
Red Black Trees
(C) 1999 Andrea Arcangeli <andrea@suse.de>
(C) 2002 David Woodhouse <dwmw2@infradead.org>
(C) 2012 Michel Lespinasse <walken@google.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
linux/lib/rbtree.c
*/
#include "rbtree.h"
#include "rbtree_augmented.h"
/*
* red-black trees properties: http://en.wikipedia.org/wiki/Rbtree
*
* 1) A node is either red or black
* 2) The root is black
* 3) All leaves (NULL) are black
* 4) Both children of every red node are black
* 5) Every simple path from root to leaves contains the same number
* of black nodes.
*
* 4 and 5 give the O(log n) guarantee, since 4 implies you cannot have two
* consecutive red nodes in a path and every red node is therefore followed by
* a black. So if B is the number of black nodes on every simple path (as per
* 5), then the longest possible path due to 4 is 2B.
*
* We shall indicate color with case, where black nodes are uppercase and red
* nodes will be lowercase. Unknown color nodes shall be drawn as red within
* parentheses and have some accompanying text comment.
*/
static inline void rb_set_black(struct rb_node *rb)
{
rb->__rb_parent_color |= RB_BLACK;
}
static inline struct rb_node *rb_red_parent(struct rb_node *red)
{
return (struct rb_node *)red->__rb_parent_color;
}
/*
* Helper function for rotations:
* - old's parent and color get assigned to new
* - old gets assigned new as a parent and 'color' as a color.
*/
static inline void
__rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
struct rb_root *root, int color)
{
struct rb_node *parent = rb_parent(old);
new->__rb_parent_color = old->__rb_parent_color;
rb_set_parent_color(old, new, color);
__rb_change_child(old, new, parent, root);
}
static __always_inline void
__rb_insert(struct rb_node *node, struct rb_root *root,
void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
{
struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
while (1) {
/*
* Loop invariant: node is red
*
* If there is a black parent, we are done.
* Otherwise, take some corrective action as we don't
* want a red root or two consecutive red nodes.
*/
if (!parent) {
rb_set_parent_color(node, NULL, RB_BLACK);
break;
} else if (rb_is_black(parent))
break;
gparent = rb_red_parent(parent);
tmp = gparent->rb_right;
if (parent != tmp) { /* parent == gparent->rb_left */
if (tmp && rb_is_red(tmp)) {
/*
* Case 1 - color flips
*
* G g
* / \ / \
* p u --> P U
* / /
* n n
*
* However, since g's parent might be red, and
* 4) does not allow this, we need to recurse
* at g.
*/
rb_set_parent_color(tmp, gparent, RB_BLACK);
rb_set_parent_color(parent, gparent, RB_BLACK);
node = gparent;
parent = rb_parent(node);
rb_set_parent_color(node, parent, RB_RED);
continue;
}
tmp = parent->rb_right;
if (node == tmp) {
/*
* Case 2 - left rotate at parent
*
* G G
* / \ / \
* p U --> n U
* \ /
* n p
*
* This still leaves us in violation of 4), the
* continuation into Case 3 will fix that.
*/
parent->rb_right = tmp = node->rb_left;
node->rb_left = parent;
if (tmp)
rb_set_parent_color(tmp, parent,
RB_BLACK);
rb_set_parent_color(parent, node, RB_RED);
augment_rotate(parent, node);
parent = node;
tmp = node->rb_right;
}
/*
* Case 3 - right rotate at gparent
*
* G P
* / \ / \
* p U --> n g
* / \
* n U
*/
gparent->rb_left = tmp; /* == parent->rb_right */
parent->rb_right = gparent;
if (tmp)
rb_set_parent_color(tmp, gparent, RB_BLACK);
__rb_rotate_set_parents(gparent, parent, root, RB_RED);
augment_rotate(gparent, parent);
break;
} else {
tmp = gparent->rb_left;
if (tmp && rb_is_red(tmp)) {
/* Case 1 - color flips */
rb_set_parent_color(tmp, gparent, RB_BLACK);
rb_set_parent_color(parent, gparent, RB_BLACK);
node = gparent;
parent = rb_parent(node);
rb_set_parent_color(node, parent, RB_RED);
continue;
}
tmp = parent->rb_left;
if (node == tmp) {
/* Case 2 - right rotate at parent */
parent->rb_left = tmp = node->rb_right;
node->rb_right = parent;
if (tmp)
rb_set_parent_color(tmp, parent,
RB_BLACK);
rb_set_parent_color(parent, node, RB_RED);
augment_rotate(parent, node);
parent = node;
tmp = node->rb_left;
}
/* Case 3 - left rotate at gparent */
gparent->rb_right = tmp; /* == parent->rb_left */
parent->rb_left = gparent;
if (tmp)
rb_set_parent_color(tmp, gparent, RB_BLACK);
__rb_rotate_set_parents(gparent, parent, root, RB_RED);
augment_rotate(gparent, parent);
break;
}
}
}
/*
* Inline version for rb_erase() use - we want to be able to inline
* and eliminate the dummy_rotate callback there
*/
static __always_inline void
____rb_erase_color(struct rb_node *parent, struct rb_root *root,
void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
{
struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
while (1) {
/*
* Loop invariants:
* - node is black (or NULL on first iteration)
* - node is not the root (parent is not NULL)
* - All leaf paths going through parent and node have a
* black node count that is 1 lower than other leaf paths.
*/
sibling = parent->rb_right;
if (node != sibling) { /* node == parent->rb_left */
if (rb_is_red(sibling)) {
/*
* Case 1 - left rotate at parent
*
* P S
* / \ / \
* N s --> p Sr
* / \ / \
* Sl Sr N Sl
*/
parent->rb_right = tmp1 = sibling->rb_left;
sibling->rb_left = parent;
rb_set_parent_color(tmp1, parent, RB_BLACK);
__rb_rotate_set_parents(parent, sibling, root,
RB_RED);
augment_rotate(parent, sibling);
sibling = tmp1;
}
tmp1 = sibling->rb_right;
if (!tmp1 || rb_is_black(tmp1)) {
tmp2 = sibling->rb_left;
if (!tmp2 || rb_is_black(tmp2)) {
/*
* Case 2 - sibling color flip
* (p could be either color here)
*
* (p) (p)
* / \ / \
* N S --> N s
* / \ / \
* Sl Sr Sl Sr
*
* This leaves us violating 5) which
* can be fixed by flipping p to black
* if it was red, or by recursing at p.
* p is red when coming from Case 1.
*/
rb_set_parent_color(sibling, parent,
RB_RED);
if (rb_is_red(parent))
rb_set_black(parent);
else {
node = parent;
parent = rb_parent(node);
if (parent)
continue;
}
break;
}
/*
* Case 3 - right rotate at sibling
* (p could be either color here)
*
* (p) (p)
* / \ / \
* N S --> N Sl
* / \ \
* sl Sr s
* \
* Sr
*/
sibling->rb_left = tmp1 = tmp2->rb_right;
tmp2->rb_right = sibling;
parent->rb_right = tmp2;
if (tmp1)
rb_set_parent_color(tmp1, sibling,
RB_BLACK);
augment_rotate(sibling, tmp2);
tmp1 = sibling;
sibling = tmp2;
}
/*
* Case 4 - left rotate at parent + color flips
* (p and sl could be either color here.
* After rotation, p becomes black, s acquires
* p's color, and sl keeps its color)
*
* (p) (s)
* / \ / \
* N S --> P Sr
* / \ / \
* (sl) sr N (sl)
*/
parent->rb_right = tmp2 = sibling->rb_left;
sibling->rb_left = parent;
rb_set_parent_color(tmp1, sibling, RB_BLACK);
if (tmp2)
rb_set_parent(tmp2, parent);
__rb_rotate_set_parents(parent, sibling, root,
RB_BLACK);
augment_rotate(parent, sibling);
break;
} else {
sibling = parent->rb_left;
if (rb_is_red(sibling)) {
/* Case 1 - right rotate at parent */
parent->rb_left = tmp1 = sibling->rb_right;
sibling->rb_right = parent;
rb_set_parent_color(tmp1, parent, RB_BLACK);
__rb_rotate_set_parents(parent, sibling, root,
RB_RED);
augment_rotate(parent, sibling);
sibling = tmp1;
}
tmp1 = sibling->rb_left;
if (!tmp1 || rb_is_black(tmp1)) {
tmp2 = sibling->rb_right;
if (!tmp2 || rb_is_black(tmp2)) {
/* Case 2 - sibling color flip */
rb_set_parent_color(sibling, parent,
RB_RED);
if (rb_is_red(parent))
rb_set_black(parent);
else {
node = parent;
parent = rb_parent(node);
if (parent)
continue;
}
break;
}
/* Case 3 - right rotate at sibling */
sibling->rb_right = tmp1 = tmp2->rb_left;
tmp2->rb_left = sibling;
parent->rb_left = tmp2;
if (tmp1)
rb_set_parent_color(tmp1, sibling,
RB_BLACK);
augment_rotate(sibling, tmp2);
tmp1 = sibling;
sibling = tmp2;
}
/* Case 4 - left rotate at parent + color flips */
parent->rb_left = tmp2 = sibling->rb_right;
sibling->rb_right = parent;
rb_set_parent_color(tmp1, sibling, RB_BLACK);
if (tmp2)
rb_set_parent(tmp2, parent);
__rb_rotate_set_parents(parent, sibling, root,
RB_BLACK);
augment_rotate(parent, sibling);
break;
}
}
}
/* Non-inline version for rb_erase_augmented() use */
void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
{
____rb_erase_color(parent, root, augment_rotate);
}
/*
* Non-augmented rbtree manipulation functions.
*
* We use dummy augmented callbacks here, and have the compiler optimize them
* out of the rb_insert_color() and rb_erase() function definitions.
*/
static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {}
static inline void dummy_copy(struct rb_node *old, struct rb_node *new) {}
static inline void dummy_rotate(struct rb_node *old, struct rb_node *new) {}
static const struct rb_augment_callbacks dummy_callbacks = {
dummy_propagate, dummy_copy, dummy_rotate
};
void rb_insert_color(struct rb_node *node, struct rb_root *root)
{
__rb_insert(node, root, dummy_rotate);
}
void rb_erase(struct rb_node *node, struct rb_root *root)
{
struct rb_node *rebalance;
rebalance = __rb_erase_augmented(node, root, &dummy_callbacks);
if (rebalance)
____rb_erase_color(rebalance, root, dummy_rotate);
}
/*
* Augmented rbtree manipulation functions.
*
* This instantiates the same __always_inline functions as in the non-augmented
* case, but this time with user-defined callbacks.
*/
void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
{
__rb_insert(node, root, augment_rotate);
}
/*
* This function returns the first node (in sort order) of the tree.
*/
struct rb_node *rb_first(const struct rb_root *root)
{
struct rb_node *n;
n = root->rb_node;
if (!n)
return NULL;
while (n->rb_left)
n = n->rb_left;
return n;
}
struct rb_node *rb_last(const struct rb_root *root)
{
struct rb_node *n;
n = root->rb_node;
if (!n)
return NULL;
while (n->rb_right)
n = n->rb_right;
return n;
}
struct rb_node *rb_next(const struct rb_node *node)
{
struct rb_node *parent;
if (RB_EMPTY_NODE(node))
return NULL;
/*
* If we have a right-hand child, go down and then left as far
* as we can.
*/
if (node->rb_right) {
node = node->rb_right;
while (node->rb_left)
node=node->rb_left;
return (struct rb_node *)node;
}
/*
* No right-hand children. Everything down and left is smaller than us,
* so any 'next' node must be in the general direction of our parent.
* Go up the tree; any time the ancestor is a right-hand child of its
* parent, keep going up. First time it's a left-hand child of its
* parent, said parent is our 'next' node.
*/
while ((parent = rb_parent(node)) && node == parent->rb_right)
node = parent;
return parent;
}
struct rb_node *rb_prev(const struct rb_node *node)
{
struct rb_node *parent;
if (RB_EMPTY_NODE(node))
return NULL;
/*
* If we have a left-hand child, go down and then right as far
* as we can.
*/
if (node->rb_left) {
node = node->rb_left;
while (node->rb_right)
node=node->rb_right;
return (struct rb_node *)node;
}
/*
* No left-hand children. Go up till we find an ancestor which
* is a right-hand child of its parent.
*/
while ((parent = rb_parent(node)) && node == parent->rb_left)
node = parent;
return parent;
}
void rb_replace_node(struct rb_node *victim, struct rb_node *new,
struct rb_root *root)
{
struct rb_node *parent = rb_parent(victim);
/* Set the surrounding nodes to point to the replacement */
__rb_change_child(victim, new, parent, root);
if (victim->rb_left)
rb_set_parent(victim->rb_left, new);
if (victim->rb_right)
rb_set_parent(victim->rb_right, new);
/* Copy the pointers/colour from the victim to the replacement */
*new = *victim;
}
static struct rb_node *rb_left_deepest_node(const struct rb_node *node)
{
for (;;) {
if (node->rb_left)
node = node->rb_left;
else if (node->rb_right)
node = node->rb_right;
else
return (struct rb_node *)node;
}
}
struct rb_node *rb_next_postorder(const struct rb_node *node)
{
const struct rb_node *parent;
if (!node)
return NULL;
parent = rb_parent(node);
/* If we're sitting on node, we've already seen our children */
if (parent && node == parent->rb_left && parent->rb_right) {
/* If we are the parent's left node, go to the parent's right
* node then all the way down to the left */
return rb_left_deepest_node(parent->rb_right);
} else
/* Otherwise we are the parent's right node, and the parent
* should be next */
return (struct rb_node *)parent;
}
struct rb_node *rb_first_postorder(const struct rb_root *root)
{
if (!root->rb_node)
return NULL;
return rb_left_deepest_node(root->rb_node);
}

118
src/entry/rbtree.h Normal file
View File

@@ -0,0 +1,118 @@
/*
Red Black Trees
(C) 1999 Andrea Arcangeli <andrea@suse.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
linux/include/linux/rbtree.h
To use rbtrees you'll have to implement your own insert and search cores.
This will avoid us to use callbacks and to drop drammatically performances.
I know it's not the cleaner way, but in C (not in C++) to get
performances and genericity...
See Documentation/rbtree.txt for documentation and samples.
*/
#ifndef _LINUX_RBTREE_H
#define _LINUX_RBTREE_H
#include <stdio.h>
struct rb_node {
unsigned long __rb_parent_color;
struct rb_node *rb_right;
struct rb_node *rb_left;
} __attribute__((aligned(sizeof(long))));
/* The alignment might seem pointless, but allegedly CRIS needs it */
struct rb_root {
struct rb_node *rb_node;
};
#define rb_parent(r) ((struct rb_node *)((r)->__rb_parent_color & ~3))
#define RB_ROOT (struct rb_root) { NULL, }
#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
#ifndef container_of
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type, member) );})
#endif
#define rb_entry(ptr, type, member) container_of(ptr, type, member)
#define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL)
/* 'empty' nodes are nodes that are known not to be inserted in an rbtree */
#define RB_EMPTY_NODE(node) \
((node)->__rb_parent_color == (unsigned long)(node))
#define RB_CLEAR_NODE(node) \
((node)->__rb_parent_color = (unsigned long)(node))
extern void rb_insert_color(struct rb_node *, struct rb_root *);
extern void rb_erase(struct rb_node *, struct rb_root *);
/* Find logical next and previous nodes in a tree */
extern struct rb_node *rb_next(const struct rb_node *);
extern struct rb_node *rb_prev(const struct rb_node *);
extern struct rb_node *rb_first(const struct rb_root *);
extern struct rb_node *rb_last(const struct rb_root *);
/* Postorder iteration - always visit the parent after its children */
extern struct rb_node *rb_first_postorder(const struct rb_root *);
extern struct rb_node *rb_next_postorder(const struct rb_node *);
/* Fast replacement of a single node without remove/rebalance/add/rebalance */
extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
struct rb_root *root);
static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
struct rb_node ** rb_link)
{
node->__rb_parent_color = (unsigned long)parent;
node->rb_left = node->rb_right = NULL;
*rb_link = node;
}
#define rb_entry_safe(ptr, type, member) \
({ typeof(ptr) ____ptr = (ptr); \
____ptr ? rb_entry(____ptr, type, member) : NULL; \
})
/**
* rbtree_postorder_for_each_entry_safe - iterate over rb_root in post order of
* given type safe against removal of rb_node entry
*
* @pos: the 'type *' to use as a loop cursor.
* @n: another 'type *' to use as temporary storage
* @root: 'rb_root *' of the rbtree.
* @field: the name of the rb_node field within 'type'.
*/
#define rbtree_postorder_for_each_entry_safe(pos, n, root, field) \
for (pos = rb_entry_safe(rb_first_postorder(root), typeof(*pos), field); \
pos && ({ n = rb_entry_safe(rb_next_postorder(&pos->field), \
typeof(*pos), field); 1; }); \
pos = n)
#endif /* _LINUX_RBTREE_H */

View File

@@ -0,0 +1,241 @@
/*
Red Black Trees
(C) 1999 Andrea Arcangeli <andrea@suse.de>
(C) 2002 David Woodhouse <dwmw2@infradead.org>
(C) 2012 Michel Lespinasse <walken@google.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
linux/include/linux/rbtree_augmented.h
*/
#ifndef _LINUX_RBTREE_AUGMENTED_H
#define _LINUX_RBTREE_AUGMENTED_H
#include "rbtree.h"
/*
* Please note - only struct rb_augment_callbacks and the prototypes for
* rb_insert_augmented() and rb_erase_augmented() are intended to be public.
* The rest are implementation details you are not expected to depend on.
*
* See Documentation/rbtree.txt for documentation and samples.
*/
struct rb_augment_callbacks {
void (*propagate)(struct rb_node *node, struct rb_node *stop);
void (*copy)(struct rb_node *old, struct rb_node *new);
void (*rotate)(struct rb_node *old, struct rb_node *new);
};
extern void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
/*
* Fixup the rbtree and update the augmented information when rebalancing.
*
* On insertion, the user must update the augmented information on the path
* leading to the inserted node, then call rb_link_node() as usual and
* rb_augment_inserted() instead of the usual rb_insert_color() call.
* If rb_augment_inserted() rebalances the rbtree, it will callback into
* a user provided function to update the augmented information on the
* affected subtrees.
*/
static inline void
rb_insert_augmented(struct rb_node *node, struct rb_root *root,
const struct rb_augment_callbacks *augment)
{
__rb_insert_augmented(node, root, augment->rotate);
}
#define RB_DECLARE_CALLBACKS(rbstatic, rbname, rbstruct, rbfield, \
rbtype, rbaugmented, rbcompute) \
static inline void \
rbname ## _propagate(struct rb_node *rb, struct rb_node *stop) \
{ \
while (rb != stop) { \
rbstruct *node = rb_entry(rb, rbstruct, rbfield); \
rbtype augmented = rbcompute(node); \
if (node->rbaugmented == augmented) \
break; \
node->rbaugmented = augmented; \
rb = rb_parent(&node->rbfield); \
} \
} \
static inline void \
rbname ## _copy(struct rb_node *rb_old, struct rb_node *rb_new) \
{ \
rbstruct *old = rb_entry(rb_old, rbstruct, rbfield); \
rbstruct *new = rb_entry(rb_new, rbstruct, rbfield); \
new->rbaugmented = old->rbaugmented; \
} \
static void \
rbname ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new) \
{ \
rbstruct *old = rb_entry(rb_old, rbstruct, rbfield); \
rbstruct *new = rb_entry(rb_new, rbstruct, rbfield); \
new->rbaugmented = old->rbaugmented; \
old->rbaugmented = rbcompute(old); \
} \
rbstatic const struct rb_augment_callbacks rbname = { \
rbname ## _propagate, rbname ## _copy, rbname ## _rotate \
};
#define RB_RED 0
#define RB_BLACK 1
#define __rb_parent(pc) ((struct rb_node *)(pc & ~3))
#define __rb_color(pc) ((pc) & 1)
#define __rb_is_black(pc) __rb_color(pc)
#define __rb_is_red(pc) (!__rb_color(pc))
#define rb_color(rb) __rb_color((rb)->__rb_parent_color)
#define rb_is_red(rb) __rb_is_red((rb)->__rb_parent_color)
#define rb_is_black(rb) __rb_is_black((rb)->__rb_parent_color)
static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
{
rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
}
static inline void rb_set_parent_color(struct rb_node *rb,
struct rb_node *p, int color)
{
rb->__rb_parent_color = (unsigned long)p | color;
}
static inline void
__rb_change_child(struct rb_node *old, struct rb_node *new,
struct rb_node *parent, struct rb_root *root)
{
if (parent) {
if (parent->rb_left == old)
parent->rb_left = new;
else
parent->rb_right = new;
} else
root->rb_node = new;
}
extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
static __always_inline struct rb_node *
__rb_erase_augmented(struct rb_node *node, struct rb_root *root,
const struct rb_augment_callbacks *augment)
{
struct rb_node *child = node->rb_right, *tmp = node->rb_left;
struct rb_node *parent, *rebalance;
unsigned long pc;
if (!tmp) {
/*
* Case 1: node to erase has no more than 1 child (easy!)
*
* Note that if there is one child it must be red due to 5)
* and node must be black due to 4). We adjust colors locally
* so as to bypass __rb_erase_color() later on.
*/
pc = node->__rb_parent_color;
parent = __rb_parent(pc);
__rb_change_child(node, child, parent, root);
if (child) {
child->__rb_parent_color = pc;
rebalance = NULL;
} else
rebalance = __rb_is_black(pc) ? parent : NULL;
tmp = parent;
} else if (!child) {
/* Still case 1, but this time the child is node->rb_left */
tmp->__rb_parent_color = pc = node->__rb_parent_color;
parent = __rb_parent(pc);
__rb_change_child(node, tmp, parent, root);
rebalance = NULL;
tmp = parent;
} else {
struct rb_node *successor = child, *child2;
tmp = child->rb_left;
if (!tmp) {
/*
* Case 2: node's successor is its right child
*
* (n) (s)
* / \ / \
* (x) (s) -> (x) (c)
* \
* (c)
*/
parent = successor;
child2 = successor->rb_right;
augment->copy(node, successor);
} else {
/*
* Case 3: node's successor is leftmost under
* node's right child subtree
*
* (n) (s)
* / \ / \
* (x) (y) -> (x) (y)
* / /
* (p) (p)
* / /
* (s) (c)
* \
* (c)
*/
do {
parent = successor;
successor = tmp;
tmp = tmp->rb_left;
} while (tmp);
parent->rb_left = child2 = successor->rb_right;
successor->rb_right = child;
rb_set_parent(child, successor);
augment->copy(node, successor);
augment->propagate(parent, successor);
}
successor->rb_left = tmp = node->rb_left;
rb_set_parent(tmp, successor);
pc = node->__rb_parent_color;
tmp = __rb_parent(pc);
__rb_change_child(node, successor, tmp, root);
if (child2) {
successor->__rb_parent_color = pc;
rb_set_parent_color(child2, parent, RB_BLACK);
rebalance = NULL;
} else {
unsigned long pc2 = successor->__rb_parent_color;
successor->__rb_parent_color = pc;
rebalance = __rb_is_black(pc2) ? parent : NULL;
}
tmp = successor;
}
augment->propagate(tmp, NULL);
return rebalance;
}
static __always_inline void
rb_erase_augmented(struct rb_node *node, struct rb_root *root,
const struct rb_augment_callbacks *augment)
{
struct rb_node *rebalance = __rb_erase_augmented(node, root, augment);
if (rebalance)
__rb_erase_color(rebalance, root, augment->rotate);
}
#endif /* _LINUX_RBTREE_AUGMENTED_H */

106
src/entry/sfh_internal.h Normal file
View File

@@ -0,0 +1,106 @@
#include<zt_hash.h>
#include<interval_index.h>
#include<mesa_fuzzy.h>
#ifndef __SFH_INTERNAL_H_INCLUDE_
#define __SFH_INTERNAL_H_INCLUDE_
#define ROLLING_WINDOW 7
#define BLOCKSIZE_MIN 3
#define HASH_PRIME 0x01000193
#define HASH_INIT 0x28021967
#define CALCULATE 0
#define MODIFY 1
#define EXPECT_SIGNATURE_LEN 64
#define MEMORY_OCCUPY 3
#ifndef MAX
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef container_of
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#endif
#define DEBUG (0)
int hash_length;
//int count = 0;
struct roll_state_t
{
unsigned char window[ROLLING_WINDOW];
unsigned char pad[1];
unsigned int h1, h2, h3;
unsigned int n;
};
typedef struct
{
char mbuf[ROLLING_WINDOW-1];
char pad[8-ROLLING_WINDOW+1];
int slice_num;
unsigned int msize;
struct zt_state_t ps; //partial strong hash value
struct zt_state_t s_state;//strong hash state
unsigned long long left_offset;
unsigned long long right_offset;
struct roll_state_t r_state;
unsigned int * r_array; //array to store rolling hash value
unsigned int r_cnt;
unsigned int r_size;
struct zt_state_t * s_array; //array to store strong(Tillichi-Zemor) hash value
unsigned int s_cnt; //always point to the next available position
unsigned int s_size;
}sfh_seg_t;
typedef struct
{
unsigned long long orilen;
IVI_t * ivi; //ÿһ<C3BF><D2BB>handle<6C><65><EFBFBD><EFBFBD><E6B1A3>һ<EFBFBD><D2BB>IVIָ<49>һ<EBA3AC><D2BB>IVI<56><49><EFBFBD><EFBFBD><E6B1A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>Ƭ
unsigned long long effective_length;
unsigned long long blocksize;
unsigned long long fuzzy_node_memory;
unsigned long long IVI_memory;
unsigned long long length_increase;
int s_state_cnt;
unsigned int sim_tuned_rs_cnt;//rolling state count after a tune simulation
int do_tune;
}fuzzy_handle_inner_t;
typedef struct
{
char * data; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>char<61><72><EFBFBD><EFBFBD>
unsigned int size;
unsigned int offset; //<2F><><EFBFBD><EFBFBD><E9B3A4>
unsigned long long first_ZTH_offset;
unsigned long long last_ZTH_offset;
char last_char;
}final_result;
typedef struct
{
unsigned long long first_ZTH_offset;
unsigned long long last_ZTH_offset;
unsigned long long hash_length;
}final_length;
sfh_seg_t* create_sfh_seg(fuzzy_handle_inner_t * _handle);
int destroy_sfh_seg(sfh_seg_t*p);
unsigned long long get_blocksize(unsigned long long orilen);
int sfh_merge_seg(fuzzy_handle_inner_t * _handle,sfh_seg_t * seg, sfh_seg_t * next_seg, unsigned long long blocksize);
int sfh_update_seg(fuzzy_handle_inner_t * _handle,sfh_seg_t * p, const char * data, unsigned long data_size, unsigned long long blocksize);
unsigned int segment_overlap(fuzzy_handle_inner_t * handle, unsigned int size, unsigned long long offset, const char * data);
void sfh_tune_seg(IVI_seg_t * seg, void * user_para);
void sfh_output_state(IVI_seg_t * seg, void * user_para);
void fuzzy_hash_length(IVI_seg_t * seg, void * user_para);
unsigned long long fuzzy_status(fuzzy_handle_t * handle, int type);
#endif

234
src/entry/zt_hash.h Normal file

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,71 @@
#ifndef H_SCREEN_STAT_H_INCLUDE
#define H_SCREEN_STAT_H_INCLUDE
#include <stdio.h>
#ifndef __cplusplus
#error("This file should be compiled with C++ compiler")
#endif
enum field_dsp_style_t
{
FS_STYLE_FIELD=0,
FS_STYLE_COLUMN,
FS_STYLE_LINE,
FS_STYLE_STATUS
};
enum field_calc_algo
{
FS_CALC_CURRENT=0,
FS_CALC_SPEED
};
enum field_op
{
FS_OP_ADD=1,
FS_OP_SET
};
typedef void* screen_stat_handle_t;
enum FS_option
{
OUTPUT_DEVICE, //VALUE is a const char*, indicate a file path string, SIZE = strlen(string+'\0')+1.DEFAULT:output to stdout.
PRINT_MODE, //VALUE is an interger,1:Rewrite ,2: Append. SIZE=4,DEFALUT:REWRITE.
STAT_CYCLE, //VALUE is an interger idicate interval seconds of every output, SIZE=4 ,DEFUALT:2 seconds.
PRINT_TRIGGER, //VALUE is an interger,1:Do print,0: Don't print.SIZE=4.DEFAULT:1.
CREATE_THREAD,//VALUE is an interger,1: Create a print thread,0:not create,output by call passive_output function,
//and the STAT_CYCLE is meaningless.SIZE=4,DEFAULT:0.
ID_INVISBLE,//value is field_id/status_id/column_id, not output this string, SIZE=4,DEFAULT: shutdown NO one.
};
//Always success.
screen_stat_handle_t FS_create_handle(void);
int FS_set_para(screen_stat_handle_t handle, enum FS_option type,const void* value,int size);
void FS_start(screen_stat_handle_t handle);
void FS_stop(screen_stat_handle_t* handle);
//return field_id/line_id/column_id greater than zero if success,return an interger less than zero if failed.
int FS_register(screen_stat_handle_t handle,enum field_dsp_style_t style,enum field_calc_algo calc_type,const char* name);
//numerator_id and denominator_id must be column/field/status style.
//scaling: negative value: zoom in; positive value: zoom out;
int FS_register_ratio(screen_stat_handle_t handle,int numerator_id,int denominator_id,int scaling,enum field_dsp_style_t style,enum field_calc_algo calc_type,const char* name);
//id: when id's type is FIELD , column_id is ignore.
int FS_operate(screen_stat_handle_t handle,int id,int column_id,enum field_op op,long long value);
void FS_passive_output(screen_stat_handle_t handle);
screen_stat_handle_t init_screen_stat(FILE* output_fp,int stat_cycle,int screen_print_trigger);
//return field_id >=0 when success, return -1 when failed.
int stat_field_register(screen_stat_handle_t handle,const char* field_name);
//return >=0 when success, return -1 when failed.
#define FS_OP_TYPE_ADD (FS_OP_ADD)
#define FS_OP_TYPE_SET (FS_OP_SET)
int stat_field_operation(screen_stat_handle_t handle,int field_id,int operation,long long value);
#endif

View File

@@ -1,85 +0,0 @@
#ifndef _MESA_FUZZY_
#define _MESA_FUZZY_
/*
* Copyright (C) MESA 2015
*
* These functions allow a programmer to compute the fuzzy hashes
* (also called the context-triggered piecewise hashes) of
* buffer[s] of text.
*
* See also:
* ssdeep, and
* Identifying almost identical files using context triggered piecewise hashing
*
*/
#include <stdint.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
#define TOTAL_LENGTH 0
#define EFFECTIVE_LENGTH 1
#define HASH_LENGTH 2
// typedef fuzzy_handle_t void*;
typedef struct
{
}fuzzy_handle_t;
/**
* create a fuzzy hash handle and return it.
* @return [handle]
*/
fuzzy_handle_t * fuzzy_create_handle(unsigned long long origin_len);
/**
* destroy context by a fuzzy hash handle.
* @param handle [handle]
*/
void fuzzy_destroy_handle(fuzzy_handle_t * handle);
/**
* Feed the function your data.
* Call this function several times, if you have several parts of data to feed.
* @param handle [handle]
* @param data [data that you want to fuzzy_hash]
* @param size [data size]
* @param offset [offset]
* @return [return effective data length in current feed]
*/
unsigned int fuzzy_feed(fuzzy_handle_t * handle, const char* data, unsigned int size, unsigned long long offset);
/**
* Obtain the fuzzy hash values.
* @param handle [handle]
* @param result [fuzzy hash result]
* Fuzzy hash result with offsets(in the square brackets, with colon splitted).
* eg. abc[1:100]def[200:300]
* @param size [@result size]
* @return [return zero on success, non-zero on error]
*/
int fuzzy_digest(fuzzy_handle_t * handle, char* result, unsigned int size);
/**
* Obtain certain length of fuzzy hash status.
* @param handle [handle]
* @param type [length type]
* TOTAL_LENGTH:Total length of data you have fed.
* Overlapped data will NOT count for 2 times.
* EFFECTIVE_LENGTH:Length of data that involved in the calculation of hash.
* HASH_LENGTH:Hash result length.
* @return [length value]
*/
unsigned long long fuzzy_status(fuzzy_handle_t * handle, int type);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -23,10 +23,21 @@ extern "C"
{
#endif
/* <20><><EFBFBD>岻ͬ<E5B2BB><CDAC>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
enum ScanType
{
SCANTYPE_DEFAULT = 0,
SCANTYPE_DETAIL_RESULT = 1,
SCANTYPE_REGEX_GROUP = 2
};
#define MAX_REGEX_GROUP_NUM 5 /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>֧<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD> */
#define MAX_EXPR_ITEM_NUM (1U<<3) /* ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>MAX_EXPR_ITEM_NUM<55><4D><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
#define MAX_MATCH_POS_NUM 1024 /* ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD>õĸ<C3B5><C4B8><EFBFBD> */
#define MATCH_POS_NUM_INC 64 /* ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD>õĸ<C3B5><C4B8><EFBFBD><EFBFBD><EFBFBD>ʼֵ<CABC><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ */
/* <20><><EFBFBD>岻ͬ<E5B2BB>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
/* <20><><EFBFBD>岻ͬ<E5B2BB>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
const unsigned int RULETYPE_STR = 0; /* <20>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƹ<EFBFBD><C6B9><EFBFBD> */
const unsigned int RULETYPE_REG = 1; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD> */
const unsigned int RULETYPE_INT = 2; /* <20><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
@@ -195,6 +206,17 @@ extern "C"
*/
void * rulescan_initialize(unsigned int max_thread_num);
/*
<09><><EFBFBD>ܣ<EFBFBD>
<09><><EFBFBD><EFBFBD>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>rulescan_update֮ǰ<D6AE>ɶ<EFBFBD><C9B6>ε<EFBFBD><CEB5>ã<EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
instance[in]: ɨ<><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD>
scan_type_flag[in]: ɨ<><C9A8><EFBFBD><EFBFBD><EFBFBD>͵ı<CDB5>־<EFBFBD><D6BE>Ϣ<EFBFBD><CFA2>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD>õ<EFBFBD><C3B5><EFBFBD>Ϣ<EFBFBD><CFA2>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD>õ<EFBFBD><C3B5><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD><C7B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>2<EFBFBD><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
<09><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>
1<><31><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD>ã<EFBFBD>-1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܡ<EFBFBD>
*/
int rulescan_set_param(void * instance, int scan_type_flag);
/*
<09><><EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD>̬ע<CCAC><D7A2>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɨ<EFBFBD><C9A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󡣶<EFBFBD><F3A1A3B6><EFBFBD>ͬһ<CDAC><D2BB>instance<63><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬʱ<CDAC>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̡߳<DFB3>
<09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>