1 /* See the DRL-LICENSE file for this file's software license. */
6 #include <netinet/in.h>
12 #include <sys/types.h>
15 #include "common_accounting.h"
19 simple_flow_table simple_table_create(common_accounting_t *common) {
20 simple_flow_table table = malloc(sizeof(struct sim_flow_table));
26 memset(table, 0, sizeof(struct sim_flow_table));
27 table->common = common;
28 table->hash_function = NULL;
30 gettimeofday(&table->common->last_update, NULL);
35 void simple_table_destroy(simple_flow_table table) {
39 int simple_table_sample(simple_flow_table table, const key_flow *key) {
40 /* Update aggregate. */
41 table->common->bytes_since += key->packet_size;
46 int simple_table_cleanup(simple_flow_table table) {
50 void simple_table_update_flows(simple_flow_table table, struct timeval now, double ewma_weight) {
52 double unweighted_rate;
54 time_delta = timeval_subtract(now, table->common->last_update);
56 if (time_delta <= 0) {
59 unweighted_rate = table->common->bytes_since / time_delta;
62 table->common->last_inst_rate = table->common->inst_rate;
63 table->common->inst_rate = unweighted_rate;
65 table->common->last_rate = table->common->rate;
67 /* If the rate is zero, then we don't know anything yet. Don't apply EWMA
69 if (table->common->rate == 0) {
70 table->common->rate = unweighted_rate;
72 table->common->rate = table->common->rate * ewma_weight +
73 unweighted_rate * (1 - ewma_weight);
76 table->common->bytes_since = 0;
77 table->common->last_update = now;
79 /* We don't track flow rates in this table. (Not suitable for FPS) */
80 table->common->max_flow_rate = 0;