#include #include #include #include #include #include #include #include #include #include #include #include #include #include "http_header_v1.hpp" BaseFS::~BaseFS() { FS_stop(&fsstat_handle); } BaseFS::BaseFS(const char * header_name, const char *logdir) { char fsfile[256], commd[256]; int value; if(0 != access(logdir, 0)) { snprintf(commd, 256, "mkdir -p %s", logdir); system(commd); } snprintf(fsfile, 256, "%s/%s.txt", logdir, header_name); fsstat_handle = FS_create_handle(); FS_set_para(fsstat_handle, OUTPUT_DEVICE, fsfile, strlen(fsfile)+1); value = 1; FS_set_para(fsstat_handle, PRINT_MODE, &(value), sizeof(value)); value = 1; FS_set_para(fsstat_handle, STAT_CYCLE, &(value), sizeof(value)); //value = 1; //FS_set_para(fsstat_handle, CREATE_THREAD, &value, sizeof(value)); value = 1; FS_set_para(fsstat_handle, FLUSH_BY_DATE, &value, sizeof(value)); char appname[64]; FS_set_para(fsstat_handle, APP_NAME, appname, strlen(appname)+1); char dst_ip[16] = "127.0.0.1"; int dst_port = 80; FS_set_para(fsstat_handle, STATS_SERVER_IP, dst_ip, strlen(dst_ip)+1); FS_set_para(fsstat_handle, STATS_SERVER_PORT, &dst_port, sizeof(dst_port)); } void BaseFS::start(void) { FS_start(fsstat_handle); } void BaseFS::passive_output(void) { FS_passive_output(fsstat_handle); } ContentTable::~ContentTable() { FS_stop(&fsstat_handle); } ContentTable::ContentTable(const char * header_name, const char *logdir):BaseFS(header_name, logdir) { char total_num[64], no_head_num[64], head_num[64]; total_status_id = 0; no_header_id = 0; num_column_id = 0; ratio_column_id = 0; header_total = 0; no_header_num = 0; snprintf(total_num, 64, "%s_total", header_name); snprintf(no_head_num, 64, "no_%s", header_name); snprintf(head_num, 64, "%s_num", header_name); if(0 != pthread_mutex_init(&mutex_lock, NULL)) printf("init mutex error!!!!!!!!\n"); total_status_id = FS_register(fsstat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, total_num); no_header_id = FS_register(fsstat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, no_head_num); num_column_id = FS_register(fsstat_handle, FS_STYLE_COLUMN, FS_CALC_CURRENT, head_num); ratio_column_id = FS_register(fsstat_handle, FS_STYLE_COLUMN, FS_CALC_CURRENT, "RATIO/milli"); } int ContentTable::operate(enum field_op op, string line_name, long long value) { map::iterator iter; int line_id; int ret = 0; http_header_t hht; string::size_type idx; iter = dynamic_line_id.find(line_name); if(iter == dynamic_line_id.end()) { pthread_mutex_lock(&mutex_lock); if(iter == dynamic_line_id.end()) //防止多个线程竞争锁导致的dynamic_line_id不是最新多加一层判断 { line_id = FS_register(fsstat_handle, FS_STYLE_LINE, FS_CALC_CURRENT, line_name.c_str()); hht.header_id = line_id; hht.header_num = 1; dynamic_line_id[line_name] = hht; } else { iter = dynamic_line_id.find(line_name); if(iter == dynamic_line_id.end()) { line_id = FS_register(fsstat_handle, FS_STYLE_LINE, FS_CALC_CURRENT, line_name.c_str()); hht.header_id = line_id; hht.header_num = 1; dynamic_line_id[line_name] = hht; } else { line_id = iter->second.header_id; __sync_add_and_fetch(&(iter->second.header_num), 1); } } pthread_mutex_unlock(&mutex_lock); } else { line_id = iter->second.header_id; __sync_add_and_fetch(&(iter->second.header_num), 1); } __sync_add_and_fetch(&header_total, 1); return ret; } void ContentTable::operate_ratio() { map::iterator iter; http_header_t hht; long long ratio = 0; pthread_mutex_lock(&mutex_lock); FS_operate(fsstat_handle, total_status_id, 0, FS_OP_SET, header_total); FS_operate(fsstat_handle, no_header_id, 0, FS_OP_SET, no_header_num); iter = dynamic_line_id.begin(); while(iter != dynamic_line_id.end()) { hht = iter->second; ratio = (hht.header_num*1000)/header_total; FS_operate(fsstat_handle, hht.header_id, num_column_id, FS_OP_SET, hht.header_num); FS_operate(fsstat_handle, hht.header_id, ratio_column_id, FS_OP_SET, ratio); iter ++; } FS_passive_output(fsstat_handle); pthread_mutex_unlock(&mutex_lock); } long long ContentTable::set_nocontent_num() { return __sync_add_and_fetch(&no_header_num, 1); } long long ContentTable::get_head_total() { return header_total; } ContentHistogram::~ContentHistogram() { FS_stop(&fsstat_handle); } ContentHistogram::ContentHistogram(const char * header_name, const char *logdir):BaseFS(header_name, logdir) { if(0 != pthread_mutex_init(&mutex_lock, NULL)) printf("init mutex error!!!!!!!!\n"); const char* histogram_format="0.25,0.5,0.75,0.9,0.99000"; FS_set_para(fsstat_handle, HISTOGRAM_GLOBAL_BINS, histogram_format, strlen(histogram_format)+10); histogram_id = FS_register_histogram(fsstat_handle, FS_CALC_CURRENT, "CONTENT_LENGHT" ,1,9999999999,3); } int ContentHistogram::content_lenght_operate(long long value) { int ret = 0; ret = FS_operate(fsstat_handle, histogram_id, 0, FS_OP_SET, value); //FS_passive_output(fsstat_handle); return ret; }