Updates to autotools for library detection
[distributedratelimiting.git] / drl / estimate.c
index 3cbf95a..24adbbf 100644 (file)
@@ -63,31 +63,14 @@ static double allocate_fps_under_limit(identity_t *ident, uint32_t target, doubl
     double ideal_weight;
     double total_weight = peer_weights + ident->last_localweight;
 
-    if (target >= ident->limit) {
+    if (target >= ident->effective_limit) {
         ideal_weight = total_weight;
     } else if (target <= 0) {
         ideal_weight = 0; // no flows here
     } else {
-        ideal_weight = ((double)target / (double)ident->limit) * total_weight;
+        ideal_weight = ((double)target / (double)ident->effective_limit) * total_weight;
     }
 
-#if 0
-    else if (peer_weights <= 0) {
-#if 0
-        // doesn't matter what we pick as our weight, so pick 1 / N.
-        ideal_weight = MAX_FLOW_SCALING_FACTOR / (remote_count(ident->i_handle) + 1);
-#endif
-        ideal_weight = ((double)target / (double)ident->limit) * total_weight;
-    } else {
-#if 0
-        double divisor = (double) ident->limit - (double) target;
-        ideal_weight = ((double) target * peer_weights) / divisor;
-#else
-        ideal_weight = ((double)target / (double)ident->limit) * total_weight;
-#endif
-    }
-#endif
-
     return ideal_weight;
 }
 
@@ -180,7 +163,7 @@ static uint32_t allocate_fps(identity_t *ident, double total_weight,
 
     if (ident->dampen_state == DAMPEN_TEST) {
         int64_t rate_delta = (int64_t) table->inst_rate - (int64_t) table->last_inst_rate;
-        double threshold = (double) ident->limit * (double) LARGE_INCREASE_PERCENTAGE / 10;
+        double threshold = (double) ident->effective_limit * (double) LARGE_INCREASE_PERCENTAGE / 10;
 
         if (rate_delta > threshold) {
             ident->dampen_state = DAMPEN_PASSED;
@@ -282,7 +265,7 @@ static uint32_t allocate_fps(identity_t *ident, double total_weight,
     /* Convert weight value into a rate limit.  If there is no measureable
      * weight, do a L/n allocation. */
     if (total_weight > 0) {
-        resulting_limit = (uint32_t) (ident->localweight * ident->limit / total_weight);
+        resulting_limit = (uint32_t) (ident->localweight * ident->effective_limit / total_weight);
     } else {
         resulting_limit = (uint32_t) (ident->limit / (ident->comm.remote_node_count + 1));
     }
@@ -313,7 +296,7 @@ static void allocate_fps_pretend(identity_t *ident, double total_weight,
 
     if (ident->dampen_state_copy == DAMPEN_TEST) {
         int64_t rate_delta = (int64_t) table->inst_rate - (int64_t) table->last_inst_rate;
-        double threshold = (double) ident->limit * (double) LARGE_INCREASE_PERCENTAGE / 10;
+        double threshold = (double) ident->effective_limit * (double) LARGE_INCREASE_PERCENTAGE / 10;
 
         if (rate_delta > threshold) {
             ident->dampen_state_copy = DAMPEN_PASSED;
@@ -393,7 +376,7 @@ static void allocate_fps_pretend(identity_t *ident, double total_weight,
     /* Convert weight value into a rate limit.  If there is no measureable
      * weight, do a L/n allocation. */
     if (total_weight > 0) {
-        resulting_limit = (uint32_t) (ident->localweight_copy * ident->limit / total_weight);
+        resulting_limit = (uint32_t) (ident->localweight_copy * ident->effective_limit / total_weight);
     } else {
         resulting_limit = (uint32_t) (ident->limit / (ident->comm.remote_node_count + 1));
     }
@@ -404,168 +387,12 @@ static void allocate_fps_pretend(identity_t *ident, double total_weight,
 
 #endif
 
-/**
- * Determines the amount of FPS weight to allocate to the identity during each
- * estimate interval.  Note that total_weight includes local weight.
- */
-static uint32_t allocate_fps_old(identity_t *ident, double total_weight) {
-    common_accounting_t *ftable = &ident->common; /* Common flow table info */
-    uint32_t local_rate = ftable->rate;
-    uint32_t ideallocal = 0;
-    double peer_weights; /* sum of weights of all other limiters */
-    double idealweight = 0;
-    double last_portion = 0;
-    double this_portion = 0;
-
-    static int dampen = 0;
-    int dampen_increase = 0;
-
-    double ideal_under = 0;
-    double ideal_over = 0;
-
-    int regime = 0;
-
-    /* two cases:
-       1. the aggregate is < limit
-       2. the aggregate is >= limit
-       */
-    peer_weights = total_weight - ident->last_localweight;
-    if (peer_weights < 0) {
-        peer_weights = 0;
-    }
-
-    if (dampen == 1) {
-        int64_t rate_delta =
-            (int64_t) ftable->inst_rate - (int64_t) ftable->last_inst_rate;
-        double threshold =
-            (double) ident->limit * (double) LARGE_INCREASE_PERCENTAGE / 10;
-
-        if (rate_delta > threshold) {
-            dampen_increase = 1;
-            printlog(LOG_DEBUG, "DAMPEN: delta(%.3f) thresh(%.3f)\n",
-                     rate_delta, threshold);
-        }
-    }
-
-    if (local_rate <= 0) {
-        idealweight = 0;
-    } else if (dampen_increase == 0 &&
-               (ident->locallimit <= 0 || local_rate < close_enough(ident->locallimit) || ident->flowstart)) {
-        /* We're under the limit - all flows are bottlenecked. */
-        idealweight = allocate_fps_under_limit(ident, local_rate, peer_weights);
-        ideal_over = allocate_fps_over_limit(ident);
-        ideal_under = idealweight;
-
-        if (ideal_over < idealweight) {
-            idealweight = ideal_over;
-            regime = 3;
-            dampen = 2;
-        } else {
-            regime = 1;
-            dampen = 0;
-        }
-
-        /* Apply EWMA */
-        ident->localweight = (ident->localweight * ident->ewma_weight +
-                              idealweight * (1 - ident->ewma_weight));
-        
-    } else {
-        idealweight = allocate_fps_over_limit(ident);
-        
-        /* Apply EWMA */
-        ident->localweight = (ident->localweight * ident->ewma_weight +
-                              idealweight * (1 - ident->ewma_weight));
-
-        /* This is the portion of the total weight in the system that was caused
-         * by this limiter in the last interval. */
-        last_portion = ident->last_localweight / total_weight;
-
-        /* This is the fraction of the total weight in the system that our
-         * proposed value for idealweight would use. */
-        this_portion = ident->localweight / (peer_weights + ident->localweight);
-
-        /* Dampen the large increase the first time... */
-        if (dampen == 0 && (this_portion - last_portion > LARGE_INCREASE_PERCENTAGE)) {
-            ident->localweight = ident->last_localweight + (LARGE_INCREASE_PERCENTAGE * total_weight);
-            dampen = 1;
-        } else {
-            dampen = 2;
-        }
-
-        ideal_under = allocate_fps_under_limit(ident, local_rate, peer_weights);
-        ideal_over = idealweight;
-
-        regime = 2;
-    }
-
-    /* Convert weight into a rate - add in our new local weight */
-    ident->total_weight = total_weight = ident->localweight + peer_weights;
-
-    /* compute local allocation:
-       if there is traffic elsewhere, use the weights
-       otherwise do a L/n allocation */
-    if (total_weight > 0) {
-    //if (peer_weights > 0) {
-        ideallocal = (uint32_t) (ident->localweight * ident->limit / total_weight);
-    } else {
-        ideallocal = ident->limit / (ident->comm.remote_node_count + 1);
-    }
-
-    printlog(LOG_DEBUG, "%.3f ActualWeight\n", ident->localweight);
-
-    printlog(LOG_DEBUG, "%.3f %.3f %.3f %.3f  Under / Over / Actual / Rate\n",
-            ideal_under / (ideal_under + peer_weights),
-            ideal_over / (ideal_over + peer_weights),
-            ident->localweight / (ident->localweight + peer_weights),
-            (double) local_rate / (double) ident->limit);
-
-    printlog(LOG_DEBUG, "%.3f %.3f IdealUnd IdealOve\n",ideal_under,ideal_over);
-
-    if (system_loglevel == LOG_DEBUG) {
-        printf("local_rate: %d, idealweight: %.3f, localweight: %.3f, total_weight: %.3f\n",
-            local_rate, idealweight, ident->localweight, total_weight);
-    }
-
-#if 0
-    if (printcounter <= 0) {
-        struct timeval tv;
-        double time_now;
-
-        gettimeofday(&tv, NULL);
-        time_now = (double) tv.tv_sec + (double) ((double) tv.tv_usec / (double) 1000000);
-
-        printlog(LOG_WARN, "%.2f %d %.2f %.2f %.2f %d %d %d %d %d %d %d %d ", time_now, ftable->inst_rate,
-            idealweight, ident->localweight, total_weight, ftable->num_flows, ftable->num_flows_5k,
-            ftable->num_flows_10k, ftable->num_flows_20k, ftable->num_flows_50k, ftable->avg_rate,
-            ftable->max_flow_rate, ftable->max_flow_rate_flow_hash);
-
-        printcounter = PRINT_COUNTER_RESET;
-    } else {
-        printcounter -= 1;
-    }
-
-    //printf("Dampen: %d, dampen_increase: %d, peer_weights: %.3f, regime: %d\n",
-    //       dampen, dampen_increase, peer_weights, regime);
-
-    if (regime == 3) {
-        printlog(LOG_DEBUG, "MIN: min said to use flow counting, which was %.3f when other method said %.3f.\n",
-                 ideal_over, ideal_under);
-    }
-    See print_statistics()
-#endif
-
-    printlog(LOG_DEBUG, "ideallocal is %d\n", ideallocal);
-
-    return(ideallocal);
-}
-
 /**
  * Determines the local drop probability for a GRD identity every estimate
  * interval.
  */
 static double allocate_grd(identity_t *ident, double aggdemand) {
     double dropprob;
-    double global_limit = ident->limit;
     double min_dropprob = ident->drop_prob * GRD_BIG_DROP;
 
     struct timeval tv;
@@ -575,8 +402,8 @@ static double allocate_grd(identity_t *ident, double aggdemand) {
     gettimeofday(&tv, NULL);
     time_now = (double) tv.tv_sec + (double) ((double) tv.tv_usec / (double) 1000000);
 
-    if (aggdemand > global_limit) {
-        dropprob = (aggdemand-global_limit)/aggdemand;
+    if (aggdemand > ident->effective_limit) {
+        dropprob = (aggdemand - ident->effective_limit) / aggdemand;
     } else {
         dropprob = 0.0;
     }
@@ -615,18 +442,12 @@ static double allocate_grd(identity_t *ident, double aggdemand) {
  */
 static void allocate(limiter_t *limiter, identity_t *ident) {
     /* Represents aggregate rate for GRD and aggregate weight for FPS. */
-    double comm_val = 0;
-
-    /* Read comm_val from comm layer. */
-    if (limiter->policy == POLICY_FPS) {
-        read_comm(&ident->comm, &comm_val,
-                ident->total_weight / (double) (ident->comm.remote_node_count + 1));
-    } else {
-        read_comm(&ident->comm, &comm_val,
-                (double) (ident->limit / (double) (ident->comm.remote_node_count + 1)));
-    }
-    printlog(LOG_DEBUG, "%.3f Aggregate weight/rate (FPS/GRD)\n", comm_val);
+    double aggregate = 0;
 
+    /* Read aggregate from comm layer. */
+    read_comm(&aggregate, &ident->effective_limit, &ident->comm, ident->limit);
+    printlog(LOG_DEBUG, "%.3f Aggregate weight/rate (FPS/GRD)\n", aggregate);
+    
     /* Experimental printing. */
     printlog(LOG_DEBUG, "%.3f \t Kbps used rate. ID:%d\n",
              (double) ident->common.rate / (double) 128, ident->id);
@@ -635,19 +456,19 @@ static void allocate(limiter_t *limiter, identity_t *ident) {
     if (limiter->policy == POLICY_FPS) {
 #ifdef SHADOW_ACCTING
 
-        allocate_fps_pretend(ident, comm_val, &ident->shadow_common, "SHADOW-ID");
+        allocate_fps_pretend(ident, aggregate, &ident->shadow_common, "SHADOW-ID");
 
         ident->last_localweight_copy = ident->localweight_copy;
 #endif
 
-        ident->locallimit = allocate_fps(ident, comm_val, &ident->common, "ID");
+        ident->locallimit = allocate_fps(ident, aggregate, &ident->common, "ID");
         ident->last_localweight = ident->localweight;
 
         /* Update other limiters with our weight by writing to comm layer. */
         write_local_value(&ident->comm, ident->localweight);
     } else {
         ident->last_drop_prob = ident->drop_prob;
-        ident->drop_prob = allocate_grd(ident, comm_val);
+        ident->drop_prob = allocate_grd(ident, aggregate);
         
         /* Update other limiters with our rate by writing to comm layer. */
         write_local_value(&ident->comm, ident->common.rate);