From 55b82e9f049c9ec9bce3e8f8d309a01b3619ecc2 Mon Sep 17 00:00:00 2001 From: liuchang Date: Thu, 23 Mar 2023 11:51:15 +0000 Subject: suit filestat3 --- shaping/src/shaper_stat.cpp | 270 ++++++++++++++++---------------------------- 1 file changed, 95 insertions(+), 175 deletions(-) (limited to 'shaping/src/shaper_stat.cpp') diff --git a/shaping/src/shaper_stat.cpp b/shaping/src/shaper_stat.cpp index 9ed1669..6498be9 100644 --- a/shaping/src/shaper_stat.cpp +++ b/shaping/src/shaper_stat.cpp @@ -1,284 +1,204 @@ +#include #include #include #include #include -#include - +#include "log.h" +#include "utils.h" #include "shaper.h" #include "shaper_stat.h" +thread_local struct fieldstat_tag tags[TAG_IDX_MAX]; -#define SHAPING_STAT_SEND_INTERVAL_SEC 1 //unit: second -#define SHAPING_STAT_SEND_INTERVAL_NS 500000000 //unit: nano second - -#define SHAPING_STAT_FORMAT "SHAPING-STAT,rule_id=%d,profile_id=%d,priority=%d,profile_type=%s "\ - "queueing_sessions=%d,in_rx_pkts=%llu,in_rx_bytes=%llu,"\ - "in_tx_pkts=%llu,in_tx_bytes=%llu,in_drop_pkts=%llu,in_max_latency_us=%llu,in_queue_len=%lld,"\ - "out_rx_pkts=%llu,out_rx_bytes=%llu,out_tx_pkts=%llu,out_tx_bytes=%llu,"\ - "out_drop_pkts=%llu,out_max_latency_us=%llu,out_queue_len=%lld" - -static void shaper_stat_counter_clear(struct shaping_stat_data *s) -{ - long long in_queue_len, out_queue_len; - struct shaping_stat_data_dir *in = &s->incoming; - struct shaping_stat_data_dir *out = &s->outgoing; - - in_queue_len = in->queue_len;//queue_len is gauge metric, do not clear - out_queue_len = out->queue_len; - - memset(in, 0, sizeof(struct shaping_stat_data_dir)); - memset(out, 0, sizeof(struct shaping_stat_data_dir)); - - in->queue_len = in_queue_len; - out->queue_len = out_queue_len; - - return; -} - -static void shaper_stat_data_send(struct shaping_stat *stat, struct shaping_stat_data *s) -{ - char buf[1024]; - struct shaping_stat_data_dir *in = &s->incoming; - struct shaping_stat_data_dir *out = &s->outgoing; - - snprintf(buf, sizeof(buf), SHAPING_STAT_FORMAT, s->key.rule_id, s->key.profile_id, s->key.priority, - s->key.profile_type == SHAPING_PROFILE_TYPE_PRIMARY ? "primary" : "borrow", - s->queueing_session_num, in->rx_pkts, in->rx_bytes, in->tx_pkts, in->tx_bytes, - in->drop_pkts, in->max_latency, in->queue_len, out->rx_pkts, out->rx_bytes, out->tx_pkts, out->tx_bytes, - out->drop_pkts, out->max_latency, out->queue_len); - - sendto(stat->sock_fd, buf, strlen(buf), 0, (struct sockaddr*)&stat->sock_addr, sizeof(stat->sock_addr)); - - shaper_stat_counter_clear(s); - return; -} - -static void shaper_stat_data_send_free(struct shaping_stat *stat, struct shaping_stat_data **stat_hashtbl) -{ - struct shaping_stat_data *s, *tmp = NULL; - - if (!stat || !*stat_hashtbl) { - return; - } - - HASH_ITER(hh, *stat_hashtbl, s, tmp) { - shaper_stat_data_send(stat, s); - HASH_DEL(*stat_hashtbl, s); - free(s); - } - - return; -} - -void shaper_stat_send_free(struct shaping_stat *stat) +void shaper_stat_destroy(struct shaping_stat *stat) { if (!stat) { return; } - if (stat->stat_hashtbl) { - shaper_stat_data_send_free(stat, &stat->stat_hashtbl); + if (stat->instance) { + fieldstat_dynamic_instance_free(stat->instance); } free(stat); return; } -void shaper_stat_send(struct shaping_stat *stat, struct shaping_stat_data **stat_hashtbl) +struct shaping_stat* shaper_stat_new(int thread_num) { - struct shaping_stat_data *s, *tmp = NULL; - struct timespec curr_time; - - if (!stat || !*stat_hashtbl) { - return; - } - - clock_gettime(CLOCK_MONOTONIC, &curr_time); - if (curr_time.tv_sec - stat->update_time.tv_sec >= SHAPING_STAT_SEND_INTERVAL_SEC|| - curr_time.tv_nsec - stat->update_time.tv_nsec >= SHAPING_STAT_SEND_INTERVAL_NS) { - stat->update_time = curr_time; - HASH_ITER(hh, *stat_hashtbl, s, tmp) { - shaper_stat_data_send(stat, s); - } + struct shaping_stat *stat = NULL; + int column_num; + const char *column_name[] = {"queueing_sessions", "in_max_latency_us", "in_queue_len", "out_max_latency_us", "out_queue_len", //first line is gauge, second line is counter + "in_pkts", "in_bytes", "in_drop_pkts", "out_pkts", "out_bytes", "out_drop_pkts"}; + enum field_type column_type[] = {FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE, + FIELD_TYPE_COUNTER, FIELD_TYPE_COUNTER, FIELD_TYPE_COUNTER, FIELD_TYPE_COUNTER, FIELD_TYPE_COUNTER, FIELD_TYPE_COUNTER}; + + column_num = sizeof(column_name)/sizeof(column_name[0]); + if (column_num != STAT_COLUNM_IDX_MAX) { + LOG_ERROR("%s: shaping init fieldstat failed, column_num %d != index num %d", LOG_TAG_STAT, column_num, STAT_COLUNM_IDX_MAX); + goto ERROR; } - return; -} + stat = (struct shaping_stat *)calloc(1, sizeof(struct shaping_stat)); -struct shaping_stat* shaper_stat_new(char *telegraf_ip, short telegraf_port) -{ - struct shaping_stat *stat = NULL; - struct timespec curr_time; + stat->instance = fieldstat_dynamic_instance_new("shaping_engine", thread_num); + if (stat->instance == NULL) { + LOG_ERROR("%s: shaping init fieldstat instance failed", LOG_TAG_STAT); + goto ERROR; + } - clock_gettime(CLOCK_MONOTONIC, &curr_time); + stat->table_id = fieldstat_register_dynamic_table(stat->instance, "shaping_metric", column_name, column_type, column_num, stat->column_ids); + if (stat->table_id < 0) { + LOG_ERROR("%s: shaping fieldstat register table failed", LOG_TAG_STAT); + goto ERROR; + } - stat = (struct shaping_stat *)calloc(1, sizeof(struct shaping_stat)); + tags[TAG_RULE_ID_IDX].key = "rule_id"; + tags[TAG_RULE_ID_IDX].value_type = 0; + tags[TAG_PROFILE_ID_IDX].key = "profile_id"; + tags[TAG_PROFILE_ID_IDX].value_type = 0; + tags[TAG_PRIORITY_IDX].key = "priority"; + tags[TAG_PRIORITY_IDX].value_type = 0; + tags[TAG_PROFILE_TYPE_IDX].key = "profile_type"; + tags[TAG_PROFILE_TYPE_IDX].value_type = 2; - stat->sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - stat->sock_addr.sin_family = AF_INET; - stat->sock_addr.sin_port = htons(telegraf_port); - stat->sock_addr.sin_addr.s_addr = inet_addr(telegraf_ip); - stat->update_time = curr_time; + fieldstat_dynamic_instance_start(stat->instance); return stat; + +ERROR: + if (stat) { + if (stat->instance) { + fieldstat_dynamic_instance_free(stat->instance); + } + free(stat); + } + return NULL; } -static struct shaping_stat_data *shaper_stat_ins_get(struct shaping_stat_data **stat_hashtbl, int rule_id, int profile_id, int priority, int profile_type) +static void shaper_stat_tags_build(int rule_id, int profile_id, int priority, int profile_type) { - struct shaping_stat_data *s_stat_data = NULL; - struct shaping_stat_data_key key; - memset(&key, 0, sizeof(key));//important for uthash opration - key.rule_id = rule_id; - key.profile_id = profile_id; - key.priority = priority; - key.profile_type = profile_type; + tags[TAG_RULE_ID_IDX].value_int = rule_id; - HASH_FIND(hh, *stat_hashtbl, &key, sizeof(struct shaping_stat_data_key), s_stat_data); - if (!s_stat_data) { - s_stat_data = (struct shaping_stat_data *)calloc(1, sizeof(struct shaping_stat_data)); + tags[TAG_PROFILE_ID_IDX].value_int = profile_id; - memcpy(&s_stat_data->key, &key, sizeof(key)); + tags[TAG_PRIORITY_IDX].value_int = priority; - HASH_ADD(hh, *stat_hashtbl, key, sizeof(struct shaping_stat_data_key), s_stat_data); + if (profile_type == SHAPING_PROFILE_TYPE_PRIMARY) { + tags[TAG_PROFILE_TYPE_IDX].value_str = "primary"; + } else { + tags[TAG_PROFILE_TYPE_IDX].value_str = "borrow"; } - return s_stat_data; + return; } -void shaper_stat_drop_inc(struct shaping_stat_data **stat_hashtbl, int rule_id, int profile_id, - int priority, unsigned char direction, int pkt_len) +void shaper_stat_drop_inc(struct shaping_stat *stat, int rule_id, int profile_id, + int priority, unsigned char direction, int pkt_len, int thread_id) { - struct shaping_stat_data *s_stat_data = NULL; - - s_stat_data = shaper_stat_ins_get(stat_hashtbl, rule_id, profile_id, priority, SHAPING_PROFILE_TYPE_PRIMARY); + shaper_stat_tags_build(rule_id, profile_id, priority, SHAPING_PROFILE_TYPE_PRIMARY); if (direction == SHAPING_DIR_IN) { - s_stat_data->incoming.drop_pkts++; - s_stat_data->incoming.rx_pkts++; - s_stat_data->incoming.rx_bytes += pkt_len; + fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[IN_DROP_PKTS_IDX], "shaping_metric_row", 1, tags, TAG_IDX_MAX, thread_id); } else { - s_stat_data->outgoing.drop_pkts++; - s_stat_data->outgoing.rx_pkts++; - s_stat_data->outgoing.rx_bytes += pkt_len; + fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[OUT_DROP_PKTS_IDX], "shaping_metric_row", 1, tags, TAG_IDX_MAX, thread_id); } return; } -void shaper_stat_forward_inc(struct shaping_stat_data **stat_hashtbl, int rule_id, int profile_id, - int priority, unsigned char direction, int pkt_len, int profile_type) +void shaper_stat_forward_inc(struct shaping_stat *stat, int rule_id, int profile_id, + int priority, unsigned char direction, int pkt_len, int profile_type, int thread_id) { - struct shaping_stat_data *s_stat_data = NULL; - - s_stat_data = shaper_stat_ins_get(stat_hashtbl, rule_id, profile_id, priority, profile_type); + shaper_stat_tags_build(rule_id, profile_id, priority, profile_type); if (direction == SHAPING_DIR_IN) { - s_stat_data->incoming.tx_pkts++; - s_stat_data->incoming.tx_bytes += pkt_len; - s_stat_data->incoming.rx_pkts++; - s_stat_data->incoming.rx_bytes += pkt_len; + fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[IN_PKTS_IDX], "shaping_metric_row", 1, tags, TAG_IDX_MAX, thread_id); + fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[IN_BYTES_IDX], "shaping_metric_row", pkt_len, tags, TAG_IDX_MAX, thread_id); } else { - s_stat_data->outgoing.tx_pkts++; - s_stat_data->outgoing.tx_bytes += pkt_len; - s_stat_data->outgoing.rx_pkts++; - s_stat_data->outgoing.rx_bytes += pkt_len; + fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[OUT_PKTS_IDX], "shaping_metric_row", 1, tags, TAG_IDX_MAX, thread_id); + fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[OUT_BYTES_IDX], "shaping_metric_row", pkt_len, tags, TAG_IDX_MAX, thread_id); } return; } -void shaper_stat_forward_all_rule_inc(struct shaping_stat_data **stat_hashtbl, struct shaping_flow *sf, unsigned char direction, int pkt_len) +void shaper_stat_forward_all_rule_inc(struct shaping_stat *stat, struct shaping_flow *sf, unsigned char direction, int pkt_len, int thread_id) { struct shaping_rule_info *rule; int i; for (i = 0; i < sf->rule_num; i++) { rule = &sf->matched_rule_infos[i]; - shaper_stat_forward_inc(stat_hashtbl, rule->id, rule->primary.id, rule->primary.priority, direction, pkt_len, SHAPING_PROFILE_TYPE_PRIMARY); + shaper_stat_forward_inc(stat, rule->id, rule->primary.id, rule->primary.priority, direction, pkt_len, SHAPING_PROFILE_TYPE_PRIMARY, thread_id); } return; } -void shaper_stat_queueing_session_inc(struct shaping_stat_data **stat_hashtbl, int rule_id, int profile_id, int priority, int profile_type) +void shaper_stat_queueing_session_inc(struct shaping_stat *stat, int rule_id, int profile_id, int priority, int profile_type, int thread_id) { - struct shaping_stat_data *s_stat_data = NULL; - - s_stat_data = shaper_stat_ins_get(stat_hashtbl, rule_id, profile_id, priority, profile_type); - - s_stat_data->queueing_session_num++; + shaper_stat_tags_build(rule_id, profile_id, priority, profile_type); + fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[QUEUEING_SESSIONS_IDX], "shaping_metric_row", 1, tags, TAG_IDX_MAX, thread_id); return; } -void shaper_stat_queueing_session_dec(struct shaping_stat_data **stat_hashtbl, int rule_id, int profile_id, int priority, int profile_type) +void shaper_stat_queueing_session_dec(struct shaping_stat *stat, int rule_id, int profile_id, int priority, int profile_type, int thread_id) { - struct shaping_stat_data *s_stat_data = NULL; - - s_stat_data = shaper_stat_ins_get(stat_hashtbl, rule_id, profile_id, priority, profile_type); - - s_stat_data->queueing_session_num--; + shaper_stat_tags_build(rule_id, profile_id, priority, profile_type); + fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[QUEUEING_SESSIONS_IDX], "shaping_metric_row", -1, tags, TAG_IDX_MAX, thread_id); return; } -void shaper_stat_queueing_pkt_inc(struct shaping_stat_data **stat_hashtbl, int rule_id, int profile_id, - int priority, unsigned char direction, int pkt_len, int profile_type) +void shaper_stat_queueing_pkt_inc(struct shaping_stat *stat, int rule_id, int profile_id, + int priority, unsigned char direction, int pkt_len, int profile_type, int thread_id) { - struct shaping_stat_data *s_stat_data = NULL; - - s_stat_data = shaper_stat_ins_get(stat_hashtbl, rule_id, profile_id, priority, profile_type); - + shaper_stat_tags_build(rule_id, profile_id, priority, profile_type); if (direction == SHAPING_DIR_IN) { - s_stat_data->incoming.rx_pkts++; - s_stat_data->incoming.rx_bytes += pkt_len; - s_stat_data->incoming.queue_len++; + fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[IN_QUEUE_LEN_IDX], "shaping_metric_row", 1, tags, TAG_IDX_MAX, thread_id); } else { - s_stat_data->outgoing.rx_pkts++; - s_stat_data->outgoing.rx_bytes += pkt_len; - s_stat_data->outgoing.queue_len++; + fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[OUT_QUEUE_LEN_IDX], "shaping_metric_row", 1, tags, TAG_IDX_MAX, thread_id); } return; } -void shaper_stat_queueing_pkt_dec(struct shaping_stat_data **stat_hashtbl, int rule_id, int profile_id, - int priority, unsigned char direction, int pkt_len, int profile_type) +void shaper_stat_queueing_pkt_dec(struct shaping_stat *stat, int rule_id, int profile_id, + int priority, unsigned char direction, int pkt_len, int profile_type, int thread_id) { - struct shaping_stat_data *s_stat_data = NULL; - - s_stat_data = shaper_stat_ins_get(stat_hashtbl, rule_id, profile_id, priority, profile_type); - + shaper_stat_tags_build(rule_id, profile_id, priority, profile_type); if (direction == SHAPING_DIR_IN) { - s_stat_data->incoming.rx_pkts--; - s_stat_data->incoming.rx_bytes -= pkt_len; - s_stat_data->incoming.queue_len--; + fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[IN_QUEUE_LEN_IDX], "shaping_metric_row", -1, tags, TAG_IDX_MAX, thread_id); } else { - s_stat_data->outgoing.rx_pkts--; - s_stat_data->outgoing.rx_bytes -= pkt_len; - s_stat_data->outgoing.queue_len--; + fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[OUT_QUEUE_LEN_IDX], "shaping_metric_row", -1, tags, TAG_IDX_MAX, thread_id); } return; } -void shaper_stat_max_latency_update(struct shaping_stat_data **stat_hashtbl, int rule_id, int profile_id, - int priority, unsigned char direction, unsigned long long latency, int profile_type) -{ +void shaper_stat_max_latency_update(struct shaping_stat *stat, int rule_id, int profile_id, + int priority, unsigned char direction, unsigned long long latency, int profile_type, int thread_id) +{//TODO +#if 0 struct shaping_stat_data *s_stat_data = NULL; s_stat_data = shaper_stat_ins_get(stat_hashtbl, rule_id, profile_id, priority, profile_type); + shaper_stat_tags_build(rule_id, profile_id, priority, profile_type); if (direction == SHAPING_DIR_IN) { if (latency > s_stat_data->incoming.max_latency) { s_stat_data->incoming.max_latency = latency; + fieldstat_dynamic_table_metric_value_set(stat->instance, stat->table_id, stat->column_ids[IN_MAX_LATENCY_IDX], "shaping_metric_row", latency, tags, TAG_IDX_MAX, thread_id); } } else { if (latency > s_stat_data->outgoing.max_latency) { s_stat_data->outgoing.max_latency = latency; + fieldstat_dynamic_table_metric_value_set(stat->instance, stat->table_id, stat->column_ids[OUT_MAX_LATENCY_IDX], "shaping_metric_row", latency, tags, TAG_IDX_MAX, thread_id); } } - +#endif return; } -- cgit v1.2.3 From c25b65bc60a5fb82c576458deb7ef12f1a6885b3 Mon Sep 17 00:00:00 2001 From: liuchang Date: Fri, 24 Mar 2023 09:53:38 +0000 Subject: fix test case for stat after using fieldstat3 --- conf/main.conf | 7 +- shaping/include/shaper.h | 2 - shaping/include/shaper_stat.h | 2 +- shaping/src/shaper.cpp | 10 +- shaping/src/shaper_stat.cpp | 43 +++++- shaping/test/gtest_shaper.cpp | 302 +++++++++++++++++++++++++++----------- shaping/test/gtest_shaper_bak.cpp | 234 ----------------------------- shaping/test/stub.cpp | 4 +- shaping/test/stub.h | 5 +- shaping/test/test_conf/main.conf | 8 +- 10 files changed, 274 insertions(+), 343 deletions(-) delete mode 100644 shaping/test/gtest_shaper_bak.cpp (limited to 'shaping/src/shaper_stat.cpp') diff --git a/conf/main.conf b/conf/main.conf index e134a70..5a8adec 100644 --- a/conf/main.conf +++ b/conf/main.conf @@ -28,9 +28,10 @@ SWARMKV_CLUSTER_ANNOUNCE_PORT=8501 SWARMKV_HEALTH_CHECK_PORT=0 SWARMKV_HEALTH_CHECK_ANNOUNCE_PORT=1111 -#[METRIC] -#TELEGRAF_IP="127.0.0.1" -#TELEGRAF_PORT=6667 +[METRIC] +FIELDSTAT_OUTPUT_INTERVAL_MS=500 +LINE_PROTOCOL_SERVER_IP="127.0.0.1" +LINE_PROTOCOL_SERVER_PORT=6667 [CONFIG] #PROFILE_QUEUE_LEN_PER_PRIORITY_MAX=128 diff --git a/shaping/include/shaper.h b/shaping/include/shaper.h index 0de6f62..0959ebe 100644 --- a/shaping/include/shaper.h +++ b/shaping/include/shaper.h @@ -20,8 +20,6 @@ #define SHAPING_GLOBAL_CONF_FILE "./conf/main.conf" struct shaping_global_conf { - char telegraf_ip[16]; - short telegraf_port; unsigned int session_queue_len_max; unsigned int priority_queue_len_max; int polling_node_num_max[SHAPING_PRIORITY_NUM_MAX]; diff --git a/shaping/include/shaper_stat.h b/shaping/include/shaper_stat.h index 02f9bef..30d869e 100644 --- a/shaping/include/shaper_stat.h +++ b/shaping/include/shaper_stat.h @@ -66,7 +66,7 @@ struct shaping_stat { }; void shaper_stat_destroy(struct shaping_stat *stat); -struct shaping_stat* shaper_stat_new(int thread_num); +struct shaping_stat* shaper_stat_init(int thread_num); void shaper_stat_queueing_pkt_inc(struct shaping_stat *stat, int rule_id, int profile_id, int priority, unsigned char direction, int pkt_len, int profile_type, int thread_id); void shaper_stat_queueing_pkt_dec(struct shaping_stat *stat, int rule_id, int profile_id, int priority, unsigned char direction, int pkt_len, int profile_type, int thread_id); diff --git a/shaping/src/shaper.cpp b/shaping/src/shaper.cpp index 5cd64c6..541e6e5 100644 --- a/shaping/src/shaper.cpp +++ b/shaping/src/shaper.cpp @@ -1,5 +1,3 @@ -#include "log.h" -#include "session_table.h" #include #include #include @@ -12,6 +10,8 @@ extern "C" { #include "libavl.h" } +#include "log.h" +#include "session_table.h" #include "addr_tuple4.h" #include "raw_packet.h" #include "shaper.h" @@ -913,10 +913,6 @@ int shaper_global_conf_init(struct shaping_global_conf *conf) } /*************************************************************************/ - - MESA_load_profile_string_def(SHAPING_GLOBAL_CONF_FILE, "METRIC", "TELEGRAF_IP", conf->telegraf_ip, sizeof(conf->telegraf_ip), "127.0.0.1"); - MESA_load_profile_short_def(SHAPING_GLOBAL_CONF_FILE, "METRIC", "TELEGRAF_PORT", &conf->telegraf_port, 6379); - MESA_load_profile_uint_def(SHAPING_GLOBAL_CONF_FILE, "CONFIG", "SESSION_QUEUE_LEN_MAX", &conf->session_queue_len_max, 128); MESA_load_profile_uint_def(SHAPING_GLOBAL_CONF_FILE, "CONFIG", "PRIORITY_QUEUE_LEN_MAX", &conf->priority_queue_len_max, 1024); @@ -991,7 +987,7 @@ struct shaping_ctx *shaping_engine_init() goto ERROR; } - ctx->stat = shaper_stat_new(conf.work_thread_num); + ctx->stat = shaper_stat_init(conf.work_thread_num); if (ctx->stat == NULL) { goto ERROR; } diff --git a/shaping/src/shaper_stat.cpp b/shaping/src/shaper_stat.cpp index 6498be9..878daa0 100644 --- a/shaping/src/shaper_stat.cpp +++ b/shaping/src/shaper_stat.cpp @@ -1,14 +1,22 @@ -#include #include #include #include #include +#include +#include #include "log.h" #include "utils.h" #include "shaper.h" #include "shaper_stat.h" +struct shaper_stat_conf { + int enable_backgroud_thread; + int output_interval_ms; + char telegraf_ip[16]; + short telegraf_port; +}; + thread_local struct fieldstat_tag tags[TAG_IDX_MAX]; void shaper_stat_destroy(struct shaping_stat *stat) @@ -25,10 +33,23 @@ void shaper_stat_destroy(struct shaping_stat *stat) return; } -struct shaping_stat* shaper_stat_new(int thread_num) +static int shaper_stat_conf_load(struct shaper_stat_conf *conf) +{ + memset(conf, 0, sizeof(struct shaper_stat_conf)); + + MESA_load_profile_string_def(SHAPING_GLOBAL_CONF_FILE, "METRIC", "TELEGRAF_IP", conf->telegraf_ip, sizeof(conf->telegraf_ip), "127.0.0.1"); + MESA_load_profile_short_def(SHAPING_GLOBAL_CONF_FILE, "METRIC", "TELEGRAF_PORT", &conf->telegraf_port, 6379); + MESA_load_profile_int_def(SHAPING_GLOBAL_CONF_FILE, "METRIC", "FIELDSTAT_OUTPUT_INTERVAL_MS", &conf->output_interval_ms, 500); + MESA_load_profile_int_def(SHAPING_GLOBAL_CONF_FILE, "METRIC", "FIELDSTAT_ENABLE_BACKGRUND_THREAD", &conf->enable_backgroud_thread, 1); + + return 0; +} + +struct shaping_stat* shaper_stat_init(int thread_num) { struct shaping_stat *stat = NULL; int column_num; + struct shaper_stat_conf conf; const char *column_name[] = {"queueing_sessions", "in_max_latency_us", "in_queue_len", "out_max_latency_us", "out_queue_len", //first line is gauge, second line is counter "in_pkts", "in_bytes", "in_drop_pkts", "out_pkts", "out_bytes", "out_drop_pkts"}; enum field_type column_type[] = {FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE, FIELD_TYPE_GAUGE, @@ -40,6 +61,11 @@ struct shaping_stat* shaper_stat_new(int thread_num) goto ERROR; } + if (shaper_stat_conf_load(&conf) != 0) { + LOG_ERROR("%s: shaping init metric conf failed", LOG_TAG_STAT); + goto ERROR; + } + stat = (struct shaping_stat *)calloc(1, sizeof(struct shaping_stat)); stat->instance = fieldstat_dynamic_instance_new("shaping_engine", thread_num); @@ -48,6 +74,12 @@ struct shaping_stat* shaper_stat_new(int thread_num) goto ERROR; } + fieldstat_dynamic_set_output_interval(stat->instance, conf.output_interval_ms); + fieldstat_dynamic_set_line_protocol_server(stat->instance, conf.telegraf_ip, conf.telegraf_port); + if (conf.enable_backgroud_thread == 0) { + fieldstat_dynamic_disable_background_thread(stat->instance); + } + stat->table_id = fieldstat_register_dynamic_table(stat->instance, "shaping_metric", column_name, column_type, column_num, stat->column_ids); if (stat->table_id < 0) { LOG_ERROR("%s: shaping fieldstat register table failed", LOG_TAG_STAT); @@ -199,6 +231,13 @@ void shaper_stat_max_latency_update(struct shaping_stat *stat, int rule_id, int } } #endif + + shaper_stat_tags_build(rule_id, profile_id, priority, profile_type); + if (direction == SHAPING_DIR_IN) { + fieldstat_dynamic_table_metric_value_set(stat->instance, stat->table_id, stat->column_ids[IN_MAX_LATENCY_IDX], "shaping_metric_row", -1, tags, TAG_IDX_MAX, thread_id); + } else { + fieldstat_dynamic_table_metric_value_set(stat->instance, stat->table_id, stat->column_ids[OUT_MAX_LATENCY_IDX], "shaping_metric_row", -1, tags, TAG_IDX_MAX, thread_id); + } return; } diff --git a/shaping/test/gtest_shaper.cpp b/shaping/test/gtest_shaper.cpp index 50aef6f..51b1a06 100644 --- a/shaping/test/gtest_shaper.cpp +++ b/shaping/test/gtest_shaper.cpp @@ -1,12 +1,21 @@ #include +#include +#include #include +#include #include "shaper.h" #include "shaper_maat.h" +#include "shaper_stat.h" #include "shaper_marsio.h" #include "stub.h" #define SHAPING_SESSION_QUEUE_LEN 128 +#define SHAPING_STAT_FILE_NAME "/tmp/shaping_metrics.json" +#define FIELDSTAT_AUTO_TIME_MAX 999999000 + +char profile_type_primary[] = "primary"; +char profile_type_borrow[] = "borrow"; static struct stub_packet* packet_new(unsigned long long income_time, unsigned int length, unsigned char dir) { @@ -60,7 +69,7 @@ static void send_packets(struct shaping_thread_ctx *ctx, struct shaping_flow *sf polling_entry(ctx->sp, ctx->stat, ctx); } - stub_curr_time_inc(); + stub_curr_time_inc(STUB_TIME_INC_FOR_PACKET); } return; @@ -91,6 +100,70 @@ static int judge_packet_eq(struct stub_pkt_queue *expec_queue, struct stub_pkt_q return 0; } +static void shaping_stat_judge(char *file_line, int rule_id, int profile_id, int priority, + unsigned long long tx_pkts, unsigned long long tx_bytes, + unsigned long long drop_pkts, long long queue_len, long long max_latency, + int queueing_sessions, unsigned char direction, char profile_type[]) +{ + cJSON *json = NULL; + cJSON *tmp_obj = NULL; + char attr_name[32] = {0}; + + json = cJSON_Parse(file_line); + ASSERT_TRUE(json != NULL); + + tmp_obj = cJSON_GetObjectItem(json, "rule_id"); + ASSERT_TRUE(tmp_obj != NULL); + EXPECT_EQ(rule_id, atoi(tmp_obj->valuestring)); + + tmp_obj = cJSON_GetObjectItem(json, "profile_id"); + ASSERT_TRUE(tmp_obj != NULL); + EXPECT_EQ(profile_id, atoi(tmp_obj->valuestring)); + + tmp_obj = cJSON_GetObjectItem(json, "priority"); + ASSERT_TRUE(tmp_obj != NULL); + EXPECT_EQ(priority, atoi(tmp_obj->valuestring)); + + tmp_obj = cJSON_GetObjectItem(json, "profile_type"); + ASSERT_TRUE(tmp_obj != NULL); + EXPECT_STREQ(tmp_obj->valuestring, profile_type); + + tmp_obj = cJSON_GetObjectItem(json, "queueing_sessions"); + ASSERT_TRUE(tmp_obj != NULL); + EXPECT_EQ(queueing_sessions, tmp_obj->valueint); + + snprintf(attr_name, sizeof(attr_name), "%s_pkts", direction == SHAPING_DIR_OUT ? "out" : "in"); + tmp_obj = cJSON_GetObjectItem(json, attr_name); + ASSERT_TRUE(tmp_obj != NULL); + EXPECT_EQ(tx_pkts, tmp_obj->valueint); + + snprintf(attr_name, sizeof(attr_name), "%s_bytes", direction == SHAPING_DIR_OUT ? "out" : "in"); + tmp_obj = cJSON_GetObjectItem(json, attr_name); + ASSERT_TRUE(tmp_obj != NULL); + EXPECT_EQ(tx_bytes, tmp_obj->valueint); + + snprintf(attr_name, sizeof(attr_name), "%s_drop_pkts", direction == SHAPING_DIR_OUT ? "out" : "in"); + tmp_obj = cJSON_GetObjectItem(json, attr_name); + ASSERT_TRUE(tmp_obj != NULL); + EXPECT_EQ(drop_pkts, tmp_obj->valueint); + + if (max_latency != -1) { + snprintf(attr_name, sizeof(attr_name), "%s_max_latency_us", direction == SHAPING_DIR_OUT ? "out" : "in"); + tmp_obj = cJSON_GetObjectItem(json, attr_name); + ASSERT_TRUE(tmp_obj != NULL); + EXPECT_EQ(max_latency, tmp_obj->valueint); + } + + snprintf(attr_name, sizeof(attr_name), "%s_queue_len", direction == SHAPING_DIR_OUT ? "out" : "in"); + tmp_obj = cJSON_GetObjectItem(json, attr_name); + ASSERT_TRUE(tmp_obj != NULL); + EXPECT_EQ(queue_len, tmp_obj->valueint); + + cJSON_Delete(json); + + return; +} + /*session1 match rule1 rule1: profile: limit 1000*/ @@ -117,6 +190,7 @@ TEST(single_session, udp_tx_in_order) actual_tx_queue = stub_get_tx_queue(); shaper_rules_update(&ctx->thread_ctx[0], sf, rule_id, 1); + /**********send packets*********************/ send_packets(&ctx->thread_ctx[0], sf, 100, 100, SHAPING_DIR_OUT, &expec_tx_queue, 1, 0); /*******************************************/ @@ -129,18 +203,21 @@ TEST(single_session, udp_tx_in_order) stub_refresh_token_bucket(0); for (int i = 0; i < 20; i++) {//even though invoke polling more than 10 times, there should be only 10 pkts be sent polling_entry(ctx->thread_ctx[0].sp, ctx->thread_ctx[0].stat, &ctx->thread_ctx[0]); - stub_curr_time_inc(); + stub_curr_time_inc(STUB_TIME_INC_FOR_PACKET); } ASSERT_EQ(0, judge_packet_eq(&expec_tx_queue, actual_tx_queue, 10)); ASSERT_TRUE(TAILQ_EMPTY(actual_tx_queue)); } + /***********send stat data here********************/ + stub_curr_time_inc(STUB_TIME_INC_FOR_METRIC_SEND);//inc time to send metric + fieldstat_dynamic_passive_output(ctx->stat->instance);//send metric manualy + shaping_flow_free(sf); shaping_engine_destroy(ctx); stub_clear_matched_shaping_rules(); -#if 0 /*******test statistics***********/ sleep(2);//wait telegraf to output char line[1024]; @@ -149,11 +226,11 @@ TEST(single_session, udp_tx_in_order) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - shaping_stat_judge(line, 0, 0, 1, 100, 10000, 100, 10000, 0, 0, 170, 0, DIR_ROUTE_UP, profile_type_primary);//max latency is last 10 pkts + //shaping_stat_judge(line, 0, 0, 1, 100, 10000, 0, 0, 170, 0, SHAPING_DIR_OUT, profile_type_primary);//max latency is last 10 pkts + shaping_stat_judge(line, 0, 0, 1, 100, 10000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file fclose(stat_file); -#endif } /*session1 match rule1 @@ -190,30 +267,35 @@ TEST(single_session, tcp_tx_in_order) ASSERT_EQ(0, judge_packet_eq(&expec_tx_queue, actual_tx_queue, 10)); ASSERT_TRUE(TAILQ_EMPTY(actual_tx_queue)); -#if 0 + /***********send stat data here********************/ - stub_shaper_stat_send(0); + stub_curr_time_inc(STUB_TIME_INC_FOR_METRIC_SEND);//inc time to send metric + fieldstat_dynamic_passive_output(ctx->stat->instance);//send metric manualy sleep(2);//wait telegraf generate metric -#endif + stub_refresh_token_bucket(0); for (int i = 0; i < 10; i++) {//10 pkts which is not pure control polling_entry(ctx->thread_ctx[0].sp, ctx->thread_ctx[0].stat, &ctx->thread_ctx[0]); - stub_curr_time_inc(); + stub_curr_time_inc(STUB_TIME_INC_FOR_PACKET); } ASSERT_EQ(0, judge_packet_eq(&expec_tx_queue, actual_tx_queue, 10)); while (!TAILQ_EMPTY(&expec_tx_queue)) {//20 pure contorl pkts polling_entry(ctx->thread_ctx[0].sp, ctx->thread_ctx[0].stat, &ctx->thread_ctx[0]); ASSERT_EQ(0, judge_packet_eq(&expec_tx_queue, actual_tx_queue, 1)); - stub_curr_time_inc(); + stub_curr_time_inc(STUB_TIME_INC_FOR_PACKET); } + /***********send stat data here********************/ + stub_curr_time_inc(STUB_TIME_INC_FOR_METRIC_SEND);//inc time to send metric + fieldstat_dynamic_passive_output(ctx->stat->instance);//send metric manualy + shaping_flow_free(sf); shaping_engine_destroy(ctx); stub_clear_matched_shaping_rules(); -#if 0 + /*******test statistics***********/ sleep(2);//wait telegraf to output char line[1024]; @@ -222,15 +304,15 @@ TEST(single_session, tcp_tx_in_order) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - shaping_stat_judge(line, 0, 0, 1, 40, 4000, 10, 1000, 0, 30, 0, 1, DIR_ROUTE_UP, profile_type_primary);//max latency is first queueing pkts + shaping_stat_judge(line, 0, 0, 1, 10, 1000, 0, 30, 0, 1, SHAPING_DIR_OUT, profile_type_primary); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - shaping_stat_judge(line, 0, 0, 1, 0, 0, 30, 3000, 0, 0, 30, 0, DIR_ROUTE_UP, profile_type_primary); + //shaping_stat_judge(line, 0, 0, 1, 30, 3000, 0, 0, 30 + (STUB_TIME_INC_FOR_METRIC_SEND / 1000), 0, SHAPING_DIR_OUT, profile_type_primary); + shaping_stat_judge(line, 0, 0, 1, 30, 3000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file fclose(stat_file); -#endif } /*session1 match rule1 @@ -275,7 +357,7 @@ TEST(single_session, udp_diff_direction) stub_refresh_token_bucket(0); for (int i = 0; i < 20; i++) { polling_entry(ctx->thread_ctx[0].sp, ctx->thread_ctx[0].stat, &ctx->thread_ctx[0]); - stub_curr_time_inc(); + stub_curr_time_inc(STUB_TIME_INC_FOR_PACKET); } //10 out packets ASSERT_EQ(0, judge_packet_eq(&expec_tx_queue, actual_tx_queue, 10)); @@ -283,11 +365,15 @@ TEST(single_session, udp_diff_direction) ASSERT_EQ(0, judge_packet_eq(&expec_tx_queue, actual_tx_queue, 10)); ASSERT_TRUE(TAILQ_EMPTY(actual_tx_queue)); + /***********send stat data here********************/ + stub_curr_time_inc(STUB_TIME_INC_FOR_METRIC_SEND);//inc time to send metric + fieldstat_dynamic_passive_output(ctx->stat->instance);//send metric manualy + shaping_flow_free(sf); shaping_engine_destroy(ctx); stub_clear_matched_shaping_rules(); -#if 0 + /*******test statistics***********/ sleep(2);//wait telegraf to output char line[1024]; @@ -296,12 +382,14 @@ TEST(single_session, udp_diff_direction) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - shaping_stat_judge(line, 0, 0, 1, 20, 2000, 20, 2000, 0, 0, 20, 0, DIR_ROUTE_UP, profile_type_primary);//max latency is last 10 pkts - shaping_stat_judge(line, 0, 0, 1, 20, 2000, 20, 2000, 0, 0, 20, 0, DIR_ROUTE_DOWN, profile_type_primary); + //shaping_stat_judge(line, 0, 0, 1, 20, 2000, 0, 0, 20, 0, SHAPING_DIR_OUT, profile_type_primary); + shaping_stat_judge(line, 0, 0, 1, 20, 2000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency + + //shaping_stat_judge(line, 0, 0, 1, 20, 2000, 0, 0, 20, 0, SHAPING_DIR_IN, profile_type_primary); + shaping_stat_judge(line, 0, 0, 1, 20, 2000, 0, 0, -1, 0, SHAPING_DIR_IN, profile_type_primary);//TODO: latency fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file fclose(stat_file); -#endif } /*session1 match rule1, rule2, rule3 @@ -351,17 +439,20 @@ TEST(single_session, udp_multi_rules) //there are 3 rules, send one packet need 3 polling process, so 10 packets need 30 polling //even though invoke polling more than 30 times, there should be only 10 pkts be sent polling_entry(ctx->thread_ctx[0].sp, ctx->thread_ctx[0].stat, &ctx->thread_ctx[0]); - stub_curr_time_inc(); + stub_curr_time_inc(STUB_TIME_INC_FOR_PACKET); } ASSERT_EQ(0, judge_packet_eq(&expec_tx_queue, actual_tx_queue, 10)); ASSERT_TRUE(TAILQ_EMPTY(actual_tx_queue)); } + /***********send stat data here********************/ + stub_curr_time_inc(STUB_TIME_INC_FOR_METRIC_SEND);//inc time to send metric + fieldstat_dynamic_passive_output(ctx->stat->instance);//send metric manualy + shaping_flow_free(sf); shaping_engine_destroy(ctx); stub_clear_matched_shaping_rules(); -#if 0 /*******test statistics***********/ sleep(2);//wait telegraf to output char line[1024]; @@ -370,18 +461,20 @@ TEST(single_session, udp_multi_rules) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 0 - shaping_stat_judge(line, 0, 0, 1, 100, 10000, 100, 10000, 0, 0, 506, 0, DIR_ROUTE_UP, profile_type_primary);//max latency is last pkt + //shaping_stat_judge(line, 0, 0, 1, 100, 10000, 0, 0, 506, 0, SHAPING_DIR_OUT, profile_type_primary); + shaping_stat_judge(line, 0, 0, 1, 100, 10000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 1 - shaping_stat_judge(line, 1, 1, 2, 100, 10000, 100, 10000, 0, 0, 1, 0, DIR_ROUTE_UP, profile_type_primary);//latency of every queued pkt is 1 + //shaping_stat_judge(line, 1, 1, 2, 100, 10000, 0, 0, 1, 0, SHAPING_DIR_OUT, profile_type_primary);//latency of every queued pkt is 1 + shaping_stat_judge(line, 1, 1, 2, 100, 10000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 2 - shaping_stat_judge(line, 2, 2, 3, 100, 10000, 100, 10000, 0, 0, 90, 0, DIR_ROUTE_UP, profile_type_primary);//max latency is first queued pkt + //shaping_stat_judge(line, 2, 2, 3, 100, 10000, 0, 0, 90, 0, SHAPING_DIR_OUT, profile_type_primary);//max latency is first queued pkt + shaping_stat_judge(line, 2, 2, 3, 100, 10000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file fclose(stat_file); -#endif } /*session1 match rule1 @@ -423,17 +516,20 @@ TEST(single_session, udp_borrow) stub_refresh_token_bucket(2); for (int i = 0; i < 20; i++) {//even though invoke polling more than 10 times, there should be only 10 pkts be sent polling_entry(ctx->thread_ctx[0].sp, ctx->thread_ctx[0].stat, &ctx->thread_ctx[0]); - stub_curr_time_inc(); + stub_curr_time_inc(STUB_TIME_INC_FOR_PACKET); } ASSERT_EQ(0, judge_packet_eq(&expec_tx_queue, actual_tx_queue, 10)); ASSERT_TRUE(TAILQ_EMPTY(actual_tx_queue)); } + /***********send stat data here********************/ + stub_curr_time_inc(STUB_TIME_INC_FOR_METRIC_SEND);//inc time to send metric + fieldstat_dynamic_passive_output(ctx->stat->instance);//send metric manualy + shaping_flow_free(sf); shaping_engine_destroy(ctx); stub_clear_matched_shaping_rules(); -#if 0 /*******test statistics***********/ sleep(2);//wait telegraf to output char line[1024]; @@ -442,15 +538,16 @@ TEST(single_session, udp_borrow) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 1, primary - shaping_stat_judge(line, 1, 1, 1, 0, 0, 0, 0, 0, 0, 170, 0, DIR_ROUTE_UP, profile_type_primary); + //shaping_stat_judge(line, 1, 1, 1, 0, 0, 0, 0, 170, 0, SHAPING_DIR_OUT, profile_type_primary); + shaping_stat_judge(line, 1, 1, 1, 0, 0, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 2, borrow - shaping_stat_judge(line, 1, 2, 2, 100, 10000, 100, 10000, 0, 0, 170, 0, DIR_ROUTE_UP, profile_type_borrow); + //shaping_stat_judge(line, 1, 2, 2, 100, 10000, 0, 0, 170, 0, SHAPING_DIR_OUT, profile_type_borrow); + shaping_stat_judge(line, 1, 2, 2, 100, 10000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_borrow);//TODO: latency fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file fclose(stat_file); -#endif } /*session1 match rule1, session2 match rule2 @@ -512,7 +609,7 @@ TEST(two_session_diff_priority, udp_in_order) stub_refresh_token_bucket(1); for (int i = 0; i < 10; i++) { polling_entry(ctx->thread_ctx[0].sp, ctx->thread_ctx[0].stat, &ctx->thread_ctx[0]); - stub_curr_time_inc(); + stub_curr_time_inc(STUB_TIME_INC_FOR_PACKET); } ASSERT_EQ(0, judge_packet_eq(&expec_tx_queue2, actual_tx_queue, 10));//stream2 priority 1, first ASSERT_TRUE(TAILQ_EMPTY(actual_tx_queue)); @@ -523,18 +620,21 @@ TEST(two_session_diff_priority, udp_in_order) stub_refresh_token_bucket(1); for (int i = 0; i < 10; i++) { polling_entry(ctx->thread_ctx[0].sp, ctx->thread_ctx[0].stat, &ctx->thread_ctx[0]); - stub_curr_time_inc(); + stub_curr_time_inc(STUB_TIME_INC_FOR_PACKET); } ASSERT_EQ(0, judge_packet_eq(&expec_tx_queue1, actual_tx_queue, 10));//stream1 priority 2 ASSERT_TRUE(TAILQ_EMPTY(actual_tx_queue)); } + /***********send stat data here********************/ + stub_curr_time_inc(STUB_TIME_INC_FOR_METRIC_SEND);//inc time to send metric + fieldstat_dynamic_passive_output(ctx->stat->instance);//send metric manualy + shaping_flow_free(sf1); shaping_flow_free(sf2); shaping_engine_destroy(ctx); stub_clear_matched_shaping_rules(); -#if 0 /*******test statistics***********/ sleep(2);//wait telegraf to output char line[1024]; @@ -543,15 +643,16 @@ TEST(two_session_diff_priority, udp_in_order) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 0 - shaping_stat_judge(line, 0, 0, 2, 100, 10000, 100, 10000, 0, 0, 280, 0, DIR_ROUTE_UP, profile_type_primary);//max latency is every queued pkts + //shaping_stat_judge(line, 0, 0, 2, 100, 10000, 0, 0, 280, 0, SHAPING_DIR_OUT, profile_type_primary);//max latency is every queued pkts + shaping_stat_judge(line, 0, 0, 2, 100, 10000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 1 - shaping_stat_judge(line, 1, 1, 1, 100, 10000, 100, 10000, 0, 0, 90, 0, DIR_ROUTE_UP, profile_type_primary);//max latency is every queued pkts + //shaping_stat_judge(line, 1, 1, 1, 100, 10000, 0, 0, 90, 0, SHAPING_DIR_OUT, profile_type_primary);//max latency is every queued pkts + shaping_stat_judge(line, 1, 1, 1, 100, 10000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file fclose(stat_file); -#endif } /*session1 match rule1,rule2,rule4; session2 match rule3 @@ -626,7 +727,7 @@ TEST(two_session_diff_priority, udp_in_order_multi_rule) stub_refresh_token_bucket(4); for (int j = 0; j < 2; j++) { polling_entry(ctx->thread_ctx[0].sp, ctx->thread_ctx[0].stat, &ctx->thread_ctx[0]); - stub_curr_time_inc(); + stub_curr_time_inc(STUB_TIME_INC_FOR_PACKET); } ASSERT_TRUE(TAILQ_EMPTY(actual_tx_queue)); @@ -639,7 +740,7 @@ TEST(two_session_diff_priority, udp_in_order_multi_rule) stub_refresh_token_bucket(4); for (int i = 0; i < 10; i++) { polling_entry(ctx->thread_ctx[0].sp, ctx->thread_ctx[0].stat, &ctx->thread_ctx[0]); - stub_curr_time_inc(); + stub_curr_time_inc(STUB_TIME_INC_FOR_PACKET); } ASSERT_EQ(0, judge_packet_eq(&expec_tx_queue2, actual_tx_queue, 10));//stream2 priority 3, first ASSERT_TRUE(TAILQ_EMPTY(actual_tx_queue)); @@ -652,18 +753,21 @@ TEST(two_session_diff_priority, udp_in_order_multi_rule) stub_refresh_token_bucket(4); for (int i = 0; i < 30; i++) { polling_entry(ctx->thread_ctx[0].sp, ctx->thread_ctx[0].stat, &ctx->thread_ctx[0]); - stub_curr_time_inc(); + stub_curr_time_inc(STUB_TIME_INC_FOR_PACKET); } ASSERT_EQ(0, judge_packet_eq(&expec_tx_queue1, actual_tx_queue, 10)); ASSERT_TRUE(TAILQ_EMPTY(actual_tx_queue)); } + /***********send stat data here********************/ + stub_curr_time_inc(STUB_TIME_INC_FOR_METRIC_SEND);//inc time to send metric + fieldstat_dynamic_passive_output(ctx->stat->instance);//send metric manualy + shaping_flow_free(sf1); shaping_flow_free(sf2); shaping_engine_destroy(ctx); stub_clear_matched_shaping_rules(); -#if 0 /*******test statistics***********/ sleep(2);//wait telegraf to output char line[1024]; @@ -673,21 +777,24 @@ TEST(two_session_diff_priority, udp_in_order_multi_rule) memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - shaping_stat_judge(line, 1, 1, 1, 20, 2000, 20, 2000, 0, 0, 58, 0, DIR_ROUTE_UP, profile_type_primary);//profile_id 1, max latency is last pkt + //shaping_stat_judge(line, 1, 1, 1, 20, 2000, 0, 0, 58, 0, SHAPING_DIR_OUT, profile_type_primary);//profile_id 1, max latency is last pkt + shaping_stat_judge(line, 1, 1, 1, 20, 2000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - shaping_stat_judge(line, 2, 2, 2, 20, 2000, 20, 2000, 0, 0, 1, 0, DIR_ROUTE_UP, profile_type_primary);//profile_id 2, evevy queued pkt's latency is 1 + //shaping_stat_judge(line, 2, 2, 2, 20, 2000, 0, 0, 1, 0, SHAPING_DIR_OUT, profile_type_primary);//profile_id 2, evevy queued pkt's latency is 1 + shaping_stat_judge(line, 2, 2, 2, 20, 2000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - shaping_stat_judge(line, 4, 4, 4, 20, 2000, 20, 2000, 0, 0, 11, 0, DIR_ROUTE_UP, profile_type_primary);//profile_id 4, max latency is first queued pkt + //shaping_stat_judge(line, 4, 4, 4, 20, 2000, 0, 0, 11, 0, SHAPING_DIR_OUT, profile_type_primary);//profile_id 4, max latency is first queued pkt + shaping_stat_judge(line, 4, 4, 4, 20, 2000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - shaping_stat_judge(line, 3, 3, 3, 20, 2000, 20, 2000, 0, 0, 12, 0, DIR_ROUTE_UP, profile_type_primary);//profile_id 3, every queued pkt's latency is 12 + //shaping_stat_judge(line, 3, 3, 3, 20, 2000, 0, 0, 12, 0, SHAPING_DIR_OUT, profile_type_primary);//profile_id 3, every queued pkt's latency is 12 + shaping_stat_judge(line, 3, 3, 3, 20, 2000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file fclose(stat_file); -#endif } /*session1 match rule1 @@ -736,7 +843,7 @@ TEST(single_session_async, udp_tx_in_order) stub_refresh_token_bucket(0); for (int i = 0; i < 10; i++) {//异步获取token多发送了10个报文,补回token,不应发送报文 polling_entry(ctx->thread_ctx[0].sp, ctx->thread_ctx[0].stat, &ctx->thread_ctx[0]); - stub_curr_time_inc(); + stub_curr_time_inc(STUB_TIME_INC_FOR_PACKET); } ASSERT_TRUE(TAILQ_EMPTY(actual_tx_queue)); @@ -744,17 +851,20 @@ TEST(single_session_async, udp_tx_in_order) stub_refresh_token_bucket(0); for (int i = 0; i < 20; i++) {//even though invoke polling more than 10 times, there should be only 10 pkts be sent polling_entry(ctx->thread_ctx[0].sp, ctx->thread_ctx[0].stat, &ctx->thread_ctx[0]); - stub_curr_time_inc(); + stub_curr_time_inc(STUB_TIME_INC_FOR_PACKET); } ASSERT_EQ(0, judge_packet_eq(&expec_tx_queue, actual_tx_queue, 10)); ASSERT_TRUE(TAILQ_EMPTY(actual_tx_queue)); } + /***********send stat data here********************/ + stub_curr_time_inc(STUB_TIME_INC_FOR_METRIC_SEND);//inc time to send metric + fieldstat_dynamic_passive_output(ctx->stat->instance);//send metric manualy + shaping_flow_free(sf); shaping_engine_destroy(ctx); stub_clear_matched_shaping_rules(); -#if 0 /*******test statistics***********/ sleep(2);//wait telegraf to output char line[1024]; @@ -763,11 +873,11 @@ TEST(single_session_async, udp_tx_in_order) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - shaping_stat_judge(line, 0, 0, 1, 100, 10000, 100, 10000, 0, 0, 160, 0, DIR_ROUTE_UP, profile_type_primary);//max latency is last 10 pkts + //shaping_stat_judge(line, 0, 0, 1, 100, 10000, 0, 0, 160, 0, SHAPING_DIR_OUT, profile_type_primary);//max latency is last 10 pkts + shaping_stat_judge(line, 0, 0, 1, 100, 10000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file fclose(stat_file); -#endif } /*session1 match rule1 @@ -809,11 +919,14 @@ TEST(single_session_async, udp_close_before_async_exec) ASSERT_EQ(0, judge_packet_eq(&expec_tx_queue, actual_tx_queue, 10)); + /***********send stat data here********************/ + stub_curr_time_inc(STUB_TIME_INC_FOR_METRIC_SEND);//inc time to send metric + fieldstat_dynamic_passive_output(ctx->stat->instance);//send metric manualy + shaping_flow_free(sf); shaping_engine_destroy(ctx); stub_clear_matched_shaping_rules(); -#if 0 /*******test statistics***********/ sleep(2);//wait telegraf to output char line[1024]; @@ -822,11 +935,10 @@ TEST(single_session_async, udp_close_before_async_exec) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - shaping_stat_judge(line, 0, 0, 1, 10, 1000, 10, 1000, 0, 0, 0, 0, DIR_ROUTE_UP, profile_type_primary); + shaping_stat_judge(line, 0, 0, 1, 10, 1000, 0, 0, 0, 0, SHAPING_DIR_OUT, profile_type_primary); fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file fclose(stat_file); -#endif } /*session1 match rule1; session2 match rule2 @@ -890,7 +1002,7 @@ TEST(two_session_diff_priority_same_profile, udp_borrow_in_order) stub_refresh_token_bucket(2); for (int i = 0; i < 20; i++) {//even though invoke polling more than 10 times, there should be only 10 pkts be sent polling_entry(ctx->thread_ctx[0].sp, ctx->thread_ctx[0].stat, &ctx->thread_ctx[0]); - stub_curr_time_inc(); + stub_curr_time_inc(STUB_TIME_INC_FOR_PACKET); } ASSERT_EQ(0, judge_packet_eq(&expec_tx_queue2, actual_tx_queue, 10)); ASSERT_TRUE(TAILQ_EMPTY(actual_tx_queue)); @@ -900,18 +1012,21 @@ TEST(two_session_diff_priority_same_profile, udp_borrow_in_order) stub_refresh_token_bucket(2); for (int i = 0; i < 20; i++) {//even though invoke polling more than 10 times, there should be only 10 pkts be sent polling_entry(ctx->thread_ctx[0].sp, ctx->thread_ctx[0].stat, &ctx->thread_ctx[0]); - stub_curr_time_inc(); + stub_curr_time_inc(STUB_TIME_INC_FOR_PACKET); } ASSERT_EQ(0, judge_packet_eq(&expec_tx_queue1, actual_tx_queue, 10)); ASSERT_TRUE(TAILQ_EMPTY(actual_tx_queue)); } + /***********send stat data here********************/ + stub_curr_time_inc(STUB_TIME_INC_FOR_METRIC_SEND);//inc time to send metric + fieldstat_dynamic_passive_output(ctx->stat->instance);//send metric manualy + shaping_flow_free(sf1); shaping_flow_free(sf2); shaping_engine_destroy(ctx); stub_clear_matched_shaping_rules(); -#if 0 /*******test statistics***********/ sleep(2);//wait telegraf to output char line[1024]; @@ -920,18 +1035,20 @@ TEST(two_session_diff_priority_same_profile, udp_borrow_in_order) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 1, primary - shaping_stat_judge(line, 1, 1, 1, 0, 0, 0, 0, 0, 0, 470, 0, DIR_ROUTE_UP, profile_type_primary); + //shaping_stat_judge(line, 1, 1, 1, 0, 0, 0, 0, 470, 0, SHAPING_DIR_OUT, profile_type_primary); + shaping_stat_judge(line, 1, 1, 1, 0, 0, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 2, borrow - shaping_stat_judge(line, 1, 2, 2, 100, 10000, 100, 10000, 0, 0, 470, 0, DIR_ROUTE_UP, profile_type_borrow); + //shaping_stat_judge(line, 1, 2, 2, 100, 10000, 0, 0, 470, 0, SHAPING_DIR_OUT, profile_type_borrow); + shaping_stat_judge(line, 1, 2, 2, 100, 10000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_borrow);//TODO: latency ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 2, primary - shaping_stat_judge(line, 2, 2, 1, 100, 10000, 100, 10000, 0, 0, 190, 0, DIR_ROUTE_UP, profile_type_primary); + //shaping_stat_judge(line, 2, 2, 1, 100, 10000, 0, 0, 190, 0, SHAPING_DIR_OUT, profile_type_primary); + shaping_stat_judge(line, 2, 2, 1, 100, 10000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file fclose(stat_file); -#endif } /*session1 match rule1; session2 match rule1 @@ -984,18 +1101,21 @@ TEST(two_session_same_rule, udp_tx_in_order) stub_refresh_token_bucket(1); for (int i = 0; i < 20; i++) {//even though invoke polling more than 10 times, there should be only 10 pkts be sent polling_entry(ctx->thread_ctx[0].sp, ctx->thread_ctx[0].stat, &ctx->thread_ctx[0]); - stub_curr_time_inc(); + stub_curr_time_inc(STUB_TIME_INC_FOR_PACKET); } ASSERT_EQ(0, judge_packet_eq(&expec_tx_queue, actual_tx_queue, 10)); ASSERT_TRUE(TAILQ_EMPTY(actual_tx_queue)); } + /***********send stat data here********************/ + stub_curr_time_inc(STUB_TIME_INC_FOR_METRIC_SEND);//inc time to send metric + fieldstat_dynamic_passive_output(ctx->stat->instance);//send metric manualy + shaping_flow_free(sf1); shaping_flow_free(sf2); shaping_engine_destroy(ctx); stub_clear_matched_shaping_rules(); -#if 0 /*******test statistics***********/ sleep(2);//wait telegraf to output char line[1024]; @@ -1004,11 +1124,11 @@ TEST(two_session_same_rule, udp_tx_in_order) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - shaping_stat_judge(line, 1, 1, 1, 200, 20000, 200, 20000, 0, 0, 370, 0, DIR_ROUTE_UP, profile_type_primary); + //shaping_stat_judge(line, 1, 1, 1, 200, 20000, 0, 0, 370, 0, SHAPING_DIR_OUT, profile_type_primary); + shaping_stat_judge(line, 1, 1, 1, 200, 20000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file fclose(stat_file); -#endif } /*session1 match rule1; session2 match rule2 @@ -1037,7 +1157,7 @@ TEST(two_session_diff_priority_same_profile, udp_random_tx_in_order) int profile_id[][MAX_REF_PROFILE] = {{0}, {0}}; int stream1_pkt_num = 0; int stream2_pkt_num = 0; - struct timespec curr_time; + time_t curr_time; TAILQ_INIT(&expec_tx_queue1); @@ -1065,8 +1185,8 @@ TEST(two_session_diff_priority_same_profile, udp_random_tx_in_order) ASSERT_EQ(0, judge_packet_eq(&expec_tx_queue1, actual_tx_queue, 1)); } - clock_gettime(CLOCK_MONOTONIC, &curr_time); - srand(curr_time.tv_sec); + time(&curr_time); + srand(curr_time); for (int i = 0; i < 99; i++) { if (rand() % 2 == 0) { send_packets(&ctx->thread_ctx[0], sf1, 1, 100, SHAPING_DIR_OUT, &expec_tx_queue1, 1, 0); @@ -1101,12 +1221,15 @@ TEST(two_session_diff_priority_same_profile, udp_random_tx_in_order) ASSERT_TRUE(TAILQ_EMPTY(actual_tx_queue)); + /***********send stat data here********************/ + stub_curr_time_inc(STUB_TIME_INC_FOR_METRIC_SEND);//inc time to send metric + fieldstat_dynamic_passive_output(ctx->stat->instance);//send metric manualy + shaping_flow_free(sf1); shaping_flow_free(sf2); shaping_engine_destroy(ctx); stub_clear_matched_shaping_rules(); -#if 0 /*******test statistics***********/ sleep(2);//wait telegraf to output char line[1024]; @@ -1115,17 +1238,14 @@ TEST(two_session_diff_priority_same_profile, udp_random_tx_in_order) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - shaping_stat_judge(line, 1, 0, 1, stream1_pkt_num, stream1_pkt_num*100, - stream1_pkt_num, stream1_pkt_num*100, 0, 0, -1, 0, DIR_ROUTE_UP, profile_type_primary);//can't predict a certain latency cause of random + shaping_stat_judge(line, 1, 0, 1, stream1_pkt_num, stream1_pkt_num*100, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//can't predict a certain latency cause of random ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - shaping_stat_judge(line, 2, 0, 2, stream2_pkt_num, stream2_pkt_num*100, - stream2_pkt_num, stream2_pkt_num*100, 0, 0, -1, 0, DIR_ROUTE_UP, profile_type_primary);//can't predict a certain latency cause of random + shaping_stat_judge(line, 2, 0, 2, stream2_pkt_num, stream2_pkt_num*100, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//can't predict a certain latency cause of random fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file fclose(stat_file); -#endif } /*session1 match rule1 @@ -1165,15 +1285,18 @@ TEST(statistics, udp_drop_pkt) while (!TAILQ_EMPTY(&expec_tx_queue)) { stub_refresh_token_bucket(0); polling_entry(ctx->thread_ctx[0].sp, ctx->thread_ctx[0].stat, &ctx->thread_ctx[0]); - stub_curr_time_inc(); + stub_curr_time_inc(STUB_TIME_INC_FOR_PACKET); ASSERT_EQ(0, judge_packet_eq(&expec_tx_queue, actual_tx_queue, 1)); } + /***********send stat data here********************/ + stub_curr_time_inc(STUB_TIME_INC_FOR_METRIC_SEND);//inc time to send metric + fieldstat_dynamic_passive_output(ctx->stat->instance);//send metric manualy + shaping_flow_free(sf); shaping_engine_destroy(ctx); stub_clear_matched_shaping_rules(); -#if 0 /*******test statistics***********/ sleep(2);//wait telegraf to output char line[1024]; @@ -1182,11 +1305,11 @@ TEST(statistics, udp_drop_pkt) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - shaping_stat_judge(line, 0, 0, 1, 210, 21000, 110, 11000, 100, 0, 200, 0, DIR_ROUTE_UP, profile_type_primary);//every queued pkt's latency is 200 + //shaping_stat_judge(line, 0, 0, 1, SHAPING_SESSION_QUEUE_LEN+10, (SHAPING_SESSION_QUEUE_LEN+10)*100, 100, 0, 228, 0, SHAPING_DIR_OUT, profile_type_primary);//every queued pkt's latency is max + shaping_stat_judge(line, 0, 0, 1, SHAPING_SESSION_QUEUE_LEN+10, (SHAPING_SESSION_QUEUE_LEN+10)*100, 100, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file fclose(stat_file); -#endif } /*session1 match rule1 @@ -1215,16 +1338,15 @@ TEST(statistics, udp_queueing_pkt) stub_set_token_bucket_avl_per_sec(0, 1000, SHAPING_DIR_OUT); actual_tx_queue = stub_get_tx_queue(); shaper_rules_update(&ctx->thread_ctx[0], sf, rule_id, 1); - - /*******packets, OP_STATE_DATA***********/ + /*******send packets***********/ send_packets(&ctx->thread_ctx[0], sf, 100, 100, SHAPING_DIR_OUT, &expec_tx_queue, 1, 0); -#if 0 + /***********send stat data here********************/ - stub_shaper_stat_send(0); + stub_curr_time_inc(STUB_TIME_INC_FOR_METRIC_SEND);//inc time to send metric + fieldstat_dynamic_passive_output(ctx->stat->instance);//send metric manualy sleep(2);//wait telegraf generate metric -#endif //first 10 packets ASSERT_EQ(0, judge_packet_eq(&expec_tx_queue, actual_tx_queue, 10)); @@ -1233,15 +1355,18 @@ TEST(statistics, udp_queueing_pkt) while (!TAILQ_EMPTY(&expec_tx_queue)) {//last 90 delay packets stub_refresh_token_bucket(0); polling_entry(ctx->thread_ctx[0].sp, ctx->thread_ctx[0].stat, &ctx->thread_ctx[0]); - stub_curr_time_inc(); + stub_curr_time_inc(STUB_TIME_INC_FOR_PACKET); ASSERT_EQ(0, judge_packet_eq(&expec_tx_queue, actual_tx_queue, 1)); } + /***********send stat data here********************/ + stub_curr_time_inc(STUB_TIME_INC_FOR_METRIC_SEND);//inc time to send metric + fieldstat_dynamic_passive_output(ctx->stat->instance);//send metric manualy + shaping_flow_free(sf); shaping_engine_destroy(ctx); stub_clear_matched_shaping_rules(); -#if 0 /*******test statistics***********/ sleep(2);//wait telegraf to output char line[1024]; @@ -1251,20 +1376,21 @@ TEST(statistics, udp_queueing_pkt) memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//stat data first sent - shaping_stat_judge(line, 0, 0, 1, 100, 10000, 10, 1000, 0, 90, 0, 1, DIR_ROUTE_UP, profile_type_primary); + //shaping_stat_judge(line, 0, 0, 1, 10, 1000, 0, 90, 0, 1, SHAPING_DIR_OUT, profile_type_primary); + shaping_stat_judge(line, 0, 0, 1, 10, 1000, 0, 90, -1, 1, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//stat data last sent - shaping_stat_judge(line, 0, 0, 1, 0, 0, 90, 9000, 0, 0, 90, 0, DIR_ROUTE_UP, profile_type_primary); + //shaping_stat_judge(line, 0, 0, 1, 90, 9000, 0, 0, 90, 0, SHAPING_DIR_OUT, profile_type_primary); + shaping_stat_judge(line, 0, 0, 1, 90, 9000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file fclose(stat_file); -#endif } int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); - //testing::GTEST_FLAG(filter) = "two_session_diff_priority.udp_in_order"; + //testing::GTEST_FLAG(filter) = "statistics.udp_queueing_pkt"; return RUN_ALL_TESTS(); } \ No newline at end of file diff --git a/shaping/test/gtest_shaper_bak.cpp b/shaping/test/gtest_shaper_bak.cpp deleted file mode 100644 index a947a8b..0000000 --- a/shaping/test/gtest_shaper_bak.cpp +++ /dev/null @@ -1,234 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include "stub.h" - -#define SHAPING_STAT_FILE_NAME "/tmp/shaping_metrics.json" -#define SHAPING_SESSION_QUEUE_LEN 100 -#define SHAPING_SESSIONS_LIMIT_PER_AVL 1000 - -char profile_type_primary[] = "primary"; -char profile_type_borrow[] = "borrow"; - - -using namespace std; - -static int judge_packet_eq(struct stub_pkt_queue *expec_queue, struct stub_pkt_queue *actual_queue, int num) -{ - struct stub_packet_node *expec_pkt_node; - struct stub_packet_node *actual_pkt_node; - - for (int i = 0; i < num; i++) { - if(TAILQ_EMPTY(actual_queue)) { - return -1; - } - expec_pkt_node = TAILQ_FIRST(expec_queue); - actual_pkt_node = TAILQ_FIRST(actual_queue); - if (expec_pkt_node->raw_packet != actual_pkt_node->raw_packet) { - return -2; - } - - TAILQ_REMOVE(expec_queue, expec_pkt_node, node); - TAILQ_REMOVE(actual_queue, actual_pkt_node, node); - free(expec_pkt_node->raw_packet); - free(expec_pkt_node); - free(actual_pkt_node); - } - - return 0; -} - -static struct stub_packet* packet_new(unsigned long long income_time, unsigned int length, unsigned char dir) -{ - struct stub_packet *packet; - - packet = (struct stub_packet*)calloc(1, sizeof(struct stub_packet)); - packet->income_time = income_time; - packet->length = length; - packet->direction = dir; - - return packet; -} - -static struct stub_packet_node* packet_node_new(stub_packet *packet) -{ - struct stub_packet_node *pkt_node; - - pkt_node = (struct stub_packet_node*)calloc(1, sizeof(struct stub_packet_node)); - pkt_node->raw_packet = packet; - - return pkt_node; -} - -static void send_packets(struct streaminfo *stream, void **pme, int pkt_num, int pkt_len, - unsigned char dir, struct stub_pkt_queue *expec_tx_queue, int polling_times, - int is_tcp, int is_pure_control) -{ - struct stub_packet_node *pkt_node; - struct stub_packet *packet; - unsigned long long time; - char ret; - - stream->threadnum = 0;//just 1 thread for test!!! - - for (int i = 0; i < pkt_num; i++) { - time = stub_curr_time_get(); - packet = packet_new(time, pkt_len, dir); - if (expec_tx_queue) { - pkt_node = packet_node_new(packet); - TAILQ_INSERT_TAIL(expec_tx_queue, pkt_node, node); - } - - time++; - - stream->routedir = dir; - stream->hash_index = pkt_len;//just for stub test!!!!! use hash_index to store pkt_len - if (is_tcp) { - stream->ptcpdetail->clientpktnum++; - if (is_pure_control) { - stream->ptcpdetail->pdata = NULL; - } else { - stream->ptcpdetail->pdata = (void*)0x1234;//just for stub test, will not access this pointer - } - } else { - stream->pudpdetail->clientpktnum++; - } - - if (is_tcp) { - ret = tcp_allpkt_raw_process(stream, NULL, packet, pme); - } else { - ret = udp_raw_process(stream, NULL, packet, pme); - } - - if (ret == APP_STATE_GIVEME) { - if (!packet->detained_flag) { - stub_send_packet(packet); - } - } - - for (int j = 0; j < polling_times; j++) { - polling_entry(NULL, NULL, 0, NULL); - } - - stub_curr_time_inc(); - } -} - -static void stream_state_change_udp(struct streaminfo *stream, void **pme, unsigned char state) -{ - stream->opstate = state; - //udp_process(stream, pme, 0, NULL);//just change state, no packet - if (state == OP_STATE_PENDING) { - stub_stream_bridge_sync_cb_invoke(stream); - } else if(state == OP_STATE_CLOSE) { - stub_stream_bridge_free_cb_invoke(stream); - } - - udp_raw_process(stream, NULL, NULL, pme);//just change state, no packet - - return; -} - -static void stream_state_change_tcp(struct streaminfo *stream, void **pme, unsigned char state) -{ - stream->pktstate = state; - //tcp_allpkt_process(stream, pme, 0, NULL);//just change state, no packet - if (state == OP_STATE_PENDING) { - stub_stream_bridge_sync_cb_invoke(stream); - } else if(state == OP_STATE_CLOSE) { - stub_stream_bridge_free_cb_invoke(stream); - } - - tcp_allpkt_raw_process(stream, NULL, NULL, pme);//just change state, no packet - - return; -} - -static void shaping_stat_judge(char *file_line, int rule_id, int profile_id, int priority, - unsigned long long rx_pkts, unsigned long long rx_bytes, - unsigned long long tx_pkts, unsigned long long tx_bytes, - unsigned long long drop_pkts, long long queue_len, long long max_latency, - int queueing_sessions, unsigned char direction, char profile_type[]) -{ - cJSON *json = NULL; - cJSON *tmp_obj = NULL; - char attr_name[32] = {0}; - - json = cJSON_Parse(file_line); - ASSERT_TRUE(json != NULL); - - tmp_obj = cJSON_GetObjectItem(json, "rule_id"); - ASSERT_TRUE(tmp_obj != NULL); - EXPECT_EQ(rule_id, atoi(tmp_obj->valuestring)); - - tmp_obj = cJSON_GetObjectItem(json, "profile_id"); - ASSERT_TRUE(tmp_obj != NULL); - EXPECT_EQ(profile_id, atoi(tmp_obj->valuestring)); - - tmp_obj = cJSON_GetObjectItem(json, "priority"); - ASSERT_TRUE(tmp_obj != NULL); - EXPECT_EQ(priority, atoi(tmp_obj->valuestring)); - - tmp_obj = cJSON_GetObjectItem(json, "profile_type"); - ASSERT_TRUE(tmp_obj != NULL); - EXPECT_STREQ(tmp_obj->valuestring, profile_type); - - tmp_obj = cJSON_GetObjectItem(json, "queueing_sessions"); - ASSERT_TRUE(tmp_obj != NULL); - EXPECT_EQ(queueing_sessions, tmp_obj->valueint); - - snprintf(attr_name, sizeof(attr_name), "%s_rx_pkts", direction == DIR_ROUTE_UP ? "out" : "in"); - tmp_obj = cJSON_GetObjectItem(json, attr_name); - ASSERT_TRUE(tmp_obj != NULL); - EXPECT_EQ(rx_pkts, tmp_obj->valueint); - - snprintf(attr_name, sizeof(attr_name), "%s_rx_bytes", direction == DIR_ROUTE_UP ? "out" : "in"); - tmp_obj = cJSON_GetObjectItem(json, attr_name); - ASSERT_TRUE(tmp_obj != NULL); - EXPECT_EQ(rx_bytes, tmp_obj->valueint); - - snprintf(attr_name, sizeof(attr_name), "%s_tx_pkts", direction == DIR_ROUTE_UP ? "out" : "in"); - tmp_obj = cJSON_GetObjectItem(json, attr_name); - ASSERT_TRUE(tmp_obj != NULL); - EXPECT_EQ(tx_pkts, tmp_obj->valueint); - - snprintf(attr_name, sizeof(attr_name), "%s_tx_bytes", direction == DIR_ROUTE_UP ? "out" : "in"); - tmp_obj = cJSON_GetObjectItem(json, attr_name); - ASSERT_TRUE(tmp_obj != NULL); - EXPECT_EQ(tx_bytes, tmp_obj->valueint); - - snprintf(attr_name, sizeof(attr_name), "%s_drop_pkts", direction == DIR_ROUTE_UP ? "out" : "in"); - tmp_obj = cJSON_GetObjectItem(json, attr_name); - ASSERT_TRUE(tmp_obj != NULL); - EXPECT_EQ(drop_pkts, tmp_obj->valueint); - - if (max_latency != -1) { - snprintf(attr_name, sizeof(attr_name), "%s_max_latency_us", direction == DIR_ROUTE_UP ? "out" : "in"); - tmp_obj = cJSON_GetObjectItem(json, attr_name); - ASSERT_TRUE(tmp_obj != NULL); - EXPECT_EQ(max_latency, tmp_obj->valueint); - } - - snprintf(attr_name, sizeof(attr_name), "%s_queue_len", direction == DIR_ROUTE_UP ? "out" : "in"); - tmp_obj = cJSON_GetObjectItem(json, attr_name); - ASSERT_TRUE(tmp_obj != NULL); - EXPECT_EQ(queue_len, tmp_obj->valueint); - - cJSON_Delete(json); - - return; -} - - - - -int main(int argc, char **argv) -{ - testing::InitGoogleTest(&argc, argv); - //testing::GTEST_FLAG(filter) = "single_session.udp_diff_direction"; - return RUN_ALL_TESTS(); -} \ No newline at end of file diff --git a/shaping/test/stub.cpp b/shaping/test/stub.cpp index 2bf4a8f..9d91b7e 100644 --- a/shaping/test/stub.cpp +++ b/shaping/test/stub.cpp @@ -158,9 +158,9 @@ struct stub_pkt_queue* stub_get_tx_queue() return &tx_queue; } -void stub_curr_time_inc() +void stub_curr_time_inc(unsigned long long time_ns) { - curr_time += 1000; + curr_time += time_ns; return; } diff --git a/shaping/test/stub.h b/shaping/test/stub.h index e394da3..e0702a2 100644 --- a/shaping/test/stub.h +++ b/shaping/test/stub.h @@ -9,6 +9,9 @@ #define STUB_MAAT_SHAPING_RULE_TABLE_ID 0 #define STUB_MAAT_SHAPING_PROFILE_TABLE_ID 1 +#define STUB_TIME_INC_FOR_PACKET 1000 +#define STUB_TIME_INC_FOR_METRIC_SEND 1000000 + struct stub_packet { unsigned char direction; unsigned char pure_control; @@ -37,7 +40,7 @@ struct stub_pkt_queue* stub_get_tx_queue(); int stub_AQM_drop_packet(int queue_len, unsigned long long income_time); -void stub_curr_time_inc(); +void stub_curr_time_inc(unsigned long long time_ns); unsigned long long stub_curr_time_get(); void stub_init(); diff --git a/shaping/test/test_conf/main.conf b/shaping/test/test_conf/main.conf index e134a70..78d458f 100644 --- a/shaping/test/test_conf/main.conf +++ b/shaping/test/test_conf/main.conf @@ -28,9 +28,11 @@ SWARMKV_CLUSTER_ANNOUNCE_PORT=8501 SWARMKV_HEALTH_CHECK_PORT=0 SWARMKV_HEALTH_CHECK_ANNOUNCE_PORT=1111 -#[METRIC] -#TELEGRAF_IP="127.0.0.1" -#TELEGRAF_PORT=6667 +[METRIC] +FIELDSTAT_OUTPUT_INTERVAL_MS=999999000 +FIELDSTAT_ENABLE_BACKGRUND_THREAD=0 +TELEGRAF_IP="127.0.0.1" +TELEGRAF_PORT=6667 [CONFIG] #PROFILE_QUEUE_LEN_PER_PRIORITY_MAX=128 -- cgit v1.2.3 From 9d5fd0099aa04d253ef08b4c7606ad3fa4a914ef Mon Sep 17 00:00:00 2001 From: liuchang Date: Tue, 28 Mar 2023 02:42:24 +0000 Subject: adapt get interface for fieldstat3, for metric max_latency --- shaping/src/shaper_stat.cpp | 39 ++++---------------- shaping/test/gtest_shaper.cpp | 82 +++++++++++++++---------------------------- 2 files changed, 35 insertions(+), 86 deletions(-) (limited to 'shaping/src/shaper_stat.cpp') diff --git a/shaping/src/shaper_stat.cpp b/shaping/src/shaper_stat.cpp index 878daa0..192fddd 100644 --- a/shaping/src/shaper_stat.cpp +++ b/shaping/src/shaper_stat.cpp @@ -212,46 +212,21 @@ void shaper_stat_queueing_pkt_dec(struct shaping_stat *stat, int rule_id, int pr void shaper_stat_max_latency_update(struct shaping_stat *stat, int rule_id, int profile_id, int priority, unsigned char direction, unsigned long long latency, int profile_type, int thread_id) -{//TODO -#if 0 - struct shaping_stat_data *s_stat_data = NULL; - - s_stat_data = shaper_stat_ins_get(stat_hashtbl, rule_id, profile_id, priority, profile_type); +{ + long long old_latency; shaper_stat_tags_build(rule_id, profile_id, priority, profile_type); if (direction == SHAPING_DIR_IN) { - if (latency > s_stat_data->incoming.max_latency) { - s_stat_data->incoming.max_latency = latency; + old_latency = fieldstat_dynamic_table_metric_value_get(stat->instance, stat->table_id, stat->column_ids[IN_MAX_LATENCY_IDX], "shaping_metric_row", tags, TAG_IDX_MAX, thread_id); + if (latency > old_latency) { fieldstat_dynamic_table_metric_value_set(stat->instance, stat->table_id, stat->column_ids[IN_MAX_LATENCY_IDX], "shaping_metric_row", latency, tags, TAG_IDX_MAX, thread_id); } } else { - if (latency > s_stat_data->outgoing.max_latency) { - s_stat_data->outgoing.max_latency = latency; + old_latency = fieldstat_dynamic_table_metric_value_get(stat->instance, stat->table_id, stat->column_ids[OUT_MAX_LATENCY_IDX], "shaping_metric_row", tags, TAG_IDX_MAX, thread_id); + if (latency > old_latency) { fieldstat_dynamic_table_metric_value_set(stat->instance, stat->table_id, stat->column_ids[OUT_MAX_LATENCY_IDX], "shaping_metric_row", latency, tags, TAG_IDX_MAX, thread_id); } } -#endif - - shaper_stat_tags_build(rule_id, profile_id, priority, profile_type); - if (direction == SHAPING_DIR_IN) { - fieldstat_dynamic_table_metric_value_set(stat->instance, stat->table_id, stat->column_ids[IN_MAX_LATENCY_IDX], "shaping_metric_row", -1, tags, TAG_IDX_MAX, thread_id); - } else { - fieldstat_dynamic_table_metric_value_set(stat->instance, stat->table_id, stat->column_ids[OUT_MAX_LATENCY_IDX], "shaping_metric_row", -1, tags, TAG_IDX_MAX, thread_id); - } - return; -} - -#if 0 -/*********just for self test stub****************/ -void stub_shaper_stat_send(int thread_seq) -{ - struct shaping_stat_data *s, *tmp; - - HASH_ITER(hh, g_rt_para.stat[thread_seq]->stat_hashtbl, s, tmp) { - shaper_stat_data_send(g_rt_para.stat[thread_seq], s); - } return; -} -/************************************************/ -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/shaping/test/gtest_shaper.cpp b/shaping/test/gtest_shaper.cpp index ada50cb..23d91a9 100644 --- a/shaping/test/gtest_shaper.cpp +++ b/shaping/test/gtest_shaper.cpp @@ -226,8 +226,7 @@ TEST(single_session, udp_tx_in_order) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - //shaping_stat_judge(line, 0, 0, 1, 100, 10000, 0, 0, 170, 0, SHAPING_DIR_OUT, profile_type_primary);//max latency is last 10 pkts - shaping_stat_judge(line, 0, 0, 1, 100, 10000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency + shaping_stat_judge(line, 0, 0, 1, 100, 10000, 0, 0, 170, 0, SHAPING_DIR_OUT, profile_type_primary);//max latency is last 10 pkts fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file fclose(stat_file); @@ -307,8 +306,7 @@ TEST(single_session, tcp_tx_in_order) shaping_stat_judge(line, 0, 0, 1, 10, 1000, 0, 30, 0, 1, SHAPING_DIR_OUT, profile_type_primary); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - //shaping_stat_judge(line, 0, 0, 1, 30, 3000, 0, 0, 30 + (STUB_TIME_INC_FOR_METRIC_SEND / 1000), 0, SHAPING_DIR_OUT, profile_type_primary); - shaping_stat_judge(line, 0, 0, 1, 30, 3000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency + shaping_stat_judge(line, 0, 0, 1, 30, 3000, 0, 0, 30 + (STUB_TIME_INC_FOR_METRIC_SEND / 1000), 0, SHAPING_DIR_OUT, profile_type_primary); fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file @@ -382,11 +380,9 @@ TEST(single_session, udp_diff_direction) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - //shaping_stat_judge(line, 0, 0, 1, 20, 2000, 0, 0, 20, 0, SHAPING_DIR_OUT, profile_type_primary); - shaping_stat_judge(line, 0, 0, 1, 20, 2000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency + shaping_stat_judge(line, 0, 0, 1, 20, 2000, 0, 0, 20, 0, SHAPING_DIR_OUT, profile_type_primary); - //shaping_stat_judge(line, 0, 0, 1, 20, 2000, 0, 0, 20, 0, SHAPING_DIR_IN, profile_type_primary); - shaping_stat_judge(line, 0, 0, 1, 20, 2000, 0, 0, -1, 0, SHAPING_DIR_IN, profile_type_primary);//TODO: latency + shaping_stat_judge(line, 0, 0, 1, 20, 2000, 0, 0, 20, 0, SHAPING_DIR_IN, profile_type_primary); fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file fclose(stat_file); @@ -461,16 +457,13 @@ TEST(single_session, udp_multi_rules) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 0 - //shaping_stat_judge(line, 0, 0, 1, 100, 10000, 0, 0, 506, 0, SHAPING_DIR_OUT, profile_type_primary); - shaping_stat_judge(line, 0, 0, 1, 100, 10000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency + shaping_stat_judge(line, 0, 0, 1, 100, 10000, 0, 0, 506, 0, SHAPING_DIR_OUT, profile_type_primary); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 1 - //shaping_stat_judge(line, 1, 1, 2, 100, 10000, 0, 0, 1, 0, SHAPING_DIR_OUT, profile_type_primary);//latency of every queued pkt is 1 - shaping_stat_judge(line, 1, 1, 2, 100, 10000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency + shaping_stat_judge(line, 1, 1, 2, 100, 10000, 0, 0, 1, 0, SHAPING_DIR_OUT, profile_type_primary);//latency of every queued pkt is 1 ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 2 - //shaping_stat_judge(line, 2, 2, 3, 100, 10000, 0, 0, 90, 0, SHAPING_DIR_OUT, profile_type_primary);//max latency is first queued pkt - shaping_stat_judge(line, 2, 2, 3, 100, 10000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency + shaping_stat_judge(line, 2, 2, 3, 100, 10000, 0, 0, 90, 0, SHAPING_DIR_OUT, profile_type_primary);//max latency is first queued pkt fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file @@ -538,12 +531,10 @@ TEST(single_session, udp_borrow) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 1, primary - //shaping_stat_judge(line, 1, 1, 1, 0, 0, 0, 0, 170, 0, SHAPING_DIR_OUT, profile_type_primary); - shaping_stat_judge(line, 1, 1, 1, 0, 0, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency + shaping_stat_judge(line, 1, 1, 1, 0, 0, 0, 0, 170, 0, SHAPING_DIR_OUT, profile_type_primary); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 2, borrow - //shaping_stat_judge(line, 1, 2, 2, 100, 10000, 0, 0, 170, 0, SHAPING_DIR_OUT, profile_type_borrow); - shaping_stat_judge(line, 1, 2, 2, 100, 10000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_borrow);//TODO: latency + shaping_stat_judge(line, 1, 2, 2, 100, 10000, 0, 0, 170, 0, SHAPING_DIR_OUT, profile_type_borrow); fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file @@ -614,16 +605,13 @@ TEST(single_session, udp_borrow_same_priority_9) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 1, primary - //shaping_stat_judge(line, 1, 1, 9, 0, 0, 0, 0, 170, 0, SHAPING_DIR_OUT, profile_type_primary); - shaping_stat_judge(line, 1, 1, 9, 0, 0, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency - - ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 1, primary - //shaping_stat_judge(line, 1, 1, 9, 0, 0, 0, 0, 170, 0, SHAPING_DIR_OUT, profile_type_primary); - shaping_stat_judge(line, 1, 2, 9, 0, 0, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_borrow);//TODO: latency + shaping_stat_judge(line, 1, 1, 9, 0, 0, 0, 0, 170, 0, SHAPING_DIR_OUT, profile_type_primary); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 2, borrow - //shaping_stat_judge(line, 1, 2, 9, 100, 10000, 0, 0, 170, 0, SHAPING_DIR_OUT, profile_type_borrow); - shaping_stat_judge(line, 1, 3, 9, 100, 10000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_borrow);//TODO: latency + shaping_stat_judge(line, 1, 2, 9, 0, 0, 0, 0, 170, 0, SHAPING_DIR_OUT, profile_type_borrow); + + ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 3, borrow + shaping_stat_judge(line, 1, 3, 9, 100, 10000, 0, 0, 170, 0, SHAPING_DIR_OUT, profile_type_borrow); fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file @@ -723,12 +711,10 @@ TEST(two_session_diff_priority, udp_in_order) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 0 - //shaping_stat_judge(line, 0, 0, 2, 100, 10000, 0, 0, 280, 0, SHAPING_DIR_OUT, profile_type_primary);//max latency is every queued pkts - shaping_stat_judge(line, 0, 0, 2, 100, 10000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency + shaping_stat_judge(line, 0, 0, 2, 100, 10000, 0, 0, 280, 0, SHAPING_DIR_OUT, profile_type_primary);//max latency is every queued pkts ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 1 - //shaping_stat_judge(line, 1, 1, 1, 100, 10000, 0, 0, 90, 0, SHAPING_DIR_OUT, profile_type_primary);//max latency is every queued pkts - shaping_stat_judge(line, 1, 1, 1, 100, 10000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency + shaping_stat_judge(line, 1, 1, 1, 100, 10000, 0, 0, 90, 0, SHAPING_DIR_OUT, profile_type_primary);//max latency is every queued pkts fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file @@ -857,20 +843,16 @@ TEST(two_session_diff_priority, udp_in_order_multi_rule) memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - //shaping_stat_judge(line, 1, 1, 1, 20, 2000, 0, 0, 58, 0, SHAPING_DIR_OUT, profile_type_primary);//profile_id 1, max latency is last pkt - shaping_stat_judge(line, 1, 1, 1, 20, 2000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency + shaping_stat_judge(line, 1, 1, 1, 20, 2000, 0, 0, 58, 0, SHAPING_DIR_OUT, profile_type_primary);//profile_id 1, max latency is last pkt ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - //shaping_stat_judge(line, 2, 2, 2, 20, 2000, 0, 0, 1, 0, SHAPING_DIR_OUT, profile_type_primary);//profile_id 2, evevy queued pkt's latency is 1 - shaping_stat_judge(line, 2, 2, 2, 20, 2000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency + shaping_stat_judge(line, 2, 2, 2, 20, 2000, 0, 0, 1, 0, SHAPING_DIR_OUT, profile_type_primary);//profile_id 2, evevy queued pkt's latency is 1 ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - //shaping_stat_judge(line, 4, 4, 4, 20, 2000, 0, 0, 11, 0, SHAPING_DIR_OUT, profile_type_primary);//profile_id 4, max latency is first queued pkt - shaping_stat_judge(line, 4, 4, 4, 20, 2000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency + shaping_stat_judge(line, 4, 4, 4, 20, 2000, 0, 0, 11, 0, SHAPING_DIR_OUT, profile_type_primary);//profile_id 4, max latency is first queued pkt ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - //shaping_stat_judge(line, 3, 3, 3, 20, 2000, 0, 0, 12, 0, SHAPING_DIR_OUT, profile_type_primary);//profile_id 3, every queued pkt's latency is 12 - shaping_stat_judge(line, 3, 3, 3, 20, 2000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency + shaping_stat_judge(line, 3, 3, 3, 20, 2000, 0, 0, 12, 0, SHAPING_DIR_OUT, profile_type_primary);//profile_id 3, every queued pkt's latency is 12 fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file @@ -953,8 +935,7 @@ TEST(single_session_async, udp_tx_in_order) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - //shaping_stat_judge(line, 0, 0, 1, 100, 10000, 0, 0, 160, 0, SHAPING_DIR_OUT, profile_type_primary);//max latency is last 10 pkts - shaping_stat_judge(line, 0, 0, 1, 100, 10000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency + shaping_stat_judge(line, 0, 0, 1, 100, 10000, 0, 0, 160, 0, SHAPING_DIR_OUT, profile_type_primary);//max latency is last 10 pkts fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file fclose(stat_file); @@ -1115,16 +1096,13 @@ TEST(two_session_diff_priority_same_profile, udp_borrow_in_order) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 1, primary - //shaping_stat_judge(line, 1, 1, 1, 0, 0, 0, 0, 470, 0, SHAPING_DIR_OUT, profile_type_primary); - shaping_stat_judge(line, 1, 1, 1, 0, 0, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency + shaping_stat_judge(line, 1, 1, 1, 0, 0, 0, 0, 470, 0, SHAPING_DIR_OUT, profile_type_primary); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 2, borrow - //shaping_stat_judge(line, 1, 2, 2, 100, 10000, 0, 0, 470, 0, SHAPING_DIR_OUT, profile_type_borrow); - shaping_stat_judge(line, 1, 2, 2, 100, 10000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_borrow);//TODO: latency + shaping_stat_judge(line, 1, 2, 2, 100, 10000, 0, 0, 470, 0, SHAPING_DIR_OUT, profile_type_borrow); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//profile_id 2, primary - //shaping_stat_judge(line, 2, 2, 1, 100, 10000, 0, 0, 190, 0, SHAPING_DIR_OUT, profile_type_primary); - shaping_stat_judge(line, 2, 2, 1, 100, 10000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency + shaping_stat_judge(line, 2, 2, 1, 100, 10000, 0, 0, 190, 0, SHAPING_DIR_OUT, profile_type_primary); fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file @@ -1204,8 +1182,7 @@ TEST(two_session_same_rule, udp_tx_in_order) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - //shaping_stat_judge(line, 1, 1, 1, 200, 20000, 0, 0, 370, 0, SHAPING_DIR_OUT, profile_type_primary); - shaping_stat_judge(line, 1, 1, 1, 200, 20000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency + shaping_stat_judge(line, 1, 1, 1, 200, 20000, 0, 0, 370, 0, SHAPING_DIR_OUT, profile_type_primary); fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file fclose(stat_file); @@ -1385,8 +1362,7 @@ TEST(statistics, udp_drop_pkt) stat_file = fopen(SHAPING_STAT_FILE_NAME, "r"); memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file)); - //shaping_stat_judge(line, 0, 0, 1, SHAPING_SESSION_QUEUE_LEN+10, (SHAPING_SESSION_QUEUE_LEN+10)*100, 100, 0, 228, 0, SHAPING_DIR_OUT, profile_type_primary);//every queued pkt's latency is max - shaping_stat_judge(line, 0, 0, 1, SHAPING_SESSION_QUEUE_LEN+10, (SHAPING_SESSION_QUEUE_LEN+10)*100, 100, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency + shaping_stat_judge(line, 0, 0, 1, SHAPING_SESSION_QUEUE_LEN+10, (SHAPING_SESSION_QUEUE_LEN+10)*100, 100, 0, 228, 0, SHAPING_DIR_OUT, profile_type_primary);//every queued pkt's latency is max fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file fclose(stat_file); @@ -1456,12 +1432,10 @@ TEST(statistics, udp_queueing_pkt) memset(line, 0, sizeof(line)); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//stat data first sent - //shaping_stat_judge(line, 0, 0, 1, 10, 1000, 0, 90, 0, 1, SHAPING_DIR_OUT, profile_type_primary); - shaping_stat_judge(line, 0, 0, 1, 10, 1000, 0, 90, -1, 1, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency + shaping_stat_judge(line, 0, 0, 1, 10, 1000, 0, 90, 0, 1, SHAPING_DIR_OUT, profile_type_primary); ASSERT_TRUE(NULL != fgets(line, sizeof(line), stat_file));//stat data last sent - //shaping_stat_judge(line, 0, 0, 1, 90, 9000, 0, 0, 90, 0, SHAPING_DIR_OUT, profile_type_primary); - shaping_stat_judge(line, 0, 0, 1, 90, 9000, 0, 0, -1, 0, SHAPING_DIR_OUT, profile_type_primary);//TODO: latency + shaping_stat_judge(line, 0, 0, 1, 90, 9000, 0, 0, 90 + (STUB_TIME_INC_FOR_METRIC_SEND / 1000), 0, SHAPING_DIR_OUT, profile_type_primary); fclose(stat_file); stat_file = fopen(SHAPING_STAT_FILE_NAME, "w");//clear stat file -- cgit v1.2.3 From c80eb321bbe603ad25c7afe0bdea59bceca03cf1 Mon Sep 17 00:00:00 2001 From: liuchang Date: Tue, 28 Mar 2023 10:23:34 +0000 Subject: fix build break --- shaping/src/shaper_stat.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'shaping/src/shaper_stat.cpp') diff --git a/shaping/src/shaper_stat.cpp b/shaping/src/shaper_stat.cpp index 192fddd..6c9644b 100644 --- a/shaping/src/shaper_stat.cpp +++ b/shaping/src/shaper_stat.cpp @@ -213,7 +213,7 @@ void shaper_stat_queueing_pkt_dec(struct shaping_stat *stat, int rule_id, int pr void shaper_stat_max_latency_update(struct shaping_stat *stat, int rule_id, int profile_id, int priority, unsigned char direction, unsigned long long latency, int profile_type, int thread_id) { - long long old_latency; + unsigned long long old_latency; shaper_stat_tags_build(rule_id, profile_id, priority, profile_type); if (direction == SHAPING_DIR_IN) { -- cgit v1.2.3