diff options
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | cmake/Version.cmake | 2 | ||||
| -rw-r--r-- | inc/field_stat2.h | 1 | ||||
| -rw-r--r-- | src/MESA_field_stat.cpp | 7 | ||||
| -rw-r--r-- | src/fs2prometheus.cpp | 43 | ||||
| -rw-r--r-- | test/fs2_test.cpp | 275 |
6 files changed, 188 insertions, 142 deletions
@@ -10,7 +10,7 @@ build/ .idea core.* version.txt -cmake-build-debug +cmake-build-*/ GPATH GRTAGS GTAGS diff --git a/cmake/Version.cmake b/cmake/Version.cmake index 9b05d0b..a47944c 100644 --- a/cmake/Version.cmake +++ b/cmake/Version.cmake @@ -3,7 +3,7 @@ set(__SOURCE_AUTORESIVISION ${CMAKE_SOURCE_DIR}/autorevision.sh) set(__AUTORESIVISION ${CMAKE_BINARY_DIR}/autorevision.sh) -set(__VERSION_CACHE ${CMAKE_SOURCE_DIR}/version.txt) +set(__VERSION_CACHE ${CMAKE_BINARY_DIR}/version.txt) set(__VERSION_CONFIG ${CMAKE_BINARY_DIR}/version.cmake) file(COPY ${__SOURCE_AUTORESIVISION} DESTINATION ${CMAKE_BINARY_DIR} diff --git a/inc/field_stat2.h b/inc/field_stat2.h index f8bdc35..1d4e79f 100644 --- a/inc/field_stat2.h +++ b/inc/field_stat2.h @@ -114,6 +114,7 @@ int FS_library_set_prometheus_port(unsigned short port); //return 1 on success, 0 on failed. int FS_library_set_prometheus_url_path(const char *url_path); int FS_library_init(void); +void FS_library_destroy(void); #endif diff --git a/src/MESA_field_stat.cpp b/src/MESA_field_stat.cpp index fd5034c..c576547 100644 --- a/src/MESA_field_stat.cpp +++ b/src/MESA_field_stat.cpp @@ -603,6 +603,13 @@ void FS_stop(screen_stat_handle_t* handle) } free(_handle->histogram_bins); _handle->histogram_bins=NULL; + + if(_handle->statsd_switch > 0) + { + close(_handle->statsd_socket); + } + + free(_handle); *handle=NULL; diff --git a/src/fs2prometheus.cpp b/src/fs2prometheus.cpp index 4782e23..210a8d8 100644 --- a/src/fs2prometheus.cpp +++ b/src/fs2prometheus.cpp @@ -12,8 +12,10 @@ struct FS_library_runtime { + pthread_t tid; int fs2_handle_cnt; int using_fs2_handle_cnt; + unsigned short create_thread; unsigned short port; char *url_path; struct mg_mgr mgr; @@ -23,7 +25,7 @@ struct FS_library_runtime }; struct FS_library_runtime g_FS2_LIBRARY_handle={ - 0, 0, 9273, (char *)"/metrics", {}, NULL, PTHREAD_MUTEX_INITIALIZER, NULL}; + 0, 0, 0, 0, 9273, NULL, {}, NULL, PTHREAD_MUTEX_INITIALIZER, NULL}; static char* str_unescape(char* s, char *d, int d_len) { @@ -346,9 +348,9 @@ void *FS_library_promethues_listen(void *arg) // Set up HTTP server parameters mg_set_protocol_http_websocket(_handle->nc); - for (;;) + while(g_FS2_LIBRARY_handle.create_thread > 0) { - mg_mgr_poll(&_handle->mgr, 1000); + mg_mgr_poll(&_handle->mgr, 1000); } mg_mgr_free(&_handle->mgr); @@ -390,6 +392,11 @@ int FS_library_set_prometheus_port(unsigned short port) } int FS_library_set_prometheus_url_path(const char *url_path) { + if(g_FS2_LIBRARY_handle.url_path != NULL) + { + free(g_FS2_LIBRARY_handle.url_path); + g_FS2_LIBRARY_handle.url_path = NULL; + } g_FS2_LIBRARY_handle.url_path=(char *)calloc(1, strlen(url_path)+1); memcpy(g_FS2_LIBRARY_handle.url_path, url_path, strlen(url_path)); @@ -398,12 +405,36 @@ int FS_library_set_prometheus_url_path(const char *url_path) int FS_library_init(void) { - pthread_t tid=0; - - pthread_create(&tid, NULL, FS_library_promethues_listen, (void *)&g_FS2_LIBRARY_handle); + if(g_FS2_LIBRARY_handle.url_path == NULL) + { + g_FS2_LIBRARY_handle.url_path = (char *)calloc(strlen((char *)"/metrics")+1, 1); + memcpy(g_FS2_LIBRARY_handle.url_path, (char *)"/metrics", strlen((char *)"/metrics")); + } + g_FS2_LIBRARY_handle.create_thread = 1; + pthread_create(&g_FS2_LIBRARY_handle.tid, NULL, FS_library_promethues_listen, (void *)&g_FS2_LIBRARY_handle); return 0; } +void FS_library_destroy(void) +{ + void* ret ; + if(g_FS2_LIBRARY_handle.create_thread == 1) + { + g_FS2_LIBRARY_handle.create_thread = 0; + pthread_join(g_FS2_LIBRARY_handle.tid, &ret); + } + if(g_FS2_LIBRARY_handle.url_path != NULL) + { + free(g_FS2_LIBRARY_handle.url_path); + g_FS2_LIBRARY_handle.url_path = NULL; + } + if(g_FS2_LIBRARY_handle.fs2_handle) + { + free(g_FS2_LIBRARY_handle.fs2_handle); + } + return ; + +} diff --git a/test/fs2_test.cpp b/test/fs2_test.cpp index 40f74f1..610db3a 100644 --- a/test/fs2_test.cpp +++ b/test/fs2_test.cpp @@ -1,167 +1,174 @@ -#include "field_stat2.h" +#include "../inc/field_stat2.h" +#include <assert.h> #include <stdio.h> #include <stdlib.h> -#include <assert.h> -#include <unistd.h> #include <string.h> +#include <unistd.h> -#define TEST_STATUS_NUM 4 -#define TEST_FIELD_NUM 9 -#define TEST_LINE_NUM 7 -#define TEST_COLUMN_NUM 8 -#define TEST_HISTOGRAM_NUM 4 +#define TEST_STATUS_NUM 4 +#define TEST_FIELD_NUM 9 +#define TEST_LINE_NUM 7 +#define TEST_COLUMN_NUM 8 +#define TEST_HISTOGRAM_NUM 4 -#define TEST_RUNTIME_REG_NUM 32 -#define TEST_RUNTIME_REG_LINE_NUM 6 +#define TEST_RUNTIME_REG_NUM 32 +#define TEST_RUNTIME_REG_LINE_NUM 6 -int main(int argc,char* argv[]) +int main(int argc, char *argv[]) { - screen_stat_handle_t handle=NULL; - const char* stat_path="./fs2_test.status"; - const char* app_name="fs2_test"; + screen_stat_handle_t handle = NULL; + const char *stat_path = "./fs2_test.status"; + const char *app_name = "fs2_test"; char buff[128]; - int value=0,i=0,j=0,runtime_reg_num=0,runtime_reg_line_num=0,ret=0; - int loops=10; - - unsigned short port=9001; + int value = 0, i = 0, j = 0, runtime_reg_num = 0, runtime_reg_line_num = 0, ret = 0; + int loops = 10; + + unsigned short port = 9001; FS_library_set_prometheus_port(port); - char *url_path=(char *)"/prometheus"; + char *url_path = (char *)"/prometheus"; FS_library_set_prometheus_url_path(url_path); FS_library_init(); - - handle=FS_create_handle(); - - FS_set_para(handle, APP_NAME, app_name, strlen(app_name)+1); - value=0; - FS_set_para(handle, FLUSH_BY_DATE, &value, sizeof(value)); - FS_set_para(handle, OUTPUT_DEVICE, stat_path, strlen(stat_path)+1); - value=1; - FS_set_para(handle, PRINT_MODE, &value, sizeof(value)); - value=1; - FS_set_para(handle, CREATE_THREAD, &value, sizeof(value)); - value=0; - FS_set_para(handle, METRIS_FORMAT, &value, sizeof(value)); - value=2; - FS_set_para(handle, STAT_CYCLE, &value, sizeof(value)); - value=4096; - FS_set_para(handle, MAX_STAT_FIELD_NUM, &value, sizeof(value)); - FS_set_para(handle, STATS_SERVER_IP, "127.0.0.1", strlen("127.0.0.1")); - value=8100; - FS_set_para(handle, STATS_SERVER_PORT, &value, sizeof(value)); + for (int repeat_cnt = 0; repeat_cnt < 10; repeat_cnt++) + { - //value=FS_OUTPUT_INFLUX_LINE; - value=FS_OUTPUT_STATSD; - FS_set_para(handle, STATS_FORMAT, &value, sizeof(value)); - - value=1; - FS_set_para(handle, OUTPUT_PROMETHEUS, &value, sizeof(value)); + handle = FS_create_handle(); - const char* histogram_format="0.1,0.5,0.8,0.9,0.95,0.99"; - FS_set_para(handle, HISTOGRAM_GLOBAL_BINS, histogram_format, strlen(histogram_format)+1); - - int status_ids[TEST_STATUS_NUM], field_ids[TEST_FIELD_NUM], line_ids[TEST_LINE_NUM+TEST_RUNTIME_REG_LINE_NUM], column_ids[TEST_COLUMN_NUM]; - int histogram_ids[TEST_HISTOGRAM_NUM]; - int runtime_status_ids[TEST_RUNTIME_REG_NUM]; - for(i=0;i<TEST_STATUS_NUM;i++) - { - snprintf(buff,sizeof(buff),"(status_%02d)/\\-,;%%$*",i); - status_ids[i]=FS_register(handle, FS_STYLE_STATUS, FS_CALC_CURRENT,buff); - } - for(i=0;i<TEST_FIELD_NUM;i++) - { - snprintf(buff,sizeof(buff),"(field_%02d)",i); - field_ids[i]=FS_register(handle, FS_STYLE_FIELD, FS_CALC_CURRENT,buff); - } - for(i=0;i<TEST_LINE_NUM;i++) - { - snprintf(buff,sizeof(buff),"line_%d",i); - line_ids[i]=FS_register(handle, FS_STYLE_LINE, FS_CALC_CURRENT,buff); - } - for(i=0;i<TEST_COLUMN_NUM;i++) - { - snprintf(buff,sizeof(buff),"column_%d",i); - column_ids[i]=FS_register(handle, FS_STYLE_COLUMN, FS_CALC_SPEED,buff); - } - for(i=0;i<TEST_HISTOGRAM_NUM;i++) - { - snprintf(buff,sizeof(buff),"{rpc_%d}",i); - if(i==1) + FS_set_para(handle, APP_NAME, app_name, strlen(app_name) + 1); + value = 0; + FS_set_para(handle, FLUSH_BY_DATE, &value, sizeof(value)); + FS_set_para(handle, OUTPUT_DEVICE, stat_path, strlen(stat_path) + 1); + value = 1; + FS_set_para(handle, PRINT_MODE, &value, sizeof(value)); + value = 1; + FS_set_para(handle, CREATE_THREAD, &value, sizeof(value)); + value = 0; + FS_set_para(handle, METRIS_FORMAT, &value, sizeof(value)); + value = 2; + FS_set_para(handle, STAT_CYCLE, &value, sizeof(value)); + value = 4096; + FS_set_para(handle, MAX_STAT_FIELD_NUM, &value, sizeof(value)); + + FS_set_para(handle, STATS_SERVER_IP, "127.0.0.1", strlen("127.0.0.1")); + value = 8100; + FS_set_para(handle, STATS_SERVER_PORT, &value, sizeof(value)); + + //value=FS_OUTPUT_INFLUX_LINE; + value = FS_OUTPUT_STATSD; + FS_set_para(handle, STATS_FORMAT, &value, sizeof(value)); + + value = 1; + FS_set_para(handle, OUTPUT_PROMETHEUS, &value, sizeof(value)); + + const char *histogram_format = "0.1,0.5,0.8,0.9,0.95,0.99"; + FS_set_para(handle, HISTOGRAM_GLOBAL_BINS, histogram_format, strlen(histogram_format) + 1); + + int status_ids[TEST_STATUS_NUM], field_ids[TEST_FIELD_NUM], line_ids[TEST_LINE_NUM + TEST_RUNTIME_REG_LINE_NUM], column_ids[TEST_COLUMN_NUM]; + int histogram_ids[TEST_HISTOGRAM_NUM]; + int runtime_status_ids[TEST_RUNTIME_REG_NUM]; + for (i = 0; i < TEST_STATUS_NUM; i++) { - histogram_ids[i]=FS_register_histogram(handle, FS_CALC_SPEED, buff - ,1,100000,3); + snprintf(buff, sizeof(buff), "(status_%02d)/\\-,;%%$*", i); + status_ids[i] = FS_register(handle, FS_STYLE_STATUS, FS_CALC_CURRENT, buff); } - else + for (i = 0; i < TEST_FIELD_NUM; i++) { - histogram_ids[i]=FS_register_histogram(handle, FS_CALC_CURRENT, buff - ,1,100000,3); + snprintf(buff, sizeof(buff), "(field_%02d)", i); + field_ids[i] = FS_register(handle, FS_STYLE_FIELD, FS_CALC_CURRENT, buff); } - } - value=column_ids[2]; - FS_set_para(handle, ID_INVISBLE, &value, sizeof(value)); - snprintf(buff,sizeof(buff),"c0/c1"); - FS_register_ratio(handle, - column_ids[0], - column_ids[1], - 1, - FS_STYLE_COLUMN, - FS_CALC_SPEED, - buff); - value=line_ids[1]; - FS_set_para(handle, NOT_SEND_METRIC_TO_SERVER, &value, sizeof(value)); - - FS_start(handle); - loops=100; - while (loops>0) - { - loops--; - for(i=0;i<TEST_STATUS_NUM;i++) + for (i = 0; i < TEST_LINE_NUM; i++) { - FS_operate(handle, status_ids[i], 0, FS_OP_SET, i*10); + snprintf(buff, sizeof(buff), "line_%d", i); + line_ids[i] = FS_register(handle, FS_STYLE_LINE, FS_CALC_CURRENT, buff); } - for(i=0;i<TEST_FIELD_NUM;i++) + for (i = 0; i < TEST_COLUMN_NUM; i++) { - FS_operate(handle, field_ids[i], 0, FS_OP_ADD, i*100); + snprintf(buff, sizeof(buff), "column_%d", i); + column_ids[i] = FS_register(handle, FS_STYLE_COLUMN, FS_CALC_SPEED, buff); } - for(i=0;i<TEST_LINE_NUM+runtime_reg_line_num;i++) + for (i = 0; i < TEST_HISTOGRAM_NUM; i++) { - for(j=0;j<TEST_COLUMN_NUM;j++) + snprintf(buff, sizeof(buff), "{rpc_%d}", i); + if (i == 1) { - FS_operate(handle, line_ids[i], column_ids[j], FS_OP_ADD, (j+1)*30); + histogram_ids[i] = FS_register_histogram(handle, FS_CALC_SPEED, buff, 1, 100000, 3); + } + else + { + histogram_ids[i] = FS_register_histogram(handle, FS_CALC_CURRENT, buff, 1, 100000, 3); } } - for(i=0;i<runtime_reg_num;i++) - { - FS_operate(handle, runtime_status_ids[i], 0, FS_OP_ADD, i*1000); - } - if(runtime_reg_num<TEST_RUNTIME_REG_NUM) - { - snprintf(buff,sizeof(buff),"rt_reg_%02d",runtime_reg_num); - ret=FS_register(handle,FS_STYLE_STATUS, FS_CALC_SPEED, buff); - assert(ret>=0); - runtime_status_ids[runtime_reg_num]=ret; - runtime_reg_num++; - ret=FS_register(handle,FS_STYLE_COLUMN, FS_CALC_SPEED, buff); - assert(ret==-1);//always failed - } - if(runtime_reg_line_num<TEST_RUNTIME_REG_LINE_NUM) - { - snprintf(buff,sizeof(buff),"line_rt_%02d",runtime_reg_line_num); - ret=FS_register(handle,FS_STYLE_LINE, FS_CALC_SPEED, buff); - assert(ret>=0); - line_ids[TEST_LINE_NUM+runtime_reg_line_num]=ret; - runtime_reg_line_num++; - } - long long preset[]={1,10,20,30,40,200,300,400,600,1000,2000,4000,5000,8000,100000}; - for(i=0;i<TEST_HISTOGRAM_NUM;i++) + value = column_ids[2]; + FS_set_para(handle, ID_INVISBLE, &value, sizeof(value)); + snprintf(buff, sizeof(buff), "c0/c1"); + FS_register_ratio(handle, + column_ids[0], + column_ids[1], + 1, + FS_STYLE_COLUMN, + FS_CALC_SPEED, + buff); + value = line_ids[1]; + FS_set_para(handle, NOT_SEND_METRIC_TO_SERVER, &value, sizeof(value)); + + FS_start(handle); + loops = 10; + while (loops > 0) { - for(j=0;(size_t)j<sizeof(preset)/sizeof(long long);j++) + loops--; + for (i = 0; i < TEST_STATUS_NUM; i++) + { + FS_operate(handle, status_ids[i], 0, FS_OP_SET, i * 10); + } + for (i = 0; i < TEST_FIELD_NUM; i++) { - FS_operate(handle, histogram_ids[i], 0, FS_OP_SET,preset[j]); + FS_operate(handle, field_ids[i], 0, FS_OP_ADD, i * 100); } + for (i = 0; i < TEST_LINE_NUM + runtime_reg_line_num; i++) + { + for (j = 0; j < TEST_COLUMN_NUM; j++) + { + FS_operate(handle, line_ids[i], column_ids[j], FS_OP_ADD, (j + 1) * 30); + } + } + for (i = 0; i < runtime_reg_num; i++) + { + FS_operate(handle, runtime_status_ids[i], 0, FS_OP_ADD, i * 1000); + } + if (runtime_reg_num < TEST_RUNTIME_REG_NUM) + { + snprintf(buff, sizeof(buff), "rt_reg_%02d", runtime_reg_num); + ret = FS_register(handle, FS_STYLE_STATUS, FS_CALC_SPEED, buff); + assert(ret >= 0); + runtime_status_ids[runtime_reg_num] = ret; + runtime_reg_num++; + ret = FS_register(handle, FS_STYLE_COLUMN, FS_CALC_SPEED, buff); + assert(ret == -1); //always failed + } + if (runtime_reg_line_num < TEST_RUNTIME_REG_LINE_NUM) + { + snprintf(buff, sizeof(buff), "line_rt_%02d", runtime_reg_line_num); + ret = FS_register(handle, FS_STYLE_LINE, FS_CALC_SPEED, buff); + assert(ret >= 0); + line_ids[TEST_LINE_NUM + runtime_reg_line_num] = ret; + runtime_reg_line_num++; + } + long long preset[] = {1, 10, 20, 30, 40, 200, 300, 400, 600, 1000, 2000, 4000, 5000, 8000, 100000}; + for (i = 0; i < TEST_HISTOGRAM_NUM; i++) + { + for (j = 0; (size_t)j < sizeof(preset) / sizeof(long long); j++) + { + FS_operate(handle, histogram_ids[i], 0, FS_OP_SET, preset[j]); + } + } + sleep(1); } + FS_stop(&handle); + printf("fs2 stoped cnt = %d, will sleep 1s\n", repeat_cnt); sleep(1); } - FS_stop(&handle); + FS_library_destroy(); + printf("FS library destoryed, will sleep 30s\n"); + sleep(30); + return 0; } - |
