summaryrefslogtreecommitdiff
path: root/shaping/src/shaper_stat.cpp
diff options
context:
space:
mode:
author刘畅 <[email protected]>2023-05-26 03:28:46 +0000
committer刘畅 <[email protected]>2023-05-26 03:28:46 +0000
commit57efeb63d5769c9f1b92b1266780968ad1c30d78 (patch)
treebd01030deab7f8c5716f7fbd325e1815d67e6c2e /shaping/src/shaper_stat.cpp
parent2fff2e3e8665cb04d73f032369b12e18b92bf575 (diff)
parent5fdcc33d27029b29cad7b4f212cdb3ab3abd81c0 (diff)
Merge branch 'update_shaping_priority_multi_rule' into 'rel'
use the highest priority when a session hit multi rules See merge request tango/shaping-engine!18
Diffstat (limited to 'shaping/src/shaper_stat.cpp')
-rw-r--r--shaping/src/shaper_stat.cpp151
1 files changed, 101 insertions, 50 deletions
diff --git a/shaping/src/shaper_stat.cpp b/shaping/src/shaper_stat.cpp
index 7e2afb5..74da337 100644
--- a/shaping/src/shaper_stat.cpp
+++ b/shaping/src/shaper_stat.cpp
@@ -1,3 +1,4 @@
+#include <cstring>
#include <stdio.h>
#include <time.h>
#include <sys/socket.h>
@@ -12,6 +13,8 @@
#define SHAPER_STAT_ROW_NAME "traffic_shaping_rule_hits"
+#define SHAPER_STAT_REFRESH_TIME_NS 200000000 //0.2s
+
struct shaper_stat_conf {
int enable_backgroud_thread;
int output_interval_ms;
@@ -52,9 +55,9 @@ 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
+ const char *column_name[] = {"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,
+ enum field_type column_type[] = {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]);
@@ -143,104 +146,152 @@ static void shaper_stat_tags_build(int vsys_id, int rule_id, int profile_id, int
return;
}
-void shaper_stat_drop_inc(struct shaping_stat *stat, int vsys_id, int rule_id, int profile_id,
- int priority, unsigned char direction, int pkt_len, int thread_id)
+static void shaper_stat_profile_metirc_refresh(struct shaping_stat *stat, int vsys_id, int thread_id, int rule_id, struct shaping_profile_info *profile, int profile_type, int need_update_guage)
{
- shaper_stat_tags_build(vsys_id, rule_id, profile_id, priority, SHAPING_PROFILE_TYPE_PRIMARY);
+ struct shaping_stat_for_profile *profile_stat = &profile->stat;
+ unsigned long long old_latency;
+
+ shaper_stat_tags_build(vsys_id, rule_id, profile->id, profile->priority, profile_type);
+ fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[IN_DROP_PKTS_IDX], SHAPER_STAT_ROW_NAME, profile_stat->in.drop_pkts, tags, TAG_IDX_MAX, thread_id);
+ fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[IN_PKTS_IDX], SHAPER_STAT_ROW_NAME, profile_stat->in.pkts, tags, TAG_IDX_MAX, thread_id);
+ fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[IN_BYTES_IDX], SHAPER_STAT_ROW_NAME, profile_stat->in.bytes, tags, TAG_IDX_MAX, thread_id);
+
+ fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[OUT_DROP_PKTS_IDX], SHAPER_STAT_ROW_NAME, profile_stat->out.drop_pkts, tags, TAG_IDX_MAX, thread_id);
+ fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[OUT_PKTS_IDX], SHAPER_STAT_ROW_NAME, profile_stat->out.pkts, tags, TAG_IDX_MAX, thread_id);
+ fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[OUT_BYTES_IDX], SHAPER_STAT_ROW_NAME, profile_stat->out.bytes, tags, TAG_IDX_MAX, thread_id);
+
+ old_latency = fieldstat_dynamic_table_metric_value_get(stat->instance, stat->table_id, stat->column_ids[IN_MAX_LATENCY_IDX], SHAPER_STAT_ROW_NAME, tags, TAG_IDX_MAX, thread_id);
+ if (profile_stat->in.max_latency > old_latency) {
+ fieldstat_dynamic_table_metric_value_set(stat->instance, stat->table_id, stat->column_ids[IN_MAX_LATENCY_IDX], SHAPER_STAT_ROW_NAME, profile_stat->in.max_latency, tags, TAG_IDX_MAX, thread_id);
+ }
- if (direction == SHAPING_DIR_IN) {
- fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[IN_DROP_PKTS_IDX], SHAPER_STAT_ROW_NAME, 1, tags, TAG_IDX_MAX, thread_id);
+ old_latency = fieldstat_dynamic_table_metric_value_get(stat->instance, stat->table_id, stat->column_ids[OUT_MAX_LATENCY_IDX], SHAPER_STAT_ROW_NAME, tags, TAG_IDX_MAX, thread_id);
+ if (profile_stat->out.max_latency > old_latency) {
+ fieldstat_dynamic_table_metric_value_set(stat->instance, stat->table_id, stat->column_ids[OUT_MAX_LATENCY_IDX], SHAPER_STAT_ROW_NAME, profile_stat->out.max_latency, tags, TAG_IDX_MAX, thread_id);
+ }
+
+ if (need_update_guage) {
+ fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[IN_QUEUE_LEN_IDX], SHAPER_STAT_ROW_NAME, profile_stat->in.queue_len, tags, TAG_IDX_MAX, thread_id);
+ fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[OUT_QUEUE_LEN_IDX], SHAPER_STAT_ROW_NAME, profile_stat->out.queue_len, tags, TAG_IDX_MAX, thread_id);
+ memset(profile_stat, 0, sizeof(struct shaping_stat_for_profile));
} else {
- fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[OUT_DROP_PKTS_IDX], SHAPER_STAT_ROW_NAME, 1, tags, TAG_IDX_MAX, thread_id);
+ profile_stat->in.pkts = 0;
+ profile_stat->in.bytes = 0;
+ profile_stat->in.drop_pkts = 0;
+ profile_stat->in.max_latency = 0;
+
+ profile_stat->out.pkts = 0;
+ profile_stat->out.bytes = 0;
+ profile_stat->out.drop_pkts = 0;
+ profile_stat->out.max_latency = 0;
}
return;
}
-void shaper_stat_forward_inc(struct shaping_stat *stat, int vsys_id, int rule_id, int profile_id,
- int priority, unsigned char direction, int pkt_len, int profile_type, int thread_id)
+void shaper_stat_refresh(struct shaping_stat *stat, struct shaping_flow *sf, int thread_id, int force)
{
- shaper_stat_tags_build(vsys_id, rule_id, profile_id, priority, profile_type);
+ struct shaping_rule_info *rule;
+ struct timespec curr_time;
+ int need_refresh = 0;
- if (direction == SHAPING_DIR_IN) {
- fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[IN_PKTS_IDX], SHAPER_STAT_ROW_NAME, 1, tags, TAG_IDX_MAX, thread_id);
- fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[IN_BYTES_IDX], SHAPER_STAT_ROW_NAME, pkt_len, tags, TAG_IDX_MAX, thread_id);
+ if (force) {
+ need_refresh = 1;
} else {
- fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[OUT_PKTS_IDX], SHAPER_STAT_ROW_NAME, 1, tags, TAG_IDX_MAX, thread_id);
- fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[OUT_BYTES_IDX], SHAPER_STAT_ROW_NAME, pkt_len, tags, TAG_IDX_MAX, thread_id);
+ clock_gettime(CLOCK_MONOTONIC, &curr_time);
+ if (curr_time.tv_sec - sf->stat_update_time.tv_sec > 0 || curr_time.tv_nsec - sf->stat_update_time.tv_nsec > SHAPER_STAT_REFRESH_TIME_NS) {
+ need_refresh = 1;
+ sf->stat_update_time = curr_time;
+ memcpy(&sf->stat_update_time, &curr_time, sizeof(struct timespec));
+ }
+ }
+
+ if (!need_refresh) {
+ return;
+ }
+
+ int need_update_guage = sf->processed_pkts > CONFIRM_PRIORITY_PKTS ? 1 : 0;
+
+ for (int i = 0; i < sf->rule_num; i++) {
+ rule = &sf->matched_rule_infos[i];
+ shaper_stat_profile_metirc_refresh(stat, rule->vsys_id, thread_id, rule->id, &rule->primary, SHAPING_PROFILE_TYPE_PRIMARY, need_update_guage);
+
+ for (int j = 0; j < rule->borrowing_num; j++) {
+ shaper_stat_profile_metirc_refresh(stat, rule->vsys_id, thread_id, rule->id, &rule->borrowing[j], SHAPING_PROFILE_TYPE_BORROW, need_update_guage);
+ }
}
return;
}
-void shaper_stat_forward_all_rule_inc(struct shaping_stat *stat, struct shaping_flow *sf, unsigned char direction, int pkt_len, int thread_id)
+void shaper_stat_drop_inc(struct shaping_stat_for_profile *profile_stat, unsigned char direction, 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, rule->vsys_id, rule->id, rule->primary.id, rule->primary.priority, direction, pkt_len, SHAPING_PROFILE_TYPE_PRIMARY, thread_id);
+ if (direction == SHAPING_DIR_IN) {
+ profile_stat->in.drop_pkts++;
+ } else {
+ profile_stat->out.drop_pkts++;
}
return;
}
-void shaper_stat_queueing_session_inc(struct shaping_stat *stat, int vsys_id, int rule_id, int profile_id, int priority, int profile_type, int thread_id)
+void shaper_stat_forward_inc(struct shaping_stat_for_profile *profile_stat, unsigned char direction, int pkt_len, int thread_id)
{
- shaper_stat_tags_build(vsys_id, rule_id, profile_id, priority, profile_type);
- fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[QUEUEING_SESSIONS_IDX], SHAPER_STAT_ROW_NAME, 1, tags, TAG_IDX_MAX, thread_id);
+ if (direction == SHAPING_DIR_IN) {
+ profile_stat->in.pkts++;
+ profile_stat->in.bytes += pkt_len;
+ } else {
+ profile_stat->out.pkts++;
+ profile_stat->out.bytes += pkt_len;
+ }
+
return;
}
-void shaper_stat_queueing_session_dec(struct shaping_stat *stat, int vsys_id, int rule_id, int profile_id, int priority, int profile_type, int thread_id)
+void shaper_stat_forward_all_rule_inc(struct shaping_stat *stat, struct shaping_flow *sf, unsigned char direction, int pkt_len, int thread_id)
{
- shaper_stat_tags_build(vsys_id, rule_id, profile_id, priority, profile_type);
- fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[QUEUEING_SESSIONS_IDX], SHAPER_STAT_ROW_NAME, -1, tags, TAG_IDX_MAX, 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(&rule->primary.stat, direction, pkt_len, thread_id);
+ }
+
return;
}
-void shaper_stat_queueing_pkt_inc(struct shaping_stat *stat, int vsys_id, 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_inc(struct shaping_stat_for_profile *profile_stat, unsigned char direction, int thread_id)
{
- shaper_stat_tags_build(vsys_id, rule_id, profile_id, priority, profile_type);
if (direction == SHAPING_DIR_IN) {
- fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[IN_QUEUE_LEN_IDX], SHAPER_STAT_ROW_NAME, 1, tags, TAG_IDX_MAX, thread_id);
+ profile_stat->in.queue_len++;
} else {
- fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[OUT_QUEUE_LEN_IDX], SHAPER_STAT_ROW_NAME, 1, tags, TAG_IDX_MAX, thread_id);
+ profile_stat->out.queue_len++;
}
return;
}
-void shaper_stat_queueing_pkt_dec(struct shaping_stat *stat, int vsys_id, 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_for_profile *profile_stat, unsigned char direction, int thread_id)
{
- shaper_stat_tags_build(vsys_id, rule_id, profile_id, priority, profile_type);
if (direction == SHAPING_DIR_IN) {
- fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[IN_QUEUE_LEN_IDX], SHAPER_STAT_ROW_NAME, -1, tags, TAG_IDX_MAX, thread_id);
+ profile_stat->in.queue_len--;
} else {
- fieldstat_dynamic_table_metric_value_incrby(stat->instance, stat->table_id, stat->column_ids[OUT_QUEUE_LEN_IDX], SHAPER_STAT_ROW_NAME, -1, tags, TAG_IDX_MAX, thread_id);
+ profile_stat->out.queue_len--;
}
return;
}
-void shaper_stat_max_latency_update(struct shaping_stat *stat, int vsys_id, int rule_id, int profile_id,
- int priority, unsigned char direction, unsigned long long latency, int profile_type, int thread_id)
+void shaper_stat_max_latency_update(struct shaping_stat_for_profile *profile_stat, unsigned char direction, unsigned long long latency, int thread_id)
{
- unsigned long long old_latency;
-
- shaper_stat_tags_build(vsys_id, rule_id, profile_id, priority, profile_type);
if (direction == SHAPING_DIR_IN) {
- old_latency = fieldstat_dynamic_table_metric_value_get(stat->instance, stat->table_id, stat->column_ids[IN_MAX_LATENCY_IDX], SHAPER_STAT_ROW_NAME, 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], SHAPER_STAT_ROW_NAME, latency, tags, TAG_IDX_MAX, thread_id);
+ if (profile_stat->in.max_latency < latency) {
+ profile_stat->in.max_latency = latency;
}
} else {
- old_latency = fieldstat_dynamic_table_metric_value_get(stat->instance, stat->table_id, stat->column_ids[OUT_MAX_LATENCY_IDX], SHAPER_STAT_ROW_NAME, 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], SHAPER_STAT_ROW_NAME, latency, tags, TAG_IDX_MAX, thread_id);
+ if (profile_stat->out.max_latency < latency) {
+ profile_stat->out.max_latency = latency;
}
}