BUG/MINOR: time: frequency counters are not totally accurate

When a frontend is rate-limited to 1000 connections per second, the
effective rate measured from the client is 999/s, and connections
experience an average response time of 99.5 ms with a standard
deviation of 2 ms.

The reason for this inaccuracy is that when computing frequency
counters, we use one part of the previous value proportional to the
number of milliseconds remaining in the current second. But even the
last millisecond still uses a part of the past value, which is wrong :
since we have a 1ms resolution, the last millisecond must be dedicated
only to filling the current second.

So we slightly adjust the algorithm to use 999/1000 of the past value
during the first millisecond, and 0/1000 of the past value during the
last millisecond.  We also slightly improve the computation by computing
the remaining time instead of the current time in tv_update_date(), so
that we don't have to negate the value in each frequency counter.

Now with the fix, the connection rate measured by both the client and
haproxy is a steady 1000/s, the average response time measured is 99.2ms
and more importantly, the standard deviation has been divided by 3 to
0.6 millisecond.

This fix should also be backported to 1.4 which has the same issue.
diff --git a/src/freq_ctr.c b/src/freq_ctr.c
index 28f5936..6dec970 100644
--- a/src/freq_ctr.c
+++ b/src/freq_ctr.c
@@ -47,7 +47,7 @@
 	if (past <= 1 && !curr)
 		return past; /* very low rate, avoid flapping */
 
-	return curr + mul32hi(past, ~curr_sec_ms_scaled);
+	return curr + mul32hi(past, ms_left_scaled);
 }
 
 /* returns the number of remaining events that can occur on this freq counter
@@ -59,7 +59,6 @@
 	unsigned int curr, past;
 	unsigned int age;
 
-	past = 0;
 	curr = 0;		
 	age = now.tv_sec - ctr->curr_sec;
 
@@ -69,7 +68,7 @@
 			curr = past;
 			past = ctr->prev_ctr;
 		}
-		curr += mul32hi(past, ~curr_sec_ms_scaled);
+		curr += mul32hi(past, ms_left_scaled);
 	}
 	curr += pend;
 
@@ -99,7 +98,7 @@
 			curr = past;
 			past = ctr->prev_ctr;
 		}
-		curr += mul32hi(past, ~curr_sec_ms_scaled);
+		curr += mul32hi(past, ms_left_scaled);
 	}
 	curr += pend;