Index: net/socket/client_socket_pool_base.cc |
=================================================================== |
--- net/socket/client_socket_pool_base.cc (revision 88319) |
+++ net/socket/client_socket_pool_base.cc (working copy) |
@@ -4,11 +4,16 @@ |
#include "net/socket/client_socket_pool_base.h" |
+#include <math.h> |
+#include <utility> |
+#include "base/command_line.h" |
#include "base/compiler_specific.h" |
#include "base/format_macros.h" |
+#include "base/logging.h" |
#include "base/message_loop.h" |
#include "base/metrics/stats_counters.h" |
#include "base/stl_util-inl.h" |
+#include "base/string_number_conversions.h" |
#include "base/string_util.h" |
#include "base/time.h" |
#include "base/values.h" |
@@ -32,6 +37,9 @@ |
// after a certain timeout has passed without receiving an ACK. |
bool g_connect_backup_jobs_enabled = true; |
+// Decay coef. |
+double g_tcp_socket_estimated_cwnd_decay_coef = 0; |
+ |
} // namespace |
namespace net { |
@@ -363,7 +371,12 @@ |
const Request* request, Group* group) { |
std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets(); |
std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end(); |
+ double max_benefit = -100000000; |
+ std::pair<int, int64> best_pair, current_pair; |
+ std::string debug_log(request->handle()->group_name()); |
+ double rtt_ms = 0; |
+ |
// Iterate through the idle sockets forwards (oldest to newest) |
// * Delete any disconnected ones. |
// * If we find a used idle socket, assign to |idle_socket|. At the end, |
@@ -379,11 +392,44 @@ |
if (it->socket->WasEverUsed()) { |
// We found one we can reuse! |
- idle_socket_it = it; |
+ double benefit = 0; |
Mike Belshe
2011/06/10 18:11:42
My thoughts on this function:
* You're calculatin
Gagan
2011/06/12 20:11:44
This function penalizes num_kb lesser than the old
|
+ int64 bytes_read = it->socket->NumBytesRead(); |
+ double num_kb = static_cast<double>(bytes_read) / 1024.0; |
+ |
+ int64 idle_time = (base::TimeTicks::Now() - it->start_time) |
+ .InMicroseconds(); |
+ double idle_time_mins = static_cast<double>(idle_time) / 60000000.0; |
+ |
+ if (g_tcp_socket_estimated_cwnd_decay_coef > 0) { |
+ double decay = pow(g_tcp_socket_estimated_cwnd_decay_coef, |
+ idle_time_mins); |
+ |
+ debug_log = StringPrintf("%s\n%f\t%f\t%f", debug_log.data(), |
+ num_kb, idle_time_mins, decay); |
+ // Cwnd of a socket is estimated as the number of kilobytes |
+ // transferred on it minus a decay function which is estimated as decay |
+ // coef exponentiated by idle time in minutes. |
+ // decay coef = 1 simulates no decay (cwnd validation disabled). |
+ // decay coef = 1.83 simulates a decay of 20KB in 5 minutes. |
+ benefit = num_kb - decay; |
+ } |
+ |
+ // Equality to prefer recently used connection. |
+ if (benefit >= max_benefit) { |
+ idle_socket_it = it; |
+ max_benefit = benefit; |
+ best_pair = std::make_pair(bytes_read, idle_time); |
+ rtt_ms = it->socket->GetConnectTimeMicros(); |
+ } |
} |
++it; |
} |
+ debug_log = StringPrintf("%s\nChose socket <%d %ld>\nRtt = %f", |
+ debug_log.data(), |
+ best_pair.first, best_pair.second, |
+ rtt_ms); |
+ LOG(ERROR) << "\n\n----------------------------------------" << debug_log; |
// If we haven't found an idle socket, that means there are no used idle |
// sockets. Pick the oldest (first) idle socket (FIFO). |
@@ -649,6 +695,31 @@ |
return old_value; |
} |
+// static |
+double ClientSocketPoolBaseHelper::SetSocketReusePolicy( |
+ ClientSocketReusePolicy policy) { |
+ double old_value = g_tcp_socket_estimated_cwnd_decay_coef; |
+ g_tcp_socket_estimated_cwnd_decay_coef = |
+ policy == USE_WARMEST_SOCKET ? 1 : |
+ (policy == USE_WARM_SOCKET ? 1.83841629 : 0); |
+ DLOG(INFO) << "Setting g_tcp_socket_estimated_cwnd_decay_coef = " |
+ << g_tcp_socket_estimated_cwnd_decay_coef; |
+ // TODO(gagan): Remove this. |
+ LOG(ERROR) << "Setting g_tcp_socket_estimated_cwnd_decay_coef = " |
+ << g_tcp_socket_estimated_cwnd_decay_coef; |
+ return old_value; |
+} |
+ |
+// static |
+double ClientSocketPoolBaseHelper::set_tcp_socket_estimated_cwnd_decay_coef( |
+ double coef) { |
+ DLOG(INFO) << "Setting g_tcp_socket_estimated_cwnd_decay_coef = " << coef; |
+ LOG(ERROR) << "Setting g_tcp_socket_estimated_cwnd_decay_coef = " << coef; |
+ double old_value = g_tcp_socket_estimated_cwnd_decay_coef; |
+ g_tcp_socket_estimated_cwnd_decay_coef = coef; |
+ return old_value; |
+} |
+ |
void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() { |
connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled; |
} |